PageRenderTime 92ms CodeModel.GetById 44ms RepoModel.GetById 0ms app.codeStats 0ms

/MantisBT/library/ezc/Base/src/features.php

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