Loader.php 8.9 KB

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