1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Marcus Boerger <helly@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include "php.h"
22 #include "php_ini.h"
23 #include "php_main.h"
24 #include "ext/standard/info.h"
25 #include "php_spl.h"
26 #include "php_spl_arginfo.h"
27 #include "spl_functions.h"
28 #include "spl_engine.h"
29 #include "spl_array.h"
30 #include "spl_directory.h"
31 #include "spl_iterators.h"
32 #include "spl_exceptions.h"
33 #include "spl_observer.h"
34 #include "spl_dllist.h"
35 #include "spl_fixedarray.h"
36 #include "spl_heap.h"
37 #include "zend_exceptions.h"
38 #include "zend_interfaces.h"
39 #include "ext/standard/php_mt_rand.h"
40 #include "main/snprintf.h"
41
42 #ifdef COMPILE_DL_SPL
43 ZEND_GET_MODULE(spl)
44 #endif
45
ZEND_DECLARE_MODULE_GLOBALS(spl)46 ZEND_DECLARE_MODULE_GLOBALS(spl)
47
48 #define SPL_DEFAULT_FILE_EXTENSIONS ".inc,.php"
49
50 /* {{{ PHP_GINIT_FUNCTION */
51 static PHP_GINIT_FUNCTION(spl)
52 {
53 spl_globals->autoload_extensions = NULL;
54 spl_globals->autoload_functions = NULL;
55 }
56 /* }}} */
57
spl_find_ce_by_name(zend_string * name,zend_bool autoload)58 static zend_class_entry * spl_find_ce_by_name(zend_string *name, zend_bool autoload)
59 {
60 zend_class_entry *ce;
61
62 if (!autoload) {
63 zend_string *lc_name = zend_string_tolower(name);
64
65 ce = zend_hash_find_ptr(EG(class_table), lc_name);
66 zend_string_release(lc_name);
67 } else {
68 ce = zend_lookup_class(name);
69 }
70 if (ce == NULL) {
71 php_error_docref(NULL, E_WARNING, "Class %s does not exist%s", ZSTR_VAL(name), autoload ? " and could not be loaded" : "");
72 return NULL;
73 }
74
75 return ce;
76 }
77
78 /* {{{ Return an array containing the names of all parent classes */
PHP_FUNCTION(class_parents)79 PHP_FUNCTION(class_parents)
80 {
81 zval *obj;
82 zend_class_entry *parent_class, *ce;
83 zend_bool autoload = 1;
84
85 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
86 RETURN_THROWS();
87 }
88
89 if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
90 zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
91 RETURN_THROWS();
92 }
93
94 if (Z_TYPE_P(obj) == IS_STRING) {
95 if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
96 RETURN_FALSE;
97 }
98 } else {
99 ce = Z_OBJCE_P(obj);
100 }
101
102 array_init(return_value);
103 parent_class = ce->parent;
104 while (parent_class) {
105 spl_add_class_name(return_value, parent_class, 0, 0);
106 parent_class = parent_class->parent;
107 }
108 }
109 /* }}} */
110
111 /* {{{ Return all classes and interfaces implemented by SPL */
PHP_FUNCTION(class_implements)112 PHP_FUNCTION(class_implements)
113 {
114 zval *obj;
115 zend_bool autoload = 1;
116 zend_class_entry *ce;
117
118 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
119 RETURN_THROWS();
120 }
121 if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
122 zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
123 RETURN_THROWS();
124 }
125
126 if (Z_TYPE_P(obj) == IS_STRING) {
127 if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
128 RETURN_FALSE;
129 }
130 } else {
131 ce = Z_OBJCE_P(obj);
132 }
133
134 array_init(return_value);
135 spl_add_interfaces(return_value, ce, 1, ZEND_ACC_INTERFACE);
136 }
137 /* }}} */
138
139 /* {{{ Return all traits used by a class. */
PHP_FUNCTION(class_uses)140 PHP_FUNCTION(class_uses)
141 {
142 zval *obj;
143 zend_bool autoload = 1;
144 zend_class_entry *ce;
145
146 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
147 RETURN_THROWS();
148 }
149 if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
150 zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
151 RETURN_THROWS();
152 }
153
154 if (Z_TYPE_P(obj) == IS_STRING) {
155 if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
156 RETURN_FALSE;
157 }
158 } else {
159 ce = Z_OBJCE_P(obj);
160 }
161
162 array_init(return_value);
163 spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT);
164 }
165 /* }}} */
166
167 #define SPL_ADD_CLASS(class_name, z_list, sub, allow, ce_flags) \
168 spl_add_classes(spl_ce_ ## class_name, z_list, sub, allow, ce_flags)
169
170 #define SPL_LIST_CLASSES(z_list, sub, allow, ce_flags) \
171 SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \
172 SPL_ADD_CLASS(ArrayIterator, z_list, sub, allow, ce_flags); \
173 SPL_ADD_CLASS(ArrayObject, z_list, sub, allow, ce_flags); \
174 SPL_ADD_CLASS(BadFunctionCallException, z_list, sub, allow, ce_flags); \
175 SPL_ADD_CLASS(BadMethodCallException, z_list, sub, allow, ce_flags); \
176 SPL_ADD_CLASS(CachingIterator, z_list, sub, allow, ce_flags); \
177 SPL_ADD_CLASS(CallbackFilterIterator, z_list, sub, allow, ce_flags); \
178 SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \
179 SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
180 SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
181 SPL_ADD_CLASS(FilesystemIterator, z_list, sub, allow, ce_flags); \
182 SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
183 SPL_ADD_CLASS(GlobIterator, z_list, sub, allow, ce_flags); \
184 SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
185 SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \
186 SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \
187 SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \
188 SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \
189 SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \
190 SPL_ADD_CLASS(MultipleIterator, z_list, sub, allow, ce_flags); \
191 SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \
192 SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \
193 SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \
194 SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \
195 SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \
196 SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \
197 SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \
198 SPL_ADD_CLASS(RecursiveArrayIterator, z_list, sub, allow, ce_flags); \
199 SPL_ADD_CLASS(RecursiveCachingIterator, z_list, sub, allow, ce_flags); \
200 SPL_ADD_CLASS(RecursiveCallbackFilterIterator, z_list, sub, allow, ce_flags); \
201 SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \
202 SPL_ADD_CLASS(RecursiveFilterIterator, z_list, sub, allow, ce_flags); \
203 SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \
204 SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \
205 SPL_ADD_CLASS(RecursiveRegexIterator, z_list, sub, allow, ce_flags); \
206 SPL_ADD_CLASS(RecursiveTreeIterator, z_list, sub, allow, ce_flags); \
207 SPL_ADD_CLASS(RegexIterator, z_list, sub, allow, ce_flags); \
208 SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \
209 SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \
210 SPL_ADD_CLASS(SplDoublyLinkedList, z_list, sub, allow, ce_flags); \
211 SPL_ADD_CLASS(SplFileInfo, z_list, sub, allow, ce_flags); \
212 SPL_ADD_CLASS(SplFileObject, z_list, sub, allow, ce_flags); \
213 SPL_ADD_CLASS(SplFixedArray, z_list, sub, allow, ce_flags); \
214 SPL_ADD_CLASS(SplHeap, z_list, sub, allow, ce_flags); \
215 SPL_ADD_CLASS(SplMinHeap, z_list, sub, allow, ce_flags); \
216 SPL_ADD_CLASS(SplMaxHeap, z_list, sub, allow, ce_flags); \
217 SPL_ADD_CLASS(SplObjectStorage, z_list, sub, allow, ce_flags); \
218 SPL_ADD_CLASS(SplObserver, z_list, sub, allow, ce_flags); \
219 SPL_ADD_CLASS(SplPriorityQueue, z_list, sub, allow, ce_flags); \
220 SPL_ADD_CLASS(SplQueue, z_list, sub, allow, ce_flags); \
221 SPL_ADD_CLASS(SplStack, z_list, sub, allow, ce_flags); \
222 SPL_ADD_CLASS(SplSubject, z_list, sub, allow, ce_flags); \
223 SPL_ADD_CLASS(SplTempFileObject, z_list, sub, allow, ce_flags); \
224 SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
225 SPL_ADD_CLASS(UnexpectedValueException, z_list, sub, allow, ce_flags); \
226
227 /* {{{ Return an array containing the names of all clsses and interfaces defined in SPL */
PHP_FUNCTION(spl_classes)228 PHP_FUNCTION(spl_classes)
229 {
230 if (zend_parse_parameters_none() == FAILURE) {
231 RETURN_THROWS();
232 }
233
234 array_init(return_value);
235
236 SPL_LIST_CLASSES(return_value, 0, 0, 0)
237 }
238 /* }}} */
239
spl_autoload(zend_string * class_name,zend_string * lc_name,const char * ext,int ext_len)240 static int spl_autoload(zend_string *class_name, zend_string *lc_name, const char *ext, int ext_len) /* {{{ */
241 {
242 char *class_file;
243 int class_file_len;
244 zval dummy;
245 zend_file_handle file_handle;
246 zend_op_array *new_op_array;
247 zval result;
248 int ret;
249
250 class_file_len = (int)spprintf(&class_file, 0, "%s%.*s", ZSTR_VAL(lc_name), ext_len, ext);
251
252 #if DEFAULT_SLASH != '\\'
253 {
254 char *ptr = class_file;
255 char *end = ptr + class_file_len;
256
257 while ((ptr = memchr(ptr, '\\', (end - ptr))) != NULL) {
258 *ptr = DEFAULT_SLASH;
259 }
260 }
261 #endif
262
263 ret = php_stream_open_for_zend_ex(class_file, &file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
264
265 if (ret == SUCCESS) {
266 zend_string *opened_path;
267 if (!file_handle.opened_path) {
268 file_handle.opened_path = zend_string_init(class_file, class_file_len, 0);
269 }
270 opened_path = zend_string_copy(file_handle.opened_path);
271 ZVAL_NULL(&dummy);
272 if (zend_hash_add(&EG(included_files), opened_path, &dummy)) {
273 new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
274 zend_destroy_file_handle(&file_handle);
275 } else {
276 new_op_array = NULL;
277 zend_file_handle_dtor(&file_handle);
278 }
279 zend_string_release_ex(opened_path, 0);
280 if (new_op_array) {
281 ZVAL_UNDEF(&result);
282 zend_execute(new_op_array, &result);
283
284 destroy_op_array(new_op_array);
285 efree(new_op_array);
286 if (!EG(exception)) {
287 zval_ptr_dtor(&result);
288 }
289
290 efree(class_file);
291 return zend_hash_exists(EG(class_table), lc_name);
292 }
293 }
294 efree(class_file);
295 return 0;
296 } /* }}} */
297
298 /* {{{ Default autoloader implementation */
PHP_FUNCTION(spl_autoload)299 PHP_FUNCTION(spl_autoload)
300 {
301 int pos_len, pos1_len;
302 char *pos, *pos1;
303 zend_string *class_name, *lc_name, *file_exts = NULL;
304
305 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &class_name, &file_exts) == FAILURE) {
306 RETURN_THROWS();
307 }
308
309 if (!file_exts) {
310 file_exts = SPL_G(autoload_extensions);
311 }
312
313 if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */
314 pos = SPL_DEFAULT_FILE_EXTENSIONS;
315 pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1;
316 } else {
317 pos = ZSTR_VAL(file_exts);
318 pos_len = (int)ZSTR_LEN(file_exts);
319 }
320
321 lc_name = zend_string_tolower(class_name);
322 while (pos && *pos && !EG(exception)) {
323 pos1 = strchr(pos, ',');
324 if (pos1) {
325 pos1_len = (int)(pos1 - pos);
326 } else {
327 pos1_len = pos_len;
328 }
329 if (spl_autoload(class_name, lc_name, pos, pos1_len)) {
330 break; /* loaded */
331 }
332 pos = pos1 ? pos1 + 1 : NULL;
333 pos_len = pos1? pos_len - pos1_len - 1 : 0;
334 }
335 zend_string_release(lc_name);
336 } /* }}} */
337
338 /* {{{ Register and return default file extensions for spl_autoload */
PHP_FUNCTION(spl_autoload_extensions)339 PHP_FUNCTION(spl_autoload_extensions)
340 {
341 zend_string *file_exts = NULL;
342
343 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &file_exts) == FAILURE) {
344 RETURN_THROWS();
345 }
346
347 if (file_exts) {
348 if (SPL_G(autoload_extensions)) {
349 zend_string_release_ex(SPL_G(autoload_extensions), 0);
350 }
351 SPL_G(autoload_extensions) = zend_string_copy(file_exts);
352 }
353
354 if (SPL_G(autoload_extensions) == NULL) {
355 RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1);
356 } else {
357 zend_string_addref(SPL_G(autoload_extensions));
358 RETURN_STR(SPL_G(autoload_extensions));
359 }
360 } /* }}} */
361
362 typedef struct {
363 zend_function *func_ptr;
364 zend_object *obj;
365 zend_object *closure;
366 zend_class_entry *ce;
367 } autoload_func_info;
368
autoload_func_info_destroy(autoload_func_info * alfi)369 static void autoload_func_info_destroy(autoload_func_info *alfi) {
370 if (alfi->obj) {
371 zend_object_release(alfi->obj);
372 }
373 if (alfi->func_ptr &&
374 UNEXPECTED(alfi->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
375 zend_string_release_ex(alfi->func_ptr->common.function_name, 0);
376 zend_free_trampoline(alfi->func_ptr);
377 }
378 if (alfi->closure) {
379 zend_object_release(alfi->closure);
380 }
381 efree(alfi);
382 }
383
autoload_func_info_zval_dtor(zval * element)384 static void autoload_func_info_zval_dtor(zval *element)
385 {
386 autoload_func_info_destroy(Z_PTR_P(element));
387 }
388
autoload_func_info_from_fci(zend_fcall_info * fci,zend_fcall_info_cache * fcc)389 static autoload_func_info *autoload_func_info_from_fci(
390 zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
391 autoload_func_info *alfi = emalloc(sizeof(autoload_func_info));
392 alfi->ce = fcc->calling_scope;
393 alfi->func_ptr = fcc->function_handler;
394 alfi->obj = fcc->object;
395 if (alfi->obj) {
396 GC_ADDREF(alfi->obj);
397 }
398 if (Z_TYPE(fci->function_name) == IS_OBJECT) {
399 alfi->closure = Z_OBJ(fci->function_name);
400 GC_ADDREF(alfi->closure);
401 } else {
402 alfi->closure = NULL;
403 }
404 return alfi;
405 }
406
autoload_func_info_equals(const autoload_func_info * alfi1,const autoload_func_info * alfi2)407 static zend_bool autoload_func_info_equals(
408 const autoload_func_info *alfi1, const autoload_func_info *alfi2) {
409 return alfi1->func_ptr == alfi2->func_ptr
410 && alfi1->obj == alfi2->obj
411 && alfi1->ce == alfi2->ce
412 && alfi1->closure == alfi2->closure;
413 }
414
spl_perform_autoload(zend_string * class_name,zend_string * lc_name)415 static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_string *lc_name) {
416 if (!SPL_G(autoload_functions)) {
417 return NULL;
418 }
419
420 /* We don't use ZEND_HASH_FOREACH here,
421 * because autoloaders may be added/removed during autoloading. */
422 HashPosition pos;
423 zend_hash_internal_pointer_reset_ex(SPL_G(autoload_functions), &pos);
424 while (1) {
425 autoload_func_info *alfi =
426 zend_hash_get_current_data_ptr_ex(SPL_G(autoload_functions), &pos);
427 if (!alfi) {
428 break;
429 }
430
431 zend_function *func = alfi->func_ptr;
432 if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
433 func = emalloc(sizeof(zend_op_array));
434 memcpy(func, alfi->func_ptr, sizeof(zend_op_array));
435 zend_string_addref(func->op_array.function_name);
436 }
437
438 zval param;
439 ZVAL_STR(¶m, class_name);
440 zend_call_known_function(func, alfi->obj, alfi->ce, NULL, 1, ¶m, NULL);
441 if (EG(exception)) {
442 break;
443 }
444
445 zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name);
446 if (ce) {
447 return ce;
448 }
449
450 zend_hash_move_forward_ex(SPL_G(autoload_functions), &pos);
451 }
452 return NULL;
453 }
454
455 /* {{{ Try all registered autoload function to load the requested class */
PHP_FUNCTION(spl_autoload_call)456 PHP_FUNCTION(spl_autoload_call)
457 {
458 zend_string *class_name;
459
460 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) {
461 RETURN_THROWS();
462 }
463
464 zend_string *lc_name = zend_string_tolower(class_name);
465 spl_perform_autoload(class_name, lc_name);
466 zend_string_release(lc_name);
467 } /* }}} */
468
469 #define HT_MOVE_TAIL_TO_HEAD(ht) \
470 do { \
471 Bucket tmp = (ht)->arData[(ht)->nNumUsed-1]; \
472 memmove((ht)->arData + 1, (ht)->arData, \
473 sizeof(Bucket) * ((ht)->nNumUsed - 1)); \
474 (ht)->arData[0] = tmp; \
475 zend_hash_rehash(ht); \
476 } while (0)
477
spl_find_registered_function(autoload_func_info * find_alfi)478 static Bucket *spl_find_registered_function(autoload_func_info *find_alfi) {
479 if (!SPL_G(autoload_functions)) {
480 return NULL;
481 }
482
483 autoload_func_info *alfi;
484 ZEND_HASH_FOREACH_PTR(SPL_G(autoload_functions), alfi) {
485 if (autoload_func_info_equals(alfi, find_alfi)) {
486 return _p;
487 }
488 } ZEND_HASH_FOREACH_END();
489 return NULL;
490 }
491
492 /* {{{ Register given function as autoloader */
PHP_FUNCTION(spl_autoload_register)493 PHP_FUNCTION(spl_autoload_register)
494 {
495 zend_bool do_throw = 1;
496 zend_bool prepend = 0;
497 zend_fcall_info fci = {0};
498 zend_fcall_info_cache fcc;
499 autoload_func_info *alfi;
500
501 ZEND_PARSE_PARAMETERS_START(0, 3)
502 Z_PARAM_OPTIONAL
503 Z_PARAM_FUNC_OR_NULL(fci, fcc)
504 Z_PARAM_BOOL(do_throw)
505 Z_PARAM_BOOL(prepend)
506 ZEND_PARSE_PARAMETERS_END();
507
508 if (!do_throw) {
509 php_error_docref(NULL, E_NOTICE, "Argument #2 ($do_throw) has been ignored, "
510 "spl_autoload_register() will always throw");
511 }
512
513 if (!SPL_G(autoload_functions)) {
514 ALLOC_HASHTABLE(SPL_G(autoload_functions));
515 zend_hash_init(SPL_G(autoload_functions), 1, NULL, autoload_func_info_zval_dtor, 0);
516 /* Initialize as non-packed hash table for prepend functionality. */
517 zend_hash_real_init_mixed(SPL_G(autoload_functions));
518 }
519
520 /* If first arg is not null */
521 if (ZEND_FCI_INITIALIZED(fci)) {
522 if (!fcc.function_handler) {
523 /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
524 * with it outselves. It is important that it is not refetched on every call,
525 * because calls may occur from different scopes. */
526 zend_is_callable_ex(&fci.function_name, NULL, 0, NULL, &fcc, NULL);
527 }
528
529 if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
530 fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
531 zend_argument_value_error(1, "must not be the spl_autoload_call() function");
532 RETURN_THROWS();
533 }
534
535 alfi = autoload_func_info_from_fci(&fci, &fcc);
536 if (UNEXPECTED(alfi->func_ptr == &EG(trampoline))) {
537 zend_function *copy = emalloc(sizeof(zend_op_array));
538
539 memcpy(copy, alfi->func_ptr, sizeof(zend_op_array));
540 alfi->func_ptr->common.function_name = NULL;
541 alfi->func_ptr = copy;
542 }
543 } else {
544 alfi = emalloc(sizeof(autoload_func_info));
545 alfi->func_ptr = zend_hash_str_find_ptr(
546 CG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
547 alfi->obj = NULL;
548 alfi->ce = NULL;
549 alfi->closure = NULL;
550 }
551
552 if (spl_find_registered_function(alfi)) {
553 autoload_func_info_destroy(alfi);
554 RETURN_TRUE;
555 }
556
557 zend_hash_next_index_insert_ptr(SPL_G(autoload_functions), alfi);
558 if (prepend && SPL_G(autoload_functions)->nNumOfElements > 1) {
559 /* Move the newly created element to the head of the hashtable */
560 HT_MOVE_TAIL_TO_HEAD(SPL_G(autoload_functions));
561 }
562
563 RETURN_TRUE;
564 } /* }}} */
565
566 /* {{{ Unregister given function as autoloader */
PHP_FUNCTION(spl_autoload_unregister)567 PHP_FUNCTION(spl_autoload_unregister)
568 {
569 zend_fcall_info fci;
570 zend_fcall_info_cache fcc;
571
572 if (zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fci, &fcc) == FAILURE) {
573 RETURN_THROWS();
574 }
575
576 if (fcc.function_handler && zend_string_equals_literal(
577 fcc.function_handler->common.function_name, "spl_autoload_call")) {
578 /* Don't destroy the hash table, as we might be iterating over it right now. */
579 zend_hash_clean(SPL_G(autoload_functions));
580 RETURN_TRUE;
581 }
582
583 autoload_func_info *alfi = autoload_func_info_from_fci(&fci, &fcc);
584 Bucket *p = spl_find_registered_function(alfi);
585 autoload_func_info_destroy(alfi);
586 if (p) {
587 zend_hash_del_bucket(SPL_G(autoload_functions), p);
588 RETURN_TRUE;
589 }
590
591 RETURN_FALSE;
592 } /* }}} */
593
594 /* {{{ Return all registered autoloader functions */
PHP_FUNCTION(spl_autoload_functions)595 PHP_FUNCTION(spl_autoload_functions)
596 {
597 autoload_func_info *alfi;
598
599 if (zend_parse_parameters_none() == FAILURE) {
600 RETURN_THROWS();
601 }
602
603 array_init(return_value);
604 if (SPL_G(autoload_functions)) {
605 ZEND_HASH_FOREACH_PTR(SPL_G(autoload_functions), alfi) {
606 if (alfi->closure) {
607 zval obj_zv;
608 ZVAL_OBJ_COPY(&obj_zv, alfi->closure);
609 add_next_index_zval(return_value, &obj_zv);
610 } else if (alfi->func_ptr->common.scope) {
611 zval tmp;
612
613 array_init(&tmp);
614 if (alfi->obj) {
615 zval obj_zv;
616 ZVAL_OBJ_COPY(&obj_zv, alfi->obj);
617 add_next_index_zval(&tmp, &obj_zv);
618 } else {
619 add_next_index_str(&tmp, zend_string_copy(alfi->ce->name));
620 }
621 add_next_index_str(&tmp, zend_string_copy(alfi->func_ptr->common.function_name));
622 add_next_index_zval(return_value, &tmp);
623 } else {
624 add_next_index_str(return_value, zend_string_copy(alfi->func_ptr->common.function_name));
625 }
626 } ZEND_HASH_FOREACH_END();
627 }
628 } /* }}} */
629
630 /* {{{ Return hash id for given object */
PHP_FUNCTION(spl_object_hash)631 PHP_FUNCTION(spl_object_hash)
632 {
633 zval *obj;
634
635 if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &obj) == FAILURE) {
636 RETURN_THROWS();
637 }
638
639 RETURN_NEW_STR(php_spl_object_hash(obj));
640 }
641 /* }}} */
642
643 /* {{{ Returns the integer object handle for the given object */
PHP_FUNCTION(spl_object_id)644 PHP_FUNCTION(spl_object_id)
645 {
646 zval *obj;
647
648 ZEND_PARSE_PARAMETERS_START(1, 1)
649 Z_PARAM_OBJECT(obj)
650 ZEND_PARSE_PARAMETERS_END();
651
652 RETURN_LONG((zend_long)Z_OBJ_HANDLE_P(obj));
653 }
654 /* }}} */
655
php_spl_object_hash(zval * obj)656 PHPAPI zend_string *php_spl_object_hash(zval *obj) /* {{{*/
657 {
658 intptr_t hash_handle, hash_handlers;
659
660 if (!SPL_G(hash_mask_init)) {
661 SPL_G(hash_mask_handle) = (intptr_t)(php_mt_rand() >> 1);
662 SPL_G(hash_mask_handlers) = (intptr_t)(php_mt_rand() >> 1);
663 SPL_G(hash_mask_init) = 1;
664 }
665
666 hash_handle = SPL_G(hash_mask_handle)^(intptr_t)Z_OBJ_HANDLE_P(obj);
667 hash_handlers = SPL_G(hash_mask_handlers);
668
669 return strpprintf(32, "%016zx%016zx", hash_handle, hash_handlers);
670 }
671 /* }}} */
672
spl_build_class_list_string(zval * entry,char ** list)673 static void spl_build_class_list_string(zval *entry, char **list) /* {{{ */
674 {
675 char *res;
676
677 spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry));
678 efree(*list);
679 *list = res;
680 } /* }}} */
681
682 /* {{{ PHP_MINFO(spl) */
PHP_MINFO_FUNCTION(spl)683 PHP_MINFO_FUNCTION(spl)
684 {
685 zval list, *zv;
686 char *strg;
687
688 php_info_print_table_start();
689 php_info_print_table_header(2, "SPL support", "enabled");
690
691 array_init(&list);
692 SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE)
693 strg = estrdup("");
694 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
695 spl_build_class_list_string(zv, &strg);
696 } ZEND_HASH_FOREACH_END();
697 zend_array_destroy(Z_ARR(list));
698 php_info_print_table_row(2, "Interfaces", strg + 2);
699 efree(strg);
700
701 array_init(&list);
702 SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE)
703 strg = estrdup("");
704 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
705 spl_build_class_list_string(zv, &strg);
706 } ZEND_HASH_FOREACH_END();
707 zend_array_destroy(Z_ARR(list));
708 php_info_print_table_row(2, "Classes", strg + 2);
709 efree(strg);
710
711 php_info_print_table_end();
712 }
713 /* }}} */
714
715 /* {{{ PHP_MINIT_FUNCTION(spl) */
PHP_MINIT_FUNCTION(spl)716 PHP_MINIT_FUNCTION(spl)
717 {
718 zend_autoload = spl_perform_autoload;
719
720 PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
721 PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
722 PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
723 PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
724 PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
725 PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
726 PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
727 PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
728
729 return SUCCESS;
730 }
731 /* }}} */
732
PHP_RINIT_FUNCTION(spl)733 PHP_RINIT_FUNCTION(spl) /* {{{ */
734 {
735 SPL_G(autoload_extensions) = NULL;
736 SPL_G(autoload_functions) = NULL;
737 SPL_G(hash_mask_init) = 0;
738 return SUCCESS;
739 } /* }}} */
740
PHP_RSHUTDOWN_FUNCTION(spl)741 PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
742 {
743 if (SPL_G(autoload_extensions)) {
744 zend_string_release_ex(SPL_G(autoload_extensions), 0);
745 SPL_G(autoload_extensions) = NULL;
746 }
747 if (SPL_G(autoload_functions)) {
748 zend_hash_destroy(SPL_G(autoload_functions));
749 FREE_HASHTABLE(SPL_G(autoload_functions));
750 SPL_G(autoload_functions) = NULL;
751 }
752 if (SPL_G(hash_mask_init)) {
753 SPL_G(hash_mask_init) = 0;
754 }
755 return SUCCESS;
756 } /* }}} */
757
758 /* {{{ spl_module_entry */
759 zend_module_entry spl_module_entry = {
760 STANDARD_MODULE_HEADER,
761 "SPL",
762 ext_functions,
763 PHP_MINIT(spl),
764 NULL,
765 PHP_RINIT(spl),
766 PHP_RSHUTDOWN(spl),
767 PHP_MINFO(spl),
768 PHP_SPL_VERSION,
769 PHP_MODULE_GLOBALS(spl),
770 PHP_GINIT(spl),
771 NULL,
772 NULL,
773 STANDARD_MODULE_PROPERTIES_EX
774 };
775 /* }}} */
776