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