1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Marcus Boerger <helly@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 #include "php.h"
22 #include "ext/standard/file.h"
23 #include "ext/standard/php_filestat.h"
24 #include "ext/standard/flock_compat.h"
25 #include "ext/standard/scanf.h"
26 #include "ext/standard/php_string.h"
27 #include "zend_exceptions.h"
28 #include "zend_interfaces.h"
29
30 #include "spl_iterators.h"
31 #include "spl_directory.h"
32 #include "spl_directory_arginfo.h"
33 #include "spl_exceptions.h"
34 #include "spl_functions.h" /* For spl_set_private_debug_info_property() */
35
36 #define SPL_HAS_FLAG(flags, test_flag) ((flags & test_flag) ? 1 : 0)
37
38 /* declare the class handlers */
39 static zend_object_handlers spl_filesystem_object_handlers;
40 /* includes handler to validate object state when retrieving methods */
41 static zend_object_handlers spl_filesystem_object_check_handlers;
42
43 /* decalre the class entry */
44 PHPAPI zend_class_entry *spl_ce_SplFileInfo;
45 PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
46 PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
47 PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
48 PHPAPI zend_class_entry *spl_ce_GlobIterator;
49 PHPAPI zend_class_entry *spl_ce_SplFileObject;
50 PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
51
52 /* Object helper */
spl_filesystem_from_obj(zend_object * obj)53 static inline spl_filesystem_object *spl_filesystem_from_obj(zend_object *obj) /* {{{ */ {
54 return (spl_filesystem_object*)((char*)(obj) - XtOffsetOf(spl_filesystem_object, std));
55 }
56 /* }}} */
57
58 /* define an overloaded iterator structure */
59 typedef struct {
60 zend_object_iterator intern;
61 zval current;
62 void *object;
63 } spl_filesystem_iterator;
64
spl_filesystem_object_to_iterator(spl_filesystem_object * obj)65 static inline spl_filesystem_iterator* spl_filesystem_object_to_iterator(spl_filesystem_object *obj)
66 {
67 spl_filesystem_iterator *it;
68
69 it = ecalloc(1, sizeof(spl_filesystem_iterator));
70 it->object = (void *)obj;
71 zend_iterator_init(&it->intern);
72 return it;
73 }
74
spl_filesystem_iterator_to_object(spl_filesystem_iterator * it)75 static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_filesystem_iterator *it)
76 {
77 return (spl_filesystem_object*)it->object;
78 }
79
80 #define CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(spl_filesystem_object_pointer) \
81 if (!(spl_filesystem_object_pointer)->u.file.stream) { \
82 zend_throw_error(NULL, "Object not initialized"); \
83 RETURN_THROWS(); \
84 }
85
86 #define CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern) \
87 if (!(intern)->u.dir.dirp) { \
88 zend_throw_error(NULL, "Object not initialized"); \
89 RETURN_THROWS(); \
90 }
91
spl_filesystem_file_free_line(spl_filesystem_object * intern)92 static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{ */
93 {
94 if (intern->u.file.current_line) {
95 zend_string_release_ex(intern->u.file.current_line, /* persistent */ false);
96 intern->u.file.current_line = NULL;
97 }
98 if (!Z_ISUNDEF(intern->u.file.current_zval)) {
99 zval_ptr_dtor(&intern->u.file.current_zval);
100 ZVAL_UNDEF(&intern->u.file.current_zval);
101 }
102 } /* }}} */
103
spl_filesystem_object_destroy_object(zend_object * object)104 static void spl_filesystem_object_destroy_object(zend_object *object) /* {{{ */
105 {
106 spl_filesystem_object *intern = spl_filesystem_from_obj(object);
107
108 zend_objects_destroy_object(object);
109
110 switch(intern->type) {
111 case SPL_FS_DIR:
112 if (intern->u.dir.dirp) {
113 php_stream_close(intern->u.dir.dirp);
114 intern->u.dir.dirp = NULL;
115 }
116 break;
117 case SPL_FS_FILE:
118 if (intern->u.file.stream) {
119 /*
120 if (intern->u.file.zcontext) {
121 zend_list_delref(Z_RESVAL_P(intern->zcontext));
122 }
123 */
124 if (!intern->u.file.stream->is_persistent) {
125 php_stream_close(intern->u.file.stream);
126 } else {
127 php_stream_pclose(intern->u.file.stream);
128 }
129 intern->u.file.stream = NULL;
130 ZVAL_UNDEF(&intern->u.file.zresource);
131 }
132 break;
133 default:
134 break;
135 }
136 } /* }}} */
137
spl_filesystem_object_free_storage(zend_object * object)138 static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
139 {
140 spl_filesystem_object *intern = spl_filesystem_from_obj(object);
141
142 if (intern->oth_handler && intern->oth_handler->dtor) {
143 intern->oth_handler->dtor(intern);
144 }
145
146 zend_object_std_dtor(&intern->std);
147
148 if (intern->path) {
149 zend_string_release(intern->path);
150 }
151 if (intern->file_name) {
152 zend_string_release(intern->file_name);
153 }
154 switch(intern->type) {
155 case SPL_FS_INFO:
156 break;
157 case SPL_FS_DIR:
158 if (intern->u.dir.sub_path) {
159 zend_string_release(intern->u.dir.sub_path);
160 }
161 break;
162 case SPL_FS_FILE:
163 if (intern->u.file.open_mode) {
164 zend_string_release(intern->u.file.open_mode);
165 }
166 if (intern->orig_path) {
167 zend_string_release(intern->orig_path);
168 }
169 spl_filesystem_file_free_line(intern);
170 break;
171 }
172 } /* }}} */
173
174 /* {{{ spl_ce_dir_object_new */
175 /* creates the object by
176 - allocating memory
177 - initializing the object members
178 - storing the object
179 - setting it's handlers
180
181 called from
182 - clone
183 - new
184 */
spl_filesystem_object_new(zend_class_entry * class_type)185 static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
186 {
187 spl_filesystem_object *intern;
188
189 intern = ecalloc(1, sizeof(spl_filesystem_object) + zend_object_properties_size(class_type));
190 /* intern->type = SPL_FS_INFO; done by set 0 */
191 intern->file_class = spl_ce_SplFileObject;
192 intern->info_class = spl_ce_SplFileInfo;
193
194 zend_object_std_init(&intern->std, class_type);
195 object_properties_init(&intern->std, class_type);
196
197 return &intern->std;
198 }
199 /* }}} */
200
spl_filesystem_object_get_path(const spl_filesystem_object * intern)201 PHPAPI zend_string *spl_filesystem_object_get_path(const spl_filesystem_object *intern) /* {{{ */
202 {
203 #ifdef HAVE_GLOB
204 if (intern->type == SPL_FS_DIR && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops)) {
205 size_t len = 0;
206 char *tmp = php_glob_stream_get_path(intern->u.dir.dirp, &len);
207 if (len == 0) {
208 return NULL;
209 }
210 return zend_string_init(tmp, len, /* persistent */ false);
211 }
212 #endif
213 if (!intern->path) {
214 return NULL;
215 }
216 return zend_string_copy(intern->path);
217 } /* }}} */
218
spl_filesystem_object_get_file_name(spl_filesystem_object * intern)219 static inline zend_result spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
220 {
221 if (intern->file_name) {
222 /* already known */
223 return SUCCESS;
224 }
225
226 switch (intern->type) {
227 case SPL_FS_INFO:
228 case SPL_FS_FILE:
229 zend_throw_error(NULL, "Object not initialized");
230 return FAILURE;
231 case SPL_FS_DIR: {
232 size_t name_len;
233 zend_string *path;
234 char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
235
236 path = spl_filesystem_object_get_path(intern);
237 /* if there is parent path, amend it, otherwise just use the given path as is */
238 name_len = strlen(intern->u.dir.entry.d_name);
239 if (!path) {
240 intern->file_name = zend_string_init(intern->u.dir.entry.d_name, name_len, 0);
241 return SUCCESS;
242 }
243
244 ZEND_ASSERT(ZSTR_LEN(path) != 0);
245 intern->file_name = zend_string_concat3(
246 ZSTR_VAL(path), ZSTR_LEN(path), &slash, 1, intern->u.dir.entry.d_name, name_len);
247 zend_string_release_ex(path, /* persistent */ false);
248 break;
249 }
250 }
251 return SUCCESS;
252 } /* }}} */
253
254 /* TODO Make void or have callers check return value */
spl_filesystem_dir_read(spl_filesystem_object * intern)255 static bool spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
256 {
257 if (intern->file_name) {
258 /* invalidate */
259 zend_string_release(intern->file_name);
260 intern->file_name = NULL;
261 }
262 if (!intern->u.dir.dirp || !php_stream_readdir(intern->u.dir.dirp, &intern->u.dir.entry)) {
263 intern->u.dir.entry.d_name[0] = '\0';
264 return 0;
265 } else {
266 return 1;
267 }
268 }
269 /* }}} */
270
271 #define IS_SLASH_AT(zs, pos) (IS_SLASH(zs[pos]))
272
spl_filesystem_is_dot(const char * d_name)273 static inline bool spl_filesystem_is_dot(const char * d_name) /* {{{ */
274 {
275 return !strcmp(d_name, ".") || !strcmp(d_name, "..");
276 }
277 /* }}} */
278
279 /* {{{ spl_filesystem_dir_open */
280 /* open a directory resource
281 * Can emit an E_WARNING as it reports errors from php_stream_opendir() */
spl_filesystem_dir_open(spl_filesystem_object * intern,zend_string * path)282 static void spl_filesystem_dir_open(spl_filesystem_object* intern, zend_string *path)
283 {
284 bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
285
286 intern->type = SPL_FS_DIR;
287 intern->u.dir.dirp = php_stream_opendir(ZSTR_VAL(path), REPORT_ERRORS, FG(default_context));
288
289 if (ZSTR_LEN(path) > 1 && IS_SLASH_AT(ZSTR_VAL(path), ZSTR_LEN(path)-1)) {
290 intern->path = zend_string_init(ZSTR_VAL(path), ZSTR_LEN(path)-1, 0);
291 } else {
292 intern->path = zend_string_copy(path);
293 }
294 intern->u.dir.index = 0;
295
296 if (EG(exception) || intern->u.dir.dirp == NULL) {
297 intern->u.dir.entry.d_name[0] = '\0';
298 if (!EG(exception)) {
299 /* open failed w/out notice (turned to exception due to EH_THROW) */
300 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
301 "Failed to open directory \"%s\"", ZSTR_VAL(path));
302 }
303 } else {
304 do {
305 spl_filesystem_dir_read(intern);
306 } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
307 }
308 }
309 /* }}} */
310
311 /* Can generate E_WARNINGS as we report errors from stream initialized via
312 * php_stream_open_wrapper_ex() */
spl_filesystem_file_open(spl_filesystem_object * intern,bool use_include_path)313 static zend_result spl_filesystem_file_open(spl_filesystem_object *intern, bool use_include_path) /* {{{ */
314 {
315 zval tmp;
316
317 intern->type = SPL_FS_FILE;
318 php_stat(intern->file_name, FS_IS_DIR, &tmp);
319 if (Z_TYPE(tmp) == IS_TRUE) {
320 zend_string_release(intern->u.file.open_mode);
321 intern->u.file.open_mode = NULL;
322 intern->file_name = NULL;
323 zend_throw_exception_ex(spl_ce_LogicException, 0, "Cannot use SplFileObject with directories");
324 return FAILURE;
325 }
326
327 intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0);
328 intern->u.file.stream = php_stream_open_wrapper_ex(ZSTR_VAL(intern->file_name), ZSTR_VAL(intern->u.file.open_mode), (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context);
329
330 if (!ZSTR_LEN(intern->file_name) || !intern->u.file.stream) {
331 if (!EG(exception)) {
332 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot open file '%s'", ZSTR_VAL(intern->file_name));
333 }
334 zend_string_release(intern->u.file.open_mode);
335 intern->u.file.open_mode = NULL;
336 intern->file_name = NULL; /* until here it is not a copy */
337 return FAILURE;
338 }
339
340 /* prevent closing the stream outside of SplFileObject */
341 intern->u.file.stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
342
343 /*
344 if (intern->u.file.zcontext) {
345 //zend_list_addref(Z_RES_VAL(intern->u.file.zcontext));
346 Z_ADDREF_P(intern->u.file.zcontext);
347 }
348 */
349
350 if (ZSTR_LEN(intern->file_name) > 1 && IS_SLASH_AT(ZSTR_VAL(intern->file_name), ZSTR_LEN(intern->file_name)-1)) {
351 intern->file_name = zend_string_init(ZSTR_VAL(intern->file_name), ZSTR_LEN(intern->file_name)-1, 0);
352 } else {
353 intern->file_name = zend_string_copy(intern->file_name);
354 }
355
356 intern->orig_path = zend_string_init(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path), 0);
357
358 /* avoid reference counting in debug mode, thus do it manually */
359 ZVAL_RES(&intern->u.file.zresource, intern->u.file.stream->res);
360 /*!!! TODO: maybe bug?
361 Z_SET_REFCOUNT(intern->u.file.zresource, 1);
362 */
363
364 intern->u.file.delimiter = ',';
365 intern->u.file.enclosure = '"';
366 intern->u.file.escape = (unsigned char) '\\';
367
368 intern->u.file.func_getCurr = zend_hash_str_find_ptr(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline") - 1);
369
370 return SUCCESS;
371 } /* }}} */
372
373 /* {{{ spl_filesystem_object_clone */
374 /* Local zend_object creation (on stack)
375 Load the 'other' object
376 Create a new empty object (See spl_filesystem_object_new)
377 Open the directory
378 Clone other members (properties)
379 */
spl_filesystem_object_clone(zend_object * old_object)380 static zend_object *spl_filesystem_object_clone(zend_object *old_object)
381 {
382 zend_object *new_object;
383 spl_filesystem_object *intern;
384 spl_filesystem_object *source;
385
386 source = spl_filesystem_from_obj(old_object);
387 new_object = spl_filesystem_object_new(old_object->ce);
388 intern = spl_filesystem_from_obj(new_object);
389
390 intern->flags = source->flags;
391
392 switch (source->type) {
393 case SPL_FS_INFO:
394 if (source->path != NULL) {
395 intern->path = zend_string_copy(source->path);
396 }
397 if (source->file_name != NULL) {
398 intern->file_name = zend_string_copy(source->file_name);
399 }
400 break;
401 case SPL_FS_DIR: {
402 spl_filesystem_dir_open(intern, source->path);
403 /* read until we hit the position in which we were before */
404 bool skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
405 int index;
406 for (index = 0; index < source->u.dir.index; ++index) {
407 do {
408 spl_filesystem_dir_read(intern);
409 } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
410 }
411 intern->u.dir.index = index;
412 break;
413 }
414 case SPL_FS_FILE:
415 ZEND_UNREACHABLE();
416 }
417
418 intern->file_class = source->file_class;
419 intern->info_class = source->info_class;
420 intern->oth = source->oth;
421 intern->oth_handler = source->oth_handler;
422
423 zend_objects_clone_members(new_object, old_object);
424
425 if (intern->oth_handler && intern->oth_handler->clone) {
426 intern->oth_handler->clone(source, intern);
427 }
428
429 return new_object;
430 }
431 /* }}} */
432
spl_filesystem_info_set_filename(spl_filesystem_object * intern,zend_string * path)433 static void spl_filesystem_info_set_filename(spl_filesystem_object *intern, zend_string *path) /* {{{ */
434 {
435 size_t path_len;
436
437 if (intern->file_name) {
438 zend_string_release(intern->file_name);
439 }
440
441 path_len = ZSTR_LEN(path);
442 if (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len-1)) {
443 do {
444 path_len--;
445 } while (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len - 1));
446 intern->file_name = zend_string_init(ZSTR_VAL(path), path_len, 0);
447 } else {
448 intern->file_name = zend_string_copy(path);
449 }
450 while (path_len > 1 && !IS_SLASH_AT(ZSTR_VAL(path), path_len-1)) {
451 path_len--;
452 }
453 if (path_len) {
454 path_len--;
455 }
456
457 if (intern->path) {
458 zend_string_release(intern->path);
459 }
460 intern->path = zend_string_init(ZSTR_VAL(path), path_len, 0);
461 } /* }}} */
462
463 // TODO Do not pass return_value pointer but actually use value returned by function at call site?
spl_filesystem_object_create_info(zend_string * file_path,zend_class_entry * ce,zval * return_value)464 static spl_filesystem_object *spl_filesystem_object_create_info(zend_string *file_path, zend_class_entry *ce, zval *return_value) /* {{{ */
465 {
466 spl_filesystem_object *intern;
467 zval arg1;
468
469 ZEND_ASSERT(file_path && ZSTR_LEN(file_path) > 0);
470 ZEND_ASSERT(ce != NULL);
471
472 intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
473 RETVAL_OBJ(&intern->std);
474
475 if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
476 ZVAL_STR_COPY(&arg1, file_path);
477 zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
478 zval_ptr_dtor(&arg1);
479 } else {
480 spl_filesystem_info_set_filename(intern, file_path);
481 }
482
483 return intern;
484 } /* }}} */
485
spl_filesystem_object_create_type(int num_args,spl_filesystem_object * source,int type,zend_class_entry * ce,zval * return_value)486 static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, spl_filesystem_object *source, int type, zend_class_entry *ce, zval *return_value) /* {{{ */
487 {
488 spl_filesystem_object *intern;
489 bool use_include_path = 0;
490 zval arg1, arg2;
491 zend_error_handling error_handling;
492
493 switch (source->type) {
494 case SPL_FS_INFO:
495 case SPL_FS_FILE:
496 break;
497 case SPL_FS_DIR:
498 if (!source->u.dir.entry.d_name[0]) {
499 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Could not open file");
500 return NULL;
501 }
502 }
503
504 switch (type) {
505 case SPL_FS_INFO:
506 ce = ce ? ce : source->info_class;
507
508 intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
509 RETVAL_OBJ(&intern->std);
510
511 if (spl_filesystem_object_get_file_name(source) == FAILURE) {
512 return NULL;
513 }
514
515 if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
516 ZVAL_STR_COPY(&arg1, source->file_name);
517 zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
518 zval_ptr_dtor(&arg1);
519 } else {
520 intern->file_name = zend_string_copy(source->file_name);
521 intern->path = spl_filesystem_object_get_path(source);
522 }
523 break;
524 case SPL_FS_FILE:
525 {
526 ce = ce ? ce : source->file_class;
527
528 zend_string *open_mode = ZSTR_CHAR('r');
529 zval *resource = NULL;
530
531 if (zend_parse_parameters(num_args, "|Sbr!",
532 &open_mode, &use_include_path, &resource) == FAILURE
533 ) {
534 return NULL;
535 }
536
537 intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
538 RETVAL_OBJ(&intern->std);
539
540 if (spl_filesystem_object_get_file_name(source) == FAILURE) {
541 return NULL;
542 }
543
544 if (ce->constructor->common.scope != spl_ce_SplFileObject) {
545 ZVAL_STR_COPY(&arg1, source->file_name);
546 ZVAL_STR_COPY(&arg2, open_mode);
547 zend_call_method_with_2_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1, &arg2);
548 zval_ptr_dtor(&arg1);
549 zval_ptr_dtor(&arg2);
550 } else {
551 intern->file_name = source->file_name;
552 intern->path = spl_filesystem_object_get_path(source);
553 intern->u.file.open_mode = zend_string_copy(open_mode);
554 intern->u.file.zcontext = resource;
555
556 /* spl_filesystem_file_open() can generate E_WARNINGs which we want to promote to exceptions */
557 zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
558 if (spl_filesystem_file_open(intern, use_include_path) == FAILURE) {
559 zend_restore_error_handling(&error_handling);
560 zval_ptr_dtor(return_value);
561 ZVAL_NULL(return_value);
562 return NULL;
563 }
564 zend_restore_error_handling(&error_handling);
565 }
566 break;
567 }
568 case SPL_FS_DIR:
569 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Operation not supported");
570 return NULL;
571 }
572 return NULL;
573 } /* }}} */
574
spl_filesystem_is_invalid_or_dot(const char * d_name)575 static bool spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
576 {
577 return d_name[0] == '\0' || spl_filesystem_is_dot(d_name);
578 }
579 /* }}} */
580
spl_filesystem_object_get_pathname(spl_filesystem_object * intern)581 static zend_string *spl_filesystem_object_get_pathname(spl_filesystem_object *intern) { /* {{{ */
582 switch (intern->type) {
583 case SPL_FS_INFO:
584 case SPL_FS_FILE:
585 return intern->file_name;
586 case SPL_FS_DIR:
587 if (intern->u.dir.entry.d_name[0]) {
588 spl_filesystem_object_get_file_name(intern);
589 return intern->file_name;
590 }
591 }
592 return NULL;
593 }
594 /* }}} */
595
spl_filesystem_object_get_debug_info(zend_object * object)596 static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *object) /* {{{ */
597 {
598 spl_filesystem_object *intern = spl_filesystem_from_obj(object);
599 zval tmp;
600 HashTable *debug_info;
601 zend_string *path_name;
602
603 if (!intern->std.properties) {
604 rebuild_object_properties(&intern->std);
605 }
606
607 // TODO Do zend_new_array() + zend_hash_copy() trick?
608 debug_info = zend_array_dup(intern->std.properties);
609
610 path_name = spl_filesystem_object_get_pathname(intern);
611 if (path_name) {
612 ZVAL_STR_COPY(&tmp, path_name);
613 } else {
614 ZVAL_EMPTY_STRING(&tmp);
615 }
616 /* IMPORTANT: Do not free path_name as spl_filesystem_object_get_pathname()
617 * updates/sets the intern->file_name and returns the pointer to
618 * intern->file_name which must remain allocated. */
619 spl_set_private_debug_info_property(spl_ce_SplFileInfo, "pathName", strlen("pathName"), debug_info, &tmp);
620
621 if (intern->file_name) {
622 zend_string *path = spl_filesystem_object_get_path(intern);
623 if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
624 /* +1 to skip the trailing / of the path in the file name */
625 ZVAL_STRINGL(&tmp, ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1, ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1));
626 } else {
627 ZVAL_STR_COPY(&tmp, intern->file_name);
628 }
629 if (path) {
630 zend_string_release_ex(path, /* persistent */ false);
631 }
632
633 spl_set_private_debug_info_property(spl_ce_SplFileInfo, "fileName", strlen("fileName"), debug_info, &tmp);
634 }
635 if (intern->type == SPL_FS_DIR) {
636 #ifdef HAVE_GLOB
637 if (php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops)) {
638 ZVAL_STR_COPY(&tmp, intern->path);
639 } else {
640 ZVAL_FALSE(&tmp);
641 }
642 spl_set_private_debug_info_property(spl_ce_DirectoryIterator, "glob", strlen("glob"), debug_info, &tmp);
643 #endif
644 if (intern->u.dir.sub_path) {
645 ZVAL_STR_COPY(&tmp, intern->u.dir.sub_path);
646 } else {
647 ZVAL_EMPTY_STRING(&tmp);
648 }
649 spl_set_private_debug_info_property(spl_ce_RecursiveDirectoryIterator, "subPathName", strlen("subPathName"), debug_info, &tmp);
650 }
651 if (intern->type == SPL_FS_FILE) {
652 ZVAL_STR_COPY(&tmp, intern->u.file.open_mode);
653 spl_set_private_debug_info_property(spl_ce_SplFileObject, "openMode", strlen("openMode"), debug_info, &tmp);
654
655 ZVAL_STR(&tmp, ZSTR_CHAR((zend_uchar)intern->u.file.delimiter));
656 spl_set_private_debug_info_property(spl_ce_SplFileObject, "delimiter", strlen("delimiter"), debug_info, &tmp);
657
658 ZVAL_STR(&tmp, ZSTR_CHAR((zend_uchar)intern->u.file.enclosure));
659 spl_set_private_debug_info_property(spl_ce_SplFileObject, "enclosure", strlen("enclosure"), debug_info, &tmp);
660 }
661
662 return debug_info;
663 }
664 /* }}} */
665
spl_filesystem_object_get_method_check(zend_object ** object,zend_string * method,const zval * key)666 static zend_function *spl_filesystem_object_get_method_check(zend_object **object, zend_string *method, const zval *key) /* {{{ */
667 {
668 spl_filesystem_object *fsobj = spl_filesystem_from_obj(*object);
669
670 if (fsobj->u.dir.dirp == NULL && fsobj->orig_path == NULL) {
671 zend_throw_error(NULL, "The parent constructor was not called: the object is in an invalid state");
672 return NULL;
673 }
674
675 return zend_std_get_method(object, method, key);
676 }
677 /* }}} */
678
679 #define DIT_CTOR_FLAGS 0x00000001
680 #define DIT_CTOR_GLOB 0x00000002
681
spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS,zend_long ctor_flags)682 static void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long ctor_flags) /* {{{ */
683 {
684 spl_filesystem_object *intern;
685 zend_string *path;
686 zend_result parsed;
687 zend_long flags = (ctor_flags & ~DIT_CTOR_FLAGS);
688 zend_error_handling error_handling;
689
690 if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
691 flags |= SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
692 parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &path, &flags);
693 } else {
694 flags |= SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
695 parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "P", &path);
696 }
697 if (parsed == FAILURE) {
698 RETURN_THROWS();
699 }
700
701 if (ZSTR_LEN(path) == 0) {
702 zend_argument_value_error(1, "cannot be empty");
703 RETURN_THROWS();
704 }
705
706 intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
707 if (intern->path) {
708 /* object is already initialized */
709 zend_throw_error(NULL, "Directory object is already initialized");
710 RETURN_THROWS();
711 }
712 intern->flags = flags;
713
714 /* spl_filesystem_dir_open() may emit an E_WARNING */
715 zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
716 #ifdef HAVE_GLOB
717 if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && !zend_string_starts_with_literal(path, "glob://")) {
718 path = zend_strpprintf(0, "glob://%s", ZSTR_VAL(path));
719 spl_filesystem_dir_open(intern, path);
720 zend_string_release(path);
721 } else
722 #endif
723 {
724 spl_filesystem_dir_open(intern, path);
725
726 }
727 zend_restore_error_handling(&error_handling);
728 }
729 /* }}} */
730
731 /* {{{ Cronstructs a new dir iterator from a path. */
PHP_METHOD(DirectoryIterator,__construct)732 PHP_METHOD(DirectoryIterator, __construct)
733 {
734 spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
735 }
736 /* }}} */
737
738 /* {{{ Rewind dir back to the start */
PHP_METHOD(DirectoryIterator,rewind)739 PHP_METHOD(DirectoryIterator, rewind)
740 {
741 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
742
743 if (zend_parse_parameters_none() == FAILURE) {
744 RETURN_THROWS();
745 }
746
747 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
748 intern->u.dir.index = 0;
749 php_stream_rewinddir(intern->u.dir.dirp);
750 spl_filesystem_dir_read(intern);
751 }
752 /* }}} */
753
754 /* {{{ Return current dir entry */
PHP_METHOD(DirectoryIterator,key)755 PHP_METHOD(DirectoryIterator, key)
756 {
757 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
758
759 if (zend_parse_parameters_none() == FAILURE) {
760 RETURN_THROWS();
761 }
762
763 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
764 RETURN_LONG(intern->u.dir.index);
765 }
766 /* }}} */
767
768 /* {{{ Return this (needed for Iterator interface) */
PHP_METHOD(DirectoryIterator,current)769 PHP_METHOD(DirectoryIterator, current)
770 {
771 if (zend_parse_parameters_none() == FAILURE) {
772 RETURN_THROWS();
773 }
774
775 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS)));
776 RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
777 }
778 /* }}} */
779
780 /* {{{ Move to next entry */
PHP_METHOD(DirectoryIterator,next)781 PHP_METHOD(DirectoryIterator, next)
782 {
783 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
784 bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
785
786 if (zend_parse_parameters_none() == FAILURE) {
787 RETURN_THROWS();
788 }
789
790 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
791 intern->u.dir.index++;
792 do {
793 spl_filesystem_dir_read(intern);
794 } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
795 if (intern->file_name) {
796 zend_string_release(intern->file_name);
797 intern->file_name = NULL;
798 }
799 }
800 /* }}} */
801
802 /* {{{ Seek to the given position */
PHP_METHOD(DirectoryIterator,seek)803 PHP_METHOD(DirectoryIterator, seek)
804 {
805 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
806 zval retval;
807 zend_long pos;
808
809 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &pos) == FAILURE) {
810 RETURN_THROWS();
811 }
812
813 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
814 if (intern->u.dir.index > pos) {
815 /* we first rewind */
816 zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_rewind, "rewind", NULL);
817 }
818
819 while (intern->u.dir.index < pos) {
820 bool valid = false;
821 zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_valid, "valid", &retval);
822 valid = zend_is_true(&retval);
823 zval_ptr_dtor(&retval);
824 if (!valid) {
825 zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", pos);
826 RETURN_THROWS();
827 }
828 zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_next, "next", NULL);
829 }
830 } /* }}} */
831
832 /* {{{ Check whether dir contains more entries */
PHP_METHOD(DirectoryIterator,valid)833 PHP_METHOD(DirectoryIterator, valid)
834 {
835 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
836
837 if (zend_parse_parameters_none() == FAILURE) {
838 RETURN_THROWS();
839 }
840
841 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
842 RETURN_BOOL(intern->u.dir.entry.d_name[0] != '\0');
843 }
844 /* }}} */
845
846 /* {{{ Return the path */
PHP_METHOD(SplFileInfo,getPath)847 PHP_METHOD(SplFileInfo, getPath)
848 {
849 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
850 zend_string *path;
851
852 if (zend_parse_parameters_none() == FAILURE) {
853 RETURN_THROWS();
854 }
855
856 path = spl_filesystem_object_get_path(intern);
857 if (path) {
858 RETURN_STR(path);
859 } else {
860 RETURN_EMPTY_STRING();
861 }
862 }
863 /* }}} */
864
865 /* {{{ Return filename only */
PHP_METHOD(SplFileInfo,getFilename)866 PHP_METHOD(SplFileInfo, getFilename)
867 {
868 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
869 zend_string *path;
870
871 if (zend_parse_parameters_none() == FAILURE) {
872 RETURN_THROWS();
873 }
874
875 if (!intern->file_name) {
876 zend_throw_error(NULL, "Object not initialized");
877 RETURN_THROWS();
878 }
879
880 path = spl_filesystem_object_get_path(intern);
881
882 if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
883 /* +1 to skip the trailing / of the path in the file name */
884 size_t path_len = ZSTR_LEN(path) + 1;
885 RETVAL_STRINGL(ZSTR_VAL(intern->file_name) + path_len, ZSTR_LEN(intern->file_name) - path_len);
886 } else {
887 RETVAL_STR_COPY(intern->file_name);
888 }
889 if (path) {
890 zend_string_release_ex(path, /* persistent */ false);
891 }
892 }
893 /* }}} */
894
895 /* {{{ Return filename of current dir entry */
PHP_METHOD(DirectoryIterator,getFilename)896 PHP_METHOD(DirectoryIterator, getFilename)
897 {
898 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
899
900 if (zend_parse_parameters_none() == FAILURE) {
901 RETURN_THROWS();
902 }
903
904 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
905 RETURN_STRING(intern->u.dir.entry.d_name);
906 }
907 /* }}} */
908
909 /* {{{ Returns file extension component of path */
PHP_METHOD(SplFileInfo,getExtension)910 PHP_METHOD(SplFileInfo, getExtension)
911 {
912 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
913 char *fname = NULL;
914 const char *p;
915 size_t flen;
916 zend_string *path;
917 size_t idx;
918 zend_string *ret;
919
920 if (zend_parse_parameters_none() == FAILURE) {
921 RETURN_THROWS();
922 }
923
924 if (!intern->file_name) {
925 zend_throw_error(NULL, "Object not initialized");
926 RETURN_THROWS();
927 }
928
929 path = spl_filesystem_object_get_path(intern);
930
931 if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
932 fname = ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1;
933 flen = ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1);
934 } else {
935 fname = ZSTR_VAL(intern->file_name);
936 flen = ZSTR_LEN(intern->file_name);
937 }
938 if (path) {
939 zend_string_release_ex(path, /* persistent */ false);
940 }
941
942 ret = php_basename(fname, flen, NULL, 0);
943
944 p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
945 if (p) {
946 idx = p - ZSTR_VAL(ret);
947 RETVAL_STRINGL(ZSTR_VAL(ret) + idx + 1, ZSTR_LEN(ret) - idx - 1);
948 zend_string_release_ex(ret, 0);
949 return;
950 } else {
951 zend_string_release_ex(ret, 0);
952 RETURN_EMPTY_STRING();
953 }
954 }
955 /* }}}*/
956
957 /* {{{ Returns the file extension component of path */
PHP_METHOD(DirectoryIterator,getExtension)958 PHP_METHOD(DirectoryIterator, getExtension)
959 {
960 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
961 const char *p;
962 size_t idx;
963 zend_string *fname;
964
965 if (zend_parse_parameters_none() == FAILURE) {
966 RETURN_THROWS();
967 }
968
969 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
970 fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), NULL, 0);
971
972 p = zend_memrchr(ZSTR_VAL(fname), '.', ZSTR_LEN(fname));
973 if (p) {
974 idx = p - ZSTR_VAL(fname);
975 RETVAL_STRINGL(ZSTR_VAL(fname) + idx + 1, ZSTR_LEN(fname) - idx - 1);
976 zend_string_release_ex(fname, 0);
977 } else {
978 zend_string_release_ex(fname, 0);
979 RETURN_EMPTY_STRING();
980 }
981 }
982 /* }}} */
983
984 /* {{{ Returns filename component of path */
PHP_METHOD(SplFileInfo,getBasename)985 PHP_METHOD(SplFileInfo, getBasename)
986 {
987 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
988 char *fname, *suffix = 0;
989 size_t flen;
990 size_t slen = 0;
991 zend_string *path;
992
993 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
994 RETURN_THROWS();
995 }
996
997 if (!intern->file_name) {
998 zend_throw_error(NULL, "Object not initialized");
999 RETURN_THROWS();
1000 }
1001
1002 path = spl_filesystem_object_get_path(intern);
1003
1004 if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
1005 /* +1 to skip the trailing / of the path in the file name */
1006 fname = ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1;
1007 flen = ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1);
1008 } else {
1009 fname = ZSTR_VAL(intern->file_name);
1010 flen = ZSTR_LEN(intern->file_name);
1011 }
1012 if (path) {
1013 zend_string_release_ex(path, /* persistent */ false);
1014 }
1015
1016 RETURN_STR(php_basename(fname, flen, suffix, slen));
1017 }
1018 /* }}}*/
1019
1020 /* {{{ Returns filename component of current dir entry */
PHP_METHOD(DirectoryIterator,getBasename)1021 PHP_METHOD(DirectoryIterator, getBasename)
1022 {
1023 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1024 char *suffix = 0;
1025 size_t slen = 0;
1026 zend_string *fname;
1027
1028 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
1029 RETURN_THROWS();
1030 }
1031
1032 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
1033 fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), suffix, slen);
1034
1035 RETURN_STR(fname);
1036 }
1037 /* }}} */
1038
1039 /* {{{ Return path and filename */
PHP_METHOD(SplFileInfo,getPathname)1040 PHP_METHOD(SplFileInfo, getPathname)
1041 {
1042 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1043 zend_string *path;
1044
1045 if (zend_parse_parameters_none() == FAILURE) {
1046 RETURN_THROWS();
1047 }
1048 path = spl_filesystem_object_get_pathname(intern);
1049 if (path) {
1050 RETURN_STR_COPY(path);
1051 } else {
1052 RETURN_EMPTY_STRING();
1053 }
1054 }
1055 /* }}} */
1056
1057 /* {{{ Return getPathname() or getFilename() depending on flags */
PHP_METHOD(FilesystemIterator,key)1058 PHP_METHOD(FilesystemIterator, key)
1059 {
1060 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1061
1062 if (zend_parse_parameters_none() == FAILURE) {
1063 RETURN_THROWS();
1064 }
1065
1066 if (SPL_FILE_DIR_KEY(intern, SPL_FILE_DIR_KEY_AS_FILENAME)) {
1067 RETURN_STRING(intern->u.dir.entry.d_name);
1068 } else {
1069 if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1070 RETURN_THROWS();
1071 }
1072 RETURN_STR_COPY(intern->file_name);
1073 }
1074 }
1075 /* }}} */
1076
1077 /* {{{ Return getFilename(), getFileInfo() or $this depending on flags */
PHP_METHOD(FilesystemIterator,current)1078 PHP_METHOD(FilesystemIterator, current)
1079 {
1080 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1081
1082 if (zend_parse_parameters_none() == FAILURE) {
1083 RETURN_THROWS();
1084 }
1085
1086 if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
1087 if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1088 RETURN_THROWS();
1089 }
1090 RETURN_STR_COPY(intern->file_name);
1091 } else if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
1092 if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1093 RETURN_THROWS();
1094 }
1095 spl_filesystem_object_create_type(0, intern, SPL_FS_INFO, NULL, return_value);
1096 } else {
1097 RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
1098 }
1099 }
1100 /* }}} */
1101
1102 /* {{{ Returns true if current entry is '.' or '..' */
PHP_METHOD(DirectoryIterator,isDot)1103 PHP_METHOD(DirectoryIterator, isDot)
1104 {
1105 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1106
1107 if (zend_parse_parameters_none() == FAILURE) {
1108 RETURN_THROWS();
1109 }
1110
1111 CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
1112 RETURN_BOOL(spl_filesystem_is_dot(intern->u.dir.entry.d_name));
1113 }
1114 /* }}} */
1115
1116 /* {{{ Constructs a new SplFileInfo from a path. */
1117 /* When the constructor gets called the object is already created
1118 by the engine, so we must only call 'additional' initializations.
1119 */
PHP_METHOD(SplFileInfo,__construct)1120 PHP_METHOD(SplFileInfo, __construct)
1121 {
1122 spl_filesystem_object *intern;
1123 zend_string *path;
1124
1125 if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &path) == FAILURE) {
1126 RETURN_THROWS();
1127 }
1128
1129 intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1130
1131 spl_filesystem_info_set_filename(intern, path);
1132
1133 /* intern->type = SPL_FS_INFO; already set */
1134 }
1135 /* }}} */
1136
1137 /* {{{ FileInfoFunction */
1138 #define FileInfoFunction(func_name, func_num) \
1139 PHP_METHOD(SplFileInfo, func_name) \
1140 { \
1141 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS)); \
1142 zend_error_handling error_handling; \
1143 if (zend_parse_parameters_none() == FAILURE) { \
1144 RETURN_THROWS(); \
1145 } \
1146 if (spl_filesystem_object_get_file_name(intern) == FAILURE) { \
1147 RETURN_THROWS(); \
1148 } \
1149 zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);\
1150 php_stat(intern->file_name, func_num, return_value); \
1151 zend_restore_error_handling(&error_handling); \
1152 }
1153 /* }}} */
1154
1155 /* {{{ Get file permissions */
FileInfoFunction(getPerms,FS_PERMS)1156 FileInfoFunction(getPerms, FS_PERMS)
1157 /* }}} */
1158
1159 /* {{{ Get file inode */
1160 FileInfoFunction(getInode, FS_INODE)
1161 /* }}} */
1162
1163 /* {{{ Get file size */
1164 FileInfoFunction(getSize, FS_SIZE)
1165 /* }}} */
1166
1167 /* {{{ Get file owner */
1168 FileInfoFunction(getOwner, FS_OWNER)
1169 /* }}} */
1170
1171 /* {{{ Get file group */
1172 FileInfoFunction(getGroup, FS_GROUP)
1173 /* }}} */
1174
1175 /* {{{ Get last access time of file */
1176 FileInfoFunction(getATime, FS_ATIME)
1177 /* }}} */
1178
1179 /* {{{ Get last modification time of file */
1180 FileInfoFunction(getMTime, FS_MTIME)
1181 /* }}} */
1182
1183 /* {{{ Get inode modification time of file */
1184 FileInfoFunction(getCTime, FS_CTIME)
1185 /* }}} */
1186
1187 /* {{{ Get file type */
1188 FileInfoFunction(getType, FS_TYPE)
1189 /* }}} */
1190
1191 /* {{{ Returns true if file can be written */
1192 FileInfoFunction(isWritable, FS_IS_W)
1193 /* }}} */
1194
1195 /* {{{ Returns true if file can be read */
1196 FileInfoFunction(isReadable, FS_IS_R)
1197 /* }}} */
1198
1199 /* {{{ Returns true if file is executable */
1200 FileInfoFunction(isExecutable, FS_IS_X)
1201 /* }}} */
1202
1203 /* {{{ Returns true if file is a regular file */
1204 FileInfoFunction(isFile, FS_IS_FILE)
1205 /* }}} */
1206
1207 /* {{{ Returns true if file is directory */
1208 FileInfoFunction(isDir, FS_IS_DIR)
1209 /* }}} */
1210
1211 /* {{{ Returns true if file is symbolic link */
1212 FileInfoFunction(isLink, FS_IS_LINK)
1213 /* }}} */
1214
1215 /* {{{ Return the target of a symbolic link */
1216 PHP_METHOD(SplFileInfo, getLinkTarget)
1217 {
1218 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1219 ssize_t ret;
1220 char buff[MAXPATHLEN];
1221
1222 if (zend_parse_parameters_none() == FAILURE) {
1223 RETURN_THROWS();
1224 }
1225
1226 if (intern->file_name == NULL) {
1227 if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1228 RETURN_THROWS();
1229 }
1230 }
1231 #if defined(PHP_WIN32) || defined(HAVE_SYMLINK)
1232 if (intern->file_name == NULL) {
1233 zend_value_error("Filename cannot be empty");
1234 RETURN_THROWS();
1235 }
1236 if (!IS_ABSOLUTE_PATH(ZSTR_VAL(intern->file_name), ZSTR_LEN(intern->file_name))) {
1237 char expanded_path[MAXPATHLEN];
1238 if (!expand_filepath_with_mode(ZSTR_VAL(intern->file_name), expanded_path, NULL, 0, CWD_EXPAND )) {
1239 php_error_docref(NULL, E_WARNING, "No such file or directory");
1240 RETURN_FALSE;
1241 }
1242 ret = php_sys_readlink(expanded_path, buff, MAXPATHLEN - 1);
1243 } else {
1244 ret = php_sys_readlink(ZSTR_VAL(intern->file_name), buff, MAXPATHLEN-1);
1245 }
1246 #else
1247 ret = -1; /* always fail if not implemented */
1248 #endif
1249
1250 if (ret == -1) {
1251 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Unable to read link %s, error: %s", ZSTR_VAL(intern->file_name), strerror(errno));
1252 RETVAL_FALSE;
1253 } else {
1254 /* Append NULL to the end of the string */
1255 buff[ret] = '\0';
1256
1257 RETVAL_STRINGL(buff, ret);
1258 }
1259 }
1260 /* }}} */
1261
1262 /* {{{ Return the resolved path */
PHP_METHOD(SplFileInfo,getRealPath)1263 PHP_METHOD(SplFileInfo, getRealPath)
1264 {
1265 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1266 char buff[MAXPATHLEN];
1267 char *filename;
1268
1269 if (zend_parse_parameters_none() == FAILURE) {
1270 RETURN_THROWS();
1271 }
1272
1273 if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
1274 if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1275 RETURN_THROWS();
1276 }
1277 }
1278
1279 if (intern->orig_path) {
1280 filename = ZSTR_VAL(intern->orig_path);
1281 } else {
1282 filename = intern->file_name ? ZSTR_VAL(intern->file_name) : NULL;
1283 }
1284
1285
1286 if (filename && VCWD_REALPATH(filename, buff)) {
1287 #ifdef ZTS
1288 if (VCWD_ACCESS(buff, F_OK)) {
1289 RETURN_FALSE;
1290 } else
1291 #endif
1292 RETURN_STRING(buff);
1293 } else {
1294 RETURN_FALSE;
1295 }
1296 }
1297 /* }}} */
1298
1299 /* {{{ Open the current file */
PHP_METHOD(SplFileInfo,openFile)1300 PHP_METHOD(SplFileInfo, openFile)
1301 {
1302 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1303
1304 spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_FILE, NULL, return_value);
1305 }
1306 /* }}} */
1307
1308 /* {{{ Class to use in openFile() */
PHP_METHOD(SplFileInfo,setFileClass)1309 PHP_METHOD(SplFileInfo, setFileClass)
1310 {
1311 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1312 zend_class_entry *ce = spl_ce_SplFileObject;
1313
1314 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == FAILURE) {
1315 RETURN_THROWS();
1316 }
1317
1318 intern->file_class = ce;
1319 }
1320 /* }}} */
1321
1322 /* {{{ Class to use in getFileInfo(), getPathInfo() */
PHP_METHOD(SplFileInfo,setInfoClass)1323 PHP_METHOD(SplFileInfo, setInfoClass)
1324 {
1325 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1326 zend_class_entry *ce = spl_ce_SplFileInfo;
1327
1328 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == FAILURE) {
1329 RETURN_THROWS();
1330 }
1331
1332 intern->info_class = ce;
1333 }
1334 /* }}} */
1335
1336 /* {{{ Get/copy file info */
PHP_METHOD(SplFileInfo,getFileInfo)1337 PHP_METHOD(SplFileInfo, getFileInfo)
1338 {
1339 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1340 zend_class_entry *ce = intern->info_class;
1341
1342 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C!", &ce) == FAILURE) {
1343 RETURN_THROWS();
1344 }
1345
1346 spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_INFO, ce, return_value);
1347 }
1348 /* }}} */
1349
1350 /* {{{ Get/copy file info */
PHP_METHOD(SplFileInfo,getPathInfo)1351 PHP_METHOD(SplFileInfo, getPathInfo)
1352 {
1353 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1354 zend_class_entry *ce = NULL;
1355 zend_string *path;
1356
1357 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C!", &ce) == FAILURE) {
1358 RETURN_THROWS();
1359 }
1360
1361 if (ce == NULL) {
1362 ce = intern->info_class;
1363 }
1364
1365 path = spl_filesystem_object_get_pathname(intern);
1366 if (path && ZSTR_LEN(path)) {
1367 zend_string *dpath = zend_string_init(ZSTR_VAL(path), ZSTR_LEN(path), 0);
1368 ZSTR_LEN(dpath) = php_dirname(ZSTR_VAL(dpath), ZSTR_LEN(path));
1369 spl_filesystem_object_create_info(dpath, ce, return_value);
1370 zend_string_release(dpath);
1371 }
1372 }
1373 /* }}} */
1374
1375 /* {{{ */
PHP_METHOD(SplFileInfo,__debugInfo)1376 PHP_METHOD(SplFileInfo, __debugInfo)
1377 {
1378 if (zend_parse_parameters_none() == FAILURE) {
1379 RETURN_THROWS();
1380 }
1381
1382 RETURN_ARR(spl_filesystem_object_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1383 } /* }}} */
1384
1385 /* {{{ */
PHP_METHOD(SplFileInfo,_bad_state_ex)1386 PHP_METHOD(SplFileInfo, _bad_state_ex)
1387 {
1388 if (zend_parse_parameters_none() == FAILURE) {
1389 RETURN_THROWS();
1390 }
1391 zend_throw_error(NULL, "The parent constructor was not called: the object is in an invalid state");
1392 RETURN_THROWS();
1393 }
1394 /* }}} */
1395
1396 /* {{{ Constructs a new dir iterator from a path. */
PHP_METHOD(FilesystemIterator,__construct)1397 PHP_METHOD(FilesystemIterator, __construct)
1398 {
1399 spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS | SPL_FILE_DIR_SKIPDOTS);
1400 }
1401 /* }}} */
1402
1403 /* {{{ Rewind dir back to the start */
PHP_METHOD(FilesystemIterator,rewind)1404 PHP_METHOD(FilesystemIterator, rewind)
1405 {
1406 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1407 bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
1408
1409 if (zend_parse_parameters_none() == FAILURE) {
1410 RETURN_THROWS();
1411 }
1412
1413 intern->u.dir.index = 0;
1414 if (intern->u.dir.dirp) {
1415 php_stream_rewinddir(intern->u.dir.dirp);
1416 }
1417 do {
1418 spl_filesystem_dir_read(intern);
1419 } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
1420 }
1421 /* }}} */
1422
1423 /* {{{ Get handling flags */
PHP_METHOD(FilesystemIterator,getFlags)1424 PHP_METHOD(FilesystemIterator, getFlags)
1425 {
1426 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1427
1428 if (zend_parse_parameters_none() == FAILURE) {
1429 RETURN_THROWS();
1430 }
1431
1432 RETURN_LONG(intern->flags & (SPL_FILE_DIR_KEY_MODE_MASK | SPL_FILE_DIR_CURRENT_MODE_MASK | SPL_FILE_DIR_OTHERS_MASK));
1433 } /* }}} */
1434
1435 /* {{{ Set handling flags */
PHP_METHOD(FilesystemIterator,setFlags)1436 PHP_METHOD(FilesystemIterator, setFlags)
1437 {
1438 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1439 zend_long flags;
1440
1441 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &flags) == FAILURE) {
1442 RETURN_THROWS();
1443 }
1444
1445 intern->flags &= ~(SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK);
1446 intern->flags |= ((SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK) & flags);
1447 } /* }}} */
1448
1449 /* {{{ Returns whether current entry is a directory and not '.' or '..' */
PHP_METHOD(RecursiveDirectoryIterator,hasChildren)1450 PHP_METHOD(RecursiveDirectoryIterator, hasChildren)
1451 {
1452 bool allow_links = 0;
1453 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1454
1455 ZEND_PARSE_PARAMETERS_START(0, 1)
1456 Z_PARAM_OPTIONAL
1457 Z_PARAM_BOOL(allow_links)
1458 ZEND_PARSE_PARAMETERS_END();
1459
1460 if (spl_filesystem_is_invalid_or_dot(intern->u.dir.entry.d_name)) {
1461 RETURN_FALSE;
1462 } else {
1463 if (intern->u.dir.entry.d_type == DT_DIR) {
1464 RETURN_TRUE;
1465 } else if (intern->u.dir.entry.d_type == DT_REG) {
1466 RETURN_FALSE;
1467 }
1468 if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1469 RETURN_THROWS();
1470 }
1471 php_stat(intern->file_name, FS_LPERMS, return_value);
1472 if (Z_TYPE_P(return_value) == IS_FALSE) {
1473 return;
1474 } else if (!S_ISLNK(Z_LVAL_P(return_value))) {
1475 RETURN_BOOL(S_ISDIR(Z_LVAL_P(return_value)));
1476 } else {
1477 if (!allow_links
1478 && !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
1479 RETURN_FALSE;
1480 }
1481 php_stat(intern->file_name, FS_IS_DIR, return_value);
1482 }
1483 }
1484 }
1485 /* }}} */
1486
1487 /* {{{ Returns an iterator for the current entry if it is a directory */
PHP_METHOD(RecursiveDirectoryIterator,getChildren)1488 PHP_METHOD(RecursiveDirectoryIterator, getChildren)
1489 {
1490 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1491 spl_filesystem_object *subdir;
1492 char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
1493
1494 if (zend_parse_parameters_none() == FAILURE) {
1495 RETURN_THROWS();
1496 }
1497
1498 if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1499 RETURN_THROWS();
1500 }
1501
1502 zval params[2];
1503 ZVAL_STR_COPY(¶ms[0], intern->file_name);
1504 ZVAL_LONG(¶ms[1], intern->flags);
1505
1506 zend_result is_initialized = object_init_with_constructor(return_value, Z_OBJCE_P(ZEND_THIS), 2, params, NULL);
1507 zval_ptr_dtor_str(¶ms[0]);
1508 if (is_initialized == FAILURE) {
1509 RETURN_THROWS();
1510 }
1511
1512 subdir = spl_filesystem_from_obj(Z_OBJ_P(return_value));
1513 if (subdir) {
1514 size_t name_len = strlen(intern->u.dir.entry.d_name);
1515 if (intern->u.dir.sub_path && ZSTR_LEN(intern->u.dir.sub_path)) {
1516 zend_string *sub_path = zend_string_alloc(ZSTR_LEN(intern->u.dir.sub_path) + 1 + name_len, 0);
1517 memcpy(ZSTR_VAL(sub_path), ZSTR_VAL(intern->u.dir.sub_path), ZSTR_LEN(intern->u.dir.sub_path));
1518 ZSTR_VAL(sub_path)[ZSTR_LEN(intern->u.dir.sub_path)] = slash;
1519 memcpy(ZSTR_VAL(sub_path) + ZSTR_LEN(intern->u.dir.sub_path) + 1, intern->u.dir.entry.d_name, name_len);
1520 ZSTR_VAL(sub_path)[ZSTR_LEN(intern->u.dir.sub_path) + 1 + name_len] = 0;
1521 subdir->u.dir.sub_path = sub_path;
1522 } else {
1523 subdir->u.dir.sub_path = zend_string_init(intern->u.dir.entry.d_name, name_len, 0);
1524 }
1525 subdir->info_class = intern->info_class;
1526 subdir->file_class = intern->file_class;
1527 subdir->oth = intern->oth;
1528 }
1529 }
1530 /* }}} */
1531
1532 /* {{{ Get sub path */
PHP_METHOD(RecursiveDirectoryIterator,getSubPath)1533 PHP_METHOD(RecursiveDirectoryIterator, getSubPath)
1534 {
1535 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1536
1537 if (zend_parse_parameters_none() == FAILURE) {
1538 RETURN_THROWS();
1539 }
1540
1541 if (intern->u.dir.sub_path) {
1542 RETURN_STR_COPY(intern->u.dir.sub_path);
1543 } else {
1544 RETURN_EMPTY_STRING();
1545 }
1546 }
1547 /* }}} */
1548
1549 /* {{{ Get sub path and file name */
PHP_METHOD(RecursiveDirectoryIterator,getSubPathname)1550 PHP_METHOD(RecursiveDirectoryIterator, getSubPathname)
1551 {
1552 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1553 char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
1554
1555 if (zend_parse_parameters_none() == FAILURE) {
1556 RETURN_THROWS();
1557 }
1558
1559 if (intern->u.dir.sub_path) {
1560 RETURN_NEW_STR(strpprintf(0, "%s%c%s", ZSTR_VAL(intern->u.dir.sub_path), slash, intern->u.dir.entry.d_name));
1561 } else {
1562 RETURN_STRING(intern->u.dir.entry.d_name);
1563 }
1564 }
1565 /* }}} */
1566
1567 /* {{{ Cronstructs a new dir iterator from a path. */
PHP_METHOD(RecursiveDirectoryIterator,__construct)1568 PHP_METHOD(RecursiveDirectoryIterator, __construct)
1569 {
1570 spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS);
1571 }
1572 /* }}} */
1573
1574 #ifdef HAVE_GLOB
1575 /* {{{ Cronstructs a new dir iterator from a glob expression (no glob:// needed). */
PHP_METHOD(GlobIterator,__construct)1576 PHP_METHOD(GlobIterator, __construct)
1577 {
1578 spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS|DIT_CTOR_GLOB);
1579 }
1580 /* }}} */
1581
1582 /* {{{ Return the number of directories and files found by globbing */
PHP_METHOD(GlobIterator,count)1583 PHP_METHOD(GlobIterator, count)
1584 {
1585 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1586
1587 if (zend_parse_parameters_none() == FAILURE) {
1588 RETURN_THROWS();
1589 }
1590
1591 if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
1592 RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
1593 } else {
1594 /* should not happen */
1595 // TODO ZEND_ASSERT ?
1596 php_error_docref(NULL, E_ERROR, "GlobIterator lost glob state");
1597 }
1598 }
1599 /* }}} */
1600 #endif /* HAVE_GLOB */
1601
1602 /* {{{ forward declarations to the iterator handlers */
1603 static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter);
1604 static zend_result spl_filesystem_dir_it_valid(zend_object_iterator *iter);
1605 static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter);
1606 static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key);
1607 static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter);
1608 static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter);
1609
1610 /* iterator handler table */
1611 static const zend_object_iterator_funcs spl_filesystem_dir_it_funcs = {
1612 spl_filesystem_dir_it_dtor,
1613 spl_filesystem_dir_it_valid,
1614 spl_filesystem_dir_it_current_data,
1615 spl_filesystem_dir_it_current_key,
1616 spl_filesystem_dir_it_move_forward,
1617 spl_filesystem_dir_it_rewind,
1618 NULL,
1619 NULL, /* get_gc */
1620 };
1621 /* }}} */
1622
1623 /* {{{ spl_ce_dir_get_iterator */
spl_filesystem_dir_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1624 static zend_object_iterator *spl_filesystem_dir_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
1625 {
1626 spl_filesystem_iterator *iterator;
1627 spl_filesystem_object *dir_object;
1628
1629 if (by_ref) {
1630 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1631 return NULL;
1632 }
1633 dir_object = spl_filesystem_from_obj(Z_OBJ_P(object));
1634 iterator = spl_filesystem_object_to_iterator(dir_object);
1635 ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
1636 iterator->intern.funcs = &spl_filesystem_dir_it_funcs;
1637 /* ->current must be initialized; rewind doesn't set it and valid
1638 * doesn't check whether it's set */
1639 iterator->current = *object;
1640
1641 return &iterator->intern;
1642 }
1643 /* }}} */
1644
1645 /* {{{ spl_filesystem_dir_it_dtor */
spl_filesystem_dir_it_dtor(zend_object_iterator * iter)1646 static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter)
1647 {
1648 spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1649 zval_ptr_dtor(&iterator->intern.data);
1650 }
1651 /* }}} */
1652
1653 /* {{{ spl_filesystem_dir_it_valid */
spl_filesystem_dir_it_valid(zend_object_iterator * iter)1654 static zend_result spl_filesystem_dir_it_valid(zend_object_iterator *iter)
1655 {
1656 spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1657
1658 return object->u.dir.entry.d_name[0] != '\0' ? SUCCESS : FAILURE;
1659 }
1660 /* }}} */
1661
1662 /* {{{ spl_filesystem_dir_it_current_data */
spl_filesystem_dir_it_current_data(zend_object_iterator * iter)1663 static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter)
1664 {
1665 spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1666
1667 return &iterator->current;
1668 }
1669 /* }}} */
1670
1671 /* {{{ spl_filesystem_dir_it_current_key */
spl_filesystem_dir_it_current_key(zend_object_iterator * iter,zval * key)1672 static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key)
1673 {
1674 spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1675
1676 ZVAL_LONG(key, object->u.dir.index);
1677 }
1678 /* }}} */
1679
1680 /* {{{ spl_filesystem_dir_it_move_forward */
spl_filesystem_dir_it_move_forward(zend_object_iterator * iter)1681 static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter)
1682 {
1683 spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1684
1685 object->u.dir.index++;
1686 spl_filesystem_dir_read(object);
1687 if (object->file_name) {
1688 zend_string_release(object->file_name);
1689 object->file_name = NULL;
1690 }
1691 }
1692 /* }}} */
1693
1694 /* {{{ spl_filesystem_dir_it_rewind */
spl_filesystem_dir_it_rewind(zend_object_iterator * iter)1695 static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter)
1696 {
1697 spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1698
1699 object->u.dir.index = 0;
1700 if (object->u.dir.dirp) {
1701 php_stream_rewinddir(object->u.dir.dirp);
1702 }
1703 spl_filesystem_dir_read(object);
1704 }
1705 /* }}} */
1706
1707 /* {{{ spl_filesystem_tree_it_dtor */
spl_filesystem_tree_it_dtor(zend_object_iterator * iter)1708 static void spl_filesystem_tree_it_dtor(zend_object_iterator *iter)
1709 {
1710 spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1711 zval_ptr_dtor(&iterator->intern.data);
1712 zval_ptr_dtor(&iterator->current);
1713 }
1714 /* }}} */
1715
1716 /* {{{ spl_filesystem_tree_it_current_data */
spl_filesystem_tree_it_current_data(zend_object_iterator * iter)1717 static zval *spl_filesystem_tree_it_current_data(zend_object_iterator *iter)
1718 {
1719 spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1720 spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
1721
1722 if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
1723 if (Z_ISUNDEF(iterator->current)) {
1724 if (spl_filesystem_object_get_file_name(object) == FAILURE) {
1725 return NULL;
1726 }
1727 ZVAL_STR_COPY(&iterator->current, object->file_name);
1728 }
1729 return &iterator->current;
1730 } else if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
1731 if (Z_ISUNDEF(iterator->current)) {
1732 if (spl_filesystem_object_get_file_name(object) == FAILURE) {
1733 return NULL;
1734 }
1735 spl_filesystem_object_create_type(0, object, SPL_FS_INFO, NULL, &iterator->current);
1736 }
1737 return &iterator->current;
1738 } else {
1739 return &iterator->intern.data;
1740 }
1741 }
1742 /* }}} */
1743
1744 /* {{{ spl_filesystem_tree_it_current_key */
spl_filesystem_tree_it_current_key(zend_object_iterator * iter,zval * key)1745 static void spl_filesystem_tree_it_current_key(zend_object_iterator *iter, zval *key)
1746 {
1747 spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1748
1749 if (SPL_FILE_DIR_KEY(object, SPL_FILE_DIR_KEY_AS_FILENAME)) {
1750 ZVAL_STRING(key, object->u.dir.entry.d_name);
1751 } else {
1752 if (spl_filesystem_object_get_file_name(object) == FAILURE) {
1753 return;
1754 }
1755 ZVAL_STR_COPY(key, object->file_name);
1756 }
1757 }
1758 /* }}} */
1759
1760 /* {{{ spl_filesystem_tree_it_move_forward */
spl_filesystem_tree_it_move_forward(zend_object_iterator * iter)1761 static void spl_filesystem_tree_it_move_forward(zend_object_iterator *iter)
1762 {
1763 spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1764 spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
1765 bool skip_dots = SPL_HAS_FLAG(object->flags, SPL_FILE_DIR_SKIPDOTS);
1766
1767 object->u.dir.index++;
1768 do {
1769 spl_filesystem_dir_read(object);
1770 } while (skip_dots && spl_filesystem_is_dot(object->u.dir.entry.d_name));
1771 if (object->file_name) {
1772 zend_string_release(object->file_name);
1773 object->file_name = NULL;
1774 }
1775 if (!Z_ISUNDEF(iterator->current)) {
1776 zval_ptr_dtor(&iterator->current);
1777 ZVAL_UNDEF(&iterator->current);
1778 }
1779 }
1780 /* }}} */
1781
1782 /* {{{ spl_filesystem_tree_it_rewind */
spl_filesystem_tree_it_rewind(zend_object_iterator * iter)1783 static void spl_filesystem_tree_it_rewind(zend_object_iterator *iter)
1784 {
1785 spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1786 spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
1787 bool skip_dots = SPL_HAS_FLAG(object->flags, SPL_FILE_DIR_SKIPDOTS);
1788
1789 object->u.dir.index = 0;
1790 if (object->u.dir.dirp) {
1791 php_stream_rewinddir(object->u.dir.dirp);
1792 }
1793 do {
1794 spl_filesystem_dir_read(object);
1795 } while (skip_dots && spl_filesystem_is_dot(object->u.dir.entry.d_name));
1796 if (!Z_ISUNDEF(iterator->current)) {
1797 zval_ptr_dtor(&iterator->current);
1798 ZVAL_UNDEF(&iterator->current);
1799 }
1800 }
1801 /* }}} */
1802
1803 /* {{{ iterator handler table */
1804 static const zend_object_iterator_funcs spl_filesystem_tree_it_funcs = {
1805 spl_filesystem_tree_it_dtor,
1806 spl_filesystem_dir_it_valid,
1807 spl_filesystem_tree_it_current_data,
1808 spl_filesystem_tree_it_current_key,
1809 spl_filesystem_tree_it_move_forward,
1810 spl_filesystem_tree_it_rewind,
1811 NULL,
1812 NULL, /* get_gc */
1813 };
1814 /* }}} */
1815
1816 /* {{{ spl_ce_dir_get_iterator */
spl_filesystem_tree_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1817 static zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
1818 {
1819 spl_filesystem_iterator *iterator;
1820 spl_filesystem_object *dir_object;
1821
1822 if (by_ref) {
1823 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1824 return NULL;
1825 }
1826 dir_object = spl_filesystem_from_obj(Z_OBJ_P(object));
1827 iterator = spl_filesystem_object_to_iterator(dir_object);
1828
1829 ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
1830 iterator->intern.funcs = &spl_filesystem_tree_it_funcs;
1831
1832 return &iterator->intern;
1833 }
1834 /* }}} */
1835
spl_filesystem_file_cannot_read(spl_filesystem_object * intern)1836 static ZEND_COLD void spl_filesystem_file_cannot_read(spl_filesystem_object *intern)
1837 {
1838 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", ZSTR_VAL(intern->file_name));
1839 }
1840
spl_filesystem_file_read_ex(spl_filesystem_object * intern,bool silent,zend_long line_add,bool csv)1841 static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bool silent, zend_long line_add, bool csv)
1842 {
1843 char *buf;
1844 size_t line_len = 0;
1845
1846 spl_filesystem_file_free_line(intern);
1847
1848 if (php_stream_eof(intern->u.file.stream)) {
1849 if (!silent) {
1850 spl_filesystem_file_cannot_read(intern);
1851 }
1852 return FAILURE;
1853 }
1854
1855 if (intern->u.file.max_line_len > 0) {
1856 buf = safe_emalloc((intern->u.file.max_line_len + 1), sizeof(char), 0);
1857 if (php_stream_get_line(intern->u.file.stream, buf, intern->u.file.max_line_len + 1, &line_len) == NULL) {
1858 efree(buf);
1859 buf = NULL;
1860 } else {
1861 buf[line_len] = '\0';
1862 }
1863 } else {
1864 buf = php_stream_get_line(intern->u.file.stream, NULL, 0, &line_len);
1865 }
1866
1867 if (!buf) {
1868 intern->u.file.current_line = ZSTR_EMPTY_ALLOC();
1869 } else {
1870 if (!csv && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)) {
1871 if (line_len > 0 && buf[line_len - 1] == '\n') {
1872 line_len--;
1873 if (line_len > 0 && buf[line_len - 1] == '\r') {
1874 line_len--;
1875 }
1876 buf[line_len] = '\0';
1877 }
1878 }
1879
1880 intern->u.file.current_line = zend_string_init(buf, line_len, /* persistent */ false);
1881 efree(buf);
1882 }
1883 intern->u.file.current_line_num += line_add;
1884
1885 return SUCCESS;
1886 } /* }}} */
1887
spl_filesystem_file_read(spl_filesystem_object * intern,bool silent,bool csv)1888 static inline zend_result spl_filesystem_file_read(spl_filesystem_object *intern, bool silent, bool csv)
1889 {
1890 zend_long line_add = (intern->u.file.current_line) ? 1 : 0;
1891 return spl_filesystem_file_read_ex(intern, silent, line_add, csv);
1892 }
1893
is_line_empty(const spl_filesystem_object * intern)1894 static bool is_line_empty(const spl_filesystem_object *intern)
1895 {
1896 const char *current_line = ZSTR_VAL(intern->u.file.current_line);
1897 size_t current_line_len = ZSTR_LEN(intern->u.file.current_line);
1898 return current_line_len == 0 || (
1899 SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
1900 && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)
1901 && (
1902 (current_line_len == 1 && current_line[0] == '\n')
1903 || (current_line_len == 2 && current_line[0] == '\r' && current_line[1] == '\n')
1904 )
1905 );
1906 }
1907
spl_filesystem_file_read_csv(spl_filesystem_object * intern,char delimiter,char enclosure,int escape,zval * return_value,bool silent)1908 static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value, bool silent) /* {{{ */
1909 {
1910 do {
1911 zend_result ret = spl_filesystem_file_read(intern, silent, /* csv */ true);
1912 if (ret != SUCCESS) {
1913 return ret;
1914 }
1915 } while (is_line_empty(intern) && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY));
1916
1917 /* We need to duplicate the current line content as php_fgetcsv() will free it.
1918 * This is because it might reach the end of the line when it's in an enclosure and
1919 * thus must fetch the next line from the stream */
1920 size_t buf_len = ZSTR_LEN(intern->u.file.current_line);
1921 char *buf = estrndup(ZSTR_VAL(intern->u.file.current_line), buf_len);
1922
1923 if (!Z_ISUNDEF(intern->u.file.current_zval)) {
1924 zval_ptr_dtor(&intern->u.file.current_zval);
1925 ZVAL_UNDEF(&intern->u.file.current_zval);
1926 }
1927
1928 HashTable *values = php_fgetcsv(intern->u.file.stream, delimiter, enclosure, escape, buf_len, buf);
1929 if (values == NULL) {
1930 values = php_bc_fgetcsv_empty_line();
1931 }
1932 ZVAL_ARR(&intern->u.file.current_zval, values);
1933 if (return_value) {
1934 ZVAL_COPY(return_value, &intern->u.file.current_zval);
1935 }
1936 return SUCCESS;
1937 }
1938 /* }}} */
1939
spl_filesystem_file_read_line_ex(zval * this_ptr,spl_filesystem_object * intern,bool silent)1940 static zend_result spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern, bool silent) /* {{{ */
1941 {
1942 zval retval;
1943
1944 /* 1) use fgetcsv? 2) overloaded call the function, 3) do it directly */
1945 if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) {
1946 return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL, silent);
1947 }
1948 if (intern->u.file.func_getCurr->common.scope != spl_ce_SplFileObject) {
1949 spl_filesystem_file_free_line(intern);
1950
1951 if (php_stream_eof(intern->u.file.stream)) {
1952 if (!silent) {
1953 spl_filesystem_file_cannot_read(intern);
1954 }
1955 return FAILURE;
1956 }
1957 zend_call_method_with_0_params(Z_OBJ_P(this_ptr), Z_OBJCE_P(this_ptr), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
1958 if (Z_ISUNDEF(retval)) {
1959 return FAILURE;
1960 }
1961
1962 if (Z_TYPE(retval) != IS_STRING) {
1963 zend_type_error("%s::getCurrentLine(): Return value must be of type string, %s returned",
1964 ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), zend_zval_value_name(&retval));
1965 zval_ptr_dtor(&retval);
1966 return FAILURE;
1967 }
1968
1969 if (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) {
1970 intern->u.file.current_line_num++;
1971 }
1972 spl_filesystem_file_free_line(intern);
1973 intern->u.file.current_line = zend_string_copy(Z_STR(retval));
1974 zval_ptr_dtor(&retval);
1975 return SUCCESS;
1976 } else {
1977 return spl_filesystem_file_read(intern, silent, /* csv */ false);
1978 }
1979 } /* }}} */
1980
spl_filesystem_file_read_line(zval * this_ptr,spl_filesystem_object * intern,bool silent)1981 static zend_result spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern, bool silent) /* {{{ */
1982 {
1983 zend_result ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
1984
1985 while (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY) && ret == SUCCESS && is_line_empty(intern)) {
1986 spl_filesystem_file_free_line(intern);
1987 ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
1988 }
1989
1990 return ret;
1991 }
1992 /* }}} */
1993
spl_filesystem_file_rewind(zval * this_ptr,spl_filesystem_object * intern)1994 static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */
1995 {
1996 if (!intern->u.file.stream) {
1997 zend_throw_error(NULL, "Object not initialized");
1998 return;
1999 }
2000 if (-1 == php_stream_rewind(intern->u.file.stream)) {
2001 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot rewind file %s", ZSTR_VAL(intern->file_name));
2002 return;
2003 }
2004
2005 spl_filesystem_file_free_line(intern);
2006 intern->u.file.current_line_num = 0;
2007
2008 if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2009 spl_filesystem_file_read_line(this_ptr, intern, true);
2010 }
2011 } /* }}} */
2012
2013 /* {{{ Construct a new file object */
PHP_METHOD(SplFileObject,__construct)2014 PHP_METHOD(SplFileObject, __construct)
2015 {
2016 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2017 zend_string *open_mode = ZSTR_CHAR('r');
2018 bool use_include_path = 0;
2019 size_t path_len;
2020 zend_error_handling error_handling;
2021
2022 intern->u.file.open_mode = ZSTR_CHAR('r');
2023
2024 if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|Sbr!",
2025 &intern->file_name, &open_mode,
2026 &use_include_path, &intern->u.file.zcontext) == FAILURE) {
2027 intern->u.file.open_mode = NULL;
2028 intern->file_name = NULL;
2029 RETURN_THROWS();
2030 }
2031
2032 intern->u.file.open_mode = zend_string_copy(open_mode);
2033
2034 /* spl_filesystem_file_open() can generate E_WARNINGs which we want to promote to exceptions */
2035 zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
2036 zend_result retval = spl_filesystem_file_open(intern, use_include_path);
2037 zend_restore_error_handling(&error_handling);
2038 if (retval == FAILURE) {
2039 RETURN_THROWS();
2040 }
2041
2042 path_len = strlen(intern->u.file.stream->orig_path);
2043
2044 if (path_len > 1 && IS_SLASH_AT(intern->u.file.stream->orig_path, path_len-1)) {
2045 path_len--;
2046 }
2047
2048 while (path_len > 1 && !IS_SLASH_AT(intern->u.file.stream->orig_path, path_len-1)) {
2049 path_len--;
2050 }
2051
2052 if (path_len) {
2053 path_len--;
2054 }
2055
2056 intern->path = zend_string_init(intern->u.file.stream->orig_path, path_len, 0);
2057 } /* }}} */
2058
2059 /* {{{ Construct a new temp file object */
PHP_METHOD(SplTempFileObject,__construct)2060 PHP_METHOD(SplTempFileObject, __construct)
2061 {
2062 zend_string *file_name;
2063 zend_long max_memory = PHP_STREAM_MAX_MEM;
2064 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2065 zend_error_handling error_handling;
2066
2067 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &max_memory) == FAILURE) {
2068 RETURN_THROWS();
2069 }
2070
2071 if (max_memory < 0) {
2072 file_name = ZSTR_INIT_LITERAL("php://memory", 0);
2073 } else if (ZEND_NUM_ARGS()) {
2074 file_name = zend_strpprintf(0, "php://temp/maxmemory:" ZEND_LONG_FMT, max_memory);
2075 } else {
2076 file_name = ZSTR_INIT_LITERAL("php://temp", 0);
2077 }
2078 intern->file_name = file_name;
2079 intern->u.file.open_mode = ZSTR_INIT_LITERAL("wb", 0);
2080
2081 /* spl_filesystem_file_open() can generate E_WARNINGs which we want to promote to exceptions */
2082 zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
2083 if (spl_filesystem_file_open(intern, /* use_include_path */ false) == SUCCESS) {
2084 intern->path = ZSTR_EMPTY_ALLOC();
2085 }
2086 zend_string_release(file_name);
2087 zend_restore_error_handling(&error_handling);
2088 } /* }}} */
2089
2090 /* {{{ Rewind the file and read the first line */
PHP_METHOD(SplFileObject,rewind)2091 PHP_METHOD(SplFileObject, rewind)
2092 {
2093 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2094
2095 if (zend_parse_parameters_none() == FAILURE) {
2096 RETURN_THROWS();
2097 }
2098
2099 spl_filesystem_file_rewind(ZEND_THIS, intern);
2100 } /* }}} */
2101
2102 /* {{{ Return whether end of file is reached */
PHP_METHOD(SplFileObject,eof)2103 PHP_METHOD(SplFileObject, eof)
2104 {
2105 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2106
2107 if (zend_parse_parameters_none() == FAILURE) {
2108 RETURN_THROWS();
2109 }
2110
2111 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2112
2113 RETURN_BOOL(php_stream_eof(intern->u.file.stream));
2114 } /* }}} */
2115
2116 /* {{{ Return !eof() */
PHP_METHOD(SplFileObject,valid)2117 PHP_METHOD(SplFileObject, valid)
2118 {
2119 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2120
2121 if (zend_parse_parameters_none() == FAILURE) {
2122 RETURN_THROWS();
2123 }
2124
2125 if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2126 RETURN_BOOL(intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval));
2127 }
2128 if (!intern->u.file.stream) {
2129 RETURN_FALSE;
2130 }
2131 RETURN_BOOL(!php_stream_eof(intern->u.file.stream));
2132 } /* }}} */
2133
2134 /* {{{ Return next line from file */
PHP_METHOD(SplFileObject,fgets)2135 PHP_METHOD(SplFileObject, fgets)
2136 {
2137 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2138
2139 if (zend_parse_parameters_none() == FAILURE) {
2140 RETURN_THROWS();
2141 }
2142
2143 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2144
2145 if (spl_filesystem_file_read_ex(intern, /* silent */ false, /* line_add */ 1, /* csv */ false) == FAILURE) {
2146 RETURN_THROWS();
2147 }
2148 RETURN_STR_COPY(intern->u.file.current_line);
2149 } /* }}} */
2150
2151 /* {{{ Return current line from file */
PHP_METHOD(SplFileObject,current)2152 PHP_METHOD(SplFileObject, current)
2153 {
2154 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2155
2156 if (zend_parse_parameters_none() == FAILURE) {
2157 RETURN_THROWS();
2158 }
2159
2160 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2161
2162 if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
2163 spl_filesystem_file_read_line(ZEND_THIS, intern, true);
2164 }
2165 if (intern->u.file.current_line && (!SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || Z_ISUNDEF(intern->u.file.current_zval))) {
2166 RETURN_STR_COPY(intern->u.file.current_line);
2167 } else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
2168 ZEND_ASSERT(!Z_ISREF(intern->u.file.current_zval));
2169 ZEND_ASSERT(Z_TYPE(intern->u.file.current_zval) == IS_ARRAY);
2170 RETURN_COPY(&intern->u.file.current_zval);
2171 }
2172 RETURN_FALSE;
2173 } /* }}} */
2174
2175 /* {{{ Return line number */
PHP_METHOD(SplFileObject,key)2176 PHP_METHOD(SplFileObject, key)
2177 {
2178 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2179
2180 if (zend_parse_parameters_none() == FAILURE) {
2181 RETURN_THROWS();
2182 }
2183
2184 /* Do not read the next line to support correct counting with fgetc()
2185 if (!intern->u.file.current_line) {
2186 spl_filesystem_file_read_line(ZEND_THIS, intern);
2187 } */
2188 RETURN_LONG(intern->u.file.current_line_num);
2189 } /* }}} */
2190
2191 /* {{{ Read next line */
PHP_METHOD(SplFileObject,next)2192 PHP_METHOD(SplFileObject, next)
2193 {
2194 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2195
2196 if (zend_parse_parameters_none() == FAILURE) {
2197 RETURN_THROWS();
2198 }
2199
2200 spl_filesystem_file_free_line(intern);
2201 if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2202 spl_filesystem_file_read_line(ZEND_THIS, intern, true);
2203 }
2204 intern->u.file.current_line_num++;
2205 } /* }}} */
2206
2207 /* {{{ Set file handling flags */
PHP_METHOD(SplFileObject,setFlags)2208 PHP_METHOD(SplFileObject, setFlags)
2209 {
2210 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2211
2212 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) {
2213 RETURN_THROWS();
2214 }
2215 } /* }}} */
2216
2217 /* {{{ Get file handling flags */
PHP_METHOD(SplFileObject,getFlags)2218 PHP_METHOD(SplFileObject, getFlags)
2219 {
2220 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2221
2222 if (zend_parse_parameters_none() == FAILURE) {
2223 RETURN_THROWS();
2224 }
2225
2226 RETURN_LONG(intern->flags & SPL_FILE_OBJECT_MASK);
2227 } /* }}} */
2228
2229 /* {{{ Set maximum line length */
PHP_METHOD(SplFileObject,setMaxLineLen)2230 PHP_METHOD(SplFileObject, setMaxLineLen)
2231 {
2232 zend_long max_len;
2233
2234 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2235
2236 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &max_len) == FAILURE) {
2237 RETURN_THROWS();
2238 }
2239
2240 if (max_len < 0) {
2241 zend_argument_value_error(1, "must be greater than or equal to 0");
2242 RETURN_THROWS();
2243 }
2244
2245 intern->u.file.max_line_len = max_len;
2246 } /* }}} */
2247
2248 /* {{{ Get maximum line length */
PHP_METHOD(SplFileObject,getMaxLineLen)2249 PHP_METHOD(SplFileObject, getMaxLineLen)
2250 {
2251 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2252
2253 if (zend_parse_parameters_none() == FAILURE) {
2254 RETURN_THROWS();
2255 }
2256
2257 RETURN_LONG((zend_long)intern->u.file.max_line_len);
2258 } /* }}} */
2259
2260 /* {{{ Return false */
PHP_METHOD(SplFileObject,hasChildren)2261 PHP_METHOD(SplFileObject, hasChildren)
2262 {
2263 if (zend_parse_parameters_none() == FAILURE) {
2264 RETURN_THROWS();
2265 }
2266
2267 RETURN_FALSE;
2268 } /* }}} */
2269
2270 /* {{{ Read NULL */
PHP_METHOD(SplFileObject,getChildren)2271 PHP_METHOD(SplFileObject, getChildren)
2272 {
2273 if (zend_parse_parameters_none() == FAILURE) {
2274 RETURN_THROWS();
2275 }
2276 /* return NULL */
2277 } /* }}} */
2278
2279 /* {{{ Return current line as CSV */
PHP_METHOD(SplFileObject,fgetcsv)2280 PHP_METHOD(SplFileObject, fgetcsv)
2281 {
2282 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2283 char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure;
2284 int escape = intern->u.file.escape;
2285 char *delim = NULL, *enclo = NULL, *esc = NULL;
2286 size_t d_len = 0, e_len = 0, esc_len = 0;
2287
2288 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == FAILURE) {
2289 RETURN_THROWS();
2290 }
2291
2292 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2293
2294 if (delim) {
2295 if (d_len != 1) {
2296 zend_argument_value_error(1, "must be a single character");
2297 RETURN_THROWS();
2298 }
2299 delimiter = delim[0];
2300 }
2301 if (enclo) {
2302 if (e_len != 1) {
2303 zend_argument_value_error(2, "must be a single character");
2304 RETURN_THROWS();
2305 }
2306 enclosure = enclo[0];
2307 }
2308 if (esc) {
2309 if (esc_len > 1) {
2310 zend_argument_value_error(3, "must be empty or a single character");
2311 RETURN_THROWS();
2312 }
2313 if (esc_len == 0) {
2314 escape = PHP_CSV_NO_ESCAPE;
2315 } else {
2316 escape = (unsigned char) esc[0];
2317 }
2318 }
2319
2320 if (spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value, true) == FAILURE) {
2321 RETURN_FALSE;
2322 }
2323 }
2324 /* }}} */
2325
2326 /* {{{ Output a field array as a CSV line */
PHP_METHOD(SplFileObject,fputcsv)2327 PHP_METHOD(SplFileObject, fputcsv)
2328 {
2329 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2330 char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure;
2331 int escape = intern->u.file.escape;
2332 char *delim = NULL, *enclo = NULL, *esc = NULL;
2333 size_t d_len = 0, e_len = 0, esc_len = 0;
2334 zend_long ret;
2335 zval *fields = NULL;
2336 zend_string *eol = NULL;
2337
2338 if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|sssS", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len, &eol) == FAILURE) {
2339 RETURN_THROWS();
2340 }
2341
2342 if (delim) {
2343 if (d_len != 1) {
2344 zend_argument_value_error(2, "must be a single character");
2345 RETURN_THROWS();
2346 }
2347 delimiter = delim[0];
2348 }
2349 if (enclo) {
2350 if (e_len != 1) {
2351 zend_argument_value_error(3, "must be a single character");
2352 RETURN_THROWS();
2353 }
2354 enclosure = enclo[0];
2355 }
2356 if (esc) {
2357 if (esc_len > 1) {
2358 zend_argument_value_error(4, "must be empty or a single character");
2359 RETURN_THROWS();
2360 }
2361 if (esc_len == 0) {
2362 escape = PHP_CSV_NO_ESCAPE;
2363 } else {
2364 escape = (unsigned char) esc[0];
2365 }
2366 }
2367
2368 ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape, eol);
2369 if (ret < 0) {
2370 RETURN_FALSE;
2371 }
2372 RETURN_LONG(ret);
2373 }
2374 /* }}} */
2375
2376 /* {{{ Set the delimiter, enclosure and escape character used in fgetcsv */
PHP_METHOD(SplFileObject,setCsvControl)2377 PHP_METHOD(SplFileObject, setCsvControl)
2378 {
2379 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2380 char delimiter = ',', enclosure = '"';
2381 int escape = (unsigned char) '\\';
2382 char *delim = NULL, *enclo = NULL, *esc = NULL;
2383 size_t d_len = 0, e_len = 0, esc_len = 0;
2384
2385 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == FAILURE) {
2386 RETURN_THROWS();
2387 }
2388
2389 if (delim) {
2390 if (d_len != 1) {
2391 zend_argument_value_error(1, "must be a single character");
2392 RETURN_THROWS();
2393 }
2394 delimiter = delim[0];
2395 }
2396 if (enclo) {
2397 if (e_len != 1) {
2398 zend_argument_value_error(2, "must be a single character");
2399 RETURN_THROWS();
2400 }
2401 enclosure = enclo[0];
2402 }
2403 if (esc) {
2404 if (esc_len > 1) {
2405 zend_argument_value_error(3, "must be empty or a single character");
2406 RETURN_THROWS();
2407 }
2408 if (esc_len == 0) {
2409 escape = PHP_CSV_NO_ESCAPE;
2410 } else {
2411 escape = (unsigned char) esc[0];
2412 }
2413 }
2414
2415 intern->u.file.delimiter = delimiter;
2416 intern->u.file.enclosure = enclosure;
2417 intern->u.file.escape = escape;
2418 }
2419 /* }}} */
2420
2421 /* {{{ Get the delimiter, enclosure and escape character used in fgetcsv */
PHP_METHOD(SplFileObject,getCsvControl)2422 PHP_METHOD(SplFileObject, getCsvControl)
2423 {
2424 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2425 char delimiter[2], enclosure[2], escape[2];
2426
2427 if (zend_parse_parameters_none() == FAILURE) {
2428 RETURN_THROWS();
2429 }
2430
2431 array_init(return_value);
2432
2433 delimiter[0] = intern->u.file.delimiter;
2434 delimiter[1] = '\0';
2435 enclosure[0] = intern->u.file.enclosure;
2436 enclosure[1] = '\0';
2437 if (intern->u.file.escape == PHP_CSV_NO_ESCAPE) {
2438 escape[0] = '\0';
2439 } else {
2440 escape[0] = (unsigned char) intern->u.file.escape;
2441 escape[1] = '\0';
2442 }
2443
2444 add_next_index_string(return_value, delimiter);
2445 add_next_index_string(return_value, enclosure);
2446 add_next_index_string(return_value, escape);
2447 }
2448 /* }}} */
2449
2450 /* {{{ Portable file locking */
PHP_METHOD(SplFileObject,flock)2451 PHP_METHOD(SplFileObject, flock)
2452 {
2453 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2454 zval *wouldblock = NULL;
2455 zend_long operation = 0;
2456
2457 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|z", &operation, &wouldblock) == FAILURE) {
2458 RETURN_THROWS();
2459 }
2460
2461 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2462
2463 php_flock_common(intern->u.file.stream, operation, 1, wouldblock, return_value);
2464 }
2465 /* }}} */
2466
2467 /* {{{ Flush the file */
PHP_METHOD(SplFileObject,fflush)2468 PHP_METHOD(SplFileObject, fflush)
2469 {
2470 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2471
2472 if (zend_parse_parameters_none() == FAILURE) {
2473 RETURN_THROWS();
2474 }
2475
2476 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2477
2478 RETURN_BOOL(!php_stream_flush(intern->u.file.stream));
2479 } /* }}} */
2480
2481 /* {{{ Return current file position */
PHP_METHOD(SplFileObject,ftell)2482 PHP_METHOD(SplFileObject, ftell)
2483 {
2484 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2485 zend_long ret;
2486
2487 if (zend_parse_parameters_none() == FAILURE) {
2488 RETURN_THROWS();
2489 }
2490
2491 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2492
2493 ret = php_stream_tell(intern->u.file.stream);
2494
2495 if (ret == -1) {
2496 RETURN_FALSE;
2497 } else {
2498 RETURN_LONG(ret);
2499 }
2500 } /* }}} */
2501
2502 /* {{{ Seek to a position */
PHP_METHOD(SplFileObject,fseek)2503 PHP_METHOD(SplFileObject, fseek)
2504 {
2505 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2506 zend_long pos, whence = SEEK_SET;
2507
2508 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &pos, &whence) == FAILURE) {
2509 RETURN_THROWS();
2510 }
2511
2512 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2513
2514 spl_filesystem_file_free_line(intern);
2515 RETURN_LONG(php_stream_seek(intern->u.file.stream, pos, (int)whence));
2516 } /* }}} */
2517
2518 /* {{{ Get a character from the file */
PHP_METHOD(SplFileObject,fgetc)2519 PHP_METHOD(SplFileObject, fgetc)
2520 {
2521 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2522
2523 if (zend_parse_parameters_none() == FAILURE) {
2524 RETURN_THROWS();
2525 }
2526
2527 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2528
2529 spl_filesystem_file_free_line(intern);
2530
2531 int result = php_stream_getc(intern->u.file.stream);
2532
2533 if (result == EOF) {
2534 RETURN_FALSE;
2535 }
2536 if (result == '\n') {
2537 intern->u.file.current_line_num++;
2538 }
2539
2540 RETURN_STR(ZSTR_CHAR((zend_uchar)result));
2541 } /* }}} */
2542
2543 /* {{{ Output all remaining data from a file pointer */
PHP_METHOD(SplFileObject,fpassthru)2544 PHP_METHOD(SplFileObject, fpassthru)
2545 {
2546 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2547
2548 if (zend_parse_parameters_none() == FAILURE) {
2549 RETURN_THROWS();
2550 }
2551
2552 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2553
2554 RETURN_LONG(php_stream_passthru(intern->u.file.stream));
2555 } /* }}} */
2556
2557 /* {{{ Implements a mostly ANSI compatible fscanf() */
PHP_METHOD(SplFileObject,fscanf)2558 PHP_METHOD(SplFileObject, fscanf)
2559 {
2560 uint32_t num_varargs = 0;
2561 zend_string *format_str;
2562 zval *varargs= NULL;
2563 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2564
2565 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S*", &format_str, &varargs, &num_varargs) == FAILURE) {
2566 RETURN_THROWS();
2567 }
2568
2569 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2570
2571 /* Get next line */
2572 if (spl_filesystem_file_read(intern, /* silent */ false, /* csv */ false) == FAILURE) {
2573 RETURN_THROWS();
2574 }
2575
2576 int result = php_sscanf_internal(ZSTR_VAL(intern->u.file.current_line), ZSTR_VAL(format_str), (int)num_varargs, varargs, 0, return_value);
2577
2578 if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
2579 WRONG_PARAM_COUNT;
2580 }
2581 }
2582 /* }}} */
2583
2584 /* {{{ Binary-safe file write */
PHP_METHOD(SplFileObject,fwrite)2585 PHP_METHOD(SplFileObject, fwrite)
2586 {
2587 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2588 char *str;
2589 size_t str_len;
2590 zend_long length = 0;
2591 ssize_t written;
2592
2593 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &str, &str_len, &length) == FAILURE) {
2594 RETURN_THROWS();
2595 }
2596
2597 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2598
2599 if (ZEND_NUM_ARGS() > 1) {
2600 if (length >= 0) {
2601 str_len = MIN((size_t)length, str_len);
2602 } else {
2603 /* Negative length given, nothing to write */
2604 str_len = 0;
2605 }
2606 }
2607 if (!str_len) {
2608 RETURN_LONG(0);
2609 }
2610
2611 written = php_stream_write(intern->u.file.stream, str, str_len);
2612 if (written < 0) {
2613 RETURN_FALSE;
2614 }
2615 RETURN_LONG(written);
2616 } /* }}} */
2617
PHP_METHOD(SplFileObject,fread)2618 PHP_METHOD(SplFileObject, fread)
2619 {
2620 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2621 zend_long length = 0;
2622 zend_string *str;
2623
2624 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &length) == FAILURE) {
2625 RETURN_THROWS();
2626 }
2627
2628 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2629
2630 if (length <= 0) {
2631 zend_argument_value_error(1, "must be greater than 0");
2632 RETURN_THROWS();
2633 }
2634
2635 str = php_stream_read_to_str(intern->u.file.stream, length);
2636 if (!str) {
2637 RETURN_FALSE;
2638 }
2639 RETURN_STR(str);
2640 }
2641
2642 /* {{{ Stat() on a filehandle */
PHP_METHOD(SplFileObject,fstat)2643 PHP_METHOD(SplFileObject, fstat)
2644 {
2645 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2646
2647 if (zend_parse_parameters_none() == FAILURE) {
2648 RETURN_THROWS();
2649 }
2650
2651 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2652
2653 php_fstat(intern->u.file.stream, return_value);
2654 }
2655 /* }}} */
2656
2657 /* {{{ Truncate file to 'size' length */
PHP_METHOD(SplFileObject,ftruncate)2658 PHP_METHOD(SplFileObject, ftruncate)
2659 {
2660 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2661 zend_long size;
2662
2663 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
2664 RETURN_THROWS();
2665 }
2666
2667 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2668
2669 if (!php_stream_truncate_supported(intern->u.file.stream)) {
2670 zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't truncate file %s", ZSTR_VAL(intern->file_name));
2671 RETURN_THROWS();
2672 }
2673
2674 RETURN_BOOL(0 == php_stream_truncate_set_size(intern->u.file.stream, size));
2675 } /* }}} */
2676
2677 /* {{{ Seek to specified line */
PHP_METHOD(SplFileObject,seek)2678 PHP_METHOD(SplFileObject, seek)
2679 {
2680 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2681 zend_long line_pos, i;
2682
2683 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &line_pos) == FAILURE) {
2684 RETURN_THROWS();
2685 }
2686
2687 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2688
2689 if (line_pos < 0) {
2690 zend_argument_value_error(1, "must be greater than or equal to 0");
2691 RETURN_THROWS();
2692 }
2693
2694 spl_filesystem_file_rewind(ZEND_THIS, intern);
2695
2696 for (i = 0; i < line_pos; i++) {
2697 if (spl_filesystem_file_read_line(ZEND_THIS, intern, true) == FAILURE) {
2698 return;
2699 }
2700 }
2701 if (line_pos > 0 && !SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2702 intern->u.file.current_line_num++;
2703 spl_filesystem_file_free_line(intern);
2704 }
2705 } /* }}} */
2706
PHP_METHOD(SplFileObject,__toString)2707 PHP_METHOD(SplFileObject, __toString)
2708 {
2709 if (zend_parse_parameters_none() == FAILURE) {
2710 RETURN_THROWS();
2711 }
2712
2713 spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2714
2715 CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2716
2717 if (!intern->u.file.current_line) {
2718 ZEND_ASSERT(Z_ISUNDEF(intern->u.file.current_zval));
2719 zend_result result = spl_filesystem_file_read_line(ZEND_THIS, intern, false);
2720 if (UNEXPECTED(result != SUCCESS)) {
2721 RETURN_THROWS();
2722 }
2723 }
2724
2725 RETURN_STR_COPY(intern->u.file.current_line);
2726 }
2727
2728 /* {{{ PHP_MINIT_FUNCTION(spl_directory) */
PHP_MINIT_FUNCTION(spl_directory)2729 PHP_MINIT_FUNCTION(spl_directory)
2730 {
2731 spl_ce_SplFileInfo = register_class_SplFileInfo(zend_ce_stringable);
2732 spl_ce_SplFileInfo->create_object = spl_filesystem_object_new;
2733 spl_ce_SplFileInfo->default_object_handlers = &spl_filesystem_object_handlers;
2734
2735 memcpy(&spl_filesystem_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
2736 spl_filesystem_object_handlers.offset = XtOffsetOf(spl_filesystem_object, std);
2737 spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
2738 spl_filesystem_object_handlers.dtor_obj = spl_filesystem_object_destroy_object;
2739 spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
2740
2741 spl_ce_DirectoryIterator = register_class_DirectoryIterator(spl_ce_SplFileInfo, spl_ce_SeekableIterator);
2742 spl_ce_DirectoryIterator->create_object = spl_filesystem_object_new;
2743 spl_ce_DirectoryIterator->get_iterator = spl_filesystem_dir_get_iterator;
2744
2745 spl_ce_FilesystemIterator = register_class_FilesystemIterator(spl_ce_DirectoryIterator);
2746 spl_ce_FilesystemIterator->create_object = spl_filesystem_object_new;
2747 spl_ce_FilesystemIterator->get_iterator = spl_filesystem_tree_get_iterator;
2748
2749 spl_ce_RecursiveDirectoryIterator = register_class_RecursiveDirectoryIterator(spl_ce_FilesystemIterator, spl_ce_RecursiveIterator);
2750 spl_ce_RecursiveDirectoryIterator->create_object = spl_filesystem_object_new;
2751
2752 memcpy(&spl_filesystem_object_check_handlers, &spl_filesystem_object_handlers, sizeof(zend_object_handlers));
2753 spl_filesystem_object_check_handlers.clone_obj = NULL;
2754 spl_filesystem_object_check_handlers.get_method = spl_filesystem_object_get_method_check;
2755
2756 #ifdef HAVE_GLOB
2757 spl_ce_GlobIterator = register_class_GlobIterator(spl_ce_FilesystemIterator, zend_ce_countable);
2758 spl_ce_GlobIterator->create_object = spl_filesystem_object_new;
2759 spl_ce_GlobIterator->default_object_handlers = &spl_filesystem_object_check_handlers;
2760 #endif
2761
2762 spl_ce_SplFileObject = register_class_SplFileObject(spl_ce_SplFileInfo, spl_ce_RecursiveIterator, spl_ce_SeekableIterator);
2763 spl_ce_SplFileObject->default_object_handlers = &spl_filesystem_object_check_handlers;
2764 spl_ce_SplFileObject->create_object = spl_filesystem_object_new;
2765
2766 spl_ce_SplTempFileObject = register_class_SplTempFileObject(spl_ce_SplFileObject);
2767 spl_ce_SplTempFileObject->create_object = spl_filesystem_object_new;
2768
2769 return SUCCESS;
2770 }
2771 /* }}} */
2772