spl_directory.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177
  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. | Author: Marcus Boerger <helly@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_ini.h"
  23. #include "ext/standard/info.h"
  24. #include "ext/standard/file.h"
  25. #include "ext/standard/php_string.h"
  26. #include "zend_compile.h"
  27. #include "zend_exceptions.h"
  28. #include "zend_interfaces.h"
  29. #include "php_spl.h"
  30. #include "spl_functions.h"
  31. #include "spl_engine.h"
  32. #include "spl_iterators.h"
  33. #include "spl_directory.h"
  34. #include "spl_exceptions.h"
  35. #include "php.h"
  36. #include "fopen_wrappers.h"
  37. #include "ext/standard/basic_functions.h"
  38. #include "ext/standard/php_filestat.h"
  39. #define SPL_HAS_FLAG(flags, test_flag) ((flags & test_flag) ? 1 : 0)
  40. /* declare the class handlers */
  41. static zend_object_handlers spl_filesystem_object_handlers;
  42. /* includes handler to validate object state when retrieving methods */
  43. static zend_object_handlers spl_filesystem_object_check_handlers;
  44. /* decalre the class entry */
  45. PHPAPI zend_class_entry *spl_ce_SplFileInfo;
  46. PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
  47. PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
  48. PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
  49. PHPAPI zend_class_entry *spl_ce_GlobIterator;
  50. PHPAPI zend_class_entry *spl_ce_SplFileObject;
  51. PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
  52. static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{ */
  53. {
  54. if (intern->u.file.current_line) {
  55. efree(intern->u.file.current_line);
  56. intern->u.file.current_line = NULL;
  57. }
  58. if (!Z_ISUNDEF(intern->u.file.current_zval)) {
  59. zval_ptr_dtor(&intern->u.file.current_zval);
  60. ZVAL_UNDEF(&intern->u.file.current_zval);
  61. }
  62. } /* }}} */
  63. static void spl_filesystem_object_destroy_object(zend_object *object) /* {{{ */
  64. {
  65. spl_filesystem_object *intern = spl_filesystem_from_obj(object);
  66. zend_objects_destroy_object(object);
  67. switch(intern->type) {
  68. case SPL_FS_DIR:
  69. if (intern->u.dir.dirp) {
  70. php_stream_close(intern->u.dir.dirp);
  71. intern->u.dir.dirp = NULL;
  72. }
  73. break;
  74. case SPL_FS_FILE:
  75. if (intern->u.file.stream) {
  76. /*
  77. if (intern->u.file.zcontext) {
  78. zend_list_delref(Z_RESVAL_P(intern->zcontext));
  79. }
  80. */
  81. if (!intern->u.file.stream->is_persistent) {
  82. php_stream_close(intern->u.file.stream);
  83. } else {
  84. php_stream_pclose(intern->u.file.stream);
  85. }
  86. intern->u.file.stream = NULL;
  87. ZVAL_UNDEF(&intern->u.file.zresource);
  88. }
  89. break;
  90. default:
  91. break;
  92. }
  93. } /* }}} */
  94. static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
  95. {
  96. spl_filesystem_object *intern = spl_filesystem_from_obj(object);
  97. if (intern->oth_handler && intern->oth_handler->dtor) {
  98. intern->oth_handler->dtor(intern);
  99. }
  100. zend_object_std_dtor(&intern->std);
  101. if (intern->_path) {
  102. efree(intern->_path);
  103. }
  104. if (intern->file_name) {
  105. efree(intern->file_name);
  106. }
  107. switch(intern->type) {
  108. case SPL_FS_INFO:
  109. break;
  110. case SPL_FS_DIR:
  111. if (intern->u.dir.sub_path) {
  112. efree(intern->u.dir.sub_path);
  113. }
  114. break;
  115. case SPL_FS_FILE:
  116. if (intern->u.file.open_mode) {
  117. efree(intern->u.file.open_mode);
  118. }
  119. if (intern->orig_path) {
  120. efree(intern->orig_path);
  121. }
  122. spl_filesystem_file_free_line(intern);
  123. break;
  124. }
  125. } /* }}} */
  126. /* {{{ spl_ce_dir_object_new */
  127. /* creates the object by
  128. - allocating memory
  129. - initializing the object members
  130. - storing the object
  131. - setting it's handlers
  132. called from
  133. - clone
  134. - new
  135. */
  136. static zend_object *spl_filesystem_object_new_ex(zend_class_entry *class_type)
  137. {
  138. spl_filesystem_object *intern;
  139. intern = zend_object_alloc(sizeof(spl_filesystem_object), class_type);
  140. /* intern->type = SPL_FS_INFO; done by set 0 */
  141. intern->file_class = spl_ce_SplFileObject;
  142. intern->info_class = spl_ce_SplFileInfo;
  143. zend_object_std_init(&intern->std, class_type);
  144. object_properties_init(&intern->std, class_type);
  145. intern->std.handlers = &spl_filesystem_object_handlers;
  146. return &intern->std;
  147. }
  148. /* }}} */
  149. /* {{{ spl_filesystem_object_new */
  150. /* See spl_filesystem_object_new_ex */
  151. static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
  152. {
  153. return spl_filesystem_object_new_ex(class_type);
  154. }
  155. /* }}} */
  156. /* {{{ spl_filesystem_object_new_check */
  157. static zend_object *spl_filesystem_object_new_check(zend_class_entry *class_type)
  158. {
  159. spl_filesystem_object *ret = spl_filesystem_from_obj(spl_filesystem_object_new_ex(class_type));
  160. ret->std.handlers = &spl_filesystem_object_check_handlers;
  161. return &ret->std;
  162. }
  163. /* }}} */
  164. PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, size_t *len) /* {{{ */
  165. {
  166. #ifdef HAVE_GLOB
  167. if (intern->type == SPL_FS_DIR) {
  168. if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
  169. return php_glob_stream_get_path(intern->u.dir.dirp, 0, len);
  170. }
  171. }
  172. #endif
  173. if (len) {
  174. *len = intern->_path_len;
  175. }
  176. return intern->_path;
  177. } /* }}} */
  178. static inline void spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
  179. {
  180. char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
  181. switch (intern->type) {
  182. case SPL_FS_INFO:
  183. case SPL_FS_FILE:
  184. if (!intern->file_name) {
  185. php_error_docref(NULL, E_ERROR, "Object not initialized");
  186. }
  187. break;
  188. case SPL_FS_DIR:
  189. {
  190. size_t path_len = 0;
  191. char *path = spl_filesystem_object_get_path(intern, &path_len);
  192. if (intern->file_name) {
  193. efree(intern->file_name);
  194. }
  195. /* if there is parent path, ammend it, otherwise just use the given path as is */
  196. if (path_len == 0) {
  197. intern->file_name_len = spprintf(
  198. &intern->file_name, 0, "%s", intern->u.dir.entry.d_name);
  199. } else {
  200. intern->file_name_len = spprintf(
  201. &intern->file_name, 0, "%s%c%s", path, slash, intern->u.dir.entry.d_name);
  202. }
  203. }
  204. break;
  205. }
  206. } /* }}} */
  207. static int spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
  208. {
  209. if (!intern->u.dir.dirp || !php_stream_readdir(intern->u.dir.dirp, &intern->u.dir.entry)) {
  210. intern->u.dir.entry.d_name[0] = '\0';
  211. return 0;
  212. } else {
  213. return 1;
  214. }
  215. }
  216. /* }}} */
  217. #define IS_SLASH_AT(zs, pos) (IS_SLASH(zs[pos]))
  218. static inline int spl_filesystem_is_dot(const char * d_name) /* {{{ */
  219. {
  220. return !strcmp(d_name, ".") || !strcmp(d_name, "..");
  221. }
  222. /* }}} */
  223. /* {{{ spl_filesystem_dir_open */
  224. /* open a directory resource */
  225. static void spl_filesystem_dir_open(spl_filesystem_object* intern, char *path)
  226. {
  227. int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
  228. intern->type = SPL_FS_DIR;
  229. intern->_path_len = strlen(path);
  230. intern->u.dir.dirp = php_stream_opendir(path, REPORT_ERRORS, FG(default_context));
  231. if (intern->_path_len > 1 && IS_SLASH_AT(path, intern->_path_len-1)) {
  232. intern->_path = estrndup(path, --intern->_path_len);
  233. } else {
  234. intern->_path = estrndup(path, intern->_path_len);
  235. }
  236. intern->u.dir.index = 0;
  237. if (EG(exception) || intern->u.dir.dirp == NULL) {
  238. intern->u.dir.entry.d_name[0] = '\0';
  239. if (!EG(exception)) {
  240. /* open failed w/out notice (turned to exception due to EH_THROW) */
  241. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
  242. "Failed to open directory \"%s\"", path);
  243. }
  244. } else {
  245. do {
  246. spl_filesystem_dir_read(intern);
  247. } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  248. }
  249. }
  250. /* }}} */
  251. static int spl_filesystem_file_open(spl_filesystem_object *intern, int use_include_path, int silent) /* {{{ */
  252. {
  253. zval tmp;
  254. intern->type = SPL_FS_FILE;
  255. php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, &tmp);
  256. if (Z_TYPE(tmp) == IS_TRUE) {
  257. intern->u.file.open_mode = NULL;
  258. intern->file_name = NULL;
  259. zend_throw_exception_ex(spl_ce_LogicException, 0, "Cannot use SplFileObject with directories");
  260. return FAILURE;
  261. }
  262. intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0);
  263. intern->u.file.stream = php_stream_open_wrapper_ex(intern->file_name, intern->u.file.open_mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context);
  264. if (!intern->file_name_len || !intern->u.file.stream) {
  265. if (!EG(exception)) {
  266. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot open file '%s'", intern->file_name_len ? intern->file_name : "");
  267. }
  268. intern->file_name = NULL; /* until here it is not a copy */
  269. intern->u.file.open_mode = NULL;
  270. return FAILURE;
  271. }
  272. /*
  273. if (intern->u.file.zcontext) {
  274. //zend_list_addref(Z_RES_VAL(intern->u.file.zcontext));
  275. Z_ADDREF_P(intern->u.file.zcontext);
  276. }
  277. */
  278. if (intern->file_name_len > 1 && IS_SLASH_AT(intern->file_name, intern->file_name_len-1)) {
  279. intern->file_name_len--;
  280. }
  281. intern->orig_path = estrndup(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path));
  282. intern->file_name = estrndup(intern->file_name, intern->file_name_len);
  283. intern->u.file.open_mode = estrndup(intern->u.file.open_mode, intern->u.file.open_mode_len);
  284. /* avoid reference counting in debug mode, thus do it manually */
  285. ZVAL_RES(&intern->u.file.zresource, intern->u.file.stream->res);
  286. /*!!! TODO: maybe bug?
  287. Z_SET_REFCOUNT(intern->u.file.zresource, 1);
  288. */
  289. intern->u.file.delimiter = ',';
  290. intern->u.file.enclosure = '"';
  291. intern->u.file.escape = '\\';
  292. intern->u.file.func_getCurr = zend_hash_str_find_ptr(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline") - 1);
  293. return SUCCESS;
  294. } /* }}} */
  295. /* {{{ spl_filesystem_object_clone */
  296. /* Local zend_object creation (on stack)
  297. Load the 'other' object
  298. Create a new empty object (See spl_filesystem_object_new_ex)
  299. Open the directory
  300. Clone other members (properties)
  301. */
  302. static zend_object *spl_filesystem_object_clone(zval *zobject)
  303. {
  304. zend_object *old_object;
  305. zend_object *new_object;
  306. spl_filesystem_object *intern;
  307. spl_filesystem_object *source;
  308. int index, skip_dots;
  309. old_object = Z_OBJ_P(zobject);
  310. source = spl_filesystem_from_obj(old_object);
  311. new_object = spl_filesystem_object_new_ex(old_object->ce);
  312. intern = spl_filesystem_from_obj(new_object);
  313. intern->flags = source->flags;
  314. switch (source->type) {
  315. case SPL_FS_INFO:
  316. intern->_path_len = source->_path_len;
  317. intern->_path = estrndup(source->_path, source->_path_len);
  318. intern->file_name_len = source->file_name_len;
  319. intern->file_name = estrndup(source->file_name, intern->file_name_len);
  320. break;
  321. case SPL_FS_DIR:
  322. spl_filesystem_dir_open(intern, source->_path);
  323. /* read until we hit the position in which we were before */
  324. skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
  325. for(index = 0; index < source->u.dir.index; ++index) {
  326. do {
  327. spl_filesystem_dir_read(intern);
  328. } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  329. }
  330. intern->u.dir.index = index;
  331. break;
  332. case SPL_FS_FILE:
  333. ZEND_ASSERT(0);
  334. }
  335. intern->file_class = source->file_class;
  336. intern->info_class = source->info_class;
  337. intern->oth = source->oth;
  338. intern->oth_handler = source->oth_handler;
  339. zend_objects_clone_members(new_object, old_object);
  340. if (intern->oth_handler && intern->oth_handler->clone) {
  341. intern->oth_handler->clone(source, intern);
  342. }
  343. return new_object;
  344. }
  345. /* }}} */
  346. void spl_filesystem_info_set_filename(spl_filesystem_object *intern, char *path, size_t len, size_t use_copy) /* {{{ */
  347. {
  348. char *p1, *p2;
  349. if (intern->file_name) {
  350. efree(intern->file_name);
  351. }
  352. intern->file_name = use_copy ? estrndup(path, len) : path;
  353. intern->file_name_len = len;
  354. while (intern->file_name_len > 1 && IS_SLASH_AT(intern->file_name, intern->file_name_len-1)) {
  355. intern->file_name[intern->file_name_len-1] = 0;
  356. intern->file_name_len--;
  357. }
  358. p1 = strrchr(intern->file_name, '/');
  359. #if defined(PHP_WIN32)
  360. p2 = strrchr(intern->file_name, '\\');
  361. #else
  362. p2 = 0;
  363. #endif
  364. if (p1 || p2) {
  365. intern->_path_len = ((p1 > p2 ? p1 : p2) - intern->file_name);
  366. } else {
  367. intern->_path_len = 0;
  368. }
  369. if (intern->_path) {
  370. efree(intern->_path);
  371. }
  372. intern->_path = estrndup(path, intern->_path_len);
  373. } /* }}} */
  374. static spl_filesystem_object *spl_filesystem_object_create_info(spl_filesystem_object *source, char *file_path, size_t file_path_len, int use_copy, zend_class_entry *ce, zval *return_value) /* {{{ */
  375. {
  376. spl_filesystem_object *intern;
  377. zval arg1;
  378. zend_error_handling error_handling;
  379. if (!file_path || !file_path_len) {
  380. #if defined(PHP_WIN32)
  381. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot create SplFileInfo for empty path");
  382. if (file_path && !use_copy) {
  383. efree(file_path);
  384. }
  385. #else
  386. if (file_path && !use_copy) {
  387. efree(file_path);
  388. }
  389. file_path_len = 1;
  390. file_path = "/";
  391. #endif
  392. return NULL;
  393. }
  394. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
  395. ce = ce ? ce : source->info_class;
  396. zend_update_class_constants(ce);
  397. intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
  398. ZVAL_OBJ(return_value, &intern->std);
  399. if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
  400. ZVAL_STRINGL(&arg1, file_path, file_path_len);
  401. zend_call_method_with_1_params(return_value, ce, &ce->constructor, "__construct", NULL, &arg1);
  402. zval_ptr_dtor(&arg1);
  403. } else {
  404. spl_filesystem_info_set_filename(intern, file_path, file_path_len, use_copy);
  405. }
  406. zend_restore_error_handling(&error_handling);
  407. return intern;
  408. } /* }}} */
  409. static spl_filesystem_object *spl_filesystem_object_create_type(int ht, spl_filesystem_object *source, int type, zend_class_entry *ce, zval *return_value) /* {{{ */
  410. {
  411. spl_filesystem_object *intern;
  412. zend_bool use_include_path = 0;
  413. zval arg1, arg2;
  414. zend_error_handling error_handling;
  415. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
  416. switch (source->type) {
  417. case SPL_FS_INFO:
  418. case SPL_FS_FILE:
  419. break;
  420. case SPL_FS_DIR:
  421. if (!source->u.dir.entry.d_name[0]) {
  422. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Could not open file");
  423. zend_restore_error_handling(&error_handling);
  424. return NULL;
  425. }
  426. }
  427. switch (type) {
  428. case SPL_FS_INFO:
  429. ce = ce ? ce : source->info_class;
  430. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  431. break;
  432. }
  433. intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
  434. ZVAL_OBJ(return_value, &intern->std);
  435. spl_filesystem_object_get_file_name(source);
  436. if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
  437. ZVAL_STRINGL(&arg1, source->file_name, source->file_name_len);
  438. zend_call_method_with_1_params(return_value, ce, &ce->constructor, "__construct", NULL, &arg1);
  439. zval_ptr_dtor(&arg1);
  440. } else {
  441. intern->file_name = estrndup(source->file_name, source->file_name_len);
  442. intern->file_name_len = source->file_name_len;
  443. intern->_path = spl_filesystem_object_get_path(source, &intern->_path_len);
  444. intern->_path = estrndup(intern->_path, intern->_path_len);
  445. }
  446. break;
  447. case SPL_FS_FILE:
  448. ce = ce ? ce : source->file_class;
  449. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  450. break;
  451. }
  452. intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
  453. ZVAL_OBJ(return_value, &intern->std);
  454. spl_filesystem_object_get_file_name(source);
  455. if (ce->constructor->common.scope != spl_ce_SplFileObject) {
  456. ZVAL_STRINGL(&arg1, source->file_name, source->file_name_len);
  457. ZVAL_STRINGL(&arg2, "r", 1);
  458. zend_call_method_with_2_params(return_value, ce, &ce->constructor, "__construct", NULL, &arg1, &arg2);
  459. zval_ptr_dtor(&arg1);
  460. zval_ptr_dtor(&arg2);
  461. } else {
  462. intern->file_name = source->file_name;
  463. intern->file_name_len = source->file_name_len;
  464. intern->_path = spl_filesystem_object_get_path(source, &intern->_path_len);
  465. intern->_path = estrndup(intern->_path, intern->_path_len);
  466. intern->u.file.open_mode = "r";
  467. intern->u.file.open_mode_len = 1;
  468. if (ht && zend_parse_parameters(ht, "|sbr",
  469. &intern->u.file.open_mode, &intern->u.file.open_mode_len,
  470. &use_include_path, &intern->u.file.zcontext) == FAILURE) {
  471. zend_restore_error_handling(&error_handling);
  472. intern->u.file.open_mode = NULL;
  473. intern->file_name = NULL;
  474. zval_ptr_dtor(return_value);
  475. ZVAL_NULL(return_value);
  476. return NULL;
  477. }
  478. if (spl_filesystem_file_open(intern, use_include_path, 0) == FAILURE) {
  479. zend_restore_error_handling(&error_handling);
  480. zval_ptr_dtor(return_value);
  481. ZVAL_NULL(return_value);
  482. return NULL;
  483. }
  484. }
  485. break;
  486. case SPL_FS_DIR:
  487. zend_restore_error_handling(&error_handling);
  488. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Operation not supported");
  489. return NULL;
  490. }
  491. zend_restore_error_handling(&error_handling);
  492. return NULL;
  493. } /* }}} */
  494. static int spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
  495. {
  496. return d_name[0] == '\0' || spl_filesystem_is_dot(d_name);
  497. }
  498. /* }}} */
  499. static char *spl_filesystem_object_get_pathname(spl_filesystem_object *intern, size_t *len) { /* {{{ */
  500. switch (intern->type) {
  501. case SPL_FS_INFO:
  502. case SPL_FS_FILE:
  503. *len = intern->file_name_len;
  504. return intern->file_name;
  505. case SPL_FS_DIR:
  506. if (intern->u.dir.entry.d_name[0]) {
  507. spl_filesystem_object_get_file_name(intern);
  508. *len = intern->file_name_len;
  509. return intern->file_name;
  510. }
  511. }
  512. *len = 0;
  513. return NULL;
  514. }
  515. /* }}} */
  516. static HashTable *spl_filesystem_object_get_debug_info(zval *object, int *is_temp) /* {{{ */
  517. {
  518. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(object);
  519. zval tmp;
  520. HashTable *rv;
  521. zend_string *pnstr;
  522. char *path;
  523. size_t path_len;
  524. char stmp[2];
  525. *is_temp = 1;
  526. if (!intern->std.properties) {
  527. rebuild_object_properties(&intern->std);
  528. }
  529. rv = zend_array_dup(intern->std.properties);
  530. pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "pathName", sizeof("pathName")-1);
  531. path = spl_filesystem_object_get_pathname(intern, &path_len);
  532. ZVAL_STRINGL(&tmp, path, path_len);
  533. zend_symtable_update(rv, pnstr, &tmp);
  534. zend_string_release_ex(pnstr, 0);
  535. if (intern->file_name) {
  536. pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "fileName", sizeof("fileName")-1);
  537. spl_filesystem_object_get_path(intern, &path_len);
  538. if (path_len && path_len < intern->file_name_len) {
  539. ZVAL_STRINGL(&tmp, intern->file_name + path_len + 1, intern->file_name_len - (path_len + 1));
  540. } else {
  541. ZVAL_STRINGL(&tmp, intern->file_name, intern->file_name_len);
  542. }
  543. zend_symtable_update(rv, pnstr, &tmp);
  544. zend_string_release_ex(pnstr, 0);
  545. }
  546. if (intern->type == SPL_FS_DIR) {
  547. #ifdef HAVE_GLOB
  548. pnstr = spl_gen_private_prop_name(spl_ce_DirectoryIterator, "glob", sizeof("glob")-1);
  549. if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
  550. ZVAL_STRINGL(&tmp, intern->_path, intern->_path_len);
  551. } else {
  552. ZVAL_FALSE(&tmp);
  553. }
  554. zend_symtable_update(rv, pnstr, &tmp);
  555. zend_string_release_ex(pnstr, 0);
  556. #endif
  557. pnstr = spl_gen_private_prop_name(spl_ce_RecursiveDirectoryIterator, "subPathName", sizeof("subPathName")-1);
  558. if (intern->u.dir.sub_path) {
  559. ZVAL_STRINGL(&tmp, intern->u.dir.sub_path, intern->u.dir.sub_path_len);
  560. } else {
  561. ZVAL_EMPTY_STRING(&tmp);
  562. }
  563. zend_symtable_update(rv, pnstr, &tmp);
  564. zend_string_release_ex(pnstr, 0);
  565. }
  566. if (intern->type == SPL_FS_FILE) {
  567. pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "openMode", sizeof("openMode")-1);
  568. ZVAL_STRINGL(&tmp, intern->u.file.open_mode, intern->u.file.open_mode_len);
  569. zend_symtable_update(rv, pnstr, &tmp);
  570. zend_string_release_ex(pnstr, 0);
  571. stmp[1] = '\0';
  572. stmp[0] = intern->u.file.delimiter;
  573. pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "delimiter", sizeof("delimiter")-1);
  574. ZVAL_STRINGL(&tmp, stmp, 1);
  575. zend_symtable_update(rv, pnstr, &tmp);
  576. zend_string_release_ex(pnstr, 0);
  577. stmp[0] = intern->u.file.enclosure;
  578. pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "enclosure", sizeof("enclosure")-1);
  579. ZVAL_STRINGL(&tmp, stmp, 1);
  580. zend_symtable_update(rv, pnstr, &tmp);
  581. zend_string_release_ex(pnstr, 0);
  582. }
  583. return rv;
  584. }
  585. /* }}} */
  586. zend_function *spl_filesystem_object_get_method_check(zend_object **object, zend_string *method, const zval *key) /* {{{ */
  587. {
  588. spl_filesystem_object *fsobj = spl_filesystem_from_obj(*object);
  589. if (fsobj->u.dir.dirp == NULL && fsobj->orig_path == NULL) {
  590. zend_function *func;
  591. zend_string *tmp = zend_string_init("_bad_state_ex", sizeof("_bad_state_ex") - 1, 0);
  592. func = zend_std_get_method(object, tmp, NULL);
  593. zend_string_release_ex(tmp, 0);
  594. return func;
  595. }
  596. return zend_std_get_method(object, method, key);
  597. }
  598. /* }}} */
  599. #define DIT_CTOR_FLAGS 0x00000001
  600. #define DIT_CTOR_GLOB 0x00000002
  601. void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long ctor_flags) /* {{{ */
  602. {
  603. spl_filesystem_object *intern;
  604. char *path;
  605. int parsed;
  606. size_t len;
  607. zend_long flags;
  608. zend_error_handling error_handling;
  609. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
  610. if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
  611. flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
  612. parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &path, &len, &flags);
  613. } else {
  614. flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
  615. parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &len);
  616. }
  617. if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_SKIPDOTS)) {
  618. flags |= SPL_FILE_DIR_SKIPDOTS;
  619. }
  620. if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_UNIXPATHS)) {
  621. flags |= SPL_FILE_DIR_UNIXPATHS;
  622. }
  623. if (parsed == FAILURE) {
  624. zend_restore_error_handling(&error_handling);
  625. return;
  626. }
  627. if (!len) {
  628. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Directory name must not be empty.");
  629. zend_restore_error_handling(&error_handling);
  630. return;
  631. }
  632. intern = Z_SPLFILESYSTEM_P(getThis());
  633. if (intern->_path) {
  634. /* object is alreay initialized */
  635. zend_restore_error_handling(&error_handling);
  636. php_error_docref(NULL, E_WARNING, "Directory object is already initialized");
  637. return;
  638. }
  639. intern->flags = flags;
  640. #ifdef HAVE_GLOB
  641. if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && strstr(path, "glob://") != path) {
  642. spprintf(&path, 0, "glob://%s", path);
  643. spl_filesystem_dir_open(intern, path);
  644. efree(path);
  645. } else
  646. #endif
  647. {
  648. spl_filesystem_dir_open(intern, path);
  649. }
  650. intern->u.dir.is_recursive = instanceof_function(intern->std.ce, spl_ce_RecursiveDirectoryIterator) ? 1 : 0;
  651. zend_restore_error_handling(&error_handling);
  652. }
  653. /* }}} */
  654. /* {{{ proto DirectoryIterator::__construct(string path)
  655. Cronstructs a new dir iterator from a path. */
  656. SPL_METHOD(DirectoryIterator, __construct)
  657. {
  658. spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  659. }
  660. /* }}} */
  661. /* {{{ proto void DirectoryIterator::rewind()
  662. Rewind dir back to the start */
  663. SPL_METHOD(DirectoryIterator, rewind)
  664. {
  665. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  666. if (zend_parse_parameters_none() == FAILURE) {
  667. return;
  668. }
  669. intern->u.dir.index = 0;
  670. if (intern->u.dir.dirp) {
  671. php_stream_rewinddir(intern->u.dir.dirp);
  672. }
  673. spl_filesystem_dir_read(intern);
  674. }
  675. /* }}} */
  676. /* {{{ proto string DirectoryIterator::key()
  677. Return current dir entry */
  678. SPL_METHOD(DirectoryIterator, key)
  679. {
  680. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  681. if (zend_parse_parameters_none() == FAILURE) {
  682. return;
  683. }
  684. if (intern->u.dir.dirp) {
  685. RETURN_LONG(intern->u.dir.index);
  686. } else {
  687. RETURN_FALSE;
  688. }
  689. }
  690. /* }}} */
  691. /* {{{ proto DirectoryIterator DirectoryIterator::current()
  692. Return this (needed for Iterator interface) */
  693. SPL_METHOD(DirectoryIterator, current)
  694. {
  695. if (zend_parse_parameters_none() == FAILURE) {
  696. return;
  697. }
  698. ZVAL_OBJ(return_value, Z_OBJ_P(getThis()));
  699. Z_ADDREF_P(return_value);
  700. }
  701. /* }}} */
  702. /* {{{ proto void DirectoryIterator::next()
  703. Move to next entry */
  704. SPL_METHOD(DirectoryIterator, next)
  705. {
  706. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  707. int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
  708. if (zend_parse_parameters_none() == FAILURE) {
  709. return;
  710. }
  711. intern->u.dir.index++;
  712. do {
  713. spl_filesystem_dir_read(intern);
  714. } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  715. if (intern->file_name) {
  716. efree(intern->file_name);
  717. intern->file_name = NULL;
  718. }
  719. }
  720. /* }}} */
  721. /* {{{ proto void DirectoryIterator::seek(int position)
  722. Seek to the given position */
  723. SPL_METHOD(DirectoryIterator, seek)
  724. {
  725. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  726. zval retval;
  727. zend_long pos;
  728. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &pos) == FAILURE) {
  729. return;
  730. }
  731. if (intern->u.dir.index > pos) {
  732. /* we first rewind */
  733. zend_call_method_with_0_params(&EX(This), Z_OBJCE(EX(This)), &intern->u.dir.func_rewind, "rewind", NULL);
  734. }
  735. while (intern->u.dir.index < pos) {
  736. int valid = 0;
  737. zend_call_method_with_0_params(&EX(This), Z_OBJCE(EX(This)), &intern->u.dir.func_valid, "valid", &retval);
  738. valid = zend_is_true(&retval);
  739. zval_ptr_dtor(&retval);
  740. if (!valid) {
  741. zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", pos);
  742. return;
  743. }
  744. zend_call_method_with_0_params(&EX(This), Z_OBJCE(EX(This)), &intern->u.dir.func_next, "next", NULL);
  745. }
  746. } /* }}} */
  747. /* {{{ proto string DirectoryIterator::valid()
  748. Check whether dir contains more entries */
  749. SPL_METHOD(DirectoryIterator, valid)
  750. {
  751. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  752. if (zend_parse_parameters_none() == FAILURE) {
  753. return;
  754. }
  755. RETURN_BOOL(intern->u.dir.entry.d_name[0] != '\0');
  756. }
  757. /* }}} */
  758. /* {{{ proto string SplFileInfo::getPath()
  759. Return the path */
  760. SPL_METHOD(SplFileInfo, getPath)
  761. {
  762. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  763. char *path;
  764. size_t path_len;
  765. if (zend_parse_parameters_none() == FAILURE) {
  766. return;
  767. }
  768. path = spl_filesystem_object_get_path(intern, &path_len);
  769. RETURN_STRINGL(path, path_len);
  770. }
  771. /* }}} */
  772. /* {{{ proto string SplFileInfo::getFilename()
  773. Return filename only */
  774. SPL_METHOD(SplFileInfo, getFilename)
  775. {
  776. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  777. size_t path_len;
  778. if (zend_parse_parameters_none() == FAILURE) {
  779. return;
  780. }
  781. spl_filesystem_object_get_path(intern, &path_len);
  782. if (path_len && path_len < intern->file_name_len) {
  783. RETURN_STRINGL(intern->file_name + path_len + 1, intern->file_name_len - (path_len + 1));
  784. } else {
  785. RETURN_STRINGL(intern->file_name, intern->file_name_len);
  786. }
  787. }
  788. /* }}} */
  789. /* {{{ proto string DirectoryIterator::getFilename()
  790. Return filename of current dir entry */
  791. SPL_METHOD(DirectoryIterator, getFilename)
  792. {
  793. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  794. if (zend_parse_parameters_none() == FAILURE) {
  795. return;
  796. }
  797. RETURN_STRING(intern->u.dir.entry.d_name);
  798. }
  799. /* }}} */
  800. /* {{{ proto string SplFileInfo::getExtension()
  801. Returns file extension component of path */
  802. SPL_METHOD(SplFileInfo, getExtension)
  803. {
  804. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  805. char *fname = NULL;
  806. const char *p;
  807. size_t flen;
  808. size_t path_len;
  809. size_t idx;
  810. zend_string *ret;
  811. if (zend_parse_parameters_none() == FAILURE) {
  812. return;
  813. }
  814. spl_filesystem_object_get_path(intern, &path_len);
  815. if (path_len && path_len < intern->file_name_len) {
  816. fname = intern->file_name + path_len + 1;
  817. flen = intern->file_name_len - (path_len + 1);
  818. } else {
  819. fname = intern->file_name;
  820. flen = intern->file_name_len;
  821. }
  822. ret = php_basename(fname, flen, NULL, 0);
  823. p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
  824. if (p) {
  825. idx = p - ZSTR_VAL(ret);
  826. RETVAL_STRINGL(ZSTR_VAL(ret) + idx + 1, ZSTR_LEN(ret) - idx - 1);
  827. zend_string_release_ex(ret, 0);
  828. return;
  829. } else {
  830. zend_string_release_ex(ret, 0);
  831. RETURN_EMPTY_STRING();
  832. }
  833. }
  834. /* }}}*/
  835. /* {{{ proto string DirectoryIterator::getExtension()
  836. Returns the file extension component of path */
  837. SPL_METHOD(DirectoryIterator, getExtension)
  838. {
  839. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  840. const char *p;
  841. size_t idx;
  842. zend_string *fname;
  843. if (zend_parse_parameters_none() == FAILURE) {
  844. return;
  845. }
  846. fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), NULL, 0);
  847. p = zend_memrchr(ZSTR_VAL(fname), '.', ZSTR_LEN(fname));
  848. if (p) {
  849. idx = p - ZSTR_VAL(fname);
  850. RETVAL_STRINGL(ZSTR_VAL(fname) + idx + 1, ZSTR_LEN(fname) - idx - 1);
  851. zend_string_release_ex(fname, 0);
  852. } else {
  853. zend_string_release_ex(fname, 0);
  854. RETURN_EMPTY_STRING();
  855. }
  856. }
  857. /* }}} */
  858. /* {{{ proto string SplFileInfo::getBasename([string $suffix])
  859. Returns filename component of path */
  860. SPL_METHOD(SplFileInfo, getBasename)
  861. {
  862. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  863. char *fname, *suffix = 0;
  864. size_t flen;
  865. size_t slen = 0, path_len;
  866. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
  867. return;
  868. }
  869. spl_filesystem_object_get_path(intern, &path_len);
  870. if (path_len && path_len < intern->file_name_len) {
  871. fname = intern->file_name + path_len + 1;
  872. flen = intern->file_name_len - (path_len + 1);
  873. } else {
  874. fname = intern->file_name;
  875. flen = intern->file_name_len;
  876. }
  877. RETURN_STR(php_basename(fname, flen, suffix, slen));
  878. }
  879. /* }}}*/
  880. /* {{{ proto string DirectoryIterator::getBasename([string $suffix])
  881. Returns filename component of current dir entry */
  882. SPL_METHOD(DirectoryIterator, getBasename)
  883. {
  884. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  885. char *suffix = 0;
  886. size_t slen = 0;
  887. zend_string *fname;
  888. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
  889. return;
  890. }
  891. fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), suffix, slen);
  892. RETVAL_STR(fname);
  893. }
  894. /* }}} */
  895. /* {{{ proto string SplFileInfo::getPathname()
  896. Return path and filename */
  897. SPL_METHOD(SplFileInfo, getPathname)
  898. {
  899. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  900. char *path;
  901. size_t path_len;
  902. if (zend_parse_parameters_none() == FAILURE) {
  903. return;
  904. }
  905. path = spl_filesystem_object_get_pathname(intern, &path_len);
  906. if (path != NULL) {
  907. RETURN_STRINGL(path, path_len);
  908. } else {
  909. RETURN_FALSE;
  910. }
  911. }
  912. /* }}} */
  913. /* {{{ proto string FilesystemIterator::key()
  914. Return getPathname() or getFilename() depending on flags */
  915. SPL_METHOD(FilesystemIterator, key)
  916. {
  917. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  918. if (zend_parse_parameters_none() == FAILURE) {
  919. return;
  920. }
  921. if (SPL_FILE_DIR_KEY(intern, SPL_FILE_DIR_KEY_AS_FILENAME)) {
  922. RETURN_STRING(intern->u.dir.entry.d_name);
  923. } else {
  924. spl_filesystem_object_get_file_name(intern);
  925. RETURN_STRINGL(intern->file_name, intern->file_name_len);
  926. }
  927. }
  928. /* }}} */
  929. /* {{{ proto string FilesystemIterator::current()
  930. Return getFilename(), getFileInfo() or $this depending on flags */
  931. SPL_METHOD(FilesystemIterator, current)
  932. {
  933. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  934. if (zend_parse_parameters_none() == FAILURE) {
  935. return;
  936. }
  937. if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
  938. spl_filesystem_object_get_file_name(intern);
  939. RETURN_STRINGL(intern->file_name, intern->file_name_len);
  940. } else if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
  941. spl_filesystem_object_get_file_name(intern);
  942. spl_filesystem_object_create_type(0, intern, SPL_FS_INFO, NULL, return_value);
  943. } else {
  944. ZVAL_OBJ(return_value, Z_OBJ_P(getThis()));
  945. Z_ADDREF_P(return_value);
  946. /*RETURN_STRING(intern->u.dir.entry.d_name, 1);*/
  947. }
  948. }
  949. /* }}} */
  950. /* {{{ proto bool DirectoryIterator::isDot()
  951. Returns true if current entry is '.' or '..' */
  952. SPL_METHOD(DirectoryIterator, isDot)
  953. {
  954. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  955. if (zend_parse_parameters_none() == FAILURE) {
  956. return;
  957. }
  958. RETURN_BOOL(spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  959. }
  960. /* }}} */
  961. /* {{{ proto SplFileInfo::__construct(string file_name)
  962. Cronstructs a new SplFileInfo from a path. */
  963. /* When the constructor gets called the object is already created
  964. by the engine, so we must only call 'additional' initializations.
  965. */
  966. SPL_METHOD(SplFileInfo, __construct)
  967. {
  968. spl_filesystem_object *intern;
  969. char *path;
  970. size_t len;
  971. if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p", &path, &len) == FAILURE) {
  972. return;
  973. }
  974. intern = Z_SPLFILESYSTEM_P(getThis());
  975. spl_filesystem_info_set_filename(intern, path, len, 1);
  976. /* intern->type = SPL_FS_INFO; already set */
  977. }
  978. /* }}} */
  979. /* {{{ FileInfoFunction */
  980. #define FileInfoFunction(func_name, func_num) \
  981. SPL_METHOD(SplFileInfo, func_name) \
  982. { \
  983. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis()); \
  984. zend_error_handling error_handling; \
  985. if (zend_parse_parameters_none() == FAILURE) { \
  986. return; \
  987. } \
  988. \
  989. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);\
  990. spl_filesystem_object_get_file_name(intern); \
  991. php_stat(intern->file_name, intern->file_name_len, func_num, return_value); \
  992. zend_restore_error_handling(&error_handling); \
  993. }
  994. /* }}} */
  995. /* {{{ proto int SplFileInfo::getPerms()
  996. Get file permissions */
  997. FileInfoFunction(getPerms, FS_PERMS)
  998. /* }}} */
  999. /* {{{ proto int SplFileInfo::getInode()
  1000. Get file inode */
  1001. FileInfoFunction(getInode, FS_INODE)
  1002. /* }}} */
  1003. /* {{{ proto int SplFileInfo::getSize()
  1004. Get file size */
  1005. FileInfoFunction(getSize, FS_SIZE)
  1006. /* }}} */
  1007. /* {{{ proto int SplFileInfo::getOwner()
  1008. Get file owner */
  1009. FileInfoFunction(getOwner, FS_OWNER)
  1010. /* }}} */
  1011. /* {{{ proto int SplFileInfo::getGroup()
  1012. Get file group */
  1013. FileInfoFunction(getGroup, FS_GROUP)
  1014. /* }}} */
  1015. /* {{{ proto int SplFileInfo::getATime()
  1016. Get last access time of file */
  1017. FileInfoFunction(getATime, FS_ATIME)
  1018. /* }}} */
  1019. /* {{{ proto int SplFileInfo::getMTime()
  1020. Get last modification time of file */
  1021. FileInfoFunction(getMTime, FS_MTIME)
  1022. /* }}} */
  1023. /* {{{ proto int SplFileInfo::getCTime()
  1024. Get inode modification time of file */
  1025. FileInfoFunction(getCTime, FS_CTIME)
  1026. /* }}} */
  1027. /* {{{ proto string SplFileInfo::getType()
  1028. Get file type */
  1029. FileInfoFunction(getType, FS_TYPE)
  1030. /* }}} */
  1031. /* {{{ proto bool SplFileInfo::isWritable()
  1032. Returns true if file can be written */
  1033. FileInfoFunction(isWritable, FS_IS_W)
  1034. /* }}} */
  1035. /* {{{ proto bool SplFileInfo::isReadable()
  1036. Returns true if file can be read */
  1037. FileInfoFunction(isReadable, FS_IS_R)
  1038. /* }}} */
  1039. /* {{{ proto bool SplFileInfo::isExecutable()
  1040. Returns true if file is executable */
  1041. FileInfoFunction(isExecutable, FS_IS_X)
  1042. /* }}} */
  1043. /* {{{ proto bool SplFileInfo::isFile()
  1044. Returns true if file is a regular file */
  1045. FileInfoFunction(isFile, FS_IS_FILE)
  1046. /* }}} */
  1047. /* {{{ proto bool SplFileInfo::isDir()
  1048. Returns true if file is directory */
  1049. FileInfoFunction(isDir, FS_IS_DIR)
  1050. /* }}} */
  1051. /* {{{ proto bool SplFileInfo::isLink()
  1052. Returns true if file is symbolic link */
  1053. FileInfoFunction(isLink, FS_IS_LINK)
  1054. /* }}} */
  1055. /* {{{ proto string SplFileInfo::getLinkTarget()
  1056. Return the target of a symbolic link */
  1057. SPL_METHOD(SplFileInfo, getLinkTarget)
  1058. {
  1059. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1060. ssize_t ret;
  1061. char buff[MAXPATHLEN];
  1062. zend_error_handling error_handling;
  1063. if (zend_parse_parameters_none() == FAILURE) {
  1064. return;
  1065. }
  1066. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
  1067. if (intern->file_name == NULL) {
  1068. spl_filesystem_object_get_file_name(intern);
  1069. }
  1070. #if defined(PHP_WIN32) || HAVE_SYMLINK
  1071. if (intern->file_name == NULL) {
  1072. php_error_docref(NULL, E_WARNING, "Empty filename");
  1073. RETURN_FALSE;
  1074. } else if (!IS_ABSOLUTE_PATH(intern->file_name, intern->file_name_len)) {
  1075. char expanded_path[MAXPATHLEN];
  1076. if (!expand_filepath_with_mode(intern->file_name, expanded_path, NULL, 0, CWD_EXPAND )) {
  1077. php_error_docref(NULL, E_WARNING, "No such file or directory");
  1078. RETURN_FALSE;
  1079. }
  1080. ret = php_sys_readlink(expanded_path, buff, MAXPATHLEN - 1);
  1081. } else {
  1082. ret = php_sys_readlink(intern->file_name, buff, MAXPATHLEN-1);
  1083. }
  1084. #else
  1085. ret = -1; /* always fail if not implemented */
  1086. #endif
  1087. if (ret == -1) {
  1088. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Unable to read link %s, error: %s", intern->file_name, strerror(errno));
  1089. RETVAL_FALSE;
  1090. } else {
  1091. /* Append NULL to the end of the string */
  1092. buff[ret] = '\0';
  1093. RETVAL_STRINGL(buff, ret);
  1094. }
  1095. zend_restore_error_handling(&error_handling);
  1096. }
  1097. /* }}} */
  1098. #if HAVE_REALPATH || defined(ZTS)
  1099. /* {{{ proto string SplFileInfo::getRealPath()
  1100. Return the resolved path */
  1101. SPL_METHOD(SplFileInfo, getRealPath)
  1102. {
  1103. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1104. char buff[MAXPATHLEN];
  1105. char *filename;
  1106. zend_error_handling error_handling;
  1107. if (zend_parse_parameters_none() == FAILURE) {
  1108. return;
  1109. }
  1110. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
  1111. if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
  1112. spl_filesystem_object_get_file_name(intern);
  1113. }
  1114. if (intern->orig_path) {
  1115. filename = intern->orig_path;
  1116. } else {
  1117. filename = intern->file_name;
  1118. }
  1119. if (filename && VCWD_REALPATH(filename, buff)) {
  1120. #ifdef ZTS
  1121. if (VCWD_ACCESS(buff, F_OK)) {
  1122. RETVAL_FALSE;
  1123. } else
  1124. #endif
  1125. RETVAL_STRING(buff);
  1126. } else {
  1127. RETVAL_FALSE;
  1128. }
  1129. zend_restore_error_handling(&error_handling);
  1130. }
  1131. /* }}} */
  1132. #endif
  1133. /* {{{ proto SplFileObject SplFileInfo::openFile([string mode = 'r' [, bool use_include_path [, resource context]]])
  1134. Open the current file */
  1135. SPL_METHOD(SplFileInfo, openFile)
  1136. {
  1137. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1138. spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_FILE, NULL, return_value);
  1139. }
  1140. /* }}} */
  1141. /* {{{ proto void SplFileInfo::setFileClass([string class_name])
  1142. Class to use in openFile() */
  1143. SPL_METHOD(SplFileInfo, setFileClass)
  1144. {
  1145. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1146. zend_class_entry *ce = spl_ce_SplFileObject;
  1147. zend_error_handling error_handling;
  1148. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
  1149. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == SUCCESS) {
  1150. intern->file_class = ce;
  1151. }
  1152. zend_restore_error_handling(&error_handling);
  1153. }
  1154. /* }}} */
  1155. /* {{{ proto void SplFileInfo::setInfoClass([string class_name])
  1156. Class to use in getFileInfo(), getPathInfo() */
  1157. SPL_METHOD(SplFileInfo, setInfoClass)
  1158. {
  1159. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1160. zend_class_entry *ce = spl_ce_SplFileInfo;
  1161. zend_error_handling error_handling;
  1162. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling );
  1163. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == SUCCESS) {
  1164. intern->info_class = ce;
  1165. }
  1166. zend_restore_error_handling(&error_handling);
  1167. }
  1168. /* }}} */
  1169. /* {{{ proto SplFileInfo SplFileInfo::getFileInfo([string $class_name])
  1170. Get/copy file info */
  1171. SPL_METHOD(SplFileInfo, getFileInfo)
  1172. {
  1173. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1174. zend_class_entry *ce = intern->info_class;
  1175. zend_error_handling error_handling;
  1176. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
  1177. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == SUCCESS) {
  1178. spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_INFO, ce, return_value);
  1179. }
  1180. zend_restore_error_handling(&error_handling);
  1181. }
  1182. /* }}} */
  1183. /* {{{ proto SplFileInfo SplFileInfo::getPathInfo([string $class_name])
  1184. Get/copy file info */
  1185. SPL_METHOD(SplFileInfo, getPathInfo)
  1186. {
  1187. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1188. zend_class_entry *ce = intern->info_class;
  1189. zend_error_handling error_handling;
  1190. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
  1191. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == SUCCESS) {
  1192. size_t path_len;
  1193. char *path = spl_filesystem_object_get_pathname(intern, &path_len);
  1194. if (path) {
  1195. char *dpath = estrndup(path, path_len);
  1196. path_len = php_dirname(dpath, path_len);
  1197. spl_filesystem_object_create_info(intern, dpath, path_len, 1, ce, return_value);
  1198. efree(dpath);
  1199. }
  1200. }
  1201. zend_restore_error_handling(&error_handling);
  1202. }
  1203. /* }}} */
  1204. /* {{{ proto SplFileInfo::_bad_state_ex(void) */
  1205. SPL_METHOD(SplFileInfo, _bad_state_ex)
  1206. {
  1207. zend_throw_exception_ex(spl_ce_LogicException, 0,
  1208. "The parent constructor was not called: the object is in an "
  1209. "invalid state ");
  1210. }
  1211. /* }}} */
  1212. /* {{{ proto FilesystemIterator::__construct(string path [, int flags])
  1213. Cronstructs a new dir iterator from a path. */
  1214. SPL_METHOD(FilesystemIterator, __construct)
  1215. {
  1216. spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS | SPL_FILE_DIR_SKIPDOTS);
  1217. }
  1218. /* }}} */
  1219. /* {{{ proto void FilesystemIterator::rewind()
  1220. Rewind dir back to the start */
  1221. SPL_METHOD(FilesystemIterator, rewind)
  1222. {
  1223. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1224. int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
  1225. if (zend_parse_parameters_none() == FAILURE) {
  1226. return;
  1227. }
  1228. intern->u.dir.index = 0;
  1229. if (intern->u.dir.dirp) {
  1230. php_stream_rewinddir(intern->u.dir.dirp);
  1231. }
  1232. do {
  1233. spl_filesystem_dir_read(intern);
  1234. } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  1235. }
  1236. /* }}} */
  1237. /* {{{ proto int FilesystemIterator::getFlags()
  1238. Get handling flags */
  1239. SPL_METHOD(FilesystemIterator, getFlags)
  1240. {
  1241. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1242. if (zend_parse_parameters_none() == FAILURE) {
  1243. return;
  1244. }
  1245. RETURN_LONG(intern->flags & (SPL_FILE_DIR_KEY_MODE_MASK | SPL_FILE_DIR_CURRENT_MODE_MASK | SPL_FILE_DIR_OTHERS_MASK));
  1246. } /* }}} */
  1247. /* {{{ proto void FilesystemIterator::setFlags(long $flags)
  1248. Set handling flags */
  1249. SPL_METHOD(FilesystemIterator, setFlags)
  1250. {
  1251. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1252. zend_long flags;
  1253. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &flags) == FAILURE) {
  1254. return;
  1255. }
  1256. intern->flags &= ~(SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK);
  1257. intern->flags |= ((SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK) & flags);
  1258. } /* }}} */
  1259. /* {{{ proto bool RecursiveDirectoryIterator::hasChildren([bool $allow_links = false])
  1260. Returns whether current entry is a directory and not '.' or '..' */
  1261. SPL_METHOD(RecursiveDirectoryIterator, hasChildren)
  1262. {
  1263. zend_bool allow_links = 0;
  1264. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1265. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &allow_links) == FAILURE) {
  1266. return;
  1267. }
  1268. if (spl_filesystem_is_invalid_or_dot(intern->u.dir.entry.d_name)) {
  1269. RETURN_FALSE;
  1270. } else {
  1271. spl_filesystem_object_get_file_name(intern);
  1272. if (!allow_links && !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
  1273. php_stat(intern->file_name, intern->file_name_len, FS_IS_LINK, return_value);
  1274. if (zend_is_true(return_value)) {
  1275. RETURN_FALSE;
  1276. }
  1277. }
  1278. php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, return_value);
  1279. }
  1280. }
  1281. /* }}} */
  1282. /* {{{ proto RecursiveDirectoryIterator DirectoryIterator::getChildren()
  1283. Returns an iterator for the current entry if it is a directory */
  1284. SPL_METHOD(RecursiveDirectoryIterator, getChildren)
  1285. {
  1286. zval zpath, zflags;
  1287. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1288. spl_filesystem_object *subdir;
  1289. char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
  1290. if (zend_parse_parameters_none() == FAILURE) {
  1291. return;
  1292. }
  1293. spl_filesystem_object_get_file_name(intern);
  1294. ZVAL_LONG(&zflags, intern->flags);
  1295. ZVAL_STRINGL(&zpath, intern->file_name, intern->file_name_len);
  1296. spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), return_value, &zpath, &zflags);
  1297. zval_ptr_dtor(&zpath);
  1298. subdir = Z_SPLFILESYSTEM_P(return_value);
  1299. if (subdir) {
  1300. if (intern->u.dir.sub_path && intern->u.dir.sub_path[0]) {
  1301. subdir->u.dir.sub_path_len = spprintf(&subdir->u.dir.sub_path, 0, "%s%c%s", intern->u.dir.sub_path, slash, intern->u.dir.entry.d_name);
  1302. } else {
  1303. subdir->u.dir.sub_path_len = strlen(intern->u.dir.entry.d_name);
  1304. subdir->u.dir.sub_path = estrndup(intern->u.dir.entry.d_name, subdir->u.dir.sub_path_len);
  1305. }
  1306. subdir->info_class = intern->info_class;
  1307. subdir->file_class = intern->file_class;
  1308. subdir->oth = intern->oth;
  1309. }
  1310. }
  1311. /* }}} */
  1312. /* {{{ proto void RecursiveDirectoryIterator::getSubPath()
  1313. Get sub path */
  1314. SPL_METHOD(RecursiveDirectoryIterator, getSubPath)
  1315. {
  1316. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1317. if (zend_parse_parameters_none() == FAILURE) {
  1318. return;
  1319. }
  1320. if (intern->u.dir.sub_path) {
  1321. RETURN_STRINGL(intern->u.dir.sub_path, intern->u.dir.sub_path_len);
  1322. } else {
  1323. RETURN_EMPTY_STRING();
  1324. }
  1325. }
  1326. /* }}} */
  1327. /* {{{ proto void RecursiveDirectoryIterator::getSubPathname()
  1328. Get sub path and file name */
  1329. SPL_METHOD(RecursiveDirectoryIterator, getSubPathname)
  1330. {
  1331. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1332. char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
  1333. if (zend_parse_parameters_none() == FAILURE) {
  1334. return;
  1335. }
  1336. if (intern->u.dir.sub_path) {
  1337. RETURN_NEW_STR(strpprintf(0, "%s%c%s", intern->u.dir.sub_path, slash, intern->u.dir.entry.d_name));
  1338. } else {
  1339. RETURN_STRING(intern->u.dir.entry.d_name);
  1340. }
  1341. }
  1342. /* }}} */
  1343. /* {{{ proto RecursiveDirectoryIterator::__construct(string path [, int flags])
  1344. Cronstructs a new dir iterator from a path. */
  1345. SPL_METHOD(RecursiveDirectoryIterator, __construct)
  1346. {
  1347. spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS);
  1348. }
  1349. /* }}} */
  1350. #ifdef HAVE_GLOB
  1351. /* {{{ proto GlobIterator::__construct(string path [, int flags])
  1352. Cronstructs a new dir iterator from a glob expression (no glob:// needed). */
  1353. SPL_METHOD(GlobIterator, __construct)
  1354. {
  1355. spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS|DIT_CTOR_GLOB);
  1356. }
  1357. /* }}} */
  1358. /* {{{ proto int GlobIterator::cont()
  1359. Return the number of directories and files found by globbing */
  1360. SPL_METHOD(GlobIterator, count)
  1361. {
  1362. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1363. if (zend_parse_parameters_none() == FAILURE) {
  1364. return;
  1365. }
  1366. if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
  1367. RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
  1368. } else {
  1369. /* should not happen */
  1370. php_error_docref(NULL, E_ERROR, "GlobIterator lost glob state");
  1371. }
  1372. }
  1373. /* }}} */
  1374. #endif /* HAVE_GLOB */
  1375. /* {{{ forward declarations to the iterator handlers */
  1376. static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter);
  1377. static int spl_filesystem_dir_it_valid(zend_object_iterator *iter);
  1378. static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter);
  1379. static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key);
  1380. static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter);
  1381. static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter);
  1382. /* iterator handler table */
  1383. static const zend_object_iterator_funcs spl_filesystem_dir_it_funcs = {
  1384. spl_filesystem_dir_it_dtor,
  1385. spl_filesystem_dir_it_valid,
  1386. spl_filesystem_dir_it_current_data,
  1387. spl_filesystem_dir_it_current_key,
  1388. spl_filesystem_dir_it_move_forward,
  1389. spl_filesystem_dir_it_rewind,
  1390. NULL
  1391. };
  1392. /* }}} */
  1393. /* {{{ spl_ce_dir_get_iterator */
  1394. zend_object_iterator *spl_filesystem_dir_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
  1395. {
  1396. spl_filesystem_iterator *iterator;
  1397. spl_filesystem_object *dir_object;
  1398. if (by_ref) {
  1399. zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
  1400. return NULL;
  1401. }
  1402. dir_object = Z_SPLFILESYSTEM_P(object);
  1403. iterator = spl_filesystem_object_to_iterator(dir_object);
  1404. ZVAL_COPY(&iterator->intern.data, object);
  1405. iterator->intern.funcs = &spl_filesystem_dir_it_funcs;
  1406. /* ->current must be initialized; rewind doesn't set it and valid
  1407. * doesn't check whether it's set */
  1408. iterator->current = *object;
  1409. return &iterator->intern;
  1410. }
  1411. /* }}} */
  1412. /* {{{ spl_filesystem_dir_it_dtor */
  1413. static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter)
  1414. {
  1415. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1416. if (!Z_ISUNDEF(iterator->intern.data)) {
  1417. zval *object = &iterator->intern.data;
  1418. zval_ptr_dtor(object);
  1419. }
  1420. /* Otherwise we were called from the owning object free storage handler as
  1421. * it sets iterator->intern.data to IS_UNDEF.
  1422. * We don't even need to destroy iterator->current as we didn't add a
  1423. * reference to it in move_forward or get_iterator */
  1424. }
  1425. /* }}} */
  1426. /* {{{ spl_filesystem_dir_it_valid */
  1427. static int spl_filesystem_dir_it_valid(zend_object_iterator *iter)
  1428. {
  1429. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1430. return object->u.dir.entry.d_name[0] != '\0' ? SUCCESS : FAILURE;
  1431. }
  1432. /* }}} */
  1433. /* {{{ spl_filesystem_dir_it_current_data */
  1434. static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter)
  1435. {
  1436. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1437. return &iterator->current;
  1438. }
  1439. /* }}} */
  1440. /* {{{ spl_filesystem_dir_it_current_key */
  1441. static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key)
  1442. {
  1443. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1444. ZVAL_LONG(key, object->u.dir.index);
  1445. }
  1446. /* }}} */
  1447. /* {{{ spl_filesystem_dir_it_move_forward */
  1448. static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter)
  1449. {
  1450. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1451. object->u.dir.index++;
  1452. spl_filesystem_dir_read(object);
  1453. if (object->file_name) {
  1454. efree(object->file_name);
  1455. object->file_name = NULL;
  1456. }
  1457. }
  1458. /* }}} */
  1459. /* {{{ spl_filesystem_dir_it_rewind */
  1460. static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter)
  1461. {
  1462. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1463. object->u.dir.index = 0;
  1464. if (object->u.dir.dirp) {
  1465. php_stream_rewinddir(object->u.dir.dirp);
  1466. }
  1467. spl_filesystem_dir_read(object);
  1468. }
  1469. /* }}} */
  1470. /* {{{ spl_filesystem_tree_it_dtor */
  1471. static void spl_filesystem_tree_it_dtor(zend_object_iterator *iter)
  1472. {
  1473. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1474. if (!Z_ISUNDEF(iterator->intern.data)) {
  1475. zval *object = &iterator->intern.data;
  1476. zval_ptr_dtor(object);
  1477. } else {
  1478. if (!Z_ISUNDEF(iterator->current)) {
  1479. zval_ptr_dtor(&iterator->current);
  1480. ZVAL_UNDEF(&iterator->current);
  1481. }
  1482. }
  1483. }
  1484. /* }}} */
  1485. /* {{{ spl_filesystem_tree_it_current_data */
  1486. static zval *spl_filesystem_tree_it_current_data(zend_object_iterator *iter)
  1487. {
  1488. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1489. spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
  1490. if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
  1491. if (Z_ISUNDEF(iterator->current)) {
  1492. spl_filesystem_object_get_file_name(object);
  1493. ZVAL_STRINGL(&iterator->current, object->file_name, object->file_name_len);
  1494. }
  1495. return &iterator->current;
  1496. } else if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
  1497. if (Z_ISUNDEF(iterator->current)) {
  1498. spl_filesystem_object_get_file_name(object);
  1499. spl_filesystem_object_create_type(0, object, SPL_FS_INFO, NULL, &iterator->current);
  1500. }
  1501. return &iterator->current;
  1502. } else {
  1503. return &iterator->intern.data;
  1504. }
  1505. }
  1506. /* }}} */
  1507. /* {{{ spl_filesystem_tree_it_current_key */
  1508. static void spl_filesystem_tree_it_current_key(zend_object_iterator *iter, zval *key)
  1509. {
  1510. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1511. if (SPL_FILE_DIR_KEY(object, SPL_FILE_DIR_KEY_AS_FILENAME)) {
  1512. ZVAL_STRING(key, object->u.dir.entry.d_name);
  1513. } else {
  1514. spl_filesystem_object_get_file_name(object);
  1515. ZVAL_STRINGL(key, object->file_name, object->file_name_len);
  1516. }
  1517. }
  1518. /* }}} */
  1519. /* {{{ spl_filesystem_tree_it_move_forward */
  1520. static void spl_filesystem_tree_it_move_forward(zend_object_iterator *iter)
  1521. {
  1522. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1523. spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
  1524. object->u.dir.index++;
  1525. do {
  1526. spl_filesystem_dir_read(object);
  1527. } while (spl_filesystem_is_dot(object->u.dir.entry.d_name));
  1528. if (object->file_name) {
  1529. efree(object->file_name);
  1530. object->file_name = NULL;
  1531. }
  1532. if (!Z_ISUNDEF(iterator->current)) {
  1533. zval_ptr_dtor(&iterator->current);
  1534. ZVAL_UNDEF(&iterator->current);
  1535. }
  1536. }
  1537. /* }}} */
  1538. /* {{{ spl_filesystem_tree_it_rewind */
  1539. static void spl_filesystem_tree_it_rewind(zend_object_iterator *iter)
  1540. {
  1541. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1542. spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
  1543. object->u.dir.index = 0;
  1544. if (object->u.dir.dirp) {
  1545. php_stream_rewinddir(object->u.dir.dirp);
  1546. }
  1547. do {
  1548. spl_filesystem_dir_read(object);
  1549. } while (spl_filesystem_is_dot(object->u.dir.entry.d_name));
  1550. if (!Z_ISUNDEF(iterator->current)) {
  1551. zval_ptr_dtor(&iterator->current);
  1552. ZVAL_UNDEF(&iterator->current);
  1553. }
  1554. }
  1555. /* }}} */
  1556. /* {{{ iterator handler table */
  1557. static const zend_object_iterator_funcs spl_filesystem_tree_it_funcs = {
  1558. spl_filesystem_tree_it_dtor,
  1559. spl_filesystem_dir_it_valid,
  1560. spl_filesystem_tree_it_current_data,
  1561. spl_filesystem_tree_it_current_key,
  1562. spl_filesystem_tree_it_move_forward,
  1563. spl_filesystem_tree_it_rewind,
  1564. NULL
  1565. };
  1566. /* }}} */
  1567. /* {{{ spl_ce_dir_get_iterator */
  1568. zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
  1569. {
  1570. spl_filesystem_iterator *iterator;
  1571. spl_filesystem_object *dir_object;
  1572. if (by_ref) {
  1573. zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
  1574. return NULL;
  1575. }
  1576. dir_object = Z_SPLFILESYSTEM_P(object);
  1577. iterator = spl_filesystem_object_to_iterator(dir_object);
  1578. ZVAL_COPY(&iterator->intern.data, object);
  1579. iterator->intern.funcs = &spl_filesystem_tree_it_funcs;
  1580. return &iterator->intern;
  1581. }
  1582. /* }}} */
  1583. /* {{{ spl_filesystem_object_cast */
  1584. static int spl_filesystem_object_cast(zval *readobj, zval *writeobj, int type)
  1585. {
  1586. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(readobj);
  1587. if (type == IS_STRING) {
  1588. if (Z_OBJCE_P(readobj)->__tostring) {
  1589. return zend_std_cast_object_tostring(readobj, writeobj, type);
  1590. }
  1591. switch (intern->type) {
  1592. case SPL_FS_INFO:
  1593. case SPL_FS_FILE:
  1594. ZVAL_STRINGL(writeobj, intern->file_name, intern->file_name_len);
  1595. return SUCCESS;
  1596. case SPL_FS_DIR:
  1597. ZVAL_STRING(writeobj, intern->u.dir.entry.d_name);
  1598. return SUCCESS;
  1599. }
  1600. } else if (type == _IS_BOOL) {
  1601. ZVAL_TRUE(writeobj);
  1602. return SUCCESS;
  1603. }
  1604. ZVAL_NULL(writeobj);
  1605. return FAILURE;
  1606. }
  1607. /* }}} */
  1608. /* {{{ declare method parameters */
  1609. /* supply a name and default to call by parameter */
  1610. ZEND_BEGIN_ARG_INFO(arginfo_info___construct, 0)
  1611. ZEND_ARG_INFO(0, file_name)
  1612. ZEND_END_ARG_INFO()
  1613. ZEND_BEGIN_ARG_INFO_EX(arginfo_info_openFile, 0, 0, 0)
  1614. ZEND_ARG_INFO(0, open_mode)
  1615. ZEND_ARG_INFO(0, use_include_path)
  1616. ZEND_ARG_INFO(0, context)
  1617. ZEND_END_ARG_INFO()
  1618. ZEND_BEGIN_ARG_INFO_EX(arginfo_info_optinalFileClass, 0, 0, 0)
  1619. ZEND_ARG_INFO(0, class_name)
  1620. ZEND_END_ARG_INFO()
  1621. ZEND_BEGIN_ARG_INFO_EX(arginfo_optinalSuffix, 0, 0, 0)
  1622. ZEND_ARG_INFO(0, suffix)
  1623. ZEND_END_ARG_INFO()
  1624. ZEND_BEGIN_ARG_INFO(arginfo_splfileinfo_void, 0)
  1625. ZEND_END_ARG_INFO()
  1626. /* the method table */
  1627. /* each method can have its own parameters and visibility */
  1628. static const zend_function_entry spl_SplFileInfo_functions[] = {
  1629. SPL_ME(SplFileInfo, __construct, arginfo_info___construct, ZEND_ACC_PUBLIC)
  1630. SPL_ME(SplFileInfo, getPath, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1631. SPL_ME(SplFileInfo, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1632. SPL_ME(SplFileInfo, getExtension, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1633. SPL_ME(SplFileInfo, getBasename, arginfo_optinalSuffix, ZEND_ACC_PUBLIC)
  1634. SPL_ME(SplFileInfo, getPathname, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1635. SPL_ME(SplFileInfo, getPerms, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1636. SPL_ME(SplFileInfo, getInode, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1637. SPL_ME(SplFileInfo, getSize, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1638. SPL_ME(SplFileInfo, getOwner, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1639. SPL_ME(SplFileInfo, getGroup, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1640. SPL_ME(SplFileInfo, getATime, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1641. SPL_ME(SplFileInfo, getMTime, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1642. SPL_ME(SplFileInfo, getCTime, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1643. SPL_ME(SplFileInfo, getType, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1644. SPL_ME(SplFileInfo, isWritable, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1645. SPL_ME(SplFileInfo, isReadable, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1646. SPL_ME(SplFileInfo, isExecutable, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1647. SPL_ME(SplFileInfo, isFile, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1648. SPL_ME(SplFileInfo, isDir, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1649. SPL_ME(SplFileInfo, isLink, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1650. SPL_ME(SplFileInfo, getLinkTarget, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1651. #if HAVE_REALPATH || defined(ZTS)
  1652. SPL_ME(SplFileInfo, getRealPath, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1653. #endif
  1654. SPL_ME(SplFileInfo, getFileInfo, arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
  1655. SPL_ME(SplFileInfo, getPathInfo, arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
  1656. SPL_ME(SplFileInfo, openFile, arginfo_info_openFile, ZEND_ACC_PUBLIC)
  1657. SPL_ME(SplFileInfo, setFileClass, arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
  1658. SPL_ME(SplFileInfo, setInfoClass, arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
  1659. SPL_ME(SplFileInfo, _bad_state_ex, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1660. SPL_MA(SplFileInfo, __toString, SplFileInfo, getPathname, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1661. PHP_FE_END
  1662. };
  1663. ZEND_BEGIN_ARG_INFO(arginfo_dir___construct, 0)
  1664. ZEND_ARG_INFO(0, path)
  1665. ZEND_END_ARG_INFO()
  1666. ZEND_BEGIN_ARG_INFO(arginfo_dir_it_seek, 0)
  1667. ZEND_ARG_INFO(0, position)
  1668. ZEND_END_ARG_INFO();
  1669. /* the method table */
  1670. /* each method can have its own parameters and visibility */
  1671. static const zend_function_entry spl_DirectoryIterator_functions[] = {
  1672. SPL_ME(DirectoryIterator, __construct, arginfo_dir___construct, ZEND_ACC_PUBLIC)
  1673. SPL_ME(DirectoryIterator, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1674. SPL_ME(DirectoryIterator, getExtension, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1675. SPL_ME(DirectoryIterator, getBasename, arginfo_optinalSuffix, ZEND_ACC_PUBLIC)
  1676. SPL_ME(DirectoryIterator, isDot, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1677. SPL_ME(DirectoryIterator, rewind, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1678. SPL_ME(DirectoryIterator, valid, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1679. SPL_ME(DirectoryIterator, key, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1680. SPL_ME(DirectoryIterator, current, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1681. SPL_ME(DirectoryIterator, next, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1682. SPL_ME(DirectoryIterator, seek, arginfo_dir_it_seek, ZEND_ACC_PUBLIC)
  1683. SPL_MA(DirectoryIterator, __toString, DirectoryIterator, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1684. PHP_FE_END
  1685. };
  1686. ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir___construct, 0, 0, 1)
  1687. ZEND_ARG_INFO(0, path)
  1688. ZEND_ARG_INFO(0, flags)
  1689. ZEND_END_ARG_INFO()
  1690. ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir_hasChildren, 0, 0, 0)
  1691. ZEND_ARG_INFO(0, allow_links)
  1692. ZEND_END_ARG_INFO()
  1693. ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir_setFlags, 0, 0, 0)
  1694. ZEND_ARG_INFO(0, flags)
  1695. ZEND_END_ARG_INFO()
  1696. static const zend_function_entry spl_FilesystemIterator_functions[] = {
  1697. SPL_ME(FilesystemIterator, __construct, arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
  1698. SPL_ME(FilesystemIterator, rewind, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1699. SPL_ME(DirectoryIterator, next, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1700. SPL_ME(FilesystemIterator, key, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1701. SPL_ME(FilesystemIterator, current, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1702. SPL_ME(FilesystemIterator, getFlags, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1703. SPL_ME(FilesystemIterator, setFlags, arginfo_r_dir_setFlags, ZEND_ACC_PUBLIC)
  1704. PHP_FE_END
  1705. };
  1706. static const zend_function_entry spl_RecursiveDirectoryIterator_functions[] = {
  1707. SPL_ME(RecursiveDirectoryIterator, __construct, arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
  1708. SPL_ME(RecursiveDirectoryIterator, hasChildren, arginfo_r_dir_hasChildren, ZEND_ACC_PUBLIC)
  1709. SPL_ME(RecursiveDirectoryIterator, getChildren, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1710. SPL_ME(RecursiveDirectoryIterator, getSubPath, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1711. SPL_ME(RecursiveDirectoryIterator, getSubPathname,arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1712. PHP_FE_END
  1713. };
  1714. #ifdef HAVE_GLOB
  1715. static const zend_function_entry spl_GlobIterator_functions[] = {
  1716. SPL_ME(GlobIterator, __construct, arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
  1717. SPL_ME(GlobIterator, count, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1718. PHP_FE_END
  1719. };
  1720. #endif
  1721. /* }}} */
  1722. static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) /* {{{ */
  1723. {
  1724. char *buf;
  1725. size_t line_len = 0;
  1726. zend_long line_add = (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) ? 1 : 0;
  1727. spl_filesystem_file_free_line(intern);
  1728. if (php_stream_eof(intern->u.file.stream)) {
  1729. if (!silent) {
  1730. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", intern->file_name);
  1731. }
  1732. return FAILURE;
  1733. }
  1734. if (intern->u.file.max_line_len > 0) {
  1735. buf = safe_emalloc((intern->u.file.max_line_len + 1), sizeof(char), 0);
  1736. if (php_stream_get_line(intern->u.file.stream, buf, intern->u.file.max_line_len + 1, &line_len) == NULL) {
  1737. efree(buf);
  1738. buf = NULL;
  1739. } else {
  1740. buf[line_len] = '\0';
  1741. }
  1742. } else {
  1743. buf = php_stream_get_line(intern->u.file.stream, NULL, 0, &line_len);
  1744. }
  1745. if (!buf) {
  1746. intern->u.file.current_line = estrdup("");
  1747. intern->u.file.current_line_len = 0;
  1748. } else {
  1749. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)) {
  1750. line_len = strcspn(buf, "\r\n");
  1751. buf[line_len] = '\0';
  1752. }
  1753. intern->u.file.current_line = buf;
  1754. intern->u.file.current_line_len = line_len;
  1755. }
  1756. intern->u.file.current_line_num += line_add;
  1757. return SUCCESS;
  1758. } /* }}} */
  1759. static int spl_filesystem_file_call(spl_filesystem_object *intern, zend_function *func_ptr, int pass_num_args, zval *return_value, zval *arg2) /* {{{ */
  1760. {
  1761. zend_fcall_info fci;
  1762. zend_fcall_info_cache fcic;
  1763. zval *zresource_ptr = &intern->u.file.zresource, *params, retval;
  1764. int result;
  1765. int num_args = pass_num_args + (arg2 ? 2 : 1);
  1766. if (Z_ISUNDEF_P(zresource_ptr)) {
  1767. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  1768. return FAILURE;
  1769. }
  1770. params = (zval*)safe_emalloc(num_args, sizeof(zval), 0);
  1771. params[0] = *zresource_ptr;
  1772. if (arg2) {
  1773. params[1] = *arg2;
  1774. }
  1775. if (zend_get_parameters_array_ex(pass_num_args, params + (arg2 ? 2 : 1)) != SUCCESS) {
  1776. efree(params);
  1777. WRONG_PARAM_COUNT_WITH_RETVAL(FAILURE);
  1778. }
  1779. ZVAL_UNDEF(&retval);
  1780. fci.size = sizeof(fci);
  1781. fci.object = NULL;
  1782. fci.retval = &retval;
  1783. fci.param_count = num_args;
  1784. fci.params = params;
  1785. fci.no_separation = 1;
  1786. ZVAL_STR(&fci.function_name, func_ptr->common.function_name);
  1787. fcic.function_handler = func_ptr;
  1788. fcic.called_scope = NULL;
  1789. fcic.object = NULL;
  1790. result = zend_call_function(&fci, &fcic);
  1791. if (result == FAILURE || Z_ISUNDEF(retval)) {
  1792. RETVAL_FALSE;
  1793. } else {
  1794. ZVAL_ZVAL(return_value, &retval, 0, 0);
  1795. }
  1796. efree(params);
  1797. return result;
  1798. } /* }}} */
  1799. #define FileFunctionCall(func_name, pass_num_args, arg2) /* {{{ */ \
  1800. { \
  1801. zend_function *func_ptr; \
  1802. func_ptr = (zend_function *)zend_hash_str_find_ptr(EG(function_table), #func_name, sizeof(#func_name) - 1); \
  1803. if (func_ptr == NULL) { \
  1804. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Internal error, function '%s' not found. Please report", #func_name); \
  1805. return; \
  1806. } \
  1807. spl_filesystem_file_call(intern, func_ptr, pass_num_args, return_value, arg2); \
  1808. } /* }}} */
  1809. static int spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, char escape, zval *return_value) /* {{{ */
  1810. {
  1811. int ret = SUCCESS;
  1812. zval *value;
  1813. do {
  1814. ret = spl_filesystem_file_read(intern, 1);
  1815. } while (ret == SUCCESS && !intern->u.file.current_line_len && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY));
  1816. if (ret == SUCCESS) {
  1817. size_t buf_len = intern->u.file.current_line_len;
  1818. char *buf = estrndup(intern->u.file.current_line, buf_len);
  1819. if (!Z_ISUNDEF(intern->u.file.current_zval)) {
  1820. zval_ptr_dtor(&intern->u.file.current_zval);
  1821. ZVAL_UNDEF(&intern->u.file.current_zval);
  1822. }
  1823. php_fgetcsv(intern->u.file.stream, delimiter, enclosure, escape, buf_len, buf, &intern->u.file.current_zval);
  1824. if (return_value) {
  1825. zval_ptr_dtor(return_value);
  1826. value = &intern->u.file.current_zval;
  1827. ZVAL_COPY_DEREF(return_value, value);
  1828. }
  1829. }
  1830. return ret;
  1831. }
  1832. /* }}} */
  1833. static int spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern, int silent) /* {{{ */
  1834. {
  1835. zval retval;
  1836. /* 1) use fgetcsv? 2) overloaded call the function, 3) do it directly */
  1837. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || intern->u.file.func_getCurr->common.scope != spl_ce_SplFileObject) {
  1838. if (php_stream_eof(intern->u.file.stream)) {
  1839. if (!silent) {
  1840. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", intern->file_name);
  1841. }
  1842. return FAILURE;
  1843. }
  1844. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) {
  1845. return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL);
  1846. } else {
  1847. zend_execute_data *execute_data = EG(current_execute_data);
  1848. zend_call_method_with_0_params(this_ptr, Z_OBJCE(EX(This)), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
  1849. }
  1850. if (!Z_ISUNDEF(retval)) {
  1851. if (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) {
  1852. intern->u.file.current_line_num++;
  1853. }
  1854. spl_filesystem_file_free_line(intern);
  1855. if (Z_TYPE(retval) == IS_STRING) {
  1856. intern->u.file.current_line = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
  1857. intern->u.file.current_line_len = Z_STRLEN(retval);
  1858. } else {
  1859. zval *value = &retval;
  1860. ZVAL_COPY_DEREF(&intern->u.file.current_zval, value);
  1861. }
  1862. zval_ptr_dtor(&retval);
  1863. return SUCCESS;
  1864. } else {
  1865. return FAILURE;
  1866. }
  1867. } else {
  1868. return spl_filesystem_file_read(intern, silent);
  1869. }
  1870. } /* }}} */
  1871. static int spl_filesystem_file_is_empty_line(spl_filesystem_object *intern) /* {{{ */
  1872. {
  1873. if (intern->u.file.current_line) {
  1874. return intern->u.file.current_line_len == 0;
  1875. } else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
  1876. switch(Z_TYPE(intern->u.file.current_zval)) {
  1877. case IS_STRING:
  1878. return Z_STRLEN(intern->u.file.current_zval) == 0;
  1879. case IS_ARRAY:
  1880. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
  1881. && zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 1) {
  1882. uint32_t idx = 0;
  1883. zval *first;
  1884. while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val)) {
  1885. idx++;
  1886. }
  1887. first = &Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val;
  1888. return Z_TYPE_P(first) == IS_STRING && Z_STRLEN_P(first) == 0;
  1889. }
  1890. return zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 0;
  1891. case IS_NULL:
  1892. return 1;
  1893. default:
  1894. return 0;
  1895. }
  1896. } else {
  1897. return 1;
  1898. }
  1899. }
  1900. /* }}} */
  1901. static int spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern, int silent) /* {{{ */
  1902. {
  1903. int ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
  1904. while (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY) && ret == SUCCESS && spl_filesystem_file_is_empty_line(intern)) {
  1905. spl_filesystem_file_free_line(intern);
  1906. ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
  1907. }
  1908. return ret;
  1909. }
  1910. /* }}} */
  1911. static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */
  1912. {
  1913. if(!intern->u.file.stream) {
  1914. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  1915. return;
  1916. }
  1917. if (-1 == php_stream_rewind(intern->u.file.stream)) {
  1918. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot rewind file %s", intern->file_name);
  1919. } else {
  1920. spl_filesystem_file_free_line(intern);
  1921. intern->u.file.current_line_num = 0;
  1922. }
  1923. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
  1924. spl_filesystem_file_read_line(this_ptr, intern, 1);
  1925. }
  1926. } /* }}} */
  1927. /* {{{ proto SplFileObject::__construct(string filename [, string mode = 'r' [, bool use_include_path [, resource context]]]])
  1928. Construct a new file object */
  1929. SPL_METHOD(SplFileObject, __construct)
  1930. {
  1931. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1932. zend_bool use_include_path = 0;
  1933. char *p1, *p2;
  1934. char *tmp_path;
  1935. size_t tmp_path_len;
  1936. zend_error_handling error_handling;
  1937. intern->u.file.open_mode = NULL;
  1938. intern->u.file.open_mode_len = 0;
  1939. if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|sbr!",
  1940. &intern->file_name, &intern->file_name_len,
  1941. &intern->u.file.open_mode, &intern->u.file.open_mode_len,
  1942. &use_include_path, &intern->u.file.zcontext) == FAILURE) {
  1943. intern->u.file.open_mode = NULL;
  1944. intern->file_name = NULL;
  1945. return;
  1946. }
  1947. if (intern->u.file.open_mode == NULL) {
  1948. intern->u.file.open_mode = "r";
  1949. intern->u.file.open_mode_len = 1;
  1950. }
  1951. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
  1952. if (spl_filesystem_file_open(intern, use_include_path, 0) == SUCCESS) {
  1953. tmp_path_len = strlen(intern->u.file.stream->orig_path);
  1954. if (tmp_path_len > 1 && IS_SLASH_AT(intern->u.file.stream->orig_path, tmp_path_len-1)) {
  1955. tmp_path_len--;
  1956. }
  1957. tmp_path = estrndup(intern->u.file.stream->orig_path, tmp_path_len);
  1958. p1 = strrchr(tmp_path, '/');
  1959. #if defined(PHP_WIN32)
  1960. p2 = strrchr(tmp_path, '\\');
  1961. #else
  1962. p2 = 0;
  1963. #endif
  1964. if (p1 || p2) {
  1965. intern->_path_len = ((p1 > p2 ? p1 : p2) - tmp_path);
  1966. } else {
  1967. intern->_path_len = 0;
  1968. }
  1969. efree(tmp_path);
  1970. intern->_path = estrndup(intern->u.file.stream->orig_path, intern->_path_len);
  1971. }
  1972. zend_restore_error_handling(&error_handling);
  1973. } /* }}} */
  1974. /* {{{ proto SplTempFileObject::__construct([int max_memory])
  1975. Construct a new temp file object */
  1976. SPL_METHOD(SplTempFileObject, __construct)
  1977. {
  1978. zend_long max_memory = PHP_STREAM_MAX_MEM;
  1979. char tmp_fname[48];
  1980. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  1981. zend_error_handling error_handling;
  1982. if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|l", &max_memory) == FAILURE) {
  1983. return;
  1984. }
  1985. if (max_memory < 0) {
  1986. intern->file_name = "php://memory";
  1987. intern->file_name_len = 12;
  1988. } else if (ZEND_NUM_ARGS()) {
  1989. intern->file_name_len = slprintf(tmp_fname, sizeof(tmp_fname), "php://temp/maxmemory:" ZEND_LONG_FMT, max_memory);
  1990. intern->file_name = tmp_fname;
  1991. } else {
  1992. intern->file_name = "php://temp";
  1993. intern->file_name_len = 10;
  1994. }
  1995. intern->u.file.open_mode = "wb";
  1996. intern->u.file.open_mode_len = 1;
  1997. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
  1998. if (spl_filesystem_file_open(intern, 0, 0) == SUCCESS) {
  1999. intern->_path_len = 0;
  2000. intern->_path = estrndup("", 0);
  2001. }
  2002. zend_restore_error_handling(&error_handling);
  2003. } /* }}} */
  2004. /* {{{ proto void SplFileObject::rewind()
  2005. Rewind the file and read the first line */
  2006. SPL_METHOD(SplFileObject, rewind)
  2007. {
  2008. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2009. if (zend_parse_parameters_none() == FAILURE) {
  2010. return;
  2011. }
  2012. spl_filesystem_file_rewind(getThis(), intern);
  2013. } /* }}} */
  2014. /* {{{ proto void SplFileObject::eof()
  2015. Return whether end of file is reached */
  2016. SPL_METHOD(SplFileObject, eof)
  2017. {
  2018. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2019. if (zend_parse_parameters_none() == FAILURE) {
  2020. return;
  2021. }
  2022. if(!intern->u.file.stream) {
  2023. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2024. return;
  2025. }
  2026. RETURN_BOOL(php_stream_eof(intern->u.file.stream));
  2027. } /* }}} */
  2028. /* {{{ proto void SplFileObject::valid()
  2029. Return !eof() */
  2030. SPL_METHOD(SplFileObject, valid)
  2031. {
  2032. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2033. if (zend_parse_parameters_none() == FAILURE) {
  2034. return;
  2035. }
  2036. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
  2037. RETURN_BOOL(intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval));
  2038. } else {
  2039. if(!intern->u.file.stream) {
  2040. RETURN_FALSE;
  2041. }
  2042. RETVAL_BOOL(!php_stream_eof(intern->u.file.stream));
  2043. }
  2044. } /* }}} */
  2045. /* {{{ proto string SplFileObject::fgets()
  2046. Rturn next line from file */
  2047. SPL_METHOD(SplFileObject, fgets)
  2048. {
  2049. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2050. if (zend_parse_parameters_none() == FAILURE) {
  2051. return;
  2052. }
  2053. if(!intern->u.file.stream) {
  2054. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2055. return;
  2056. }
  2057. if (spl_filesystem_file_read(intern, 0) == FAILURE) {
  2058. RETURN_FALSE;
  2059. }
  2060. RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
  2061. } /* }}} */
  2062. /* {{{ proto string SplFileObject::current()
  2063. Return current line from file */
  2064. SPL_METHOD(SplFileObject, current)
  2065. {
  2066. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2067. if (zend_parse_parameters_none() == FAILURE) {
  2068. return;
  2069. }
  2070. if(!intern->u.file.stream) {
  2071. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2072. return;
  2073. }
  2074. if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
  2075. spl_filesystem_file_read_line(getThis(), intern, 1);
  2076. }
  2077. if (intern->u.file.current_line && (!SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || Z_ISUNDEF(intern->u.file.current_zval))) {
  2078. RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
  2079. } else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
  2080. zval *value = &intern->u.file.current_zval;
  2081. ZVAL_COPY_DEREF(return_value, value);
  2082. return;
  2083. }
  2084. RETURN_FALSE;
  2085. } /* }}} */
  2086. /* {{{ proto int SplFileObject::key()
  2087. Return line number */
  2088. SPL_METHOD(SplFileObject, key)
  2089. {
  2090. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2091. if (zend_parse_parameters_none() == FAILURE) {
  2092. return;
  2093. }
  2094. /* Do not read the next line to support correct counting with fgetc()
  2095. if (!intern->current_line) {
  2096. spl_filesystem_file_read_line(getThis(), intern, 1);
  2097. } */
  2098. RETURN_LONG(intern->u.file.current_line_num);
  2099. } /* }}} */
  2100. /* {{{ proto void SplFileObject::next()
  2101. Read next line */
  2102. SPL_METHOD(SplFileObject, next)
  2103. {
  2104. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2105. if (zend_parse_parameters_none() == FAILURE) {
  2106. return;
  2107. }
  2108. spl_filesystem_file_free_line(intern);
  2109. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
  2110. spl_filesystem_file_read_line(getThis(), intern, 1);
  2111. }
  2112. intern->u.file.current_line_num++;
  2113. } /* }}} */
  2114. /* {{{ proto void SplFileObject::setFlags(int flags)
  2115. Set file handling flags */
  2116. SPL_METHOD(SplFileObject, setFlags)
  2117. {
  2118. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2119. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) {
  2120. return;
  2121. }
  2122. } /* }}} */
  2123. /* {{{ proto int SplFileObject::getFlags()
  2124. Get file handling flags */
  2125. SPL_METHOD(SplFileObject, getFlags)
  2126. {
  2127. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2128. if (zend_parse_parameters_none() == FAILURE) {
  2129. return;
  2130. }
  2131. RETURN_LONG(intern->flags & SPL_FILE_OBJECT_MASK);
  2132. } /* }}} */
  2133. /* {{{ proto void SplFileObject::setMaxLineLen(int max_len)
  2134. Set maximum line length */
  2135. SPL_METHOD(SplFileObject, setMaxLineLen)
  2136. {
  2137. zend_long max_len;
  2138. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2139. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &max_len) == FAILURE) {
  2140. return;
  2141. }
  2142. if (max_len < 0) {
  2143. zend_throw_exception_ex(spl_ce_DomainException, 0, "Maximum line length must be greater than or equal zero");
  2144. return;
  2145. }
  2146. intern->u.file.max_line_len = max_len;
  2147. } /* }}} */
  2148. /* {{{ proto int SplFileObject::getMaxLineLen()
  2149. Get maximum line length */
  2150. SPL_METHOD(SplFileObject, getMaxLineLen)
  2151. {
  2152. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2153. if (zend_parse_parameters_none() == FAILURE) {
  2154. return;
  2155. }
  2156. RETURN_LONG((zend_long)intern->u.file.max_line_len);
  2157. } /* }}} */
  2158. /* {{{ proto bool SplFileObject::hasChildren()
  2159. Return false */
  2160. SPL_METHOD(SplFileObject, hasChildren)
  2161. {
  2162. if (zend_parse_parameters_none() == FAILURE) {
  2163. return;
  2164. }
  2165. RETURN_FALSE;
  2166. } /* }}} */
  2167. /* {{{ proto bool SplFileObject::getChildren()
  2168. Read NULL */
  2169. SPL_METHOD(SplFileObject, getChildren)
  2170. {
  2171. if (zend_parse_parameters_none() == FAILURE) {
  2172. return;
  2173. }
  2174. /* return NULL */
  2175. } /* }}} */
  2176. /* {{{ FileFunction */
  2177. #define FileFunction(func_name) \
  2178. SPL_METHOD(SplFileObject, func_name) \
  2179. { \
  2180. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis()); \
  2181. FileFunctionCall(func_name, ZEND_NUM_ARGS(), NULL); \
  2182. }
  2183. /* }}} */
  2184. /* {{{ proto array SplFileObject::fgetcsv([string delimiter [, string enclosure [, escape = '\\']]])
  2185. Return current line as csv */
  2186. SPL_METHOD(SplFileObject, fgetcsv)
  2187. {
  2188. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2189. char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure, escape = intern->u.file.escape;
  2190. char *delim = NULL, *enclo = NULL, *esc = NULL;
  2191. size_t d_len = 0, e_len = 0, esc_len = 0;
  2192. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
  2193. if(!intern->u.file.stream) {
  2194. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2195. return;
  2196. }
  2197. switch(ZEND_NUM_ARGS())
  2198. {
  2199. case 3:
  2200. if (esc_len != 1) {
  2201. php_error_docref(NULL, E_WARNING, "escape must be a character");
  2202. RETURN_FALSE;
  2203. }
  2204. escape = esc[0];
  2205. /* no break */
  2206. case 2:
  2207. if (e_len != 1) {
  2208. php_error_docref(NULL, E_WARNING, "enclosure must be a character");
  2209. RETURN_FALSE;
  2210. }
  2211. enclosure = enclo[0];
  2212. /* no break */
  2213. case 1:
  2214. if (d_len != 1) {
  2215. php_error_docref(NULL, E_WARNING, "delimiter must be a character");
  2216. RETURN_FALSE;
  2217. }
  2218. delimiter = delim[0];
  2219. /* no break */
  2220. case 0:
  2221. break;
  2222. }
  2223. spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value);
  2224. }
  2225. }
  2226. /* }}} */
  2227. /* {{{ proto int SplFileObject::fputcsv(array fields, [string delimiter [, string enclosure [, string escape]]])
  2228. Output a field array as a CSV line */
  2229. SPL_METHOD(SplFileObject, fputcsv)
  2230. {
  2231. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2232. char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure, escape = intern->u.file.escape;
  2233. char *delim = NULL, *enclo = NULL, *esc = NULL;
  2234. size_t d_len = 0, e_len = 0, esc_len = 0;
  2235. zend_long ret;
  2236. zval *fields = NULL;
  2237. if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|sss", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
  2238. switch(ZEND_NUM_ARGS())
  2239. {
  2240. case 4:
  2241. if (esc_len != 1) {
  2242. php_error_docref(NULL, E_WARNING, "escape must be a character");
  2243. RETURN_FALSE;
  2244. }
  2245. escape = esc[0];
  2246. /* no break */
  2247. case 3:
  2248. if (e_len != 1) {
  2249. php_error_docref(NULL, E_WARNING, "enclosure must be a character");
  2250. RETURN_FALSE;
  2251. }
  2252. enclosure = enclo[0];
  2253. /* no break */
  2254. case 2:
  2255. if (d_len != 1) {
  2256. php_error_docref(NULL, E_WARNING, "delimiter must be a character");
  2257. RETURN_FALSE;
  2258. }
  2259. delimiter = delim[0];
  2260. /* no break */
  2261. case 1:
  2262. case 0:
  2263. break;
  2264. }
  2265. ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape);
  2266. RETURN_LONG(ret);
  2267. }
  2268. }
  2269. /* }}} */
  2270. /* {{{ proto void SplFileObject::setCsvControl([string delimiter [, string enclosure [, string escape ]]])
  2271. Set the delimiter, enclosure and escape character used in fgetcsv */
  2272. SPL_METHOD(SplFileObject, setCsvControl)
  2273. {
  2274. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2275. char delimiter = ',', enclosure = '"', escape='\\';
  2276. char *delim = NULL, *enclo = NULL, *esc = NULL;
  2277. size_t d_len = 0, e_len = 0, esc_len = 0;
  2278. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
  2279. switch(ZEND_NUM_ARGS())
  2280. {
  2281. case 3:
  2282. if (esc_len != 1) {
  2283. php_error_docref(NULL, E_WARNING, "escape must be a character");
  2284. RETURN_FALSE;
  2285. }
  2286. escape = esc[0];
  2287. /* no break */
  2288. case 2:
  2289. if (e_len != 1) {
  2290. php_error_docref(NULL, E_WARNING, "enclosure must be a character");
  2291. RETURN_FALSE;
  2292. }
  2293. enclosure = enclo[0];
  2294. /* no break */
  2295. case 1:
  2296. if (d_len != 1) {
  2297. php_error_docref(NULL, E_WARNING, "delimiter must be a character");
  2298. RETURN_FALSE;
  2299. }
  2300. delimiter = delim[0];
  2301. /* no break */
  2302. case 0:
  2303. break;
  2304. }
  2305. intern->u.file.delimiter = delimiter;
  2306. intern->u.file.enclosure = enclosure;
  2307. intern->u.file.escape = escape;
  2308. }
  2309. }
  2310. /* }}} */
  2311. /* {{{ proto array SplFileObject::getCsvControl()
  2312. Get the delimiter, enclosure and escape character used in fgetcsv */
  2313. SPL_METHOD(SplFileObject, getCsvControl)
  2314. {
  2315. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2316. char delimiter[2], enclosure[2], escape[2];
  2317. array_init(return_value);
  2318. delimiter[0] = intern->u.file.delimiter;
  2319. delimiter[1] = '\0';
  2320. enclosure[0] = intern->u.file.enclosure;
  2321. enclosure[1] = '\0';
  2322. escape[0] = intern->u.file.escape;
  2323. escape[1] = '\0';
  2324. add_next_index_string(return_value, delimiter);
  2325. add_next_index_string(return_value, enclosure);
  2326. add_next_index_string(return_value, escape);
  2327. }
  2328. /* }}} */
  2329. /* {{{ proto bool SplFileObject::flock(int operation [, int &wouldblock])
  2330. Portable file locking */
  2331. FileFunction(flock)
  2332. /* }}} */
  2333. /* {{{ proto bool SplFileObject::fflush()
  2334. Flush the file */
  2335. SPL_METHOD(SplFileObject, fflush)
  2336. {
  2337. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2338. if(!intern->u.file.stream) {
  2339. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2340. return;
  2341. }
  2342. RETURN_BOOL(!php_stream_flush(intern->u.file.stream));
  2343. } /* }}} */
  2344. /* {{{ proto int SplFileObject::ftell()
  2345. Return current file position */
  2346. SPL_METHOD(SplFileObject, ftell)
  2347. {
  2348. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2349. zend_long ret;
  2350. if(!intern->u.file.stream) {
  2351. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2352. return;
  2353. }
  2354. ret = php_stream_tell(intern->u.file.stream);
  2355. if (ret == -1) {
  2356. RETURN_FALSE;
  2357. } else {
  2358. RETURN_LONG(ret);
  2359. }
  2360. } /* }}} */
  2361. /* {{{ proto int SplFileObject::fseek(int pos [, int whence = SEEK_SET])
  2362. Return current file position */
  2363. SPL_METHOD(SplFileObject, fseek)
  2364. {
  2365. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2366. zend_long pos, whence = SEEK_SET;
  2367. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &pos, &whence) == FAILURE) {
  2368. return;
  2369. }
  2370. if(!intern->u.file.stream) {
  2371. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2372. return;
  2373. }
  2374. spl_filesystem_file_free_line(intern);
  2375. RETURN_LONG(php_stream_seek(intern->u.file.stream, pos, (int)whence));
  2376. } /* }}} */
  2377. /* {{{ proto int SplFileObject::fgetc()
  2378. Get a character form the file */
  2379. SPL_METHOD(SplFileObject, fgetc)
  2380. {
  2381. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2382. char buf[2];
  2383. int result;
  2384. if(!intern->u.file.stream) {
  2385. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2386. return;
  2387. }
  2388. spl_filesystem_file_free_line(intern);
  2389. result = php_stream_getc(intern->u.file.stream);
  2390. if (result == EOF) {
  2391. RETVAL_FALSE;
  2392. } else {
  2393. if (result == '\n') {
  2394. intern->u.file.current_line_num++;
  2395. }
  2396. buf[0] = result;
  2397. buf[1] = '\0';
  2398. RETURN_STRINGL(buf, 1);
  2399. }
  2400. } /* }}} */
  2401. /* {{{ proto string SplFileObject::fgetss([string allowable_tags])
  2402. Get a line from file pointer and strip HTML tags */
  2403. SPL_METHOD(SplFileObject, fgetss)
  2404. {
  2405. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2406. zval arg2;
  2407. if(!intern->u.file.stream) {
  2408. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2409. return;
  2410. }
  2411. if (intern->u.file.max_line_len > 0) {
  2412. ZVAL_LONG(&arg2, intern->u.file.max_line_len);
  2413. } else {
  2414. ZVAL_LONG(&arg2, 1024);
  2415. }
  2416. spl_filesystem_file_free_line(intern);
  2417. intern->u.file.current_line_num++;
  2418. FileFunctionCall(fgetss, ZEND_NUM_ARGS(), &arg2);
  2419. } /* }}} */
  2420. /* {{{ proto int SplFileObject::fpassthru()
  2421. Output all remaining data from a file pointer */
  2422. SPL_METHOD(SplFileObject, fpassthru)
  2423. {
  2424. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2425. if(!intern->u.file.stream) {
  2426. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2427. return;
  2428. }
  2429. RETURN_LONG(php_stream_passthru(intern->u.file.stream));
  2430. } /* }}} */
  2431. /* {{{ proto bool SplFileObject::fscanf(string format [, string ...])
  2432. Implements a mostly ANSI compatible fscanf() */
  2433. SPL_METHOD(SplFileObject, fscanf)
  2434. {
  2435. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2436. if(!intern->u.file.stream) {
  2437. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2438. return;
  2439. }
  2440. spl_filesystem_file_free_line(intern);
  2441. intern->u.file.current_line_num++;
  2442. FileFunctionCall(fscanf, ZEND_NUM_ARGS(), NULL);
  2443. }
  2444. /* }}} */
  2445. /* {{{ proto mixed SplFileObject::fwrite(string str [, int length])
  2446. Binary-safe file write */
  2447. SPL_METHOD(SplFileObject, fwrite)
  2448. {
  2449. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2450. char *str;
  2451. size_t str_len;
  2452. zend_long length = 0;
  2453. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &str, &str_len, &length) == FAILURE) {
  2454. return;
  2455. }
  2456. if(!intern->u.file.stream) {
  2457. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2458. return;
  2459. }
  2460. if (ZEND_NUM_ARGS() > 1) {
  2461. if (length >= 0) {
  2462. str_len = MIN((size_t)length, str_len);
  2463. } else {
  2464. /* Negative length given, nothing to write */
  2465. str_len = 0;
  2466. }
  2467. }
  2468. if (!str_len) {
  2469. RETURN_LONG(0);
  2470. }
  2471. RETURN_LONG(php_stream_write(intern->u.file.stream, str, str_len));
  2472. } /* }}} */
  2473. SPL_METHOD(SplFileObject, fread)
  2474. {
  2475. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2476. zend_long length = 0;
  2477. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &length) == FAILURE) {
  2478. return;
  2479. }
  2480. if(!intern->u.file.stream) {
  2481. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2482. return;
  2483. }
  2484. if (length <= 0) {
  2485. php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
  2486. RETURN_FALSE;
  2487. }
  2488. ZVAL_NEW_STR(return_value, zend_string_alloc(length, 0));
  2489. Z_STRLEN_P(return_value) = php_stream_read(intern->u.file.stream, Z_STRVAL_P(return_value), length);
  2490. /* needed because recv/read/gzread doesn't put a null at the end*/
  2491. Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
  2492. }
  2493. /* {{{ proto bool SplFileObject::fstat()
  2494. Stat() on a filehandle */
  2495. FileFunction(fstat)
  2496. /* }}} */
  2497. /* {{{ proto bool SplFileObject::ftruncate(int size)
  2498. Truncate file to 'size' length */
  2499. SPL_METHOD(SplFileObject, ftruncate)
  2500. {
  2501. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2502. zend_long size;
  2503. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
  2504. return;
  2505. }
  2506. if(!intern->u.file.stream) {
  2507. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2508. return;
  2509. }
  2510. if (!php_stream_truncate_supported(intern->u.file.stream)) {
  2511. zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't truncate file %s", intern->file_name);
  2512. RETURN_FALSE;
  2513. }
  2514. RETURN_BOOL(0 == php_stream_truncate_set_size(intern->u.file.stream, size));
  2515. } /* }}} */
  2516. /* {{{ proto void SplFileObject::seek(int line_pos)
  2517. Seek to specified line */
  2518. SPL_METHOD(SplFileObject, seek)
  2519. {
  2520. spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
  2521. zend_long line_pos;
  2522. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &line_pos) == FAILURE) {
  2523. return;
  2524. }
  2525. if(!intern->u.file.stream) {
  2526. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
  2527. return;
  2528. }
  2529. if (line_pos < 0) {
  2530. zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't seek file %s to negative line " ZEND_LONG_FMT, intern->file_name, line_pos);
  2531. RETURN_FALSE;
  2532. }
  2533. spl_filesystem_file_rewind(getThis(), intern);
  2534. while(intern->u.file.current_line_num < line_pos) {
  2535. if (spl_filesystem_file_read_line(getThis(), intern, 1) == FAILURE) {
  2536. break;
  2537. }
  2538. }
  2539. } /* }}} */
  2540. /* {{{ Function/Class/Method definitions */
  2541. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object___construct, 0, 0, 1)
  2542. ZEND_ARG_INFO(0, file_name)
  2543. ZEND_ARG_INFO(0, open_mode)
  2544. ZEND_ARG_INFO(0, use_include_path)
  2545. ZEND_ARG_INFO(0, context)
  2546. ZEND_END_ARG_INFO()
  2547. ZEND_BEGIN_ARG_INFO(arginfo_file_object_setFlags, 0)
  2548. ZEND_ARG_INFO(0, flags)
  2549. ZEND_END_ARG_INFO()
  2550. ZEND_BEGIN_ARG_INFO(arginfo_file_object_setMaxLineLen, 0)
  2551. ZEND_ARG_INFO(0, max_len)
  2552. ZEND_END_ARG_INFO()
  2553. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fgetcsv, 0, 0, 0)
  2554. ZEND_ARG_INFO(0, delimiter)
  2555. ZEND_ARG_INFO(0, enclosure)
  2556. ZEND_ARG_INFO(0, escape)
  2557. ZEND_END_ARG_INFO()
  2558. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fputcsv, 0, 0, 1)
  2559. ZEND_ARG_INFO(0, fields)
  2560. ZEND_ARG_INFO(0, delimiter)
  2561. ZEND_ARG_INFO(0, enclosure)
  2562. ZEND_ARG_INFO(0, escape)
  2563. ZEND_END_ARG_INFO()
  2564. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_flock, 0, 0, 1)
  2565. ZEND_ARG_INFO(0, operation)
  2566. ZEND_ARG_INFO(1, wouldblock)
  2567. ZEND_END_ARG_INFO()
  2568. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fseek, 0, 0, 1)
  2569. ZEND_ARG_INFO(0, pos)
  2570. ZEND_ARG_INFO(0, whence)
  2571. ZEND_END_ARG_INFO()
  2572. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fgetss, 0, 0, 0)
  2573. ZEND_ARG_INFO(0, allowable_tags)
  2574. ZEND_END_ARG_INFO()
  2575. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fscanf, 0, 0, 1)
  2576. ZEND_ARG_INFO(0, format)
  2577. ZEND_ARG_VARIADIC_INFO(1, vars)
  2578. ZEND_END_ARG_INFO()
  2579. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fwrite, 0, 0, 1)
  2580. ZEND_ARG_INFO(0, str)
  2581. ZEND_ARG_INFO(0, length)
  2582. ZEND_END_ARG_INFO()
  2583. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fread, 0, 0, 1)
  2584. ZEND_ARG_INFO(0, length)
  2585. ZEND_END_ARG_INFO()
  2586. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_ftruncate, 0, 0, 1)
  2587. ZEND_ARG_INFO(0, size)
  2588. ZEND_END_ARG_INFO()
  2589. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_seek, 0, 0, 1)
  2590. ZEND_ARG_INFO(0, line_pos)
  2591. ZEND_END_ARG_INFO()
  2592. static const zend_function_entry spl_SplFileObject_functions[] = {
  2593. SPL_ME(SplFileObject, __construct, arginfo_file_object___construct, ZEND_ACC_PUBLIC)
  2594. SPL_ME(SplFileObject, rewind, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2595. SPL_ME(SplFileObject, eof, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2596. SPL_ME(SplFileObject, valid, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2597. SPL_ME(SplFileObject, fgets, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2598. SPL_ME(SplFileObject, fgetcsv, arginfo_file_object_fgetcsv, ZEND_ACC_PUBLIC)
  2599. SPL_ME(SplFileObject, fputcsv, arginfo_file_object_fputcsv, ZEND_ACC_PUBLIC)
  2600. SPL_ME(SplFileObject, setCsvControl, arginfo_file_object_fgetcsv, ZEND_ACC_PUBLIC)
  2601. SPL_ME(SplFileObject, getCsvControl, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2602. SPL_ME(SplFileObject, flock, arginfo_file_object_flock, ZEND_ACC_PUBLIC)
  2603. SPL_ME(SplFileObject, fflush, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2604. SPL_ME(SplFileObject, ftell, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2605. SPL_ME(SplFileObject, fseek, arginfo_file_object_fseek, ZEND_ACC_PUBLIC)
  2606. SPL_ME(SplFileObject, fgetc, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2607. SPL_ME(SplFileObject, fpassthru, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2608. SPL_ME(SplFileObject, fgetss, arginfo_file_object_fgetss, ZEND_ACC_PUBLIC)
  2609. SPL_ME(SplFileObject, fscanf, arginfo_file_object_fscanf, ZEND_ACC_PUBLIC)
  2610. SPL_ME(SplFileObject, fwrite, arginfo_file_object_fwrite, ZEND_ACC_PUBLIC)
  2611. SPL_ME(SplFileObject, fread, arginfo_file_object_fread, ZEND_ACC_PUBLIC)
  2612. SPL_ME(SplFileObject, fstat, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2613. SPL_ME(SplFileObject, ftruncate, arginfo_file_object_ftruncate, ZEND_ACC_PUBLIC)
  2614. SPL_ME(SplFileObject, current, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2615. SPL_ME(SplFileObject, key, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2616. SPL_ME(SplFileObject, next, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2617. SPL_ME(SplFileObject, setFlags, arginfo_file_object_setFlags, ZEND_ACC_PUBLIC)
  2618. SPL_ME(SplFileObject, getFlags, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2619. SPL_ME(SplFileObject, setMaxLineLen, arginfo_file_object_setMaxLineLen, ZEND_ACC_PUBLIC)
  2620. SPL_ME(SplFileObject, getMaxLineLen, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2621. SPL_ME(SplFileObject, hasChildren, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2622. SPL_ME(SplFileObject, getChildren, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2623. SPL_ME(SplFileObject, seek, arginfo_file_object_seek, ZEND_ACC_PUBLIC)
  2624. /* mappings */
  2625. SPL_MA(SplFileObject, getCurrentLine, SplFileObject, fgets, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2626. SPL_MA(SplFileObject, __toString, SplFileObject, fgets, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2627. PHP_FE_END
  2628. };
  2629. ZEND_BEGIN_ARG_INFO_EX(arginfo_temp_file_object___construct, 0, 0, 0)
  2630. ZEND_ARG_INFO(0, max_memory)
  2631. ZEND_END_ARG_INFO()
  2632. static const zend_function_entry spl_SplTempFileObject_functions[] = {
  2633. SPL_ME(SplTempFileObject, __construct, arginfo_temp_file_object___construct, ZEND_ACC_PUBLIC)
  2634. PHP_FE_END
  2635. };
  2636. /* }}} */
  2637. /* {{{ PHP_MINIT_FUNCTION(spl_directory)
  2638. */
  2639. PHP_MINIT_FUNCTION(spl_directory)
  2640. {
  2641. REGISTER_SPL_STD_CLASS_EX(SplFileInfo, spl_filesystem_object_new, spl_SplFileInfo_functions);
  2642. memcpy(&spl_filesystem_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  2643. spl_filesystem_object_handlers.offset = XtOffsetOf(spl_filesystem_object, std);
  2644. spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
  2645. spl_filesystem_object_handlers.cast_object = spl_filesystem_object_cast;
  2646. spl_filesystem_object_handlers.get_debug_info = spl_filesystem_object_get_debug_info;
  2647. spl_filesystem_object_handlers.dtor_obj = spl_filesystem_object_destroy_object;
  2648. spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
  2649. spl_ce_SplFileInfo->serialize = zend_class_serialize_deny;
  2650. spl_ce_SplFileInfo->unserialize = zend_class_unserialize_deny;
  2651. REGISTER_SPL_SUB_CLASS_EX(DirectoryIterator, SplFileInfo, spl_filesystem_object_new, spl_DirectoryIterator_functions);
  2652. zend_class_implements(spl_ce_DirectoryIterator, 1, zend_ce_iterator);
  2653. REGISTER_SPL_IMPLEMENTS(DirectoryIterator, SeekableIterator);
  2654. spl_ce_DirectoryIterator->get_iterator = spl_filesystem_dir_get_iterator;
  2655. REGISTER_SPL_SUB_CLASS_EX(FilesystemIterator, DirectoryIterator, spl_filesystem_object_new, spl_FilesystemIterator_functions);
  2656. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_MODE_MASK", SPL_FILE_DIR_CURRENT_MODE_MASK);
  2657. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_PATHNAME", SPL_FILE_DIR_CURRENT_AS_PATHNAME);
  2658. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_FILEINFO", SPL_FILE_DIR_CURRENT_AS_FILEINFO);
  2659. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_SELF", SPL_FILE_DIR_CURRENT_AS_SELF);
  2660. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_MODE_MASK", SPL_FILE_DIR_KEY_MODE_MASK);
  2661. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_AS_PATHNAME", SPL_FILE_DIR_KEY_AS_PATHNAME);
  2662. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "FOLLOW_SYMLINKS", SPL_FILE_DIR_FOLLOW_SYMLINKS);
  2663. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_AS_FILENAME", SPL_FILE_DIR_KEY_AS_FILENAME);
  2664. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "NEW_CURRENT_AND_KEY", SPL_FILE_DIR_KEY_AS_FILENAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO);
  2665. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "OTHER_MODE_MASK", SPL_FILE_DIR_OTHERS_MASK);
  2666. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "SKIP_DOTS", SPL_FILE_DIR_SKIPDOTS);
  2667. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "UNIX_PATHS", SPL_FILE_DIR_UNIXPATHS);
  2668. spl_ce_FilesystemIterator->get_iterator = spl_filesystem_tree_get_iterator;
  2669. REGISTER_SPL_SUB_CLASS_EX(RecursiveDirectoryIterator, FilesystemIterator, spl_filesystem_object_new, spl_RecursiveDirectoryIterator_functions);
  2670. REGISTER_SPL_IMPLEMENTS(RecursiveDirectoryIterator, RecursiveIterator);
  2671. memcpy(&spl_filesystem_object_check_handlers, &spl_filesystem_object_handlers, sizeof(zend_object_handlers));
  2672. spl_filesystem_object_check_handlers.clone_obj = NULL;
  2673. spl_filesystem_object_check_handlers.get_method = spl_filesystem_object_get_method_check;
  2674. #ifdef HAVE_GLOB
  2675. REGISTER_SPL_SUB_CLASS_EX(GlobIterator, FilesystemIterator, spl_filesystem_object_new_check, spl_GlobIterator_functions);
  2676. REGISTER_SPL_IMPLEMENTS(GlobIterator, Countable);
  2677. #endif
  2678. REGISTER_SPL_SUB_CLASS_EX(SplFileObject, SplFileInfo, spl_filesystem_object_new_check, spl_SplFileObject_functions);
  2679. REGISTER_SPL_IMPLEMENTS(SplFileObject, RecursiveIterator);
  2680. REGISTER_SPL_IMPLEMENTS(SplFileObject, SeekableIterator);
  2681. REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "DROP_NEW_LINE", SPL_FILE_OBJECT_DROP_NEW_LINE);
  2682. REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "READ_AHEAD", SPL_FILE_OBJECT_READ_AHEAD);
  2683. REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "SKIP_EMPTY", SPL_FILE_OBJECT_SKIP_EMPTY);
  2684. REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "READ_CSV", SPL_FILE_OBJECT_READ_CSV);
  2685. REGISTER_SPL_SUB_CLASS_EX(SplTempFileObject, SplFileObject, spl_filesystem_object_new_check, spl_SplTempFileObject_functions);
  2686. return SUCCESS;
  2687. }
  2688. /* }}} */
  2689. /*
  2690. * Local variables:
  2691. * tab-width: 4
  2692. * c-basic-offset: 4
  2693. * End:
  2694. * vim600: noet sw=4 ts=4 fdm=marker
  2695. * vim<600: noet sw=4 ts=4
  2696. */