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