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