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