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-2008 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 15519 2009-05-11 11:57:05Z matthew $
|
---|
20 | */
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Static methods for loading classes and files.
|
---|
24 | *
|
---|
25 | * @category Zend
|
---|
26 | * @package Zend_Loader
|
---|
27 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
28 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
29 | */
|
---|
30 | class Zend_Loader
|
---|
31 | {
|
---|
32 | /**
|
---|
33 | * Loads a class from a PHP file. The filename must be formatted
|
---|
34 | * as "$class.php".
|
---|
35 | *
|
---|
36 | * If $dirs is a string or an array, it will search the directories
|
---|
37 | * in the order supplied, and attempt to load the first matching file.
|
---|
38 | *
|
---|
39 | * If $dirs is null, it will split the class name at underscores to
|
---|
40 | * generate a path hierarchy (e.g., "Zend_Example_Class" will map
|
---|
41 | * to "Zend/Example/Class.php").
|
---|
42 | *
|
---|
43 | * If the file was not found in the $dirs, or if no $dirs were specified,
|
---|
44 | * it will attempt to load it from PHP's include_path.
|
---|
45 | *
|
---|
46 | * @param string $class - The full class name of a Zend component.
|
---|
47 | * @param string|array $dirs - OPTIONAL Either a path or an array of paths
|
---|
48 | * to search.
|
---|
49 | * @return void
|
---|
50 | * @throws Zend_Exception
|
---|
51 | */
|
---|
52 | public static function loadClass($class, $dirs = null)
|
---|
53 | {
|
---|
54 | if (class_exists($class, false) || interface_exists($class, false)) {
|
---|
55 | return;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
|
---|
59 | require_once 'Zend/Exception.php';
|
---|
60 | throw new Zend_Exception('Directory argument must be a string or an array');
|
---|
61 | }
|
---|
62 |
|
---|
63 | // autodiscover the path from the class name
|
---|
64 | $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
|
---|
65 | if (!empty($dirs)) {
|
---|
66 | // use the autodiscovered path
|
---|
67 | $dirPath = dirname($file);
|
---|
68 | if (is_string($dirs)) {
|
---|
69 | $dirs = explode(PATH_SEPARATOR, $dirs);
|
---|
70 | }
|
---|
71 | foreach ($dirs as $key => $dir) {
|
---|
72 | if ($dir == '.') {
|
---|
73 | $dirs[$key] = $dirPath;
|
---|
74 | } else {
|
---|
75 | $dir = rtrim($dir, '\\/');
|
---|
76 | $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | $file = basename($file);
|
---|
80 | self::loadFile($file, $dirs, true);
|
---|
81 | } else {
|
---|
82 | self::_securityCheck($file);
|
---|
83 | include $file;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (!class_exists($class, false) && !interface_exists($class, false)) {
|
---|
87 | require_once 'Zend/Exception.php';
|
---|
88 | throw new Zend_Exception("File \"$file\" does not exist or class \"$class\" was not found in the file");
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Loads a PHP file. This is a wrapper for PHP's include() function.
|
---|
94 | *
|
---|
95 | * $filename must be the complete filename, including any
|
---|
96 | * extension such as ".php". Note that a security check is performed that
|
---|
97 | * does not permit extended characters in the filename. This method is
|
---|
98 | * intended for loading Zend Framework files.
|
---|
99 | *
|
---|
100 | * If $dirs is a string or an array, it will search the directories
|
---|
101 | * in the order supplied, and attempt to load the first matching file.
|
---|
102 | *
|
---|
103 | * If the file was not found in the $dirs, or if no $dirs were specified,
|
---|
104 | * it will attempt to load it from PHP's include_path.
|
---|
105 | *
|
---|
106 | * If $once is TRUE, it will use include_once() instead of include().
|
---|
107 | *
|
---|
108 | * @param string $filename
|
---|
109 | * @param string|array $dirs - OPTIONAL either a path or array of paths
|
---|
110 | * to search.
|
---|
111 | * @param boolean $once
|
---|
112 | * @return boolean
|
---|
113 | * @throws Zend_Exception
|
---|
114 | */
|
---|
115 | public static function loadFile($filename, $dirs = null, $once = false)
|
---|
116 | {
|
---|
117 | self::_securityCheck($filename);
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Search in provided directories, as well as include_path
|
---|
121 | */
|
---|
122 | $incPath = false;
|
---|
123 | if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
|
---|
124 | if (is_array($dirs)) {
|
---|
125 | $dirs = implode(PATH_SEPARATOR, $dirs);
|
---|
126 | }
|
---|
127 | $incPath = get_include_path();
|
---|
128 | set_include_path($dirs . PATH_SEPARATOR . $incPath);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Try finding for the plain filename in the include_path.
|
---|
133 | */
|
---|
134 | if ($once) {
|
---|
135 | include_once $filename;
|
---|
136 | } else {
|
---|
137 | include $filename;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * If searching in directories, reset include_path
|
---|
142 | */
|
---|
143 | if ($incPath) {
|
---|
144 | set_include_path($incPath);
|
---|
145 | }
|
---|
146 |
|
---|
147 | return true;
|
---|
148 | }
|
---|
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 (!$fh = @fopen($filename, 'r', true)) {
|
---|
166 | return false;
|
---|
167 | }
|
---|
168 | @fclose($fh);
|
---|
169 | return true;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * spl_autoload() suitable implementation for supporting class autoloading.
|
---|
174 | *
|
---|
175 | * Attach to spl_autoload() using the following:
|
---|
176 | * <code>
|
---|
177 | * spl_autoload_register(array('Zend_Loader', 'autoload'));
|
---|
178 | * </code>
|
---|
179 | *
|
---|
180 | * @deprecated Since 1.8.0
|
---|
181 | * @param string $class
|
---|
182 | * @return string|false Class name on success; false on failure
|
---|
183 | */
|
---|
184 | public static function autoload($class)
|
---|
185 | {
|
---|
186 | 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);
|
---|
187 | try {
|
---|
188 | @self::loadClass($class);
|
---|
189 | return $class;
|
---|
190 | } catch (Exception $e) {
|
---|
191 | return false;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Register {@link autoload()} with spl_autoload()
|
---|
197 | *
|
---|
198 | * @deprecated Since 1.8.0
|
---|
199 | * @param string $class (optional)
|
---|
200 | * @param boolean $enabled (optional)
|
---|
201 | * @return void
|
---|
202 | * @throws Zend_Exception if spl_autoload() is not found
|
---|
203 | * or if the specified class does not have an autoload() method.
|
---|
204 | */
|
---|
205 | public static function registerAutoload($class = 'Zend_Loader', $enabled = true)
|
---|
206 | {
|
---|
207 | 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);
|
---|
208 | require_once 'Zend/Loader/Autoloader.php';
|
---|
209 | $autoloader = Zend_Loader_Autoloader::getInstance();
|
---|
210 | $autoloader->setFallbackAutoloader(true);
|
---|
211 |
|
---|
212 | if ('Zend_Loader' != $class) {
|
---|
213 | self::loadClass($class);
|
---|
214 | $methods = get_class_methods($class);
|
---|
215 | if (!in_array('autoload', (array) $methods)) {
|
---|
216 | require_once 'Zend/Exception.php';
|
---|
217 | throw new Zend_Exception("The class \"$class\" does not have an autoload() method");
|
---|
218 | }
|
---|
219 |
|
---|
220 | $callback = array($class, 'autoload');
|
---|
221 |
|
---|
222 | if ($enabled) {
|
---|
223 | $autoloader->pushAutoloader($callback);
|
---|
224 | } else {
|
---|
225 | $autoloader->removeAutoloader($callback);
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Ensure that filename does not contain exploits
|
---|
232 | *
|
---|
233 | * @param string $filename
|
---|
234 | * @return void
|
---|
235 | * @throws Zend_Exception
|
---|
236 | */
|
---|
237 | protected static function _securityCheck($filename)
|
---|
238 | {
|
---|
239 | /**
|
---|
240 | * Security check
|
---|
241 | */
|
---|
242 | if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
|
---|
243 | require_once 'Zend/Exception.php';
|
---|
244 | throw new Zend_Exception('Security check: Illegal character in filename');
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Attempt to include() the file.
|
---|
250 | *
|
---|
251 | * include() is not prefixed with the @ operator because if
|
---|
252 | * the file is loaded and contains a parse error, execution
|
---|
253 | * will halt silently and this is difficult to debug.
|
---|
254 | *
|
---|
255 | * Always set display_errors = Off on production servers!
|
---|
256 | *
|
---|
257 | * @param string $filespec
|
---|
258 | * @param boolean $once
|
---|
259 | * @return boolean
|
---|
260 | * @deprecated Since 1.5.0; use loadFile() instead
|
---|
261 | */
|
---|
262 | protected static function _includeFile($filespec, $once = false)
|
---|
263 | {
|
---|
264 | if ($once) {
|
---|
265 | return include_once $filespec;
|
---|
266 | } else {
|
---|
267 | return include $filespec ;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|