PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/standard/php_fopen_wrapper.c

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