1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Rasmus Lerdorf <rasmus@php.net> |
16 | Stig Bakken <ssb@php.net> |
17 | Andi Gutmans <andi@php.net> |
18 | Zeev Suraski <zeev@php.net> |
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 /* {{{ includes */
25
26 #include "php.h"
27 #include "php_globals.h"
28 #include "ext/standard/flock_compat.h"
29 #include "ext/standard/exec.h"
30 #include "ext/standard/php_filestat.h"
31 #include "php_open_temporary_file.h"
32 #include "ext/standard/basic_functions.h"
33 #include "php_ini.h"
34 #include "zend_smart_str.h"
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42
43 #ifdef PHP_WIN32
44 # include <io.h>
45 # define O_RDONLY _O_RDONLY
46 # include "win32/param.h"
47 # include "win32/winutil.h"
48 # include "win32/fnmatch.h"
49 # include "win32/ioutil.h"
50 #else
51 # if HAVE_SYS_PARAM_H
52 # include <sys/param.h>
53 # endif
54 # if HAVE_SYS_SELECT_H
55 # include <sys/select.h>
56 # endif
57 # include <sys/socket.h>
58 # include <netinet/in.h>
59 # include <netdb.h>
60 # if HAVE_ARPA_INET_H
61 # include <arpa/inet.h>
62 # endif
63 #endif
64
65 #include "ext/standard/head.h"
66 #include "php_string.h"
67 #include "file.h"
68
69 #if HAVE_PWD_H
70 # ifdef PHP_WIN32
71 # include "win32/pwd.h"
72 # else
73 # include <pwd.h>
74 # endif
75 #endif
76
77 #ifdef HAVE_SYS_TIME_H
78 # include <sys/time.h>
79 #endif
80
81 #include "fsock.h"
82 #include "fopen_wrappers.h"
83 #include "streamsfuncs.h"
84 #include "php_globals.h"
85
86 #ifdef HAVE_SYS_FILE_H
87 # include <sys/file.h>
88 #endif
89
90 #if MISSING_FCLOSE_DECL
91 extern int fclose(FILE *);
92 #endif
93
94 #ifdef HAVE_SYS_MMAN_H
95 # include <sys/mman.h>
96 #endif
97
98 #include "scanf.h"
99 #include "zend_API.h"
100
101 #ifdef ZTS
102 int file_globals_id;
103 #else
104 php_file_globals file_globals;
105 #endif
106
107 #if defined(HAVE_FNMATCH) && !defined(PHP_WIN32)
108 # ifndef _GNU_SOURCE
109 # define _GNU_SOURCE
110 # endif
111 # include <fnmatch.h>
112 #endif
113
114 #ifdef HAVE_WCHAR_H
115 # include <wchar.h>
116 #endif
117
118 /* }}} */
119
120 #define PHP_STREAM_TO_ZVAL(stream, arg) \
121 ZEND_ASSERT(Z_TYPE_P(arg) == IS_RESOURCE); \
122 php_stream_from_res(stream, Z_RES_P(arg));
123
124 /* {{{ ZTS-stuff / Globals / Prototypes */
125
126 /* sharing globals is *evil* */
127 static int le_stream_context = FAILURE;
128
php_le_stream_context(void)129 PHPAPI int php_le_stream_context(void)
130 {
131 return le_stream_context;
132 }
133 /* }}} */
134
135 /* {{{ Module-Stuff
136 */
ZEND_RSRC_DTOR_FUNC(file_context_dtor)137 static ZEND_RSRC_DTOR_FUNC(file_context_dtor)
138 {
139 php_stream_context *context = (php_stream_context*)res->ptr;
140 if (Z_TYPE(context->options) != IS_UNDEF) {
141 zval_ptr_dtor(&context->options);
142 ZVAL_UNDEF(&context->options);
143 }
144 php_stream_context_free(context);
145 }
146
file_globals_ctor(php_file_globals * file_globals_p)147 static void file_globals_ctor(php_file_globals *file_globals_p)
148 {
149 memset(file_globals_p, 0, sizeof(php_file_globals));
150 file_globals_p->def_chunk_size = PHP_SOCK_CHUNK_SIZE;
151 }
152
file_globals_dtor(php_file_globals * file_globals_p)153 static void file_globals_dtor(php_file_globals *file_globals_p)
154 {
155 #if defined(HAVE_GETHOSTBYNAME_R)
156 if (file_globals_p->tmp_host_buf) {
157 free(file_globals_p->tmp_host_buf);
158 }
159 #endif
160 }
161
162 PHP_INI_BEGIN()
163 STD_PHP_INI_ENTRY("user_agent", NULL, PHP_INI_ALL, OnUpdateString, user_agent, php_file_globals, file_globals)
164 STD_PHP_INI_ENTRY("from", NULL, PHP_INI_ALL, OnUpdateString, from_address, php_file_globals, file_globals)
165 STD_PHP_INI_ENTRY("default_socket_timeout", "60", PHP_INI_ALL, OnUpdateLong, default_socket_timeout, php_file_globals, file_globals)
166 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()167 PHP_INI_END()
168
169 PHP_MINIT_FUNCTION(file)
170 {
171 le_stream_context = zend_register_list_destructors_ex(file_context_dtor, NULL, "stream-context", module_number);
172
173 #ifdef ZTS
174 ts_allocate_id(&file_globals_id, sizeof(php_file_globals), (ts_allocate_ctor) file_globals_ctor, (ts_allocate_dtor) file_globals_dtor);
175 #else
176 file_globals_ctor(&file_globals);
177 #endif
178
179 REGISTER_INI_ENTRIES();
180
181 REGISTER_LONG_CONSTANT("SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
182 REGISTER_LONG_CONSTANT("SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
183 REGISTER_LONG_CONSTANT("SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
184 REGISTER_LONG_CONSTANT("LOCK_SH", PHP_LOCK_SH, CONST_CS | CONST_PERSISTENT);
185 REGISTER_LONG_CONSTANT("LOCK_EX", PHP_LOCK_EX, CONST_CS | CONST_PERSISTENT);
186 REGISTER_LONG_CONSTANT("LOCK_UN", PHP_LOCK_UN, CONST_CS | CONST_PERSISTENT);
187 REGISTER_LONG_CONSTANT("LOCK_NB", PHP_LOCK_NB, CONST_CS | CONST_PERSISTENT);
188
189 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_CONNECT", PHP_STREAM_NOTIFY_CONNECT, CONST_CS | CONST_PERSISTENT);
190 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_REQUIRED", PHP_STREAM_NOTIFY_AUTH_REQUIRED, CONST_CS | CONST_PERSISTENT);
191 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_RESULT", PHP_STREAM_NOTIFY_AUTH_RESULT, CONST_CS | CONST_PERSISTENT);
192 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_MIME_TYPE_IS", PHP_STREAM_NOTIFY_MIME_TYPE_IS, CONST_CS | CONST_PERSISTENT);
193 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FILE_SIZE_IS", PHP_STREAM_NOTIFY_FILE_SIZE_IS, CONST_CS | CONST_PERSISTENT);
194 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_REDIRECTED", PHP_STREAM_NOTIFY_REDIRECTED, CONST_CS | CONST_PERSISTENT);
195 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_PROGRESS", PHP_STREAM_NOTIFY_PROGRESS, CONST_CS | CONST_PERSISTENT);
196 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FAILURE", PHP_STREAM_NOTIFY_FAILURE, CONST_CS | CONST_PERSISTENT);
197 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_COMPLETED", PHP_STREAM_NOTIFY_COMPLETED, CONST_CS | CONST_PERSISTENT);
198 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_RESOLVE", PHP_STREAM_NOTIFY_RESOLVE, CONST_CS | CONST_PERSISTENT);
199
200 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_INFO", PHP_STREAM_NOTIFY_SEVERITY_INFO, CONST_CS | CONST_PERSISTENT);
201 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_WARN", PHP_STREAM_NOTIFY_SEVERITY_WARN, CONST_CS | CONST_PERSISTENT);
202 REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_ERR", PHP_STREAM_NOTIFY_SEVERITY_ERR, CONST_CS | CONST_PERSISTENT);
203
204 REGISTER_LONG_CONSTANT("STREAM_FILTER_READ", PHP_STREAM_FILTER_READ, CONST_CS | CONST_PERSISTENT);
205 REGISTER_LONG_CONSTANT("STREAM_FILTER_WRITE", PHP_STREAM_FILTER_WRITE, CONST_CS | CONST_PERSISTENT);
206 REGISTER_LONG_CONSTANT("STREAM_FILTER_ALL", PHP_STREAM_FILTER_ALL, CONST_CS | CONST_PERSISTENT);
207
208 REGISTER_LONG_CONSTANT("STREAM_CLIENT_PERSISTENT", PHP_STREAM_CLIENT_PERSISTENT, CONST_CS | CONST_PERSISTENT);
209 REGISTER_LONG_CONSTANT("STREAM_CLIENT_ASYNC_CONNECT", PHP_STREAM_CLIENT_ASYNC_CONNECT, CONST_CS | CONST_PERSISTENT);
210 REGISTER_LONG_CONSTANT("STREAM_CLIENT_CONNECT", PHP_STREAM_CLIENT_CONNECT, CONST_CS | CONST_PERSISTENT);
211
212 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_ANY_CLIENT", STREAM_CRYPTO_METHOD_ANY_CLIENT, CONST_CS|CONST_PERSISTENT);
213 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv2_CLIENT", STREAM_CRYPTO_METHOD_SSLv2_CLIENT, CONST_CS|CONST_PERSISTENT);
214 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv3_CLIENT", STREAM_CRYPTO_METHOD_SSLv3_CLIENT, CONST_CS|CONST_PERSISTENT);
215 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_CLIENT", STREAM_CRYPTO_METHOD_SSLv23_CLIENT, CONST_CS|CONST_PERSISTENT);
216 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_CLIENT", STREAM_CRYPTO_METHOD_TLS_CLIENT, CONST_CS|CONST_PERSISTENT);
217 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT, CONST_CS|CONST_PERSISTENT);
218 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, CONST_CS|CONST_PERSISTENT);
219 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, CONST_CS|CONST_PERSISTENT);
220 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT, CONST_CS|CONST_PERSISTENT);
221 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_ANY_SERVER", STREAM_CRYPTO_METHOD_ANY_SERVER, CONST_CS|CONST_PERSISTENT);
222 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv2_SERVER", STREAM_CRYPTO_METHOD_SSLv2_SERVER, CONST_CS|CONST_PERSISTENT);
223 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv3_SERVER", STREAM_CRYPTO_METHOD_SSLv3_SERVER, CONST_CS|CONST_PERSISTENT);
224 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_SERVER", STREAM_CRYPTO_METHOD_SSLv23_SERVER, CONST_CS|CONST_PERSISTENT);
225 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_SERVER", STREAM_CRYPTO_METHOD_TLS_SERVER, CONST_CS|CONST_PERSISTENT);
226 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_0_SERVER", STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, CONST_CS|CONST_PERSISTENT);
227 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_1_SERVER", STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, CONST_CS|CONST_PERSISTENT);
228 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_2_SERVER", STREAM_CRYPTO_METHOD_TLSv1_2_SERVER, CONST_CS|CONST_PERSISTENT);
229 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_3_SERVER", STREAM_CRYPTO_METHOD_TLSv1_3_SERVER, CONST_CS|CONST_PERSISTENT);
230
231 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_SSLv3", STREAM_CRYPTO_METHOD_SSLv3_SERVER, CONST_CS|CONST_PERSISTENT);
232 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_0", STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, CONST_CS|CONST_PERSISTENT);
233 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_1", STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, CONST_CS|CONST_PERSISTENT);
234 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_2", STREAM_CRYPTO_METHOD_TLSv1_2_SERVER, CONST_CS|CONST_PERSISTENT);
235 REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_3", STREAM_CRYPTO_METHOD_TLSv1_3_SERVER, CONST_CS|CONST_PERSISTENT);
236
237 REGISTER_LONG_CONSTANT("STREAM_SHUT_RD", STREAM_SHUT_RD, CONST_CS|CONST_PERSISTENT);
238 REGISTER_LONG_CONSTANT("STREAM_SHUT_WR", STREAM_SHUT_WR, CONST_CS|CONST_PERSISTENT);
239 REGISTER_LONG_CONSTANT("STREAM_SHUT_RDWR", STREAM_SHUT_RDWR, CONST_CS|CONST_PERSISTENT);
240
241 #ifdef PF_INET
242 REGISTER_LONG_CONSTANT("STREAM_PF_INET", PF_INET, CONST_CS|CONST_PERSISTENT);
243 #elif defined(AF_INET)
244 REGISTER_LONG_CONSTANT("STREAM_PF_INET", AF_INET, CONST_CS|CONST_PERSISTENT);
245 #endif
246
247 #if HAVE_IPV6
248 # ifdef PF_INET6
249 REGISTER_LONG_CONSTANT("STREAM_PF_INET6", PF_INET6, CONST_CS|CONST_PERSISTENT);
250 # elif defined(AF_INET6)
251 REGISTER_LONG_CONSTANT("STREAM_PF_INET6", AF_INET6, CONST_CS|CONST_PERSISTENT);
252 # endif
253 #endif
254
255 #ifdef PF_UNIX
256 REGISTER_LONG_CONSTANT("STREAM_PF_UNIX", PF_UNIX, CONST_CS|CONST_PERSISTENT);
257 #elif defined(AF_UNIX)
258 REGISTER_LONG_CONSTANT("STREAM_PF_UNIX", AF_UNIX, CONST_CS|CONST_PERSISTENT);
259 #endif
260
261 #ifdef IPPROTO_IP
262 /* most people will use this one when calling socket() or socketpair() */
263 REGISTER_LONG_CONSTANT("STREAM_IPPROTO_IP", IPPROTO_IP, CONST_CS|CONST_PERSISTENT);
264 #endif
265
266 #if defined(IPPROTO_TCP) || defined(PHP_WIN32)
267 REGISTER_LONG_CONSTANT("STREAM_IPPROTO_TCP", IPPROTO_TCP, CONST_CS|CONST_PERSISTENT);
268 #endif
269
270 #if defined(IPPROTO_UDP) || defined(PHP_WIN32)
271 REGISTER_LONG_CONSTANT("STREAM_IPPROTO_UDP", IPPROTO_UDP, CONST_CS|CONST_PERSISTENT);
272 #endif
273
274 #if defined(IPPROTO_ICMP) || defined(PHP_WIN32)
275 REGISTER_LONG_CONSTANT("STREAM_IPPROTO_ICMP", IPPROTO_ICMP, CONST_CS|CONST_PERSISTENT);
276 #endif
277
278 #if defined(IPPROTO_RAW) || defined(PHP_WIN32)
279 REGISTER_LONG_CONSTANT("STREAM_IPPROTO_RAW", IPPROTO_RAW, CONST_CS|CONST_PERSISTENT);
280 #endif
281
282 REGISTER_LONG_CONSTANT("STREAM_SOCK_STREAM", SOCK_STREAM, CONST_CS|CONST_PERSISTENT);
283 REGISTER_LONG_CONSTANT("STREAM_SOCK_DGRAM", SOCK_DGRAM, CONST_CS|CONST_PERSISTENT);
284
285 #ifdef SOCK_RAW
286 REGISTER_LONG_CONSTANT("STREAM_SOCK_RAW", SOCK_RAW, CONST_CS|CONST_PERSISTENT);
287 #endif
288
289 #ifdef SOCK_SEQPACKET
290 REGISTER_LONG_CONSTANT("STREAM_SOCK_SEQPACKET", SOCK_SEQPACKET, CONST_CS|CONST_PERSISTENT);
291 #endif
292
293 #ifdef SOCK_RDM
294 REGISTER_LONG_CONSTANT("STREAM_SOCK_RDM", SOCK_RDM, CONST_CS|CONST_PERSISTENT);
295 #endif
296
297 REGISTER_LONG_CONSTANT("STREAM_PEEK", STREAM_PEEK, CONST_CS | CONST_PERSISTENT);
298 REGISTER_LONG_CONSTANT("STREAM_OOB", STREAM_OOB, CONST_CS | CONST_PERSISTENT);
299
300 REGISTER_LONG_CONSTANT("STREAM_SERVER_BIND", STREAM_XPORT_BIND, CONST_CS | CONST_PERSISTENT);
301 REGISTER_LONG_CONSTANT("STREAM_SERVER_LISTEN", STREAM_XPORT_LISTEN, CONST_CS | CONST_PERSISTENT);
302
303 REGISTER_LONG_CONSTANT("FILE_USE_INCLUDE_PATH", PHP_FILE_USE_INCLUDE_PATH, CONST_CS | CONST_PERSISTENT);
304 REGISTER_LONG_CONSTANT("FILE_IGNORE_NEW_LINES", PHP_FILE_IGNORE_NEW_LINES, CONST_CS | CONST_PERSISTENT);
305 REGISTER_LONG_CONSTANT("FILE_SKIP_EMPTY_LINES", PHP_FILE_SKIP_EMPTY_LINES, CONST_CS | CONST_PERSISTENT);
306 REGISTER_LONG_CONSTANT("FILE_APPEND", PHP_FILE_APPEND, CONST_CS | CONST_PERSISTENT);
307 REGISTER_LONG_CONSTANT("FILE_NO_DEFAULT_CONTEXT", PHP_FILE_NO_DEFAULT_CONTEXT, CONST_CS | CONST_PERSISTENT);
308
309 REGISTER_LONG_CONSTANT("FILE_TEXT", 0, CONST_CS | CONST_PERSISTENT);
310 REGISTER_LONG_CONSTANT("FILE_BINARY", 0, CONST_CS | CONST_PERSISTENT);
311
312 #ifdef HAVE_FNMATCH
313 REGISTER_LONG_CONSTANT("FNM_NOESCAPE", FNM_NOESCAPE, CONST_CS | CONST_PERSISTENT);
314 REGISTER_LONG_CONSTANT("FNM_PATHNAME", FNM_PATHNAME, CONST_CS | CONST_PERSISTENT);
315 REGISTER_LONG_CONSTANT("FNM_PERIOD", FNM_PERIOD, CONST_CS | CONST_PERSISTENT);
316 # ifdef FNM_CASEFOLD /* a GNU extension */ /* TODO emulate if not available */
317 REGISTER_LONG_CONSTANT("FNM_CASEFOLD", FNM_CASEFOLD, CONST_CS | CONST_PERSISTENT);
318 # endif
319 #endif
320
321 return SUCCESS;
322 }
323 /* }}} */
324
PHP_MSHUTDOWN_FUNCTION(file)325 PHP_MSHUTDOWN_FUNCTION(file) /* {{{ */
326 {
327 #ifndef ZTS
328 file_globals_dtor(&file_globals);
329 #endif
330 return SUCCESS;
331 }
332 /* }}} */
333
334 static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN };
335
336 /* {{{ proto bool flock(resource fp, int operation [, int &wouldblock])
337 Portable file locking */
PHP_FUNCTION(flock)338 PHP_FUNCTION(flock)
339 {
340 zval *res, *wouldblock = NULL;
341 int act;
342 php_stream *stream;
343 zend_long operation = 0;
344
345 ZEND_PARSE_PARAMETERS_START(2, 3)
346 Z_PARAM_RESOURCE(res)
347 Z_PARAM_LONG(operation)
348 Z_PARAM_OPTIONAL
349 Z_PARAM_ZVAL(wouldblock)
350 ZEND_PARSE_PARAMETERS_END();
351
352 PHP_STREAM_TO_ZVAL(stream, res);
353
354 act = operation & 3;
355 if (act < 1 || act > 3) {
356 php_error_docref(NULL, E_WARNING, "Illegal operation argument");
357 RETURN_FALSE;
358 }
359
360 if (wouldblock) {
361 ZEND_TRY_ASSIGN_REF_LONG(wouldblock, 0);
362 }
363
364 /* flock_values contains all possible actions if (operation & 4) we won't block on the lock */
365 act = flock_values[act - 1] | (operation & PHP_LOCK_NB ? LOCK_NB : 0);
366 if (php_stream_lock(stream, act)) {
367 if (operation && errno == EWOULDBLOCK && wouldblock) {
368 ZEND_TRY_ASSIGN_REF_LONG(wouldblock, 1);
369 }
370 RETURN_FALSE;
371 }
372 RETURN_TRUE;
373 }
374 /* }}} */
375
376 #define PHP_META_UNSAFE ".\\+*?[^]$() "
377
378 /* {{{ proto array get_meta_tags(string filename [, bool use_include_path])
379 Extracts all meta tag content attributes from a file and returns an array */
PHP_FUNCTION(get_meta_tags)380 PHP_FUNCTION(get_meta_tags)
381 {
382 char *filename;
383 size_t filename_len;
384 zend_bool use_include_path = 0;
385 int in_tag = 0, done = 0;
386 int looking_for_val = 0, have_name = 0, have_content = 0;
387 int saw_name = 0, saw_content = 0;
388 char *name = NULL, *value = NULL, *temp = NULL;
389 php_meta_tags_token tok, tok_last;
390 php_meta_tags_data md;
391
392 /* Initiailize our structure */
393 memset(&md, 0, sizeof(md));
394
395 /* Parse arguments */
396 ZEND_PARSE_PARAMETERS_START(1, 2)
397 Z_PARAM_PATH(filename, filename_len)
398 Z_PARAM_OPTIONAL
399 Z_PARAM_BOOL(use_include_path)
400 ZEND_PARSE_PARAMETERS_END();
401
402 md.stream = php_stream_open_wrapper(filename, "rb",
403 (use_include_path ? USE_PATH : 0) | REPORT_ERRORS,
404 NULL);
405 if (!md.stream) {
406 RETURN_FALSE;
407 }
408
409 array_init(return_value);
410
411 tok_last = TOK_EOF;
412
413 while (!done && (tok = php_next_meta_token(&md)) != TOK_EOF) {
414 if (tok == TOK_ID) {
415 if (tok_last == TOK_OPENTAG) {
416 md.in_meta = !strcasecmp("meta", md.token_data);
417 } else if (tok_last == TOK_SLASH && in_tag) {
418 if (strcasecmp("head", md.token_data) == 0) {
419 /* We are done here! */
420 done = 1;
421 }
422 } else if (tok_last == TOK_EQUAL && looking_for_val) {
423 if (saw_name) {
424 if (name) efree(name);
425 /* Get the NAME attr (Single word attr, non-quoted) */
426 temp = name = estrndup(md.token_data, md.token_len);
427
428 while (temp && *temp) {
429 if (strchr(PHP_META_UNSAFE, *temp)) {
430 *temp = '_';
431 }
432 temp++;
433 }
434
435 have_name = 1;
436 } else if (saw_content) {
437 if (value) efree(value);
438 value = estrndup(md.token_data, md.token_len);
439 have_content = 1;
440 }
441
442 looking_for_val = 0;
443 } else {
444 if (md.in_meta) {
445 if (strcasecmp("name", md.token_data) == 0) {
446 saw_name = 1;
447 saw_content = 0;
448 looking_for_val = 1;
449 } else if (strcasecmp("content", md.token_data) == 0) {
450 saw_name = 0;
451 saw_content = 1;
452 looking_for_val = 1;
453 }
454 }
455 }
456 } else if (tok == TOK_STRING && tok_last == TOK_EQUAL && looking_for_val) {
457 if (saw_name) {
458 if (name) efree(name);
459 /* Get the NAME attr (Quoted single/double) */
460 temp = name = estrndup(md.token_data, md.token_len);
461
462 while (temp && *temp) {
463 if (strchr(PHP_META_UNSAFE, *temp)) {
464 *temp = '_';
465 }
466 temp++;
467 }
468
469 have_name = 1;
470 } else if (saw_content) {
471 if (value) efree(value);
472 value = estrndup(md.token_data, md.token_len);
473 have_content = 1;
474 }
475
476 looking_for_val = 0;
477 } else if (tok == TOK_OPENTAG) {
478 if (looking_for_val) {
479 looking_for_val = 0;
480 have_name = saw_name = 0;
481 have_content = saw_content = 0;
482 }
483 in_tag = 1;
484 } else if (tok == TOK_CLOSETAG) {
485 if (have_name) {
486 /* For BC */
487 php_strtolower(name, strlen(name));
488 if (have_content) {
489 add_assoc_string(return_value, name, value);
490 } else {
491 add_assoc_string(return_value, name, "");
492 }
493
494 efree(name);
495 if (value) efree(value);
496 } else if (have_content) {
497 efree(value);
498 }
499
500 name = value = NULL;
501
502 /* Reset all of our flags */
503 in_tag = looking_for_val = 0;
504 have_name = saw_name = 0;
505 have_content = saw_content = 0;
506 md.in_meta = 0;
507 }
508
509 tok_last = tok;
510
511 if (md.token_data)
512 efree(md.token_data);
513
514 md.token_data = NULL;
515 }
516
517 if (value) efree(value);
518 if (name) efree(name);
519 php_stream_close(md.stream);
520 }
521 /* }}} */
522
523 /* {{{ proto string file_get_contents(string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]])
524 Read the entire file into a string */
PHP_FUNCTION(file_get_contents)525 PHP_FUNCTION(file_get_contents)
526 {
527 char *filename;
528 size_t filename_len;
529 zend_bool use_include_path = 0;
530 php_stream *stream;
531 zend_long offset = 0;
532 zend_long maxlen = (ssize_t) PHP_STREAM_COPY_ALL;
533 zval *zcontext = NULL;
534 php_stream_context *context = NULL;
535 zend_string *contents;
536
537 /* Parse arguments */
538 ZEND_PARSE_PARAMETERS_START(1, 5)
539 Z_PARAM_PATH(filename, filename_len)
540 Z_PARAM_OPTIONAL
541 Z_PARAM_BOOL(use_include_path)
542 Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
543 Z_PARAM_LONG(offset)
544 Z_PARAM_LONG(maxlen)
545 ZEND_PARSE_PARAMETERS_END();
546
547 if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
548 php_error_docref(NULL, E_WARNING, "length must be greater than or equal to zero");
549 RETURN_FALSE;
550 }
551
552 context = php_stream_context_from_zval(zcontext, 0);
553
554 stream = php_stream_open_wrapper_ex(filename, "rb",
555 (use_include_path ? USE_PATH : 0) | REPORT_ERRORS,
556 NULL, context);
557 if (!stream) {
558 RETURN_FALSE;
559 }
560
561 if (offset != 0 && php_stream_seek(stream, offset, ((offset > 0) ? SEEK_SET : SEEK_END)) < 0) {
562 php_error_docref(NULL, E_WARNING, "Failed to seek to position " ZEND_LONG_FMT " in the stream", offset);
563 php_stream_close(stream);
564 RETURN_FALSE;
565 }
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 ssize_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 %zd 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 ssize_t bytes_written;
669 zval *tmp;
670
671 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), tmp) {
672 zend_string *t;
673 zend_string *str = zval_get_tmp_string(tmp, &t);
674 if (ZSTR_LEN(str)) {
675 numbytes += ZSTR_LEN(str);
676 bytes_written = php_stream_write(stream, ZSTR_VAL(str), ZSTR_LEN(str));
677 if (bytes_written != ZSTR_LEN(str)) {
678 php_error_docref(NULL, E_WARNING, "Failed to write %zd bytes to %s", ZSTR_LEN(str), filename);
679 zend_tmp_string_release(t);
680 numbytes = -1;
681 break;
682 }
683 }
684 zend_tmp_string_release(t);
685 } ZEND_HASH_FOREACH_END();
686 }
687 break;
688
689 case IS_OBJECT:
690 if (Z_OBJ_HT_P(data) != NULL) {
691 zval out;
692
693 if (zend_std_cast_object_tostring(data, &out, IS_STRING) == SUCCESS) {
694 numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRLEN(out));
695 if (numbytes != Z_STRLEN(out)) {
696 php_error_docref(NULL, E_WARNING, "Only %zd of %zd bytes written, possibly out of free disk space", numbytes, Z_STRLEN(out));
697 numbytes = -1;
698 }
699 zval_ptr_dtor_str(&out);
700 break;
701 }
702 }
703 default:
704 numbytes = -1;
705 break;
706 }
707 php_stream_close(stream);
708
709 if (numbytes < 0) {
710 RETURN_FALSE;
711 }
712
713 RETURN_LONG(numbytes);
714 }
715 /* }}} */
716
717 #define PHP_FILE_BUF_SIZE 80
718
719 /* {{{ proto array file(string filename [, int flags[, resource context]])
720 Read entire file into an array */
PHP_FUNCTION(file)721 PHP_FUNCTION(file)
722 {
723 char *filename;
724 size_t filename_len;
725 char *p, *s, *e;
726 register int i = 0;
727 char eol_marker = '\n';
728 zend_long flags = 0;
729 zend_bool use_include_path;
730 zend_bool include_new_line;
731 zend_bool skip_blank_lines;
732 php_stream *stream;
733 zval *zcontext = NULL;
734 php_stream_context *context = NULL;
735 zend_string *target_buf;
736
737 /* Parse arguments */
738 ZEND_PARSE_PARAMETERS_START(1, 3)
739 Z_PARAM_PATH(filename, filename_len)
740 Z_PARAM_OPTIONAL
741 Z_PARAM_LONG(flags)
742 Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
743 ZEND_PARSE_PARAMETERS_END();
744
745 if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
746 php_error_docref(NULL, E_WARNING, "'" ZEND_LONG_FMT "' flag is not supported", flags);
747 RETURN_FALSE;
748 }
749
750 use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH;
751 include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES);
752 skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES;
753
754 context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
755
756 stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
757 if (!stream) {
758 RETURN_FALSE;
759 }
760
761 /* Initialize return array */
762 array_init(return_value);
763
764 if ((target_buf = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0)) != NULL) {
765 s = ZSTR_VAL(target_buf);
766 e = ZSTR_VAL(target_buf) + ZSTR_LEN(target_buf);
767
768 if (!(p = (char*)php_stream_locate_eol(stream, target_buf))) {
769 p = e;
770 goto parse_eol;
771 }
772
773 if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
774 eol_marker = '\r';
775 }
776
777 /* for performance reasons the code is duplicated, so that the if (include_new_line)
778 * will not need to be done for every single line in the file. */
779 if (include_new_line) {
780 do {
781 p++;
782 parse_eol:
783 add_index_stringl(return_value, i++, s, p-s);
784 s = p;
785 } while ((p = memchr(p, eol_marker, (e-p))));
786 } else {
787 do {
788 int windows_eol = 0;
789 if (p != ZSTR_VAL(target_buf) && eol_marker == '\n' && *(p - 1) == '\r') {
790 windows_eol++;
791 }
792 if (skip_blank_lines && !(p-s-windows_eol)) {
793 s = ++p;
794 continue;
795 }
796 add_index_stringl(return_value, i++, s, p-s-windows_eol);
797 s = ++p;
798 } while ((p = memchr(p, eol_marker, (e-p))));
799 }
800
801 /* handle any left overs of files without new lines */
802 if (s != e) {
803 p = e;
804 goto parse_eol;
805 }
806 }
807
808 if (target_buf) {
809 zend_string_free(target_buf);
810 }
811 php_stream_close(stream);
812 }
813 /* }}} */
814
815 /* {{{ proto string tempnam(string dir, string prefix)
816 Create a unique filename in a directory */
PHP_FUNCTION(tempnam)817 PHP_FUNCTION(tempnam)
818 {
819 char *dir, *prefix;
820 size_t dir_len, prefix_len;
821 zend_string *opened_path;
822 int fd;
823 zend_string *p;
824
825 ZEND_PARSE_PARAMETERS_START(2, 2)
826 Z_PARAM_PATH(dir, dir_len)
827 Z_PARAM_PATH(prefix, prefix_len)
828 ZEND_PARSE_PARAMETERS_END();
829
830 p = php_basename(prefix, prefix_len, NULL, 0);
831 if (ZSTR_LEN(p) > 64) {
832 ZSTR_VAL(p)[63] = '\0';
833 }
834
835 RETVAL_FALSE;
836
837 if ((fd = php_open_temporary_fd_ex(dir, ZSTR_VAL(p), &opened_path, PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ALWAYS)) >= 0) {
838 close(fd);
839 RETVAL_STR(opened_path);
840 }
841 zend_string_release_ex(p, 0);
842 }
843 /* }}} */
844
845 /* {{{ proto resource tmpfile(void)
846 Create a temporary file that will be deleted automatically after use */
PHP_NAMED_FUNCTION(php_if_tmpfile)847 PHP_NAMED_FUNCTION(php_if_tmpfile)
848 {
849 php_stream *stream;
850
851 if (zend_parse_parameters_none() == FAILURE) {
852 return;
853 }
854
855 stream = php_stream_fopen_tmpfile();
856
857 if (stream) {
858 php_stream_to_zval(stream, return_value);
859 } else {
860 RETURN_FALSE;
861 }
862 }
863 /* }}} */
864
865 /* {{{ proto resource fopen(string filename, string mode [, bool use_include_path [, resource context]])
866 Open a file or a URL and return a file pointer */
PHP_NAMED_FUNCTION(php_if_fopen)867 PHP_NAMED_FUNCTION(php_if_fopen)
868 {
869 char *filename, *mode;
870 size_t filename_len, mode_len;
871 zend_bool use_include_path = 0;
872 zval *zcontext = NULL;
873 php_stream *stream;
874 php_stream_context *context = NULL;
875
876 ZEND_PARSE_PARAMETERS_START(2, 4)
877 Z_PARAM_PATH(filename, filename_len)
878 Z_PARAM_STRING(mode, mode_len)
879 Z_PARAM_OPTIONAL
880 Z_PARAM_BOOL(use_include_path)
881 Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
882 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
883
884 context = php_stream_context_from_zval(zcontext, 0);
885
886 stream = php_stream_open_wrapper_ex(filename, mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
887
888 if (stream == NULL) {
889 RETURN_FALSE;
890 }
891
892 php_stream_to_zval(stream, return_value);
893 }
894 /* }}} */
895
896 /* {{{ proto bool fclose(resource fp)
897 Close an open file pointer */
PHP_FUNCTION(fclose)898 PHPAPI PHP_FUNCTION(fclose)
899 {
900 zval *res;
901 php_stream *stream;
902
903 ZEND_PARSE_PARAMETERS_START(1, 1)
904 Z_PARAM_RESOURCE(res)
905 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
906
907 PHP_STREAM_TO_ZVAL(stream, res);
908
909 if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) {
910 php_error_docref(NULL, E_WARNING, "%d is not a valid stream resource", stream->res->handle);
911 RETURN_FALSE;
912 }
913
914 php_stream_free(stream,
915 PHP_STREAM_FREE_KEEP_RSRC |
916 (stream->is_persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE));
917
918 RETURN_TRUE;
919 }
920 /* }}} */
921
922 /* {{{ proto resource popen(string command, string mode)
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 }
944 }
945 #endif
946
947 fp = VCWD_POPEN(command, posix_mode);
948 if (!fp) {
949 php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));
950 efree(posix_mode);
951 RETURN_FALSE;
952 }
953
954 stream = php_stream_fopen_from_pipe(fp, mode);
955
956 if (stream == NULL) {
957 php_error_docref2(NULL, command, mode, E_WARNING, "%s", strerror(errno));
958 RETVAL_FALSE;
959 } else {
960 php_stream_to_zval(stream, return_value);
961 }
962
963 efree(posix_mode);
964 }
965 /* }}} */
966
967 /* {{{ proto int pclose(resource fp)
968 Close a file pointer opened by popen() */
PHP_FUNCTION(pclose)969 PHP_FUNCTION(pclose)
970 {
971 zval *res;
972 php_stream *stream;
973
974 ZEND_PARSE_PARAMETERS_START(1, 1)
975 Z_PARAM_RESOURCE(res)
976 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
977
978 PHP_STREAM_TO_ZVAL(stream, res);
979
980 FG(pclose_wait) = 1;
981 zend_list_close(stream->res);
982 FG(pclose_wait) = 0;
983 RETURN_LONG(FG(pclose_ret));
984 }
985 /* }}} */
986
987 /* {{{ proto bool feof(resource fp)
988 Test for end-of-file on a file pointer */
PHP_FUNCTION(feof)989 PHPAPI PHP_FUNCTION(feof)
990 {
991 zval *res;
992 php_stream *stream;
993
994 ZEND_PARSE_PARAMETERS_START(1, 1)
995 Z_PARAM_RESOURCE(res)
996 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
997
998 PHP_STREAM_TO_ZVAL(stream, res);
999
1000 if (php_stream_eof(stream)) {
1001 RETURN_TRUE;
1002 } else {
1003 RETURN_FALSE;
1004 }
1005 }
1006 /* }}} */
1007
1008 /* {{{ proto string fgets(resource fp[, int length])
1009 Get a line from file pointer */
PHP_FUNCTION(fgets)1010 PHPAPI PHP_FUNCTION(fgets)
1011 {
1012 zval *res;
1013 zend_long len = 1024;
1014 char *buf = NULL;
1015 int argc = ZEND_NUM_ARGS();
1016 size_t line_len = 0;
1017 zend_string *str;
1018 php_stream *stream;
1019
1020 ZEND_PARSE_PARAMETERS_START(1, 2)
1021 Z_PARAM_RESOURCE(res)
1022 Z_PARAM_OPTIONAL
1023 Z_PARAM_LONG(len)
1024 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1025
1026 PHP_STREAM_TO_ZVAL(stream, res);
1027
1028 if (argc == 1) {
1029 /* ask streams to give us a buffer of an appropriate size */
1030 buf = php_stream_get_line(stream, NULL, 0, &line_len);
1031 if (buf == NULL) {
1032 RETURN_FALSE;
1033 }
1034 // TODO: avoid reallocation ???
1035 RETVAL_STRINGL(buf, line_len);
1036 efree(buf);
1037 } else if (argc > 1) {
1038 if (len <= 0) {
1039 php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
1040 RETURN_FALSE;
1041 }
1042
1043 str = zend_string_alloc(len, 0);
1044 if (php_stream_get_line(stream, ZSTR_VAL(str), len, &line_len) == NULL) {
1045 zend_string_efree(str);
1046 RETURN_FALSE;
1047 }
1048 /* resize buffer if it's much larger than the result.
1049 * Only needed if the user requested a buffer size. */
1050 if (line_len < (size_t)len / 2) {
1051 str = zend_string_truncate(str, line_len, 0);
1052 } else {
1053 ZSTR_LEN(str) = line_len;
1054 }
1055 RETURN_NEW_STR(str);
1056 }
1057 }
1058 /* }}} */
1059
1060 /* {{{ proto string fgetc(resource fp)
1061 Get a character from file pointer */
PHP_FUNCTION(fgetc)1062 PHPAPI PHP_FUNCTION(fgetc)
1063 {
1064 zval *res;
1065 char buf[2];
1066 int result;
1067 php_stream *stream;
1068
1069 ZEND_PARSE_PARAMETERS_START(1, 1)
1070 Z_PARAM_RESOURCE(res)
1071 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1072
1073 PHP_STREAM_TO_ZVAL(stream, res);
1074
1075 result = php_stream_getc(stream);
1076
1077 if (result == EOF) {
1078 RETVAL_FALSE;
1079 } else {
1080 buf[0] = result;
1081 buf[1] = '\0';
1082
1083 RETURN_STRINGL(buf, 1);
1084 }
1085 }
1086 /* }}} */
1087
1088 /* {{{ proto string fgetss(resource fp [, int length [, string allowable_tags]])
1089 Get a line from file pointer and strip HTML tags */
PHP_FUNCTION(fgetss)1090 PHPAPI PHP_FUNCTION(fgetss)
1091 {
1092 zval *fd;
1093 zend_long bytes = 0;
1094 size_t len = 0;
1095 size_t actual_len, retval_len;
1096 char *buf = NULL, *retval;
1097 php_stream *stream;
1098 char *allowed_tags=NULL;
1099 size_t allowed_tags_len=0;
1100
1101 ZEND_PARSE_PARAMETERS_START(1, 3)
1102 Z_PARAM_RESOURCE(fd)
1103 Z_PARAM_OPTIONAL
1104 Z_PARAM_LONG(bytes)
1105 Z_PARAM_STRING(allowed_tags, allowed_tags_len)
1106 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1107
1108 PHP_STREAM_TO_ZVAL(stream, fd);
1109
1110 if (ZEND_NUM_ARGS() >= 2) {
1111 if (bytes <= 0) {
1112 php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
1113 RETURN_FALSE;
1114 }
1115
1116 len = (size_t) bytes;
1117 buf = safe_emalloc(sizeof(char), (len + 1), 0);
1118 /*needed because recv doesn't set null char at end*/
1119 memset(buf, 0, len + 1);
1120 }
1121
1122 if ((retval = php_stream_get_line(stream, buf, len, &actual_len)) == NULL) {
1123 if (buf != NULL) {
1124 efree(buf);
1125 }
1126 RETURN_FALSE;
1127 }
1128
1129 retval_len = php_strip_tags(retval, actual_len, &stream->fgetss_state, allowed_tags, allowed_tags_len);
1130
1131 // TODO: avoid reallocation ???
1132 RETVAL_STRINGL(retval, retval_len);
1133 efree(retval);
1134 }
1135 /* }}} */
1136
1137 /* {{{ proto mixed fscanf(resource stream, string format [, string ...])
1138 Implements a mostly ANSI compatible fscanf() */
PHP_FUNCTION(fscanf)1139 PHP_FUNCTION(fscanf)
1140 {
1141 int result, argc = 0;
1142 size_t format_len;
1143 zval *args = NULL;
1144 zval *file_handle;
1145 char *buf, *format;
1146 size_t len;
1147 void *what;
1148
1149 ZEND_PARSE_PARAMETERS_START(2, -1)
1150 Z_PARAM_RESOURCE(file_handle)
1151 Z_PARAM_STRING(format, format_len)
1152 Z_PARAM_VARIADIC('*', args, argc)
1153 ZEND_PARSE_PARAMETERS_END();
1154
1155 what = zend_fetch_resource2(Z_RES_P(file_handle), "File-Handle", php_file_le_stream(), php_file_le_pstream());
1156
1157 /* we can't do a ZEND_VERIFY_RESOURCE(what), otherwise we end up
1158 * with a leak if we have an invalid filehandle. This needs changing
1159 * if the code behind ZEND_VERIFY_RESOURCE changed. - cc */
1160 if (!what) {
1161 RETURN_FALSE;
1162 }
1163
1164 buf = php_stream_get_line((php_stream *) what, NULL, 0, &len);
1165 if (buf == NULL) {
1166 RETURN_FALSE;
1167 }
1168
1169 result = php_sscanf_internal(buf, format, argc, args, 0, return_value);
1170
1171 efree(buf);
1172
1173 if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
1174 WRONG_PARAM_COUNT;
1175 }
1176 }
1177 /* }}} */
1178
1179 /* {{{ proto int|false fwrite(resource fp, string str [, int length])
1180 Binary-safe file write */
PHP_FUNCTION(fwrite)1181 PHPAPI PHP_FUNCTION(fwrite)
1182 {
1183 zval *res;
1184 char *input;
1185 size_t inputlen;
1186 ssize_t ret;
1187 size_t num_bytes;
1188 zend_long maxlen = 0;
1189 php_stream *stream;
1190
1191 ZEND_PARSE_PARAMETERS_START(2, 3)
1192 Z_PARAM_RESOURCE(res)
1193 Z_PARAM_STRING(input, inputlen)
1194 Z_PARAM_OPTIONAL
1195 Z_PARAM_LONG(maxlen)
1196 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1197
1198 if (ZEND_NUM_ARGS() == 2) {
1199 num_bytes = inputlen;
1200 } else if (maxlen <= 0) {
1201 num_bytes = 0;
1202 } else {
1203 num_bytes = MIN((size_t) maxlen, inputlen);
1204 }
1205
1206 if (!num_bytes) {
1207 RETURN_LONG(0);
1208 }
1209
1210 PHP_STREAM_TO_ZVAL(stream, res);
1211
1212 ret = php_stream_write(stream, input, num_bytes);
1213 if (ret < 0) {
1214 RETURN_FALSE;
1215 }
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_EX(zcontext, 1, 0)
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_EX(zcontext, 1, 0)
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_EX(zcontext, 1, 0)
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_EX(zcontext, 1, 0)
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_STRUCT_STAT_ST_RDEV
1591 ZVAL_LONG(&stat_rdev, stat_ssb.sb.st_rdev);
1592 #else
1593 ZVAL_LONG(&stat_rdev, -1);
1594 #endif
1595 ZVAL_LONG(&stat_size, stat_ssb.sb.st_size);
1596 ZVAL_LONG(&stat_atime, stat_ssb.sb.st_atime);
1597 ZVAL_LONG(&stat_mtime, stat_ssb.sb.st_mtime);
1598 ZVAL_LONG(&stat_ctime, stat_ssb.sb.st_ctime);
1599 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1600 ZVAL_LONG(&stat_blksize, stat_ssb.sb.st_blksize);
1601 #else
1602 ZVAL_LONG(&stat_blksize,-1);
1603 #endif
1604 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1605 ZVAL_LONG(&stat_blocks, stat_ssb.sb.st_blocks);
1606 #else
1607 ZVAL_LONG(&stat_blocks,-1);
1608 #endif
1609 /* Store numeric indexes in proper order */
1610 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_dev);
1611 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ino);
1612 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mode);
1613 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_nlink);
1614 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_uid);
1615 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_gid);
1616 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_rdev);
1617 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_size);
1618 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_atime);
1619 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mtime);
1620 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ctime);
1621 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blksize);
1622 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blocks);
1623
1624 /* Store string indexes referencing the same zval*/
1625 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[0], strlen(stat_sb_names[0]), &stat_dev);
1626 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[1], strlen(stat_sb_names[1]), &stat_ino);
1627 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[2], strlen(stat_sb_names[2]), &stat_mode);
1628 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[3], strlen(stat_sb_names[3]), &stat_nlink);
1629 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[4], strlen(stat_sb_names[4]), &stat_uid);
1630 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[5], strlen(stat_sb_names[5]), &stat_gid);
1631 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[6], strlen(stat_sb_names[6]), &stat_rdev);
1632 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[7], strlen(stat_sb_names[7]), &stat_size);
1633 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[8], strlen(stat_sb_names[8]), &stat_atime);
1634 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[9], strlen(stat_sb_names[9]), &stat_mtime);
1635 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[10], strlen(stat_sb_names[10]), &stat_ctime);
1636 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize);
1637 zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks);
1638 }
1639 /* }}} */
1640
1641 /* {{{ proto bool copy(string source_file, string destination_file [, resource context])
1642 Copy a file */
PHP_FUNCTION(copy)1643 PHP_FUNCTION(copy)
1644 {
1645 char *source, *target;
1646 size_t source_len, target_len;
1647 zval *zcontext = NULL;
1648 php_stream_context *context;
1649
1650 ZEND_PARSE_PARAMETERS_START(2, 3)
1651 Z_PARAM_PATH(source, source_len)
1652 Z_PARAM_PATH(target, target_len)
1653 Z_PARAM_OPTIONAL
1654 Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
1655 ZEND_PARSE_PARAMETERS_END();
1656
1657 if (php_stream_locate_url_wrapper(source, NULL, 0) == &php_plain_files_wrapper && php_check_open_basedir(source)) {
1658 RETURN_FALSE;
1659 }
1660
1661 context = php_stream_context_from_zval(zcontext, 0);
1662
1663 if (php_copy_file_ctx(source, target, 0, context) == SUCCESS) {
1664 RETURN_TRUE;
1665 } else {
1666 RETURN_FALSE;
1667 }
1668 }
1669 /* }}} */
1670
1671 /* {{{ php_copy_file
1672 */
php_copy_file(const char * src,const char * dest)1673 PHPAPI int php_copy_file(const char *src, const char *dest)
1674 {
1675 return php_copy_file_ctx(src, dest, 0, NULL);
1676 }
1677 /* }}} */
1678
1679 /* {{{ php_copy_file_ex
1680 */
php_copy_file_ex(const char * src,const char * dest,int src_flg)1681 PHPAPI int php_copy_file_ex(const char *src, const char *dest, int src_flg)
1682 {
1683 return php_copy_file_ctx(src, dest, src_flg, NULL);
1684 }
1685 /* }}} */
1686
1687 /* {{{ php_copy_file_ctx
1688 */
php_copy_file_ctx(const char * src,const char * dest,int src_flg,php_stream_context * ctx)1689 PHPAPI int php_copy_file_ctx(const char *src, const char *dest, int src_flg, php_stream_context *ctx)
1690 {
1691 php_stream *srcstream = NULL, *deststream = NULL;
1692 int ret = FAILURE;
1693 php_stream_statbuf src_s, dest_s;
1694
1695 switch (php_stream_stat_path_ex(src, 0, &src_s, ctx)) {
1696 case -1:
1697 /* non-statable stream */
1698 goto safe_to_copy;
1699 break;
1700 case 0:
1701 break;
1702 default: /* failed to stat file, does not exist? */
1703 return ret;
1704 }
1705 if (S_ISDIR(src_s.sb.st_mode)) {
1706 php_error_docref(NULL, E_WARNING, "The first argument to copy() function cannot be a directory");
1707 return FAILURE;
1708 }
1709
1710 switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET | PHP_STREAM_URL_STAT_NOCACHE, &dest_s, ctx)) {
1711 case -1:
1712 /* non-statable stream */
1713 goto safe_to_copy;
1714 break;
1715 case 0:
1716 break;
1717 default: /* failed to stat file, does not exist? */
1718 return ret;
1719 }
1720 if (S_ISDIR(dest_s.sb.st_mode)) {
1721 php_error_docref(NULL, E_WARNING, "The second argument to copy() function cannot be a directory");
1722 return FAILURE;
1723 }
1724 if (!src_s.sb.st_ino || !dest_s.sb.st_ino) {
1725 goto no_stat;
1726 }
1727 if (src_s.sb.st_ino == dest_s.sb.st_ino && src_s.sb.st_dev == dest_s.sb.st_dev) {
1728 return ret;
1729 } else {
1730 goto safe_to_copy;
1731 }
1732 no_stat:
1733 {
1734 char *sp, *dp;
1735 int res;
1736
1737 if ((sp = expand_filepath(src, NULL)) == NULL) {
1738 return ret;
1739 }
1740 if ((dp = expand_filepath(dest, NULL)) == NULL) {
1741 efree(sp);
1742 goto safe_to_copy;
1743 }
1744
1745 res =
1746 #ifndef PHP_WIN32
1747 !strcmp(sp, dp);
1748 #else
1749 !strcasecmp(sp, dp);
1750 #endif
1751
1752 efree(sp);
1753 efree(dp);
1754 if (res) {
1755 return ret;
1756 }
1757 }
1758 safe_to_copy:
1759
1760 srcstream = php_stream_open_wrapper_ex(src, "rb", src_flg | REPORT_ERRORS, NULL, ctx);
1761
1762 if (!srcstream) {
1763 return ret;
1764 }
1765
1766 deststream = php_stream_open_wrapper_ex(dest, "wb", REPORT_ERRORS, NULL, ctx);
1767
1768 if (srcstream && deststream) {
1769 ret = php_stream_copy_to_stream_ex(srcstream, deststream, PHP_STREAM_COPY_ALL, NULL);
1770 }
1771 if (srcstream) {
1772 php_stream_close(srcstream);
1773 }
1774 if (deststream) {
1775 php_stream_close(deststream);
1776 }
1777 return ret;
1778 }
1779 /* }}} */
1780
1781 /* {{{ proto string|false fread(resource fp, int length)
1782 Binary-safe file read */
PHP_FUNCTION(fread)1783 PHPAPI PHP_FUNCTION(fread)
1784 {
1785 zval *res;
1786 zend_long len;
1787 php_stream *stream;
1788 zend_string *str;
1789
1790 ZEND_PARSE_PARAMETERS_START(2, 2)
1791 Z_PARAM_RESOURCE(res)
1792 Z_PARAM_LONG(len)
1793 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1794
1795 PHP_STREAM_TO_ZVAL(stream, res);
1796
1797 if (len <= 0) {
1798 php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
1799 RETURN_FALSE;
1800 }
1801
1802 str = php_stream_read_to_str(stream, len);
1803 if (!str) {
1804 zval_ptr_dtor_str(return_value);
1805 RETURN_FALSE;
1806 }
1807
1808 RETURN_STR(str);
1809 }
1810 /* }}} */
1811
php_fgetcsv_lookup_trailing_spaces(const char * ptr,size_t len,const char delimiter)1812 static const char *php_fgetcsv_lookup_trailing_spaces(const char *ptr, size_t len, const char delimiter) /* {{{ */
1813 {
1814 int inc_len;
1815 unsigned char last_chars[2] = { 0, 0 };
1816
1817 while (len > 0) {
1818 inc_len = (*ptr == '\0' ? 1 : php_mblen(ptr, len));
1819 switch (inc_len) {
1820 case -2:
1821 case -1:
1822 inc_len = 1;
1823 php_mb_reset();
1824 break;
1825 case 0:
1826 goto quit_loop;
1827 case 1:
1828 default:
1829 last_chars[0] = last_chars[1];
1830 last_chars[1] = *ptr;
1831 break;
1832 }
1833 ptr += inc_len;
1834 len -= inc_len;
1835 }
1836 quit_loop:
1837 switch (last_chars[1]) {
1838 case '\n':
1839 if (last_chars[0] == '\r') {
1840 return ptr - 2;
1841 }
1842 /* break is omitted intentionally */
1843 case '\r':
1844 return ptr - 1;
1845 }
1846 return ptr;
1847 }
1848 /* }}} */
1849
1850 #define FPUTCSV_FLD_CHK(c) memchr(ZSTR_VAL(field_str), c, ZSTR_LEN(field_str))
1851
1852 /* {{{ proto int fputcsv(resource fp, array fields [, string delimiter [, string enclosure [, string escape_char]]])
1853 Format line as CSV and write to file pointer */
PHP_FUNCTION(fputcsv)1854 PHP_FUNCTION(fputcsv)
1855 {
1856 char delimiter = ','; /* allow this to be set as parameter */
1857 char enclosure = '"'; /* allow this to be set as parameter */
1858 int escape_char = (unsigned char) '\\'; /* allow this to be set as parameter */
1859 php_stream *stream;
1860 zval *fp = NULL, *fields = NULL;
1861 ssize_t ret;
1862 char *delimiter_str = NULL, *enclosure_str = NULL, *escape_str = NULL;
1863 size_t delimiter_str_len = 0, enclosure_str_len = 0, escape_str_len = 0;
1864
1865 ZEND_PARSE_PARAMETERS_START(2, 5)
1866 Z_PARAM_RESOURCE(fp)
1867 Z_PARAM_ARRAY(fields)
1868 Z_PARAM_OPTIONAL
1869 Z_PARAM_STRING(delimiter_str, delimiter_str_len)
1870 Z_PARAM_STRING(enclosure_str, enclosure_str_len)
1871 Z_PARAM_STRING(escape_str, escape_str_len)
1872 ZEND_PARSE_PARAMETERS_END();
1873
1874 if (delimiter_str != NULL) {
1875 /* Make sure that there is at least one character in string */
1876 if (delimiter_str_len < 1) {
1877 php_error_docref(NULL, E_WARNING, "delimiter must be a character");
1878 RETURN_FALSE;
1879 } else if (delimiter_str_len > 1) {
1880 php_error_docref(NULL, E_NOTICE, "delimiter must be a single character");
1881 }
1882
1883 /* use first character from string */
1884 delimiter = *delimiter_str;
1885 }
1886
1887 if (enclosure_str != NULL) {
1888 if (enclosure_str_len < 1) {
1889 php_error_docref(NULL, E_WARNING, "enclosure must be a character");
1890 RETURN_FALSE;
1891 } else if (enclosure_str_len > 1) {
1892 php_error_docref(NULL, E_NOTICE, "enclosure must be a single character");
1893 }
1894 /* use first character from string */
1895 enclosure = *enclosure_str;
1896 }
1897
1898 if (escape_str != NULL) {
1899 if (escape_str_len > 1) {
1900 php_error_docref(NULL, E_NOTICE, "escape must be empty or a single character");
1901 }
1902 if (escape_str_len < 1) {
1903 escape_char = PHP_CSV_NO_ESCAPE;
1904 } else {
1905 /* use first character from string */
1906 escape_char = (unsigned char) *escape_str;
1907 }
1908 }
1909
1910 PHP_STREAM_TO_ZVAL(stream, fp);
1911
1912 ret = php_fputcsv(stream, fields, delimiter, enclosure, escape_char);
1913 if (ret < 0) {
1914 RETURN_FALSE;
1915 }
1916 RETURN_LONG(ret);
1917 }
1918 /* }}} */
1919
1920 /* {{{ PHPAPI size_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, int escape_char) */
php_fputcsv(php_stream * stream,zval * fields,char delimiter,char enclosure,int escape_char)1921 PHPAPI ssize_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, int escape_char)
1922 {
1923 int count, i = 0;
1924 size_t ret;
1925 zval *field_tmp;
1926 smart_str csvline = {0};
1927
1928 ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE);
1929 count = zend_hash_num_elements(Z_ARRVAL_P(fields));
1930 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(fields), field_tmp) {
1931 zend_string *tmp_field_str;
1932 zend_string *field_str = zval_get_tmp_string(field_tmp, &tmp_field_str);
1933
1934 /* enclose a field that contains a delimiter, an enclosure character, or a newline */
1935 if (FPUTCSV_FLD_CHK(delimiter) ||
1936 FPUTCSV_FLD_CHK(enclosure) ||
1937 (escape_char != PHP_CSV_NO_ESCAPE && FPUTCSV_FLD_CHK(escape_char)) ||
1938 FPUTCSV_FLD_CHK('\n') ||
1939 FPUTCSV_FLD_CHK('\r') ||
1940 FPUTCSV_FLD_CHK('\t') ||
1941 FPUTCSV_FLD_CHK(' ')
1942 ) {
1943 char *ch = ZSTR_VAL(field_str);
1944 char *end = ch + ZSTR_LEN(field_str);
1945 int escaped = 0;
1946
1947 smart_str_appendc(&csvline, enclosure);
1948 while (ch < end) {
1949 if (escape_char != PHP_CSV_NO_ESCAPE && *ch == escape_char) {
1950 escaped = 1;
1951 } else if (!escaped && *ch == enclosure) {
1952 smart_str_appendc(&csvline, enclosure);
1953 } else {
1954 escaped = 0;
1955 }
1956 smart_str_appendc(&csvline, *ch);
1957 ch++;
1958 }
1959 smart_str_appendc(&csvline, enclosure);
1960 } else {
1961 smart_str_append(&csvline, field_str);
1962 }
1963
1964 if (++i != count) {
1965 smart_str_appendl(&csvline, &delimiter, 1);
1966 }
1967 zend_tmp_string_release(tmp_field_str);
1968 } ZEND_HASH_FOREACH_END();
1969
1970 smart_str_appendc(&csvline, '\n');
1971 smart_str_0(&csvline);
1972
1973 ret = php_stream_write(stream, ZSTR_VAL(csvline.s), ZSTR_LEN(csvline.s));
1974
1975 smart_str_free(&csvline);
1976
1977 return ret;
1978 }
1979 /* }}} */
1980
1981 /* {{{ proto array fgetcsv(resource fp [,int length [, string delimiter [, string enclosure [, string escape]]]])
1982 Get line from file pointer and parse for CSV fields */
PHP_FUNCTION(fgetcsv)1983 PHP_FUNCTION(fgetcsv)
1984 {
1985 char delimiter = ','; /* allow this to be set as parameter */
1986 char enclosure = '"'; /* allow this to be set as parameter */
1987 int escape = (unsigned char) '\\';
1988
1989 /* first section exactly as php_fgetss */
1990
1991 zend_long len = 0;
1992 size_t buf_len;
1993 char *buf;
1994 php_stream *stream;
1995
1996 {
1997 zval *fd, *len_zv = NULL;
1998 char *delimiter_str = NULL;
1999 size_t delimiter_str_len = 0;
2000 char *enclosure_str = NULL;
2001 size_t enclosure_str_len = 0;
2002 char *escape_str = NULL;
2003 size_t escape_str_len = 0;
2004
2005 ZEND_PARSE_PARAMETERS_START(1, 5)
2006 Z_PARAM_RESOURCE(fd)
2007 Z_PARAM_OPTIONAL
2008 Z_PARAM_ZVAL(len_zv)
2009 Z_PARAM_STRING(delimiter_str, delimiter_str_len)
2010 Z_PARAM_STRING(enclosure_str, enclosure_str_len)
2011 Z_PARAM_STRING(escape_str, escape_str_len)
2012 ZEND_PARSE_PARAMETERS_END();
2013
2014 if (delimiter_str != NULL) {
2015 /* Make sure that there is at least one character in string */
2016 if (delimiter_str_len < 1) {
2017 php_error_docref(NULL, E_WARNING, "delimiter must be a character");
2018 RETURN_FALSE;
2019 } else if (delimiter_str_len > 1) {
2020 php_error_docref(NULL, E_NOTICE, "delimiter must be a single character");
2021 }
2022
2023 /* use first character from string */
2024 delimiter = delimiter_str[0];
2025 }
2026
2027 if (enclosure_str != NULL) {
2028 if (enclosure_str_len < 1) {
2029 php_error_docref(NULL, E_WARNING, "enclosure must be a character");
2030 RETURN_FALSE;
2031 } else if (enclosure_str_len > 1) {
2032 php_error_docref(NULL, E_NOTICE, "enclosure must be a single character");
2033 }
2034
2035 /* use first character from string */
2036 enclosure = enclosure_str[0];
2037 }
2038
2039 if (escape_str != NULL) {
2040 if (escape_str_len > 1) {
2041 php_error_docref(NULL, E_NOTICE, "escape must be empty or a single character");
2042 }
2043
2044 if (escape_str_len < 1) {
2045 escape = PHP_CSV_NO_ESCAPE;
2046 } else {
2047 escape = (unsigned char) escape_str[0];
2048 }
2049 }
2050
2051 if (len_zv != NULL && Z_TYPE_P(len_zv) != IS_NULL) {
2052 len = zval_get_long(len_zv);
2053 if (len < 0) {
2054 php_error_docref(NULL, E_WARNING, "Length parameter may not be negative");
2055 RETURN_FALSE;
2056 } else if (len == 0) {
2057 len = -1;
2058 }
2059 } else {
2060 len = -1;
2061 }
2062
2063 PHP_STREAM_TO_ZVAL(stream, fd);
2064 }
2065
2066 if (len < 0) {
2067 if ((buf = php_stream_get_line(stream, NULL, 0, &buf_len)) == NULL) {
2068 RETURN_FALSE;
2069 }
2070 } else {
2071 buf = emalloc(len + 1);
2072 if (php_stream_get_line(stream, buf, len + 1, &buf_len) == NULL) {
2073 efree(buf);
2074 RETURN_FALSE;
2075 }
2076 }
2077
2078 php_fgetcsv(stream, delimiter, enclosure, escape, buf_len, buf, return_value);
2079 }
2080 /* }}} */
2081
php_fgetcsv(php_stream * stream,char delimiter,char enclosure,int escape_char,size_t buf_len,char * buf,zval * return_value)2082 PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf, zval *return_value) /* {{{ */
2083 {
2084 char *temp, *tptr, *bptr, *line_end, *limit;
2085 size_t temp_len, line_end_len;
2086 int inc_len;
2087 zend_bool first_field = 1;
2088
2089 ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE);
2090
2091 /* initialize internal state */
2092 php_mb_reset();
2093
2094 /* Now into new section that parses buf for delimiter/enclosure fields */
2095
2096 /* Strip trailing space from buf, saving end of line in case required for enclosure field */
2097
2098 bptr = buf;
2099 tptr = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len, delimiter);
2100 line_end_len = buf_len - (size_t)(tptr - buf);
2101 line_end = limit = tptr;
2102
2103 /* reserve workspace for building each individual field */
2104 temp_len = buf_len;
2105 temp = emalloc(temp_len + line_end_len + 1);
2106
2107 /* Initialize return array */
2108 array_init(return_value);
2109
2110 /* Main loop to read CSV fields */
2111 /* NB this routine will return a single null entry for a blank line */
2112
2113 do {
2114 char *comp_end, *hunk_begin;
2115
2116 tptr = temp;
2117
2118 inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
2119 if (inc_len == 1) {
2120 char *tmp = bptr;
2121 while ((*tmp != delimiter) && isspace((int)*(unsigned char *)tmp)) {
2122 tmp++;
2123 }
2124 if (*tmp == enclosure) {
2125 bptr = tmp;
2126 }
2127 }
2128
2129 if (first_field && bptr == line_end) {
2130 add_next_index_null(return_value);
2131 break;
2132 }
2133 first_field = 0;
2134 /* 2. Read field, leaving bptr pointing at start of next field */
2135 if (inc_len != 0 && *bptr == enclosure) {
2136 int state = 0;
2137
2138 bptr++; /* move on to first character in field */
2139 hunk_begin = bptr;
2140
2141 /* 2A. handle enclosure delimited field */
2142 for (;;) {
2143 switch (inc_len) {
2144 case 0:
2145 switch (state) {
2146 case 2:
2147 memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
2148 tptr += (bptr - hunk_begin - 1);
2149 hunk_begin = bptr;
2150 goto quit_loop_2;
2151
2152 case 1:
2153 memcpy(tptr, hunk_begin, bptr - hunk_begin);
2154 tptr += (bptr - hunk_begin);
2155 hunk_begin = bptr;
2156 /* break is omitted intentionally */
2157
2158 case 0: {
2159 char *new_buf;
2160 size_t new_len;
2161 char *new_temp;
2162
2163 if (hunk_begin != line_end) {
2164 memcpy(tptr, hunk_begin, bptr - hunk_begin);
2165 tptr += (bptr - hunk_begin);
2166 hunk_begin = bptr;
2167 }
2168
2169 /* add the embedded line end to the field */
2170 memcpy(tptr, line_end, line_end_len);
2171 tptr += line_end_len;
2172
2173 if (stream == NULL) {
2174 goto quit_loop_2;
2175 } else if ((new_buf = php_stream_get_line(stream, NULL, 0, &new_len)) == NULL) {
2176 /* we've got an unterminated enclosure,
2177 * assign all the data from the start of
2178 * the enclosure to end of data to the
2179 * last element */
2180 if ((size_t)temp_len > (size_t)(limit - buf)) {
2181 goto quit_loop_2;
2182 }
2183 zend_array_destroy(Z_ARR_P(return_value));
2184 RETVAL_FALSE;
2185 goto out;
2186 }
2187 temp_len += new_len;
2188 new_temp = erealloc(temp, temp_len);
2189 tptr = new_temp + (size_t)(tptr - temp);
2190 temp = new_temp;
2191
2192 efree(buf);
2193 buf_len = new_len;
2194 bptr = buf = new_buf;
2195 hunk_begin = buf;
2196
2197 line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len, delimiter);
2198 line_end_len = buf_len - (size_t)(limit - buf);
2199
2200 state = 0;
2201 } break;
2202 }
2203 break;
2204
2205 case -2:
2206 case -1:
2207 php_mb_reset();
2208 /* break is omitted intentionally */
2209 case 1:
2210 /* we need to determine if the enclosure is
2211 * 'real' or is it escaped */
2212 switch (state) {
2213 case 1: /* escaped */
2214 bptr++;
2215 state = 0;
2216 break;
2217 case 2: /* embedded enclosure ? let's check it */
2218 if (*bptr != enclosure) {
2219 /* real enclosure */
2220 memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
2221 tptr += (bptr - hunk_begin - 1);
2222 hunk_begin = bptr;
2223 goto quit_loop_2;
2224 }
2225 memcpy(tptr, hunk_begin, bptr - hunk_begin);
2226 tptr += (bptr - hunk_begin);
2227 bptr++;
2228 hunk_begin = bptr;
2229 state = 0;
2230 break;
2231 default:
2232 if (*bptr == enclosure) {
2233 state = 2;
2234 } else if (escape_char != PHP_CSV_NO_ESCAPE && *bptr == escape_char) {
2235 state = 1;
2236 }
2237 bptr++;
2238 break;
2239 }
2240 break;
2241
2242 default:
2243 switch (state) {
2244 case 2:
2245 /* real enclosure */
2246 memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
2247 tptr += (bptr - hunk_begin - 1);
2248 hunk_begin = bptr;
2249 goto quit_loop_2;
2250 case 1:
2251 bptr += inc_len;
2252 memcpy(tptr, hunk_begin, bptr - hunk_begin);
2253 tptr += (bptr - hunk_begin);
2254 hunk_begin = bptr;
2255 state = 0;
2256 break;
2257 default:
2258 bptr += inc_len;
2259 break;
2260 }
2261 break;
2262 }
2263 inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
2264 }
2265
2266 quit_loop_2:
2267 /* look up for a delimiter */
2268 for (;;) {
2269 switch (inc_len) {
2270 case 0:
2271 goto quit_loop_3;
2272
2273 case -2:
2274 case -1:
2275 inc_len = 1;
2276 php_mb_reset();
2277 /* break is omitted intentionally */
2278 case 1:
2279 if (*bptr == delimiter) {
2280 goto quit_loop_3;
2281 }
2282 break;
2283 default:
2284 break;
2285 }
2286 bptr += inc_len;
2287 inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
2288 }
2289
2290 quit_loop_3:
2291 memcpy(tptr, hunk_begin, bptr - hunk_begin);
2292 tptr += (bptr - hunk_begin);
2293 bptr += inc_len;
2294 comp_end = tptr;
2295 } else {
2296 /* 2B. Handle non-enclosure field */
2297
2298 hunk_begin = bptr;
2299
2300 for (;;) {
2301 switch (inc_len) {
2302 case 0:
2303 goto quit_loop_4;
2304 case -2:
2305 case -1:
2306 inc_len = 1;
2307 php_mb_reset();
2308 /* break is omitted intentionally */
2309 case 1:
2310 if (*bptr == delimiter) {
2311 goto quit_loop_4;
2312 }
2313 break;
2314 default:
2315 break;
2316 }
2317 bptr += inc_len;
2318 inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
2319 }
2320 quit_loop_4:
2321 memcpy(tptr, hunk_begin, bptr - hunk_begin);
2322 tptr += (bptr - hunk_begin);
2323
2324 comp_end = (char *)php_fgetcsv_lookup_trailing_spaces(temp, tptr - temp, delimiter);
2325 if (*bptr == delimiter) {
2326 bptr++;
2327 }
2328 }
2329
2330 /* 3. Now pass our field back to php */
2331 *comp_end = '\0';
2332 add_next_index_stringl(return_value, temp, comp_end - temp);
2333 } while (inc_len > 0);
2334
2335 out:
2336 efree(temp);
2337 if (stream) {
2338 efree(buf);
2339 }
2340 }
2341 /* }}} */
2342
2343 #if HAVE_REALPATH || defined(ZTS)
2344 /* {{{ proto string realpath(string path)
2345 Return the resolved path */
PHP_FUNCTION(realpath)2346 PHP_FUNCTION(realpath)
2347 {
2348 char *filename;
2349 size_t filename_len;
2350 char resolved_path_buff[MAXPATHLEN];
2351
2352 ZEND_PARSE_PARAMETERS_START(1, 1)
2353 Z_PARAM_PATH(filename, filename_len)
2354 ZEND_PARSE_PARAMETERS_END();
2355
2356 if (VCWD_REALPATH(filename, resolved_path_buff)) {
2357 if (php_check_open_basedir(resolved_path_buff)) {
2358 RETURN_FALSE;
2359 }
2360
2361 #ifdef ZTS
2362 if (VCWD_ACCESS(resolved_path_buff, F_OK)) {
2363 RETURN_FALSE;
2364 }
2365 #endif
2366 RETURN_STRING(resolved_path_buff);
2367 } else {
2368 RETURN_FALSE;
2369 }
2370 }
2371 /* }}} */
2372 #endif
2373
2374 /* See http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2 */
2375 #define PHP_META_HTML401_CHARS "-_.:"
2376
2377 /* {{{ php_next_meta_token
2378 Tokenizes an HTML file for get_meta_tags */
php_next_meta_token(php_meta_tags_data * md)2379 php_meta_tags_token php_next_meta_token(php_meta_tags_data *md)
2380 {
2381 int ch = 0, compliment;
2382 char buff[META_DEF_BUFSIZE + 1];
2383
2384 memset((void *)buff, 0, META_DEF_BUFSIZE + 1);
2385
2386 while (md->ulc || (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)))) {
2387 if (php_stream_eof(md->stream)) {
2388 break;
2389 }
2390
2391 if (md->ulc) {
2392 ch = md->lc;
2393 md->ulc = 0;
2394 }
2395
2396 switch (ch) {
2397 case '<':
2398 return TOK_OPENTAG;
2399 break;
2400
2401 case '>':
2402 return TOK_CLOSETAG;
2403 break;
2404
2405 case '=':
2406 return TOK_EQUAL;
2407 break;
2408 case '/':
2409 return TOK_SLASH;
2410 break;
2411
2412 case '\'':
2413 case '"':
2414 compliment = ch;
2415 md->token_len = 0;
2416 while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && ch != compliment && ch != '<' && ch != '>') {
2417 buff[(md->token_len)++] = ch;
2418
2419 if (md->token_len == META_DEF_BUFSIZE) {
2420 break;
2421 }
2422 }
2423
2424 if (ch == '<' || ch == '>') {
2425 /* Was just an apostrohpe */
2426 md->ulc = 1;
2427 md->lc = ch;
2428 }
2429
2430 /* We don't need to alloc unless we are in a meta tag */
2431 if (md->in_meta) {
2432 md->token_data = (char *) emalloc(md->token_len + 1);
2433 memcpy(md->token_data, buff, md->token_len+1);
2434 }
2435
2436 return TOK_STRING;
2437 break;
2438
2439 case '\n':
2440 case '\r':
2441 case '\t':
2442 break;
2443
2444 case ' ':
2445 return TOK_SPACE;
2446 break;
2447
2448 default:
2449 if (isalnum(ch)) {
2450 md->token_len = 0;
2451 buff[(md->token_len)++] = ch;
2452 while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && (isalnum(ch) || strchr(PHP_META_HTML401_CHARS, ch))) {
2453 buff[(md->token_len)++] = ch;
2454
2455 if (md->token_len == META_DEF_BUFSIZE) {
2456 break;
2457 }
2458 }
2459
2460 /* This is ugly, but we have to replace ungetc */
2461 if (!isalpha(ch) && ch != '-') {
2462 md->ulc = 1;
2463 md->lc = ch;
2464 }
2465
2466 md->token_data = (char *) emalloc(md->token_len + 1);
2467 memcpy(md->token_data, buff, md->token_len+1);
2468
2469 return TOK_ID;
2470 } else {
2471 return TOK_OTHER;
2472 }
2473 break;
2474 }
2475 }
2476
2477 return TOK_EOF;
2478 }
2479 /* }}} */
2480
2481 #ifdef HAVE_FNMATCH
2482 /* {{{ proto bool fnmatch(string pattern, string filename [, int flags])
2483 Match filename against pattern */
PHP_FUNCTION(fnmatch)2484 PHP_FUNCTION(fnmatch)
2485 {
2486 char *pattern, *filename;
2487 size_t pattern_len, filename_len;
2488 zend_long flags = 0;
2489
2490 ZEND_PARSE_PARAMETERS_START(2, 3)
2491 Z_PARAM_PATH(pattern, pattern_len)
2492 Z_PARAM_PATH(filename, filename_len)
2493 Z_PARAM_OPTIONAL
2494 Z_PARAM_LONG(flags)
2495 ZEND_PARSE_PARAMETERS_END();
2496
2497 if (filename_len >= MAXPATHLEN) {
2498 php_error_docref(NULL, E_WARNING, "Filename exceeds the maximum allowed length of %d characters", MAXPATHLEN);
2499 RETURN_FALSE;
2500 }
2501 if (pattern_len >= MAXPATHLEN) {
2502 php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
2503 RETURN_FALSE;
2504 }
2505
2506 RETURN_BOOL( ! fnmatch( pattern, filename, (int)flags ));
2507 }
2508 /* }}} */
2509 #endif
2510
2511 /* {{{ proto string sys_get_temp_dir()
2512 Returns directory path used for temporary files */
PHP_FUNCTION(sys_get_temp_dir)2513 PHP_FUNCTION(sys_get_temp_dir)
2514 {
2515 if (zend_parse_parameters_none() == FAILURE) {
2516 return;
2517 }
2518 RETURN_STRING((char *)php_get_temporary_directory());
2519 }
2520 /* }}} */
2521