PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/Zend/Loader.php

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