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