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