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