streamsfuncs.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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. if (!PHP_SAFE_MAX_FD(max_fd, max_set_count)) {
  675. RETURN_FALSE;
  676. }
  677. if (secnull && !usecnull) {
  678. if (usec != 0) {
  679. zend_argument_value_error(5, "must be null when argument #4 ($seconds) is null");
  680. RETURN_THROWS();
  681. }
  682. }
  683. /* If seconds is not set to null, build the timeval, else we wait indefinitely */
  684. if (!secnull) {
  685. if (sec < 0) {
  686. zend_argument_value_error(4, "must be greater than or equal to 0");
  687. RETURN_THROWS();
  688. } else if (usec < 0) {
  689. zend_argument_value_error(5, "must be greater than or equal to 0");
  690. RETURN_THROWS();
  691. }
  692. /* Windows, Solaris and BSD do not like microsecond values which are >= 1 sec */
  693. tv.tv_sec = (long)(sec + (usec / 1000000));
  694. tv.tv_usec = (long)(usec % 1000000);
  695. tv_p = &tv;
  696. }
  697. /* slight hack to support buffered data; if there is data sitting in the
  698. * read buffer of any of the streams in the read array, let's pretend
  699. * that we selected, but return only the readable sockets */
  700. if (r_array != NULL) {
  701. retval = stream_array_emulate_read_fd_set(r_array);
  702. if (retval > 0) {
  703. if (w_array != NULL) {
  704. zval_ptr_dtor(w_array);
  705. ZVAL_EMPTY_ARRAY(w_array);
  706. }
  707. if (e_array != NULL) {
  708. zval_ptr_dtor(e_array);
  709. ZVAL_EMPTY_ARRAY(e_array);
  710. }
  711. RETURN_LONG(retval);
  712. }
  713. }
  714. retval = php_select(max_fd+1, &rfds, &wfds, &efds, tv_p);
  715. if (retval == -1) {
  716. php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)",
  717. errno, strerror(errno), max_fd);
  718. RETURN_FALSE;
  719. }
  720. if (r_array != NULL) stream_array_from_fd_set(r_array, &rfds);
  721. if (w_array != NULL) stream_array_from_fd_set(w_array, &wfds);
  722. if (e_array != NULL) stream_array_from_fd_set(e_array, &efds);
  723. RETURN_LONG(retval);
  724. }
  725. /* }}} */
  726. /* {{{ stream_context related functions */
  727. static void user_space_stream_notifier(php_stream_context *context, int notifycode, int severity,
  728. char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr)
  729. {
  730. zval *callback = &context->notifier->ptr;
  731. zval retval;
  732. zval zvs[6];
  733. int i;
  734. ZVAL_LONG(&zvs[0], notifycode);
  735. ZVAL_LONG(&zvs[1], severity);
  736. if (xmsg) {
  737. ZVAL_STRING(&zvs[2], xmsg);
  738. } else {
  739. ZVAL_NULL(&zvs[2]);
  740. }
  741. ZVAL_LONG(&zvs[3], xcode);
  742. ZVAL_LONG(&zvs[4], bytes_sofar);
  743. ZVAL_LONG(&zvs[5], bytes_max);
  744. if (FAILURE == call_user_function(NULL, NULL, callback, &retval, 6, zvs)) {
  745. php_error_docref(NULL, E_WARNING, "Failed to call user notifier");
  746. }
  747. for (i = 0; i < 6; i++) {
  748. zval_ptr_dtor(&zvs[i]);
  749. }
  750. zval_ptr_dtor(&retval);
  751. }
  752. static void user_space_stream_notifier_dtor(php_stream_notifier *notifier)
  753. {
  754. if (notifier && Z_TYPE(notifier->ptr) != IS_UNDEF) {
  755. zval_ptr_dtor(&notifier->ptr);
  756. ZVAL_UNDEF(&notifier->ptr);
  757. }
  758. }
  759. static int parse_context_options(php_stream_context *context, HashTable *options)
  760. {
  761. zval *wval, *oval;
  762. zend_string *wkey, *okey;
  763. int ret = SUCCESS;
  764. ZEND_HASH_FOREACH_STR_KEY_VAL(options, wkey, wval) {
  765. ZVAL_DEREF(wval);
  766. if (wkey && Z_TYPE_P(wval) == IS_ARRAY) {
  767. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(wval), okey, oval) {
  768. if (okey) {
  769. php_stream_context_set_option(context, ZSTR_VAL(wkey), ZSTR_VAL(okey), oval);
  770. }
  771. } ZEND_HASH_FOREACH_END();
  772. } else {
  773. zend_value_error("Options should have the form [\"wrappername\"][\"optionname\"] = $value");
  774. return FAILURE;
  775. }
  776. } ZEND_HASH_FOREACH_END();
  777. return ret;
  778. }
  779. static int parse_context_params(php_stream_context *context, HashTable *params)
  780. {
  781. int ret = SUCCESS;
  782. zval *tmp;
  783. if (NULL != (tmp = zend_hash_str_find(params, "notification", sizeof("notification")-1))) {
  784. if (context->notifier) {
  785. php_stream_notification_free(context->notifier);
  786. context->notifier = NULL;
  787. }
  788. context->notifier = php_stream_notification_alloc();
  789. context->notifier->func = user_space_stream_notifier;
  790. ZVAL_COPY(&context->notifier->ptr, tmp);
  791. context->notifier->dtor = user_space_stream_notifier_dtor;
  792. }
  793. if (NULL != (tmp = zend_hash_str_find(params, "options", sizeof("options")-1))) {
  794. if (Z_TYPE_P(tmp) == IS_ARRAY) {
  795. return parse_context_options(context, Z_ARRVAL_P(tmp));
  796. } else {
  797. zend_type_error("Invalid stream/context parameter");
  798. return FAILURE;
  799. }
  800. }
  801. return ret;
  802. }
  803. /* given a zval which is either a stream or a context, return the underlying
  804. * stream_context. If it is a stream that does not have a context assigned, it
  805. * will create and assign a context and return that. */
  806. static php_stream_context *decode_context_param(zval *contextresource)
  807. {
  808. php_stream_context *context = NULL;
  809. context = zend_fetch_resource_ex(contextresource, NULL, php_le_stream_context());
  810. if (context == NULL) {
  811. php_stream *stream;
  812. stream = zend_fetch_resource2_ex(contextresource, NULL, php_file_le_stream(), php_file_le_pstream());
  813. if (stream) {
  814. context = PHP_STREAM_CONTEXT(stream);
  815. if (context == NULL) {
  816. /* Only way this happens is if file is opened with NO_DEFAULT_CONTEXT
  817. param, but then something is called which requires a context.
  818. Don't give them the default one though since they already said they
  819. didn't want it. */
  820. context = php_stream_context_alloc();
  821. stream->ctx = context->res;
  822. }
  823. }
  824. }
  825. return context;
  826. }
  827. /* }}} */
  828. /* {{{ Retrieve options for a stream/wrapper/context */
  829. PHP_FUNCTION(stream_context_get_options)
  830. {
  831. zval *zcontext;
  832. php_stream_context *context;
  833. ZEND_PARSE_PARAMETERS_START(1, 1)
  834. Z_PARAM_RESOURCE(zcontext)
  835. ZEND_PARSE_PARAMETERS_END();
  836. context = decode_context_param(zcontext);
  837. if (!context) {
  838. zend_argument_type_error(1, "must be a valid stream/context");
  839. RETURN_THROWS();
  840. }
  841. ZVAL_COPY(return_value, &context->options);
  842. }
  843. /* }}} */
  844. /* {{{ Set an option for a wrapper */
  845. PHP_FUNCTION(stream_context_set_option)
  846. {
  847. zval *zcontext = NULL;
  848. php_stream_context *context;
  849. zend_string *wrappername;
  850. HashTable *options;
  851. char *optionname = NULL;
  852. size_t optionname_len;
  853. zval *zvalue = NULL;
  854. ZEND_PARSE_PARAMETERS_START(2, 4)
  855. Z_PARAM_RESOURCE(zcontext)
  856. Z_PARAM_ARRAY_HT_OR_STR(options, wrappername)
  857. Z_PARAM_OPTIONAL
  858. Z_PARAM_STRING_OR_NULL(optionname, optionname_len)
  859. Z_PARAM_ZVAL(zvalue)
  860. ZEND_PARSE_PARAMETERS_END();
  861. /* figure out where the context is coming from exactly */
  862. if (!(context = decode_context_param(zcontext))) {
  863. zend_argument_type_error(1, "must be a valid stream/context");
  864. RETURN_THROWS();
  865. }
  866. if (options) {
  867. if (optionname) {
  868. zend_argument_value_error(3, "must be null when argument #2 ($wrapper_or_options) is an array");
  869. RETURN_THROWS();
  870. }
  871. if (zvalue) {
  872. zend_argument_value_error(4, "cannot be provided when argument #2 ($wrapper_or_options) is an array");
  873. RETURN_THROWS();
  874. }
  875. RETURN_BOOL(parse_context_options(context, options) == SUCCESS);
  876. } else {
  877. if (!optionname) {
  878. zend_argument_value_error(3, "cannot be null when argument #2 ($wrapper_or_options) is a string");
  879. RETURN_THROWS();
  880. }
  881. if (!zvalue) {
  882. zend_argument_value_error(4, "must be provided when argument #2 ($wrapper_or_options) is a string");
  883. RETURN_THROWS();
  884. }
  885. RETURN_BOOL(php_stream_context_set_option(context, ZSTR_VAL(wrappername), optionname, zvalue) == SUCCESS);
  886. }
  887. }
  888. /* }}} */
  889. /* {{{ Set parameters for a file context */
  890. PHP_FUNCTION(stream_context_set_params)
  891. {
  892. HashTable *params;
  893. zval *zcontext;
  894. php_stream_context *context;
  895. ZEND_PARSE_PARAMETERS_START(2, 2)
  896. Z_PARAM_RESOURCE(zcontext)
  897. Z_PARAM_ARRAY_HT(params)
  898. ZEND_PARSE_PARAMETERS_END();
  899. context = decode_context_param(zcontext);
  900. if (!context) {
  901. zend_argument_type_error(1, "must be a valid stream/context");
  902. RETURN_THROWS();
  903. }
  904. RETVAL_BOOL(parse_context_params(context, params) == SUCCESS);
  905. }
  906. /* }}} */
  907. /* {{{ Get parameters of a file context */
  908. PHP_FUNCTION(stream_context_get_params)
  909. {
  910. zval *zcontext;
  911. php_stream_context *context;
  912. ZEND_PARSE_PARAMETERS_START(1, 1)
  913. Z_PARAM_RESOURCE(zcontext)
  914. ZEND_PARSE_PARAMETERS_END();
  915. context = decode_context_param(zcontext);
  916. if (!context) {
  917. zend_argument_type_error(1, "must be a valid stream/context");
  918. RETURN_THROWS();
  919. }
  920. array_init(return_value);
  921. if (context->notifier && Z_TYPE(context->notifier->ptr) != IS_UNDEF && context->notifier->func == user_space_stream_notifier) {
  922. Z_TRY_ADDREF(context->notifier->ptr);
  923. add_assoc_zval_ex(return_value, "notification", sizeof("notification")-1, &context->notifier->ptr);
  924. }
  925. Z_TRY_ADDREF(context->options);
  926. add_assoc_zval_ex(return_value, "options", sizeof("options")-1, &context->options);
  927. }
  928. /* }}} */
  929. /* {{{ Get a handle on the default file/stream context and optionally set parameters */
  930. PHP_FUNCTION(stream_context_get_default)
  931. {
  932. HashTable *params = NULL;
  933. php_stream_context *context;
  934. ZEND_PARSE_PARAMETERS_START(0, 1)
  935. Z_PARAM_OPTIONAL
  936. Z_PARAM_ARRAY_HT_OR_NULL(params)
  937. ZEND_PARSE_PARAMETERS_END();
  938. if (FG(default_context) == NULL) {
  939. FG(default_context) = php_stream_context_alloc();
  940. }
  941. context = FG(default_context);
  942. if (params) {
  943. if (parse_context_options(context, params) == FAILURE) {
  944. RETURN_THROWS();
  945. }
  946. }
  947. php_stream_context_to_zval(context, return_value);
  948. }
  949. /* }}} */
  950. /* {{{ Set default file/stream context, returns the context as a resource */
  951. PHP_FUNCTION(stream_context_set_default)
  952. {
  953. HashTable *options;
  954. php_stream_context *context;
  955. ZEND_PARSE_PARAMETERS_START(1, 1)
  956. Z_PARAM_ARRAY_HT(options)
  957. ZEND_PARSE_PARAMETERS_END();
  958. if (FG(default_context) == NULL) {
  959. FG(default_context) = php_stream_context_alloc();
  960. }
  961. context = FG(default_context);
  962. if (parse_context_options(context, options) == FAILURE) {
  963. RETURN_THROWS();
  964. }
  965. php_stream_context_to_zval(context, return_value);
  966. }
  967. /* }}} */
  968. /* {{{ Create a file context and optionally set parameters */
  969. PHP_FUNCTION(stream_context_create)
  970. {
  971. HashTable *options = NULL;
  972. HashTable *params = NULL;
  973. php_stream_context *context;
  974. ZEND_PARSE_PARAMETERS_START(0, 2)
  975. Z_PARAM_OPTIONAL
  976. Z_PARAM_ARRAY_HT_OR_NULL(options)
  977. Z_PARAM_ARRAY_HT_OR_NULL(params)
  978. ZEND_PARSE_PARAMETERS_END();
  979. context = php_stream_context_alloc();
  980. if (options) {
  981. parse_context_options(context, options);
  982. }
  983. if (params) {
  984. parse_context_params(context, params);
  985. }
  986. RETURN_RES(context->res);
  987. }
  988. /* }}} */
  989. /* {{{ streams filter functions */
  990. static void apply_filter_to_stream(int append, INTERNAL_FUNCTION_PARAMETERS)
  991. {
  992. zval *zstream;
  993. php_stream *stream;
  994. char *filtername;
  995. size_t filternamelen;
  996. zend_long read_write = 0;
  997. zval *filterparams = NULL;
  998. php_stream_filter *filter = NULL;
  999. int ret;
  1000. ZEND_PARSE_PARAMETERS_START(2, 4)
  1001. Z_PARAM_RESOURCE(zstream)
  1002. Z_PARAM_STRING(filtername, filternamelen)
  1003. Z_PARAM_OPTIONAL
  1004. Z_PARAM_LONG(read_write)
  1005. Z_PARAM_ZVAL(filterparams)
  1006. ZEND_PARSE_PARAMETERS_END();
  1007. php_stream_from_zval(stream, zstream);
  1008. if ((read_write & PHP_STREAM_FILTER_ALL) == 0) {
  1009. /* Chain not specified.
  1010. * Examine stream->mode to determine which filters are needed
  1011. * There's no harm in attaching a filter to an unused chain,
  1012. * but why waste the memory and clock cycles?
  1013. */
  1014. if (strchr(stream->mode, 'r') || strchr(stream->mode, '+')) {
  1015. read_write |= PHP_STREAM_FILTER_READ;
  1016. }
  1017. if (strchr(stream->mode, 'w') || strchr(stream->mode, '+') || strchr(stream->mode, 'a')) {
  1018. read_write |= PHP_STREAM_FILTER_WRITE;
  1019. }
  1020. }
  1021. if (read_write & PHP_STREAM_FILTER_READ) {
  1022. filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream));
  1023. if (filter == NULL) {
  1024. RETURN_FALSE;
  1025. }
  1026. if (append) {
  1027. ret = php_stream_filter_append_ex(&stream->readfilters, filter);
  1028. } else {
  1029. ret = php_stream_filter_prepend_ex(&stream->readfilters, filter);
  1030. }
  1031. if (ret != SUCCESS) {
  1032. php_stream_filter_remove(filter, 1);
  1033. RETURN_FALSE;
  1034. }
  1035. }
  1036. if (read_write & PHP_STREAM_FILTER_WRITE) {
  1037. filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream));
  1038. if (filter == NULL) {
  1039. RETURN_FALSE;
  1040. }
  1041. if (append) {
  1042. ret = php_stream_filter_append_ex(&stream->writefilters, filter);
  1043. } else {
  1044. ret = php_stream_filter_prepend_ex(&stream->writefilters, filter);
  1045. }
  1046. if (ret != SUCCESS) {
  1047. php_stream_filter_remove(filter, 1);
  1048. RETURN_FALSE;
  1049. }
  1050. }
  1051. if (filter) {
  1052. filter->res = zend_register_resource(filter, php_file_le_stream_filter());
  1053. GC_ADDREF(filter->res);
  1054. RETURN_RES(filter->res);
  1055. } else {
  1056. RETURN_FALSE;
  1057. }
  1058. }
  1059. /* }}} */
  1060. /* {{{ Prepend a filter to a stream */
  1061. PHP_FUNCTION(stream_filter_prepend)
  1062. {
  1063. apply_filter_to_stream(0, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1064. }
  1065. /* }}} */
  1066. /* {{{ Append a filter to a stream */
  1067. PHP_FUNCTION(stream_filter_append)
  1068. {
  1069. apply_filter_to_stream(1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1070. }
  1071. /* }}} */
  1072. /* {{{ Flushes any data in the filter's internal buffer, removes it from the chain, and frees the resource */
  1073. PHP_FUNCTION(stream_filter_remove)
  1074. {
  1075. zval *zfilter;
  1076. php_stream_filter *filter;
  1077. ZEND_PARSE_PARAMETERS_START(1, 1)
  1078. Z_PARAM_RESOURCE(zfilter)
  1079. ZEND_PARSE_PARAMETERS_END();
  1080. filter = zend_fetch_resource(Z_RES_P(zfilter), "stream filter", php_file_le_stream_filter());
  1081. if (!filter) {
  1082. RETURN_THROWS();
  1083. }
  1084. if (php_stream_filter_flush(filter, 1) == FAILURE) {
  1085. php_error_docref(NULL, E_WARNING, "Unable to flush filter, not removing");
  1086. RETURN_FALSE;
  1087. }
  1088. zend_list_close(Z_RES_P(zfilter));
  1089. php_stream_filter_remove(filter, 1);
  1090. RETURN_TRUE;
  1091. }
  1092. /* }}} */
  1093. /* {{{ Read up to maxlen bytes from a stream or until the ending string is found */
  1094. PHP_FUNCTION(stream_get_line)
  1095. {
  1096. char *str = NULL;
  1097. size_t str_len = 0;
  1098. zend_long max_length;
  1099. zval *zstream;
  1100. zend_string *buf;
  1101. php_stream *stream;
  1102. ZEND_PARSE_PARAMETERS_START(2, 3)
  1103. Z_PARAM_RESOURCE(zstream)
  1104. Z_PARAM_LONG(max_length)
  1105. Z_PARAM_OPTIONAL
  1106. Z_PARAM_STRING(str, str_len)
  1107. ZEND_PARSE_PARAMETERS_END();
  1108. if (max_length < 0) {
  1109. zend_argument_value_error(2, "must be greater than or equal to 0");
  1110. RETURN_THROWS();
  1111. }
  1112. if (!max_length) {
  1113. max_length = PHP_SOCK_CHUNK_SIZE;
  1114. }
  1115. php_stream_from_zval(stream, zstream);
  1116. if ((buf = php_stream_get_record(stream, max_length, str, str_len))) {
  1117. RETURN_STR(buf);
  1118. } else {
  1119. RETURN_FALSE;
  1120. }
  1121. }
  1122. /* }}} */
  1123. /* {{{ Set blocking/non-blocking mode on a socket or stream */
  1124. PHP_FUNCTION(stream_set_blocking)
  1125. {
  1126. zval *zstream;
  1127. bool block;
  1128. php_stream *stream;
  1129. ZEND_PARSE_PARAMETERS_START(2, 2)
  1130. Z_PARAM_RESOURCE(zstream)
  1131. Z_PARAM_BOOL(block)
  1132. ZEND_PARSE_PARAMETERS_END();
  1133. php_stream_from_zval(stream, zstream);
  1134. if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, block, NULL) == -1) {
  1135. RETURN_FALSE;
  1136. }
  1137. RETURN_TRUE;
  1138. }
  1139. /* }}} */
  1140. /* {{{ Set timeout on stream read to seconds + microseonds */
  1141. #if HAVE_SYS_TIME_H || defined(PHP_WIN32)
  1142. PHP_FUNCTION(stream_set_timeout)
  1143. {
  1144. zval *socket;
  1145. zend_long seconds, microseconds = 0;
  1146. struct timeval t;
  1147. php_stream *stream;
  1148. int argc = ZEND_NUM_ARGS();
  1149. ZEND_PARSE_PARAMETERS_START(2, 3)
  1150. Z_PARAM_RESOURCE(socket)
  1151. Z_PARAM_LONG(seconds)
  1152. Z_PARAM_OPTIONAL
  1153. Z_PARAM_LONG(microseconds)
  1154. ZEND_PARSE_PARAMETERS_END();
  1155. php_stream_from_zval(stream, socket);
  1156. #ifdef PHP_WIN32
  1157. t.tv_sec = (long)seconds;
  1158. if (argc == 3) {
  1159. t.tv_usec = (long)(microseconds % 1000000);
  1160. t.tv_sec +=(long)(microseconds / 1000000);
  1161. } else {
  1162. t.tv_usec = 0;
  1163. }
  1164. #else
  1165. t.tv_sec = seconds;
  1166. if (argc == 3) {
  1167. t.tv_usec = microseconds % 1000000;
  1168. t.tv_sec += microseconds / 1000000;
  1169. } else {
  1170. t.tv_usec = 0;
  1171. }
  1172. #endif
  1173. if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &t)) {
  1174. RETURN_TRUE;
  1175. }
  1176. RETURN_FALSE;
  1177. }
  1178. #endif /* HAVE_SYS_TIME_H || defined(PHP_WIN32) */
  1179. /* }}} */
  1180. /* {{{ Set file write buffer */
  1181. PHP_FUNCTION(stream_set_write_buffer)
  1182. {
  1183. zval *arg1;
  1184. int ret;
  1185. zend_long arg2;
  1186. size_t buff;
  1187. php_stream *stream;
  1188. ZEND_PARSE_PARAMETERS_START(2, 2)
  1189. Z_PARAM_RESOURCE(arg1)
  1190. Z_PARAM_LONG(arg2)
  1191. ZEND_PARSE_PARAMETERS_END();
  1192. php_stream_from_zval(stream, arg1);
  1193. buff = arg2;
  1194. /* if buff is 0 then set to non-buffered */
  1195. if (buff == 0) {
  1196. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
  1197. } else {
  1198. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buff);
  1199. }
  1200. RETURN_LONG(ret == 0 ? 0 : EOF);
  1201. }
  1202. /* }}} */
  1203. /* {{{ Set the stream chunk size */
  1204. PHP_FUNCTION(stream_set_chunk_size)
  1205. {
  1206. int ret;
  1207. zend_long csize;
  1208. zval *zstream;
  1209. php_stream *stream;
  1210. ZEND_PARSE_PARAMETERS_START(2, 2)
  1211. Z_PARAM_RESOURCE(zstream)
  1212. Z_PARAM_LONG(csize)
  1213. ZEND_PARSE_PARAMETERS_END();
  1214. if (csize <= 0) {
  1215. zend_argument_value_error(2, "must be greater than 0");
  1216. RETURN_THROWS();
  1217. }
  1218. /* stream.chunk_size is actually a size_t, but php_stream_set_option
  1219. * can only use an int to accept the new value and return the old one.
  1220. * In any case, values larger than INT_MAX for a chunk size make no sense.
  1221. */
  1222. if (csize > INT_MAX) {
  1223. zend_argument_value_error(2, "is too large");
  1224. RETURN_THROWS();
  1225. }
  1226. php_stream_from_zval(stream, zstream);
  1227. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_SET_CHUNK_SIZE, (int)csize, NULL);
  1228. RETURN_LONG(ret > 0 ? (zend_long)ret : (zend_long)EOF);
  1229. }
  1230. /* }}} */
  1231. /* {{{ Set file read buffer */
  1232. PHP_FUNCTION(stream_set_read_buffer)
  1233. {
  1234. zval *arg1;
  1235. int ret;
  1236. zend_long arg2;
  1237. size_t buff;
  1238. php_stream *stream;
  1239. ZEND_PARSE_PARAMETERS_START(2, 2)
  1240. Z_PARAM_RESOURCE(arg1)
  1241. Z_PARAM_LONG(arg2)
  1242. ZEND_PARSE_PARAMETERS_END();
  1243. php_stream_from_zval(stream, arg1);
  1244. buff = arg2;
  1245. /* if buff is 0 then set to non-buffered */
  1246. if (buff == 0) {
  1247. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
  1248. } else {
  1249. ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_FULL, &buff);
  1250. }
  1251. RETURN_LONG(ret == 0 ? 0 : EOF);
  1252. }
  1253. /* }}} */
  1254. /* {{{ Enable or disable a specific kind of crypto on the stream */
  1255. PHP_FUNCTION(stream_socket_enable_crypto)
  1256. {
  1257. zend_long cryptokind = 0;
  1258. zval *zstream, *zsessstream = NULL;
  1259. php_stream *stream, *sessstream = NULL;
  1260. bool enable, cryptokindnull = 1;
  1261. int ret;
  1262. ZEND_PARSE_PARAMETERS_START(2, 4)
  1263. Z_PARAM_RESOURCE(zstream)
  1264. Z_PARAM_BOOL(enable)
  1265. Z_PARAM_OPTIONAL
  1266. Z_PARAM_LONG_OR_NULL(cryptokind, cryptokindnull)
  1267. Z_PARAM_RESOURCE_OR_NULL(zsessstream)
  1268. ZEND_PARSE_PARAMETERS_END();
  1269. php_stream_from_zval(stream, zstream);
  1270. if (enable) {
  1271. if (cryptokindnull) {
  1272. zval *val;
  1273. if (!GET_CTX_OPT(stream, "ssl", "crypto_method", val)) {
  1274. zend_argument_value_error(3, "must be specified when enabling encryption");
  1275. RETURN_THROWS();
  1276. }
  1277. cryptokind = Z_LVAL_P(val);
  1278. }
  1279. if (zsessstream) {
  1280. php_stream_from_zval(sessstream, zsessstream);
  1281. }
  1282. if (php_stream_xport_crypto_setup(stream, cryptokind, sessstream) < 0) {
  1283. RETURN_FALSE;
  1284. }
  1285. }
  1286. ret = php_stream_xport_crypto_enable(stream, enable);
  1287. switch (ret) {
  1288. case -1:
  1289. RETURN_FALSE;
  1290. case 0:
  1291. RETURN_LONG(0);
  1292. default:
  1293. RETURN_TRUE;
  1294. }
  1295. }
  1296. /* }}} */
  1297. /* {{{ Determine what file will be opened by calls to fopen() with a relative path */
  1298. PHP_FUNCTION(stream_resolve_include_path)
  1299. {
  1300. zend_string *filename;
  1301. zend_string *resolved_path;
  1302. ZEND_PARSE_PARAMETERS_START(1, 1)
  1303. Z_PARAM_PATH_STR(filename)
  1304. ZEND_PARSE_PARAMETERS_END();
  1305. resolved_path = zend_resolve_path(filename);
  1306. if (resolved_path) {
  1307. RETURN_STR(resolved_path);
  1308. }
  1309. RETURN_FALSE;
  1310. }
  1311. /* }}} */
  1312. /* {{{ */
  1313. PHP_FUNCTION(stream_is_local)
  1314. {
  1315. zval *zstream;
  1316. php_stream *stream = NULL;
  1317. php_stream_wrapper *wrapper = NULL;
  1318. ZEND_PARSE_PARAMETERS_START(1, 1)
  1319. Z_PARAM_ZVAL(zstream)
  1320. ZEND_PARSE_PARAMETERS_END();
  1321. if (Z_TYPE_P(zstream) == IS_RESOURCE) {
  1322. php_stream_from_zval(stream, zstream);
  1323. if (stream == NULL) {
  1324. RETURN_FALSE;
  1325. }
  1326. wrapper = stream->wrapper;
  1327. } else {
  1328. if (!try_convert_to_string(zstream)) {
  1329. RETURN_THROWS();
  1330. }
  1331. wrapper = php_stream_locate_url_wrapper(Z_STRVAL_P(zstream), NULL, 0);
  1332. }
  1333. if (!wrapper) {
  1334. RETURN_FALSE;
  1335. }
  1336. RETURN_BOOL(wrapper->is_url==0);
  1337. }
  1338. /* }}} */
  1339. /* {{{ Tells whether the stream supports locking through flock(). */
  1340. PHP_FUNCTION(stream_supports_lock)
  1341. {
  1342. php_stream *stream;
  1343. zval *zsrc;
  1344. ZEND_PARSE_PARAMETERS_START(1, 1)
  1345. Z_PARAM_RESOURCE(zsrc)
  1346. ZEND_PARSE_PARAMETERS_END();
  1347. php_stream_from_zval(stream, zsrc);
  1348. if (!php_stream_supports_lock(stream)) {
  1349. RETURN_FALSE;
  1350. }
  1351. RETURN_TRUE;
  1352. }
  1353. /* {{{ Check if a stream is a TTY. */
  1354. PHP_FUNCTION(stream_isatty)
  1355. {
  1356. zval *zsrc;
  1357. php_stream *stream;
  1358. php_socket_t fileno;
  1359. ZEND_PARSE_PARAMETERS_START(1, 1)
  1360. Z_PARAM_RESOURCE(zsrc)
  1361. ZEND_PARSE_PARAMETERS_END();
  1362. php_stream_from_zval(stream, zsrc);
  1363. if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
  1364. php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
  1365. } else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
  1366. php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
  1367. } else {
  1368. RETURN_FALSE;
  1369. }
  1370. #ifdef PHP_WIN32
  1371. /* Check if the Windows standard handle is redirected to file */
  1372. RETVAL_BOOL(php_win32_console_fileno_is_console(fileno));
  1373. #elif HAVE_UNISTD_H
  1374. /* Check if the file descriptor identifier is a terminal */
  1375. RETVAL_BOOL(isatty(fileno));
  1376. #else
  1377. {
  1378. zend_stat_t stat = {0};
  1379. RETVAL_BOOL(zend_fstat(fileno, &stat) == 0 && (stat.st_mode & /*S_IFMT*/0170000) == /*S_IFCHR*/0020000);
  1380. }
  1381. #endif
  1382. }
  1383. #ifdef PHP_WIN32
  1384. /* {{{ Get or set VT100 support for the specified stream associated to an
  1385. output buffer of a Windows console.
  1386. */
  1387. PHP_FUNCTION(sapi_windows_vt100_support)
  1388. {
  1389. zval *zsrc;
  1390. php_stream *stream;
  1391. bool enable, enable_is_null = 1;
  1392. zend_long fileno;
  1393. ZEND_PARSE_PARAMETERS_START(1, 2)
  1394. Z_PARAM_RESOURCE(zsrc)
  1395. Z_PARAM_OPTIONAL
  1396. Z_PARAM_BOOL_OR_NULL(enable, enable_is_null)
  1397. ZEND_PARSE_PARAMETERS_END();
  1398. php_stream_from_zval(stream, zsrc);
  1399. if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
  1400. php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
  1401. }
  1402. else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
  1403. php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
  1404. }
  1405. else {
  1406. if (!enable_is_null) {
  1407. php_error_docref(
  1408. NULL,
  1409. E_WARNING,
  1410. "not able to analyze the specified stream"
  1411. );
  1412. }
  1413. RETURN_FALSE;
  1414. }
  1415. /* Check if the file descriptor is a console */
  1416. if (!php_win32_console_fileno_is_console(fileno)) {
  1417. RETURN_FALSE;
  1418. }
  1419. if (enable_is_null) {
  1420. /* Check if the Windows standard handle has VT100 control codes enabled */
  1421. if (php_win32_console_fileno_has_vt100(fileno)) {
  1422. RETURN_TRUE;
  1423. }
  1424. else {
  1425. RETURN_FALSE;
  1426. }
  1427. }
  1428. else {
  1429. /* Enable/disable VT100 control codes support for the specified Windows standard handle */
  1430. if (php_win32_console_fileno_set_vt100(fileno, enable ? TRUE : FALSE)) {
  1431. RETURN_TRUE;
  1432. }
  1433. else {
  1434. RETURN_FALSE;
  1435. }
  1436. }
  1437. }
  1438. #endif
  1439. #ifdef HAVE_SHUTDOWN
  1440. /* {{{ causes all or part of a full-duplex connection on the socket associated
  1441. with stream to be shut down. If how is SHUT_RD, further receptions will
  1442. be disallowed. If how is SHUT_WR, further transmissions will be disallowed.
  1443. If how is SHUT_RDWR, further receptions and transmissions will be
  1444. disallowed. */
  1445. PHP_FUNCTION(stream_socket_shutdown)
  1446. {
  1447. zend_long how;
  1448. zval *zstream;
  1449. php_stream *stream;
  1450. ZEND_PARSE_PARAMETERS_START(2, 2)
  1451. Z_PARAM_RESOURCE(zstream)
  1452. Z_PARAM_LONG(how)
  1453. ZEND_PARSE_PARAMETERS_END();
  1454. if (how != STREAM_SHUT_RD &&
  1455. how != STREAM_SHUT_WR &&
  1456. how != STREAM_SHUT_RDWR) {
  1457. zend_argument_value_error(2, "must be one of STREAM_SHUT_RD, STREAM_SHUT_WR, or STREAM_SHUT_RDWR");
  1458. RETURN_THROWS();
  1459. }
  1460. php_stream_from_zval(stream, zstream);
  1461. RETURN_BOOL(php_stream_xport_shutdown(stream, (stream_shutdown_t)how) == 0);
  1462. }
  1463. /* }}} */
  1464. #endif