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