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

/Phrozn/Vendor/Zend/Loader.php

http://github.com/farazdagi/phrozn
PHP | 201 lines | 77 code | 13 blank | 111 comment | 22 complexity | 96b5b7c3b3c1a7855cb33a55a7efdbb4 MD5 | raw file
Possible License(s): Apache-2.0
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. namespace Zend;
  21. /**
  22. * Static methods for loading classes and files.
  23. *
  24. * @category Zend
  25. * @package Zend_Loader
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Loader
  30. {
  31. /**
  32. * Loads a PHP file. This is a wrapper for PHP's include() function.
  33. *
  34. * $filename must be the complete filename, including any
  35. * extension such as ".php". Note that a security check is performed that
  36. * does not permit extended characters in the filename. This method is
  37. * intended for loading Zend Framework files.
  38. *
  39. * If $dirs is a string or an array, it will search the directories
  40. * in the order supplied, and attempt to load the first matching file.
  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. * If $once is TRUE, it will use include_once() instead of include().
  46. *
  47. * @param string $filename
  48. * @param string|array $dirs - OPTIONAL either a path or array of paths
  49. * to search.
  50. * @param boolean $once
  51. * @return boolean
  52. * @throws Zend\Loader\Exception\SecurityException
  53. */
  54. public static function loadFile($filename, $dirs = null, $once = false)
  55. {
  56. self::_securityCheck($filename);
  57. /**
  58. * Search in provided directories, as well as include_path
  59. */
  60. $incPath = false;
  61. if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
  62. if (is_array($dirs)) {
  63. $dirs = implode(PATH_SEPARATOR, $dirs);
  64. }
  65. $incPath = get_include_path();
  66. set_include_path($dirs . PATH_SEPARATOR . $incPath);
  67. }
  68. /**
  69. * Try finding for the plain filename in the include_path.
  70. */
  71. if ($once) {
  72. include_once $filename;
  73. } else {
  74. include $filename;
  75. }
  76. /**
  77. * If searching in directories, reset include_path
  78. */
  79. if ($incPath) {
  80. set_include_path($incPath);
  81. }
  82. return true;
  83. }
  84. /**
  85. * Returns TRUE if the $filename is readable, or FALSE otherwise.
  86. * This function uses the PHP include_path, where PHP's is_readable()
  87. * does not.
  88. *
  89. * Note from ZF-2900:
  90. * If you use custom error handler, please check whether return value
  91. * from error_reporting() is zero or not.
  92. * At mark of fopen() can not suppress warning if the handler is used.
  93. *
  94. * @param string $filename
  95. * @return boolean
  96. */
  97. public static function isReadable($filename)
  98. {
  99. if (is_readable($filename)) {
  100. // Return early if the filename is readable without needing the
  101. // include_path
  102. return true;
  103. }
  104. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
  105. && preg_match('/^[a-z]:/i', $filename)
  106. ) {
  107. // If on windows, and path provided is clearly an absolute path,
  108. // return false immediately
  109. return false;
  110. }
  111. foreach (self::explodeIncludePath() as $path) {
  112. if ($path == '.') {
  113. if (is_readable($filename)) {
  114. return true;
  115. }
  116. continue;
  117. }
  118. $file = $path . '/' . $filename;
  119. if (is_readable($file)) {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * Explode an include path into an array
  127. *
  128. * If no path provided, uses current include_path. Works around issues that
  129. * occur when the path includes stream schemas.
  130. *
  131. * @param string|null $path
  132. * @return array
  133. */
  134. public static function explodeIncludePath($path = null)
  135. {
  136. if (null === $path) {
  137. $path = get_include_path();
  138. }
  139. if (PATH_SEPARATOR == ':') {
  140. // On *nix systems, include_paths which include paths with a stream
  141. // schema cannot be safely explode'd, so we have to be a bit more
  142. // intelligent in the approach.
  143. $paths = preg_split('#:(?!//)#', $path);
  144. } else {
  145. $paths = explode(PATH_SEPARATOR, $path);
  146. }
  147. return $paths;
  148. }
  149. /**
  150. * Ensure that filename does not contain exploits
  151. *
  152. * @param string $filename
  153. * @return void
  154. * @throws Zend\Loader\Exception\SecurityException
  155. */
  156. protected static function _securityCheck($filename)
  157. {
  158. /**
  159. * Security check
  160. */
  161. if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
  162. require_once __DIR__ . '/Loader/Exception/SecurityException.php';
  163. throw new Loader\Exception\SecurityException('Illegal character in filename');
  164. }
  165. }
  166. /**
  167. * Attempt to include() the file.
  168. *
  169. * include() is not prefixed with the @ operator because if
  170. * the file is loaded and contains a parse error, execution
  171. * will halt silently and this is difficult to debug.
  172. *
  173. * Always set display_errors = Off on production servers!
  174. *
  175. * @param string $filespec
  176. * @param boolean $once
  177. * @return boolean
  178. * @deprecated Since 1.5.0; use loadFile() instead
  179. */
  180. protected static function _includeFile($filespec, $once = false)
  181. {
  182. if ($once) {
  183. return include_once $filespec;
  184. } else {
  185. return include $filespec ;
  186. }
  187. }
  188. }