streamsfuncs.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Wez Furlong <wez@thebrainroom.com> |
  14. | Sara Golemon <pollita@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php.h"
  18. #include "php_globals.h"
  19. #include "ext/standard/flock_compat.h"
  20. #include "ext/standard/file.h"
  21. #include "ext/standard/php_filestat.h"
  22. #include "php_open_temporary_file.h"
  23. #include "ext/standard/basic_functions.h"
  24. #include "php_ini.h"
  25. #include "streamsfuncs.h"
  26. #include "php_network.h"
  27. #include "php_string.h"
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #ifndef PHP_WIN32
  32. #define php_select(m, r, w, e, t) select(m, r, w, e, t)
  33. typedef unsigned long long php_timeout_ull;
  34. #else
  35. #include "win32/select.h"
  36. #include "win32/sockets.h"
  37. #include "win32/console.h"
  38. typedef unsigned __int64 php_timeout_ull;
  39. #endif
  40. #define GET_CTX_OPT(stream, wrapper, name, val) (PHP_STREAM_CONTEXT(stream) && NULL != (val = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), wrapper, name)))
  41. static php_stream_context *decode_context_param(zval *contextresource);
  42. /* Streams based network functions */
  43. #if HAVE_SOCKETPAIR
  44. /* {{{ Creates a pair of connected, indistinguishable socket streams */
  45. PHP_FUNCTION(stream_socket_pair)
  46. {
  47. zend_long domain, type, protocol;
  48. php_stream *s1, *s2;
  49. php_socket_t pair[2];
  50. ZEND_PARSE_PARAMETERS_START(3, 3)
  51. Z_PARAM_LONG(domain)
  52. Z_PARAM_LONG(type)
  53. Z_PARAM_LONG(protocol)
  54. ZEND_PARSE_PARAMETERS_END();
  55. if (0 != socketpair((int)domain, (int)type, (int)protocol, pair)) {
  56. char errbuf[256];
  57. php_error_docref(NULL, E_WARNING, "Failed to create sockets: [%d]: %s",
  58. php_socket_errno(), php_socket_strerror(php_socket_errno(), errbuf, sizeof(errbuf)));
  59. RETURN_FALSE;
  60. }
  61. s1 = php_stream_sock_open_from_socket(pair[0], 0);
  62. if (s1 == NULL) {
  63. close(pair[0]);
  64. close(pair[1]);
  65. php_error_docref(NULL, E_WARNING, "Failed to open stream from socketpair");
  66. RETURN_FALSE;
  67. }
  68. s2 = php_stream_sock_open_from_socket(pair[1], 0);
  69. if (s2 == NULL) {
  70. php_stream_free(s1, PHP_STREAM_FREE_CLOSE);
  71. close(pair[1]);
  72. php_error_docref(NULL, E_WARNING, "Failed to open stream from socketpair");
  73. RETURN_FALSE;
  74. }
  75. array_init(return_value);
  76. /* set the __exposed flag.
  77. * php_stream_to_zval() does, add_next_index_resource() does not */
  78. php_stream_auto_cleanup(s1);
  79. php_stream_auto_cleanup(s2);
  80. add_next_index_resource(return_value, s1->res);
  81. add_next_index_resource(return_value, s2->res);
  82. }
  83. /* }}} */
  84. #endif
  85. /* {{{ Open a client connection to a remote address */
  86. PHP_FUNCTION(stream_socket_client)
  87. {
  88. zend_string *host;
  89. zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL;
  90. double timeout;
  91. bool timeout_is_null = 1;
  92. php_timeout_ull conv;
  93. struct timeval tv;
  94. char *hashkey = NULL;
  95. php_stream *stream = NULL;
  96. int err;
  97. zend_long flags = PHP_STREAM_CLIENT_CONNECT;
  98. zend_string *errstr = NULL;
  99. php_stream_context *context = NULL;
  100. ZEND_PARSE_PARAMETERS_START(1, 6)
  101. Z_PARAM_STR(host)
  102. Z_PARAM_OPTIONAL
  103. Z_PARAM_ZVAL(zerrno)
  104. Z_PARAM_ZVAL(zerrstr)
  105. Z_PARAM_DOUBLE_OR_NULL(timeout, timeout_is_null)
  106. Z_PARAM_LONG(flags)
  107. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  108. ZEND_PARSE_PARAMETERS_END();
  109. RETVAL_FALSE;
  110. if (timeout_is_null) {
  111. timeout = (double)FG(default_socket_timeout);
  112. }
  113. context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
  114. if (flags & PHP_STREAM_CLIENT_PERSISTENT) {
  115. spprintf(&hashkey, 0, "stream_socket_client__%s", ZSTR_VAL(host));
  116. }
  117. /* prepare the timeout value for use */
  118. conv = (php_timeout_ull) (timeout * 1000000.0);
  119. #ifdef PHP_WIN32
  120. tv.tv_sec = (long)(conv / 1000000);
  121. tv.tv_usec =(long)(conv % 1000000);
  122. #else
  123. tv.tv_sec = conv / 1000000;
  124. tv.tv_usec = conv % 1000000;
  125. #endif
  126. if (zerrno) {
  127. ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
  128. }
  129. if (zerrstr) {
  130. ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr);
  131. }
  132. stream = php_stream_xport_create(ZSTR_VAL(host), ZSTR_LEN(host), REPORT_ERRORS,
  133. STREAM_XPORT_CLIENT | (flags & PHP_STREAM_CLIENT_CONNECT ? STREAM_XPORT_CONNECT : 0) |
  134. (flags & PHP_STREAM_CLIENT_ASYNC_CONNECT ? STREAM_XPORT_CONNECT_ASYNC : 0),
  135. hashkey, &tv, context, &errstr, &err);
  136. if (stream == NULL) {
  137. /* host might contain binary characters */
  138. zend_string *quoted_host = php_addslashes(host);
  139. php_error_docref(NULL, E_WARNING, "Unable to connect to %s (%s)", ZSTR_VAL(quoted_host), errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
  140. zend_string_release_ex(quoted_host, 0);
  141. }
  142. if (hashkey) {
  143. efree(hashkey);
  144. }
  145. if (stream == NULL) {
  146. if (zerrno) {
  147. ZEND_TRY_ASSIGN_REF_LONG(zerrno, err);
  148. }
  149. if (zerrstr && errstr) {
  150. ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr);
  151. } else if (errstr) {
  152. zend_string_release_ex(errstr, 0);
  153. }
  154. RETURN_FALSE;
  155. }
  156. if (errstr) {
  157. zend_string_release_ex(errstr, 0);
  158. }
  159. php_stream_to_zval(stream, return_value);
  160. }
  161. /* }}} */
  162. /* {{{ Create a server socket bound to localaddress */
  163. PHP_FUNCTION(stream_socket_server)
  164. {
  165. char *host;
  166. size_t host_len;
  167. zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL;
  168. php_stream *stream = NULL;
  169. int err = 0;
  170. zend_long flags = STREAM_XPORT_BIND | STREAM_XPORT_LISTEN;
  171. zend_string *errstr = NULL;
  172. php_stream_context *context = NULL;
  173. RETVAL_FALSE;
  174. ZEND_PARSE_PARAMETERS_START(1, 5)
  175. Z_PARAM_STRING(host, host_len)
  176. Z_PARAM_OPTIONAL
  177. Z_PARAM_ZVAL(zerrno)
  178. Z_PARAM_ZVAL(zerrstr)
  179. Z_PARAM_LONG(flags)
  180. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  181. ZEND_PARSE_PARAMETERS_END();
  182. context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
  183. if (context) {
  184. GC_ADDREF(context->res);
  185. }
  186. if (zerrno) {
  187. ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
  188. }
  189. if (zerrstr) {
  190. ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr);
  191. }
  192. stream = php_stream_xport_create(host, host_len, REPORT_ERRORS,
  193. STREAM_XPORT_SERVER | (int)flags,
  194. NULL, NULL, context, &errstr, &err);
  195. if (stream == NULL) {
  196. php_error_docref(NULL, E_WARNING, "Unable to connect to %s (%s)", host, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
  197. }
  198. if (stream == NULL) {
  199. if (zerrno) {
  200. ZEND_TRY_ASSIGN_REF_LONG(zerrno, err);
  201. }
  202. if (zerrstr && errstr) {
  203. ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr);
  204. } else if (errstr) {
  205. zend_string_release_ex(errstr, 0);
  206. }
  207. RETURN_FALSE;
  208. }
  209. if (errstr) {
  210. zend_string_release_ex(errstr, 0);
  211. }
  212. php_stream_to_zval(stream, return_value);
  213. }
  214. /* }}} */
  215. /* {{{ Accept a client connection from a server socket */
  216. PHP_FUNCTION(stream_socket_accept)
  217. {
  218. double timeout;
  219. bool timeout_is_null = 1;
  220. zval *zpeername = NULL;
  221. zend_string *peername = NULL;
  222. php_timeout_ull conv;
  223. struct timeval tv;
  224. php_stream *stream = NULL, *clistream = NULL;
  225. zval *zstream;
  226. zend_string *errstr = NULL;
  227. ZEND_PARSE_PARAMETERS_START(1, 3)
  228. Z_PARAM_RESOURCE(zstream)
  229. Z_PARAM_OPTIONAL
  230. Z_PARAM_DOUBLE_OR_NULL(timeout, timeout_is_null)
  231. Z_PARAM_ZVAL(zpeername)
  232. ZEND_PARSE_PARAMETERS_END();
  233. if (timeout_is_null) {
  234. timeout = (double)FG(default_socket_timeout);
  235. }
  236. php_stream_from_zval(stream, zstream);
  237. /* prepare the timeout value for use */
  238. conv = (php_timeout_ull) (timeout * 1000000.0);
  239. #ifdef PHP_WIN32
  240. tv.tv_sec = (long)(conv / 1000000);
  241. tv.tv_usec = (long)(conv % 1000000);
  242. #else
  243. tv.tv_sec = conv / 1000000;
  244. tv.tv_usec = conv % 1000000;
  245. #endif
  246. if (0 == php_stream_xport_accept(stream, &clistream,
  247. zpeername ? &peername : NULL,
  248. NULL, NULL,
  249. &tv, &errstr
  250. ) && clistream) {
  251. if (peername) {
  252. ZEND_TRY_ASSIGN_REF_STR(zpeername, peername);
  253. }
  254. php_stream_to_zval(clistream, return_value);
  255. } else {
  256. if (peername) {
  257. zend_string_release(peername);
  258. }
  259. php_error_docref(NULL, E_WARNING, "Accept failed: %s", errstr ? ZSTR_VAL(errstr) : "Unknown error");
  260. RETVAL_FALSE;
  261. }
  262. if (errstr) {
  263. zend_string_release_ex(errstr, 0);
  264. }
  265. }
  266. /* }}} */
  267. /* {{{ Returns either the locally bound or remote name for a socket stream */
  268. PHP_FUNCTION(stream_socket_get_name)
  269. {
  270. php_stream *stream;
  271. zval *zstream;
  272. bool want_peer;
  273. zend_string *name = NULL;
  274. ZEND_PARSE_PARAMETERS_START(2, 2)
  275. Z_PARAM_RESOURCE(zstream)
  276. Z_PARAM_BOOL(want_peer)
  277. ZEND_PARSE_PARAMETERS_END();
  278. php_stream_from_zval(stream, zstream);
  279. if (0 != php_stream_xport_get_name(stream, want_peer,
  280. &name,
  281. NULL, NULL
  282. ) || !name) {
  283. RETURN_FALSE;
  284. }
  285. if ((ZSTR_LEN(name) == 0) || (ZSTR_VAL(name)[0] == 0)) {
  286. zend_string_release_ex(name, 0);
  287. RETURN_FALSE;
  288. }
  289. RETVAL_STR(name);
  290. }
  291. /* }}} */
  292. /* {{{ Send data to a socket stream. If target_addr is specified it must be in dotted quad (or [ipv6]) format */
  293. PHP_FUNCTION(stream_socket_sendto)
  294. {
  295. php_stream *stream;
  296. zval *zstream;
  297. zend_long flags = 0;
  298. char *data, *target_addr = NULL;
  299. size_t datalen, target_addr_len = 0;
  300. php_sockaddr_storage sa;
  301. socklen_t sl = 0;
  302. ZEND_PARSE_PARAMETERS_START(2, 4)
  303. Z_PARAM_RESOURCE(zstream)
  304. Z_PARAM_STRING(data, datalen)
  305. Z_PARAM_OPTIONAL
  306. Z_PARAM_LONG(flags)
  307. Z_PARAM_STRING(target_addr, target_addr_len)
  308. ZEND_PARSE_PARAMETERS_END();
  309. php_stream_from_zval(stream, zstream);
  310. if (target_addr_len) {
  311. /* parse the address */
  312. if (FAILURE == php_network_parse_network_address_with_port(target_addr, target_addr_len, (struct sockaddr*)&sa, &sl)) {
  313. php_error_docref(NULL, E_WARNING, "Failed to parse `%s' into a valid network address", target_addr);
  314. RETURN_FALSE;
  315. }
  316. }
  317. RETURN_LONG(php_stream_xport_sendto(stream, data, datalen, (int)flags, target_addr_len ? &sa : NULL, sl));
  318. }
  319. /* }}} */
  320. /* {{{ Receives data from a socket stream */
  321. PHP_FUNCTION(stream_socket_recvfrom)
  322. {
  323. php_stream *stream;
  324. zval *zstream, *zremote = NULL;
  325. zend_string *remote_addr = NULL;
  326. zend_long to_read = 0;
  327. zend_string *read_buf;
  328. zend_long flags = 0;
  329. int recvd;
  330. ZEND_PARSE_PARAMETERS_START(2, 4)
  331. Z_PARAM_RESOURCE(zstream)
  332. Z_PARAM_LONG(to_read)
  333. Z_PARAM_OPTIONAL
  334. Z_PARAM_LONG(flags)
  335. Z_PARAM_ZVAL(zremote)
  336. ZEND_PARSE_PARAMETERS_END();
  337. php_stream_from_zval(stream, zstream);
  338. if (zremote) {
  339. ZEND_TRY_ASSIGN_REF_NULL(zremote);
  340. }
  341. if (to_read <= 0) {
  342. zend_argument_value_error(2, "must be greater than 0");
  343. RETURN_THROWS();
  344. }
  345. read_buf = zend_string_alloc(to_read, 0);
  346. recvd = php_stream_xport_recvfrom(stream, ZSTR_VAL(read_buf), to_read, (int)flags, NULL, NULL,
  347. zremote ? &remote_addr : NULL
  348. );
  349. if (recvd >= 0) {
  350. if (zremote && remote_addr) {
  351. ZEND_TRY_ASSIGN_REF_STR(zremote, remote_addr);
  352. }
  353. ZSTR_VAL(read_buf)[recvd] = '\0';
  354. ZSTR_LEN(read_buf) = recvd;
  355. RETURN_NEW_STR(read_buf);
  356. }
  357. zend_string_efree(read_buf);
  358. RETURN_FALSE;
  359. }
  360. /* }}} */
  361. /* {{{ Reads all remaining bytes (or up to maxlen bytes) from a stream and returns them as a string. */
  362. PHP_FUNCTION(stream_get_contents)
  363. {
  364. php_stream *stream;
  365. zval *zsrc;
  366. zend_long maxlen, desiredpos = -1L;
  367. bool maxlen_is_null = 1;
  368. zend_string *contents;
  369. ZEND_PARSE_PARAMETERS_START(1, 3)
  370. Z_PARAM_RESOURCE(zsrc)
  371. Z_PARAM_OPTIONAL
  372. Z_PARAM_LONG_OR_NULL(maxlen, maxlen_is_null)
  373. Z_PARAM_LONG(desiredpos)
  374. ZEND_PARSE_PARAMETERS_END();
  375. if (maxlen_is_null) {
  376. maxlen = (ssize_t) PHP_STREAM_COPY_ALL;
  377. } else if (maxlen < 0 && maxlen != (ssize_t)PHP_STREAM_COPY_ALL) {
  378. zend_argument_value_error(2, "must be greater than or equal to -1");
  379. RETURN_THROWS();
  380. }
  381. php_stream_from_zval(stream, zsrc);
  382. if (desiredpos >= 0) {
  383. int seek_res = 0;
  384. zend_off_t position;
  385. position = php_stream_tell(stream);
  386. if (position >= 0 && desiredpos > position) {
  387. /* use SEEK_CUR to allow emulation in streams that don't support seeking */
  388. seek_res = php_stream_seek(stream, desiredpos - position, SEEK_CUR);
  389. } else if (desiredpos < position) {
  390. /* desired position before position or error on tell */
  391. seek_res = php_stream_seek(stream, desiredpos, SEEK_SET);
  392. }
  393. if (seek_res != 0) {
  394. php_error_docref(NULL, E_WARNING,
  395. "Failed to seek to position " ZEND_LONG_FMT " in the stream", desiredpos);
  396. RETURN_FALSE;
  397. }
  398. }
  399. if ((contents = php_stream_copy_to_mem(stream, maxlen, 0))) {
  400. RETURN_STR(contents);
  401. } else {
  402. RETURN_EMPTY_STRING();
  403. }
  404. }
  405. /* }}} */
  406. /* {{{ Reads up to maxlen bytes from source stream and writes them to dest stream. */
  407. PHP_FUNCTION(stream_copy_to_stream)
  408. {
  409. php_stream *src, *dest;
  410. zval *zsrc, *zdest;
  411. zend_long maxlen, pos = 0;
  412. bool maxlen_is_null = 1;
  413. size_t len;
  414. int ret;
  415. ZEND_PARSE_PARAMETERS_START(2, 4)
  416. Z_PARAM_RESOURCE(zsrc)
  417. Z_PARAM_RESOURCE(zdest)
  418. Z_PARAM_OPTIONAL
  419. Z_PARAM_LONG_OR_NULL(maxlen, maxlen_is_null)
  420. Z_PARAM_LONG(pos)
  421. ZEND_PARSE_PARAMETERS_END();
  422. if (maxlen_is_null) {
  423. maxlen = PHP_STREAM_COPY_ALL;
  424. }
  425. php_stream_from_zval(src, zsrc);
  426. php_stream_from_zval(dest, zdest);
  427. if (pos > 0 && php_stream_seek(src, pos, SEEK_SET) < 0) {
  428. php_error_docref(NULL, E_WARNING, "Failed to seek to position " ZEND_LONG_FMT " in the stream", pos);
  429. RETURN_FALSE;
  430. }
  431. ret = php_stream_copy_to_stream_ex(src, dest, maxlen, &len);
  432. if (ret != SUCCESS) {
  433. RETURN_FALSE;
  434. }
  435. RETURN_LONG(len);
  436. }
  437. /* }}} */
  438. /* {{{ Retrieves header/meta data from streams/file pointers */
  439. PHP_FUNCTION(stream_get_meta_data)
  440. {
  441. zval *zstream;
  442. php_stream *stream;
  443. ZEND_PARSE_PARAMETERS_START(1, 1)
  444. Z_PARAM_RESOURCE(zstream)
  445. ZEND_PARSE_PARAMETERS_END();
  446. php_stream_from_zval(stream, zstream);
  447. array_init(return_value);
  448. if (!php_stream_populate_meta_data(stream, return_value)) {
  449. add_assoc_bool(return_value, "timed_out", 0);
  450. add_assoc_bool(return_value, "blocked", 1);
  451. add_assoc_bool(return_value, "eof", php_stream_eof(stream));
  452. }
  453. if (!Z_ISUNDEF(stream->wrapperdata)) {
  454. Z_ADDREF_P(&stream->wrapperdata);
  455. add_assoc_zval(return_value, "wrapper_data", &stream->wrapperdata);
  456. }
  457. if (stream->wrapper) {
  458. add_assoc_string(return_value, "wrapper_type", (char *)stream->wrapper->wops->label);
  459. }
  460. add_assoc_string(return_value, "stream_type", (char *)stream->ops->label);
  461. add_assoc_string(return_value, "mode", stream->mode);
  462. #if 0 /* TODO: needs updating for new filter API */
  463. if (stream->filterhead) {
  464. php_stream_filter *filter;
  465. MAKE_STD_ZVAL(newval);
  466. array_init(newval);
  467. for (filter = stream->filterhead; filter != NULL; filter = filter->next) {
  468. add_next_index_string(newval, (char *)filter->fops->label);
  469. }
  470. add_assoc_zval(return_value, "filters", newval);
  471. }
  472. #endif
  473. add_assoc_long(return_value, "unread_bytes", stream->writepos - stream->readpos);
  474. add_assoc_bool(return_value, "seekable", (stream->ops->seek) && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0);
  475. if (stream->orig_path) {
  476. add_assoc_string(return_value, "uri", stream->orig_path);
  477. }
  478. }
  479. /* }}} */
  480. /* {{{ Retrieves list of registered socket transports */
  481. PHP_FUNCTION(stream_get_transports)
  482. {
  483. HashTable *stream_xport_hash;
  484. zend_string *stream_xport;
  485. ZEND_PARSE_PARAMETERS_NONE();
  486. stream_xport_hash = php_stream_xport_get_hash();
  487. array_init(return_value);
  488. ZEND_HASH_FOREACH_STR_KEY(stream_xport_hash, stream_xport) {
  489. add_next_index_str(return_value, zend_string_copy(stream_xport));
  490. } ZEND_HASH_FOREACH_END();
  491. }
  492. /* }}} */
  493. /* {{{ Retrieves list of registered stream wrappers */
  494. PHP_FUNCTION(stream_get_wrappers)
  495. {
  496. HashTable *url_stream_wrappers_hash;
  497. zend_string *stream_protocol;
  498. ZEND_PARSE_PARAMETERS_NONE();
  499. url_stream_wrappers_hash = php_stream_get_url_stream_wrappers_hash();
  500. array_init(return_value);
  501. ZEND_HASH_FOREACH_STR_KEY(url_stream_wrappers_hash, stream_protocol) {
  502. if (stream_protocol) {
  503. add_next_index_str(return_value, zend_string_copy(stream_protocol));
  504. }
  505. } ZEND_HASH_FOREACH_END();
  506. }
  507. /* }}} */
  508. /* {{{ stream_select related functions */
  509. static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t *max_fd)
  510. {
  511. zval *elem;
  512. php_stream *stream;
  513. int cnt = 0;
  514. if (Z_TYPE_P(stream_array) != IS_ARRAY) {
  515. return 0;
  516. }
  517. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(stream_array), elem) {
  518. /* Temporary int fd is needed for the STREAM data type on windows, passing this_fd directly to php_stream_cast()
  519. would eventually bring a wrong result on x64. php_stream_cast() casts to int internally, and this will leave
  520. the higher bits of a SOCKET variable uninitialized on systems with little endian. */
  521. php_socket_t this_fd;
  522. ZVAL_DEREF(elem);
  523. php_stream_from_zval_no_verify(stream, elem);
  524. if (stream == NULL) {
  525. continue;
  526. }
  527. /* get the fd.
  528. * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
  529. * when casting. It is only used here so that the buffered data warning
  530. * is not displayed.
  531. * */
  532. if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != -1) {
  533. PHP_SAFE_FD_SET(this_fd, fds);
  534. if (this_fd > *max_fd) {
  535. *max_fd = this_fd;
  536. }
  537. cnt++;
  538. }
  539. } ZEND_HASH_FOREACH_END();
  540. return cnt ? 1 : 0;
  541. }
  542. static int stream_array_from_fd_set(zval *stream_array, fd_set *fds)
  543. {
  544. zval *elem, *dest_elem;
  545. HashTable *ht;
  546. php_stream *stream;
  547. int ret = 0;
  548. zend_string *key;
  549. zend_ulong num_ind;
  550. if (Z_TYPE_P(stream_array) != IS_ARRAY) {
  551. return 0;
  552. }
  553. ht = zend_new_array(zend_hash_num_elements(Z_ARRVAL_P(stream_array)));
  554. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(stream_array), num_ind, key, elem) {
  555. php_socket_t this_fd;
  556. ZVAL_DEREF(elem);
  557. php_stream_from_zval_no_verify(stream, elem);
  558. if (stream == NULL) {
  559. continue;
  560. }
  561. /* get the fd
  562. * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
  563. * when casting. It is only used here so that the buffered data warning
  564. * is not displayed.
  565. */
  566. if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != SOCK_ERR) {
  567. if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
  568. if (!key) {
  569. dest_elem = zend_hash_index_update(ht, num_ind, elem);
  570. } else {
  571. dest_elem = zend_hash_update(ht, key, elem);
  572. }
  573. zval_add_ref(dest_elem);
  574. ret++;
  575. continue;
  576. }
  577. }
  578. } ZEND_HASH_FOREACH_END();
  579. /* destroy old array and add new one */
  580. zval_ptr_dtor(stream_array);
  581. ZVAL_ARR(stream_array, ht);
  582. return ret;
  583. }
  584. static int stream_array_emulate_read_fd_set(zval *stream_array)
  585. {
  586. zval *elem, *dest_elem;
  587. HashTable *ht;
  588. php_stream *stream;
  589. int ret = 0;
  590. zend_ulong num_ind;
  591. zend_string *key;
  592. if (Z_TYPE_P(stream_array) != IS_ARRAY) {
  593. return 0;
  594. }
  595. ht = zend_new_array(zend_hash_num_elements(Z_ARRVAL_P(stream_array)));
  596. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(stream_array), num_ind, key, elem) {
  597. ZVAL_DEREF(elem);
  598. php_stream_from_zval_no_verify(stream, elem);
  599. if (stream == NULL) {
  600. continue;
  601. }
  602. if ((stream->writepos - stream->readpos) > 0) {
  603. /* allow readable non-descriptor based streams to participate in stream_select.
  604. * Non-descriptor streams will only "work" if they have previously buffered the
  605. * data. Not ideal, but better than nothing.
  606. * This branch of code also allows blocking streams with buffered data to
  607. * operate correctly in stream_select.
  608. * */
  609. if (!key) {
  610. dest_elem = zend_hash_index_update(ht, num_ind, elem);
  611. } else {
  612. dest_elem = zend_hash_update(ht, key, elem);
  613. }
  614. zval_add_ref(dest_elem);
  615. ret++;
  616. continue;
  617. }
  618. } ZEND_HASH_FOREACH_END();
  619. if (ret > 0) {
  620. /* destroy old array and add new one */
  621. zval_ptr_dtor(stream_array);
  622. ZVAL_ARR(stream_array, ht);
  623. } else {
  624. zend_array_destroy(ht);
  625. }
  626. return ret;
  627. }
  628. /* }}} */
  629. /* {{{ Runs the select() system call on the sets of streams with a timeout specified by tv_sec and tv_usec */
  630. PHP_FUNCTION(stream_select)
  631. {
  632. zval *r_array, *w_array, *e_array;
  633. struct timeval tv, *tv_p = NULL;
  634. fd_set rfds, wfds, efds;
  635. php_socket_t max_fd = 0;
  636. int retval, sets = 0;
  637. zend_long sec, usec = 0;
  638. bool secnull;
  639. bool usecnull = 1;
  640. int set_count, max_set_count = 0;
  641. ZEND_PARSE_PARAMETERS_START(4, 5)
  642. Z_PARAM_ARRAY_EX2(r_array, 1, 1, 0)
  643. Z_PARAM_ARRAY_EX2(w_array, 1, 1, 0)
  644. Z_PARAM_ARRAY_EX2(e_array, 1, 1, 0)
  645. Z_PARAM_LONG_OR_NULL(sec, secnull)
  646. Z_PARAM_OPTIONAL
  647. Z_PARAM_LONG_OR_NULL(usec, usecnull)
  648. ZEND_PARSE_PARAMETERS_END();
  649. FD_ZERO(&rfds);
  650. FD_ZERO(&wfds);
  651. FD_ZERO(&efds);
  652. if (r_array != NULL) {
  653. set_count = stream_array_to_fd_set(r_array, &rfds, &max_fd);
  654. if (set_count > max_set_count)
  655. max_set_count = set_count;
  656. sets += set_count;
  657. }
  658. if (w_array != NULL) {
  659. set_count = stream_array_to_fd_set(w_array, &wfds, &max_fd);
  660. if (set_count > max_set_count)
  661. max_set_count = set_count;
  662. sets += set_count;
  663. }
  664. if (e_array != NULL) {
  665. set_count = stream_array_to_fd_set(e_array, &efds, &max_fd);
  666. if (set_count > max_set_count)
  667. max_set_count = set_count;
  668. sets += set_count;
  669. }
  670. if (!sets) {
  671. zend_value_error("No stream arrays were passed");
  672. RETURN_THROWS();
  673. }
  674. PHP_SAFE_MAX_FD(max_fd, max_set_count);
  675. if (secnull && !usecnull) {
  676. if (usec != 0) {
  677. zend_argument_value_error(5, "must be null when argument #4 ($seconds) is null");
  678. RETURN_THROWS();
  679. }
  680. }
  681. /* If seconds is not set to null, build the timeval, else we wait indefinitely */
  682. if (!secnull) {
  683. if (sec < 0) {
  684. zend_argument_value_error(4, "must be greater than or equal to 0");
  685. RETURN_THROWS();
  686. } else if (usec < 0) {
  687. zend_argument_value_error(5, "must be greater than or equal to 0");
  688. RETURN_THROWS();
  689. }
  690. /* Windows, Solaris and BSD do not like microsecond values which are >= 1 sec */
  691. tv.tv_sec = (long)(sec + (usec / 1000000));
  692. tv.tv_usec = (long)(usec % 1000000);
  693. tv_p = &tv;
  694. }
  695. /* slight hack to support buffered data; if there is data sitting in the
  696. * read buffer of any of the streams in the read array, let's pretend
  697. * that we selected, but return only the readable sockets */
  698. if (r_array != NULL) {
  699. retval = stream_array_emulate_read_fd_set(r_array);
  700. if (retval > 0) {
  701. if (w_array != NULL) {
  702. zval_ptr_dtor(w_array);
  703. ZVAL_EMPTY_ARRAY(w_array);
  704. }
  705. if (e_array != NULL) {
  706. zval_ptr_dtor(e_array);
  707. ZVAL_EMPTY_ARRAY(e_array);
  708. }
  709. RETURN_LONG(retval);
  710. }
  711. }
  712. retval = php_select(max_fd+1, &rfds, &wfds, &efds, tv_p);
  713. if (retval == -1) {
  714. php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)",
  715. errno, strerror(errno), max_fd);
  716. RETURN_FALSE;
  717. }
  718. if (r_array != NULL) stream_array_from_fd_set(r_array, &rfds);
  719. if (w_array != NULL) stream_array_from_fd_set(w_array, &wfds);
  720. if (e_array != NULL) stream_array_from_fd_set(e_array, &efds);
  721. RETURN_LONG(retval);
  722. }
  723. /* }}} */
  724. /* {{{ stream_context related functions */
  725. static void user_space_stream_notifier(php_stream_context *context, int notifycode, int severity,
  726. char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr)
  727. {
  728. zval *callback = &context->notifier->ptr;
  729. zval retval;
  730. zval zvs[6];
  731. int i;
  732. ZVAL_LONG(&zvs[0], notifycode);
  733. ZVAL_LONG(&zvs[1], severity);
  734. if (xmsg) {
  735. ZVAL_STRING(&zvs[2], xmsg);
  736. } else {
  737. ZVAL_NULL(&zvs[2]);
  738. }
  739. ZVAL_LONG(&zvs[3], xcode);
  740. ZVAL_LONG(&zvs[4], bytes_sofar);
  741. ZVAL_LONG(&zvs[5], bytes_max);
  742. if (FAILURE == call_user_function(NULL, NULL, callback, &retval, 6, zvs)) {
  743. php_error_docref(NULL, E_WARNING, "Failed to call user notifier");
  744. }
  745. for (i = 0; i < 6; i++) {
  746. zval_ptr_dtor(&zvs[i]);
  747. }
  748. zval_ptr_dtor(&retval);
  749. }
  750. static void user_space_stream_notifier_dtor(php_stream_notifier *notifier)
  751. {
  752. if (notifier && Z_TYPE(notifier->ptr) != IS_UNDEF) {
  753. zval_ptr_dtor(&notifier->ptr);
  754. ZVAL_UNDEF(&notifier->ptr);
  755. }
  756. }
  757. static int parse_context_options(php_stream_context *context, HashTable *options)
  758. {
  759. zval *wval, *oval;
  760. zend_string *wkey, *okey;
  761. int ret = SUCCESS;
  762. ZEND_HASH_FOREACH_STR_KEY_VAL(options, wkey, wval) {
  763. ZVAL_DEREF(wval);
  764. if (wkey && Z_TYPE_P(wval) == IS_ARRAY) {
  765. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(wval), okey, oval) {
  766. if (okey) {
  767. php_stream_context_set_option(context, ZSTR_VAL(wkey), ZSTR_VAL(okey), oval);
  768. }
  769. } ZEND_HASH_FOREACH_END();
  770. } else {
  771. zend_value_error("Options should have the form [\"wrappername\"][\"optionname\"] = $value");
  772. return FAILURE;
  773. }
  774. } ZEND_HASH_FOREACH_END();
  775. return ret;
  776. }
  777. static int parse_context_params(php_stream_context *context, HashTable *params)
  778. {
  779. int ret = SUCCESS;
  780. zval *tmp;
  781. if (NULL != (tmp = zend_hash_str_find(params, "notification", sizeof("notification")-1))) {
  782. if (context->notifier) {
  783. php_stream_notification_free(context->notifier);
  784. context->notifier = NULL;
  785. }
  786. context->notifier = php_stream_notification_alloc();
  787. context->notifier->func = user_space_stream_notifier;
  788. ZVAL_COPY(&context->notifier->ptr, tmp);
  789. context->notifier->dtor = user_space_stream_notifier_dtor;
  790. }
  791. if (NULL != (tmp = zend_hash_str_find(params, "options", sizeof("options")-1))) {
  792. if (Z_TYPE_P(tmp) == IS_ARRAY) {
  793. return parse_context_options(context, Z_ARRVAL_P(tmp));
  794. } else {
  795. zend_type_error("Invalid stream/context parameter");
  796. return FAILURE;
  797. }
  798. }
  799. return ret;
  800. }
  801. /* given a zval which is either a stream or a context, return the underlying
  802. * stream_context. If it is a stream that does not have a context assigned, it
  803. * will create and assign a context and return that. */
  804. static php_stream_context *decode_context_param(zval *contextresource)
  805. {
  806. php_stream_context *context = NULL;
  807. context = zend_fetch_resource_ex(contextresource, NULL, php_le_stream_context());
  808. if (context == NULL) {
  809. php_stream *stream;
  810. stream = zend_fetch_resource2_ex(contextresource, NULL, php_file_le_stream(), php_file_le_pstream());
  811. if (stream) {
  812. context = PHP_STREAM_CONTEXT(stream);
  813. if (context == NULL) {
  814. /* Only way this happens is if file is opened with NO_DEFAULT_CONTEXT
  815. param, but then something is called which requires a context.
  816. Don't give them the default one though since they already said they
  817. didn't want it. */
  818. context = php_stream_context_alloc();
  819. stream->ctx = context->res;
  820. }
  821. }
  822. }
  823. return context;
  824. }
  825. /* }}} */
  826. /* {{{ Retrieve options for a stream/wrapper/context */
  827. PHP_FUNCTION(stream_context_get_options)
  828. {
  829. zval *zcontext;
  830. php_stream_context *context;
  831. ZEND_PARSE_PARAMETERS_START(1, 1)
  832. Z_PARAM_RESOURCE(zcontext)
  833. ZEND_PARSE_PARAMETERS_END();
  834. context = decode_context_param(zcontext);
  835. if (!context) {
  836. zend_argument_type_error(1, "must be a valid stream/context");
  837. RETURN_THROWS();
  838. }
  839. ZVAL_COPY(return_value, &context->options);
  840. }
  841. /* }}} */
  842. /* {{{ Set an option for a wrapper */
  843. PHP_FUNCTION(stream_context_set_option)
  844. {
  845. zval *zcontext = NULL;
  846. php_stream_context *context;
  847. zend_string *wrappername;
  848. HashTable *options;
  849. char *optionname = NULL;
  850. size_t optionname_len;
  851. zval *zvalue = NULL;
  852. ZEND_PARSE_PARAMETERS_START(2, 4)
  853. Z_PARAM_RESOURCE(zcontext)
  854. Z_PARAM_ARRAY_HT_OR_STR(options, wrappername)
  855. Z_PARAM_OPTIONAL
  856. Z_PARAM_STRING_OR_NULL(optionname, optionname_len)
  857. Z_PARAM_ZVAL(zvalue)
  858. ZEND_PARSE_PARAMETERS_END();
  859. /* figure out where the context is coming from exactly */
  860. if (!(context = decode_context_param(zcontext))) {
  861. zend_argument_type_error(1, "must be a valid stream/context");
  862. RETURN_THROWS();
  863. }
  864. if (options) {
  865. if (optionname) {
  866. zend_argument_value_error(3, "must be null when argument #2 ($wrapper_or_options) is an array");
  867. RETURN_THROWS();
  868. }
  869. if (zvalue) {
  870. zend_argument_value_error(4, "cannot be provided when argument #2 ($wrapper_or_options) is an array");
  871. RETURN_THROWS();
  872. }
  873. RETURN_BOOL(parse_context_options(context, options) == SUCCESS);
  874. } else {
  875. if (!optionname) {
  876. zend_argument_value_error(3, "cannot be null when argument #2 ($wrapper_or_options) is a string");
  877. RETURN_THROWS();
  878. }
  879. if (!zvalue) {
  880. zend_argument_value_error(4, "must be provided when argument #2 ($wrapper_or_options) is a string");
  881. RETURN_THROWS();
  882. }
  883. RETURN_BOOL(php_stream_context_set_option(context, ZSTR_VAL(wrappername), optionname, zvalue) == SUCCESS);
  884. }
  885. }
  886. /* }}} */
  887. /* {{{ Set parameters for a file context */
  888. PHP_FUNCTION(stream_context_set_params)
  889. {
  890. HashTable *params;
  891. zval *zcontext;
  892. php_stream_context *context;
  893. ZEND_PARSE_PARAMETERS_START(2, 2)
  894. Z_PARAM_RESOURCE(zcontext)
  895. Z_PARAM_ARRAY_HT(params)
  896. ZEND_PARSE_PARAMETERS_END();
  897. context = decode_context_param(zcontext);
  898. if (!context) {
  899. zend_argument_type_error(1, "must be a valid stream/context");
  900. RETURN_THROWS();
  901. }
  902. RETVAL_BOOL(parse_context_params(context, params) == SUCCESS);
  903. }
  904. /* }}} */
  905. /* {{{ Get parameters of a file context */
  906. PHP_FUNCTION(stream_context_get_params)
  907. {
  908. zval *zcontext;
  909. php_stream_context *context;
  910. ZEND_PARSE_PARAMETERS_START(1, 1)
  911. Z_PARAM_RESOURCE(zcontext)
  912. ZEND_PARSE_PARAMETERS_END();
  913. context = decode_context_param(zcontext);
  914. if (!context) {
  915. zend_argument_type_error(1, "must be a valid stream/context");
  916. RETURN_THROWS();
  917. }
  918. array_init(return_value);
  919. if (context->notifier && Z_TYPE(context->notifier->ptr) != IS_UNDEF && context->notifier->func == user_space_stream_notifier) {
  920. Z_TRY_ADDREF(context->notifier->ptr);
  921. add_assoc_zval_ex(return_value, "notification", sizeof("notification")-1, &context->notifier->ptr);
  922. }
  923. Z_TRY_ADDREF(context->options);
  924. add_assoc_zval_ex(return_value, "options", sizeof("options")-1, &context->options);
  925. }
  926. /* }}} */
  927. /* {{{ Get a handle on the default file/stream context and optionally set parameters */
  928. PHP_FUNCTION(stream_context_get_default)
  929. {
  930. HashTable *params = NULL;
  931. php_stream_context *context;
  932. ZEND_PARSE_PARAMETERS_START(0, 1)
  933. Z_PARAM_OPTIONAL
  934. Z_PARAM_ARRAY_HT_OR_NULL(params)
  935. ZEND_PARSE_PARAMETERS_END();
  936. if (FG(default_context) == NULL) {
  937. FG(default_context) = php_stream_context_alloc();
  938. }
  939. context = FG(default_context);
  940. if (params) {
  941. if (parse_context_options(context, params) == FAILURE) {
  942. RETURN_THROWS();
  943. }
  944. }
  945. php_stream_context_to_zval(context, return_value);
  946. }
  947. /* }}} */
  948. /* {{{ Set default file/stream context, returns the context as a resource */
  949. PHP_FUNCTION(stream_context_set_default)
  950. {
  951. HashTable *options;
  952. php_stream_context *context;
  953. ZEND_PARSE_PARAMETERS_START(1, 1)
  954. Z_PARAM_ARRAY_HT(options)
  955. ZEND_PARSE_PARAMETERS_END();
  956. if (FG(default_context) == NULL) {
  957. FG(default_context) = php_stream_context_alloc();
  958. }
  959. context = FG(default_context);
  960. if (parse_context_options(context, options) == FAILURE) {
  961. RETURN_THROWS();
  962. }
  963. php_stream_context_to_zval(context, return_value);
  964. }
  965. /* }}} */
  966. /* {{{ Create a file context and optionally set parameters */
  967. PHP_FUNCTION(stream_context_create)
  968. {
  969. HashTable *options = NULL;
  970. HashTable *params = NULL;
  971. php_stream_context *context;
  972. ZEND_PARSE_PARAMETERS_START(0, 2)
  973. Z_PARAM_OPTIONAL
  974. Z_PARAM_ARRAY_HT_OR_NULL(options)
  975. Z_PARAM_ARRAY_HT_OR_NULL(params)
  976. ZEND_PARSE_PARAMETERS_END();
  977. context = php_stream_context_alloc();
  978. if (options) {
  979. parse_context_options(context, options);
  980. }
  981. if (params) {
  982. parse_context_params(context, params);
  983. }
  984. RETURN_RES(context->res);
  985. }
  986. /* }}} */
  987. /* {{{ streams filter functions */
  988. static void apply_filter_to_stream(int append, INTERNAL_FUNCTION_PARAMETERS)
  989. {
  990. zval *zstream;
  991. php_stream *stream;
  992. char *filtername;
  993. size_t filternamelen;
  994. zend_long read_write = 0;
  995. zval *filterparams = NULL;
  996. php_stream_filter *filter = NULL;
  997. int ret;
  998. ZEND_PARSE_PARAMETERS_START(2, 4)
  999. Z_PARAM_RESOURCE(zstream)
  1000. Z_PARAM_STRING(filtername, filternamelen)
  1001. Z_PARAM_OPTIONAL
  1002. Z_PARAM_LONG(read_write)
  1003. Z_PARAM_ZVAL(filterparams)
  1004. ZEND_PARSE_PARAMETERS_END();
  1005. php_stream_from_zval(stream, zstream);
  1006. if ((read_write & PHP_STREAM_FILTER_ALL) == 0) {
  1007. /* Chain not specified.
  1008. * Examine stream->mode to determine which filters are needed
  1009. * There's no harm in attaching a filter to an unused chain,
  1010. * but why waste the memory and clock cycles?
  1011. */
  1012. if (strchr(stream->mode, 'r') || strchr(stream->mode, '+')) {
  1013. read_write |= PHP_STREAM_FILTER_READ;
  1014. }
  1015. if (strchr(stream->mode, 'w') || strchr(stream->mode, '+') || strchr(stream->mode, 'a')) {
  1016. read_write |= PHP_STREAM_FILTER_WRITE;
  1017. }
  1018. }
  1019. if (read_write & PHP_STREAM_FILTER_READ) {
  1020. filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream));
  1021. if (filter == NULL) {
  1022. RETURN_FALSE;
  1023. }
  1024. if (append) {
  1025. ret = php_stream_filter_append_ex(&stream->readfilters, filter);
  1026. } else {
  1027. ret = php_stream_filter_prepend_ex(&stream->readfilters, filter);
  1028. }
  1029. if (ret != SUCCESS) {
  1030. php_stream_filter_remove(filter, 1);
  1031. RETURN_FALSE;
  1032. }
  1033. }
  1034. if (read_write & PHP_STREAM_FILTER_WRITE) {
  1035. filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream));
  1036. if (filter == NULL) {
  1037. RETURN_FALSE;
  1038. }
  1039. if (append) {
  1040. ret = php_stream_filter_append_ex(&stream->writefilters, filter);
  1041. } else {
  1042. ret = php_stream_filter_prepend_ex(&stream->writefilters, filter);
  1043. }
  1044. if (ret != SUCCESS) {
  1045. php_stream_filter_remove(filter, 1);
  1046. RETURN_FALSE;
  1047. }
  1048. }
  1049. if (filter) {
  1050. filter->res = zend_register_resource(filter, php_file_le_stream_filter());
  1051. GC_ADDREF(filter->res);
  1052. RETURN_RES(filter->res);
  1053. } else {
  1054. RETURN_FALSE;
  1055. }
  1056. }
  1057. /* }}} */
  1058. /* {{{ Prepend a filter to a stream */
  1059. PHP_FUNCTION(stream_filter_prepend)
  1060. {
  1061. apply_filter_to_stream(0, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1062. }
  1063. /* }}} */
  1064. /* {{{ Append a filter to a stream */
  1065. PHP_FUNCTION(stream_filter_append)
  1066. {
  1067. apply_filter_to_stream(1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1068. }
  1069. /* }}} */
  1070. /* {{{ Flushes any data in the filter's internal buffer, removes it from the chain, and frees the resource */
  1071. PHP_FUNCTION(stream_filter_remove)
  1072. {
  1073. zval *zfilter;
  1074. php_stream_filter *filter;
  1075. ZEND_PARSE_PARAMETERS_START(1, 1)
  1076. Z_PARAM_RESOURCE(zfilter)
  1077. ZEND_PARSE_PARAMETERS_END();
  1078. filter = zend_fetch_resource(Z_RES_P(zfilter), "stream filter", php_file_le_stream_filter());
  1079. if (!filter) {
  1080. RETURN_THROWS();
  1081. }
  1082. if (php_stream_filter_flush(filter, 1) == FAILURE) {
  1083. php_error_docref(NULL, E_WARNING, "Unable to flush filter, not removing");
  1084. RETURN_FALSE;
  1085. }
  1086. zend_list_close(Z_RES_P(zfilter));
  1087. php_stream_filter_remove(filter, 1);
  1088. RETURN_TRUE;
  1089. }
  1090. /* }}} */
  1091. /* {{{ Read up to maxlen bytes from a stream or until the ending string is found */
  1092. PHP_FUNCTION(stream_get_line)
  1093. {
  1094. char *str = NULL;
  1095. size_t str_len = 0;
  1096. zend_long max_length;
  1097. zval *zstream;
  1098. zend_string *buf;
  1099. php_stream *stream;
  1100. ZEND_PARSE_PARAMETERS_START(2, 3)
  1101. Z_PARAM_RESOURCE(zstream)
  1102. Z_PARAM_LONG(max_length)
  1103. Z_PARAM_OPTIONAL
  1104. Z_PARAM_STRING(str, str_len)
  1105. ZEND_PARSE_PARAMETERS_END();
  1106. if (max_length < 0) {
  1107. zend_argument_value_error(2, "must be greater than or equal to 0");
  1108. RETURN_THROWS();
  1109. }
  1110. if (!max_length) {
  1111. max_length = PHP_SOCK_CHUNK_SIZE;
  1112. }
  1113. php_stream_from_zval(stream, zstream);
  1114. if ((buf = php_stream_get_record(stream, max_length, str, str_len))) {
  1115. RETURN_STR(buf);
  1116. } else {
  1117. RETURN_FALSE;
  1118. }
  1119. }
  1120. /* }}} */
  1121. /* {{{ Set blocking/non-blocking mode on a socket or stream */
  1122. PHP_FUNCTION(stream_set_blocking)
  1123. {
  1124. zval *zstream;
  1125. bool block;
  1126. php_stream *stream;
  1127. ZEND_PARSE_PARAMETERS_START(2, 2)
  1128. Z_PARAM_RESOURCE(zstream)
  1129. Z_PARAM_BOOL(block)
  1130. ZEND_PARSE_PARAMETERS_END();
  1131. php_stream_from_zval(stream, zstream);
  1132. if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, block, NULL) == -1) {
  1133. RETURN_FALSE;
  1134. }
  1135. RETURN_TRUE;
  1136. }
  1137. /* }}} */
  1138. /* {{{ Set timeout on stream read to seconds + microseonds */
  1139. #if HAVE_SYS_TIME_H || defined(PHP_WIN32)
  1140. PHP_FUNCTION(stream_set_timeout)
  1141. {
  1142. zval *socket;
  1143. zend_long seconds, microseconds = 0;
  1144. struct timeval t;
  1145. php_stream *stream;
  1146. int argc = ZEND_NUM_ARGS();
  1147. ZEND_PARSE_PARAMETERS_START(2, 3)
  1148. Z_PARAM_RESOURCE(socket)
  1149. Z_PARAM_LONG(seconds)
  1150. Z_PARAM_OPTIONAL
  1151. Z_PARAM_LONG(microseconds)
  1152. ZEND_PARSE_PARAMETERS_END();
  1153. php_stream_from_zval(stream, socket);
  1154. #ifdef PHP_WIN32
  1155. t.tv_sec = (long)seconds;
  1156. if (argc == 3) {
  1157. t.tv_usec = (long)(microseconds % 1000000);
  1158. t.tv_sec +=(long)(microseconds / 1000000);
  1159. } else {
  1160. t.tv_usec = 0;
  1161. }
  1162. #else
  1163. t.tv_sec = seconds;
  1164. if (argc == 3) {
  1165. t.tv_usec = microseconds % 1000000;
  1166. t.tv_sec += microseconds / 1000000;
  1167. } else {
  1168. t.tv_usec = 0;
  1169. }
  1170. #endif
  1171. if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &t)) {
  1172. RETURN_TRUE;
  1173. }
  1174. RETURN_FALSE;
  1175. }
  1176. #endif /* HAVE_SYS_TIME_H || defined(PHP_WIN32) */
  1177. /* }}} */
  1178. /* {{{ Set file write buffer */
  1179. PHP_FUNCTION(stream_set_write_buffer)
  1180. {
  1181. zval *arg1;
  1182. int ret;
  1183. zend_long arg2;
  1184. size_t buff;
  1185. php_stream *stream;
  1186. ZEND_PARSE_PARAMETERS_START(2, 2)
  1187. Z_PARAM_RESOURCE(arg1)
  1188. Z_PARAM_LONG(arg2)
  1189. ZEND_PARSE_PARAMETERS_END();
  1190. php_stream_from_zval(stream, arg1);
  1191. buff = arg2;
  1192. /* if buff is 0 then set to non-buffered */
  1193. if (buff == 0) {
  1194. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
  1195. } else {
  1196. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buff);
  1197. }
  1198. RETURN_LONG(ret == 0 ? 0 : EOF);
  1199. }
  1200. /* }}} */
  1201. /* {{{ Set the stream chunk size */
  1202. PHP_FUNCTION(stream_set_chunk_size)
  1203. {
  1204. int ret;
  1205. zend_long csize;
  1206. zval *zstream;
  1207. php_stream *stream;
  1208. ZEND_PARSE_PARAMETERS_START(2, 2)
  1209. Z_PARAM_RESOURCE(zstream)
  1210. Z_PARAM_LONG(csize)
  1211. ZEND_PARSE_PARAMETERS_END();
  1212. if (csize <= 0) {
  1213. zend_argument_value_error(2, "must be greater than 0");
  1214. RETURN_THROWS();
  1215. }
  1216. /* stream.chunk_size is actually a size_t, but php_stream_set_option
  1217. * can only use an int to accept the new value and return the old one.
  1218. * In any case, values larger than INT_MAX for a chunk size make no sense.
  1219. */
  1220. if (csize > INT_MAX) {
  1221. zend_argument_value_error(2, "is too large");
  1222. RETURN_THROWS();
  1223. }
  1224. php_stream_from_zval(stream, zstream);
  1225. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_SET_CHUNK_SIZE, (int)csize, NULL);
  1226. RETURN_LONG(ret > 0 ? (zend_long)ret : (zend_long)EOF);
  1227. }
  1228. /* }}} */
  1229. /* {{{ Set file read buffer */
  1230. PHP_FUNCTION(stream_set_read_buffer)
  1231. {
  1232. zval *arg1;
  1233. int ret;
  1234. zend_long arg2;
  1235. size_t buff;
  1236. php_stream *stream;
  1237. ZEND_PARSE_PARAMETERS_START(2, 2)
  1238. Z_PARAM_RESOURCE(arg1)
  1239. Z_PARAM_LONG(arg2)
  1240. ZEND_PARSE_PARAMETERS_END();
  1241. php_stream_from_zval(stream, arg1);
  1242. buff = arg2;
  1243. /* if buff is 0 then set to non-buffered */
  1244. if (buff == 0) {
  1245. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
  1246. } else {
  1247. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_FULL, &buff);
  1248. }
  1249. RETURN_LONG(ret == 0 ? 0 : EOF);
  1250. }
  1251. /* }}} */
  1252. /* {{{ Enable or disable a specific kind of crypto on the stream */
  1253. PHP_FUNCTION(stream_socket_enable_crypto)
  1254. {
  1255. zend_long cryptokind = 0;
  1256. zval *zstream, *zsessstream = NULL;
  1257. php_stream *stream, *sessstream = NULL;
  1258. bool enable, cryptokindnull = 1;
  1259. int ret;
  1260. ZEND_PARSE_PARAMETERS_START(2, 4)
  1261. Z_PARAM_RESOURCE(zstream)
  1262. Z_PARAM_BOOL(enable)
  1263. Z_PARAM_OPTIONAL
  1264. Z_PARAM_LONG_OR_NULL(cryptokind, cryptokindnull)
  1265. Z_PARAM_RESOURCE_OR_NULL(zsessstream)
  1266. ZEND_PARSE_PARAMETERS_END();
  1267. php_stream_from_zval(stream, zstream);
  1268. if (enable) {
  1269. if (cryptokindnull) {
  1270. zval *val;
  1271. if (!GET_CTX_OPT(stream, "ssl", "crypto_method", val)) {
  1272. zend_argument_value_error(3, "must be specified when enabling encryption");
  1273. RETURN_THROWS();
  1274. }
  1275. cryptokind = Z_LVAL_P(val);
  1276. }
  1277. if (zsessstream) {
  1278. php_stream_from_zval(sessstream, zsessstream);
  1279. }
  1280. if (php_stream_xport_crypto_setup(stream, cryptokind, sessstream) < 0) {
  1281. RETURN_FALSE;
  1282. }
  1283. }
  1284. ret = php_stream_xport_crypto_enable(stream, enable);
  1285. switch (ret) {
  1286. case -1:
  1287. RETURN_FALSE;
  1288. case 0:
  1289. RETURN_LONG(0);
  1290. default:
  1291. RETURN_TRUE;
  1292. }
  1293. }
  1294. /* }}} */
  1295. /* {{{ Determine what file will be opened by calls to fopen() with a relative path */
  1296. PHP_FUNCTION(stream_resolve_include_path)
  1297. {
  1298. zend_string *filename;
  1299. zend_string *resolved_path;
  1300. ZEND_PARSE_PARAMETERS_START(1, 1)
  1301. Z_PARAM_PATH_STR(filename)
  1302. ZEND_PARSE_PARAMETERS_END();
  1303. resolved_path = zend_resolve_path(filename);
  1304. if (resolved_path) {
  1305. RETURN_STR(resolved_path);
  1306. }
  1307. RETURN_FALSE;
  1308. }
  1309. /* }}} */
  1310. /* {{{ */
  1311. PHP_FUNCTION(stream_is_local)
  1312. {
  1313. zval *zstream;
  1314. php_stream *stream = NULL;
  1315. php_stream_wrapper *wrapper = NULL;
  1316. ZEND_PARSE_PARAMETERS_START(1, 1)
  1317. Z_PARAM_ZVAL(zstream)
  1318. ZEND_PARSE_PARAMETERS_END();
  1319. if (Z_TYPE_P(zstream) == IS_RESOURCE) {
  1320. php_stream_from_zval(stream, zstream);
  1321. if (stream == NULL) {
  1322. RETURN_FALSE;
  1323. }
  1324. wrapper = stream->wrapper;
  1325. } else {
  1326. if (!try_convert_to_string(zstream)) {
  1327. RETURN_THROWS();
  1328. }
  1329. wrapper = php_stream_locate_url_wrapper(Z_STRVAL_P(zstream), NULL, 0);
  1330. }
  1331. if (!wrapper) {
  1332. RETURN_FALSE;
  1333. }
  1334. RETURN_BOOL(wrapper->is_url==0);
  1335. }
  1336. /* }}} */
  1337. /* {{{ Tells whether the stream supports locking through flock(). */
  1338. PHP_FUNCTION(stream_supports_lock)
  1339. {
  1340. php_stream *stream;
  1341. zval *zsrc;
  1342. ZEND_PARSE_PARAMETERS_START(1, 1)
  1343. Z_PARAM_RESOURCE(zsrc)
  1344. ZEND_PARSE_PARAMETERS_END();
  1345. php_stream_from_zval(stream, zsrc);
  1346. if (!php_stream_supports_lock(stream)) {
  1347. RETURN_FALSE;
  1348. }
  1349. RETURN_TRUE;
  1350. }
  1351. /* {{{ Check if a stream is a TTY. */
  1352. PHP_FUNCTION(stream_isatty)
  1353. {
  1354. zval *zsrc;
  1355. php_stream *stream;
  1356. php_socket_t fileno;
  1357. ZEND_PARSE_PARAMETERS_START(1, 1)
  1358. Z_PARAM_RESOURCE(zsrc)
  1359. ZEND_PARSE_PARAMETERS_END();
  1360. php_stream_from_zval(stream, zsrc);
  1361. if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
  1362. php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
  1363. } else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
  1364. php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
  1365. } else {
  1366. RETURN_FALSE;
  1367. }
  1368. #ifdef PHP_WIN32
  1369. /* Check if the Windows standard handle is redirected to file */
  1370. RETVAL_BOOL(php_win32_console_fileno_is_console(fileno));
  1371. #elif HAVE_UNISTD_H
  1372. /* Check if the file descriptor identifier is a terminal */
  1373. RETVAL_BOOL(isatty(fileno));
  1374. #else
  1375. {
  1376. zend_stat_t stat = {0};
  1377. RETVAL_BOOL(zend_fstat(fileno, &stat) == 0 && (stat.st_mode & /*S_IFMT*/0170000) == /*S_IFCHR*/0020000);
  1378. }
  1379. #endif
  1380. }
  1381. #ifdef PHP_WIN32
  1382. /* {{{ Get or set VT100 support for the specified stream associated to an
  1383. output buffer of a Windows console.
  1384. */
  1385. PHP_FUNCTION(sapi_windows_vt100_support)
  1386. {
  1387. zval *zsrc;
  1388. php_stream *stream;
  1389. bool enable, enable_is_null = 1;
  1390. zend_long fileno;
  1391. ZEND_PARSE_PARAMETERS_START(1, 2)
  1392. Z_PARAM_RESOURCE(zsrc)
  1393. Z_PARAM_OPTIONAL
  1394. Z_PARAM_BOOL_OR_NULL(enable, enable_is_null)
  1395. ZEND_PARSE_PARAMETERS_END();
  1396. php_stream_from_zval(stream, zsrc);
  1397. if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
  1398. php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
  1399. }
  1400. else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
  1401. php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
  1402. }
  1403. else {
  1404. if (!enable_is_null) {
  1405. php_error_docref(
  1406. NULL,
  1407. E_WARNING,
  1408. "not able to analyze the specified stream"
  1409. );
  1410. }
  1411. RETURN_FALSE;
  1412. }
  1413. /* Check if the file descriptor is a console */
  1414. if (!php_win32_console_fileno_is_console(fileno)) {
  1415. RETURN_FALSE;
  1416. }
  1417. if (enable_is_null) {
  1418. /* Check if the Windows standard handle has VT100 control codes enabled */
  1419. if (php_win32_console_fileno_has_vt100(fileno)) {
  1420. RETURN_TRUE;
  1421. }
  1422. else {
  1423. RETURN_FALSE;
  1424. }
  1425. }
  1426. else {
  1427. /* Enable/disable VT100 control codes support for the specified Windows standard handle */
  1428. if (php_win32_console_fileno_set_vt100(fileno, enable ? TRUE : FALSE)) {
  1429. RETURN_TRUE;
  1430. }
  1431. else {
  1432. RETURN_FALSE;
  1433. }
  1434. }
  1435. }
  1436. #endif
  1437. #ifdef HAVE_SHUTDOWN
  1438. /* {{{ causes all or part of a full-duplex connection on the socket associated
  1439. with stream to be shut down. If how is SHUT_RD, further receptions will
  1440. be disallowed. If how is SHUT_WR, further transmissions will be disallowed.
  1441. If how is SHUT_RDWR, further receptions and transmissions will be
  1442. disallowed. */
  1443. PHP_FUNCTION(stream_socket_shutdown)
  1444. {
  1445. zend_long how;
  1446. zval *zstream;
  1447. php_stream *stream;
  1448. ZEND_PARSE_PARAMETERS_START(2, 2)
  1449. Z_PARAM_RESOURCE(zstream)
  1450. Z_PARAM_LONG(how)
  1451. ZEND_PARSE_PARAMETERS_END();
  1452. if (how != STREAM_SHUT_RD &&
  1453. how != STREAM_SHUT_WR &&
  1454. how != STREAM_SHUT_RDWR) {
  1455. zend_argument_value_error(2, "must be one of STREAM_SHUT_RD, STREAM_SHUT_WR, or STREAM_SHUT_RDWR");
  1456. RETURN_THROWS();
  1457. }
  1458. php_stream_from_zval(stream, zstream);
  1459. RETURN_BOOL(php_stream_xport_shutdown(stream, (stream_shutdown_t)how) == 0);
  1460. }
  1461. /* }}} */
  1462. #endif