file.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  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: Rasmus Lerdorf <rasmus@php.net> |
  16. | Stig Bakken <ssb@php.net> |
  17. | Andi Gutmans <andi@php.net> |
  18. | Zeev Suraski <zeev@php.net> |
  19. | PHP 4.0 patches by Thies C. Arntzen (thies@thieso.net) |
  20. | PHP streams by Wez Furlong (wez@thebrainroom.com) |
  21. +----------------------------------------------------------------------+
  22. */
  23. /* {{{ includes */
  24. #include "php.h"
  25. #include "php_globals.h"
  26. #include "ext/standard/flock_compat.h"
  27. #include "ext/standard/exec.h"
  28. #include "ext/standard/php_filestat.h"
  29. #include "php_open_temporary_file.h"
  30. #include "ext/standard/basic_functions.h"
  31. #include "php_ini.h"
  32. #include "zend_smart_str.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <errno.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <fcntl.h>
  39. #ifdef PHP_WIN32
  40. # include <io.h>
  41. # define O_RDONLY _O_RDONLY
  42. # include "win32/param.h"
  43. # include "win32/winutil.h"
  44. # include "win32/fnmatch.h"
  45. # include "win32/ioutil.h"
  46. #else
  47. # if HAVE_SYS_PARAM_H
  48. # include <sys/param.h>
  49. # endif
  50. # if HAVE_SYS_SELECT_H
  51. # include <sys/select.h>
  52. # endif
  53. # include <sys/socket.h>
  54. # include <netinet/in.h>
  55. # include <netdb.h>
  56. # if HAVE_ARPA_INET_H
  57. # include <arpa/inet.h>
  58. # endif
  59. #endif
  60. #include "ext/standard/head.h"
  61. #include "php_string.h"
  62. #include "file.h"
  63. #if HAVE_PWD_H
  64. # ifdef PHP_WIN32
  65. # include "win32/pwd.h"
  66. # else
  67. # include <pwd.h>
  68. # endif
  69. #endif
  70. #ifdef HAVE_SYS_TIME_H
  71. # include <sys/time.h>
  72. #endif
  73. #include "fsock.h"
  74. #include "fopen_wrappers.h"
  75. #include "streamsfuncs.h"
  76. #include "php_globals.h"
  77. #ifdef HAVE_SYS_FILE_H
  78. # include <sys/file.h>
  79. #endif
  80. #if MISSING_FCLOSE_DECL
  81. extern int fclose(FILE *);
  82. #endif
  83. #ifdef HAVE_SYS_MMAN_H
  84. # include <sys/mman.h>
  85. #endif
  86. #include "scanf.h"
  87. #include "zend_API.h"
  88. #ifdef ZTS
  89. int file_globals_id;
  90. #else
  91. php_file_globals file_globals;
  92. #endif
  93. #if defined(HAVE_FNMATCH) && !defined(PHP_WIN32)
  94. # ifndef _GNU_SOURCE
  95. # define _GNU_SOURCE
  96. # endif
  97. # include <fnmatch.h>
  98. #endif
  99. #ifdef HAVE_WCHAR_H
  100. # include <wchar.h>
  101. #endif
  102. /* }}} */
  103. #define PHP_STREAM_TO_ZVAL(stream, arg) \
  104. ZEND_ASSERT(Z_TYPE_P(arg) == IS_RESOURCE); \
  105. php_stream_from_res(stream, Z_RES_P(arg));
  106. /* {{{ ZTS-stuff / Globals / Prototypes */
  107. /* sharing globals is *evil* */
  108. static int le_stream_context = FAILURE;
  109. PHPAPI int php_le_stream_context(void)
  110. {
  111. return le_stream_context;
  112. }
  113. /* }}} */
  114. /* {{{ Module-Stuff
  115. */
  116. static ZEND_RSRC_DTOR_FUNC(file_context_dtor)
  117. {
  118. php_stream_context *context = (php_stream_context*)res->ptr;
  119. if (Z_TYPE(context->options) != IS_UNDEF) {
  120. zval_ptr_dtor(&context->options);
  121. ZVAL_UNDEF(&context->options);
  122. }
  123. php_stream_context_free(context);
  124. }
  125. static void file_globals_ctor(php_file_globals *file_globals_p)
  126. {
  127. memset(file_globals_p, 0, sizeof(php_file_globals));
  128. file_globals_p->def_chunk_size = PHP_SOCK_CHUNK_SIZE;
  129. }
  130. static void file_globals_dtor(php_file_globals *file_globals_p)
  131. {
  132. #if defined(HAVE_GETHOSTBYNAME_R)
  133. if (file_globals_p->tmp_host_buf) {
  134. free(file_globals_p->tmp_host_buf);
  135. }
  136. #endif
  137. }
  138. PHP_INI_BEGIN()
  139. STD_PHP_INI_ENTRY("user_agent", NULL, PHP_INI_ALL, OnUpdateString, user_agent, php_file_globals, file_globals)
  140. STD_PHP_INI_ENTRY("from", NULL, PHP_INI_ALL, OnUpdateString, from_address, php_file_globals, file_globals)
  141. STD_PHP_INI_ENTRY("default_socket_timeout", "60", PHP_INI_ALL, OnUpdateLong, default_socket_timeout, php_file_globals, file_globals)
  142. STD_PHP_INI_ENTRY("auto_detect_line_endings", "0", PHP_INI_ALL, OnUpdateBool, auto_detect_line_endings, php_file_globals, file_globals)
  143. PHP_INI_END()
  144. PHP_MINIT_FUNCTION(file)
  145. {
  146. le_stream_context = zend_register_list_destructors_ex(file_context_dtor, NULL, "stream-context", module_number);
  147. #ifdef ZTS
  148. ts_allocate_id(&file_globals_id, sizeof(php_file_globals), (ts_allocate_ctor) file_globals_ctor, (ts_allocate_dtor) file_globals_dtor);
  149. #else
  150. file_globals_ctor(&file_globals);
  151. #endif
  152. REGISTER_INI_ENTRIES();
  153. REGISTER_LONG_CONSTANT("SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
  154. REGISTER_LONG_CONSTANT("SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
  155. REGISTER_LONG_CONSTANT("SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
  156. REGISTER_LONG_CONSTANT("LOCK_SH", PHP_LOCK_SH, CONST_CS | CONST_PERSISTENT);
  157. REGISTER_LONG_CONSTANT("LOCK_EX", PHP_LOCK_EX, CONST_CS | CONST_PERSISTENT);
  158. REGISTER_LONG_CONSTANT("LOCK_UN", PHP_LOCK_UN, CONST_CS | CONST_PERSISTENT);
  159. REGISTER_LONG_CONSTANT("LOCK_NB", PHP_LOCK_NB, CONST_CS | CONST_PERSISTENT);
  160. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_CONNECT", PHP_STREAM_NOTIFY_CONNECT, CONST_CS | CONST_PERSISTENT);
  161. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_REQUIRED", PHP_STREAM_NOTIFY_AUTH_REQUIRED, CONST_CS | CONST_PERSISTENT);
  162. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_RESULT", PHP_STREAM_NOTIFY_AUTH_RESULT, CONST_CS | CONST_PERSISTENT);
  163. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_MIME_TYPE_IS", PHP_STREAM_NOTIFY_MIME_TYPE_IS, CONST_CS | CONST_PERSISTENT);
  164. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FILE_SIZE_IS", PHP_STREAM_NOTIFY_FILE_SIZE_IS, CONST_CS | CONST_PERSISTENT);
  165. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_REDIRECTED", PHP_STREAM_NOTIFY_REDIRECTED, CONST_CS | CONST_PERSISTENT);
  166. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_PROGRESS", PHP_STREAM_NOTIFY_PROGRESS, CONST_CS | CONST_PERSISTENT);
  167. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FAILURE", PHP_STREAM_NOTIFY_FAILURE, CONST_CS | CONST_PERSISTENT);
  168. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_COMPLETED", PHP_STREAM_NOTIFY_COMPLETED, CONST_CS | CONST_PERSISTENT);
  169. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_RESOLVE", PHP_STREAM_NOTIFY_RESOLVE, CONST_CS | CONST_PERSISTENT);
  170. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_INFO", PHP_STREAM_NOTIFY_SEVERITY_INFO, CONST_CS | CONST_PERSISTENT);
  171. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_WARN", PHP_STREAM_NOTIFY_SEVERITY_WARN, CONST_CS | CONST_PERSISTENT);
  172. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_ERR", PHP_STREAM_NOTIFY_SEVERITY_ERR, CONST_CS | CONST_PERSISTENT);
  173. REGISTER_LONG_CONSTANT("STREAM_FILTER_READ", PHP_STREAM_FILTER_READ, CONST_CS | CONST_PERSISTENT);
  174. REGISTER_LONG_CONSTANT("STREAM_FILTER_WRITE", PHP_STREAM_FILTER_WRITE, CONST_CS | CONST_PERSISTENT);
  175. REGISTER_LONG_CONSTANT("STREAM_FILTER_ALL", PHP_STREAM_FILTER_ALL, CONST_CS | CONST_PERSISTENT);
  176. REGISTER_LONG_CONSTANT("STREAM_CLIENT_PERSISTENT", PHP_STREAM_CLIENT_PERSISTENT, CONST_CS | CONST_PERSISTENT);
  177. REGISTER_LONG_CONSTANT("STREAM_CLIENT_ASYNC_CONNECT", PHP_STREAM_CLIENT_ASYNC_CONNECT, CONST_CS | CONST_PERSISTENT);
  178. REGISTER_LONG_CONSTANT("STREAM_CLIENT_CONNECT", PHP_STREAM_CLIENT_CONNECT, CONST_CS | CONST_PERSISTENT);
  179. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_ANY_CLIENT", STREAM_CRYPTO_METHOD_ANY_CLIENT, CONST_CS|CONST_PERSISTENT);
  180. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv2_CLIENT", STREAM_CRYPTO_METHOD_SSLv2_CLIENT, CONST_CS|CONST_PERSISTENT);
  181. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv3_CLIENT", STREAM_CRYPTO_METHOD_SSLv3_CLIENT, CONST_CS|CONST_PERSISTENT);
  182. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_CLIENT", STREAM_CRYPTO_METHOD_SSLv23_CLIENT, CONST_CS|CONST_PERSISTENT);
  183. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_CLIENT", STREAM_CRYPTO_METHOD_TLS_CLIENT, CONST_CS|CONST_PERSISTENT);
  184. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT, CONST_CS|CONST_PERSISTENT);
  185. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, CONST_CS|CONST_PERSISTENT);
  186. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, CONST_CS|CONST_PERSISTENT);
  187. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_ANY_SERVER", STREAM_CRYPTO_METHOD_ANY_SERVER, CONST_CS|CONST_PERSISTENT);
  188. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv2_SERVER", STREAM_CRYPTO_METHOD_SSLv2_SERVER, CONST_CS|CONST_PERSISTENT);
  189. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv3_SERVER", STREAM_CRYPTO_METHOD_SSLv3_SERVER, CONST_CS|CONST_PERSISTENT);
  190. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_SERVER", STREAM_CRYPTO_METHOD_SSLv23_SERVER, CONST_CS|CONST_PERSISTENT);
  191. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_SERVER", STREAM_CRYPTO_METHOD_TLS_SERVER, CONST_CS|CONST_PERSISTENT);
  192. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_0_SERVER", STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, CONST_CS|CONST_PERSISTENT);
  193. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_1_SERVER", STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, CONST_CS|CONST_PERSISTENT);
  194. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_2_SERVER", STREAM_CRYPTO_METHOD_TLSv1_2_SERVER, CONST_CS|CONST_PERSISTENT);
  195. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_SSLv3", STREAM_CRYPTO_METHOD_SSLv3_SERVER, CONST_CS|CONST_PERSISTENT);
  196. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_0", STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, CONST_CS|CONST_PERSISTENT);
  197. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_1", STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, CONST_CS|CONST_PERSISTENT);
  198. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_2", STREAM_CRYPTO_METHOD_TLSv1_2_SERVER, CONST_CS|CONST_PERSISTENT);
  199. REGISTER_LONG_CONSTANT("STREAM_SHUT_RD", STREAM_SHUT_RD, CONST_CS|CONST_PERSISTENT);
  200. REGISTER_LONG_CONSTANT("STREAM_SHUT_WR", STREAM_SHUT_WR, CONST_CS|CONST_PERSISTENT);
  201. REGISTER_LONG_CONSTANT("STREAM_SHUT_RDWR", STREAM_SHUT_RDWR, CONST_CS|CONST_PERSISTENT);
  202. #ifdef PF_INET
  203. REGISTER_LONG_CONSTANT("STREAM_PF_INET", PF_INET, CONST_CS|CONST_PERSISTENT);
  204. #elif defined(AF_INET)
  205. REGISTER_LONG_CONSTANT("STREAM_PF_INET", AF_INET, CONST_CS|CONST_PERSISTENT);
  206. #endif
  207. #if HAVE_IPV6
  208. # ifdef PF_INET6
  209. REGISTER_LONG_CONSTANT("STREAM_PF_INET6", PF_INET6, CONST_CS|CONST_PERSISTENT);
  210. # elif defined(AF_INET6)
  211. REGISTER_LONG_CONSTANT("STREAM_PF_INET6", AF_INET6, CONST_CS|CONST_PERSISTENT);
  212. # endif
  213. #endif
  214. #ifdef PF_UNIX
  215. REGISTER_LONG_CONSTANT("STREAM_PF_UNIX", PF_UNIX, CONST_CS|CONST_PERSISTENT);
  216. #elif defined(AF_UNIX)
  217. REGISTER_LONG_CONSTANT("STREAM_PF_UNIX", AF_UNIX, CONST_CS|CONST_PERSISTENT);
  218. #endif
  219. #ifdef IPPROTO_IP
  220. /* most people will use this one when calling socket() or socketpair() */
  221. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_IP", IPPROTO_IP, CONST_CS|CONST_PERSISTENT);
  222. #endif
  223. #if defined(IPPROTO_TCP) || defined(PHP_WIN32)
  224. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_TCP", IPPROTO_TCP, CONST_CS|CONST_PERSISTENT);
  225. #endif
  226. #if defined(IPPROTO_UDP) || defined(PHP_WIN32)
  227. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_UDP", IPPROTO_UDP, CONST_CS|CONST_PERSISTENT);
  228. #endif
  229. #if defined(IPPROTO_ICMP) || defined(PHP_WIN32)
  230. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_ICMP", IPPROTO_ICMP, CONST_CS|CONST_PERSISTENT);
  231. #endif
  232. #if defined(IPPROTO_RAW) || defined(PHP_WIN32)
  233. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_RAW", IPPROTO_RAW, CONST_CS|CONST_PERSISTENT);
  234. #endif
  235. REGISTER_LONG_CONSTANT("STREAM_SOCK_STREAM", SOCK_STREAM, CONST_CS|CONST_PERSISTENT);
  236. REGISTER_LONG_CONSTANT("STREAM_SOCK_DGRAM", SOCK_DGRAM, CONST_CS|CONST_PERSISTENT);
  237. #ifdef SOCK_RAW
  238. REGISTER_LONG_CONSTANT("STREAM_SOCK_RAW", SOCK_RAW, CONST_CS|CONST_PERSISTENT);
  239. #endif
  240. #ifdef SOCK_SEQPACKET
  241. REGISTER_LONG_CONSTANT("STREAM_SOCK_SEQPACKET", SOCK_SEQPACKET, CONST_CS|CONST_PERSISTENT);
  242. #endif
  243. #ifdef SOCK_RDM
  244. REGISTER_LONG_CONSTANT("STREAM_SOCK_RDM", SOCK_RDM, CONST_CS|CONST_PERSISTENT);
  245. #endif
  246. REGISTER_LONG_CONSTANT("STREAM_PEEK", STREAM_PEEK, CONST_CS | CONST_PERSISTENT);
  247. REGISTER_LONG_CONSTANT("STREAM_OOB", STREAM_OOB, CONST_CS | CONST_PERSISTENT);
  248. REGISTER_LONG_CONSTANT("STREAM_SERVER_BIND", STREAM_XPORT_BIND, CONST_CS | CONST_PERSISTENT);
  249. REGISTER_LONG_CONSTANT("STREAM_SERVER_LISTEN", STREAM_XPORT_LISTEN, CONST_CS | CONST_PERSISTENT);
  250. REGISTER_LONG_CONSTANT("FILE_USE_INCLUDE_PATH", PHP_FILE_USE_INCLUDE_PATH, CONST_CS | CONST_PERSISTENT);
  251. REGISTER_LONG_CONSTANT("FILE_IGNORE_NEW_LINES", PHP_FILE_IGNORE_NEW_LINES, CONST_CS | CONST_PERSISTENT);
  252. REGISTER_LONG_CONSTANT("FILE_SKIP_EMPTY_LINES", PHP_FILE_SKIP_EMPTY_LINES, CONST_CS | CONST_PERSISTENT);
  253. REGISTER_LONG_CONSTANT("FILE_APPEND", PHP_FILE_APPEND, CONST_CS | CONST_PERSISTENT);
  254. REGISTER_LONG_CONSTANT("FILE_NO_DEFAULT_CONTEXT", PHP_FILE_NO_DEFAULT_CONTEXT, CONST_CS | CONST_PERSISTENT);
  255. REGISTER_LONG_CONSTANT("FILE_TEXT", 0, CONST_CS | CONST_PERSISTENT);
  256. REGISTER_LONG_CONSTANT("FILE_BINARY", 0, CONST_CS | CONST_PERSISTENT);
  257. #ifdef HAVE_FNMATCH
  258. REGISTER_LONG_CONSTANT("FNM_NOESCAPE", FNM_NOESCAPE, CONST_CS | CONST_PERSISTENT);
  259. REGISTER_LONG_CONSTANT("FNM_PATHNAME", FNM_PATHNAME, CONST_CS | CONST_PERSISTENT);
  260. REGISTER_LONG_CONSTANT("FNM_PERIOD", FNM_PERIOD, CONST_CS | CONST_PERSISTENT);
  261. # ifdef FNM_CASEFOLD /* a GNU extension */ /* TODO emulate if not available */
  262. REGISTER_LONG_CONSTANT("FNM_CASEFOLD", FNM_CASEFOLD, CONST_CS | CONST_PERSISTENT);
  263. # endif
  264. #endif
  265. return SUCCESS;
  266. }
  267. /* }}} */
  268. PHP_MSHUTDOWN_FUNCTION(file) /* {{{ */
  269. {
  270. #ifndef ZTS
  271. file_globals_dtor(&file_globals);
  272. #endif
  273. return SUCCESS;
  274. }
  275. /* }}} */
  276. static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN };
  277. /* {{{ proto bool flock(resource fp, int operation [, int &wouldblock])
  278. Portable file locking */
  279. PHP_FUNCTION(flock)
  280. {
  281. zval *res, *wouldblock = NULL;
  282. int act;
  283. php_stream *stream;
  284. zend_long operation = 0;
  285. ZEND_PARSE_PARAMETERS_START(2, 3)
  286. Z_PARAM_RESOURCE(res)
  287. Z_PARAM_LONG(operation)
  288. Z_PARAM_OPTIONAL
  289. Z_PARAM_ZVAL_DEREF(wouldblock)
  290. ZEND_PARSE_PARAMETERS_END();
  291. PHP_STREAM_TO_ZVAL(stream, res);
  292. act = operation & 3;
  293. if (act < 1 || act > 3) {
  294. php_error_docref(NULL, E_WARNING, "Illegal operation argument");
  295. RETURN_FALSE;
  296. }
  297. if (wouldblock) {
  298. zval_ptr_dtor(wouldblock);
  299. ZVAL_LONG(wouldblock, 0);
  300. }
  301. /* flock_values contains all possible actions if (operation & 4) we won't block on the lock */
  302. act = flock_values[act - 1] | (operation & PHP_LOCK_NB ? LOCK_NB : 0);
  303. if (php_stream_lock(stream, act)) {
  304. if (operation && errno == EWOULDBLOCK && wouldblock) {
  305. ZVAL_LONG(wouldblock, 1);
  306. }
  307. RETURN_FALSE;
  308. }
  309. RETURN_TRUE;
  310. }
  311. /* }}} */
  312. #define PHP_META_UNSAFE ".\\+*?[^]$() "
  313. /* {{{ proto array get_meta_tags(string filename [, bool use_include_path])
  314. Extracts all meta tag content attributes from a file and returns an array */
  315. PHP_FUNCTION(get_meta_tags)
  316. {
  317. char *filename;
  318. size_t filename_len;
  319. zend_bool use_include_path = 0;
  320. int in_tag = 0, done = 0;
  321. int looking_for_val = 0, have_name = 0, have_content = 0;
  322. int saw_name = 0, saw_content = 0;
  323. char *name = NULL, *value = NULL, *temp = NULL;
  324. php_meta_tags_token tok, tok_last;
  325. php_meta_tags_data md;
  326. /* Initiailize our structure */
  327. memset(&md, 0, sizeof(md));
  328. /* Parse arguments */
  329. ZEND_PARSE_PARAMETERS_START(1, 2)
  330. Z_PARAM_PATH(filename, filename_len)
  331. Z_PARAM_OPTIONAL
  332. Z_PARAM_BOOL(use_include_path)
  333. ZEND_PARSE_PARAMETERS_END();
  334. md.stream = php_stream_open_wrapper(filename, "rb",
  335. (use_include_path ? USE_PATH : 0) | REPORT_ERRORS,
  336. NULL);
  337. if (!md.stream) {
  338. RETURN_FALSE;
  339. }
  340. array_init(return_value);
  341. tok_last = TOK_EOF;
  342. while (!done && (tok = php_next_meta_token(&md)) != TOK_EOF) {
  343. if (tok == TOK_ID) {
  344. if (tok_last == TOK_OPENTAG) {
  345. md.in_meta = !strcasecmp("meta", md.token_data);
  346. } else if (tok_last == TOK_SLASH && in_tag) {
  347. if (strcasecmp("head", md.token_data) == 0) {
  348. /* We are done here! */
  349. done = 1;
  350. }
  351. } else if (tok_last == TOK_EQUAL && looking_for_val) {
  352. if (saw_name) {
  353. if (name) efree(name);
  354. /* Get the NAME attr (Single word attr, non-quoted) */
  355. temp = name = estrndup(md.token_data, md.token_len);
  356. while (temp && *temp) {
  357. if (strchr(PHP_META_UNSAFE, *temp)) {
  358. *temp = '_';
  359. }
  360. temp++;
  361. }
  362. have_name = 1;
  363. } else if (saw_content) {
  364. if (value) efree(value);
  365. value = estrndup(md.token_data, md.token_len);
  366. have_content = 1;
  367. }
  368. looking_for_val = 0;
  369. } else {
  370. if (md.in_meta) {
  371. if (strcasecmp("name", md.token_data) == 0) {
  372. saw_name = 1;
  373. saw_content = 0;
  374. looking_for_val = 1;
  375. } else if (strcasecmp("content", md.token_data) == 0) {
  376. saw_name = 0;
  377. saw_content = 1;
  378. looking_for_val = 1;
  379. }
  380. }
  381. }
  382. } else if (tok == TOK_STRING && tok_last == TOK_EQUAL && looking_for_val) {
  383. if (saw_name) {
  384. if (name) efree(name);
  385. /* Get the NAME attr (Quoted single/double) */
  386. temp = name = estrndup(md.token_data, md.token_len);
  387. while (temp && *temp) {
  388. if (strchr(PHP_META_UNSAFE, *temp)) {
  389. *temp = '_';
  390. }
  391. temp++;
  392. }
  393. have_name = 1;
  394. } else if (saw_content) {
  395. if (value) efree(value);
  396. value = estrndup(md.token_data, md.token_len);
  397. have_content = 1;
  398. }
  399. looking_for_val = 0;
  400. } else if (tok == TOK_OPENTAG) {
  401. if (looking_for_val) {
  402. looking_for_val = 0;
  403. have_name = saw_name = 0;
  404. have_content = saw_content = 0;
  405. }
  406. in_tag = 1;
  407. } else if (tok == TOK_CLOSETAG) {
  408. if (have_name) {
  409. /* For BC */
  410. php_strtolower(name, strlen(name));
  411. if (have_content) {
  412. add_assoc_string(return_value, name, value);
  413. } else {
  414. add_assoc_string(return_value, name, "");
  415. }
  416. efree(name);
  417. if (value) efree(value);
  418. } else if (have_content) {
  419. efree(value);
  420. }
  421. name = value = NULL;
  422. /* Reset all of our flags */
  423. in_tag = looking_for_val = 0;
  424. have_name = saw_name = 0;
  425. have_content = saw_content = 0;
  426. md.in_meta = 0;
  427. }
  428. tok_last = tok;
  429. if (md.token_data)
  430. efree(md.token_data);
  431. md.token_data = NULL;
  432. }
  433. if (value) efree(value);
  434. if (name) efree(name);
  435. php_stream_close(md.stream);
  436. }
  437. /* }}} */
  438. /* {{{ proto string file_get_contents(string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]])
  439. Read the entire file into a string */
  440. PHP_FUNCTION(file_get_contents)
  441. {
  442. char *filename;
  443. size_t filename_len;
  444. zend_bool use_include_path = 0;
  445. php_stream *stream;
  446. zend_long offset = 0;
  447. zend_long maxlen = (ssize_t) PHP_STREAM_COPY_ALL;
  448. zval *zcontext = NULL;
  449. php_stream_context *context = NULL;
  450. zend_string *contents;
  451. /* Parse arguments */
  452. ZEND_PARSE_PARAMETERS_START(1, 5)
  453. Z_PARAM_PATH(filename, filename_len)
  454. Z_PARAM_OPTIONAL
  455. Z_PARAM_BOOL(use_include_path)
  456. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  457. Z_PARAM_LONG(offset)
  458. Z_PARAM_LONG(maxlen)
  459. ZEND_PARSE_PARAMETERS_END();
  460. if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
  461. php_error_docref(NULL, E_WARNING, "length must be greater than or equal to zero");
  462. RETURN_FALSE;
  463. }
  464. context = php_stream_context_from_zval(zcontext, 0);
  465. stream = php_stream_open_wrapper_ex(filename, "rb",
  466. (use_include_path ? USE_PATH : 0) | REPORT_ERRORS,
  467. NULL, context);
  468. if (!stream) {
  469. RETURN_FALSE;
  470. }
  471. if (offset != 0 && php_stream_seek(stream, offset, ((offset > 0) ? SEEK_SET : SEEK_END)) < 0) {
  472. php_error_docref(NULL, E_WARNING, "Failed to seek to position " ZEND_LONG_FMT " in the stream", offset);
  473. php_stream_close(stream);
  474. RETURN_FALSE;
  475. }
  476. if (maxlen > INT_MAX) {
  477. php_error_docref(NULL, E_WARNING, "maxlen truncated from " ZEND_LONG_FMT " to %d bytes", maxlen, INT_MAX);
  478. maxlen = INT_MAX;
  479. }
  480. if ((contents = php_stream_copy_to_mem(stream, maxlen, 0)) != NULL) {
  481. RETVAL_STR(contents);
  482. } else {
  483. RETVAL_EMPTY_STRING();
  484. }
  485. php_stream_close(stream);
  486. }
  487. /* }}} */
  488. /* {{{ proto int file_put_contents(string file, mixed data [, int flags [, resource context]])
  489. Write/Create a file with contents data and return the number of bytes written */
  490. PHP_FUNCTION(file_put_contents)
  491. {
  492. php_stream *stream;
  493. char *filename;
  494. size_t filename_len;
  495. zval *data;
  496. size_t numbytes = 0;
  497. zend_long flags = 0;
  498. zval *zcontext = NULL;
  499. php_stream_context *context = NULL;
  500. php_stream *srcstream = NULL;
  501. char mode[3] = "wb";
  502. ZEND_PARSE_PARAMETERS_START(2, 4)
  503. Z_PARAM_PATH(filename, filename_len)
  504. Z_PARAM_ZVAL(data)
  505. Z_PARAM_OPTIONAL
  506. Z_PARAM_LONG(flags)
  507. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  508. ZEND_PARSE_PARAMETERS_END();
  509. if (Z_TYPE_P(data) == IS_RESOURCE) {
  510. php_stream_from_zval(srcstream, data);
  511. }
  512. context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
  513. if (flags & PHP_FILE_APPEND) {
  514. mode[0] = 'a';
  515. } else if (flags & LOCK_EX) {
  516. /* check to make sure we are dealing with a regular file */
  517. if (php_memnstr(filename, "://", sizeof("://") - 1, filename + filename_len)) {
  518. if (strncasecmp(filename, "file://", sizeof("file://") - 1)) {
  519. php_error_docref(NULL, E_WARNING, "Exclusive locks may only be set for regular files");
  520. RETURN_FALSE;
  521. }
  522. }
  523. mode[0] = 'c';
  524. }
  525. mode[2] = '\0';
  526. stream = php_stream_open_wrapper_ex(filename, mode, ((flags & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
  527. if (stream == NULL) {
  528. RETURN_FALSE;
  529. }
  530. if (flags & LOCK_EX && (!php_stream_supports_lock(stream) || php_stream_lock(stream, LOCK_EX))) {
  531. php_stream_close(stream);
  532. php_error_docref(NULL, E_WARNING, "Exclusive locks are not supported for this stream");
  533. RETURN_FALSE;
  534. }
  535. if (mode[0] == 'c') {
  536. php_stream_truncate_set_size(stream, 0);
  537. }
  538. switch (Z_TYPE_P(data)) {
  539. case IS_RESOURCE: {
  540. size_t len;
  541. if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &len) != SUCCESS) {
  542. numbytes = -1;
  543. } else {
  544. if (len > ZEND_LONG_MAX) {
  545. php_error_docref(NULL, E_WARNING, "content truncated from %zu to " ZEND_LONG_FMT " bytes", len, ZEND_LONG_MAX);
  546. len = ZEND_LONG_MAX;
  547. }
  548. numbytes = len;
  549. }
  550. break;
  551. }
  552. case IS_NULL:
  553. case IS_LONG:
  554. case IS_DOUBLE:
  555. case IS_FALSE:
  556. case IS_TRUE:
  557. convert_to_string_ex(data);
  558. case IS_STRING:
  559. if (Z_STRLEN_P(data)) {
  560. numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data));
  561. if (numbytes != Z_STRLEN_P(data)) {
  562. php_error_docref(NULL, E_WARNING, "Only "ZEND_LONG_FMT" of %zd bytes written, possibly out of free disk space", numbytes, Z_STRLEN_P(data));
  563. numbytes = -1;
  564. }
  565. }
  566. break;
  567. case IS_ARRAY:
  568. if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
  569. size_t bytes_written;
  570. zval *tmp;
  571. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), tmp) {
  572. zend_string *t;
  573. zend_string *str = zval_get_tmp_string(tmp, &t);
  574. if (ZSTR_LEN(str)) {
  575. numbytes += ZSTR_LEN(str);
  576. bytes_written = php_stream_write(stream, ZSTR_VAL(str), ZSTR_LEN(str));
  577. if (bytes_written != ZSTR_LEN(str)) {
  578. php_error_docref(NULL, E_WARNING, "Failed to write %zd bytes to %s", ZSTR_LEN(str), filename);
  579. zend_tmp_string_release(t);
  580. numbytes = -1;
  581. break;
  582. }
  583. }
  584. zend_tmp_string_release(t);
  585. } ZEND_HASH_FOREACH_END();
  586. }
  587. break;
  588. case IS_OBJECT:
  589. if (Z_OBJ_HT_P(data) != NULL) {
  590. zval out;
  591. if (zend_std_cast_object_tostring(data, &out, IS_STRING) == SUCCESS) {
  592. numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRLEN(out));
  593. if (numbytes != Z_STRLEN(out)) {
  594. php_error_docref(NULL, E_WARNING, "Only "ZEND_LONG_FMT" of %zd bytes written, possibly out of free disk space", numbytes, Z_STRLEN(out));
  595. numbytes = -1;
  596. }
  597. zval_ptr_dtor_str(&out);
  598. break;
  599. }
  600. }
  601. default:
  602. numbytes = -1;
  603. break;
  604. }
  605. php_stream_close(stream);
  606. if (numbytes == (size_t)-1) {
  607. RETURN_FALSE;
  608. }
  609. RETURN_LONG(numbytes);
  610. }
  611. /* }}} */
  612. #define PHP_FILE_BUF_SIZE 80
  613. /* {{{ proto array file(string filename [, int flags[, resource context]])
  614. Read entire file into an array */
  615. PHP_FUNCTION(file)
  616. {
  617. char *filename;
  618. size_t filename_len;
  619. char *p, *s, *e;
  620. register int i = 0;
  621. char eol_marker = '\n';
  622. zend_long flags = 0;
  623. zend_bool use_include_path;
  624. zend_bool include_new_line;
  625. zend_bool skip_blank_lines;
  626. php_stream *stream;
  627. zval *zcontext = NULL;
  628. php_stream_context *context = NULL;
  629. zend_string *target_buf;
  630. /* Parse arguments */
  631. ZEND_PARSE_PARAMETERS_START(1, 3)
  632. Z_PARAM_PATH(filename, filename_len)
  633. Z_PARAM_OPTIONAL
  634. Z_PARAM_LONG(flags)
  635. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  636. ZEND_PARSE_PARAMETERS_END();
  637. if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
  638. php_error_docref(NULL, E_WARNING, "'" ZEND_LONG_FMT "' flag is not supported", flags);
  639. RETURN_FALSE;
  640. }
  641. use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH;
  642. include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES);
  643. skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES;
  644. context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
  645. stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
  646. if (!stream) {
  647. RETURN_FALSE;
  648. }
  649. /* Initialize return array */
  650. array_init(return_value);
  651. if ((target_buf = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0)) != NULL) {
  652. s = ZSTR_VAL(target_buf);
  653. e = ZSTR_VAL(target_buf) + ZSTR_LEN(target_buf);
  654. if (!(p = (char*)php_stream_locate_eol(stream, target_buf))) {
  655. p = e;
  656. goto parse_eol;
  657. }
  658. if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
  659. eol_marker = '\r';
  660. }
  661. /* for performance reasons the code is duplicated, so that the if (include_new_line)
  662. * will not need to be done for every single line in the file. */
  663. if (include_new_line) {
  664. do {
  665. p++;
  666. parse_eol:
  667. add_index_stringl(return_value, i++, s, p-s);
  668. s = p;
  669. } while ((p = memchr(p, eol_marker, (e-p))));
  670. } else {
  671. do {
  672. int windows_eol = 0;
  673. if (p != ZSTR_VAL(target_buf) && eol_marker == '\n' && *(p - 1) == '\r') {
  674. windows_eol++;
  675. }
  676. if (skip_blank_lines && !(p-s-windows_eol)) {
  677. s = ++p;
  678. continue;
  679. }
  680. add_index_stringl(return_value, i++, s, p-s-windows_eol);
  681. s = ++p;
  682. } while ((p = memchr(p, eol_marker, (e-p))));
  683. }
  684. /* handle any left overs of files without new lines */
  685. if (s != e) {
  686. p = e;
  687. goto parse_eol;
  688. }
  689. }
  690. if (target_buf) {
  691. zend_string_free(target_buf);
  692. }
  693. php_stream_close(stream);
  694. }
  695. /* }}} */
  696. /* {{{ proto string tempnam(string dir, string prefix)
  697. Create a unique filename in a directory */
  698. PHP_FUNCTION(tempnam)
  699. {
  700. char *dir, *prefix;
  701. size_t dir_len, prefix_len;
  702. zend_string *opened_path;
  703. int fd;
  704. zend_string *p;
  705. ZEND_PARSE_PARAMETERS_START(2, 2)
  706. Z_PARAM_PATH(dir, dir_len)
  707. Z_PARAM_PATH(prefix, prefix_len)
  708. ZEND_PARSE_PARAMETERS_END();
  709. if (php_check_open_basedir(dir)) {
  710. RETURN_FALSE;
  711. }
  712. p = php_basename(prefix, prefix_len, NULL, 0);
  713. if (ZSTR_LEN(p) > 64) {
  714. ZSTR_VAL(p)[63] = '\0';
  715. }
  716. RETVAL_FALSE;
  717. if ((fd = php_open_temporary_fd_ex(dir, ZSTR_VAL(p), &opened_path, 1)) >= 0) {
  718. close(fd);
  719. RETVAL_STR(opened_path);
  720. }
  721. zend_string_release_ex(p, 0);
  722. }
  723. /* }}} */
  724. /* {{{ proto resource tmpfile(void)
  725. Create a temporary file that will be deleted automatically after use */
  726. PHP_NAMED_FUNCTION(php_if_tmpfile)
  727. {
  728. php_stream *stream;
  729. if (zend_parse_parameters_none() == FAILURE) {
  730. return;
  731. }
  732. stream = php_stream_fopen_tmpfile();
  733. if (stream) {
  734. php_stream_to_zval(stream, return_value);
  735. } else {
  736. RETURN_FALSE;
  737. }
  738. }
  739. /* }}} */
  740. /* {{{ proto resource fopen(string filename, string mode [, bool use_include_path [, resource context]])
  741. Open a file or a URL and return a file pointer */
  742. PHP_NAMED_FUNCTION(php_if_fopen)
  743. {
  744. char *filename, *mode;
  745. size_t filename_len, mode_len;
  746. zend_bool use_include_path = 0;
  747. zval *zcontext = NULL;
  748. php_stream *stream;
  749. php_stream_context *context = NULL;
  750. ZEND_PARSE_PARAMETERS_START(2, 4)
  751. Z_PARAM_PATH(filename, filename_len)
  752. Z_PARAM_STRING(mode, mode_len)
  753. Z_PARAM_OPTIONAL
  754. Z_PARAM_BOOL(use_include_path)
  755. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  756. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  757. context = php_stream_context_from_zval(zcontext, 0);
  758. stream = php_stream_open_wrapper_ex(filename, mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
  759. if (stream == NULL) {
  760. RETURN_FALSE;
  761. }
  762. php_stream_to_zval(stream, return_value);
  763. }
  764. /* }}} */
  765. /* {{{ proto bool fclose(resource fp)
  766. Close an open file pointer */
  767. PHPAPI PHP_FUNCTION(fclose)
  768. {
  769. zval *res;
  770. php_stream *stream;
  771. ZEND_PARSE_PARAMETERS_START(1, 1)
  772. Z_PARAM_RESOURCE(res)
  773. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  774. PHP_STREAM_TO_ZVAL(stream, res);
  775. if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) {
  776. php_error_docref(NULL, E_WARNING, "%d is not a valid stream resource", stream->res->handle);
  777. RETURN_FALSE;
  778. }
  779. php_stream_free(stream,
  780. PHP_STREAM_FREE_KEEP_RSRC |
  781. (stream->is_persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE));
  782. RETURN_TRUE;
  783. }
  784. /* }}} */
  785. /* {{{ proto resource popen(string command, string mode)
  786. Execute a command and open either a read or a write pipe to it */
  787. PHP_FUNCTION(popen)
  788. {
  789. char *command, *mode;
  790. size_t command_len, mode_len;
  791. FILE *fp;
  792. php_stream *stream;
  793. char *posix_mode;
  794. ZEND_PARSE_PARAMETERS_START(2, 2)
  795. Z_PARAM_PATH(command, command_len)
  796. Z_PARAM_STRING(mode, mode_len)
  797. ZEND_PARSE_PARAMETERS_END();
  798. posix_mode = estrndup(mode, mode_len);
  799. #ifndef PHP_WIN32
  800. {
  801. char *z = memchr(posix_mode, 'b', mode_len);
  802. if (z) {
  803. memmove(z, z + 1, mode_len - (z - posix_mode));
  804. }
  805. }
  806. #endif
  807. fp = VCWD_POPEN(command, posix_mode);
  808. if (!fp) {
  809. php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));
  810. efree(posix_mode);
  811. RETURN_FALSE;
  812. }
  813. stream = php_stream_fopen_from_pipe(fp, mode);
  814. if (stream == NULL) {
  815. php_error_docref2(NULL, command, mode, E_WARNING, "%s", strerror(errno));
  816. RETVAL_FALSE;
  817. } else {
  818. php_stream_to_zval(stream, return_value);
  819. }
  820. efree(posix_mode);
  821. }
  822. /* }}} */
  823. /* {{{ proto int pclose(resource fp)
  824. Close a file pointer opened by popen() */
  825. PHP_FUNCTION(pclose)
  826. {
  827. zval *res;
  828. php_stream *stream;
  829. ZEND_PARSE_PARAMETERS_START(1, 1)
  830. Z_PARAM_RESOURCE(res)
  831. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  832. PHP_STREAM_TO_ZVAL(stream, res);
  833. FG(pclose_wait) = 1;
  834. zend_list_close(stream->res);
  835. FG(pclose_wait) = 0;
  836. RETURN_LONG(FG(pclose_ret));
  837. }
  838. /* }}} */
  839. /* {{{ proto bool feof(resource fp)
  840. Test for end-of-file on a file pointer */
  841. PHPAPI PHP_FUNCTION(feof)
  842. {
  843. zval *res;
  844. php_stream *stream;
  845. ZEND_PARSE_PARAMETERS_START(1, 1)
  846. Z_PARAM_RESOURCE(res)
  847. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  848. PHP_STREAM_TO_ZVAL(stream, res);
  849. if (php_stream_eof(stream)) {
  850. RETURN_TRUE;
  851. } else {
  852. RETURN_FALSE;
  853. }
  854. }
  855. /* }}} */
  856. /* {{{ proto string fgets(resource fp[, int length])
  857. Get a line from file pointer */
  858. PHPAPI PHP_FUNCTION(fgets)
  859. {
  860. zval *res;
  861. zend_long len = 1024;
  862. char *buf = NULL;
  863. int argc = ZEND_NUM_ARGS();
  864. size_t line_len = 0;
  865. zend_string *str;
  866. php_stream *stream;
  867. ZEND_PARSE_PARAMETERS_START(1, 2)
  868. Z_PARAM_RESOURCE(res)
  869. Z_PARAM_OPTIONAL
  870. Z_PARAM_LONG(len)
  871. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  872. PHP_STREAM_TO_ZVAL(stream, res);
  873. if (argc == 1) {
  874. /* ask streams to give us a buffer of an appropriate size */
  875. buf = php_stream_get_line(stream, NULL, 0, &line_len);
  876. if (buf == NULL) {
  877. RETURN_FALSE;
  878. }
  879. // TODO: avoid reallocation ???
  880. RETVAL_STRINGL(buf, line_len);
  881. efree(buf);
  882. } else if (argc > 1) {
  883. if (len <= 0) {
  884. php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
  885. RETURN_FALSE;
  886. }
  887. str = zend_string_alloc(len, 0);
  888. if (php_stream_get_line(stream, ZSTR_VAL(str), len, &line_len) == NULL) {
  889. zend_string_efree(str);
  890. RETURN_FALSE;
  891. }
  892. /* resize buffer if it's much larger than the result.
  893. * Only needed if the user requested a buffer size. */
  894. if (line_len < (size_t)len / 2) {
  895. str = zend_string_truncate(str, line_len, 0);
  896. } else {
  897. ZSTR_LEN(str) = line_len;
  898. }
  899. RETURN_NEW_STR(str);
  900. }
  901. }
  902. /* }}} */
  903. /* {{{ proto string fgetc(resource fp)
  904. Get a character from file pointer */
  905. PHPAPI PHP_FUNCTION(fgetc)
  906. {
  907. zval *res;
  908. char buf[2];
  909. int result;
  910. php_stream *stream;
  911. ZEND_PARSE_PARAMETERS_START(1, 1)
  912. Z_PARAM_RESOURCE(res)
  913. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  914. PHP_STREAM_TO_ZVAL(stream, res);
  915. result = php_stream_getc(stream);
  916. if (result == EOF) {
  917. RETVAL_FALSE;
  918. } else {
  919. buf[0] = result;
  920. buf[1] = '\0';
  921. RETURN_STRINGL(buf, 1);
  922. }
  923. }
  924. /* }}} */
  925. /* {{{ proto string fgetss(resource fp [, int length [, string allowable_tags]])
  926. Get a line from file pointer and strip HTML tags */
  927. PHPAPI PHP_FUNCTION(fgetss)
  928. {
  929. zval *fd;
  930. zend_long bytes = 0;
  931. size_t len = 0;
  932. size_t actual_len, retval_len;
  933. char *buf = NULL, *retval;
  934. php_stream *stream;
  935. char *allowed_tags=NULL;
  936. size_t allowed_tags_len=0;
  937. ZEND_PARSE_PARAMETERS_START(1, 3)
  938. Z_PARAM_RESOURCE(fd)
  939. Z_PARAM_OPTIONAL
  940. Z_PARAM_LONG(bytes)
  941. Z_PARAM_STRING(allowed_tags, allowed_tags_len)
  942. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  943. PHP_STREAM_TO_ZVAL(stream, fd);
  944. if (ZEND_NUM_ARGS() >= 2) {
  945. if (bytes <= 0) {
  946. php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
  947. RETURN_FALSE;
  948. }
  949. len = (size_t) bytes;
  950. buf = safe_emalloc(sizeof(char), (len + 1), 0);
  951. /*needed because recv doesn't set null char at end*/
  952. memset(buf, 0, len + 1);
  953. }
  954. if ((retval = php_stream_get_line(stream, buf, len, &actual_len)) == NULL) {
  955. if (buf != NULL) {
  956. efree(buf);
  957. }
  958. RETURN_FALSE;
  959. }
  960. retval_len = php_strip_tags(retval, actual_len, &stream->fgetss_state, allowed_tags, allowed_tags_len);
  961. // TODO: avoid reallocation ???
  962. RETVAL_STRINGL(retval, retval_len);
  963. efree(retval);
  964. }
  965. /* }}} */
  966. /* {{{ proto mixed fscanf(resource stream, string format [, string ...])
  967. Implements a mostly ANSI compatible fscanf() */
  968. PHP_FUNCTION(fscanf)
  969. {
  970. int result, argc = 0;
  971. size_t format_len;
  972. zval *args = NULL;
  973. zval *file_handle;
  974. char *buf, *format;
  975. size_t len;
  976. void *what;
  977. ZEND_PARSE_PARAMETERS_START(2, -1)
  978. Z_PARAM_RESOURCE(file_handle)
  979. Z_PARAM_STRING(format, format_len)
  980. Z_PARAM_VARIADIC('*', args, argc)
  981. ZEND_PARSE_PARAMETERS_END();
  982. what = zend_fetch_resource2(Z_RES_P(file_handle), "File-Handle", php_file_le_stream(), php_file_le_pstream());
  983. /* we can't do a ZEND_VERIFY_RESOURCE(what), otherwise we end up
  984. * with a leak if we have an invalid filehandle. This needs changing
  985. * if the code behind ZEND_VERIFY_RESOURCE changed. - cc */
  986. if (!what) {
  987. RETURN_FALSE;
  988. }
  989. buf = php_stream_get_line((php_stream *) what, NULL, 0, &len);
  990. if (buf == NULL) {
  991. RETURN_FALSE;
  992. }
  993. result = php_sscanf_internal(buf, format, argc, args, 0, return_value);
  994. efree(buf);
  995. if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
  996. WRONG_PARAM_COUNT;
  997. }
  998. }
  999. /* }}} */
  1000. /* {{{ proto int fwrite(resource fp, string str [, int length])
  1001. Binary-safe file write */
  1002. PHPAPI PHP_FUNCTION(fwrite)
  1003. {
  1004. zval *res;
  1005. char *input;
  1006. size_t inputlen;
  1007. size_t ret;
  1008. size_t num_bytes;
  1009. zend_long maxlen = 0;
  1010. php_stream *stream;
  1011. ZEND_PARSE_PARAMETERS_START(2, 3)
  1012. Z_PARAM_RESOURCE(res)
  1013. Z_PARAM_STRING(input, inputlen)
  1014. Z_PARAM_OPTIONAL
  1015. Z_PARAM_LONG(maxlen)
  1016. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1017. if (ZEND_NUM_ARGS() == 2) {
  1018. num_bytes = inputlen;
  1019. } else if (maxlen <= 0) {
  1020. num_bytes = 0;
  1021. } else {
  1022. num_bytes = MIN((size_t) maxlen, inputlen);
  1023. }
  1024. if (!num_bytes) {
  1025. RETURN_LONG(0);
  1026. }
  1027. PHP_STREAM_TO_ZVAL(stream, res);
  1028. ret = php_stream_write(stream, input, num_bytes);
  1029. RETURN_LONG(ret);
  1030. }
  1031. /* }}} */
  1032. /* {{{ proto bool fflush(resource fp)
  1033. Flushes output */
  1034. PHPAPI PHP_FUNCTION(fflush)
  1035. {
  1036. zval *res;
  1037. int ret;
  1038. php_stream *stream;
  1039. ZEND_PARSE_PARAMETERS_START(1, 1)
  1040. Z_PARAM_RESOURCE(res)
  1041. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1042. PHP_STREAM_TO_ZVAL(stream, res);
  1043. ret = php_stream_flush(stream);
  1044. if (ret) {
  1045. RETURN_FALSE;
  1046. }
  1047. RETURN_TRUE;
  1048. }
  1049. /* }}} */
  1050. /* {{{ proto bool rewind(resource fp)
  1051. Rewind the position of a file pointer */
  1052. PHPAPI PHP_FUNCTION(rewind)
  1053. {
  1054. zval *res;
  1055. php_stream *stream;
  1056. ZEND_PARSE_PARAMETERS_START(1, 1)
  1057. Z_PARAM_RESOURCE(res)
  1058. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1059. PHP_STREAM_TO_ZVAL(stream, res);
  1060. if (-1 == php_stream_rewind(stream)) {
  1061. RETURN_FALSE;
  1062. }
  1063. RETURN_TRUE;
  1064. }
  1065. /* }}} */
  1066. /* {{{ proto int ftell(resource fp)
  1067. Get file pointer's read/write position */
  1068. PHPAPI PHP_FUNCTION(ftell)
  1069. {
  1070. zval *res;
  1071. zend_long ret;
  1072. php_stream *stream;
  1073. ZEND_PARSE_PARAMETERS_START(1, 1)
  1074. Z_PARAM_RESOURCE(res)
  1075. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1076. PHP_STREAM_TO_ZVAL(stream, res);
  1077. ret = php_stream_tell(stream);
  1078. if (ret == -1) {
  1079. RETURN_FALSE;
  1080. }
  1081. RETURN_LONG(ret);
  1082. }
  1083. /* }}} */
  1084. /* {{{ proto int fseek(resource fp, int offset [, int whence])
  1085. Seek on a file pointer */
  1086. PHPAPI PHP_FUNCTION(fseek)
  1087. {
  1088. zval *res;
  1089. zend_long offset, whence = SEEK_SET;
  1090. php_stream *stream;
  1091. ZEND_PARSE_PARAMETERS_START(2, 3)
  1092. Z_PARAM_RESOURCE(res)
  1093. Z_PARAM_LONG(offset)
  1094. Z_PARAM_OPTIONAL
  1095. Z_PARAM_LONG(whence)
  1096. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1097. PHP_STREAM_TO_ZVAL(stream, res);
  1098. RETURN_LONG(php_stream_seek(stream, offset, (int) whence));
  1099. }
  1100. /* }}} */
  1101. /* {{{ php_mkdir
  1102. */
  1103. /* DEPRECATED APIs: Use php_stream_mkdir() instead */
  1104. PHPAPI int php_mkdir_ex(const char *dir, zend_long mode, int options)
  1105. {
  1106. int ret;
  1107. if (php_check_open_basedir(dir)) {
  1108. return -1;
  1109. }
  1110. if ((ret = VCWD_MKDIR(dir, (mode_t)mode)) < 0 && (options & REPORT_ERRORS)) {
  1111. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  1112. }
  1113. return ret;
  1114. }
  1115. PHPAPI int php_mkdir(const char *dir, zend_long mode)
  1116. {
  1117. return php_mkdir_ex(dir, mode, REPORT_ERRORS);
  1118. }
  1119. /* }}} */
  1120. /* {{{ proto bool mkdir(string pathname [, int mode [, bool recursive [, resource context]]])
  1121. Create a directory */
  1122. PHP_FUNCTION(mkdir)
  1123. {
  1124. char *dir;
  1125. size_t dir_len;
  1126. zval *zcontext = NULL;
  1127. zend_long mode = 0777;
  1128. zend_bool recursive = 0;
  1129. php_stream_context *context;
  1130. ZEND_PARSE_PARAMETERS_START(1, 4)
  1131. Z_PARAM_PATH(dir, dir_len)
  1132. Z_PARAM_OPTIONAL
  1133. Z_PARAM_LONG(mode)
  1134. Z_PARAM_BOOL(recursive)
  1135. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  1136. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1137. context = php_stream_context_from_zval(zcontext, 0);
  1138. RETURN_BOOL(php_stream_mkdir(dir, (int)mode, (recursive ? PHP_STREAM_MKDIR_RECURSIVE : 0) | REPORT_ERRORS, context));
  1139. }
  1140. /* }}} */
  1141. /* {{{ proto bool rmdir(string dirname[, resource context])
  1142. Remove a directory */
  1143. PHP_FUNCTION(rmdir)
  1144. {
  1145. char *dir;
  1146. size_t dir_len;
  1147. zval *zcontext = NULL;
  1148. php_stream_context *context;
  1149. ZEND_PARSE_PARAMETERS_START(1, 2)
  1150. Z_PARAM_PATH(dir, dir_len)
  1151. Z_PARAM_OPTIONAL
  1152. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  1153. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1154. context = php_stream_context_from_zval(zcontext, 0);
  1155. RETURN_BOOL(php_stream_rmdir(dir, REPORT_ERRORS, context));
  1156. }
  1157. /* }}} */
  1158. /* {{{ proto int readfile(string filename [, bool use_include_path[, resource context]])
  1159. Output a file or a URL */
  1160. PHP_FUNCTION(readfile)
  1161. {
  1162. char *filename;
  1163. size_t filename_len;
  1164. size_t size = 0;
  1165. zend_bool use_include_path = 0;
  1166. zval *zcontext = NULL;
  1167. php_stream *stream;
  1168. php_stream_context *context = NULL;
  1169. ZEND_PARSE_PARAMETERS_START(1, 3)
  1170. Z_PARAM_PATH(filename, filename_len)
  1171. Z_PARAM_OPTIONAL
  1172. Z_PARAM_BOOL(use_include_path)
  1173. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  1174. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1175. context = php_stream_context_from_zval(zcontext, 0);
  1176. stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
  1177. if (stream) {
  1178. size = php_stream_passthru(stream);
  1179. php_stream_close(stream);
  1180. RETURN_LONG(size);
  1181. }
  1182. RETURN_FALSE;
  1183. }
  1184. /* }}} */
  1185. /* {{{ proto int umask([int mask])
  1186. Return or change the umask */
  1187. PHP_FUNCTION(umask)
  1188. {
  1189. zend_long mask = 0;
  1190. int oldumask;
  1191. oldumask = umask(077);
  1192. if (BG(umask) == -1) {
  1193. BG(umask) = oldumask;
  1194. }
  1195. ZEND_PARSE_PARAMETERS_START(0, 1)
  1196. Z_PARAM_OPTIONAL
  1197. Z_PARAM_LONG(mask)
  1198. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1199. if (ZEND_NUM_ARGS() == 0) {
  1200. umask(oldumask);
  1201. } else {
  1202. umask((int) mask);
  1203. }
  1204. RETURN_LONG(oldumask);
  1205. }
  1206. /* }}} */
  1207. /* {{{ proto int fpassthru(resource fp)
  1208. Output all remaining data from a file pointer */
  1209. PHPAPI PHP_FUNCTION(fpassthru)
  1210. {
  1211. zval *res;
  1212. size_t size;
  1213. php_stream *stream;
  1214. ZEND_PARSE_PARAMETERS_START(1, 1)
  1215. Z_PARAM_RESOURCE(res)
  1216. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1217. PHP_STREAM_TO_ZVAL(stream, res);
  1218. size = php_stream_passthru(stream);
  1219. RETURN_LONG(size);
  1220. }
  1221. /* }}} */
  1222. /* {{{ proto bool rename(string old_name, string new_name[, resource context])
  1223. Rename a file */
  1224. PHP_FUNCTION(rename)
  1225. {
  1226. char *old_name, *new_name;
  1227. size_t old_name_len, new_name_len;
  1228. zval *zcontext = NULL;
  1229. php_stream_wrapper *wrapper;
  1230. php_stream_context *context;
  1231. ZEND_PARSE_PARAMETERS_START(2, 3)
  1232. Z_PARAM_PATH(old_name, old_name_len)
  1233. Z_PARAM_PATH(new_name, new_name_len)
  1234. Z_PARAM_OPTIONAL
  1235. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  1236. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1237. wrapper = php_stream_locate_url_wrapper(old_name, NULL, 0);
  1238. if (!wrapper || !wrapper->wops) {
  1239. php_error_docref(NULL, E_WARNING, "Unable to locate stream wrapper");
  1240. RETURN_FALSE;
  1241. }
  1242. if (!wrapper->wops->rename) {
  1243. php_error_docref(NULL, E_WARNING, "%s wrapper does not support renaming", wrapper->wops->label ? wrapper->wops->label : "Source");
  1244. RETURN_FALSE;
  1245. }
  1246. if (wrapper != php_stream_locate_url_wrapper(new_name, NULL, 0)) {
  1247. php_error_docref(NULL, E_WARNING, "Cannot rename a file across wrapper types");
  1248. RETURN_FALSE;
  1249. }
  1250. context = php_stream_context_from_zval(zcontext, 0);
  1251. RETURN_BOOL(wrapper->wops->rename(wrapper, old_name, new_name, 0, context));
  1252. }
  1253. /* }}} */
  1254. /* {{{ proto bool unlink(string filename[, context context])
  1255. Delete a file */
  1256. PHP_FUNCTION(unlink)
  1257. {
  1258. char *filename;
  1259. size_t filename_len;
  1260. php_stream_wrapper *wrapper;
  1261. zval *zcontext = NULL;
  1262. php_stream_context *context = NULL;
  1263. ZEND_PARSE_PARAMETERS_START(1, 2)
  1264. Z_PARAM_PATH(filename, filename_len)
  1265. Z_PARAM_OPTIONAL
  1266. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  1267. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1268. context = php_stream_context_from_zval(zcontext, 0);
  1269. wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
  1270. if (!wrapper || !wrapper->wops) {
  1271. php_error_docref(NULL, E_WARNING, "Unable to locate stream wrapper");
  1272. RETURN_FALSE;
  1273. }
  1274. if (!wrapper->wops->unlink) {
  1275. php_error_docref(NULL, E_WARNING, "%s does not allow unlinking", wrapper->wops->label ? wrapper->wops->label : "Wrapper");
  1276. RETURN_FALSE;
  1277. }
  1278. RETURN_BOOL(wrapper->wops->unlink(wrapper, filename, REPORT_ERRORS, context));
  1279. }
  1280. /* }}} */
  1281. /* {{{ proto bool ftruncate(resource fp, int size)
  1282. Truncate file to 'size' length */
  1283. PHP_NAMED_FUNCTION(php_if_ftruncate)
  1284. {
  1285. zval *fp;
  1286. zend_long size;
  1287. php_stream *stream;
  1288. ZEND_PARSE_PARAMETERS_START(2, 2)
  1289. Z_PARAM_RESOURCE(fp)
  1290. Z_PARAM_LONG(size)
  1291. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1292. if (size < 0) {
  1293. php_error_docref(NULL, E_WARNING, "Negative size is not supported");
  1294. RETURN_FALSE;
  1295. }
  1296. PHP_STREAM_TO_ZVAL(stream, fp);
  1297. if (!php_stream_truncate_supported(stream)) {
  1298. php_error_docref(NULL, E_WARNING, "Can't truncate this stream!");
  1299. RETURN_FALSE;
  1300. }
  1301. RETURN_BOOL(0 == php_stream_truncate_set_size(stream, size));
  1302. }
  1303. /* }}} */
  1304. /* {{{ proto array fstat(resource fp)
  1305. Stat() on a filehandle */
  1306. PHP_NAMED_FUNCTION(php_if_fstat)
  1307. {
  1308. zval *fp;
  1309. zval stat_dev, stat_ino, stat_mode, stat_nlink, stat_uid, stat_gid, stat_rdev,
  1310. stat_size, stat_atime, stat_mtime, stat_ctime, stat_blksize, stat_blocks;
  1311. php_stream *stream;
  1312. php_stream_statbuf stat_ssb;
  1313. char *stat_sb_names[13] = {
  1314. "dev", "ino", "mode", "nlink", "uid", "gid", "rdev",
  1315. "size", "atime", "mtime", "ctime", "blksize", "blocks"
  1316. };
  1317. ZEND_PARSE_PARAMETERS_START(1, 1)
  1318. Z_PARAM_RESOURCE(fp)
  1319. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1320. PHP_STREAM_TO_ZVAL(stream, fp);
  1321. if (php_stream_stat(stream, &stat_ssb)) {
  1322. RETURN_FALSE;
  1323. }
  1324. array_init(return_value);
  1325. ZVAL_LONG(&stat_dev, stat_ssb.sb.st_dev);
  1326. ZVAL_LONG(&stat_ino, stat_ssb.sb.st_ino);
  1327. ZVAL_LONG(&stat_mode, stat_ssb.sb.st_mode);
  1328. ZVAL_LONG(&stat_nlink, stat_ssb.sb.st_nlink);
  1329. ZVAL_LONG(&stat_uid, stat_ssb.sb.st_uid);
  1330. ZVAL_LONG(&stat_gid, stat_ssb.sb.st_gid);
  1331. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  1332. # ifdef PHP_WIN32
  1333. /* It is unsigned, so if a negative came from userspace, it'll
  1334. convert to UINT_MAX, but we wan't to keep the userspace value.
  1335. Almost the same as in php_fstat. This is ugly, but otherwise
  1336. we would have to maintain a fully compatible struct stat. */
  1337. if ((int)stat_ssb.sb.st_rdev < 0) {
  1338. ZVAL_LONG(&stat_rdev, (int)stat_ssb.sb.st_rdev);
  1339. } else {
  1340. ZVAL_LONG(&stat_rdev, stat_ssb.sb.st_rdev);
  1341. }
  1342. # else
  1343. ZVAL_LONG(&stat_rdev, stat_ssb.sb.st_rdev);
  1344. # endif
  1345. #else
  1346. ZVAL_LONG(&stat_rdev, -1);
  1347. #endif
  1348. ZVAL_LONG(&stat_size, stat_ssb.sb.st_size);
  1349. ZVAL_LONG(&stat_atime, stat_ssb.sb.st_atime);
  1350. ZVAL_LONG(&stat_mtime, stat_ssb.sb.st_mtime);
  1351. ZVAL_LONG(&stat_ctime, stat_ssb.sb.st_ctime);
  1352. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  1353. ZVAL_LONG(&stat_blksize, stat_ssb.sb.st_blksize);
  1354. #else
  1355. ZVAL_LONG(&stat_blksize,-1);
  1356. #endif
  1357. #ifdef HAVE_ST_BLOCKS
  1358. ZVAL_LONG(&stat_blocks, stat_ssb.sb.st_blocks);
  1359. #else
  1360. ZVAL_LONG(&stat_blocks,-1);
  1361. #endif
  1362. /* Store numeric indexes in proper order */
  1363. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_dev);
  1364. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ino);
  1365. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mode);
  1366. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_nlink);
  1367. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_uid);
  1368. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_gid);
  1369. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_rdev);
  1370. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_size);
  1371. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_atime);
  1372. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mtime);
  1373. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ctime);
  1374. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blksize);
  1375. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blocks);
  1376. /* Store string indexes referencing the same zval*/
  1377. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[0], strlen(stat_sb_names[0]), &stat_dev);
  1378. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[1], strlen(stat_sb_names[1]), &stat_ino);
  1379. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[2], strlen(stat_sb_names[2]), &stat_mode);
  1380. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[3], strlen(stat_sb_names[3]), &stat_nlink);
  1381. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[4], strlen(stat_sb_names[4]), &stat_uid);
  1382. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[5], strlen(stat_sb_names[5]), &stat_gid);
  1383. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[6], strlen(stat_sb_names[6]), &stat_rdev);
  1384. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[7], strlen(stat_sb_names[7]), &stat_size);
  1385. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[8], strlen(stat_sb_names[8]), &stat_atime);
  1386. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[9], strlen(stat_sb_names[9]), &stat_mtime);
  1387. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[10], strlen(stat_sb_names[10]), &stat_ctime);
  1388. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize);
  1389. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks);
  1390. }
  1391. /* }}} */
  1392. /* {{{ proto bool copy(string source_file, string destination_file [, resource context])
  1393. Copy a file */
  1394. PHP_FUNCTION(copy)
  1395. {
  1396. char *source, *target;
  1397. size_t source_len, target_len;
  1398. zval *zcontext = NULL;
  1399. php_stream_context *context;
  1400. ZEND_PARSE_PARAMETERS_START(2, 3)
  1401. Z_PARAM_PATH(source, source_len)
  1402. Z_PARAM_PATH(target, target_len)
  1403. Z_PARAM_OPTIONAL
  1404. Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
  1405. ZEND_PARSE_PARAMETERS_END();
  1406. if (php_stream_locate_url_wrapper(source, NULL, 0) == &php_plain_files_wrapper && php_check_open_basedir(source)) {
  1407. RETURN_FALSE;
  1408. }
  1409. context = php_stream_context_from_zval(zcontext, 0);
  1410. if (php_copy_file_ctx(source, target, 0, context) == SUCCESS) {
  1411. RETURN_TRUE;
  1412. } else {
  1413. RETURN_FALSE;
  1414. }
  1415. }
  1416. /* }}} */
  1417. /* {{{ php_copy_file
  1418. */
  1419. PHPAPI int php_copy_file(const char *src, const char *dest)
  1420. {
  1421. return php_copy_file_ctx(src, dest, 0, NULL);
  1422. }
  1423. /* }}} */
  1424. /* {{{ php_copy_file_ex
  1425. */
  1426. PHPAPI int php_copy_file_ex(const char *src, const char *dest, int src_flg)
  1427. {
  1428. return php_copy_file_ctx(src, dest, src_flg, NULL);
  1429. }
  1430. /* }}} */
  1431. /* {{{ php_copy_file_ctx
  1432. */
  1433. PHPAPI int php_copy_file_ctx(const char *src, const char *dest, int src_flg, php_stream_context *ctx)
  1434. {
  1435. php_stream *srcstream = NULL, *deststream = NULL;
  1436. int ret = FAILURE;
  1437. php_stream_statbuf src_s, dest_s;
  1438. switch (php_stream_stat_path_ex(src, 0, &src_s, ctx)) {
  1439. case -1:
  1440. /* non-statable stream */
  1441. goto safe_to_copy;
  1442. break;
  1443. case 0:
  1444. break;
  1445. default: /* failed to stat file, does not exist? */
  1446. return ret;
  1447. }
  1448. if (S_ISDIR(src_s.sb.st_mode)) {
  1449. php_error_docref(NULL, E_WARNING, "The first argument to copy() function cannot be a directory");
  1450. return FAILURE;
  1451. }
  1452. switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET | PHP_STREAM_URL_STAT_NOCACHE, &dest_s, ctx)) {
  1453. case -1:
  1454. /* non-statable stream */
  1455. goto safe_to_copy;
  1456. break;
  1457. case 0:
  1458. break;
  1459. default: /* failed to stat file, does not exist? */
  1460. return ret;
  1461. }
  1462. if (S_ISDIR(dest_s.sb.st_mode)) {
  1463. php_error_docref(NULL, E_WARNING, "The second argument to copy() function cannot be a directory");
  1464. return FAILURE;
  1465. }
  1466. if (!src_s.sb.st_ino || !dest_s.sb.st_ino) {
  1467. goto no_stat;
  1468. }
  1469. if (src_s.sb.st_ino == dest_s.sb.st_ino && src_s.sb.st_dev == dest_s.sb.st_dev) {
  1470. return ret;
  1471. } else {
  1472. goto safe_to_copy;
  1473. }
  1474. no_stat:
  1475. {
  1476. char *sp, *dp;
  1477. int res;
  1478. if ((sp = expand_filepath(src, NULL)) == NULL) {
  1479. return ret;
  1480. }
  1481. if ((dp = expand_filepath(dest, NULL)) == NULL) {
  1482. efree(sp);
  1483. goto safe_to_copy;
  1484. }
  1485. res =
  1486. #ifndef PHP_WIN32
  1487. !strcmp(sp, dp);
  1488. #else
  1489. !strcasecmp(sp, dp);
  1490. #endif
  1491. efree(sp);
  1492. efree(dp);
  1493. if (res) {
  1494. return ret;
  1495. }
  1496. }
  1497. safe_to_copy:
  1498. srcstream = php_stream_open_wrapper_ex(src, "rb", src_flg | REPORT_ERRORS, NULL, ctx);
  1499. if (!srcstream) {
  1500. return ret;
  1501. }
  1502. deststream = php_stream_open_wrapper_ex(dest, "wb", REPORT_ERRORS, NULL, ctx);
  1503. if (srcstream && deststream) {
  1504. ret = php_stream_copy_to_stream_ex(srcstream, deststream, PHP_STREAM_COPY_ALL, NULL);
  1505. }
  1506. if (srcstream) {
  1507. php_stream_close(srcstream);
  1508. }
  1509. if (deststream) {
  1510. php_stream_close(deststream);
  1511. }
  1512. return ret;
  1513. }
  1514. /* }}} */
  1515. /* {{{ proto string fread(resource fp, int length)
  1516. Binary-safe file read */
  1517. PHPAPI PHP_FUNCTION(fread)
  1518. {
  1519. zval *res;
  1520. zend_long len;
  1521. php_stream *stream;
  1522. ZEND_PARSE_PARAMETERS_START(2, 2)
  1523. Z_PARAM_RESOURCE(res)
  1524. Z_PARAM_LONG(len)
  1525. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1526. PHP_STREAM_TO_ZVAL(stream, res);
  1527. if (len <= 0) {
  1528. php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
  1529. RETURN_FALSE;
  1530. }
  1531. ZVAL_NEW_STR(return_value, zend_string_alloc(len, 0));
  1532. Z_STRLEN_P(return_value) = php_stream_read(stream, Z_STRVAL_P(return_value), len);
  1533. /* needed because recv/read/gzread doesn't put a null at the end*/
  1534. Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
  1535. if (Z_STRLEN_P(return_value) < len / 2) {
  1536. Z_STR_P(return_value) = zend_string_truncate(Z_STR_P(return_value), Z_STRLEN_P(return_value), 0);
  1537. }
  1538. }
  1539. /* }}} */
  1540. static const char *php_fgetcsv_lookup_trailing_spaces(const char *ptr, size_t len, const char delimiter) /* {{{ */
  1541. {
  1542. int inc_len;
  1543. unsigned char last_chars[2] = { 0, 0 };
  1544. while (len > 0) {
  1545. inc_len = (*ptr == '\0' ? 1 : php_mblen(ptr, len));
  1546. switch (inc_len) {
  1547. case -2:
  1548. case -1:
  1549. inc_len = 1;
  1550. php_mb_reset();
  1551. break;
  1552. case 0:
  1553. goto quit_loop;
  1554. case 1:
  1555. default:
  1556. last_chars[0] = last_chars[1];
  1557. last_chars[1] = *ptr;
  1558. break;
  1559. }
  1560. ptr += inc_len;
  1561. len -= inc_len;
  1562. }
  1563. quit_loop:
  1564. switch (last_chars[1]) {
  1565. case '\n':
  1566. if (last_chars[0] == '\r') {
  1567. return ptr - 2;
  1568. }
  1569. /* break is omitted intentionally */
  1570. case '\r':
  1571. return ptr - 1;
  1572. }
  1573. return ptr;
  1574. }
  1575. /* }}} */
  1576. #define FPUTCSV_FLD_CHK(c) memchr(ZSTR_VAL(field_str), c, ZSTR_LEN(field_str))
  1577. /* {{{ proto int fputcsv(resource fp, array fields [, string delimiter [, string enclosure [, string escape_char]]])
  1578. Format line as CSV and write to file pointer */
  1579. PHP_FUNCTION(fputcsv)
  1580. {
  1581. char delimiter = ','; /* allow this to be set as parameter */
  1582. char enclosure = '"'; /* allow this to be set as parameter */
  1583. char escape_char = '\\'; /* allow this to be set as parameter */
  1584. php_stream *stream;
  1585. zval *fp = NULL, *fields = NULL;
  1586. size_t ret;
  1587. char *delimiter_str = NULL, *enclosure_str = NULL, *escape_str = NULL;
  1588. size_t delimiter_str_len = 0, enclosure_str_len = 0, escape_str_len = 0;
  1589. ZEND_PARSE_PARAMETERS_START(2, 5)
  1590. Z_PARAM_RESOURCE(fp)
  1591. Z_PARAM_ARRAY(fields)
  1592. Z_PARAM_OPTIONAL
  1593. Z_PARAM_STRING(delimiter_str, delimiter_str_len)
  1594. Z_PARAM_STRING(enclosure_str, enclosure_str_len)
  1595. Z_PARAM_STRING(escape_str, escape_str_len)
  1596. ZEND_PARSE_PARAMETERS_END();
  1597. if (delimiter_str != NULL) {
  1598. /* Make sure that there is at least one character in string */
  1599. if (delimiter_str_len < 1) {
  1600. php_error_docref(NULL, E_WARNING, "delimiter must be a character");
  1601. RETURN_FALSE;
  1602. } else if (delimiter_str_len > 1) {
  1603. php_error_docref(NULL, E_NOTICE, "delimiter must be a single character");
  1604. }
  1605. /* use first character from string */
  1606. delimiter = *delimiter_str;
  1607. }
  1608. if (enclosure_str != NULL) {
  1609. if (enclosure_str_len < 1) {
  1610. php_error_docref(NULL, E_WARNING, "enclosure must be a character");
  1611. RETURN_FALSE;
  1612. } else if (enclosure_str_len > 1) {
  1613. php_error_docref(NULL, E_NOTICE, "enclosure must be a single character");
  1614. }
  1615. /* use first character from string */
  1616. enclosure = *enclosure_str;
  1617. }
  1618. if (escape_str != NULL) {
  1619. if (escape_str_len < 1) {
  1620. php_error_docref(NULL, E_WARNING, "escape must be a character");
  1621. RETURN_FALSE;
  1622. } else if (escape_str_len > 1) {
  1623. php_error_docref(NULL, E_NOTICE, "escape must be a single character");
  1624. }
  1625. /* use first character from string */
  1626. escape_char = *escape_str;
  1627. }
  1628. PHP_STREAM_TO_ZVAL(stream, fp);
  1629. ret = php_fputcsv(stream, fields, delimiter, enclosure, escape_char);
  1630. RETURN_LONG(ret);
  1631. }
  1632. /* }}} */
  1633. /* {{{ PHPAPI size_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, char escape_char) */
  1634. PHPAPI size_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, char escape_char)
  1635. {
  1636. int count, i = 0;
  1637. size_t ret;
  1638. zval *field_tmp;
  1639. smart_str csvline = {0};
  1640. count = zend_hash_num_elements(Z_ARRVAL_P(fields));
  1641. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(fields), field_tmp) {
  1642. zend_string *tmp_field_str;
  1643. zend_string *field_str = zval_get_tmp_string(field_tmp, &tmp_field_str);
  1644. /* enclose a field that contains a delimiter, an enclosure character, or a newline */
  1645. if (FPUTCSV_FLD_CHK(delimiter) ||
  1646. FPUTCSV_FLD_CHK(enclosure) ||
  1647. FPUTCSV_FLD_CHK(escape_char) ||
  1648. FPUTCSV_FLD_CHK('\n') ||
  1649. FPUTCSV_FLD_CHK('\r') ||
  1650. FPUTCSV_FLD_CHK('\t') ||
  1651. FPUTCSV_FLD_CHK(' ')
  1652. ) {
  1653. char *ch = ZSTR_VAL(field_str);
  1654. char *end = ch + ZSTR_LEN(field_str);
  1655. int escaped = 0;
  1656. smart_str_appendc(&csvline, enclosure);
  1657. while (ch < end) {
  1658. if (*ch == escape_char) {
  1659. escaped = 1;
  1660. } else if (!escaped && *ch == enclosure) {
  1661. smart_str_appendc(&csvline, enclosure);
  1662. } else {
  1663. escaped = 0;
  1664. }
  1665. smart_str_appendc(&csvline, *ch);
  1666. ch++;
  1667. }
  1668. smart_str_appendc(&csvline, enclosure);
  1669. } else {
  1670. smart_str_append(&csvline, field_str);
  1671. }
  1672. if (++i != count) {
  1673. smart_str_appendl(&csvline, &delimiter, 1);
  1674. }
  1675. zend_tmp_string_release(tmp_field_str);
  1676. } ZEND_HASH_FOREACH_END();
  1677. smart_str_appendc(&csvline, '\n');
  1678. smart_str_0(&csvline);
  1679. ret = php_stream_write(stream, ZSTR_VAL(csvline.s), ZSTR_LEN(csvline.s));
  1680. smart_str_free(&csvline);
  1681. return ret;
  1682. }
  1683. /* }}} */
  1684. /* {{{ proto array fgetcsv(resource fp [,int length [, string delimiter [, string enclosure [, string escape]]]])
  1685. Get line from file pointer and parse for CSV fields */
  1686. PHP_FUNCTION(fgetcsv)
  1687. {
  1688. char delimiter = ','; /* allow this to be set as parameter */
  1689. char enclosure = '"'; /* allow this to be set as parameter */
  1690. char escape = '\\';
  1691. /* first section exactly as php_fgetss */
  1692. zend_long len = 0;
  1693. size_t buf_len;
  1694. char *buf;
  1695. php_stream *stream;
  1696. {
  1697. zval *fd, *len_zv = NULL;
  1698. char *delimiter_str = NULL;
  1699. size_t delimiter_str_len = 0;
  1700. char *enclosure_str = NULL;
  1701. size_t enclosure_str_len = 0;
  1702. char *escape_str = NULL;
  1703. size_t escape_str_len = 0;
  1704. ZEND_PARSE_PARAMETERS_START(1, 5)
  1705. Z_PARAM_RESOURCE(fd)
  1706. Z_PARAM_OPTIONAL
  1707. Z_PARAM_ZVAL(len_zv)
  1708. Z_PARAM_STRING(delimiter_str, delimiter_str_len)
  1709. Z_PARAM_STRING(enclosure_str, enclosure_str_len)
  1710. Z_PARAM_STRING(escape_str, escape_str_len)
  1711. ZEND_PARSE_PARAMETERS_END();
  1712. if (delimiter_str != NULL) {
  1713. /* Make sure that there is at least one character in string */
  1714. if (delimiter_str_len < 1) {
  1715. php_error_docref(NULL, E_WARNING, "delimiter must be a character");
  1716. RETURN_FALSE;
  1717. } else if (delimiter_str_len > 1) {
  1718. php_error_docref(NULL, E_NOTICE, "delimiter must be a single character");
  1719. }
  1720. /* use first character from string */
  1721. delimiter = delimiter_str[0];
  1722. }
  1723. if (enclosure_str != NULL) {
  1724. if (enclosure_str_len < 1) {
  1725. php_error_docref(NULL, E_WARNING, "enclosure must be a character");
  1726. RETURN_FALSE;
  1727. } else if (enclosure_str_len > 1) {
  1728. php_error_docref(NULL, E_NOTICE, "enclosure must be a single character");
  1729. }
  1730. /* use first character from string */
  1731. enclosure = enclosure_str[0];
  1732. }
  1733. if (escape_str != NULL) {
  1734. if (escape_str_len < 1) {
  1735. php_error_docref(NULL, E_WARNING, "escape must be character");
  1736. RETURN_FALSE;
  1737. } else if (escape_str_len > 1) {
  1738. php_error_docref(NULL, E_NOTICE, "escape must be a single character");
  1739. }
  1740. escape = escape_str[0];
  1741. }
  1742. if (len_zv != NULL && Z_TYPE_P(len_zv) != IS_NULL) {
  1743. len = zval_get_long(len_zv);
  1744. if (len < 0) {
  1745. php_error_docref(NULL, E_WARNING, "Length parameter may not be negative");
  1746. RETURN_FALSE;
  1747. } else if (len == 0) {
  1748. len = -1;
  1749. }
  1750. } else {
  1751. len = -1;
  1752. }
  1753. PHP_STREAM_TO_ZVAL(stream, fd);
  1754. }
  1755. if (len < 0) {
  1756. if ((buf = php_stream_get_line(stream, NULL, 0, &buf_len)) == NULL) {
  1757. RETURN_FALSE;
  1758. }
  1759. } else {
  1760. buf = emalloc(len + 1);
  1761. if (php_stream_get_line(stream, buf, len + 1, &buf_len) == NULL) {
  1762. efree(buf);
  1763. RETURN_FALSE;
  1764. }
  1765. }
  1766. php_fgetcsv(stream, delimiter, enclosure, escape, buf_len, buf, return_value);
  1767. }
  1768. /* }}} */
  1769. PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, char escape_char, size_t buf_len, char *buf, zval *return_value) /* {{{ */
  1770. {
  1771. char *temp, *tptr, *bptr, *line_end, *limit;
  1772. size_t temp_len, line_end_len;
  1773. int inc_len;
  1774. zend_bool first_field = 1;
  1775. /* initialize internal state */
  1776. php_mb_reset();
  1777. /* Now into new section that parses buf for delimiter/enclosure fields */
  1778. /* Strip trailing space from buf, saving end of line in case required for enclosure field */
  1779. bptr = buf;
  1780. tptr = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len, delimiter);
  1781. line_end_len = buf_len - (size_t)(tptr - buf);
  1782. line_end = limit = tptr;
  1783. /* reserve workspace for building each individual field */
  1784. temp_len = buf_len;
  1785. temp = emalloc(temp_len + line_end_len + 1);
  1786. /* Initialize return array */
  1787. array_init(return_value);
  1788. /* Main loop to read CSV fields */
  1789. /* NB this routine will return a single null entry for a blank line */
  1790. do {
  1791. char *comp_end, *hunk_begin;
  1792. tptr = temp;
  1793. inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
  1794. if (inc_len == 1) {
  1795. char *tmp = bptr;
  1796. while ((*tmp != delimiter) && isspace((int)*(unsigned char *)tmp)) {
  1797. tmp++;
  1798. }
  1799. if (*tmp == enclosure) {
  1800. bptr = tmp;
  1801. }
  1802. }
  1803. if (first_field && bptr == line_end) {
  1804. add_next_index_null(return_value);
  1805. break;
  1806. }
  1807. first_field = 0;
  1808. /* 2. Read field, leaving bptr pointing at start of next field */
  1809. if (inc_len != 0 && *bptr == enclosure) {
  1810. int state = 0;
  1811. bptr++; /* move on to first character in field */
  1812. hunk_begin = bptr;
  1813. /* 2A. handle enclosure delimited field */
  1814. for (;;) {
  1815. switch (inc_len) {
  1816. case 0:
  1817. switch (state) {
  1818. case 2:
  1819. memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
  1820. tptr += (bptr - hunk_begin - 1);
  1821. hunk_begin = bptr;
  1822. goto quit_loop_2;
  1823. case 1:
  1824. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1825. tptr += (bptr - hunk_begin);
  1826. hunk_begin = bptr;
  1827. /* break is omitted intentionally */
  1828. case 0: {
  1829. char *new_buf;
  1830. size_t new_len;
  1831. char *new_temp;
  1832. if (hunk_begin != line_end) {
  1833. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1834. tptr += (bptr - hunk_begin);
  1835. hunk_begin = bptr;
  1836. }
  1837. /* add the embedded line end to the field */
  1838. memcpy(tptr, line_end, line_end_len);
  1839. tptr += line_end_len;
  1840. if (stream == NULL) {
  1841. goto quit_loop_2;
  1842. } else if ((new_buf = php_stream_get_line(stream, NULL, 0, &new_len)) == NULL) {
  1843. /* we've got an unterminated enclosure,
  1844. * assign all the data from the start of
  1845. * the enclosure to end of data to the
  1846. * last element */
  1847. if ((size_t)temp_len > (size_t)(limit - buf)) {
  1848. goto quit_loop_2;
  1849. }
  1850. zend_array_destroy(Z_ARR_P(return_value));
  1851. RETVAL_FALSE;
  1852. goto out;
  1853. }
  1854. temp_len += new_len;
  1855. new_temp = erealloc(temp, temp_len);
  1856. tptr = new_temp + (size_t)(tptr - temp);
  1857. temp = new_temp;
  1858. efree(buf);
  1859. buf_len = new_len;
  1860. bptr = buf = new_buf;
  1861. hunk_begin = buf;
  1862. line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len, delimiter);
  1863. line_end_len = buf_len - (size_t)(limit - buf);
  1864. state = 0;
  1865. } break;
  1866. }
  1867. break;
  1868. case -2:
  1869. case -1:
  1870. php_mb_reset();
  1871. /* break is omitted intentionally */
  1872. case 1:
  1873. /* we need to determine if the enclosure is
  1874. * 'real' or is it escaped */
  1875. switch (state) {
  1876. case 1: /* escaped */
  1877. bptr++;
  1878. state = 0;
  1879. break;
  1880. case 2: /* embedded enclosure ? let's check it */
  1881. if (*bptr != enclosure) {
  1882. /* real enclosure */
  1883. memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
  1884. tptr += (bptr - hunk_begin - 1);
  1885. hunk_begin = bptr;
  1886. goto quit_loop_2;
  1887. }
  1888. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1889. tptr += (bptr - hunk_begin);
  1890. bptr++;
  1891. hunk_begin = bptr;
  1892. state = 0;
  1893. break;
  1894. default:
  1895. if (*bptr == enclosure) {
  1896. state = 2;
  1897. } else if (*bptr == escape_char) {
  1898. state = 1;
  1899. }
  1900. bptr++;
  1901. break;
  1902. }
  1903. break;
  1904. default:
  1905. switch (state) {
  1906. case 2:
  1907. /* real enclosure */
  1908. memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
  1909. tptr += (bptr - hunk_begin - 1);
  1910. hunk_begin = bptr;
  1911. goto quit_loop_2;
  1912. case 1:
  1913. bptr += inc_len;
  1914. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1915. tptr += (bptr - hunk_begin);
  1916. hunk_begin = bptr;
  1917. state = 0;
  1918. break;
  1919. default:
  1920. bptr += inc_len;
  1921. break;
  1922. }
  1923. break;
  1924. }
  1925. inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
  1926. }
  1927. quit_loop_2:
  1928. /* look up for a delimiter */
  1929. for (;;) {
  1930. switch (inc_len) {
  1931. case 0:
  1932. goto quit_loop_3;
  1933. case -2:
  1934. case -1:
  1935. inc_len = 1;
  1936. php_mb_reset();
  1937. /* break is omitted intentionally */
  1938. case 1:
  1939. if (*bptr == delimiter) {
  1940. goto quit_loop_3;
  1941. }
  1942. break;
  1943. default:
  1944. break;
  1945. }
  1946. bptr += inc_len;
  1947. inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
  1948. }
  1949. quit_loop_3:
  1950. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1951. tptr += (bptr - hunk_begin);
  1952. bptr += inc_len;
  1953. comp_end = tptr;
  1954. } else {
  1955. /* 2B. Handle non-enclosure field */
  1956. hunk_begin = bptr;
  1957. for (;;) {
  1958. switch (inc_len) {
  1959. case 0:
  1960. goto quit_loop_4;
  1961. case -2:
  1962. case -1:
  1963. inc_len = 1;
  1964. php_mb_reset();
  1965. /* break is omitted intentionally */
  1966. case 1:
  1967. if (*bptr == delimiter) {
  1968. goto quit_loop_4;
  1969. }
  1970. break;
  1971. default:
  1972. break;
  1973. }
  1974. bptr += inc_len;
  1975. inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
  1976. }
  1977. quit_loop_4:
  1978. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1979. tptr += (bptr - hunk_begin);
  1980. comp_end = (char *)php_fgetcsv_lookup_trailing_spaces(temp, tptr - temp, delimiter);
  1981. if (*bptr == delimiter) {
  1982. bptr++;
  1983. }
  1984. }
  1985. /* 3. Now pass our field back to php */
  1986. *comp_end = '\0';
  1987. add_next_index_stringl(return_value, temp, comp_end - temp);
  1988. } while (inc_len > 0);
  1989. out:
  1990. efree(temp);
  1991. if (stream) {
  1992. efree(buf);
  1993. }
  1994. }
  1995. /* }}} */
  1996. #if HAVE_REALPATH || defined(ZTS)
  1997. /* {{{ proto string realpath(string path)
  1998. Return the resolved path */
  1999. PHP_FUNCTION(realpath)
  2000. {
  2001. char *filename;
  2002. size_t filename_len;
  2003. char resolved_path_buff[MAXPATHLEN];
  2004. ZEND_PARSE_PARAMETERS_START(1, 1)
  2005. Z_PARAM_PATH(filename, filename_len)
  2006. ZEND_PARSE_PARAMETERS_END();
  2007. if (VCWD_REALPATH(filename, resolved_path_buff)) {
  2008. if (php_check_open_basedir(resolved_path_buff)) {
  2009. RETURN_FALSE;
  2010. }
  2011. #ifdef ZTS
  2012. if (VCWD_ACCESS(resolved_path_buff, F_OK)) {
  2013. RETURN_FALSE;
  2014. }
  2015. #endif
  2016. RETURN_STRING(resolved_path_buff);
  2017. } else {
  2018. RETURN_FALSE;
  2019. }
  2020. }
  2021. /* }}} */
  2022. #endif
  2023. /* See http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2 */
  2024. #define PHP_META_HTML401_CHARS "-_.:"
  2025. /* {{{ php_next_meta_token
  2026. Tokenizes an HTML file for get_meta_tags */
  2027. php_meta_tags_token php_next_meta_token(php_meta_tags_data *md)
  2028. {
  2029. int ch = 0, compliment;
  2030. char buff[META_DEF_BUFSIZE + 1];
  2031. memset((void *)buff, 0, META_DEF_BUFSIZE + 1);
  2032. while (md->ulc || (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)))) {
  2033. if (php_stream_eof(md->stream)) {
  2034. break;
  2035. }
  2036. if (md->ulc) {
  2037. ch = md->lc;
  2038. md->ulc = 0;
  2039. }
  2040. switch (ch) {
  2041. case '<':
  2042. return TOK_OPENTAG;
  2043. break;
  2044. case '>':
  2045. return TOK_CLOSETAG;
  2046. break;
  2047. case '=':
  2048. return TOK_EQUAL;
  2049. break;
  2050. case '/':
  2051. return TOK_SLASH;
  2052. break;
  2053. case '\'':
  2054. case '"':
  2055. compliment = ch;
  2056. md->token_len = 0;
  2057. while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && ch != compliment && ch != '<' && ch != '>') {
  2058. buff[(md->token_len)++] = ch;
  2059. if (md->token_len == META_DEF_BUFSIZE) {
  2060. break;
  2061. }
  2062. }
  2063. if (ch == '<' || ch == '>') {
  2064. /* Was just an apostrohpe */
  2065. md->ulc = 1;
  2066. md->lc = ch;
  2067. }
  2068. /* We don't need to alloc unless we are in a meta tag */
  2069. if (md->in_meta) {
  2070. md->token_data = (char *) emalloc(md->token_len + 1);
  2071. memcpy(md->token_data, buff, md->token_len+1);
  2072. }
  2073. return TOK_STRING;
  2074. break;
  2075. case '\n':
  2076. case '\r':
  2077. case '\t':
  2078. break;
  2079. case ' ':
  2080. return TOK_SPACE;
  2081. break;
  2082. default:
  2083. if (isalnum(ch)) {
  2084. md->token_len = 0;
  2085. buff[(md->token_len)++] = ch;
  2086. while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && (isalnum(ch) || strchr(PHP_META_HTML401_CHARS, ch))) {
  2087. buff[(md->token_len)++] = ch;
  2088. if (md->token_len == META_DEF_BUFSIZE) {
  2089. break;
  2090. }
  2091. }
  2092. /* This is ugly, but we have to replace ungetc */
  2093. if (!isalpha(ch) && ch != '-') {
  2094. md->ulc = 1;
  2095. md->lc = ch;
  2096. }
  2097. md->token_data = (char *) emalloc(md->token_len + 1);
  2098. memcpy(md->token_data, buff, md->token_len+1);
  2099. return TOK_ID;
  2100. } else {
  2101. return TOK_OTHER;
  2102. }
  2103. break;
  2104. }
  2105. }
  2106. return TOK_EOF;
  2107. }
  2108. /* }}} */
  2109. #ifdef HAVE_FNMATCH
  2110. /* {{{ proto bool fnmatch(string pattern, string filename [, int flags])
  2111. Match filename against pattern */
  2112. PHP_FUNCTION(fnmatch)
  2113. {
  2114. char *pattern, *filename;
  2115. size_t pattern_len, filename_len;
  2116. zend_long flags = 0;
  2117. ZEND_PARSE_PARAMETERS_START(2, 3)
  2118. Z_PARAM_PATH(pattern, pattern_len)
  2119. Z_PARAM_PATH(filename, filename_len)
  2120. Z_PARAM_OPTIONAL
  2121. Z_PARAM_LONG(flags)
  2122. ZEND_PARSE_PARAMETERS_END();
  2123. if (filename_len >= MAXPATHLEN) {
  2124. php_error_docref(NULL, E_WARNING, "Filename exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  2125. RETURN_FALSE;
  2126. }
  2127. if (pattern_len >= MAXPATHLEN) {
  2128. php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  2129. RETURN_FALSE;
  2130. }
  2131. RETURN_BOOL( ! fnmatch( pattern, filename, (int)flags ));
  2132. }
  2133. /* }}} */
  2134. #endif
  2135. /* {{{ proto string sys_get_temp_dir()
  2136. Returns directory path used for temporary files */
  2137. PHP_FUNCTION(sys_get_temp_dir)
  2138. {
  2139. if (zend_parse_parameters_none() == FAILURE) {
  2140. return;
  2141. }
  2142. RETURN_STRING((char *)php_get_temporary_directory());
  2143. }
  2144. /* }}} */
  2145. /*
  2146. * Local variables:
  2147. * tab-width: 4
  2148. * c-basic-offset: 4
  2149. * End:
  2150. * vim600: noet sw=4 ts=4 fdm=marker
  2151. * vim<600: noet sw=4 ts=4
  2152. */