PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/php5/ext/standard/php_fopen_wrapper.c

http://github.com/vpj/PHP-Extension-API
C | 351 lines | 284 code | 35 blank | 32 comment | 79 complexity | d9881f0788dd5903c27201f6ce7d485f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2009 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. | Jim Winstead <jimw@php.net> |
  17. | Hartmut Holzgraefe <hholzgra@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id: php_fopen_wrapper.c,v 1.45.2.4.2.7.2.7 2008/12/31 11:15:45 sebastian Exp $ */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #if HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include "php.h"
  27. #include "php_globals.h"
  28. #include "php_standard.h"
  29. #include "php_fopen_wrappers.h"
  30. #include "SAPI.h"
  31. static size_t php_stream_output_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
  32. {
  33. PHPWRITE(buf, count);
  34. return count;
  35. }
  36. static size_t php_stream_output_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
  37. {
  38. stream->eof = 1;
  39. return 0;
  40. }
  41. static int php_stream_output_close(php_stream *stream, int close_handle TSRMLS_DC)
  42. {
  43. return 0;
  44. }
  45. php_stream_ops php_stream_output_ops = {
  46. php_stream_output_write,
  47. php_stream_output_read,
  48. php_stream_output_close,
  49. NULL, /* flush */
  50. "Output",
  51. NULL, /* seek */
  52. NULL, /* cast */
  53. NULL, /* stat */
  54. NULL /* set_option */
  55. };
  56. static size_t php_stream_input_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
  57. {
  58. return -1;
  59. }
  60. static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
  61. {
  62. off_t *position = (off_t*)stream->abstract;
  63. size_t read_bytes = 0;
  64. if(!stream->eof) {
  65. if(SG(request_info).raw_post_data) { /* data has already been read by a post handler */
  66. read_bytes = SG(request_info).raw_post_data_length - *position;
  67. if(read_bytes <= count) {
  68. stream->eof = 1;
  69. } else {
  70. read_bytes = count;
  71. }
  72. if(read_bytes) {
  73. memcpy(buf, SG(request_info).raw_post_data + *position, read_bytes);
  74. }
  75. } else if(sapi_module.read_post) {
  76. read_bytes = sapi_module.read_post(buf, count TSRMLS_CC);
  77. if(read_bytes <= 0){
  78. stream->eof = 1;
  79. read_bytes = 0;
  80. }
  81. } else {
  82. stream->eof = 1;
  83. }
  84. }
  85. *position += read_bytes;
  86. SG(read_post_bytes) += read_bytes;
  87. return read_bytes;
  88. }
  89. static int php_stream_input_close(php_stream *stream, int close_handle TSRMLS_DC)
  90. {
  91. efree(stream->abstract);
  92. return 0;
  93. }
  94. static int php_stream_input_flush(php_stream *stream TSRMLS_DC)
  95. {
  96. return -1;
  97. }
  98. php_stream_ops php_stream_input_ops = {
  99. php_stream_input_write,
  100. php_stream_input_read,
  101. php_stream_input_close,
  102. php_stream_input_flush,
  103. "Input",
  104. NULL, /* seek */
  105. NULL, /* cast */
  106. NULL, /* stat */
  107. NULL /* set_option */
  108. };
  109. static void php_stream_apply_filter_list(php_stream *stream, char *filterlist, int read_chain, int write_chain TSRMLS_DC) {
  110. char *p, *token;
  111. php_stream_filter *temp_filter;
  112. p = php_strtok_r(filterlist, "|", &token);
  113. while (p) {
  114. php_url_decode(p, strlen(p));
  115. if (read_chain) {
  116. if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream) TSRMLS_CC))) {
  117. php_stream_filter_append(&stream->readfilters, temp_filter);
  118. } else {
  119. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create filter (%s)", p);
  120. }
  121. }
  122. if (write_chain) {
  123. if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream) TSRMLS_CC))) {
  124. php_stream_filter_append(&stream->writefilters, temp_filter);
  125. } else {
  126. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create filter (%s)", p);
  127. }
  128. }
  129. p = php_strtok_r(NULL, "|", &token);
  130. }
  131. }
  132. php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
  133. {
  134. int fd = -1;
  135. int mode_rw = 0;
  136. php_stream * stream = NULL;
  137. char *p, *token, *pathdup;
  138. long max_memory;
  139. FILE *file = NULL;
  140. if (!strncasecmp(path, "php://", 6)) {
  141. path += 6;
  142. }
  143. if (!strncasecmp(path, "temp", 4)) {
  144. path += 4;
  145. max_memory = PHP_STREAM_MAX_MEM;
  146. if (!strncasecmp(path, "/maxmemory:", 11)) {
  147. path += 11;
  148. max_memory = strtol(path, NULL, 10);
  149. if (max_memory < 0) {
  150. php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Max memory must be >= 0");
  151. return NULL;
  152. }
  153. }
  154. if (strpbrk(mode, "wa+")) {
  155. mode_rw = TEMP_STREAM_DEFAULT;
  156. } else {
  157. mode_rw = TEMP_STREAM_READONLY;
  158. }
  159. return php_stream_temp_create(mode_rw, max_memory);
  160. }
  161. if (!strcasecmp(path, "memory")) {
  162. if (strpbrk(mode, "wa+")) {
  163. mode_rw = TEMP_STREAM_DEFAULT;
  164. } else {
  165. mode_rw = TEMP_STREAM_READONLY;
  166. }
  167. return php_stream_memory_create(mode_rw);
  168. }
  169. if (!strcasecmp(path, "output")) {
  170. return php_stream_alloc(&php_stream_output_ops, NULL, 0, "wb");
  171. }
  172. if (!strcasecmp(path, "input")) {
  173. if ((options & STREAM_OPEN_FOR_INCLUDE) && !PG(allow_url_include) ) {
  174. if (options & REPORT_ERRORS) {
  175. php_error_docref(NULL TSRMLS_CC, E_WARNING, "URL file-access is disabled in the server configuration");
  176. }
  177. return NULL;
  178. }
  179. return php_stream_alloc(&php_stream_input_ops, ecalloc(1, sizeof(off_t)), 0, "rb");
  180. }
  181. if (!strcasecmp(path, "stdin")) {
  182. if ((options & STREAM_OPEN_FOR_INCLUDE) && !PG(allow_url_include) ) {
  183. if (options & REPORT_ERRORS) {
  184. php_error_docref(NULL TSRMLS_CC, E_WARNING, "URL file-access is disabled in the server configuration");
  185. }
  186. return NULL;
  187. }
  188. if (!strcmp(sapi_module.name, "cli")) {
  189. static int cli_in = 0;
  190. fd = STDIN_FILENO;
  191. if (cli_in) {
  192. fd = dup(fd);
  193. } else {
  194. cli_in = 1;
  195. file = stdin;
  196. }
  197. } else {
  198. fd = dup(STDIN_FILENO);
  199. }
  200. } else if (!strcasecmp(path, "stdout")) {
  201. if (!strcmp(sapi_module.name, "cli")) {
  202. static int cli_out = 0;
  203. fd = STDOUT_FILENO;
  204. if (cli_out++) {
  205. fd = dup(fd);
  206. } else {
  207. cli_out = 1;
  208. file = stdout;
  209. }
  210. } else {
  211. fd = dup(STDOUT_FILENO);
  212. }
  213. } else if (!strcasecmp(path, "stderr")) {
  214. if (!strcmp(sapi_module.name, "cli")) {
  215. static int cli_err = 0;
  216. fd = STDERR_FILENO;
  217. if (cli_err++) {
  218. fd = dup(fd);
  219. } else {
  220. cli_err = 1;
  221. file = stderr;
  222. }
  223. } else {
  224. fd = dup(STDERR_FILENO);
  225. }
  226. } else if (!strncasecmp(path, "filter/", 7)) {
  227. /* Save time/memory when chain isn't specified */
  228. if (strchr(mode, 'r') || strchr(mode, '+')) {
  229. mode_rw |= PHP_STREAM_FILTER_READ;
  230. }
  231. if (strchr(mode, 'w') || strchr(mode, '+') || strchr(mode, 'a')) {
  232. mode_rw |= PHP_STREAM_FILTER_WRITE;
  233. }
  234. pathdup = estrndup(path + 6, strlen(path + 6));
  235. p = strstr(pathdup, "/resource=");
  236. if (!p) {
  237. php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "No URL resource specified");
  238. efree(pathdup);
  239. return NULL;
  240. }
  241. if (!(stream = php_stream_open_wrapper(p + 10, mode, options, opened_path))) {
  242. efree(pathdup);
  243. return NULL;
  244. }
  245. *p = '\0';
  246. p = php_strtok_r(pathdup + 1, "/", &token);
  247. while (p) {
  248. if (!strncasecmp(p, "read=", 5)) {
  249. php_stream_apply_filter_list(stream, p + 5, 1, 0 TSRMLS_CC);
  250. } else if (!strncasecmp(p, "write=", 6)) {
  251. php_stream_apply_filter_list(stream, p + 6, 0, 1 TSRMLS_CC);
  252. } else {
  253. php_stream_apply_filter_list(stream, p, mode_rw & PHP_STREAM_FILTER_READ, mode_rw & PHP_STREAM_FILTER_WRITE TSRMLS_CC);
  254. }
  255. p = php_strtok_r(NULL, "/", &token);
  256. }
  257. efree(pathdup);
  258. return stream;
  259. } else {
  260. /* invalid php://thingy */
  261. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid php:// URL specified");
  262. return NULL;
  263. }
  264. /* must be stdin, stderr or stdout */
  265. if (fd == -1) {
  266. /* failed to dup */
  267. return NULL;
  268. }
  269. #if defined(S_IFSOCK) && !defined(WIN32) && !defined(__BEOS__)
  270. do {
  271. struct stat st;
  272. memset(&st, 0, sizeof(st));
  273. if (fstat(fd, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
  274. stream = php_stream_sock_open_from_socket(fd, NULL);
  275. if (stream) {
  276. stream->ops = &php_stream_socket_ops;
  277. return stream;
  278. }
  279. }
  280. } while (0);
  281. #endif
  282. if (file) {
  283. stream = php_stream_fopen_from_file(file, mode);
  284. } else {
  285. stream = php_stream_fopen_from_fd(fd, mode, NULL);
  286. if (stream == NULL) {
  287. close(fd);
  288. }
  289. }
  290. return stream;
  291. }
  292. static php_stream_wrapper_ops php_stdio_wops = {
  293. php_stream_url_wrap_php,
  294. NULL, /* close */
  295. NULL, /* fstat */
  296. NULL, /* stat */
  297. NULL, /* opendir */
  298. "PHP",
  299. NULL, /* unlink */
  300. NULL, /* rename */
  301. NULL, /* mkdir */
  302. NULL /* rmdir */
  303. };
  304. php_stream_wrapper php_stream_php_wrapper = {
  305. &php_stdio_wops,
  306. NULL,
  307. 0, /* is_url */
  308. };
  309. /*
  310. * Local variables:
  311. * tab-width: 4
  312. * c-basic-offset: 4
  313. * End:
  314. * vim600: sw=4 ts=4 fdm=marker
  315. * vim<600: sw=4 ts=4
  316. */