1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Marcus Boerger <helly@php.net> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: php_spl.c 283179 2009-06-30 17:14:37Z cseiler $ */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include "php.h"
26 : #include "php_ini.h"
27 : #include "php_main.h"
28 : #include "ext/standard/info.h"
29 : #include "php_spl.h"
30 : #include "spl_functions.h"
31 : #include "spl_engine.h"
32 : #include "spl_array.h"
33 : #include "spl_directory.h"
34 : #include "spl_iterators.h"
35 : #include "spl_exceptions.h"
36 : #include "spl_observer.h"
37 : #include "spl_dllist.h"
38 : #include "spl_fixedarray.h"
39 : #include "spl_heap.h"
40 : #include "zend_exceptions.h"
41 : #include "zend_interfaces.h"
42 : #include "ext/standard/php_rand.h"
43 : #include "ext/standard/php_lcg.h"
44 : #include "main/snprintf.h"
45 :
46 : #ifdef COMPILE_DL_SPL
47 : ZEND_GET_MODULE(spl)
48 : #endif
49 :
50 : ZEND_DECLARE_MODULE_GLOBALS(spl)
51 :
52 : #define SPL_DEFAULT_FILE_EXTENSIONS ".inc,.php"
53 :
54 : /* {{{ PHP_GINIT_FUNCTION
55 : */
56 : static PHP_GINIT_FUNCTION(spl)
57 17633 : {
58 17633 : spl_globals->autoload_extensions = NULL;
59 17633 : spl_globals->autoload_extensions_len = 0;
60 17633 : spl_globals->autoload_functions = NULL;
61 17633 : spl_globals->autoload_running = 0;
62 17633 : }
63 : /* }}} */
64 :
65 : static zend_class_entry * spl_find_ce_by_name(char *name, int len, zend_bool autoload TSRMLS_DC)
66 36 : {
67 : zend_class_entry **ce;
68 : int found;
69 :
70 36 : if (!autoload) {
71 : char *lc_name;
72 : ALLOCA_FLAG(use_heap)
73 :
74 12 : lc_name = do_alloca(len + 1, use_heap);
75 12 : zend_str_tolower_copy(lc_name, name, len);
76 :
77 12 : found = zend_hash_find(EG(class_table), lc_name, len +1, (void **) &ce);
78 12 : free_alloca(lc_name, use_heap);
79 : } else {
80 24 : found = zend_lookup_class(name, len, &ce TSRMLS_CC);
81 : }
82 36 : if (found != SUCCESS) {
83 8 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class %s does not exist%s", name, autoload ? " and could not be loaded" : "");
84 8 : return NULL;
85 : }
86 :
87 28 : return *ce;
88 : }
89 :
90 : /* {{{ proto array class_parents(object instance [, boolean autoload = true])
91 : Return an array containing the names of all parent classes */
92 : PHP_FUNCTION(class_parents)
93 8 : {
94 : zval *obj;
95 : zend_class_entry *parent_class, *ce;
96 8 : zend_bool autoload = 1;
97 :
98 8 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &obj, &autoload) == FAILURE) {
99 0 : RETURN_FALSE;
100 : }
101 :
102 8 : if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
103 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "object or string expected");
104 0 : RETURN_FALSE;
105 : }
106 :
107 8 : if (Z_TYPE_P(obj) == IS_STRING) {
108 5 : if (NULL == (ce = spl_find_ce_by_name(Z_STRVAL_P(obj), Z_STRLEN_P(obj), autoload TSRMLS_CC))) {
109 2 : RETURN_FALSE;
110 : }
111 : } else {
112 3 : ce = Z_OBJCE_P(obj);
113 : }
114 :
115 6 : array_init(return_value);
116 6 : parent_class = ce->parent;
117 20 : while (parent_class) {
118 8 : spl_add_class_name(return_value, parent_class, 0, 0 TSRMLS_CC);
119 8 : parent_class = parent_class->parent;
120 : }
121 : }
122 : /* }}} */
123 :
124 : /* {{{ proto array class_implements(mixed what [, bool autoload ])
125 : Return all classes and interfaces implemented by SPL */
126 : PHP_FUNCTION(class_implements)
127 68 : {
128 : zval *obj;
129 68 : zend_bool autoload = 1;
130 : zend_class_entry *ce;
131 :
132 68 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &obj, &autoload) == FAILURE) {
133 7 : RETURN_FALSE;
134 : }
135 61 : if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
136 22 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "object or string expected");
137 22 : RETURN_FALSE;
138 : }
139 :
140 39 : if (Z_TYPE_P(obj) == IS_STRING) {
141 31 : if (NULL == (ce = spl_find_ce_by_name(Z_STRVAL_P(obj), Z_STRLEN_P(obj), autoload TSRMLS_CC))) {
142 6 : RETURN_FALSE;
143 : }
144 : } else {
145 8 : ce = Z_OBJCE_P(obj);
146 : }
147 :
148 33 : array_init(return_value);
149 33 : spl_add_interfaces(return_value, ce, 1, ZEND_ACC_INTERFACE TSRMLS_CC);
150 : }
151 : /* }}} */
152 :
153 : #define SPL_ADD_CLASS(class_name, z_list, sub, allow, ce_flags) \
154 : spl_add_classes(spl_ce_ ## class_name, z_list, sub, allow, ce_flags TSRMLS_CC)
155 :
156 : #define SPL_LIST_CLASSES(z_list, sub, allow, ce_flags) \
157 : SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \
158 : SPL_ADD_CLASS(ArrayIterator, z_list, sub, allow, ce_flags); \
159 : SPL_ADD_CLASS(ArrayObject, z_list, sub, allow, ce_flags); \
160 : SPL_ADD_CLASS(BadFunctionCallException, z_list, sub, allow, ce_flags); \
161 : SPL_ADD_CLASS(BadMethodCallException, z_list, sub, allow, ce_flags); \
162 : SPL_ADD_CLASS(CachingIterator, z_list, sub, allow, ce_flags); \
163 : SPL_ADD_CLASS(Countable, z_list, sub, allow, ce_flags); \
164 : SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \
165 : SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
166 : SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
167 : SPL_ADD_CLASS(FilesystemIterator, z_list, sub, allow, ce_flags); \
168 : SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
169 : SPL_ADD_CLASS(GlobIterator, z_list, sub, allow, ce_flags); \
170 : SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
171 : SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \
172 : SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \
173 : SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \
174 : SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \
175 : SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \
176 : SPL_ADD_CLASS(MultipleIterator, z_list, sub, allow, ce_flags); \
177 : SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \
178 : SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \
179 : SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \
180 : SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \
181 : SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \
182 : SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \
183 : SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \
184 : SPL_ADD_CLASS(RecursiveArrayIterator, z_list, sub, allow, ce_flags); \
185 : SPL_ADD_CLASS(RecursiveCachingIterator, z_list, sub, allow, ce_flags); \
186 : SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \
187 : SPL_ADD_CLASS(RecursiveFilterIterator, z_list, sub, allow, ce_flags); \
188 : SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \
189 : SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \
190 : SPL_ADD_CLASS(RecursiveRegexIterator, z_list, sub, allow, ce_flags); \
191 : SPL_ADD_CLASS(RecursiveTreeIterator, z_list, sub, allow, ce_flags); \
192 : SPL_ADD_CLASS(RegexIterator, z_list, sub, allow, ce_flags); \
193 : SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \
194 : SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \
195 : SPL_ADD_CLASS(SplDoublyLinkedList, z_list, sub, allow, ce_flags); \
196 : SPL_ADD_CLASS(SplFileInfo, z_list, sub, allow, ce_flags); \
197 : SPL_ADD_CLASS(SplFileObject, z_list, sub, allow, ce_flags); \
198 : SPL_ADD_CLASS(SplFixedArray, z_list, sub, allow, ce_flags); \
199 : SPL_ADD_CLASS(SplHeap, z_list, sub, allow, ce_flags); \
200 : SPL_ADD_CLASS(SplMinHeap, z_list, sub, allow, ce_flags); \
201 : SPL_ADD_CLASS(SplMaxHeap, z_list, sub, allow, ce_flags); \
202 : SPL_ADD_CLASS(SplObjectStorage, z_list, sub, allow, ce_flags); \
203 : SPL_ADD_CLASS(SplObserver, z_list, sub, allow, ce_flags); \
204 : SPL_ADD_CLASS(SplPriorityQueue, z_list, sub, allow, ce_flags); \
205 : SPL_ADD_CLASS(SplQueue, z_list, sub, allow, ce_flags); \
206 : SPL_ADD_CLASS(SplStack, z_list, sub, allow, ce_flags); \
207 : SPL_ADD_CLASS(SplSubject, z_list, sub, allow, ce_flags); \
208 : SPL_ADD_CLASS(SplTempFileObject, z_list, sub, allow, ce_flags); \
209 : SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
210 : SPL_ADD_CLASS(UnexpectedValueException, z_list, sub, allow, ce_flags); \
211 :
212 : /* {{{ proto array spl_classes()
213 : Return an array containing the names of all clsses and interfaces defined in SPL */
214 : PHP_FUNCTION(spl_classes)
215 1 : {
216 1 : array_init(return_value);
217 :
218 1 : SPL_LIST_CLASSES(return_value, 0, 0, 0)
219 1 : }
220 : /* }}} */
221 :
222 : static int spl_autoload(const char *class_name, const char * lc_name, int class_name_len, const char * file_extension TSRMLS_DC) /* {{{ */
223 15 : {
224 : char *class_file;
225 : int class_file_len;
226 15 : int dummy = 1;
227 : zend_file_handle file_handle;
228 : zend_op_array *new_op_array;
229 15 : zval *result = NULL;
230 : int ret;
231 :
232 15 : class_file_len = spprintf(&class_file, 0, "%s%s", lc_name, file_extension);
233 :
234 15 : ret = php_stream_open_for_zend_ex(class_file, &file_handle, ENFORCE_SAFE_MODE|USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
235 :
236 15 : if (ret == SUCCESS) {
237 10 : if (!file_handle.opened_path) {
238 0 : file_handle.opened_path = estrndup(class_file, class_file_len);
239 : }
240 10 : if (zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL)==SUCCESS) {
241 5 : new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE TSRMLS_CC);
242 5 : zend_destroy_file_handle(&file_handle TSRMLS_CC);
243 : } else {
244 5 : new_op_array = NULL;
245 5 : zend_file_handle_dtor(&file_handle TSRMLS_CC);
246 : }
247 10 : if (new_op_array) {
248 5 : EG(return_value_ptr_ptr) = &result;
249 5 : EG(active_op_array) = new_op_array;
250 5 : if (!EG(active_symbol_table)) {
251 0 : zend_rebuild_symbol_table(TSRMLS_C);
252 : }
253 :
254 5 : zend_execute(new_op_array TSRMLS_CC);
255 :
256 5 : destroy_op_array(new_op_array TSRMLS_CC);
257 5 : efree(new_op_array);
258 5 : if (!EG(exception)) {
259 5 : if (EG(return_value_ptr_ptr)) {
260 5 : zval_ptr_dtor(EG(return_value_ptr_ptr));
261 : }
262 : }
263 :
264 5 : efree(class_file);
265 5 : return zend_hash_exists(EG(class_table), (char*)lc_name, class_name_len+1);
266 : }
267 : }
268 10 : efree(class_file);
269 10 : return 0;
270 : } /* }}} */
271 :
272 : /* {{{ proto void spl_autoload(string class_name [, string file_extensions])
273 : Default implementation for __autoload() */
274 : PHP_FUNCTION(spl_autoload)
275 10 : {
276 10 : char *class_name, *lc_name, *file_exts = SPL_G(autoload_extensions);
277 10 : int class_name_len, file_exts_len = SPL_G(autoload_extensions_len), found = 0;
278 : char *copy, *pos1, *pos2;
279 10 : zval **original_return_value = EG(return_value_ptr_ptr);
280 10 : zend_op **original_opline_ptr = EG(opline_ptr);
281 10 : zend_op_array *original_active_op_array = EG(active_op_array);
282 :
283 10 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &class_name, &class_name_len, &file_exts, &file_exts_len) == FAILURE) {
284 0 : RETURN_FALSE;
285 : }
286 :
287 10 : if (file_exts == NULL) { /* autoload_extensions is not intialzed, set to defaults */
288 3 : copy = pos1 = estrndup(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS)-1);
289 : } else {
290 7 : copy = pos1 = estrndup(file_exts, file_exts_len);
291 : }
292 10 : lc_name = zend_str_tolower_dup(class_name, class_name_len);
293 34 : while(pos1 && *pos1 && !EG(exception)) {
294 15 : EG(return_value_ptr_ptr) = original_return_value;
295 15 : EG(opline_ptr) = original_opline_ptr;
296 15 : EG(active_op_array) = original_active_op_array;
297 15 : pos2 = strchr(pos1, ',');
298 15 : if (pos2) *pos2 = '\0';
299 15 : if (spl_autoload(class_name, lc_name, class_name_len, pos1 TSRMLS_CC)) {
300 1 : found = 1;
301 1 : break; /* loaded */
302 : }
303 14 : pos1 = pos2 ? pos2 + 1 : NULL;
304 : }
305 10 : efree(lc_name);
306 10 : if (copy) {
307 10 : efree(copy);
308 : }
309 :
310 10 : EG(return_value_ptr_ptr) = original_return_value;
311 10 : EG(opline_ptr) = original_opline_ptr;
312 10 : EG(active_op_array) = original_active_op_array;
313 :
314 10 : if (!found && !SPL_G(autoload_running)) {
315 : /* For internal errors, we generate E_ERROR, for direct calls an exception is thrown.
316 : * The "scope" is determined by an opcode, if it is ZEND_FETCH_CLASS we know function was called indirectly by
317 : * the Zend engine.
318 : */
319 8 : if (active_opline->opcode != ZEND_FETCH_CLASS) {
320 7 : zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Class %s could not be loaded", class_name);
321 : } else {
322 1 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "Class %s could not be loaded", class_name);
323 : }
324 : }
325 : } /* }}} */
326 :
327 : /* {{{ proto string spl_autoload_extensions([string file_extensions])
328 : Register and return default file extensions for spl_autoload */
329 : PHP_FUNCTION(spl_autoload_extensions)
330 4 : {
331 : char *file_exts;
332 : int file_exts_len;
333 :
334 4 : if (ZEND_NUM_ARGS() > 0) {
335 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file_exts, &file_exts_len) == FAILURE) {
336 0 : return;
337 : }
338 :
339 3 : if (SPL_G(autoload_extensions)) {
340 2 : efree(SPL_G(autoload_extensions));
341 : }
342 3 : SPL_G(autoload_extensions) = estrndup(file_exts, file_exts_len);
343 3 : SPL_G(autoload_extensions_len) = file_exts_len;
344 : }
345 :
346 4 : if (SPL_G(autoload_extensions) == NULL) {
347 1 : RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1, 1);
348 : } else {
349 3 : RETURN_STRINGL(SPL_G(autoload_extensions), SPL_G(autoload_extensions_len), 1);
350 : }
351 : } /* }}} */
352 :
353 : typedef struct {
354 : zend_function *func_ptr;
355 : zval *obj;
356 : zval *closure;
357 : zend_class_entry *ce;
358 : } autoload_func_info;
359 :
360 : static void autoload_func_info_dtor(autoload_func_info *alfi)
361 45 : {
362 45 : if (alfi->obj) {
363 12 : zval_ptr_dtor(&alfi->obj);
364 : }
365 45 : if (alfi->closure) {
366 10 : zval_ptr_dtor(&alfi->closure);
367 : }
368 45 : }
369 :
370 : /* {{{ proto void spl_autoload_call(string class_name)
371 : Try all registerd autoload function to load the requested class */
372 : PHP_FUNCTION(spl_autoload_call)
373 25 : {
374 25 : zval *class_name, *retval = NULL;
375 : int class_name_len;
376 : char *func_name, *lc_name;
377 : uint func_name_len;
378 : ulong dummy;
379 : HashPosition function_pos;
380 : autoload_func_info *alfi;
381 :
382 25 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &class_name) == FAILURE || Z_TYPE_P(class_name) != IS_STRING) {
383 0 : return;
384 : }
385 :
386 25 : if (SPL_G(autoload_functions)) {
387 25 : int l_autoload_running = SPL_G(autoload_running);
388 25 : SPL_G(autoload_running) = 1;
389 25 : class_name_len = Z_STRLEN_P(class_name);
390 25 : lc_name = zend_str_tolower_dup(Z_STRVAL_P(class_name), class_name_len);
391 25 : zend_hash_internal_pointer_reset_ex(SPL_G(autoload_functions), &function_pos);
392 82 : while(zend_hash_has_more_elements_ex(SPL_G(autoload_functions), &function_pos) == SUCCESS) {
393 41 : zend_hash_get_current_key_ex(SPL_G(autoload_functions), &func_name, &func_name_len, &dummy, 0, &function_pos);
394 41 : zend_hash_get_current_data_ex(SPL_G(autoload_functions), (void **) &alfi, &function_pos);
395 41 : zend_call_method(alfi->obj ? &alfi->obj : NULL, alfi->ce, &alfi->func_ptr, func_name, func_name_len, &retval, 1, class_name, NULL TSRMLS_CC);
396 41 : zend_exception_save(TSRMLS_C);
397 41 : if (retval) {
398 28 : zval_ptr_dtor(&retval);
399 : }
400 41 : if (zend_hash_exists(EG(class_table), lc_name, class_name_len + 1)) {
401 9 : break;
402 : }
403 32 : zend_hash_move_forward_ex(SPL_G(autoload_functions), &function_pos);
404 : }
405 25 : zend_exception_restore(TSRMLS_C);
406 25 : efree(lc_name);
407 25 : SPL_G(autoload_running) = l_autoload_running;
408 : } else {
409 : /* do not use or overwrite &EG(autoload_func) here */
410 0 : zend_call_method_with_1_params(NULL, NULL, NULL, "spl_autoload", NULL, class_name);
411 : }
412 : } /* }}} */
413 :
414 : #define HT_MOVE_TAIL_TO_HEAD(ht) \
415 : (ht)->pListTail->pListNext = (ht)->pListHead; \
416 : (ht)->pListHead = (ht)->pListTail; \
417 : (ht)->pListTail = (ht)->pListHead->pListLast; \
418 : (ht)->pListHead->pListNext->pListLast = (ht)->pListHead;\
419 : (ht)->pListTail->pListNext = NULL; \
420 : (ht)->pListHead->pListLast = NULL;
421 :
422 : /* {{{ proto bool spl_autoload_register([mixed autoload_function = "spl_autoload" [, throw = true [, prepend]]])
423 : Register given function as __autoload() implementation */
424 : PHP_FUNCTION(spl_autoload_register)
425 67 : {
426 67 : char *func_name, *error = NULL;
427 : int func_name_len;
428 67 : char *lc_name = NULL;
429 67 : zval *zcallable = NULL;
430 67 : zend_bool do_throw = 1;
431 67 : zend_bool prepend = 0;
432 : zend_function *spl_func_ptr;
433 : autoload_func_info alfi;
434 : zval *obj_ptr;
435 : zend_fcall_info_cache fcc;
436 :
437 67 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "|zbb", &zcallable, &do_throw, &prepend) == FAILURE) {
438 0 : return;
439 : }
440 :
441 67 : if (ZEND_NUM_ARGS()) {
442 63 : if (Z_TYPE_P(zcallable) == IS_STRING) {
443 30 : if (Z_STRLEN_P(zcallable) == sizeof("spl_autoload_call") - 1) {
444 0 : if (!zend_binary_strcasecmp(Z_STRVAL_P(zcallable), sizeof("spl_autoload_call"), "spl_autoload_call", sizeof("spl_autoload_call"))) {
445 0 : if (do_throw) {
446 0 : zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Function spl_autoload_call() cannot be registered");
447 : }
448 0 : RETURN_FALSE;
449 : }
450 : }
451 : }
452 :
453 63 : if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_STRICT, &func_name, &func_name_len, &fcc, &error TSRMLS_CC)) {
454 12 : alfi.ce = fcc.calling_scope;
455 12 : alfi.func_ptr = fcc.function_handler;
456 12 : obj_ptr = fcc.object_ptr;
457 12 : if (Z_TYPE_P(zcallable) == IS_ARRAY) {
458 7 : if (!obj_ptr && alfi.func_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
459 3 : if (do_throw) {
460 3 : zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Passed array specifies a non static method but no object (%s)", error);
461 : }
462 3 : if (error) {
463 3 : efree(error);
464 : }
465 3 : efree(func_name);
466 3 : RETURN_FALSE;
467 : }
468 4 : else if (do_throw) {
469 4 : zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Passed array does not specify %s %smethod (%s)", alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
470 : }
471 4 : if (error) {
472 4 : efree(error);
473 : }
474 4 : efree(func_name);
475 4 : RETURN_FALSE;
476 5 : } else if (Z_TYPE_P(zcallable) == IS_STRING) {
477 5 : if (do_throw) {
478 5 : zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Function '%s' not %s (%s)", func_name, alfi.func_ptr ? "callable" : "found", error);
479 : }
480 5 : if (error) {
481 5 : efree(error);
482 : }
483 5 : efree(func_name);
484 5 : RETURN_FALSE;
485 : } else {
486 0 : if (do_throw) {
487 0 : zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Illegal value passed (%s)", error);
488 : }
489 0 : if (error) {
490 0 : efree(error);
491 : }
492 0 : efree(func_name);
493 0 : RETURN_FALSE;
494 : }
495 : }
496 51 : alfi.closure = NULL;
497 51 : alfi.ce = fcc.calling_scope;
498 51 : alfi.func_ptr = fcc.function_handler;
499 51 : obj_ptr = fcc.object_ptr;
500 51 : if (error) {
501 0 : efree(error);
502 : }
503 :
504 51 : lc_name = safe_emalloc(func_name_len, 1, sizeof(long) + 1);
505 51 : zend_str_tolower_copy(lc_name, func_name, func_name_len);
506 51 : efree(func_name);
507 :
508 51 : if (Z_TYPE_P(zcallable) == IS_OBJECT) {
509 12 : alfi.closure = zcallable;
510 12 : Z_ADDREF_P(zcallable);
511 :
512 12 : lc_name = erealloc(lc_name, func_name_len + 2 + sizeof(zend_object_handle));
513 12 : memcpy(lc_name + func_name_len, &Z_OBJ_HANDLE_P(zcallable),
514 : sizeof(zend_object_handle));
515 12 : func_name_len += sizeof(zend_object_handle);
516 12 : lc_name[func_name_len] = '\0';
517 : }
518 :
519 51 : if (SPL_G(autoload_functions) && zend_hash_exists(SPL_G(autoload_functions), (char*)lc_name, func_name_len+1)) {
520 7 : if (alfi.closure) {
521 2 : Z_DELREF_P(zcallable);
522 : }
523 7 : goto skip;
524 : }
525 :
526 56 : if (obj_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
527 : /* add object id to the hash to ensure uniqueness, for more reference look at bug #40091 */
528 12 : lc_name = erealloc(lc_name, func_name_len + 2 + sizeof(zend_object_handle));
529 12 : memcpy(lc_name + func_name_len, &Z_OBJ_HANDLE_P(obj_ptr), sizeof(zend_object_handle));
530 12 : func_name_len += sizeof(zend_object_handle);
531 12 : lc_name[func_name_len] = '\0';
532 12 : alfi.obj = obj_ptr;
533 12 : Z_ADDREF_P(alfi.obj);
534 : } else {
535 32 : alfi.obj = NULL;
536 : }
537 :
538 44 : if (!SPL_G(autoload_functions)) {
539 21 : ALLOC_HASHTABLE(SPL_G(autoload_functions));
540 21 : zend_hash_init(SPL_G(autoload_functions), 1, NULL, (dtor_func_t) autoload_func_info_dtor, 0);
541 : }
542 :
543 44 : zend_hash_find(EG(function_table), "spl_autoload", sizeof("spl_autoload"), (void **) &spl_func_ptr);
544 :
545 44 : if (EG(autoload_func) == spl_func_ptr) { /* registered already, so we insert that first */
546 : autoload_func_info spl_alfi;
547 :
548 1 : spl_alfi.func_ptr = spl_func_ptr;
549 1 : spl_alfi.obj = NULL;
550 1 : spl_alfi.ce = NULL;
551 1 : spl_alfi.closure = NULL;
552 1 : zend_hash_add(SPL_G(autoload_functions), "spl_autoload", sizeof("spl_autoload"), &spl_alfi, sizeof(autoload_func_info), NULL);
553 1 : if (prepend && SPL_G(autoload_functions)->nNumOfElements > 1) {
554 : /* Move the newly created element to the head of the hashtable */
555 0 : HT_MOVE_TAIL_TO_HEAD(SPL_G(autoload_functions));
556 : }
557 : }
558 :
559 44 : zend_hash_add(SPL_G(autoload_functions), lc_name, func_name_len+1, &alfi.func_ptr, sizeof(autoload_func_info), NULL);
560 44 : if (prepend && SPL_G(autoload_functions)->nNumOfElements > 1) {
561 : /* Move the newly created element to the head of the hashtable */
562 2 : HT_MOVE_TAIL_TO_HEAD(SPL_G(autoload_functions));
563 : }
564 51 : skip:
565 51 : efree(lc_name);
566 : }
567 :
568 55 : if (SPL_G(autoload_functions)) {
569 51 : zend_hash_find(EG(function_table), "spl_autoload_call", sizeof("spl_autoload_call"), (void **) &EG(autoload_func));
570 : } else {
571 4 : zend_hash_find(EG(function_table), "spl_autoload", sizeof("spl_autoload"), (void **) &EG(autoload_func));
572 : }
573 55 : RETURN_TRUE;
574 : } /* }}} */
575 :
576 : /* {{{ proto bool spl_autoload_unregister(mixed autoload_function)
577 : Unregister given function as __autoload() implementation */
578 : PHP_FUNCTION(spl_autoload_unregister)
579 14 : {
580 14 : char *func_name, *error = NULL;
581 : int func_name_len;
582 14 : char *lc_name = NULL;
583 : zval *zcallable;
584 14 : int success = FAILURE;
585 : zend_function *spl_func_ptr;
586 : zval *obj_ptr;
587 : zend_fcall_info_cache fcc;
588 :
589 14 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zcallable) == FAILURE) {
590 0 : return;
591 : }
592 :
593 14 : if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_CHECK_SYNTAX_ONLY, &func_name, &func_name_len, &fcc, &error TSRMLS_CC)) {
594 0 : zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Unable to unregister invalid function (%s)", error);
595 0 : if (error) {
596 0 : efree(error);
597 : }
598 0 : if (func_name) {
599 0 : efree(func_name);
600 : }
601 0 : RETURN_FALSE;
602 : }
603 14 : obj_ptr = fcc.object_ptr;
604 14 : if (error) {
605 0 : efree(error);
606 : }
607 :
608 14 : lc_name = safe_emalloc(func_name_len, 1, sizeof(long) + 1);
609 14 : zend_str_tolower_copy(lc_name, func_name, func_name_len);
610 14 : efree(func_name);
611 :
612 14 : if (Z_TYPE_P(zcallable) == IS_OBJECT) {
613 2 : lc_name = erealloc(lc_name, func_name_len + 2 + sizeof(zend_object_handle));
614 2 : memcpy(lc_name + func_name_len, &Z_OBJ_HANDLE_P(zcallable),
615 : sizeof(zend_object_handle));
616 2 : func_name_len += sizeof(zend_object_handle);
617 2 : lc_name[func_name_len] = '\0';
618 : }
619 :
620 14 : if (SPL_G(autoload_functions)) {
621 13 : if (func_name_len == sizeof("spl_autoload_call")-1 && !strcmp(lc_name, "spl_autoload_call")) {
622 : /* remove all */
623 1 : zend_hash_destroy(SPL_G(autoload_functions));
624 1 : FREE_HASHTABLE(SPL_G(autoload_functions));
625 1 : SPL_G(autoload_functions) = NULL;
626 1 : EG(autoload_func) = NULL;
627 1 : success = SUCCESS;
628 : } else {
629 : /* remove specific */
630 11 : success = zend_hash_del(SPL_G(autoload_functions), lc_name, func_name_len+1);
631 11 : if (success != SUCCESS && obj_ptr) {
632 2 : lc_name = erealloc(lc_name, func_name_len + 2 + sizeof(zend_object_handle));
633 2 : memcpy(lc_name + func_name_len, &Z_OBJ_HANDLE_P(obj_ptr), sizeof(zend_object_handle));
634 2 : func_name_len += sizeof(zend_object_handle);
635 2 : lc_name[func_name_len] = '\0';
636 2 : success = zend_hash_del(SPL_G(autoload_functions), lc_name, func_name_len+1);
637 : }
638 : }
639 2 : } else if (func_name_len == sizeof("spl_autoload")-1 && !strcmp(lc_name, "spl_autoload")) {
640 : /* register single spl_autoload() */
641 2 : zend_hash_find(EG(function_table), "spl_autoload", sizeof("spl_autoload"), (void **) &spl_func_ptr);
642 :
643 2 : if (EG(autoload_func) == spl_func_ptr) {
644 2 : success = SUCCESS;
645 2 : EG(autoload_func) = NULL;
646 : }
647 : }
648 :
649 14 : efree(lc_name);
650 14 : RETURN_BOOL(success == SUCCESS);
651 : } /* }}} */
652 :
653 : /* {{{ proto false|array spl_autoload_functions()
654 : Return all registered __autoload() functionns */
655 : PHP_FUNCTION(spl_autoload_functions)
656 27 : {
657 : zend_function *fptr;
658 : HashPosition function_pos;
659 : autoload_func_info *alfi;
660 :
661 27 : if (!EG(autoload_func)) {
662 4 : if (zend_hash_find(EG(function_table), ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME), (void **) &fptr) == SUCCESS) {
663 0 : array_init(return_value);
664 0 : add_next_index_stringl(return_value, ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME)-1, 1);
665 0 : return;
666 : }
667 4 : RETURN_FALSE;
668 : }
669 :
670 23 : zend_hash_find(EG(function_table), "spl_autoload_call", sizeof("spl_autoload_call"), (void **) &fptr);
671 :
672 23 : if (EG(autoload_func) == fptr) {
673 21 : array_init(return_value);
674 21 : zend_hash_internal_pointer_reset_ex(SPL_G(autoload_functions), &function_pos);
675 63 : while(zend_hash_has_more_elements_ex(SPL_G(autoload_functions), &function_pos) == SUCCESS) {
676 21 : zend_hash_get_current_data_ex(SPL_G(autoload_functions), (void **) &alfi, &function_pos);
677 21 : if (alfi->closure) {
678 3 : Z_ADDREF_P(alfi->closure);
679 3 : add_next_index_zval(return_value, alfi->closure);
680 18 : } else if (alfi->func_ptr->common.scope) {
681 : zval *tmp;
682 9 : MAKE_STD_ZVAL(tmp);
683 9 : array_init(tmp);
684 :
685 9 : if (alfi->obj) {
686 4 : Z_ADDREF_P(alfi->obj);
687 4 : add_next_index_zval(tmp, alfi->obj);
688 : } else {
689 5 : add_next_index_string(tmp, alfi->ce->name, 1);
690 : }
691 9 : add_next_index_string(tmp, alfi->func_ptr->common.function_name, 1);
692 9 : add_next_index_zval(return_value, tmp);
693 : } else
694 9 : add_next_index_string(return_value, alfi->func_ptr->common.function_name, 1);
695 :
696 21 : zend_hash_move_forward_ex(SPL_G(autoload_functions), &function_pos);
697 : }
698 21 : return;
699 : }
700 :
701 2 : array_init(return_value);
702 2 : add_next_index_string(return_value, EG(autoload_func)->common.function_name, 1);
703 : } /* }}} */
704 :
705 : /* {{{ proto string spl_object_hash(object obj)
706 : Return hash id for given object */
707 : PHP_FUNCTION(spl_object_hash)
708 3 : {
709 : zval *obj;
710 : char* hash;
711 :
712 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
713 2 : return;
714 : }
715 :
716 1 : hash = emalloc(33);
717 1 : php_spl_object_hash(obj, hash TSRMLS_CC);
718 :
719 1 : RETVAL_STRING(hash, 0);
720 : }
721 : /* }}} */
722 :
723 : PHPAPI void php_spl_object_hash(zval *obj, char *result TSRMLS_DC) /* {{{*/
724 28 : {
725 : intptr_t hash_handle, hash_handlers;
726 : char *hex;
727 :
728 28 : if (!SPL_G(hash_mask_init)) {
729 6 : if (!BG(mt_rand_is_seeded)) {
730 6 : php_mt_srand(GENERATE_SEED() TSRMLS_CC);
731 : }
732 :
733 6 : SPL_G(hash_mask_handle) = (intptr_t)(php_mt_rand(TSRMLS_C) >> 1);
734 6 : SPL_G(hash_mask_handlers) = (intptr_t)(php_mt_rand(TSRMLS_C) >> 1);
735 6 : SPL_G(hash_mask_init) = 1;
736 : }
737 :
738 28 : hash_handle = SPL_G(hash_mask_handle)^(intptr_t)Z_OBJ_HANDLE_P(obj);
739 28 : hash_handlers = SPL_G(hash_mask_handlers)^(intptr_t)Z_OBJ_HT_P(obj);
740 :
741 28 : spprintf(&hex, 32, "%016x%016x", hash_handle, hash_handlers);
742 :
743 28 : strlcpy(result, hex, 33);
744 28 : efree(hex);
745 28 : }
746 : /* }}} */
747 :
748 : int spl_build_class_list_string(zval **entry, char **list TSRMLS_DC) /* {{{ */
749 2268 : {
750 : char *res;
751 :
752 2268 : spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_PP(entry));
753 2268 : efree(*list);
754 2268 : *list = res;
755 2268 : return ZEND_HASH_APPLY_KEEP;
756 : } /* }}} */
757 :
758 : /* {{{ PHP_MINFO(spl)
759 : */
760 : PHP_MINFO_FUNCTION(spl)
761 42 : {
762 : zval list;
763 : char *strg;
764 :
765 42 : php_info_print_table_start();
766 42 : php_info_print_table_header(2, "SPL support", "enabled");
767 :
768 42 : INIT_PZVAL(&list);
769 42 : array_init(&list);
770 42 : SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE)
771 42 : strg = estrdup("");
772 42 : zend_hash_apply_with_argument(Z_ARRVAL_P(&list), (apply_func_arg_t)spl_build_class_list_string, &strg TSRMLS_CC);
773 42 : zval_dtor(&list);
774 42 : php_info_print_table_row(2, "Interfaces", strg + 2);
775 42 : efree(strg);
776 :
777 42 : INIT_PZVAL(&list);
778 42 : array_init(&list);
779 42 : SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE)
780 42 : strg = estrdup("");
781 42 : zend_hash_apply_with_argument(Z_ARRVAL_P(&list), (apply_func_arg_t)spl_build_class_list_string, &strg TSRMLS_CC);
782 42 : zval_dtor(&list);
783 42 : php_info_print_table_row(2, "Classes", strg + 2);
784 42 : efree(strg);
785 :
786 42 : php_info_print_table_end();
787 42 : }
788 : /* }}} */
789 :
790 : /* {{{ arginfo */
791 : ZEND_BEGIN_ARG_INFO_EX(arginfo_iterator_to_array, 0, 0, 1)
792 : ZEND_ARG_OBJ_INFO(0, iterator, Traversable, 0)
793 : ZEND_ARG_INFO(0, use_keys)
794 : ZEND_END_ARG_INFO();
795 :
796 : ZEND_BEGIN_ARG_INFO(arginfo_iterator, 0)
797 : ZEND_ARG_OBJ_INFO(0, iterator, Traversable, 0)
798 : ZEND_END_ARG_INFO();
799 :
800 : ZEND_BEGIN_ARG_INFO_EX(arginfo_iterator_apply, 0, 0, 2)
801 : ZEND_ARG_OBJ_INFO(0, iterator, Traversable, 0)
802 : ZEND_ARG_INFO(0, function)
803 : ZEND_ARG_ARRAY_INFO(0, args, 1)
804 : ZEND_END_ARG_INFO();
805 :
806 : ZEND_BEGIN_ARG_INFO_EX(arginfo_class_parents, 0, 0, 1)
807 : ZEND_ARG_INFO(0, instance)
808 : ZEND_ARG_INFO(0, autoload)
809 : ZEND_END_ARG_INFO()
810 :
811 : ZEND_BEGIN_ARG_INFO_EX(arginfo_class_implements, 0, 0, 1)
812 : ZEND_ARG_INFO(0, what)
813 : ZEND_ARG_INFO(0, autoload)
814 : ZEND_END_ARG_INFO()
815 :
816 : ZEND_BEGIN_ARG_INFO(arginfo_spl_classes, 0)
817 : ZEND_END_ARG_INFO()
818 :
819 : ZEND_BEGIN_ARG_INFO(arginfo_spl_autoload_functions, 0)
820 : ZEND_END_ARG_INFO()
821 :
822 : ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_autoload, 0, 0, 1)
823 : ZEND_ARG_INFO(0, class_name)
824 : ZEND_ARG_INFO(0, file_extensions)
825 : ZEND_END_ARG_INFO()
826 :
827 : ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_autoload_extensions, 0, 0, 0)
828 : ZEND_ARG_INFO(0, file_extensions)
829 : ZEND_END_ARG_INFO()
830 :
831 : ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_autoload_call, 0, 0, 1)
832 : ZEND_ARG_INFO(0, class_name)
833 : ZEND_END_ARG_INFO()
834 :
835 : ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_autoload_register, 0, 0, 0)
836 : ZEND_ARG_INFO(0, autoload_function)
837 : ZEND_END_ARG_INFO()
838 :
839 : ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_autoload_unregister, 0, 0, 1)
840 : ZEND_ARG_INFO(0, autoload_function)
841 : ZEND_END_ARG_INFO()
842 :
843 : ZEND_BEGIN_ARG_INFO_EX(arginfo_spl_object_hash, 0, 0, 1)
844 : ZEND_ARG_INFO(0, obj)
845 : ZEND_END_ARG_INFO()
846 : /* }}} */
847 :
848 : /* {{{ spl_functions
849 : */
850 : const zend_function_entry spl_functions[] = {
851 : PHP_FE(spl_classes, arginfo_spl_classes)
852 : PHP_FE(spl_autoload, arginfo_spl_autoload)
853 : PHP_FE(spl_autoload_extensions, arginfo_spl_autoload_extensions)
854 : PHP_FE(spl_autoload_register, arginfo_spl_autoload_register)
855 : PHP_FE(spl_autoload_unregister, arginfo_spl_autoload_unregister)
856 : PHP_FE(spl_autoload_functions, arginfo_spl_autoload_functions)
857 : PHP_FE(spl_autoload_call, arginfo_spl_autoload_call)
858 : PHP_FE(class_parents, arginfo_class_parents)
859 : PHP_FE(class_implements, arginfo_class_implements)
860 : PHP_FE(spl_object_hash, arginfo_spl_object_hash)
861 : #ifdef SPL_ITERATORS_H
862 : PHP_FE(iterator_to_array, arginfo_iterator_to_array)
863 : PHP_FE(iterator_count, arginfo_iterator)
864 : PHP_FE(iterator_apply, arginfo_iterator_apply)
865 : #endif /* SPL_ITERATORS_H */
866 : {NULL, NULL, NULL}
867 : };
868 : /* }}} */
869 :
870 : /* {{{ PHP_MINIT_FUNCTION(spl)
871 : */
872 : PHP_MINIT_FUNCTION(spl)
873 17633 : {
874 17633 : PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
875 17633 : PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
876 17633 : PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
877 17633 : PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
878 17633 : PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
879 17633 : PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
880 17633 : PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
881 17633 : PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
882 :
883 17633 : return SUCCESS;
884 : }
885 : /* }}} */
886 :
887 : PHP_RINIT_FUNCTION(spl) /* {{{ */
888 17619 : {
889 17619 : SPL_G(autoload_extensions) = NULL;
890 17619 : SPL_G(autoload_extensions_len) = 0;
891 17619 : SPL_G(autoload_functions) = NULL;
892 17619 : SPL_G(hash_mask_init) = 0;
893 17619 : return SUCCESS;
894 : } /* }}} */
895 :
896 : PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
897 17651 : {
898 17651 : if (SPL_G(autoload_extensions)) {
899 1 : efree(SPL_G(autoload_extensions));
900 1 : SPL_G(autoload_extensions) = NULL;
901 1 : SPL_G(autoload_extensions_len) = 0;
902 : }
903 17651 : if (SPL_G(autoload_functions)) {
904 20 : zend_hash_destroy(SPL_G(autoload_functions));
905 20 : FREE_HASHTABLE(SPL_G(autoload_functions));
906 20 : SPL_G(autoload_functions) = NULL;
907 : }
908 17651 : if (SPL_G(hash_mask_init)) {
909 6 : SPL_G(hash_mask_init) = 0;
910 : }
911 17651 : return SUCCESS;
912 : } /* }}} */
913 :
914 : /* {{{ spl_module_entry
915 : */
916 : zend_module_entry spl_module_entry = {
917 : STANDARD_MODULE_HEADER,
918 : "SPL",
919 : spl_functions,
920 : PHP_MINIT(spl),
921 : NULL,
922 : PHP_RINIT(spl),
923 : PHP_RSHUTDOWN(spl),
924 : PHP_MINFO(spl),
925 : "0.2",
926 : PHP_MODULE_GLOBALS(spl),
927 : PHP_GINIT(spl),
928 : NULL,
929 : NULL,
930 : STANDARD_MODULE_PROPERTIES_EX
931 : };
932 : /* }}} */
933 :
934 : /*
935 : * Local variables:
936 : * tab-width: 4
937 : * c-basic-offset: 4
938 : * End:
939 : * vim600: fdm=marker
940 : * vim: noet sw=4 ts=4
941 : */
|