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