streamsfuncs.c 47 KB

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