📄 file.c
字号:
/* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf <rasmus@php.net> | | Stig Bakken <ssb@fast.no> | | Andi Gutmans <andi@zend.com> | | Zeev Suraski <zeev@zend.com> | | PHP 4.0 patches by Thies C. Arntzen (thies@thieso.net) | | PHP streams by Wez Furlong (wez@thebrainroom.com) | +----------------------------------------------------------------------+ *//* $Id: file.c,v 1.279.2.70.2.8 2007/01/01 09:46:48 sebastian Exp $ *//* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] *//* {{{ includes */#include "php.h"#include "php_globals.h"#include "ext/standard/flock_compat.h"#include "ext/standard/exec.h"#include "ext/standard/php_filestat.h"#include "php_open_temporary_file.h"#include "ext/standard/basic_functions.h"#include "php_ini.h"#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#ifdef PHP_WIN32#include <io.h>#include <windows.h>#include <winsock.h>#define O_RDONLY _O_RDONLY#include "win32/param.h"#include "win32/winutil.h"#else#include <sys/param.h>#if HAVE_SYS_SELECT_H#include <sys/select.h>#endif#if defined(NETWARE) && defined(USE_WINSOCK)#include <novsock2.h>#else#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#endif#include "ext/standard/head.h"#include "safe_mode.h"#include "php_string.h"#include "file.h"#if HAVE_PWD_H#ifdef PHP_WIN32#include "win32/pwd.h"#else#include <pwd.h>#endif#endif#ifdef HAVE_SYS_TIME_H#include <sys/time.h>#endif#include "fsock.h"#include "fopen_wrappers.h"#include "php_streams.h"#include "php_globals.h"#ifdef HAVE_SYS_FILE_H#include <sys/file.h>#endif#if MISSING_FCLOSE_DECLextern int fclose(FILE *);#endif#ifdef HAVE_SYS_MMAN_H#include <sys/mman.h>#endif#ifndef MAP_FAILED#define MAP_FAILED ((void *) -1)#endif#include "scanf.h"#include "zend_API.h"#ifdef ZTSint file_globals_id;#elsephp_file_globals file_globals;#endif#ifdef HAVE_FNMATCH#ifndef _GNU_SOURCE#define _GNU_SOURCE#endif#include <fnmatch.h>#endif/* }}} */#define PHP_STREAM_TO_ZVAL(stream, arg) \ php_stream_from_zval_no_verify(stream, arg); \ if (stream == NULL) { \ RETURN_FALSE; \ }/* {{{ ZTS-stuff / Globals / Prototypes *//* sharing globals is *evil* */static int le_stream_context = FAILURE;PHPAPI int php_le_stream_context(void){ return le_stream_context;}/* }}} *//* {{{ Module-Stuff */static ZEND_RSRC_DTOR_FUNC(file_context_dtor){ php_stream_context *context = (php_stream_context*)rsrc->ptr; if (context->options) { zval_ptr_dtor(&context->options); context->options = NULL; } php_stream_context_free(context);}static void file_globals_ctor(php_file_globals *file_globals_p TSRMLS_DC){ FG(pclose_ret) = 0; FG(user_stream_current_filename) = NULL; FG(def_chunk_size) = PHP_SOCK_CHUNK_SIZE;}static void file_globals_dtor(php_file_globals *file_globals_p TSRMLS_DC){}PHP_INI_BEGIN() STD_PHP_INI_ENTRY("user_agent", NULL, PHP_INI_ALL, OnUpdateString, user_agent, php_file_globals, file_globals) STD_PHP_INI_ENTRY("default_socket_timeout", "60", PHP_INI_ALL, OnUpdateInt, default_socket_timeout, php_file_globals, file_globals) STD_PHP_INI_ENTRY("auto_detect_line_endings", "0", PHP_INI_ALL, OnUpdateInt, auto_detect_line_endings, php_file_globals, file_globals)PHP_INI_END()PHP_MINIT_FUNCTION(file){ le_stream_context = zend_register_list_destructors_ex(file_context_dtor, NULL, "stream-context", module_number);#ifdef ZTS ts_allocate_id(&file_globals_id, sizeof(php_file_globals), (ts_allocate_ctor) file_globals_ctor, (ts_allocate_dtor) file_globals_dtor);#else file_globals_ctor(&file_globals TSRMLS_CC);#endif REGISTER_INI_ENTRIES(); REGISTER_LONG_CONSTANT("SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("LOCK_SH", 1, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("LOCK_EX", 2, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("LOCK_UN", 3, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("LOCK_NB", 4, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_CONNECT", PHP_STREAM_NOTIFY_CONNECT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_REQUIRED", PHP_STREAM_NOTIFY_AUTH_REQUIRED, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_RESULT", PHP_STREAM_NOTIFY_AUTH_RESULT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_MIME_TYPE_IS", PHP_STREAM_NOTIFY_MIME_TYPE_IS, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FILE_SIZE_IS", PHP_STREAM_NOTIFY_FILE_SIZE_IS, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_REDIRECTED", PHP_STREAM_NOTIFY_REDIRECTED, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_PROGRESS", PHP_STREAM_NOTIFY_PROGRESS, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FAILURE", PHP_STREAM_NOTIFY_FAILURE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_INFO", PHP_STREAM_NOTIFY_SEVERITY_INFO, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_WARN", PHP_STREAM_NOTIFY_SEVERITY_WARN, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_ERR", PHP_STREAM_NOTIFY_SEVERITY_ERR, CONST_CS | CONST_PERSISTENT);#ifdef HAVE_FNMATCH REGISTER_LONG_CONSTANT("FNM_NOESCAPE", FNM_NOESCAPE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FNM_PATHNAME", FNM_PATHNAME, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FNM_PERIOD", FNM_PERIOD, CONST_CS | CONST_PERSISTENT);#ifdef FNM_CASEFOLD /* a GNU extension */ /* TODO emulate if not available */ REGISTER_LONG_CONSTANT("FNM_CASEFOLD", FNM_CASEFOLD, CONST_CS | CONST_PERSISTENT);#endif#endif return SUCCESS;}/* }}} */PHP_MSHUTDOWN_FUNCTION(file){#ifndef ZTS file_globals_dtor(&file_globals TSRMLS_CC);#endif return SUCCESS;}/* {{{ proto bool flock(resource fp, int operation [, int &wouldblock]) Portable file locking */static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN };PHP_FUNCTION(flock){ zval **arg1, **arg2, **arg3; int fd, act, arg_count = ZEND_NUM_ARGS(); php_stream *stream; if (arg_count < 2 || arg_count > 3 || zend_get_parameters_ex(arg_count, &arg1, &arg2, &arg3) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, arg1); if (php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, 1) == FAILURE) { RETURN_FALSE; } convert_to_long_ex(arg2); act = Z_LVAL_PP(arg2) & 3; if (act < 1 || act > 3) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal operation argument"); RETURN_FALSE; } if (arg_count == 3) { convert_to_long_ex(arg3); Z_LVAL_PP(arg3) = 0; } /* flock_values contains all possible actions if (arg2 & 4) we won't block on the lock */ act = flock_values[act - 1] | (Z_LVAL_PP(arg2) & 4 ? LOCK_NB : 0); if (flock(fd, act)) { if (errno == EWOULDBLOCK && arg_count == 3) { Z_LVAL_PP(arg3) = 1; } else { RETURN_FALSE; } } RETURN_TRUE;}/* }}} */#define PHP_META_UNSAFE ".\\+*?[^]$() "/* {{{ proto array get_meta_tags(string filename [, bool use_include_path]) Extracts all meta tag content attributes from a file and returns an array */PHP_FUNCTION(get_meta_tags){ char *filename; int filename_len; zend_bool use_include_path = 0; int in_tag = 0, done = 0; int looking_for_val = 0, have_name = 0, have_content = 0; int saw_name = 0, saw_content = 0; char *name = NULL, *value = NULL, *temp = NULL; php_meta_tags_token tok, tok_last; php_meta_tags_data md; /* Initiailize our structure */ memset(&md, 0, sizeof(md)); /* Parse arguments */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &filename_len, &use_include_path) == FAILURE) { return; } md.stream = php_stream_open_wrapper(filename, "rb", (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); if (!md.stream) { RETURN_FALSE; } array_init(return_value); tok_last = TOK_EOF; while (!done && (tok = php_next_meta_token(&md TSRMLS_CC)) != TOK_EOF) { if (tok == TOK_ID) { if (tok_last == TOK_OPENTAG) { md.in_meta = !strcasecmp("meta", md.token_data); } else if (tok_last == TOK_SLASH && in_tag) { if (strcasecmp("head", md.token_data) == 0) { /* We are done here! */ done = 1; } } else if (tok_last == TOK_EQUAL && looking_for_val) { if (saw_name) { /* Get the NAME attr (Single word attr, non-quoted) */ temp = name = estrndup(md.token_data, md.token_len); while (temp && *temp) { if (strchr(PHP_META_UNSAFE, *temp)) { *temp = '_'; } temp++; } have_name = 1; } else if (saw_content) { /* Get the CONTENT attr (Single word attr, non-quoted) */ if (PG(magic_quotes_runtime)) { value = php_addslashes(md.token_data, 0, &md.token_len, 0 TSRMLS_CC); } else { value = estrndup(md.token_data, md.token_len); } have_content = 1; } looking_for_val = 0; } else { if (md.in_meta) { if (strcasecmp("name", md.token_data) == 0) { saw_name = 1; saw_content = 0; looking_for_val = 1; } else if (strcasecmp("content", md.token_data) == 0) { saw_name = 0; saw_content = 1; looking_for_val = 1; } } } } else if (tok == TOK_STRING && tok_last == TOK_EQUAL && looking_for_val) { if (saw_name) { /* Get the NAME attr (Quoted single/double) */ temp = name = estrndup(md.token_data, md.token_len); while (temp && *temp) { if (strchr(PHP_META_UNSAFE, *temp)) { *temp = '_'; } temp++; } have_name = 1; } else if (saw_content) { /* Get the CONTENT attr (Single word attr, non-quoted) */ if (PG(magic_quotes_runtime)) { value = php_addslashes(md.token_data, 0, &md.token_len, 0 TSRMLS_CC); } else { value = estrndup(md.token_data, md.token_len); } have_content = 1; } looking_for_val = 0; } else if (tok == TOK_OPENTAG) { if (looking_for_val) { looking_for_val = 0; have_name = saw_name = 0; have_content = saw_content = 0; } in_tag = 1; } else if (tok == TOK_CLOSETAG) { if (have_name) { /* For BC */ php_strtolower(name, strlen(name)); if (have_content) { add_assoc_string(return_value, name, value, 0); } else { add_assoc_string(return_value, name, empty_string, 0); } efree(name); } else if (have_content) { efree(value); } name = value = NULL; /* Reset all of our flags */ in_tag = looking_for_val = 0; have_name = saw_name = 0; have_content = saw_content = 0; md.in_meta = 0; } tok_last = tok; if (md.token_data) efree(md.token_data); md.token_data = NULL; } php_stream_close(md.stream);}/* }}} *//* {{{ proto string file_get_contents(string filename [, bool use_include_path]) Read the entire file into a string */PHP_FUNCTION(file_get_contents){ char *filename; int filename_len; char *contents = NULL; zend_bool use_include_path = 0; php_stream *stream; int len, newlen; /* Parse arguments */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &filename_len, &use_include_path) == FAILURE) { return; } stream = php_stream_open_wrapper(filename, "rb", (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); if (!stream) { RETURN_FALSE; } /* uses mmap if possible */ if ((len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0)) > 0) { if (PG(magic_quotes_runtime)) { contents = php_addslashes(contents, len, &newlen, 1 TSRMLS_CC); /* 1 = free source string */ len = newlen; } RETVAL_STRINGL(contents, len, 0); contents = NULL; } else if (len == 0) { RETVAL_EMPTY_STRING(); } else { RETVAL_FALSE; } if (contents) { efree(contents); } php_stream_close(stream);}/* }}} *//* {{{ proto array file(string filename [, bool use_include_path]) Read entire file into an array */#define PHP_FILE_BUF_SIZE 80PHP_FUNCTION(file){ char *filename; int filename_len; char *slashed, *target_buf=NULL, *p, *s, *e; register int i = 0; int target_len, len; char eol_marker = '\n'; zend_bool use_include_path = 0; php_stream *stream; /* Parse arguments */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &filename_len, &use_include_path) == FAILURE) { return; } stream = php_stream_open_wrapper(filename, "rb", (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); if (!stream) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -