/src/php-src/ext/standard/file.c
Line | Count | Source (jump to first uncovered line) |
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 | | | Authors: Rasmus Lerdorf <rasmus@php.net> | |
14 | | | Stig Bakken <ssb@php.net> | |
15 | | | Andi Gutmans <andi@php.net> | |
16 | | | Zeev Suraski <zeev@php.net> | |
17 | | | PHP 4.0 patches by Thies C. Arntzen (thies@thieso.net) | |
18 | | | PHP streams by Wez Furlong (wez@thebrainroom.com) | |
19 | | +----------------------------------------------------------------------+ |
20 | | */ |
21 | | |
22 | | /* {{{ includes */ |
23 | | |
24 | | #include "php.h" |
25 | | #include "ext/standard/flock_compat.h" |
26 | | #include "ext/standard/php_filestat.h" |
27 | | #include "php_open_temporary_file.h" |
28 | | #include "ext/standard/basic_functions.h" |
29 | | #include "php_ini.h" |
30 | | #include "zend_smart_str.h" |
31 | | |
32 | | #include <stdio.h> |
33 | | #include <stdlib.h> |
34 | | #include <errno.h> |
35 | | #include <wchar.h> |
36 | | #include <sys/types.h> |
37 | | #include <sys/stat.h> |
38 | | #include <fcntl.h> |
39 | | |
40 | | #ifdef PHP_WIN32 |
41 | | # include <io.h> |
42 | | # define O_RDONLY _O_RDONLY |
43 | | # include "win32/param.h" |
44 | | # include "win32/winutil.h" |
45 | | # include "win32/fnmatch.h" |
46 | | # include "win32/ioutil.h" |
47 | | #else |
48 | | # ifdef HAVE_SYS_PARAM_H |
49 | | # include <sys/param.h> |
50 | | # endif |
51 | | # ifdef HAVE_SYS_SELECT_H |
52 | | # include <sys/select.h> |
53 | | # endif |
54 | | # include <sys/socket.h> |
55 | | # include <netinet/in.h> |
56 | | # include <netdb.h> |
57 | | # ifdef HAVE_ARPA_INET_H |
58 | | # include <arpa/inet.h> |
59 | | # endif |
60 | | #endif |
61 | | |
62 | | #include "php_string.h" |
63 | | #include "file.h" |
64 | | |
65 | | #ifdef HAVE_PWD_H |
66 | | # ifdef PHP_WIN32 |
67 | | # include "win32/pwd.h" |
68 | | # else |
69 | | # include <pwd.h> |
70 | | # endif |
71 | | #endif |
72 | | |
73 | | #include "fsock.h" |
74 | | #include "fopen_wrappers.h" |
75 | | #include "streamsfuncs.h" /* To define constants in the arg_info */ |
76 | | |
77 | | #ifdef HAVE_SYS_FILE_H |
78 | | # include <sys/file.h> |
79 | | #endif |
80 | | |
81 | | #ifdef HAVE_SYS_MMAN_H |
82 | | # include <sys/mman.h> |
83 | | #endif |
84 | | |
85 | | #include "scanf.h" |
86 | | #include "zend_API.h" |
87 | | |
88 | | #ifdef ZTS |
89 | | int file_globals_id; |
90 | | #else |
91 | | php_file_globals file_globals; |
92 | | #endif |
93 | | |
94 | | #if defined(HAVE_FNMATCH) && !defined(PHP_WIN32) |
95 | | # ifndef _GNU_SOURCE |
96 | | # define _GNU_SOURCE |
97 | | # endif |
98 | | # include <fnmatch.h> |
99 | | #endif |
100 | | |
101 | | #include "zend_attributes.h" |
102 | | #include "file_arginfo.h" |
103 | | |
104 | | /* }}} */ |
105 | | |
106 | | /* {{{ ZTS-stuff / Globals / Prototypes */ |
107 | | |
108 | | /* sharing globals is *evil* */ |
109 | | static int le_stream_context = FAILURE; |
110 | | |
111 | | PHPAPI int php_le_stream_context(void) |
112 | 30 | { |
113 | 30 | return le_stream_context; |
114 | 30 | } |
115 | | /* }}} */ |
116 | | |
117 | | /* {{{ Module-Stuff */ |
118 | | static ZEND_RSRC_DTOR_FUNC(file_context_dtor) |
119 | 30 | { |
120 | 30 | php_stream_context *context = (php_stream_context*)res->ptr; |
121 | 30 | if (Z_TYPE(context->options) != IS_UNDEF) { |
122 | 30 | zval_ptr_dtor(&context->options); |
123 | 30 | ZVAL_UNDEF(&context->options); |
124 | 30 | } |
125 | 30 | php_stream_context_free(context); |
126 | 30 | } |
127 | | |
128 | | static void file_globals_ctor(php_file_globals *file_globals_p) |
129 | 16 | { |
130 | 16 | memset(file_globals_p, 0, sizeof(php_file_globals)); |
131 | 16 | file_globals_p->def_chunk_size = PHP_SOCK_CHUNK_SIZE; |
132 | 16 | } |
133 | | |
134 | | static void file_globals_dtor(php_file_globals *file_globals_p) |
135 | 0 | { |
136 | 0 | #if defined(HAVE_GETHOSTBYNAME_R) |
137 | 0 | if (file_globals_p->tmp_host_buf) { |
138 | 0 | free(file_globals_p->tmp_host_buf); |
139 | 0 | } |
140 | 0 | #endif |
141 | 0 | } |
142 | | |
143 | | static PHP_INI_MH(OnUpdateAutoDetectLineEndings) |
144 | 16 | { |
145 | 16 | if (zend_ini_parse_bool(new_value)) { |
146 | 0 | zend_error(E_DEPRECATED, "auto_detect_line_endings is deprecated"); |
147 | 0 | } |
148 | 16 | return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage); |
149 | 16 | } |
150 | | |
151 | | PHP_INI_BEGIN() |
152 | | STD_PHP_INI_ENTRY("user_agent", NULL, PHP_INI_ALL, OnUpdateString, user_agent, php_file_globals, file_globals) |
153 | | STD_PHP_INI_ENTRY("from", NULL, PHP_INI_ALL, OnUpdateString, from_address, php_file_globals, file_globals) |
154 | | STD_PHP_INI_ENTRY("default_socket_timeout", "60", PHP_INI_ALL, OnUpdateLong, default_socket_timeout, php_file_globals, file_globals) |
155 | | STD_PHP_INI_BOOLEAN("auto_detect_line_endings", "0", PHP_INI_ALL, OnUpdateAutoDetectLineEndings, auto_detect_line_endings, php_file_globals, file_globals) |
156 | | PHP_INI_END() |
157 | | |
158 | | PHP_MINIT_FUNCTION(file) |
159 | 16 | { |
160 | 16 | le_stream_context = zend_register_list_destructors_ex(file_context_dtor, NULL, "stream-context", module_number); |
161 | | |
162 | | #ifdef ZTS |
163 | | ts_allocate_id(&file_globals_id, sizeof(php_file_globals), (ts_allocate_ctor) file_globals_ctor, (ts_allocate_dtor) file_globals_dtor); |
164 | | #else |
165 | 16 | file_globals_ctor(&file_globals); |
166 | 16 | #endif |
167 | | |
168 | 16 | REGISTER_INI_ENTRIES(); |
169 | | |
170 | 16 | register_file_symbols(module_number); |
171 | | |
172 | 16 | return SUCCESS; |
173 | 16 | } |
174 | | /* }}} */ |
175 | | |
176 | | PHP_MSHUTDOWN_FUNCTION(file) /* {{{ */ |
177 | 0 | { |
178 | 0 | #ifndef ZTS |
179 | 0 | file_globals_dtor(&file_globals); |
180 | 0 | #endif |
181 | 0 | return SUCCESS; |
182 | 0 | } |
183 | | /* }}} */ |
184 | | |
185 | | PHPAPI void php_flock_common(php_stream *stream, zend_long operation, |
186 | | uint32_t operation_arg_num, zval *wouldblock, zval *return_value) |
187 | 0 | { |
188 | 0 | int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN }; |
189 | 0 | int act; |
190 | |
|
191 | 0 | act = operation & PHP_LOCK_UN; |
192 | 0 | if (act < 1 || act > 3) { |
193 | 0 | zend_argument_value_error(operation_arg_num, "must be one of LOCK_SH, LOCK_EX, or LOCK_UN"); |
194 | 0 | RETURN_THROWS(); |
195 | 0 | } |
196 | | |
197 | 0 | if (wouldblock) { |
198 | 0 | ZEND_TRY_ASSIGN_REF_LONG(wouldblock, 0); |
199 | 0 | } |
200 | | |
201 | | /* flock_values contains all possible actions if (operation & PHP_LOCK_NB) we won't block on the lock */ |
202 | 0 | act = flock_values[act - 1] | (operation & PHP_LOCK_NB ? LOCK_NB : 0); |
203 | 0 | if (php_stream_lock(stream, act)) { |
204 | 0 | if (operation && errno == EWOULDBLOCK && wouldblock) { |
205 | 0 | ZEND_TRY_ASSIGN_REF_LONG(wouldblock, 1); |
206 | 0 | } |
207 | 0 | RETURN_FALSE; |
208 | 0 | } |
209 | 0 | RETURN_TRUE; |
210 | 0 | } |
211 | | |
212 | | /* {{{ Portable file locking */ |
213 | | PHP_FUNCTION(flock) |
214 | 0 | { |
215 | 0 | zval *wouldblock = NULL; |
216 | 0 | php_stream *stream; |
217 | 0 | zend_long operation = 0; |
218 | |
|
219 | 0 | ZEND_PARSE_PARAMETERS_START(2, 3) |
220 | 0 | PHP_Z_PARAM_STREAM(stream) |
221 | 0 | Z_PARAM_LONG(operation) |
222 | 0 | Z_PARAM_OPTIONAL |
223 | 0 | Z_PARAM_ZVAL(wouldblock) |
224 | 0 | ZEND_PARSE_PARAMETERS_END(); |
225 | | |
226 | 0 | php_flock_common(stream, operation, 2, wouldblock, return_value); |
227 | 0 | } |
228 | | /* }}} */ |
229 | | |
230 | 0 | #define PHP_META_UNSAFE ".\\+*?[^]$() " |
231 | | |
232 | | /* {{{ Extracts all meta tag content attributes from a file and returns an array */ |
233 | | PHP_FUNCTION(get_meta_tags) |
234 | 0 | { |
235 | 0 | char *filename; |
236 | 0 | size_t filename_len; |
237 | 0 | bool use_include_path = 0; |
238 | 0 | int in_tag = 0, done = 0; |
239 | 0 | int looking_for_val = 0, have_name = 0, have_content = 0; |
240 | 0 | int saw_name = 0, saw_content = 0; |
241 | 0 | char *name = NULL, *value = NULL, *temp = NULL; |
242 | 0 | php_meta_tags_token tok, tok_last; |
243 | 0 | php_meta_tags_data md; |
244 | | |
245 | | /* Initialize our structure */ |
246 | 0 | memset(&md, 0, sizeof(md)); |
247 | | |
248 | | /* Parse arguments */ |
249 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
250 | 0 | Z_PARAM_PATH(filename, filename_len) |
251 | 0 | Z_PARAM_OPTIONAL |
252 | 0 | Z_PARAM_BOOL(use_include_path) |
253 | 0 | ZEND_PARSE_PARAMETERS_END(); |
254 | | |
255 | 0 | md.stream = php_stream_open_wrapper(filename, "rb", |
256 | 0 | (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, |
257 | 0 | NULL); |
258 | 0 | if (!md.stream) { |
259 | 0 | RETURN_FALSE; |
260 | 0 | } |
261 | | |
262 | 0 | array_init(return_value); |
263 | |
|
264 | 0 | tok_last = TOK_EOF; |
265 | |
|
266 | 0 | while (!done && (tok = php_next_meta_token(&md)) != TOK_EOF) { |
267 | 0 | if (tok == TOK_ID) { |
268 | 0 | if (tok_last == TOK_OPENTAG) { |
269 | 0 | md.in_meta = !strcasecmp("meta", md.token_data); |
270 | 0 | } else if (tok_last == TOK_SLASH && in_tag) { |
271 | 0 | if (strcasecmp("head", md.token_data) == 0) { |
272 | | /* We are done here! */ |
273 | 0 | done = 1; |
274 | 0 | } |
275 | 0 | } else if (tok_last == TOK_EQUAL && looking_for_val) { |
276 | 0 | if (saw_name) { |
277 | 0 | if (name) efree(name); |
278 | | /* Get the NAME attr (Single word attr, non-quoted) */ |
279 | 0 | temp = name = estrndup(md.token_data, md.token_len); |
280 | |
|
281 | 0 | while (temp && *temp) { |
282 | 0 | if (strchr(PHP_META_UNSAFE, *temp)) { |
283 | 0 | *temp = '_'; |
284 | 0 | } |
285 | 0 | temp++; |
286 | 0 | } |
287 | |
|
288 | 0 | have_name = 1; |
289 | 0 | } else if (saw_content) { |
290 | 0 | if (value) efree(value); |
291 | 0 | value = estrndup(md.token_data, md.token_len); |
292 | 0 | have_content = 1; |
293 | 0 | } |
294 | |
|
295 | 0 | looking_for_val = 0; |
296 | 0 | } else { |
297 | 0 | if (md.in_meta) { |
298 | 0 | if (strcasecmp("name", md.token_data) == 0) { |
299 | 0 | saw_name = 1; |
300 | 0 | saw_content = 0; |
301 | 0 | looking_for_val = 1; |
302 | 0 | } else if (strcasecmp("content", md.token_data) == 0) { |
303 | 0 | saw_name = 0; |
304 | 0 | saw_content = 1; |
305 | 0 | looking_for_val = 1; |
306 | 0 | } |
307 | 0 | } |
308 | 0 | } |
309 | 0 | } else if (tok == TOK_STRING && tok_last == TOK_EQUAL && looking_for_val) { |
310 | 0 | if (saw_name) { |
311 | 0 | if (name) efree(name); |
312 | | /* Get the NAME attr (Quoted single/double) */ |
313 | 0 | temp = name = estrndup(md.token_data, md.token_len); |
314 | |
|
315 | 0 | while (temp && *temp) { |
316 | 0 | if (strchr(PHP_META_UNSAFE, *temp)) { |
317 | 0 | *temp = '_'; |
318 | 0 | } |
319 | 0 | temp++; |
320 | 0 | } |
321 | |
|
322 | 0 | have_name = 1; |
323 | 0 | } else if (saw_content) { |
324 | 0 | if (value) efree(value); |
325 | 0 | value = estrndup(md.token_data, md.token_len); |
326 | 0 | have_content = 1; |
327 | 0 | } |
328 | |
|
329 | 0 | looking_for_val = 0; |
330 | 0 | } else if (tok == TOK_OPENTAG) { |
331 | 0 | if (looking_for_val) { |
332 | 0 | looking_for_val = 0; |
333 | 0 | have_name = saw_name = 0; |
334 | 0 | have_content = saw_content = 0; |
335 | 0 | } |
336 | 0 | in_tag = 1; |
337 | 0 | } else if (tok == TOK_CLOSETAG) { |
338 | 0 | if (have_name) { |
339 | | /* For BC */ |
340 | 0 | zend_str_tolower(name, strlen(name)); |
341 | 0 | if (have_content) { |
342 | 0 | add_assoc_string(return_value, name, value); |
343 | 0 | } else { |
344 | 0 | add_assoc_string(return_value, name, ""); |
345 | 0 | } |
346 | |
|
347 | 0 | efree(name); |
348 | 0 | if (value) efree(value); |
349 | 0 | } else if (have_content) { |
350 | 0 | efree(value); |
351 | 0 | } |
352 | |
|
353 | 0 | name = value = NULL; |
354 | | |
355 | | /* Reset all of our flags */ |
356 | 0 | in_tag = looking_for_val = 0; |
357 | 0 | have_name = saw_name = 0; |
358 | 0 | have_content = saw_content = 0; |
359 | 0 | md.in_meta = 0; |
360 | 0 | } |
361 | |
|
362 | 0 | tok_last = tok; |
363 | |
|
364 | 0 | if (md.token_data) |
365 | 0 | efree(md.token_data); |
366 | |
|
367 | 0 | md.token_data = NULL; |
368 | 0 | } |
369 | |
|
370 | 0 | if (value) efree(value); |
371 | 0 | if (name) efree(name); |
372 | 0 | php_stream_close(md.stream); |
373 | 0 | } |
374 | | /* }}} */ |
375 | | |
376 | | /* {{{ Read the entire file into a string */ |
377 | | PHP_FUNCTION(file_get_contents) |
378 | 8 | { |
379 | 8 | char *filename; |
380 | 8 | size_t filename_len; |
381 | 8 | bool use_include_path = 0; |
382 | 8 | php_stream *stream; |
383 | 8 | zend_long offset = 0; |
384 | 8 | zend_long maxlen; |
385 | 8 | bool maxlen_is_null = 1; |
386 | 8 | zval *zcontext = NULL; |
387 | 8 | php_stream_context *context = NULL; |
388 | 8 | zend_string *contents; |
389 | | |
390 | | /* Parse arguments */ |
391 | 24 | ZEND_PARSE_PARAMETERS_START(1, 5) |
392 | 32 | Z_PARAM_PATH(filename, filename_len) |
393 | 8 | Z_PARAM_OPTIONAL |
394 | 16 | Z_PARAM_BOOL(use_include_path) |
395 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
396 | 0 | Z_PARAM_LONG(offset) |
397 | 0 | Z_PARAM_LONG_OR_NULL(maxlen, maxlen_is_null) |
398 | 8 | ZEND_PARSE_PARAMETERS_END(); |
399 | | |
400 | 8 | if (maxlen_is_null) { |
401 | 8 | maxlen = (ssize_t) PHP_STREAM_COPY_ALL; |
402 | 8 | } else if (maxlen < 0) { |
403 | 0 | zend_argument_value_error(5, "must be greater than or equal to 0"); |
404 | 0 | RETURN_THROWS(); |
405 | 0 | } |
406 | | |
407 | 8 | context = php_stream_context_from_zval(zcontext, 0); |
408 | | |
409 | 8 | stream = php_stream_open_wrapper_ex(filename, "rb", |
410 | 8 | (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, |
411 | 8 | NULL, context); |
412 | 8 | if (!stream) { |
413 | 8 | RETURN_FALSE; |
414 | 8 | } |
415 | | |
416 | | /* disabling the read buffer allows doing the whole transfer |
417 | | in just one read() system call */ |
418 | 0 | if (php_stream_is(stream, PHP_STREAM_IS_STDIO)) { |
419 | 0 | php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL); |
420 | 0 | } |
421 | |
|
422 | 0 | if (offset != 0 && php_stream_seek(stream, offset, ((offset > 0) ? SEEK_SET : SEEK_END)) < 0) { |
423 | 0 | php_error_docref(NULL, E_WARNING, "Failed to seek to position " ZEND_LONG_FMT " in the stream", offset); |
424 | 0 | php_stream_close(stream); |
425 | 0 | RETURN_FALSE; |
426 | 0 | } |
427 | | |
428 | 0 | if ((contents = php_stream_copy_to_mem(stream, maxlen, 0)) != NULL) { |
429 | 0 | RETVAL_STR(contents); |
430 | 0 | } else { |
431 | 0 | RETVAL_EMPTY_STRING(); |
432 | 0 | } |
433 | |
|
434 | 0 | php_stream_close(stream); |
435 | 0 | } |
436 | | /* }}} */ |
437 | | |
438 | | /* {{{ Write/Create a file with contents data and return the number of bytes written */ |
439 | | PHP_FUNCTION(file_put_contents) |
440 | 0 | { |
441 | 0 | php_stream *stream; |
442 | 0 | char *filename; |
443 | 0 | size_t filename_len; |
444 | 0 | zval *data; |
445 | 0 | ssize_t numbytes = 0; |
446 | 0 | zend_long flags = 0; |
447 | 0 | zval *zcontext = NULL; |
448 | 0 | php_stream_context *context = NULL; |
449 | 0 | php_stream *srcstream = NULL; |
450 | 0 | char mode[3] = "wb"; |
451 | |
|
452 | 0 | ZEND_PARSE_PARAMETERS_START(2, 4) |
453 | 0 | Z_PARAM_PATH(filename, filename_len) |
454 | 0 | Z_PARAM_ZVAL(data) |
455 | 0 | Z_PARAM_OPTIONAL |
456 | 0 | Z_PARAM_LONG(flags) |
457 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
458 | 0 | ZEND_PARSE_PARAMETERS_END(); |
459 | | |
460 | 0 | if (Z_TYPE_P(data) == IS_RESOURCE) { |
461 | 0 | php_stream_from_zval(srcstream, data); |
462 | 0 | } |
463 | | |
464 | 0 | context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); |
465 | |
|
466 | 0 | if (flags & PHP_FILE_APPEND) { |
467 | 0 | mode[0] = 'a'; |
468 | 0 | } else if (flags & LOCK_EX) { |
469 | | /* check to make sure we are dealing with a regular file */ |
470 | 0 | if (php_memnstr(filename, "://", sizeof("://") - 1, filename + filename_len)) { |
471 | 0 | if (strncasecmp(filename, "file://", sizeof("file://") - 1)) { |
472 | 0 | php_error_docref(NULL, E_WARNING, "Exclusive locks may only be set for regular files"); |
473 | 0 | RETURN_FALSE; |
474 | 0 | } |
475 | 0 | } |
476 | 0 | mode[0] = 'c'; |
477 | 0 | } |
478 | 0 | mode[2] = '\0'; |
479 | |
|
480 | 0 | stream = php_stream_open_wrapper_ex(filename, mode, ((flags & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); |
481 | 0 | if (stream == NULL) { |
482 | 0 | RETURN_FALSE; |
483 | 0 | } |
484 | | |
485 | 0 | if ((flags & LOCK_EX) && (!php_stream_supports_lock(stream) || php_stream_lock(stream, LOCK_EX))) { |
486 | 0 | php_stream_close(stream); |
487 | 0 | php_error_docref(NULL, E_WARNING, "Exclusive locks are not supported for this stream"); |
488 | 0 | RETURN_FALSE; |
489 | 0 | } |
490 | | |
491 | 0 | if (mode[0] == 'c') { |
492 | 0 | php_stream_truncate_set_size(stream, 0); |
493 | 0 | } |
494 | |
|
495 | 0 | switch (Z_TYPE_P(data)) { |
496 | 0 | case IS_RESOURCE: { |
497 | 0 | size_t len; |
498 | 0 | if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &len) != SUCCESS) { |
499 | 0 | numbytes = -1; |
500 | 0 | } else { |
501 | 0 | if (len > ZEND_LONG_MAX) { |
502 | 0 | php_error_docref(NULL, E_WARNING, "content truncated from %zu to " ZEND_LONG_FMT " bytes", len, ZEND_LONG_MAX); |
503 | 0 | len = ZEND_LONG_MAX; |
504 | 0 | } |
505 | 0 | numbytes = len; |
506 | 0 | } |
507 | 0 | break; |
508 | 0 | } |
509 | 0 | case IS_NULL: |
510 | 0 | case IS_LONG: |
511 | 0 | case IS_DOUBLE: |
512 | 0 | case IS_FALSE: |
513 | 0 | case IS_TRUE: |
514 | 0 | convert_to_string(data); |
515 | 0 | ZEND_FALLTHROUGH; |
516 | 0 | case IS_STRING: |
517 | 0 | if (Z_STRLEN_P(data)) { |
518 | 0 | numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data)); |
519 | 0 | if (numbytes != -1 && numbytes != Z_STRLEN_P(data)) { |
520 | 0 | php_error_docref(NULL, E_WARNING, "Only %zd of %zd bytes written, possibly out of free disk space", numbytes, Z_STRLEN_P(data)); |
521 | 0 | numbytes = -1; |
522 | 0 | } |
523 | 0 | } |
524 | 0 | break; |
525 | | |
526 | 0 | case IS_ARRAY: |
527 | 0 | if (zend_hash_num_elements(Z_ARRVAL_P(data))) { |
528 | 0 | ssize_t bytes_written; |
529 | 0 | zval *tmp; |
530 | |
|
531 | 0 | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), tmp) { |
532 | 0 | zend_string *t; |
533 | 0 | zend_string *str = zval_get_tmp_string(tmp, &t); |
534 | 0 | if (ZSTR_LEN(str)) { |
535 | 0 | numbytes += ZSTR_LEN(str); |
536 | 0 | bytes_written = php_stream_write(stream, ZSTR_VAL(str), ZSTR_LEN(str)); |
537 | 0 | if (bytes_written != ZSTR_LEN(str)) { |
538 | 0 | php_error_docref(NULL, E_WARNING, "Failed to write %zd bytes to %s", ZSTR_LEN(str), filename); |
539 | 0 | zend_tmp_string_release(t); |
540 | 0 | numbytes = -1; |
541 | 0 | break; |
542 | 0 | } |
543 | 0 | } |
544 | 0 | zend_tmp_string_release(t); |
545 | 0 | } ZEND_HASH_FOREACH_END(); |
546 | 0 | } |
547 | 0 | break; |
548 | | |
549 | 0 | case IS_OBJECT: |
550 | 0 | if (Z_OBJ_HT_P(data) != NULL) { |
551 | 0 | zval out; |
552 | |
|
553 | 0 | if (zend_std_cast_object_tostring(Z_OBJ_P(data), &out, IS_STRING) == SUCCESS) { |
554 | 0 | numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRLEN(out)); |
555 | 0 | if (numbytes != -1 && numbytes != Z_STRLEN(out)) { |
556 | 0 | php_error_docref(NULL, E_WARNING, "Only %zd of %zd bytes written, possibly out of free disk space", numbytes, Z_STRLEN(out)); |
557 | 0 | numbytes = -1; |
558 | 0 | } |
559 | 0 | zval_ptr_dtor_str(&out); |
560 | 0 | break; |
561 | 0 | } |
562 | 0 | } |
563 | 0 | ZEND_FALLTHROUGH; |
564 | 0 | default: |
565 | 0 | numbytes = -1; |
566 | 0 | break; |
567 | 0 | } |
568 | 0 | php_stream_close(stream); |
569 | |
|
570 | 0 | if (numbytes < 0) { |
571 | 0 | RETURN_FALSE; |
572 | 0 | } |
573 | | |
574 | 0 | RETURN_LONG(numbytes); |
575 | 0 | } |
576 | | /* }}} */ |
577 | | |
578 | | #define PHP_FILE_BUF_SIZE 80 |
579 | | |
580 | | /* {{{ Read entire file into an array */ |
581 | | PHP_FUNCTION(file) |
582 | 5 | { |
583 | 5 | char *filename; |
584 | 5 | size_t filename_len; |
585 | 5 | char *p, *s, *e; |
586 | 5 | int i = 0; |
587 | 5 | char eol_marker = '\n'; |
588 | 5 | zend_long flags = 0; |
589 | 5 | bool use_include_path; |
590 | 5 | bool include_new_line; |
591 | 5 | bool skip_blank_lines; |
592 | 5 | php_stream *stream; |
593 | 5 | zval *zcontext = NULL; |
594 | 5 | php_stream_context *context = NULL; |
595 | 5 | zend_string *target_buf; |
596 | | |
597 | | /* Parse arguments */ |
598 | 15 | ZEND_PARSE_PARAMETERS_START(1, 3) |
599 | 20 | Z_PARAM_PATH(filename, filename_len) |
600 | 5 | Z_PARAM_OPTIONAL |
601 | 10 | Z_PARAM_LONG(flags) |
602 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
603 | 5 | ZEND_PARSE_PARAMETERS_END(); |
604 | | |
605 | 5 | if ((flags & ~(PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) != 0) { |
606 | 0 | zend_argument_value_error(2, "must be a valid flag value"); |
607 | 0 | RETURN_THROWS(); |
608 | 0 | } |
609 | | |
610 | 5 | use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH; |
611 | 5 | include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES); |
612 | 5 | skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES; |
613 | | |
614 | 5 | context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); |
615 | | |
616 | 5 | stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); |
617 | 5 | if (!stream) { |
618 | 0 | RETURN_FALSE; |
619 | 0 | } |
620 | | |
621 | | /* Initialize return array */ |
622 | 5 | array_init(return_value); |
623 | | |
624 | 5 | if ((target_buf = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0)) != NULL) { |
625 | 0 | s = ZSTR_VAL(target_buf); |
626 | 0 | e = ZSTR_VAL(target_buf) + ZSTR_LEN(target_buf); |
627 | |
|
628 | 0 | if (!(p = (char*)php_stream_locate_eol(stream, target_buf))) { |
629 | 0 | p = e; |
630 | 0 | goto parse_eol; |
631 | 0 | } |
632 | | |
633 | 0 | if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) { |
634 | 0 | eol_marker = '\r'; |
635 | 0 | } |
636 | | |
637 | | /* for performance reasons the code is duplicated, so that the if (include_new_line) |
638 | | * will not need to be done for every single line in the file. */ |
639 | 0 | if (include_new_line) { |
640 | 0 | do { |
641 | 0 | p++; |
642 | 0 | parse_eol: |
643 | 0 | add_index_stringl(return_value, i++, s, p-s); |
644 | 0 | s = p; |
645 | 0 | } while ((p = memchr(p, eol_marker, (e-p)))); |
646 | 0 | } else { |
647 | 0 | do { |
648 | 0 | int windows_eol = 0; |
649 | 0 | if (p != ZSTR_VAL(target_buf) && eol_marker == '\n' && *(p - 1) == '\r') { |
650 | 0 | windows_eol++; |
651 | 0 | } |
652 | 0 | if (skip_blank_lines && !(p-s-windows_eol)) { |
653 | 0 | s = ++p; |
654 | 0 | continue; |
655 | 0 | } |
656 | 0 | add_index_stringl(return_value, i++, s, p-s-windows_eol); |
657 | 0 | s = ++p; |
658 | 0 | } while ((p = memchr(p, eol_marker, (e-p)))); |
659 | 0 | } |
660 | | |
661 | | /* handle any leftovers of files without new lines */ |
662 | 0 | if (s != e) { |
663 | 0 | p = e; |
664 | 0 | goto parse_eol; |
665 | 0 | } |
666 | 0 | } |
667 | | |
668 | 5 | if (target_buf) { |
669 | 0 | zend_string_free(target_buf); |
670 | 0 | } |
671 | 5 | php_stream_close(stream); |
672 | 5 | } |
673 | | /* }}} */ |
674 | | |
675 | | /* {{{ Create a unique filename in a directory */ |
676 | | PHP_FUNCTION(tempnam) |
677 | 0 | { |
678 | 0 | char *dir, *prefix; |
679 | 0 | size_t dir_len, prefix_len; |
680 | 0 | zend_string *opened_path; |
681 | 0 | int fd; |
682 | 0 | zend_string *p; |
683 | |
|
684 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
685 | 0 | Z_PARAM_PATH(dir, dir_len) |
686 | 0 | Z_PARAM_PATH(prefix, prefix_len) |
687 | 0 | ZEND_PARSE_PARAMETERS_END(); |
688 | | |
689 | 0 | p = php_basename(prefix, prefix_len, NULL, 0); |
690 | 0 | if (ZSTR_LEN(p) >= 64) { |
691 | 0 | ZSTR_VAL(p)[63] = '\0'; |
692 | 0 | } |
693 | |
|
694 | 0 | RETVAL_FALSE; |
695 | |
|
696 | 0 | if ((fd = php_open_temporary_fd_ex(dir, ZSTR_VAL(p), &opened_path, PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ALWAYS)) >= 0) { |
697 | 0 | close(fd); |
698 | 0 | RETVAL_STR(opened_path); |
699 | 0 | } |
700 | 0 | zend_string_release_ex(p, 0); |
701 | 0 | } |
702 | | /* }}} */ |
703 | | |
704 | | /* {{{ Create a temporary file that will be deleted automatically after use */ |
705 | | PHP_FUNCTION(tmpfile) |
706 | 0 | { |
707 | 0 | php_stream *stream; |
708 | |
|
709 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
710 | | |
711 | 0 | stream = php_stream_fopen_tmpfile(); |
712 | |
|
713 | 0 | if (stream) { |
714 | 0 | php_stream_to_zval(stream, return_value); |
715 | 0 | } else { |
716 | 0 | RETURN_FALSE; |
717 | 0 | } |
718 | 0 | } |
719 | | /* }}} */ |
720 | | |
721 | | /* {{{ Open a file or a URL and return a file pointer */ |
722 | | PHP_FUNCTION(fopen) |
723 | 0 | { |
724 | 0 | char *filename, *mode; |
725 | 0 | size_t filename_len, mode_len; |
726 | 0 | bool use_include_path = 0; |
727 | 0 | zval *zcontext = NULL; |
728 | 0 | php_stream *stream; |
729 | 0 | php_stream_context *context = NULL; |
730 | |
|
731 | 0 | ZEND_PARSE_PARAMETERS_START(2, 4) |
732 | 0 | Z_PARAM_PATH(filename, filename_len) |
733 | 0 | Z_PARAM_STRING(mode, mode_len) |
734 | 0 | Z_PARAM_OPTIONAL |
735 | 0 | Z_PARAM_BOOL(use_include_path) |
736 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
737 | 0 | ZEND_PARSE_PARAMETERS_END(); |
738 | | |
739 | 0 | context = php_stream_context_from_zval(zcontext, 0); |
740 | |
|
741 | 0 | stream = php_stream_open_wrapper_ex(filename, mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); |
742 | |
|
743 | 0 | if (stream == NULL) { |
744 | 0 | RETURN_FALSE; |
745 | 0 | } |
746 | | |
747 | 0 | php_stream_to_zval(stream, return_value); |
748 | 0 | } |
749 | | /* }}} */ |
750 | | |
751 | | /* {{{ Close an open file pointer */ |
752 | | PHPAPI PHP_FUNCTION(fclose) |
753 | 0 | { |
754 | 0 | php_stream *stream; |
755 | |
|
756 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
757 | 0 | PHP_Z_PARAM_STREAM(stream) |
758 | 0 | ZEND_PARSE_PARAMETERS_END(); |
759 | | |
760 | 0 | if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) { |
761 | 0 | php_error_docref(NULL, E_WARNING, "cannot close the provided stream, as it must not be manually closed"); |
762 | 0 | RETURN_FALSE; |
763 | 0 | } |
764 | | |
765 | 0 | php_stream_free(stream, |
766 | 0 | PHP_STREAM_FREE_KEEP_RSRC | |
767 | 0 | (stream->is_persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE)); |
768 | |
|
769 | 0 | RETURN_TRUE; |
770 | 0 | } |
771 | | /* }}} */ |
772 | | |
773 | | /* {{{ Execute a command and open either a read or a write pipe to it */ |
774 | | PHP_FUNCTION(popen) |
775 | 0 | { |
776 | 0 | char *command, *mode; |
777 | 0 | size_t command_len, mode_len; |
778 | 0 | FILE *fp; |
779 | 0 | php_stream *stream; |
780 | 0 | char *posix_mode; |
781 | |
|
782 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
783 | 0 | Z_PARAM_PATH(command, command_len) |
784 | 0 | Z_PARAM_STRING(mode, mode_len) |
785 | 0 | ZEND_PARSE_PARAMETERS_END(); |
786 | | |
787 | 0 | posix_mode = estrndup(mode, mode_len); |
788 | 0 | #ifndef PHP_WIN32 |
789 | 0 | { |
790 | 0 | char *z = memchr(posix_mode, 'b', mode_len); |
791 | 0 | if (z) { |
792 | 0 | memmove(z, z + 1, mode_len - (z - posix_mode)); |
793 | 0 | mode_len--; |
794 | 0 | } |
795 | 0 | } |
796 | 0 | #endif |
797 | | |
798 | | /* Musl only partially validates the mode. Manually check it to ensure consistent behavior. */ |
799 | 0 | if (mode_len > 2 || |
800 | 0 | (mode_len == 1 && (*posix_mode != 'r' && *posix_mode != 'w')) || |
801 | 0 | (mode_len == 2 && (memcmp(posix_mode, "rb", 2) && memcmp(posix_mode, "wb", 2))) |
802 | 0 | ) { |
803 | 0 | zend_argument_value_error(2, "must be one of \"r\", \"rb\", \"w\", or \"wb\""); |
804 | 0 | efree(posix_mode); |
805 | 0 | RETURN_THROWS(); |
806 | 0 | } |
807 | | |
808 | 0 | fp = VCWD_POPEN(command, posix_mode); |
809 | 0 | if (!fp) { |
810 | 0 | php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno)); |
811 | 0 | efree(posix_mode); |
812 | 0 | RETURN_FALSE; |
813 | 0 | } |
814 | | |
815 | 0 | stream = php_stream_fopen_from_pipe(fp, mode); |
816 | |
|
817 | 0 | if (stream == NULL) { |
818 | 0 | php_error_docref2(NULL, command, mode, E_WARNING, "%s", strerror(errno)); |
819 | 0 | RETVAL_FALSE; |
820 | 0 | } else { |
821 | 0 | php_stream_to_zval(stream, return_value); |
822 | 0 | } |
823 | |
|
824 | 0 | efree(posix_mode); |
825 | 0 | } |
826 | | /* }}} */ |
827 | | |
828 | | /* {{{ Close a file pointer opened by popen() */ |
829 | | PHP_FUNCTION(pclose) |
830 | 0 | { |
831 | 0 | php_stream *stream; |
832 | |
|
833 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
834 | 0 | PHP_Z_PARAM_STREAM(stream) |
835 | 0 | ZEND_PARSE_PARAMETERS_END(); |
836 | | |
837 | 0 | FG(pclose_wait) = 1; |
838 | 0 | zend_list_close(stream->res); |
839 | 0 | FG(pclose_wait) = 0; |
840 | 0 | RETURN_LONG(FG(pclose_ret)); |
841 | 0 | } |
842 | | /* }}} */ |
843 | | |
844 | | /* {{{ Test for end-of-file on a file pointer */ |
845 | | PHPAPI PHP_FUNCTION(feof) |
846 | 2 | { |
847 | 2 | php_stream *stream; |
848 | | |
849 | 6 | ZEND_PARSE_PARAMETERS_START(1, 1) |
850 | 8 | PHP_Z_PARAM_STREAM(stream) |
851 | 2 | ZEND_PARSE_PARAMETERS_END(); |
852 | | |
853 | 2 | if (php_stream_eof(stream)) { |
854 | 2 | RETURN_TRUE; |
855 | 2 | } else { |
856 | 0 | RETURN_FALSE; |
857 | 0 | } |
858 | 2 | } |
859 | | /* }}} */ |
860 | | |
861 | | /* {{{ Get a line from file pointer */ |
862 | | PHPAPI PHP_FUNCTION(fgets) |
863 | 0 | { |
864 | 0 | zend_long len = 1024; |
865 | 0 | bool len_is_null = 1; |
866 | 0 | char *buf = NULL; |
867 | 0 | size_t line_len = 0; |
868 | 0 | zend_string *str; |
869 | 0 | php_stream *stream; |
870 | |
|
871 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
872 | 0 | PHP_Z_PARAM_STREAM(stream) |
873 | 0 | Z_PARAM_OPTIONAL |
874 | 0 | Z_PARAM_LONG_OR_NULL(len, len_is_null) |
875 | 0 | ZEND_PARSE_PARAMETERS_END(); |
876 | | |
877 | 0 | if (len_is_null) { |
878 | | /* ask streams to give us a buffer of an appropriate size */ |
879 | 0 | buf = php_stream_get_line(stream, NULL, 0, &line_len); |
880 | 0 | if (buf == NULL) { |
881 | 0 | RETURN_FALSE; |
882 | 0 | } |
883 | | // TODO: avoid reallocation ??? |
884 | 0 | RETVAL_STRINGL(buf, line_len); |
885 | 0 | efree(buf); |
886 | 0 | } else { |
887 | 0 | if (len <= 0) { |
888 | 0 | zend_argument_value_error(2, "must be greater than 0"); |
889 | 0 | RETURN_THROWS(); |
890 | 0 | } |
891 | | |
892 | 0 | str = zend_string_alloc(len, 0); |
893 | 0 | if (php_stream_get_line(stream, ZSTR_VAL(str), len, &line_len) == NULL) { |
894 | 0 | zend_string_efree(str); |
895 | 0 | RETURN_FALSE; |
896 | 0 | } |
897 | | /* resize buffer if it's much larger than the result. |
898 | | * Only needed if the user requested a buffer size. */ |
899 | 0 | if (line_len < (size_t)len / 2) { |
900 | 0 | str = zend_string_truncate(str, line_len, 0); |
901 | 0 | } else { |
902 | 0 | ZSTR_LEN(str) = line_len; |
903 | 0 | } |
904 | 0 | RETURN_NEW_STR(str); |
905 | 0 | } |
906 | 0 | } |
907 | | /* }}} */ |
908 | | |
909 | | /* {{{ Get a character from file pointer */ |
910 | | PHPAPI PHP_FUNCTION(fgetc) |
911 | 0 | { |
912 | 0 | php_stream *stream; |
913 | |
|
914 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
915 | 0 | PHP_Z_PARAM_STREAM(stream) |
916 | 0 | ZEND_PARSE_PARAMETERS_END(); |
917 | | |
918 | 0 | int result = php_stream_getc(stream); |
919 | |
|
920 | 0 | if (result == EOF) { |
921 | 0 | RETVAL_FALSE; |
922 | 0 | } else { |
923 | 0 | RETURN_CHAR(result); |
924 | 0 | } |
925 | 0 | } |
926 | | /* }}} */ |
927 | | |
928 | | /* {{{ Implements a mostly ANSI compatible fscanf() */ |
929 | | PHP_FUNCTION(fscanf) |
930 | 0 | { |
931 | 0 | int result, argc = 0; |
932 | 0 | size_t format_len; |
933 | 0 | zval *args = NULL; |
934 | 0 | zval *file_handle; |
935 | 0 | char *buf, *format; |
936 | 0 | size_t len; |
937 | 0 | void *what; |
938 | |
|
939 | 0 | ZEND_PARSE_PARAMETERS_START(2, -1) |
940 | 0 | Z_PARAM_RESOURCE(file_handle) |
941 | 0 | Z_PARAM_STRING(format, format_len) |
942 | 0 | Z_PARAM_VARIADIC('*', args, argc) |
943 | 0 | ZEND_PARSE_PARAMETERS_END(); |
944 | | |
945 | 0 | what = zend_fetch_resource2(Z_RES_P(file_handle), "File-Handle", php_file_le_stream(), php_file_le_pstream()); |
946 | | |
947 | | /* we can't do a ZEND_VERIFY_RESOURCE(what), otherwise we end up |
948 | | * with a leak if we have an invalid filehandle. This needs changing |
949 | | * if the code behind ZEND_VERIFY_RESOURCE changed. - cc */ |
950 | 0 | if (!what) { |
951 | 0 | RETURN_THROWS(); |
952 | 0 | } |
953 | | |
954 | 0 | buf = php_stream_get_line((php_stream *) what, NULL, 0, &len); |
955 | 0 | if (buf == NULL) { |
956 | 0 | RETURN_FALSE; |
957 | 0 | } |
958 | | |
959 | 0 | result = php_sscanf_internal(buf, format, argc, args, 0, return_value); |
960 | |
|
961 | 0 | efree(buf); |
962 | |
|
963 | 0 | if (SCAN_ERROR_WRONG_PARAM_COUNT == result) { |
964 | 0 | WRONG_PARAM_COUNT; |
965 | 0 | } |
966 | 0 | } |
967 | | /* }}} */ |
968 | | |
969 | | /* {{{ Binary-safe file write */ |
970 | | PHPAPI PHP_FUNCTION(fwrite) |
971 | 0 | { |
972 | 0 | char *input; |
973 | 0 | size_t inputlen; |
974 | 0 | ssize_t ret; |
975 | 0 | size_t num_bytes; |
976 | 0 | zend_long maxlen = 0; |
977 | 0 | bool maxlen_is_null = 1; |
978 | 0 | php_stream *stream; |
979 | |
|
980 | 0 | ZEND_PARSE_PARAMETERS_START(2, 3) |
981 | 0 | PHP_Z_PARAM_STREAM(stream) |
982 | 0 | Z_PARAM_STRING(input, inputlen) |
983 | 0 | Z_PARAM_OPTIONAL |
984 | 0 | Z_PARAM_LONG_OR_NULL(maxlen, maxlen_is_null) |
985 | 0 | ZEND_PARSE_PARAMETERS_END(); |
986 | | |
987 | 0 | if (maxlen_is_null) { |
988 | 0 | num_bytes = inputlen; |
989 | 0 | } else if (maxlen <= 0) { |
990 | 0 | num_bytes = 0; |
991 | 0 | } else { |
992 | 0 | num_bytes = MIN((size_t) maxlen, inputlen); |
993 | 0 | } |
994 | |
|
995 | 0 | if (!num_bytes) { |
996 | 0 | RETURN_LONG(0); |
997 | 0 | } |
998 | | |
999 | 0 | ret = php_stream_write(stream, input, num_bytes); |
1000 | 0 | if (ret < 0) { |
1001 | 0 | RETURN_FALSE; |
1002 | 0 | } |
1003 | | |
1004 | 0 | RETURN_LONG(ret); |
1005 | 0 | } |
1006 | | /* }}} */ |
1007 | | |
1008 | | /* {{{ Flushes output */ |
1009 | | PHPAPI PHP_FUNCTION(fflush) |
1010 | 0 | { |
1011 | 0 | int ret; |
1012 | 0 | php_stream *stream; |
1013 | |
|
1014 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1015 | 0 | PHP_Z_PARAM_STREAM(stream) |
1016 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1017 | | |
1018 | 0 | ret = php_stream_flush(stream); |
1019 | 0 | if (ret) { |
1020 | 0 | RETURN_FALSE; |
1021 | 0 | } |
1022 | 0 | RETURN_TRUE; |
1023 | 0 | } |
1024 | | /* }}} */ |
1025 | | |
1026 | | /* {{{ Rewind the position of a file pointer */ |
1027 | | PHPAPI PHP_FUNCTION(rewind) |
1028 | 2 | { |
1029 | 2 | php_stream *stream; |
1030 | | |
1031 | 4 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1032 | 4 | PHP_Z_PARAM_STREAM(stream) |
1033 | 2 | ZEND_PARSE_PARAMETERS_END(); |
1034 | | |
1035 | 0 | if (-1 == php_stream_rewind(stream)) { |
1036 | 0 | RETURN_FALSE; |
1037 | 0 | } |
1038 | 0 | RETURN_TRUE; |
1039 | 0 | } |
1040 | | /* }}} */ |
1041 | | |
1042 | | /* {{{ Get file pointer's read/write position */ |
1043 | | PHPAPI PHP_FUNCTION(ftell) |
1044 | 0 | { |
1045 | 0 | zend_long ret; |
1046 | 0 | php_stream *stream; |
1047 | |
|
1048 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1049 | 0 | PHP_Z_PARAM_STREAM(stream) |
1050 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1051 | | |
1052 | 0 | ret = php_stream_tell(stream); |
1053 | 0 | if (ret == -1) { |
1054 | 0 | RETURN_FALSE; |
1055 | 0 | } |
1056 | 0 | RETURN_LONG(ret); |
1057 | 0 | } |
1058 | | /* }}} */ |
1059 | | |
1060 | | /* {{{ Seek on a file pointer */ |
1061 | | PHPAPI PHP_FUNCTION(fseek) |
1062 | 0 | { |
1063 | 0 | zend_long offset, whence = SEEK_SET; |
1064 | 0 | php_stream *stream; |
1065 | |
|
1066 | 0 | ZEND_PARSE_PARAMETERS_START(2, 3) |
1067 | 0 | PHP_Z_PARAM_STREAM(stream) |
1068 | 0 | Z_PARAM_LONG(offset) |
1069 | 0 | Z_PARAM_OPTIONAL |
1070 | 0 | Z_PARAM_LONG(whence) |
1071 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1072 | | |
1073 | 0 | RETURN_LONG(php_stream_seek(stream, offset, (int) whence)); |
1074 | 0 | } |
1075 | | /* }}} */ |
1076 | | |
1077 | | /* {{{ Create a directory */ |
1078 | | PHP_FUNCTION(mkdir) |
1079 | 0 | { |
1080 | 0 | char *dir; |
1081 | 0 | size_t dir_len; |
1082 | 0 | zval *zcontext = NULL; |
1083 | 0 | zend_long mode = 0777; |
1084 | 0 | bool recursive = 0; |
1085 | 0 | php_stream_context *context; |
1086 | |
|
1087 | 0 | ZEND_PARSE_PARAMETERS_START(1, 4) |
1088 | 0 | Z_PARAM_PATH(dir, dir_len) |
1089 | 0 | Z_PARAM_OPTIONAL |
1090 | 0 | Z_PARAM_LONG(mode) |
1091 | 0 | Z_PARAM_BOOL(recursive) |
1092 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
1093 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1094 | | |
1095 | 0 | context = php_stream_context_from_zval(zcontext, 0); |
1096 | |
|
1097 | 0 | RETURN_BOOL(php_stream_mkdir(dir, (int)mode, (recursive ? PHP_STREAM_MKDIR_RECURSIVE : 0) | REPORT_ERRORS, context)); |
1098 | 0 | } |
1099 | | /* }}} */ |
1100 | | |
1101 | | /* {{{ Remove a directory */ |
1102 | | PHP_FUNCTION(rmdir) |
1103 | 0 | { |
1104 | 0 | char *dir; |
1105 | 0 | size_t dir_len; |
1106 | 0 | zval *zcontext = NULL; |
1107 | 0 | php_stream_context *context; |
1108 | |
|
1109 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
1110 | 0 | Z_PARAM_PATH(dir, dir_len) |
1111 | 0 | Z_PARAM_OPTIONAL |
1112 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
1113 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1114 | | |
1115 | 0 | context = php_stream_context_from_zval(zcontext, 0); |
1116 | |
|
1117 | 0 | RETURN_BOOL(php_stream_rmdir(dir, REPORT_ERRORS, context)); |
1118 | 0 | } |
1119 | | /* }}} */ |
1120 | | |
1121 | | /* {{{ Output a file or a URL */ |
1122 | | PHP_FUNCTION(readfile) |
1123 | 0 | { |
1124 | 0 | char *filename; |
1125 | 0 | size_t filename_len; |
1126 | 0 | size_t size = 0; |
1127 | 0 | bool use_include_path = 0; |
1128 | 0 | zval *zcontext = NULL; |
1129 | 0 | php_stream *stream; |
1130 | 0 | php_stream_context *context = NULL; |
1131 | |
|
1132 | 0 | ZEND_PARSE_PARAMETERS_START(1, 3) |
1133 | 0 | Z_PARAM_PATH(filename, filename_len) |
1134 | 0 | Z_PARAM_OPTIONAL |
1135 | 0 | Z_PARAM_BOOL(use_include_path) |
1136 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
1137 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1138 | | |
1139 | 0 | context = php_stream_context_from_zval(zcontext, 0); |
1140 | |
|
1141 | 0 | stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); |
1142 | 0 | if (stream) { |
1143 | 0 | size = php_stream_passthru(stream); |
1144 | 0 | php_stream_close(stream); |
1145 | 0 | RETURN_LONG(size); |
1146 | 0 | } |
1147 | | |
1148 | 0 | RETURN_FALSE; |
1149 | 0 | } |
1150 | | /* }}} */ |
1151 | | |
1152 | | /* {{{ Return or change the umask */ |
1153 | | PHP_FUNCTION(umask) |
1154 | 0 | { |
1155 | 0 | zend_long mask = 0; |
1156 | 0 | bool mask_is_null = 1; |
1157 | 0 | int oldumask; |
1158 | |
|
1159 | 0 | ZEND_PARSE_PARAMETERS_START(0, 1) |
1160 | 0 | Z_PARAM_OPTIONAL |
1161 | 0 | Z_PARAM_LONG_OR_NULL(mask, mask_is_null) |
1162 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1163 | | |
1164 | 0 | oldumask = umask(077); |
1165 | |
|
1166 | 0 | if (BG(umask) == -1) { |
1167 | 0 | BG(umask) = oldumask; |
1168 | 0 | } |
1169 | |
|
1170 | 0 | if (mask_is_null) { |
1171 | 0 | umask(oldumask); |
1172 | 0 | } else { |
1173 | 0 | umask((int) mask); |
1174 | 0 | } |
1175 | |
|
1176 | 0 | RETURN_LONG(oldumask); |
1177 | 0 | } |
1178 | | /* }}} */ |
1179 | | |
1180 | | /* {{{ Output all remaining data from a file pointer */ |
1181 | | PHPAPI PHP_FUNCTION(fpassthru) |
1182 | 0 | { |
1183 | 0 | size_t size; |
1184 | 0 | php_stream *stream; |
1185 | |
|
1186 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1187 | 0 | PHP_Z_PARAM_STREAM(stream) |
1188 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1189 | | |
1190 | 0 | size = php_stream_passthru(stream); |
1191 | 0 | RETURN_LONG(size); |
1192 | 0 | } |
1193 | | /* }}} */ |
1194 | | |
1195 | | /* {{{ Rename a file */ |
1196 | | PHP_FUNCTION(rename) |
1197 | 0 | { |
1198 | 0 | char *old_name, *new_name; |
1199 | 0 | size_t old_name_len, new_name_len; |
1200 | 0 | zval *zcontext = NULL; |
1201 | 0 | php_stream_wrapper *wrapper; |
1202 | 0 | php_stream_context *context; |
1203 | |
|
1204 | 0 | ZEND_PARSE_PARAMETERS_START(2, 3) |
1205 | 0 | Z_PARAM_PATH(old_name, old_name_len) |
1206 | 0 | Z_PARAM_PATH(new_name, new_name_len) |
1207 | 0 | Z_PARAM_OPTIONAL |
1208 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
1209 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1210 | | |
1211 | 0 | wrapper = php_stream_locate_url_wrapper(old_name, NULL, 0); |
1212 | |
|
1213 | 0 | if (!wrapper || !wrapper->wops) { |
1214 | 0 | php_error_docref(NULL, E_WARNING, "Unable to locate stream wrapper"); |
1215 | 0 | RETURN_FALSE; |
1216 | 0 | } |
1217 | | |
1218 | 0 | if (!wrapper->wops->rename) { |
1219 | 0 | php_error_docref(NULL, E_WARNING, "%s wrapper does not support renaming", wrapper->wops->label ? wrapper->wops->label : "Source"); |
1220 | 0 | RETURN_FALSE; |
1221 | 0 | } |
1222 | | |
1223 | 0 | if (wrapper != php_stream_locate_url_wrapper(new_name, NULL, 0)) { |
1224 | 0 | php_error_docref(NULL, E_WARNING, "Cannot rename a file across wrapper types"); |
1225 | 0 | RETURN_FALSE; |
1226 | 0 | } |
1227 | | |
1228 | 0 | context = php_stream_context_from_zval(zcontext, 0); |
1229 | |
|
1230 | 0 | RETURN_BOOL(wrapper->wops->rename(wrapper, old_name, new_name, 0, context)); |
1231 | 0 | } |
1232 | | /* }}} */ |
1233 | | |
1234 | | /* {{{ Delete a file */ |
1235 | | PHP_FUNCTION(unlink) |
1236 | 0 | { |
1237 | 0 | char *filename; |
1238 | 0 | size_t filename_len; |
1239 | 0 | php_stream_wrapper *wrapper; |
1240 | 0 | zval *zcontext = NULL; |
1241 | 0 | php_stream_context *context = NULL; |
1242 | |
|
1243 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
1244 | 0 | Z_PARAM_PATH(filename, filename_len) |
1245 | 0 | Z_PARAM_OPTIONAL |
1246 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
1247 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1248 | | |
1249 | 0 | context = php_stream_context_from_zval(zcontext, 0); |
1250 | |
|
1251 | 0 | wrapper = php_stream_locate_url_wrapper(filename, NULL, 0); |
1252 | |
|
1253 | 0 | if (!wrapper || !wrapper->wops) { |
1254 | 0 | php_error_docref(NULL, E_WARNING, "Unable to locate stream wrapper"); |
1255 | 0 | RETURN_FALSE; |
1256 | 0 | } |
1257 | | |
1258 | 0 | if (!wrapper->wops->unlink) { |
1259 | 0 | php_error_docref(NULL, E_WARNING, "%s does not allow unlinking", wrapper->wops->label ? wrapper->wops->label : "Wrapper"); |
1260 | 0 | RETURN_FALSE; |
1261 | 0 | } |
1262 | 0 | RETURN_BOOL(wrapper->wops->unlink(wrapper, filename, REPORT_ERRORS, context)); |
1263 | 0 | } |
1264 | | /* }}} */ |
1265 | | |
1266 | | PHP_FUNCTION(fsync) |
1267 | 0 | { |
1268 | 0 | php_stream *stream; |
1269 | |
|
1270 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1271 | 0 | PHP_Z_PARAM_STREAM(stream) |
1272 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1273 | | |
1274 | 0 | if (!php_stream_sync_supported(stream)) { |
1275 | 0 | php_error_docref(NULL, E_WARNING, "Can't fsync this stream!"); |
1276 | 0 | RETURN_FALSE; |
1277 | 0 | } |
1278 | | |
1279 | 0 | RETURN_BOOL(php_stream_sync(stream, /* data_only */ 0) == 0); |
1280 | 0 | } |
1281 | | |
1282 | | PHP_FUNCTION(fdatasync) |
1283 | 0 | { |
1284 | 0 | php_stream *stream; |
1285 | |
|
1286 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1287 | 0 | PHP_Z_PARAM_STREAM(stream) |
1288 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1289 | | |
1290 | 0 | if (!php_stream_sync_supported(stream)) { |
1291 | 0 | php_error_docref(NULL, E_WARNING, "Can't fsync this stream!"); |
1292 | 0 | RETURN_FALSE; |
1293 | 0 | } |
1294 | | |
1295 | 0 | RETURN_BOOL(php_stream_sync(stream, /* data_only */ 1) == 0); |
1296 | 0 | } |
1297 | | |
1298 | | /* {{{ Truncate file to 'size' length */ |
1299 | | PHP_FUNCTION(ftruncate) |
1300 | 0 | { |
1301 | 0 | zend_long size; |
1302 | 0 | php_stream *stream; |
1303 | |
|
1304 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
1305 | 0 | PHP_Z_PARAM_STREAM(stream) |
1306 | 0 | Z_PARAM_LONG(size) |
1307 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1308 | | |
1309 | 0 | if (size < 0) { |
1310 | 0 | zend_argument_value_error(2, "must be greater than or equal to 0"); |
1311 | 0 | RETURN_THROWS(); |
1312 | 0 | } |
1313 | | |
1314 | 0 | if (!php_stream_truncate_supported(stream)) { |
1315 | 0 | php_error_docref(NULL, E_WARNING, "Can't truncate this stream!"); |
1316 | 0 | RETURN_FALSE; |
1317 | 0 | } |
1318 | | |
1319 | 0 | RETURN_BOOL(0 == php_stream_truncate_set_size(stream, size)); |
1320 | 0 | } |
1321 | | /* }}} */ |
1322 | | PHPAPI void php_fstat(php_stream *stream, zval *return_value) |
1323 | 0 | { |
1324 | 0 | php_stream_statbuf stat_ssb; |
1325 | 0 | zval stat_dev, stat_ino, stat_mode, stat_nlink, stat_uid, stat_gid, stat_rdev, |
1326 | 0 | stat_size, stat_atime, stat_mtime, stat_ctime, stat_blksize, stat_blocks; |
1327 | 0 | char *stat_sb_names[13] = { |
1328 | 0 | "dev", "ino", "mode", "nlink", "uid", "gid", "rdev", |
1329 | 0 | "size", "atime", "mtime", "ctime", "blksize", "blocks" |
1330 | 0 | }; |
1331 | |
|
1332 | 0 | if (php_stream_stat(stream, &stat_ssb)) { |
1333 | 0 | RETURN_FALSE; |
1334 | 0 | } |
1335 | | |
1336 | 0 | array_init(return_value); |
1337 | |
|
1338 | 0 | ZVAL_LONG(&stat_dev, stat_ssb.sb.st_dev); |
1339 | 0 | ZVAL_LONG(&stat_ino, stat_ssb.sb.st_ino); |
1340 | 0 | ZVAL_LONG(&stat_mode, stat_ssb.sb.st_mode); |
1341 | 0 | ZVAL_LONG(&stat_nlink, stat_ssb.sb.st_nlink); |
1342 | 0 | ZVAL_LONG(&stat_uid, stat_ssb.sb.st_uid); |
1343 | 0 | ZVAL_LONG(&stat_gid, stat_ssb.sb.st_gid); |
1344 | 0 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
1345 | 0 | ZVAL_LONG(&stat_rdev, stat_ssb.sb.st_rdev); |
1346 | | #else |
1347 | | ZVAL_LONG(&stat_rdev, -1); |
1348 | | #endif |
1349 | 0 | ZVAL_LONG(&stat_size, stat_ssb.sb.st_size); |
1350 | 0 | ZVAL_LONG(&stat_atime, stat_ssb.sb.st_atime); |
1351 | 0 | ZVAL_LONG(&stat_mtime, stat_ssb.sb.st_mtime); |
1352 | 0 | ZVAL_LONG(&stat_ctime, stat_ssb.sb.st_ctime); |
1353 | 0 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
1354 | 0 | ZVAL_LONG(&stat_blksize, stat_ssb.sb.st_blksize); |
1355 | | #else |
1356 | | ZVAL_LONG(&stat_blksize,-1); |
1357 | | #endif |
1358 | 0 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
1359 | 0 | ZVAL_LONG(&stat_blocks, stat_ssb.sb.st_blocks); |
1360 | | #else |
1361 | | ZVAL_LONG(&stat_blocks,-1); |
1362 | | #endif |
1363 | | /* Store numeric indexes in proper order */ |
1364 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_dev); |
1365 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ino); |
1366 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mode); |
1367 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_nlink); |
1368 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_uid); |
1369 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_gid); |
1370 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_rdev); |
1371 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_size); |
1372 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_atime); |
1373 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mtime); |
1374 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ctime); |
1375 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blksize); |
1376 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blocks); |
1377 | | |
1378 | | /* Store string indexes referencing the same zval*/ |
1379 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[0], strlen(stat_sb_names[0]), &stat_dev); |
1380 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[1], strlen(stat_sb_names[1]), &stat_ino); |
1381 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[2], strlen(stat_sb_names[2]), &stat_mode); |
1382 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[3], strlen(stat_sb_names[3]), &stat_nlink); |
1383 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[4], strlen(stat_sb_names[4]), &stat_uid); |
1384 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[5], strlen(stat_sb_names[5]), &stat_gid); |
1385 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[6], strlen(stat_sb_names[6]), &stat_rdev); |
1386 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[7], strlen(stat_sb_names[7]), &stat_size); |
1387 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[8], strlen(stat_sb_names[8]), &stat_atime); |
1388 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[9], strlen(stat_sb_names[9]), &stat_mtime); |
1389 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[10], strlen(stat_sb_names[10]), &stat_ctime); |
1390 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize); |
1391 | 0 | zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks); |
1392 | 0 | } |
1393 | | |
1394 | | /* {{{ Stat() on a filehandle */ |
1395 | | PHP_FUNCTION(fstat) |
1396 | 0 | { |
1397 | 0 | php_stream *stream; |
1398 | |
|
1399 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1400 | 0 | PHP_Z_PARAM_STREAM(stream) |
1401 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1402 | | |
1403 | 0 | php_fstat(stream, return_value); |
1404 | 0 | } |
1405 | | /* }}} */ |
1406 | | |
1407 | | /* {{{ Copy a file */ |
1408 | | PHP_FUNCTION(copy) |
1409 | 0 | { |
1410 | 0 | char *source, *target; |
1411 | 0 | size_t source_len, target_len; |
1412 | 0 | zval *zcontext = NULL; |
1413 | 0 | php_stream_context *context; |
1414 | |
|
1415 | 0 | ZEND_PARSE_PARAMETERS_START(2, 3) |
1416 | 0 | Z_PARAM_PATH(source, source_len) |
1417 | 0 | Z_PARAM_PATH(target, target_len) |
1418 | 0 | Z_PARAM_OPTIONAL |
1419 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
1420 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1421 | | |
1422 | 0 | if (php_stream_locate_url_wrapper(source, NULL, 0) == &php_plain_files_wrapper && php_check_open_basedir(source)) { |
1423 | 0 | RETURN_FALSE; |
1424 | 0 | } |
1425 | | |
1426 | 0 | context = php_stream_context_from_zval(zcontext, 0); |
1427 | |
|
1428 | 0 | RETURN_BOOL(php_copy_file_ctx(source, target, 0, context) == SUCCESS); |
1429 | 0 | } |
1430 | | /* }}} */ |
1431 | | |
1432 | | /* {{{ php_copy_file */ |
1433 | | PHPAPI zend_result php_copy_file(const char *src, const char *dest) |
1434 | 0 | { |
1435 | 0 | return php_copy_file_ctx(src, dest, 0, NULL); |
1436 | 0 | } |
1437 | | /* }}} */ |
1438 | | |
1439 | | /* {{{ php_copy_file_ex */ |
1440 | | PHPAPI zend_result php_copy_file_ex(const char *src, const char *dest, int src_flags) |
1441 | 0 | { |
1442 | 0 | return php_copy_file_ctx(src, dest, src_flags, NULL); |
1443 | 0 | } |
1444 | | /* }}} */ |
1445 | | |
1446 | | /* {{{ php_copy_file_ctx */ |
1447 | | PHPAPI zend_result php_copy_file_ctx(const char *src, const char *dest, int src_flags, php_stream_context *ctx) |
1448 | 0 | { |
1449 | 0 | php_stream *srcstream = NULL, *deststream = NULL; |
1450 | 0 | zend_result ret = FAILURE; |
1451 | 0 | php_stream_statbuf src_s, dest_s; |
1452 | 0 | int src_stat_flags = (src_flags & STREAM_DISABLE_OPEN_BASEDIR) ? PHP_STREAM_URL_STAT_IGNORE_OPEN_BASEDIR : 0; |
1453 | |
|
1454 | 0 | switch (php_stream_stat_path_ex(src, src_stat_flags, &src_s, ctx)) { |
1455 | 0 | case -1: |
1456 | | /* non-statable stream */ |
1457 | 0 | goto safe_to_copy; |
1458 | 0 | break; |
1459 | 0 | case 0: |
1460 | 0 | break; |
1461 | 0 | default: /* failed to stat file, does not exist? */ |
1462 | 0 | return ret; |
1463 | 0 | } |
1464 | 0 | if (S_ISDIR(src_s.sb.st_mode)) { |
1465 | 0 | php_error_docref(NULL, E_WARNING, "The first argument to copy() function cannot be a directory"); |
1466 | 0 | return FAILURE; |
1467 | 0 | } |
1468 | | |
1469 | 0 | switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET, &dest_s, ctx)) { |
1470 | 0 | case -1: |
1471 | | /* non-statable stream */ |
1472 | 0 | goto safe_to_copy; |
1473 | 0 | break; |
1474 | 0 | case 0: |
1475 | 0 | break; |
1476 | 0 | default: /* failed to stat file, does not exist? */ |
1477 | 0 | return ret; |
1478 | 0 | } |
1479 | 0 | if (S_ISDIR(dest_s.sb.st_mode)) { |
1480 | 0 | php_error_docref(NULL, E_WARNING, "The second argument to copy() function cannot be a directory"); |
1481 | 0 | return FAILURE; |
1482 | 0 | } |
1483 | 0 | if (!src_s.sb.st_ino || !dest_s.sb.st_ino) { |
1484 | 0 | goto no_stat; |
1485 | 0 | } |
1486 | 0 | if (src_s.sb.st_ino == dest_s.sb.st_ino && src_s.sb.st_dev == dest_s.sb.st_dev) { |
1487 | 0 | return ret; |
1488 | 0 | } else { |
1489 | 0 | goto safe_to_copy; |
1490 | 0 | } |
1491 | 0 | no_stat: |
1492 | 0 | { |
1493 | 0 | char *sp, *dp; |
1494 | 0 | int res; |
1495 | |
|
1496 | 0 | if ((sp = expand_filepath(src, NULL)) == NULL) { |
1497 | 0 | return ret; |
1498 | 0 | } |
1499 | 0 | if ((dp = expand_filepath(dest, NULL)) == NULL) { |
1500 | 0 | efree(sp); |
1501 | 0 | goto safe_to_copy; |
1502 | 0 | } |
1503 | | |
1504 | 0 | res = |
1505 | 0 | #ifndef PHP_WIN32 |
1506 | 0 | !strcmp(sp, dp); |
1507 | | #else |
1508 | | !strcasecmp(sp, dp); |
1509 | | #endif |
1510 | |
|
1511 | 0 | efree(sp); |
1512 | 0 | efree(dp); |
1513 | 0 | if (res) { |
1514 | 0 | return ret; |
1515 | 0 | } |
1516 | 0 | } |
1517 | 0 | safe_to_copy: |
1518 | |
|
1519 | 0 | srcstream = php_stream_open_wrapper_ex(src, "rb", src_flags | REPORT_ERRORS, NULL, ctx); |
1520 | |
|
1521 | 0 | if (!srcstream) { |
1522 | 0 | return ret; |
1523 | 0 | } |
1524 | | |
1525 | 0 | deststream = php_stream_open_wrapper_ex(dest, "wb", REPORT_ERRORS, NULL, ctx); |
1526 | |
|
1527 | 0 | if (deststream) { |
1528 | 0 | ret = php_stream_copy_to_stream_ex(srcstream, deststream, PHP_STREAM_COPY_ALL, NULL); |
1529 | 0 | } |
1530 | 0 | php_stream_close(srcstream); |
1531 | 0 | if (deststream) { |
1532 | 0 | php_stream_close(deststream); |
1533 | 0 | } |
1534 | 0 | return ret; |
1535 | 0 | } |
1536 | | /* }}} */ |
1537 | | |
1538 | | /* {{{ Binary-safe file read */ |
1539 | | PHPAPI PHP_FUNCTION(fread) |
1540 | 0 | { |
1541 | 0 | zend_long len; |
1542 | 0 | php_stream *stream; |
1543 | 0 | zend_string *str; |
1544 | |
|
1545 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
1546 | 0 | PHP_Z_PARAM_STREAM(stream) |
1547 | 0 | Z_PARAM_LONG(len) |
1548 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1549 | | |
1550 | 0 | if (len <= 0) { |
1551 | 0 | zend_argument_value_error(2, "must be greater than 0"); |
1552 | 0 | RETURN_THROWS(); |
1553 | 0 | } |
1554 | | |
1555 | 0 | str = php_stream_read_to_str(stream, len); |
1556 | 0 | if (!str) { |
1557 | 0 | zval_ptr_dtor_str(return_value); |
1558 | 0 | RETURN_FALSE; |
1559 | 0 | } |
1560 | | |
1561 | 0 | RETURN_STR(str); |
1562 | 0 | } |
1563 | | /* }}} */ |
1564 | | |
1565 | | static const char *php_fgetcsv_lookup_trailing_spaces(const char *ptr, size_t len) /* {{{ */ |
1566 | 6.09k | { |
1567 | 6.09k | int inc_len; |
1568 | 6.09k | unsigned char last_chars[2] = { 0, 0 }; |
1569 | | |
1570 | 196k | while (len > 0) { |
1571 | 190k | inc_len = (*ptr == '\0' ? 1 : php_mblen(ptr, len)); |
1572 | 190k | switch (inc_len) { |
1573 | 93 | case -2: |
1574 | 37.8k | case -1: |
1575 | 37.8k | inc_len = 1; |
1576 | 37.8k | php_mb_reset(); |
1577 | 37.8k | break; |
1578 | 0 | case 0: |
1579 | 0 | goto quit_loop; |
1580 | 152k | case 1: |
1581 | 152k | default: |
1582 | 152k | last_chars[0] = last_chars[1]; |
1583 | 152k | last_chars[1] = *ptr; |
1584 | 152k | break; |
1585 | 190k | } |
1586 | 190k | ptr += inc_len; |
1587 | 190k | len -= inc_len; |
1588 | 190k | } |
1589 | 6.09k | quit_loop: |
1590 | 6.09k | switch (last_chars[1]) { |
1591 | 117 | case '\n': |
1592 | 117 | if (last_chars[0] == '\r') { |
1593 | 0 | return ptr - 2; |
1594 | 0 | } |
1595 | 117 | ZEND_FALLTHROUGH; |
1596 | 117 | case '\r': |
1597 | 117 | return ptr - 1; |
1598 | 6.09k | } |
1599 | 5.97k | return ptr; |
1600 | 6.09k | } |
1601 | | /* }}} */ |
1602 | | |
1603 | | PHPAPI int php_csv_handle_escape_argument(const zend_string *escape_str, uint32_t arg_num) |
1604 | 167 | { |
1605 | 167 | if (escape_str != NULL) { |
1606 | 0 | if (ZSTR_LEN(escape_str) > 1) { |
1607 | 0 | zend_argument_value_error(arg_num, "must be empty or a single character"); |
1608 | 0 | return PHP_CSV_ESCAPE_ERROR; |
1609 | 0 | } |
1610 | 0 | if (ZSTR_LEN(escape_str) < 1) { |
1611 | 0 | return PHP_CSV_NO_ESCAPE; |
1612 | 0 | } else { |
1613 | | /* use first character from string */ |
1614 | 0 | return (unsigned char) ZSTR_VAL(escape_str)[0]; |
1615 | 0 | } |
1616 | 167 | } else { |
1617 | 167 | php_error_docref(NULL, E_DEPRECATED, "the $escape parameter must be provided as its default value will change"); |
1618 | 167 | if (UNEXPECTED(EG(exception))) { |
1619 | 0 | return PHP_CSV_ESCAPE_ERROR; |
1620 | 0 | } |
1621 | 167 | return (unsigned char) '\\'; |
1622 | 167 | } |
1623 | 167 | } |
1624 | | |
1625 | 0 | #define FPUTCSV_FLD_CHK(c) memchr(ZSTR_VAL(field_str), c, ZSTR_LEN(field_str)) |
1626 | | |
1627 | | /* {{{ Format line as CSV and write to file pointer */ |
1628 | | PHP_FUNCTION(fputcsv) |
1629 | 0 | { |
1630 | 0 | char delimiter = ','; /* allow this to be set as parameter */ |
1631 | 0 | char enclosure = '"'; /* allow this to be set as parameter */ |
1632 | 0 | php_stream *stream; |
1633 | 0 | zval *fields = NULL; |
1634 | 0 | ssize_t ret; |
1635 | 0 | char *delimiter_str = NULL, *enclosure_str = NULL; |
1636 | 0 | zend_string *escape_str = NULL; |
1637 | 0 | size_t delimiter_str_len = 0, enclosure_str_len = 0; |
1638 | 0 | zend_string *eol_str = NULL; |
1639 | |
|
1640 | 0 | ZEND_PARSE_PARAMETERS_START(2, 6) |
1641 | 0 | PHP_Z_PARAM_STREAM(stream) |
1642 | 0 | Z_PARAM_ARRAY(fields) |
1643 | 0 | Z_PARAM_OPTIONAL |
1644 | 0 | Z_PARAM_STRING(delimiter_str, delimiter_str_len) |
1645 | 0 | Z_PARAM_STRING(enclosure_str, enclosure_str_len) |
1646 | 0 | Z_PARAM_STR(escape_str) |
1647 | 0 | Z_PARAM_STR_OR_NULL(eol_str) |
1648 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1649 | | |
1650 | 0 | if (delimiter_str != NULL) { |
1651 | | /* Make sure that there is at least one character in string */ |
1652 | 0 | if (delimiter_str_len != 1) { |
1653 | 0 | zend_argument_value_error(3, "must be a single character"); |
1654 | 0 | RETURN_THROWS(); |
1655 | 0 | } |
1656 | | |
1657 | | /* use first character from string */ |
1658 | 0 | delimiter = *delimiter_str; |
1659 | 0 | } |
1660 | | |
1661 | 0 | if (enclosure_str != NULL) { |
1662 | 0 | if (enclosure_str_len != 1) { |
1663 | 0 | zend_argument_value_error(4, "must be a single character"); |
1664 | 0 | RETURN_THROWS(); |
1665 | 0 | } |
1666 | | /* use first character from string */ |
1667 | 0 | enclosure = *enclosure_str; |
1668 | 0 | } |
1669 | | |
1670 | 0 | int escape_char = php_csv_handle_escape_argument(escape_str, 5); |
1671 | 0 | if (escape_char == PHP_CSV_ESCAPE_ERROR) { |
1672 | 0 | RETURN_THROWS(); |
1673 | 0 | } |
1674 | | |
1675 | 0 | ret = php_fputcsv(stream, fields, delimiter, enclosure, escape_char, eol_str); |
1676 | 0 | if (ret < 0) { |
1677 | 0 | RETURN_FALSE; |
1678 | 0 | } |
1679 | 0 | RETURN_LONG(ret); |
1680 | 0 | } |
1681 | | /* }}} */ |
1682 | | |
1683 | | /* {{{ PHPAPI size_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, int escape_char, zend_string *eol_str) */ |
1684 | | PHPAPI ssize_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, int escape_char, zend_string *eol_str) |
1685 | 0 | { |
1686 | 0 | uint32_t count, i = 0; |
1687 | 0 | size_t ret; |
1688 | 0 | zval *field_tmp; |
1689 | 0 | smart_str csvline = {0}; |
1690 | |
|
1691 | 0 | ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE); |
1692 | 0 | count = zend_hash_num_elements(Z_ARRVAL_P(fields)); |
1693 | 0 | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(fields), field_tmp) { |
1694 | 0 | zend_string *tmp_field_str; |
1695 | 0 | zend_string *field_str = zval_get_tmp_string(field_tmp, &tmp_field_str); |
1696 | | |
1697 | | /* enclose a field that contains a delimiter, an enclosure character, or a newline */ |
1698 | 0 | if (FPUTCSV_FLD_CHK(delimiter) || |
1699 | 0 | FPUTCSV_FLD_CHK(enclosure) || |
1700 | 0 | (escape_char != PHP_CSV_NO_ESCAPE && FPUTCSV_FLD_CHK(escape_char)) || |
1701 | 0 | FPUTCSV_FLD_CHK('\n') || |
1702 | 0 | FPUTCSV_FLD_CHK('\r') || |
1703 | 0 | FPUTCSV_FLD_CHK('\t') || |
1704 | 0 | FPUTCSV_FLD_CHK(' ') |
1705 | 0 | ) { |
1706 | 0 | char *ch = ZSTR_VAL(field_str); |
1707 | 0 | char *end = ch + ZSTR_LEN(field_str); |
1708 | 0 | int escaped = 0; |
1709 | |
|
1710 | 0 | smart_str_appendc(&csvline, enclosure); |
1711 | 0 | while (ch < end) { |
1712 | 0 | if (escape_char != PHP_CSV_NO_ESCAPE && *ch == escape_char) { |
1713 | 0 | escaped = 1; |
1714 | 0 | } else if (!escaped && *ch == enclosure) { |
1715 | 0 | smart_str_appendc(&csvline, enclosure); |
1716 | 0 | } else { |
1717 | 0 | escaped = 0; |
1718 | 0 | } |
1719 | 0 | smart_str_appendc(&csvline, *ch); |
1720 | 0 | ch++; |
1721 | 0 | } |
1722 | 0 | smart_str_appendc(&csvline, enclosure); |
1723 | 0 | } else { |
1724 | 0 | smart_str_append(&csvline, field_str); |
1725 | 0 | } |
1726 | |
|
1727 | 0 | if (++i != count) { |
1728 | 0 | smart_str_appendl(&csvline, &delimiter, 1); |
1729 | 0 | } |
1730 | 0 | zend_tmp_string_release(tmp_field_str); |
1731 | 0 | } ZEND_HASH_FOREACH_END(); |
1732 | |
|
1733 | 0 | if (eol_str) { |
1734 | 0 | smart_str_append(&csvline, eol_str); |
1735 | 0 | } else { |
1736 | 0 | smart_str_appendc(&csvline, '\n'); |
1737 | 0 | } |
1738 | 0 | smart_str_0(&csvline); |
1739 | |
|
1740 | 0 | ret = php_stream_write(stream, ZSTR_VAL(csvline.s), ZSTR_LEN(csvline.s)); |
1741 | |
|
1742 | 0 | smart_str_free(&csvline); |
1743 | |
|
1744 | 0 | return ret; |
1745 | 0 | } |
1746 | | /* }}} */ |
1747 | | |
1748 | | /* {{{ Get line from file pointer and parse for CSV fields */ |
1749 | | PHP_FUNCTION(fgetcsv) |
1750 | 0 | { |
1751 | 0 | char delimiter = ','; /* allow this to be set as parameter */ |
1752 | 0 | char enclosure = '"'; /* allow this to be set as parameter */ |
1753 | |
|
1754 | 0 | zend_long len = 0; |
1755 | 0 | size_t buf_len; |
1756 | 0 | char *buf; |
1757 | 0 | php_stream *stream; |
1758 | |
|
1759 | 0 | bool len_is_null = 1; |
1760 | 0 | char *delimiter_str = NULL; |
1761 | 0 | size_t delimiter_str_len = 0; |
1762 | 0 | char *enclosure_str = NULL; |
1763 | 0 | size_t enclosure_str_len = 0; |
1764 | 0 | zend_string *escape_str = NULL; |
1765 | |
|
1766 | 0 | ZEND_PARSE_PARAMETERS_START(1, 5) |
1767 | 0 | PHP_Z_PARAM_STREAM(stream) |
1768 | 0 | Z_PARAM_OPTIONAL |
1769 | 0 | Z_PARAM_LONG_OR_NULL(len, len_is_null) |
1770 | 0 | Z_PARAM_STRING(delimiter_str, delimiter_str_len) |
1771 | 0 | Z_PARAM_STRING(enclosure_str, enclosure_str_len) |
1772 | 0 | Z_PARAM_STR(escape_str) |
1773 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1774 | | |
1775 | 0 | if (delimiter_str != NULL) { |
1776 | | /* Make sure that there is at least one character in string */ |
1777 | 0 | if (delimiter_str_len != 1) { |
1778 | 0 | zend_argument_value_error(3, "must be a single character"); |
1779 | 0 | RETURN_THROWS(); |
1780 | 0 | } |
1781 | | |
1782 | | /* use first character from string */ |
1783 | 0 | delimiter = delimiter_str[0]; |
1784 | 0 | } |
1785 | | |
1786 | 0 | if (enclosure_str != NULL) { |
1787 | 0 | if (enclosure_str_len != 1) { |
1788 | 0 | zend_argument_value_error(4, "must be a single character"); |
1789 | 0 | RETURN_THROWS(); |
1790 | 0 | } |
1791 | | |
1792 | | /* use first character from string */ |
1793 | 0 | enclosure = enclosure_str[0]; |
1794 | 0 | } |
1795 | | |
1796 | 0 | int escape_char = php_csv_handle_escape_argument(escape_str, 5); |
1797 | 0 | if (escape_char == PHP_CSV_ESCAPE_ERROR) { |
1798 | 0 | RETURN_THROWS(); |
1799 | 0 | } |
1800 | | |
1801 | 0 | if (len_is_null || len == 0) { |
1802 | 0 | len = -1; |
1803 | 0 | } else if (len < 0 || len > (ZEND_LONG_MAX - 1)) { |
1804 | 0 | zend_argument_value_error(2, "must be between 0 and " ZEND_LONG_FMT, (ZEND_LONG_MAX - 1)); |
1805 | 0 | RETURN_THROWS(); |
1806 | 0 | } |
1807 | | |
1808 | 0 | if (len < 0) { |
1809 | 0 | if ((buf = php_stream_get_line(stream, NULL, 0, &buf_len)) == NULL) { |
1810 | 0 | RETURN_FALSE; |
1811 | 0 | } |
1812 | 0 | } else { |
1813 | 0 | buf = emalloc(len + 1); |
1814 | 0 | if (php_stream_get_line(stream, buf, len + 1, &buf_len) == NULL) { |
1815 | 0 | efree(buf); |
1816 | 0 | RETURN_FALSE; |
1817 | 0 | } |
1818 | 0 | } |
1819 | | |
1820 | 0 | HashTable *values = php_fgetcsv(stream, delimiter, enclosure, escape_char, buf_len, buf); |
1821 | 0 | if (values == NULL) { |
1822 | 0 | values = php_bc_fgetcsv_empty_line(); |
1823 | 0 | } |
1824 | 0 | RETURN_ARR(values); |
1825 | 0 | } |
1826 | | /* }}} */ |
1827 | | |
1828 | | PHPAPI HashTable *php_bc_fgetcsv_empty_line(void) |
1829 | 3 | { |
1830 | 3 | HashTable *values = zend_new_array(1); |
1831 | 3 | zval tmp; |
1832 | 3 | ZVAL_NULL(&tmp); |
1833 | 3 | zend_hash_next_index_insert(values, &tmp); |
1834 | 3 | return values; |
1835 | 3 | } |
1836 | | |
1837 | | PHPAPI HashTable *php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf) /* {{{ */ |
1838 | 167 | { |
1839 | 167 | char *temp, *bptr, *line_end, *limit; |
1840 | 167 | size_t temp_len, line_end_len; |
1841 | 167 | int inc_len; |
1842 | 167 | bool first_field = true; |
1843 | | |
1844 | 167 | ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE); |
1845 | | |
1846 | | /* initialize internal state */ |
1847 | 167 | php_mb_reset(); |
1848 | | |
1849 | | /* Now into new section that parses buf for delimiter/enclosure fields */ |
1850 | | |
1851 | | /* Strip trailing space from buf, saving end of line in case required for enclosure field */ |
1852 | | |
1853 | 167 | bptr = buf; |
1854 | 167 | line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len); |
1855 | 167 | line_end_len = buf_len - (size_t)(limit - buf); |
1856 | | |
1857 | | /* reserve workspace for building each individual field */ |
1858 | 167 | temp_len = buf_len; |
1859 | 167 | temp = emalloc(temp_len + line_end_len + 1); |
1860 | | |
1861 | | /* Initialize values HashTable */ |
1862 | 167 | HashTable *values = zend_new_array(0); |
1863 | | |
1864 | | /* Main loop to read CSV fields */ |
1865 | | /* NB this routine will return NULL for a blank line */ |
1866 | 8.59k | do { |
1867 | 8.59k | char *comp_end, *hunk_begin; |
1868 | 8.59k | char *tptr = temp; |
1869 | | |
1870 | 8.59k | inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0); |
1871 | 8.59k | if (inc_len == 1) { |
1872 | 7.63k | char *tmp = bptr; |
1873 | 8.10k | while ((*tmp != delimiter) && isspace((int)*(unsigned char *)tmp)) { |
1874 | 465 | tmp++; |
1875 | 465 | } |
1876 | 7.63k | if (*tmp == enclosure && tmp < limit) { |
1877 | 2.66k | bptr = tmp; |
1878 | 2.66k | } |
1879 | 7.63k | } |
1880 | | |
1881 | 8.59k | if (first_field && bptr == line_end) { |
1882 | 3 | zend_array_destroy(values); |
1883 | 3 | values = NULL; |
1884 | 3 | break; |
1885 | 3 | } |
1886 | 8.58k | first_field = false; |
1887 | | /* 2. Read field, leaving bptr pointing at start of next field */ |
1888 | 8.58k | if (inc_len != 0 && *bptr == enclosure) { |
1889 | 2.66k | int state = 0; |
1890 | | |
1891 | 2.66k | bptr++; /* move on to first character in field */ |
1892 | 2.66k | hunk_begin = bptr; |
1893 | | |
1894 | | /* 2A. handle enclosure delimited field */ |
1895 | 74.6k | for (;;) { |
1896 | 74.6k | switch (inc_len) { |
1897 | 9 | case 0: |
1898 | 9 | switch (state) { |
1899 | 0 | case 2: |
1900 | 0 | tptr = zend_mempcpy(tptr, hunk_begin, (bptr - hunk_begin - 1)); |
1901 | 0 | hunk_begin = bptr; |
1902 | 0 | goto quit_loop_2; |
1903 | | |
1904 | 0 | case 1: |
1905 | 0 | tptr = zend_mempcpy(tptr, hunk_begin, (bptr - hunk_begin)); |
1906 | 0 | hunk_begin = bptr; |
1907 | 0 | ZEND_FALLTHROUGH; |
1908 | |
|
1909 | 9 | case 0: { |
1910 | 9 | if (hunk_begin != line_end) { |
1911 | 0 | tptr = zend_mempcpy(tptr, hunk_begin, (bptr - hunk_begin)); |
1912 | 0 | hunk_begin = bptr; |
1913 | 0 | } |
1914 | | |
1915 | | /* add the embedded line end to the field */ |
1916 | 9 | tptr = zend_mempcpy(tptr, line_end, line_end_len); |
1917 | | |
1918 | | /* nothing can be fetched if stream is NULL (e.g. str_getcsv()) */ |
1919 | 9 | if (stream == NULL) { |
1920 | | /* the enclosure is unterminated */ |
1921 | 9 | if (bptr > limit) { |
1922 | | /* if the line ends with enclosure, we need to go back by |
1923 | | * one character so the \0 character is not copied. */ |
1924 | 0 | if (hunk_begin == bptr) { |
1925 | 0 | --hunk_begin; |
1926 | 0 | } |
1927 | 0 | --bptr; |
1928 | 0 | } |
1929 | 9 | goto quit_loop_2; |
1930 | 9 | } |
1931 | | |
1932 | 0 | size_t new_len; |
1933 | 0 | char *new_buf = php_stream_get_line(stream, NULL, 0, &new_len); |
1934 | 0 | if (!new_buf) { |
1935 | | /* we've got an unterminated enclosure, |
1936 | | * assign all the data from the start of |
1937 | | * the enclosure to end of data to the |
1938 | | * last element */ |
1939 | 0 | if (bptr > limit) { |
1940 | | /* if the line ends with enclosure, we need to go back by |
1941 | | * one character so the \0 character is not copied. */ |
1942 | 0 | if (hunk_begin == bptr) { |
1943 | 0 | --hunk_begin; |
1944 | 0 | } |
1945 | 0 | --bptr; |
1946 | 0 | } |
1947 | 0 | goto quit_loop_2; |
1948 | 0 | } |
1949 | | |
1950 | 0 | temp_len += new_len; |
1951 | 0 | char *new_temp = erealloc(temp, temp_len); |
1952 | 0 | tptr = new_temp + (size_t)(tptr - temp); |
1953 | 0 | temp = new_temp; |
1954 | |
|
1955 | 0 | efree(buf); |
1956 | 0 | buf_len = new_len; |
1957 | 0 | bptr = buf = new_buf; |
1958 | 0 | hunk_begin = buf; |
1959 | |
|
1960 | 0 | line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len); |
1961 | 0 | line_end_len = buf_len - (size_t)(limit - buf); |
1962 | |
|
1963 | 0 | state = 0; |
1964 | 0 | } break; |
1965 | 9 | } |
1966 | 0 | break; |
1967 | | |
1968 | 0 | case -2: |
1969 | 9.20k | case -1: |
1970 | 9.20k | php_mb_reset(); |
1971 | 9.20k | ZEND_FALLTHROUGH; |
1972 | 73.9k | case 1: |
1973 | | /* we need to determine if the enclosure is |
1974 | | * 'real' or is it escaped */ |
1975 | 73.9k | switch (state) { |
1976 | 186 | case 1: /* escaped */ |
1977 | 186 | bptr++; |
1978 | 186 | state = 0; |
1979 | 186 | break; |
1980 | 26.3k | case 2: /* embedded enclosure ? let's check it */ |
1981 | 26.3k | if (*bptr != enclosure) { |
1982 | | /* real enclosure */ |
1983 | 2.07k | tptr = zend_mempcpy(tptr, hunk_begin, bptr - hunk_begin - 1); |
1984 | 2.07k | hunk_begin = bptr; |
1985 | 2.07k | goto quit_loop_2; |
1986 | 2.07k | } |
1987 | 24.2k | tptr = zend_mempcpy(tptr, hunk_begin, bptr - hunk_begin); |
1988 | 24.2k | bptr++; |
1989 | 24.2k | hunk_begin = bptr; |
1990 | 24.2k | state = 0; |
1991 | 24.2k | break; |
1992 | 47.4k | default: |
1993 | 47.4k | if (*bptr == enclosure) { |
1994 | 26.9k | state = 2; |
1995 | 26.9k | } else if (escape_char != PHP_CSV_NO_ESCAPE && *bptr == escape_char) { |
1996 | 255 | state = 1; |
1997 | 255 | } |
1998 | 47.4k | bptr++; |
1999 | 47.4k | break; |
2000 | 73.9k | } |
2001 | 71.8k | break; |
2002 | | |
2003 | 71.8k | default: |
2004 | 642 | switch (state) { |
2005 | 573 | case 2: |
2006 | | /* real enclosure */ |
2007 | 573 | tptr = zend_mempcpy(tptr, hunk_begin, bptr - hunk_begin - 1); |
2008 | 573 | hunk_begin = bptr; |
2009 | 573 | goto quit_loop_2; |
2010 | 69 | case 1: |
2011 | 69 | bptr += inc_len; |
2012 | 69 | tptr = zend_mempcpy(tptr, hunk_begin, bptr - hunk_begin); |
2013 | 69 | hunk_begin = bptr; |
2014 | 69 | state = 0; |
2015 | 69 | break; |
2016 | 0 | default: |
2017 | 0 | bptr += inc_len; |
2018 | 0 | break; |
2019 | 642 | } |
2020 | 69 | break; |
2021 | 74.6k | } |
2022 | 71.9k | inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0); |
2023 | 71.9k | } |
2024 | | |
2025 | 2.66k | quit_loop_2: |
2026 | | /* look up for a delimiter */ |
2027 | 31.7k | for (;;) { |
2028 | 31.7k | switch (inc_len) { |
2029 | 9 | case 0: |
2030 | 9 | goto quit_loop_3; |
2031 | | |
2032 | 0 | case -2: |
2033 | 8.14k | case -1: |
2034 | 8.14k | inc_len = 1; |
2035 | 8.14k | php_mb_reset(); |
2036 | 8.14k | ZEND_FALLTHROUGH; |
2037 | 31.1k | case 1: |
2038 | 31.1k | if (*bptr == delimiter) { |
2039 | 2.65k | goto quit_loop_3; |
2040 | 2.65k | } |
2041 | 28.5k | break; |
2042 | 28.5k | default: |
2043 | 579 | break; |
2044 | 31.7k | } |
2045 | 29.1k | bptr += inc_len; |
2046 | 29.1k | inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0); |
2047 | 29.1k | } |
2048 | | |
2049 | 2.66k | quit_loop_3: |
2050 | 2.66k | tptr = zend_mempcpy(tptr, hunk_begin, bptr - hunk_begin); |
2051 | 2.66k | bptr += inc_len; |
2052 | 2.66k | comp_end = tptr; |
2053 | 5.92k | } else { |
2054 | | /* 2B. Handle non-enclosure field */ |
2055 | | |
2056 | 5.92k | hunk_begin = bptr; |
2057 | | |
2058 | 45.2k | for (;;) { |
2059 | 45.2k | switch (inc_len) { |
2060 | 155 | case 0: |
2061 | 155 | goto quit_loop_4; |
2062 | 3 | case -2: |
2063 | 10.5k | case -1: |
2064 | 10.5k | inc_len = 1; |
2065 | 10.5k | php_mb_reset(); |
2066 | 10.5k | ZEND_FALLTHROUGH; |
2067 | 45.0k | case 1: |
2068 | 45.0k | if (*bptr == delimiter) { |
2069 | 5.77k | goto quit_loop_4; |
2070 | 5.77k | } |
2071 | 39.2k | break; |
2072 | 39.2k | default: |
2073 | 48 | break; |
2074 | 45.2k | } |
2075 | 39.2k | bptr += inc_len; |
2076 | 39.2k | inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0); |
2077 | 39.2k | } |
2078 | 5.92k | quit_loop_4: |
2079 | 5.92k | tptr = zend_mempcpy(tptr, hunk_begin, bptr - hunk_begin); |
2080 | | |
2081 | 5.92k | comp_end = (char *)php_fgetcsv_lookup_trailing_spaces(temp, tptr - temp); |
2082 | 5.92k | if (*bptr == delimiter) { |
2083 | 5.87k | bptr++; |
2084 | 5.87k | } |
2085 | 5.92k | } |
2086 | | |
2087 | | /* 3. Now pass our field back to php */ |
2088 | 8.58k | *comp_end = '\0'; |
2089 | | |
2090 | 8.58k | zval z_tmp; |
2091 | 8.58k | ZVAL_STRINGL(&z_tmp, temp, comp_end - temp); |
2092 | 8.58k | zend_hash_next_index_insert(values, &z_tmp); |
2093 | 8.58k | } while (inc_len > 0); |
2094 | | |
2095 | 167 | efree(temp); |
2096 | 167 | if (stream) { |
2097 | 0 | efree(buf); |
2098 | 0 | } |
2099 | | |
2100 | 167 | return values; |
2101 | 167 | } |
2102 | | /* }}} */ |
2103 | | |
2104 | | /* {{{ Return the resolved path */ |
2105 | | PHP_FUNCTION(realpath) |
2106 | 0 | { |
2107 | 0 | char *filename; |
2108 | 0 | size_t filename_len; |
2109 | 0 | char resolved_path_buff[MAXPATHLEN]; |
2110 | |
|
2111 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
2112 | 0 | Z_PARAM_PATH(filename, filename_len) |
2113 | 0 | ZEND_PARSE_PARAMETERS_END(); |
2114 | | |
2115 | 0 | if (VCWD_REALPATH(filename, resolved_path_buff)) { |
2116 | 0 | if (php_check_open_basedir(resolved_path_buff)) { |
2117 | 0 | RETURN_FALSE; |
2118 | 0 | } |
2119 | | |
2120 | | #ifdef ZTS |
2121 | | if (VCWD_ACCESS(resolved_path_buff, F_OK)) { |
2122 | | RETURN_FALSE; |
2123 | | } |
2124 | | #endif |
2125 | 0 | RETURN_STRING(resolved_path_buff); |
2126 | 0 | } else { |
2127 | 0 | RETURN_FALSE; |
2128 | 0 | } |
2129 | 0 | } |
2130 | | /* }}} */ |
2131 | | |
2132 | | /* See http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2 */ |
2133 | 0 | #define PHP_META_HTML401_CHARS "-_.:" |
2134 | | |
2135 | | /* {{{ php_next_meta_token |
2136 | | Tokenizes an HTML file for get_meta_tags */ |
2137 | | php_meta_tags_token php_next_meta_token(php_meta_tags_data *md) |
2138 | 0 | { |
2139 | 0 | int ch = 0, compliment; |
2140 | 0 | char buff[META_DEF_BUFSIZE + 1]; |
2141 | |
|
2142 | 0 | memset((void *)buff, 0, META_DEF_BUFSIZE + 1); |
2143 | |
|
2144 | 0 | while (md->ulc || (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)))) { |
2145 | 0 | if (php_stream_eof(md->stream)) { |
2146 | 0 | break; |
2147 | 0 | } |
2148 | | |
2149 | 0 | if (md->ulc) { |
2150 | 0 | ch = md->lc; |
2151 | 0 | md->ulc = 0; |
2152 | 0 | } |
2153 | |
|
2154 | 0 | switch (ch) { |
2155 | 0 | case '<': |
2156 | 0 | return TOK_OPENTAG; |
2157 | 0 | break; |
2158 | | |
2159 | 0 | case '>': |
2160 | 0 | return TOK_CLOSETAG; |
2161 | 0 | break; |
2162 | | |
2163 | 0 | case '=': |
2164 | 0 | return TOK_EQUAL; |
2165 | 0 | break; |
2166 | 0 | case '/': |
2167 | 0 | return TOK_SLASH; |
2168 | 0 | break; |
2169 | | |
2170 | 0 | case '\'': |
2171 | 0 | case '"': |
2172 | 0 | compliment = ch; |
2173 | 0 | md->token_len = 0; |
2174 | 0 | while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && ch != compliment && ch != '<' && ch != '>') { |
2175 | 0 | buff[(md->token_len)++] = ch; |
2176 | |
|
2177 | 0 | if (md->token_len == META_DEF_BUFSIZE) { |
2178 | 0 | break; |
2179 | 0 | } |
2180 | 0 | } |
2181 | |
|
2182 | 0 | if (ch == '<' || ch == '>') { |
2183 | | /* Was just an apostrophe */ |
2184 | 0 | md->ulc = 1; |
2185 | 0 | md->lc = ch; |
2186 | 0 | } |
2187 | | |
2188 | | /* We don't need to alloc unless we are in a meta tag */ |
2189 | 0 | if (md->in_meta) { |
2190 | 0 | md->token_data = (char *) emalloc(md->token_len + 1); |
2191 | 0 | memcpy(md->token_data, buff, md->token_len+1); |
2192 | 0 | } |
2193 | |
|
2194 | 0 | return TOK_STRING; |
2195 | 0 | break; |
2196 | | |
2197 | 0 | case '\n': |
2198 | 0 | case '\r': |
2199 | 0 | case '\t': |
2200 | 0 | break; |
2201 | | |
2202 | 0 | case ' ': |
2203 | 0 | return TOK_SPACE; |
2204 | 0 | break; |
2205 | | |
2206 | 0 | default: |
2207 | 0 | if (isalnum(ch)) { |
2208 | 0 | md->token_len = 0; |
2209 | 0 | buff[(md->token_len)++] = ch; |
2210 | 0 | while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && (isalnum(ch) || strchr(PHP_META_HTML401_CHARS, ch))) { |
2211 | 0 | buff[(md->token_len)++] = ch; |
2212 | |
|
2213 | 0 | if (md->token_len == META_DEF_BUFSIZE) { |
2214 | 0 | break; |
2215 | 0 | } |
2216 | 0 | } |
2217 | | |
2218 | | /* This is ugly, but we have to replace ungetc */ |
2219 | 0 | if (!isalpha(ch) && ch != '-') { |
2220 | 0 | md->ulc = 1; |
2221 | 0 | md->lc = ch; |
2222 | 0 | } |
2223 | |
|
2224 | 0 | md->token_data = (char *) emalloc(md->token_len + 1); |
2225 | 0 | memcpy(md->token_data, buff, md->token_len+1); |
2226 | |
|
2227 | 0 | return TOK_ID; |
2228 | 0 | } else { |
2229 | 0 | return TOK_OTHER; |
2230 | 0 | } |
2231 | 0 | break; |
2232 | 0 | } |
2233 | 0 | } |
2234 | | |
2235 | 0 | return TOK_EOF; |
2236 | 0 | } |
2237 | | /* }}} */ |
2238 | | |
2239 | | #ifdef HAVE_FNMATCH |
2240 | | /* {{{ Match filename against pattern */ |
2241 | | PHP_FUNCTION(fnmatch) |
2242 | 0 | { |
2243 | 0 | char *pattern, *filename; |
2244 | 0 | size_t pattern_len, filename_len; |
2245 | 0 | zend_long flags = 0; |
2246 | |
|
2247 | 0 | ZEND_PARSE_PARAMETERS_START(2, 3) |
2248 | 0 | Z_PARAM_PATH(pattern, pattern_len) |
2249 | 0 | Z_PARAM_PATH(filename, filename_len) |
2250 | 0 | Z_PARAM_OPTIONAL |
2251 | 0 | Z_PARAM_LONG(flags) |
2252 | 0 | ZEND_PARSE_PARAMETERS_END(); |
2253 | | |
2254 | 0 | if (filename_len >= MAXPATHLEN) { |
2255 | 0 | php_error_docref(NULL, E_WARNING, "Filename exceeds the maximum allowed length of %d characters", MAXPATHLEN); |
2256 | 0 | RETURN_FALSE; |
2257 | 0 | } |
2258 | 0 | if (pattern_len >= MAXPATHLEN) { |
2259 | 0 | php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN); |
2260 | 0 | RETURN_FALSE; |
2261 | 0 | } |
2262 | | |
2263 | 0 | RETURN_BOOL( ! fnmatch( pattern, filename, (int)flags )); |
2264 | 0 | } |
2265 | | /* }}} */ |
2266 | | #endif |
2267 | | |
2268 | | /* {{{ Returns directory path used for temporary files */ |
2269 | | PHP_FUNCTION(sys_get_temp_dir) |
2270 | 0 | { |
2271 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
2272 | | |
2273 | 0 | RETURN_STRING((char *)php_get_temporary_directory()); |
2274 | 0 | } |
2275 | | /* }}} */ |