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