PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Loader.php

https://gitlab.com/florianocomercial/centreon
PHP | 321 lines | 197 code | 13 blank | 111 comment | 25 complexity | e83d212e76ad1eb1ee4f593344a5bbe2 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Loader
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Loader.php 20990 2010-02-08 18:20:39Z matthew $
  20. */
  21. /**
  22. * Static methods for loading classes and files.
  23. *
  24. * @category Zend
  25. * @package Zend_Loader
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Loader
  30. {
  31. /**
  32. * Loads a class from a PHP file. The filename must be formatted
  33. * as "$class.php".
  34. *
  35. * If $dirs is a string or an array, it will search the directories
  36. * in the order supplied, and attempt to load the first matching file.
  37. *
  38. * If $dirs is null, it will split the class name at underscores to
  39. * generate a path hierarchy (e.g., "Zend_Example_Class" will map
  40. * to "Zend/Example/Class.php").
  41. *
  42. * If the file was not found in the $dirs, or if no $dirs were specified,
  43. * it will attempt to load it from PHP's include_path.
  44. *
  45. * @param string $class - The full class name of a Zend component.
  46. * @param string|array $dirs - OPTIONAL Either a path or an array of paths
  47. * to search.
  48. * @return void
  49. * @throws Zend_Exception
  50. */
  51. public static function loadClass($class, $dirs = null)
  52. {
  53. if (class_exists($class, false) || interface_exists($class, false)) {
  54. return;
  55. }
  56. if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
  57. require_once 'Zend/Exception.php';
  58. throw new Zend_Exception('Directory argument must be a string or an array');
  59. }
  60. // Autodiscover the path from the class name
  61. // Implementation is PHP namespace-aware, and based on
  62. // Framework Interop Group reference implementation:
  63. // http://groups.google.com/group/php-standards/web/psr-0-final-proposal
  64. $className = ltrim($class, '\\');
  65. $file = '';
  66. $namespace = '';
  67. if ($lastNsPos = strripos($className, '\\')) {
  68. $namespace = substr($className, 0, $lastNsPos);
  69. $className = substr($className, $lastNsPos + 1);
  70. $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  71. }
  72. $file .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  73. if (!empty($dirs)) {
  74. // use the autodiscovered path
  75. $dirPath = dirname($file);
  76. if (is_string($dirs)) {
  77. $dirs = explode(PATH_SEPARATOR, $dirs);
  78. }
  79. foreach ($dirs as $key => $dir) {
  80. if ($dir == '.') {
  81. $dirs[$key] = $dirPath;
  82. } else {
  83. $dir = rtrim($dir, '\\/');
  84. $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
  85. }
  86. }
  87. $file = basename($file);
  88. self::loadFile($file, $dirs, true);
  89. } else {
  90. self::loadFile($file, null, true);
  91. }
  92. if (!class_exists($class, false) && !interface_exists($class, false)) {
  93. require_once 'Zend/Exception.php';
  94. throw new Zend_Exception("File \"$file\" does not exist or class \"$class\" was not found in the file");
  95. }
  96. }
  97. /**
  98. * Loads a PHP file. This is a wrapper for PHP's include() function.
  99. *
  100. * $filename must be the complete filename, including any
  101. * extension such as ".php". Note that a security check is performed that
  102. * does not permit extended characters in the filename. This method is
  103. * intended for loading Zend Framework files.
  104. *
  105. * If $dirs is a string or an array, it will search the directories
  106. * in the order supplied, and attempt to load the first matching file.
  107. *
  108. * If the file was not found in the $dirs, or if no $dirs were specified,
  109. * it will attempt to load it from PHP's include_path.
  110. *
  111. * If $once is TRUE, it will use include_once() instead of include().
  112. *
  113. * @param string $filename
  114. * @param string|array $dirs - OPTIONAL either a path or array of paths
  115. * to search.
  116. * @param boolean $once
  117. * @return boolean
  118. * @throws Zend_Exception
  119. */
  120. public static function loadFile($filename, $dirs = null, $once = false)
  121. {
  122. self::_securityCheck($filename);
  123. /**
  124. * Search in provided directories, as well as include_path
  125. */
  126. $incPath = false;
  127. if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
  128. if (is_array($dirs)) {
  129. $dirs = implode(PATH_SEPARATOR, $dirs);
  130. }
  131. $incPath = get_include_path();
  132. set_include_path($dirs . PATH_SEPARATOR . $incPath);
  133. }
  134. /**
  135. * Try finding for the plain filename in the include_path.
  136. */
  137. if ($once) {
  138. include_once $filename;
  139. } else {
  140. include $filename;
  141. }
  142. /**
  143. * If searching in directories, reset include_path
  144. */
  145. if ($incPath) {
  146. set_include_path($incPath);
  147. }
  148. return true;
  149. }
  150. /**
  151. * Returns TRUE if the $filename is readable, or FALSE otherwise.
  152. * This function uses the PHP include_path, where PHP's is_readable()
  153. * does not.
  154. *
  155. * Note from ZF-2900:
  156. * If you use custom error handler, please check whether return value
  157. * from error_reporting() is zero or not.
  158. * At mark of fopen() can not suppress warning if the handler is used.
  159. *
  160. * @param string $filename
  161. * @return boolean
  162. */
  163. public static function isReadable($filename)
  164. {
  165. if (is_readable($filename)) {
  166. // Return early if the filename is readable without needing the
  167. // include_path
  168. return true;
  169. }
  170. foreach (self::explodeIncludePath() as $path) {
  171. if ($path == '.') {
  172. if (is_readable($filename)) {
  173. return true;
  174. }
  175. continue;
  176. }
  177. $file = $path . '/' . $filename;
  178. if (is_readable($file)) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. /**
  185. * Explode an include path into an array
  186. *
  187. * If no path provided, uses current include_path. Works around issues that
  188. * occur when the path includes stream schemas.
  189. *
  190. * @param string|null $path
  191. * @return array
  192. */
  193. public static function explodeIncludePath($path = null)
  194. {
  195. if (null === $path) {
  196. $path = get_include_path();
  197. }
  198. if (PATH_SEPARATOR == ':') {
  199. // On *nix systems, include_paths which include paths with a stream
  200. // schema cannot be safely explode'd, so we have to be a bit more
  201. // intelligent in the approach.
  202. $paths = preg_split('#:(?!//)#', $path);
  203. } else {
  204. $paths = explode(PATH_SEPARATOR, $path);
  205. }
  206. return $paths;
  207. }
  208. /**
  209. * spl_autoload() suitable implementation for supporting class autoloading.
  210. *
  211. * Attach to spl_autoload() using the following:
  212. * <code>
  213. * spl_autoload_register(array('Zend_Loader', 'autoload'));
  214. * </code>
  215. *
  216. * @deprecated Since 1.8.0
  217. * @param string $class
  218. * @return string|false Class name on success; false on failure
  219. */
  220. public static function autoload($class)
  221. {
  222. trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead', E_USER_NOTICE);
  223. try {
  224. @self::loadClass($class);
  225. return $class;
  226. } catch (Exception $e) {
  227. return false;
  228. }
  229. }
  230. /**
  231. * Register {@link autoload()} with spl_autoload()
  232. *
  233. * @deprecated Since 1.8.0
  234. * @param string $class (optional)
  235. * @param boolean $enabled (optional)
  236. * @return void
  237. * @throws Zend_Exception if spl_autoload() is not found
  238. * or if the specified class does not have an autoload() method.
  239. */
  240. public static function registerAutoload($class = 'Zend_Loader', $enabled = true)
  241. {
  242. trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead', E_USER_NOTICE);
  243. require_once 'Zend/Loader/Autoloader.php';
  244. $autoloader = Zend_Loader_Autoloader::getInstance();
  245. $autoloader->setFallbackAutoloader(true);
  246. if ('Zend_Loader' != $class) {
  247. self::loadClass($class);
  248. $methods = get_class_methods($class);
  249. if (!in_array('autoload', (array) $methods)) {
  250. require_once 'Zend/Exception.php';
  251. throw new Zend_Exception("The class \"$class\" does not have an autoload() method");
  252. }
  253. $callback = array($class, 'autoload');
  254. if ($enabled) {
  255. $autoloader->pushAutoloader($callback);
  256. } else {
  257. $autoloader->removeAutoloader($callback);
  258. }
  259. }
  260. }
  261. /**
  262. * Ensure that filename does not contain exploits
  263. *
  264. * @param string $filename
  265. * @return void
  266. * @throws Zend_Exception
  267. */
  268. protected static function _securityCheck($filename)
  269. {
  270. /**
  271. * Security check
  272. */
  273. if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
  274. require_once 'Zend/Exception.php';
  275. throw new Zend_Exception('Security check: Illegal character in filename');
  276. }
  277. }
  278. /**
  279. * Attempt to include() the file.
  280. *
  281. * include() is not prefixed with the @ operator because if
  282. * the file is loaded and contains a parse error, execution
  283. * will halt silently and this is difficult to debug.
  284. *
  285. * Always set display_errors = Off on production servers!
  286. *
  287. * @param string $filespec
  288. * @param boolean $once
  289. * @return boolean
  290. * @deprecated Since 1.5.0; use loadFile() instead
  291. */
  292. protected static function _includeFile($filespec, $once = false)
  293. {
  294. if ($once) {
  295. return include_once $filespec;
  296. } else {
  297. return include $filespec ;
  298. }
  299. }
  300. }