Loader.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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-2009 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 2504 2011-12-28 07:35:29Z liu21st $
  20. */
  21. /**
  22. * Static methods for loading classes and files.
  23. *
  24. * @category Zend
  25. * @package Zend_Loader
  26. * @copyright Copyright (c) 2005-2009 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. $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
  62. if (!empty($dirs)) {
  63. // use the autodiscovered path
  64. $dirPath = dirname($file);
  65. if (is_string($dirs)) {
  66. $dirs = explode(PATH_SEPARATOR, $dirs);
  67. }
  68. foreach ($dirs as $key => $dir) {
  69. if ($dir == '.') {
  70. $dirs[$key] = $dirPath;
  71. } else {
  72. $dir = rtrim($dir, '\\/');
  73. $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
  74. }
  75. }
  76. $file = basename($file);
  77. self::loadFile($file, $dirs, true);
  78. } else {
  79. self::_securityCheck($file);
  80. include $file;
  81. }
  82. if (!class_exists($class, false) && !interface_exists($class, false)) {
  83. require_once 'Zend/Exception.php';
  84. throw new Zend_Exception("File \"$file\" does not exist or class \"$class\" was not found in the file");
  85. }
  86. }
  87. /**
  88. * Loads a PHP file. This is a wrapper for PHP's include() function.
  89. *
  90. * $filename must be the complete filename, including any
  91. * extension such as ".php". Note that a security check is performed that
  92. * does not permit extended characters in the filename. This method is
  93. * intended for loading Zend Framework files.
  94. *
  95. * If $dirs is a string or an array, it will search the directories
  96. * in the order supplied, and attempt to load the first matching file.
  97. *
  98. * If the file was not found in the $dirs, or if no $dirs were specified,
  99. * it will attempt to load it from PHP's include_path.
  100. *
  101. * If $once is TRUE, it will use include_once() instead of include().
  102. *
  103. * @param string $filename
  104. * @param string|array $dirs - OPTIONAL either a path or array of paths
  105. * to search.
  106. * @param boolean $once
  107. * @return boolean
  108. * @throws Zend_Exception
  109. */
  110. public static function loadFile($filename, $dirs = null, $once = false)
  111. {
  112. self::_securityCheck($filename);
  113. /**
  114. * Search in provided directories, as well as include_path
  115. */
  116. $incPath = false;
  117. if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
  118. if (is_array($dirs)) {
  119. $dirs = implode(PATH_SEPARATOR, $dirs);
  120. }
  121. $incPath = get_include_path();
  122. set_include_path($dirs . PATH_SEPARATOR . $incPath);
  123. }
  124. /**
  125. * Try finding for the plain filename in the include_path.
  126. */
  127. if ($once) {
  128. include_once $filename;
  129. } else {
  130. include $filename;
  131. }
  132. /**
  133. * If searching in directories, reset include_path
  134. */
  135. if ($incPath) {
  136. set_include_path($incPath);
  137. }
  138. return true;
  139. }
  140. /**
  141. * Returns TRUE if the $filename is readable, or FALSE otherwise.
  142. * This function uses the PHP include_path, where PHP's is_readable()
  143. * does not.
  144. *
  145. * Note from ZF-2900:
  146. * If you use custom error handler, please check whether return value
  147. * from error_reporting() is zero or not.
  148. * At mark of fopen() can not suppress warning if the handler is used.
  149. *
  150. * @param string $filename
  151. * @return boolean
  152. */
  153. public static function isReadable($filename)
  154. {
  155. if (!$fh = @fopen($filename, 'r', true)) {
  156. return false;
  157. }
  158. @fclose($fh);
  159. return true;
  160. }
  161. /**
  162. * spl_autoload() suitable implementation for supporting class autoloading.
  163. *
  164. * Attach to spl_autoload() using the following:
  165. * <code>
  166. * spl_autoload_register(array('Zend_Loader', 'autoload'));
  167. * </code>
  168. *
  169. * @deprecated Since 1.8.0
  170. * @param string $class
  171. * @return string|false Class name on success; false on failure
  172. */
  173. public static function autoload($class)
  174. {
  175. 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);
  176. try {
  177. @self::loadClass($class);
  178. return $class;
  179. } catch (Exception $e) {
  180. return false;
  181. }
  182. }
  183. /**
  184. * Register {@link autoload()} with spl_autoload()
  185. *
  186. * @deprecated Since 1.8.0
  187. * @param string $class (optional)
  188. * @param boolean $enabled (optional)
  189. * @return void
  190. * @throws Zend_Exception if spl_autoload() is not found
  191. * or if the specified class does not have an autoload() method.
  192. */
  193. public static function registerAutoload($class = 'Zend_Loader', $enabled = true)
  194. {
  195. 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);
  196. require_once 'Zend/Loader/Autoloader.php';
  197. $autoloader = Zend_Loader_Autoloader::getInstance();
  198. $autoloader->setFallbackAutoloader(true);
  199. if ('Zend_Loader' != $class) {
  200. self::loadClass($class);
  201. $methods = get_class_methods($class);
  202. if (!in_array('autoload', (array) $methods)) {
  203. require_once 'Zend/Exception.php';
  204. throw new Zend_Exception("The class \"$class\" does not have an autoload() method");
  205. }
  206. $callback = array($class, 'autoload');
  207. if ($enabled) {
  208. $autoloader->pushAutoloader($callback);
  209. } else {
  210. $autoloader->removeAutoloader($callback);
  211. }
  212. }
  213. }
  214. /**
  215. * Ensure that filename does not contain exploits
  216. *
  217. * @param string $filename
  218. * @return void
  219. * @throws Zend_Exception
  220. */
  221. protected static function _securityCheck($filename)
  222. {
  223. /**
  224. * Security check
  225. */
  226. if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
  227. require_once 'Zend/Exception.php';
  228. throw new Zend_Exception('Security check: Illegal character in filename');
  229. }
  230. }
  231. /**
  232. * Attempt to include() the file.
  233. *
  234. * include() is not prefixed with the @ operator because if
  235. * the file is loaded and contains a parse error, execution
  236. * will halt silently and this is difficult to debug.
  237. *
  238. * Always set display_errors = Off on production servers!
  239. *
  240. * @param string $filespec
  241. * @param boolean $once
  242. * @return boolean
  243. * @deprecated Since 1.5.0; use loadFile() instead
  244. */
  245. protected static function _includeFile($filespec, $once = false)
  246. {
  247. if ($once) {
  248. return include_once $filespec;
  249. } else {
  250. return include $filespec ;
  251. }
  252. }
  253. }