PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/application/libraries/Zend/Loader.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 329 lines | 202 code | 14 blank | 113 comment | 28 complexity | 188915ca7e1f455edd9ee88d154c5e3e 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * Static methods for loading classes and files.
  23. *
  24. * @category Zend
  25. * @package Zend_Loader
  26. * @copyright Copyright (c) 2005-2011 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. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
  171. && preg_match('/^[a-z]:/i', $filename)
  172. ) {
  173. // If on windows, and path provided is clearly an absolute path,
  174. // return false immediately
  175. return false;
  176. }
  177. foreach (self::explodeIncludePath() as $path) {
  178. if ($path == '.') {
  179. if (is_readable($filename)) {
  180. return true;
  181. }
  182. continue;
  183. }
  184. $file = $path . '/' . $filename;
  185. if (is_readable($file)) {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. /**
  192. * Explode an include path into an array
  193. *
  194. * If no path provided, uses current include_path. Works around issues that
  195. * occur when the path includes stream schemas.
  196. *
  197. * @param string|null $path
  198. * @return array
  199. */
  200. public static function explodeIncludePath($path = null)
  201. {
  202. if (null === $path) {
  203. $path = get_include_path();
  204. }
  205. if (PATH_SEPARATOR == ':') {
  206. // On *nix systems, include_paths which include paths with a stream
  207. // schema cannot be safely explode'd, so we have to be a bit more
  208. // intelligent in the approach.
  209. $paths = preg_split('#:(?!//)#', $path);
  210. } else {
  211. $paths = explode(PATH_SEPARATOR, $path);
  212. }
  213. return $paths;
  214. }
  215. /**
  216. * spl_autoload() suitable implementation for supporting class autoloading.
  217. *
  218. * Attach to spl_autoload() using the following:
  219. * <code>
  220. * spl_autoload_register(array('Zend_Loader', 'autoload'));
  221. * </code>
  222. *
  223. * @deprecated Since 1.8.0
  224. * @param string $class
  225. * @return string|false Class name on success; false on failure
  226. */
  227. public static function autoload($class)
  228. {
  229. 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);
  230. try {
  231. @self::loadClass($class);
  232. return $class;
  233. } catch (Exception $e) {
  234. return false;
  235. }
  236. }
  237. /**
  238. * Register {@link autoload()} with spl_autoload()
  239. *
  240. * @deprecated Since 1.8.0
  241. * @param string $class (optional)
  242. * @param boolean $enabled (optional)
  243. * @return void
  244. * @throws Zend_Exception if spl_autoload() is not found
  245. * or if the specified class does not have an autoload() method.
  246. */
  247. public static function registerAutoload($class = 'Zend_Loader', $enabled = true)
  248. {
  249. 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);
  250. require_once 'Zend/Loader/Autoloader.php';
  251. $autoloader = Zend_Loader_Autoloader::getInstance();
  252. $autoloader->setFallbackAutoloader(true);
  253. if ('Zend_Loader' != $class) {
  254. self::loadClass($class);
  255. $methods = get_class_methods($class);
  256. if (!in_array('autoload', (array) $methods)) {
  257. require_once 'Zend/Exception.php';
  258. throw new Zend_Exception("The class \"$class\" does not have an autoload() method");
  259. }
  260. $callback = array($class, 'autoload');
  261. if ($enabled) {
  262. $autoloader->pushAutoloader($callback);
  263. } else {
  264. $autoloader->removeAutoloader($callback);
  265. }
  266. }
  267. }
  268. /**
  269. * Ensure that filename does not contain exploits
  270. *
  271. * @param string $filename
  272. * @return void
  273. * @throws Zend_Exception
  274. */
  275. protected static function _securityCheck($filename)
  276. {
  277. /**
  278. * Security check
  279. */
  280. if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
  281. require_once 'Zend/Exception.php';
  282. throw new Zend_Exception('Security check: Illegal character in filename');
  283. }
  284. }
  285. /**
  286. * Attempt to include() the file.
  287. *
  288. * include() is not prefixed with the @ operator because if
  289. * the file is loaded and contains a parse error, execution
  290. * will halt silently and this is difficult to debug.
  291. *
  292. * Always set display_errors = Off on production servers!
  293. *
  294. * @param string $filespec
  295. * @param boolean $once
  296. * @return boolean
  297. * @deprecated Since 1.5.0; use loadFile() instead
  298. */
  299. protected static function _includeFile($filespec, $once = false)
  300. {
  301. if ($once) {
  302. return include_once $filespec;
  303. } else {
  304. return include $filespec ;
  305. }
  306. }
  307. }