3 * File containing the ezcBaseFeatures class.
7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
12 * Provides methods needed to check for features.
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"
35 * Used to store the path of the ImageMagick convert utility.
37 * It is initialized in the {@link getImageConvertExecutable()} function.
41 private static $imageConvert = null;
44 * Used to store the path of the ImageMagick identify utility.
46 * It is initialized in the {@link getImageIdentifyExecutable()} function.
50 private static $imageIdentify = null;
53 * Used to store the operating system.
55 * It is initialized in the {@link os()} function.
59 private static $os = null;
62 * Determines if hardlinks are supported.
66 public static function supportsLink()
68 return function_exists( 'link' );
72 * Determines if symlinks are supported.
76 public static function supportsSymLink()
78 return function_exists( 'symlink' );
82 * Determines if posix uids are supported.
86 public static function supportsUserId()
88 return function_exists( 'posix_getpwuid' );
92 * Determines if the ImageMagick convert utility is installed.
96 public static function hasImageConvert()
98 return !is_null( self
::getImageConvertExecutable() );
102 * Returns the path to the ImageMagick convert utility.
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
109 public static function getImageConvertExecutable()
111 if ( !is_null( self
::$imageConvert ) )
113 return self
::$imageConvert;
115 return ( self
::$imageConvert = self
::findExecutableInPath( 'convert' ) );
119 * Determines if the ImageMagick identify utility is installed.
123 public static function hasImageIdentify()
125 return !is_null( self
::getImageIdentifyExecutable() );
129 * Returns the path to the ImageMagick identify utility.
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
136 public static function getImageIdentifyExecutable()
138 if ( !is_null( self
::$imageIdentify ) )
140 return self
::$imageIdentify;
142 return ( self
::$imageIdentify = self
::findExecutableInPath( 'identify' ) );
146 * Determines if the specified extension is loaded.
148 * If $version is specified, the specified extension will be tested also
149 * against the version of the loaded extension.
153 * hasExtensionSupport( 'gzip' );
155 * will return true if gzip extension is loaded.
158 * hasExtensionSupport( 'pdo_mysql', '1.0.2' );
160 * will return true if pdo_mysql extension is loaded and its version is at least 1.0.2.
162 * @param string $extension
163 * @param string $version
166 public static function hasExtensionSupport( $extension, $version = null )
168 if ( is_null( $version ) )
170 return extension_loaded( $extension );
172 return extension_loaded( $extension ) && version_compare( phpversion( $extension ), $version, ">=" ) ;
176 * Determines if the specified function is available.
180 * ezcBaseFeatures::hasFunction( 'imagepstext' );
182 * will return true if support for Type 1 fonts is available with your GD
185 * @param string $functionName
188 public static function hasFunction( $functionName )
190 return function_exists( $functionName );
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.
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.
202 public static function classExists( $className, $autoload = true )
206 if ( class_exists( $className, $autoload ) )
212 catch ( ezcBaseAutoloadException
$e )
219 * Returns the operating system on which PHP is running.
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
228 public static function os()
230 if ( is_null( self
::$os ) )
232 $uname = php_uname( 's' );
233 if ( substr( $uname, 0, 7 ) == 'Windows' )
235 self
::$os = 'Windows';
237 elseif ( substr( $uname, 0, 3 ) == 'Mac' )
241 elseif ( strtolower( $uname ) == 'linux' )
245 elseif ( strtolower( substr( $uname, 0, 7 ) ) == 'freebsd' )
247 self
::$os = 'FreeBSD';
258 * Returns the path of the specified executable, if it can be found in the system's path.
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.
264 * @todo consider using getenv( 'PATH' ) instead of $_ENV['PATH']
265 * (but that won't work under IIS)
267 * @param string $fileName
270 public static function findExecutableInPath( $fileName )
272 if ( array_key_exists( 'PATH', $_ENV ) )
274 $envPath = trim( $_ENV['PATH'] );
276 else if ( ( $envPath = getenv( 'PATH' ) ) !== false )
278 $envPath = trim( $envPath );
280 if ( is_string( $envPath ) && strlen( trim( $envPath ) ) == 0 )
285 switch ( self
::os() )
296 $dirs = explode( ':', $envPath );
297 foreach ( $dirs as $dir )
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}" ) )
306 return "{$dir}/{$fileName}";
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
315 elseif ( @file_exists
( "./{$fileName}" ) )
323 $dirs = explode( ';', $envPath );
324 foreach ( $dirs as $dir )
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" ) )
333 return "{$dir}\\{$fileName}.exe";
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
342 elseif ( @file_exists
( "{$fileName}.exe" ) )
344 return "{$fileName}.exe";
352 * Reset the cached information.
358 public static function reset()
360 self
::$imageIdentify = null;
361 self
::$imageConvert = null;