[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/library/ezc/Base/src/ -> features.php (source)

   1  <?php
   2  /**
   3   * File containing the ezcBaseFeatures class.
   4   *
   5   * @package Base
   6   * @version 1.8
   7   * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
   8   * @license http://ez.no/licenses/new_bsd New BSD License
   9   */
  10  
  11  /**
  12   * Provides methods needed to check for features.
  13   *
  14   * Example:
  15   * <code>
  16   * <?php
  17   * echo "supports uid: " . ezcBaseFeatures::supportsUserId() . "\n";
  18   * echo "supports symlink: " . ezcBaseFeatures::supportsSymLink() . "\n";
  19   * echo "supports hardlink: " . ezcBaseFeatures::supportsLink() . "\n";
  20   * echo "has imagemagick identify: " . ezcBaseFeatures::hasImageIdentify() . "\n";
  21   * echo " identify path: " . ezcBaseFeatures::getImageIdentifyExecutable() . "\n";
  22   * echo "has imagemagick convert: " . ezcBaseFeatures::hasImageConvert() . "\n";
  23   * echo " convert path: " . ezcBaseFeatures::getImageConvertExecutable() . "\n";
  24   * echo "has gzip extension: " . ezcBaseFeatures::hasExtensionSupport( 'zlib' ) . "\n";
  25   * echo "has pdo_mysql 1.0.2: " . ezcBaseFeatures::hasExtensionSupport( 'pdo_mysql', '1.0.2' ) . "\n"
  26   * ?>
  27   * </code>
  28   *
  29   * @package Base
  30   * @version 1.8
  31   */
  32  class ezcBaseFeatures
  33  {
  34      /**
  35        * Used to store the path of the ImageMagick convert utility.
  36        *
  37        * It is initialized in the {@link getImageConvertExecutable()} function.
  38        *
  39        * @var string
  40        */
  41      private static $imageConvert = null;
  42  
  43      /**
  44        * Used to store the path of the ImageMagick identify utility.
  45        *
  46        * It is initialized in the {@link getImageIdentifyExecutable()} function.
  47        *
  48        * @var string
  49        */
  50      private static $imageIdentify = null;
  51  
  52      /**
  53        * Used to store the operating system.
  54        *
  55        * It is initialized in the {@link os()} function.
  56        *
  57        * @var string
  58        */
  59      private static $os = null;
  60  
  61      /**
  62       * Determines if hardlinks are supported.
  63       *
  64       * @return bool
  65       */
  66      public static function supportsLink()
  67      {
  68          return function_exists( 'link' );
  69      }
  70  
  71      /**
  72       * Determines if symlinks are supported.
  73       *
  74       * @return bool
  75       */
  76      public static function supportsSymLink()
  77      {
  78          return function_exists( 'symlink' );
  79      }
  80  
  81      /**
  82       * Determines if posix uids are supported.
  83       *
  84       * @return bool
  85       */
  86      public static function supportsUserId()
  87      {
  88          return function_exists( 'posix_getpwuid' );
  89      }
  90  
  91      /**
  92       * Determines if the ImageMagick convert utility is installed.
  93       *
  94       * @return bool
  95       */
  96      public static function hasImageConvert()
  97      {
  98          return !is_null( self::getImageConvertExecutable() );
  99      }
 100  
 101      /**
 102       * Returns the path to the ImageMagick convert utility.
 103       *
 104       * On Linux, Unix,... it will return something like: /usr/bin/convert
 105       * On Windows it will return something like: C:\Windows\System32\convert.exe
 106       *
 107       * @return string
 108       */
 109      public static function getImageConvertExecutable()
 110      {
 111          if ( !is_null( self::$imageConvert ) )
 112          {
 113              return self::$imageConvert;
 114          }
 115          return ( self::$imageConvert = self::findExecutableInPath( 'convert' ) );
 116      }
 117  
 118      /**
 119       * Determines if the ImageMagick identify utility is installed.
 120       *
 121       * @return bool
 122       */
 123      public static function hasImageIdentify()
 124      {
 125          return !is_null( self::getImageIdentifyExecutable() );
 126      }
 127  
 128      /**
 129       * Returns the path to the ImageMagick identify utility.
 130       *
 131       * On Linux, Unix,... it will return something like: /usr/bin/identify
 132       * On Windows it will return something like: C:\Windows\System32\identify.exe
 133       *
 134       * @return string
 135       */
 136      public static function getImageIdentifyExecutable()
 137      {
 138          if ( !is_null( self::$imageIdentify ) )
 139          {
 140              return self::$imageIdentify;
 141          }
 142          return ( self::$imageIdentify = self::findExecutableInPath( 'identify' ) );
 143      }
 144  
 145      /**
 146       * Determines if the specified extension is loaded.
 147       *
 148       * If $version is specified, the specified extension will be tested also
 149       * against the version of the loaded extension.
 150       *
 151       * Examples:
 152       * <code>
 153       * hasExtensionSupport( 'gzip' );
 154       * </code>
 155       * will return true if gzip extension is loaded.
 156       *
 157       * <code>
 158       * hasExtensionSupport( 'pdo_mysql', '1.0.2' );
 159       * </code>
 160       * will return true if pdo_mysql extension is loaded and its version is at least 1.0.2.
 161       *
 162       * @param string $extension
 163       * @param string $version
 164       * @return bool
 165       */
 166      public static function hasExtensionSupport( $extension, $version = null )
 167      {
 168          if ( is_null( $version ) )
 169          {
 170              return extension_loaded( $extension );
 171          }
 172          return extension_loaded( $extension ) && version_compare( phpversion( $extension ), $version, ">=" ) ;
 173      }
 174  
 175      /**
 176       * Determines if the specified function is available.
 177       *
 178       * Examples:
 179       * <code>
 180       * ezcBaseFeatures::hasFunction( 'imagepstext' );
 181       * </code>
 182       * will return true if support for Type 1 fonts is available with your GD
 183       * extension.
 184       *
 185       * @param string $functionName
 186       * @return bool
 187       */
 188      public static function hasFunction( $functionName )
 189      {
 190          return function_exists( $functionName );
 191      }
 192  
 193      /**
 194       * Returns if a given class exists.
 195       * Checks for a given class name and returns if this class exists or not.
 196       * Catches the ezcBaseAutoloadException and returns false, if it was thrown.
 197       *
 198       * @param string $className The class to check for.
 199       * @param bool $autoload True to use __autoload(), otherwise false.
 200       * @return bool True if the class exists. Otherwise false.
 201       */
 202      public static function classExists( $className, $autoload = true )
 203      {
 204          try
 205          {
 206              if ( class_exists( $className, $autoload ) )
 207              {
 208                  return true;
 209              }
 210              return false;
 211          }
 212          catch ( ezcBaseAutoloadException $e )
 213          {
 214              return false;
 215          }
 216      }
 217  
 218      /**
 219       * Returns the operating system on which PHP is running.
 220       *
 221       * This method returns a sanitized form of the OS name, example
 222       * return values are "Windows", "Mac", "Linux" and "FreeBSD". In
 223       * all other cases it returns the value of the internal PHP constant
 224       * PHP_OS.
 225       *
 226       * @return string
 227       */
 228      public static function os()
 229      {
 230          if ( is_null( self::$os ) )
 231          {
 232              $uname = php_uname( 's' );
 233              if ( substr( $uname, 0, 7 ) == 'Windows' )
 234              {
 235                  self::$os = 'Windows';
 236              }
 237              elseif ( substr( $uname, 0, 3 ) == 'Mac' )
 238              {
 239                  self::$os = 'Mac';
 240              }
 241              elseif ( strtolower( $uname ) == 'linux' )
 242              {
 243                  self::$os = 'Linux';
 244              }
 245              elseif ( strtolower( substr( $uname, 0, 7 ) ) == 'freebsd' )
 246              {
 247                  self::$os = 'FreeBSD';
 248              }
 249              else
 250              {
 251                  self::$os = PHP_OS;
 252              }
 253          }
 254          return self::$os;
 255      }
 256  
 257      /**
 258       * Returns the path of the specified executable, if it can be found in the system's path.
 259       *
 260       * It scans the PATH enviroment variable based on the OS to find the
 261       * $fileName. For Windows, the path is with \, not /.  If $fileName is not
 262       * found, it returns null.
 263       *
 264       * @todo consider using getenv( 'PATH' ) instead of $_ENV['PATH']
 265       *       (but that won't work under IIS)
 266       *
 267       * @param string $fileName
 268       * @return string
 269       */
 270      public static function findExecutableInPath( $fileName )
 271      {
 272          if ( array_key_exists( 'PATH', $_ENV ) )
 273          {
 274              $envPath = trim( $_ENV['PATH'] );
 275          }
 276          else if ( ( $envPath = getenv( 'PATH' ) ) !== false )
 277          {
 278              $envPath = trim( $envPath );
 279          }
 280          if ( is_string( $envPath ) && strlen( trim( $envPath ) ) == 0 )
 281          {
 282              $envPath = false;
 283          }
 284  
 285          switch ( self::os() )
 286          {
 287              case 'Unix':
 288              case 'FreeBSD':
 289              case 'Mac':
 290              case 'MacOS':
 291              case 'Darwin':
 292              case 'Linux':
 293              case 'SunOS':
 294                  if ( $envPath )
 295                  {
 296                      $dirs = explode( ':', $envPath );
 297                      foreach ( $dirs as $dir )
 298                      {
 299                          // The @-operator is used here mainly to avoid
 300                          // open_basedir warnings. If open_basedir (or any other
 301                          // circumstance) prevents the desired file from being
 302                          // accessed, it is fine for file_exists() to return
 303                          // false, since it is useless for use then, anyway.
 304                          if ( file_exists( "{$dir}/{$fileName}" ) )
 305                          {
 306                              return "{$dir}/{$fileName}";
 307                          }
 308                      }
 309                  }
 310                  // The @-operator is used here mainly to avoid open_basedir
 311                  // warnings. If open_basedir (or any other circumstance)
 312                  // prevents the desired file from being accessed, it is fine
 313                  // for file_exists() to return false, since it is useless for
 314                  // use then, anyway.
 315                  elseif ( @file_exists( "./{$fileName}" ) )
 316                  {
 317                      return $fileName;
 318                  }
 319                  break;
 320              case 'Windows':
 321                  if ( $envPath )
 322                  {
 323                      $dirs = explode( ';', $envPath );
 324                      foreach ( $dirs as $dir )
 325                      {
 326                          // The @-operator is used here mainly to avoid
 327                          // open_basedir warnings. If open_basedir (or any other
 328                          // circumstance) prevents the desired file from being
 329                          // accessed, it is fine for file_exists() to return
 330                          // false, since it is useless for use then, anyway.
 331                          if ( @file_exists( "{$dir}\\{$fileName}.exe" ) )
 332                          {
 333                              return "{$dir}\\{$fileName}.exe";
 334                          }
 335                      }
 336                  }
 337                  // The @-operator is used here mainly to avoid open_basedir
 338                  // warnings. If open_basedir (or any other circumstance)
 339                  // prevents the desired file from being accessed, it is fine
 340                  // for file_exists() to return false, since it is useless for
 341                  // use then, anyway.
 342                  elseif ( @file_exists( "{$fileName}.exe" ) )
 343                  {
 344                      return "{$fileName}.exe";
 345                  }
 346                  break;
 347          }
 348          return null;
 349      }
 350  
 351      /**
 352       * Reset the cached information. 
 353       * 
 354       * @return void
 355       * @access private
 356       * @ignore
 357       */
 358      public static function reset()
 359      {
 360          self::$imageIdentify = null;
 361          self::$imageConvert  = null;
 362          self::$os            = null;
 363      }
 364  }
 365  ?>


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7