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