phar.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649
  1. /*
  2. +----------------------------------------------------------------------+
  3. | phar php single-file executable PHP extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2005-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Gregory Beaver <cellog@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #define PHAR_MAIN 1
  20. #include "phar_internal.h"
  21. #include "SAPI.h"
  22. #include "func_interceptors.h"
  23. static void destroy_phar_data(zval *zv);
  24. ZEND_DECLARE_MODULE_GLOBALS(phar)
  25. zend_string *(*phar_save_resolve_path)(const char *filename, size_t filename_len);
  26. /**
  27. * set's phar->is_writeable based on the current INI value
  28. */
  29. static int phar_set_writeable_bit(zval *zv, void *argument) /* {{{ */
  30. {
  31. zend_bool keep = *(zend_bool *)argument;
  32. phar_archive_data *phar = (phar_archive_data *)Z_PTR_P(zv);
  33. if (!phar->is_data) {
  34. phar->is_writeable = !keep;
  35. }
  36. return ZEND_HASH_APPLY_KEEP;
  37. }
  38. /* }}} */
  39. /* if the original value is 0 (disabled), then allow setting/unsetting at will. Otherwise only allow 1 (enabled), and error on disabling */
  40. ZEND_INI_MH(phar_ini_modify_handler) /* {{{ */
  41. {
  42. zend_bool old, ini;
  43. if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
  44. old = PHAR_G(readonly_orig);
  45. } else {
  46. old = PHAR_G(require_hash_orig);
  47. }
  48. if (ZSTR_LEN(new_value) == 2 && !strcasecmp("on", ZSTR_VAL(new_value))) {
  49. ini = (zend_bool) 1;
  50. }
  51. else if (ZSTR_LEN(new_value) == 3 && !strcasecmp("yes", ZSTR_VAL(new_value))) {
  52. ini = (zend_bool) 1;
  53. }
  54. else if (ZSTR_LEN(new_value) == 4 && !strcasecmp("true", ZSTR_VAL(new_value))) {
  55. ini = (zend_bool) 1;
  56. }
  57. else {
  58. ini = (zend_bool) atoi(ZSTR_VAL(new_value));
  59. }
  60. /* do not allow unsetting in runtime */
  61. if (stage == ZEND_INI_STAGE_STARTUP) {
  62. if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
  63. PHAR_G(readonly_orig) = ini;
  64. } else {
  65. PHAR_G(require_hash_orig) = ini;
  66. }
  67. } else if (old && !ini) {
  68. return FAILURE;
  69. }
  70. if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
  71. PHAR_G(readonly) = ini;
  72. if (PHAR_G(request_init) && HT_FLAGS(&PHAR_G(phar_fname_map))) {
  73. zend_hash_apply_with_argument(&(PHAR_G(phar_fname_map)), phar_set_writeable_bit, (void *)&ini);
  74. }
  75. } else {
  76. PHAR_G(require_hash) = ini;
  77. }
  78. return SUCCESS;
  79. }
  80. /* }}}*/
  81. /* this global stores the global cached pre-parsed manifests */
  82. HashTable cached_phars;
  83. HashTable cached_alias;
  84. static void phar_split_cache_list(void) /* {{{ */
  85. {
  86. char *tmp;
  87. char *key, *lasts, *end;
  88. char ds[2];
  89. phar_archive_data *phar;
  90. uint32_t i = 0;
  91. if (!PHAR_G(cache_list) || !(PHAR_G(cache_list)[0])) {
  92. return;
  93. }
  94. ds[0] = DEFAULT_DIR_SEPARATOR;
  95. ds[1] = '\0';
  96. tmp = estrdup(PHAR_G(cache_list));
  97. /* fake request startup */
  98. PHAR_G(request_init) = 1;
  99. zend_hash_init(&EG(regular_list), 0, NULL, NULL, 0);
  100. EG(regular_list).nNextFreeElement=1; /* we don't want resource id 0 */
  101. PHAR_G(has_bz2) = zend_hash_str_exists(&module_registry, "bz2", sizeof("bz2")-1);
  102. PHAR_G(has_zlib) = zend_hash_str_exists(&module_registry, "zlib", sizeof("zlib")-1);
  103. /* these two are dummies and will be destroyed later */
  104. zend_hash_init(&cached_phars, sizeof(phar_archive_data*), zend_get_hash_value, destroy_phar_data, 1);
  105. zend_hash_init(&cached_alias, sizeof(phar_archive_data*), zend_get_hash_value, NULL, 1);
  106. /* these two are real and will be copied over cached_phars/cached_alias later */
  107. zend_hash_init(&(PHAR_G(phar_fname_map)), sizeof(phar_archive_data*), zend_get_hash_value, destroy_phar_data, 1);
  108. zend_hash_init(&(PHAR_G(phar_alias_map)), sizeof(phar_archive_data*), zend_get_hash_value, NULL, 1);
  109. PHAR_G(manifest_cached) = 1;
  110. PHAR_G(persist) = 1;
  111. for (key = php_strtok_r(tmp, ds, &lasts);
  112. key;
  113. key = php_strtok_r(NULL, ds, &lasts)) {
  114. end = strchr(key, DEFAULT_DIR_SEPARATOR);
  115. if (end) {
  116. if (SUCCESS == phar_open_from_filename(key, end - key, NULL, 0, 0, &phar, NULL)) {
  117. finish_up:
  118. phar->phar_pos = i++;
  119. php_stream_close(phar->fp);
  120. phar->fp = NULL;
  121. } else {
  122. finish_error:
  123. PHAR_G(persist) = 0;
  124. PHAR_G(manifest_cached) = 0;
  125. efree(tmp);
  126. zend_hash_destroy(&(PHAR_G(phar_fname_map)));
  127. HT_FLAGS(&PHAR_G(phar_fname_map)) = 0;
  128. zend_hash_destroy(&(PHAR_G(phar_alias_map)));
  129. HT_FLAGS(&PHAR_G(phar_alias_map)) = 0;
  130. zend_hash_destroy(&cached_phars);
  131. zend_hash_destroy(&cached_alias);
  132. zend_hash_graceful_reverse_destroy(&EG(regular_list));
  133. memset(&EG(regular_list), 0, sizeof(HashTable));
  134. /* free cached manifests */
  135. PHAR_G(request_init) = 0;
  136. return;
  137. }
  138. } else {
  139. if (SUCCESS == phar_open_from_filename(key, strlen(key), NULL, 0, 0, &phar, NULL)) {
  140. goto finish_up;
  141. } else {
  142. goto finish_error;
  143. }
  144. }
  145. }
  146. PHAR_G(persist) = 0;
  147. PHAR_G(request_init) = 0;
  148. /* destroy dummy values from before */
  149. zend_hash_destroy(&cached_phars);
  150. zend_hash_destroy(&cached_alias);
  151. cached_phars = PHAR_G(phar_fname_map);
  152. cached_alias = PHAR_G(phar_alias_map);
  153. HT_FLAGS(&PHAR_G(phar_fname_map)) = 0;
  154. HT_FLAGS(&PHAR_G(phar_alias_map)) = 0;
  155. zend_hash_graceful_reverse_destroy(&EG(regular_list));
  156. memset(&EG(regular_list), 0, sizeof(HashTable));
  157. efree(tmp);
  158. }
  159. /* }}} */
  160. ZEND_INI_MH(phar_ini_cache_list) /* {{{ */
  161. {
  162. PHAR_G(cache_list) = ZSTR_VAL(new_value);
  163. if (stage == ZEND_INI_STAGE_STARTUP) {
  164. phar_split_cache_list();
  165. }
  166. return SUCCESS;
  167. }
  168. /* }}} */
  169. PHP_INI_BEGIN()
  170. STD_PHP_INI_BOOLEAN("phar.readonly", "1", PHP_INI_ALL, phar_ini_modify_handler, readonly, zend_phar_globals, phar_globals)
  171. STD_PHP_INI_BOOLEAN("phar.require_hash", "1", PHP_INI_ALL, phar_ini_modify_handler, require_hash, zend_phar_globals, phar_globals)
  172. STD_PHP_INI_ENTRY("phar.cache_list", "", PHP_INI_SYSTEM, phar_ini_cache_list, cache_list, zend_phar_globals, phar_globals)
  173. PHP_INI_END()
  174. /**
  175. * When all uses of a phar have been concluded, this frees the manifest
  176. * and the phar slot
  177. */
  178. void phar_destroy_phar_data(phar_archive_data *phar) /* {{{ */
  179. {
  180. if (phar->alias && phar->alias != phar->fname) {
  181. pefree(phar->alias, phar->is_persistent);
  182. phar->alias = NULL;
  183. }
  184. if (phar->fname) {
  185. pefree(phar->fname, phar->is_persistent);
  186. phar->fname = NULL;
  187. }
  188. if (phar->signature) {
  189. pefree(phar->signature, phar->is_persistent);
  190. phar->signature = NULL;
  191. }
  192. if (HT_FLAGS(&phar->manifest)) {
  193. zend_hash_destroy(&phar->manifest);
  194. HT_FLAGS(&phar->manifest) = 0;
  195. }
  196. if (HT_FLAGS(&phar->mounted_dirs)) {
  197. zend_hash_destroy(&phar->mounted_dirs);
  198. HT_FLAGS(&phar->mounted_dirs) = 0;
  199. }
  200. if (HT_FLAGS(&phar->virtual_dirs)) {
  201. zend_hash_destroy(&phar->virtual_dirs);
  202. HT_FLAGS(&phar->virtual_dirs) = 0;
  203. }
  204. if (Z_TYPE(phar->metadata) != IS_UNDEF) {
  205. if (phar->is_persistent) {
  206. if (phar->metadata_len) {
  207. /* for zip comments that are strings */
  208. free(Z_PTR(phar->metadata));
  209. } else {
  210. zval_internal_ptr_dtor(&phar->metadata);
  211. }
  212. } else {
  213. zval_ptr_dtor(&phar->metadata);
  214. }
  215. phar->metadata_len = 0;
  216. ZVAL_UNDEF(&phar->metadata);
  217. }
  218. if (phar->fp) {
  219. php_stream_close(phar->fp);
  220. phar->fp = 0;
  221. }
  222. if (phar->ufp) {
  223. php_stream_close(phar->ufp);
  224. phar->ufp = 0;
  225. }
  226. pefree(phar, phar->is_persistent);
  227. }
  228. /* }}}*/
  229. /**
  230. * Delete refcount and destruct if needed. On destruct return 1 else 0.
  231. */
  232. int phar_archive_delref(phar_archive_data *phar) /* {{{ */
  233. {
  234. if (phar->is_persistent) {
  235. return 0;
  236. }
  237. if (--phar->refcount < 0) {
  238. if (PHAR_G(request_done)
  239. || zend_hash_str_del(&(PHAR_G(phar_fname_map)), phar->fname, phar->fname_len) != SUCCESS) {
  240. phar_destroy_phar_data(phar);
  241. }
  242. return 1;
  243. } else if (!phar->refcount) {
  244. /* invalidate phar cache */
  245. PHAR_G(last_phar) = NULL;
  246. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  247. if (phar->fp && (!(phar->flags & PHAR_FILE_COMPRESSION_MASK) || !phar->alias)) {
  248. /* close open file handle - allows removal or rename of
  249. the file on windows, which has greedy locking
  250. only close if the archive was not already compressed. If it
  251. was compressed, then the fp does not refer to the original file.
  252. We're also closing compressed files to save resources,
  253. but only if the archive isn't aliased. */
  254. php_stream_close(phar->fp);
  255. phar->fp = NULL;
  256. }
  257. if (!zend_hash_num_elements(&phar->manifest)) {
  258. /* this is a new phar that has perhaps had an alias/metadata set, but has never
  259. been flushed */
  260. if (zend_hash_str_del(&(PHAR_G(phar_fname_map)), phar->fname, phar->fname_len) != SUCCESS) {
  261. phar_destroy_phar_data(phar);
  262. }
  263. return 1;
  264. }
  265. }
  266. return 0;
  267. }
  268. /* }}}*/
  269. /**
  270. * Destroy phar's in shutdown, here we don't care about aliases
  271. */
  272. static void destroy_phar_data_only(zval *zv) /* {{{ */
  273. {
  274. phar_archive_data *phar_data = (phar_archive_data *) Z_PTR_P(zv);
  275. if (EG(exception) || --phar_data->refcount < 0) {
  276. phar_destroy_phar_data(phar_data);
  277. }
  278. }
  279. /* }}}*/
  280. /**
  281. * Delete aliases to phar's that got kicked out of the global table
  282. */
  283. static int phar_unalias_apply(zval *zv, void *argument) /* {{{ */
  284. {
  285. return Z_PTR_P(zv) == argument ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_KEEP;
  286. }
  287. /* }}} */
  288. /**
  289. * Delete aliases to phar's that got kicked out of the global table
  290. */
  291. static int phar_tmpclose_apply(zval *zv) /* {{{ */
  292. {
  293. phar_entry_info *entry = (phar_entry_info *) Z_PTR_P(zv);
  294. if (entry->fp_type != PHAR_TMP) {
  295. return ZEND_HASH_APPLY_KEEP;
  296. }
  297. if (entry->fp && !entry->fp_refcount) {
  298. php_stream_close(entry->fp);
  299. entry->fp = NULL;
  300. }
  301. return ZEND_HASH_APPLY_KEEP;
  302. }
  303. /* }}} */
  304. /**
  305. * Filename map destructor
  306. */
  307. static void destroy_phar_data(zval *zv) /* {{{ */
  308. {
  309. phar_archive_data *phar_data = (phar_archive_data *)Z_PTR_P(zv);
  310. if (PHAR_G(request_ends)) {
  311. /* first, iterate over the manifest and close all PHAR_TMP entry fp handles,
  312. this prevents unnecessary unfreed stream resources */
  313. zend_hash_apply(&(phar_data->manifest), phar_tmpclose_apply);
  314. destroy_phar_data_only(zv);
  315. return;
  316. }
  317. zend_hash_apply_with_argument(&(PHAR_G(phar_alias_map)), phar_unalias_apply, phar_data);
  318. if (--phar_data->refcount < 0) {
  319. phar_destroy_phar_data(phar_data);
  320. }
  321. }
  322. /* }}}*/
  323. /**
  324. * destructor for the manifest hash, frees each file's entry
  325. */
  326. void destroy_phar_manifest_entry_int(phar_entry_info *entry) /* {{{ */
  327. {
  328. if (entry->cfp) {
  329. php_stream_close(entry->cfp);
  330. entry->cfp = 0;
  331. }
  332. if (entry->fp) {
  333. php_stream_close(entry->fp);
  334. entry->fp = 0;
  335. }
  336. if (Z_TYPE(entry->metadata) != IS_UNDEF) {
  337. if (entry->is_persistent) {
  338. if (entry->metadata_len) {
  339. /* for zip comments that are strings */
  340. free(Z_PTR(entry->metadata));
  341. } else {
  342. zval_internal_ptr_dtor(&entry->metadata);
  343. }
  344. } else {
  345. zval_ptr_dtor(&entry->metadata);
  346. }
  347. entry->metadata_len = 0;
  348. ZVAL_UNDEF(&entry->metadata);
  349. }
  350. if (entry->metadata_str.s) {
  351. smart_str_free(&entry->metadata_str);
  352. entry->metadata_str.s = NULL;
  353. }
  354. pefree(entry->filename, entry->is_persistent);
  355. if (entry->link) {
  356. pefree(entry->link, entry->is_persistent);
  357. entry->link = 0;
  358. }
  359. if (entry->tmp) {
  360. pefree(entry->tmp, entry->is_persistent);
  361. entry->tmp = 0;
  362. }
  363. }
  364. /* }}} */
  365. void destroy_phar_manifest_entry(zval *zv) /* {{{ */
  366. {
  367. phar_entry_info *entry = Z_PTR_P(zv);
  368. destroy_phar_manifest_entry_int(entry);
  369. pefree(entry, entry->is_persistent);
  370. }
  371. /* }}} */
  372. int phar_entry_delref(phar_entry_data *idata) /* {{{ */
  373. {
  374. int ret = 0;
  375. if (idata->internal_file && !idata->internal_file->is_persistent) {
  376. if (--idata->internal_file->fp_refcount < 0) {
  377. idata->internal_file->fp_refcount = 0;
  378. }
  379. if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
  380. php_stream_close(idata->fp);
  381. }
  382. /* if phar_get_or_create_entry_data returns a sub-directory, we have to free it */
  383. if (idata->internal_file->is_temp_dir) {
  384. destroy_phar_manifest_entry_int(idata->internal_file);
  385. efree(idata->internal_file);
  386. }
  387. }
  388. phar_archive_delref(idata->phar);
  389. efree(idata);
  390. return ret;
  391. }
  392. /* }}} */
  393. /**
  394. * Removes an entry, either by actually removing it or by marking it.
  395. */
  396. void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */
  397. {
  398. phar_archive_data *phar;
  399. phar = idata->phar;
  400. if (idata->internal_file->fp_refcount < 2) {
  401. if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
  402. php_stream_close(idata->fp);
  403. }
  404. zend_hash_str_del(&idata->phar->manifest, idata->internal_file->filename, idata->internal_file->filename_len);
  405. idata->phar->refcount--;
  406. efree(idata);
  407. } else {
  408. idata->internal_file->is_deleted = 1;
  409. phar_entry_delref(idata);
  410. }
  411. if (!phar->donotflush) {
  412. phar_flush(phar, 0, 0, 0, error);
  413. }
  414. }
  415. /* }}} */
  416. #define MAPPHAR_ALLOC_FAIL(msg) \
  417. if (fp) {\
  418. php_stream_close(fp);\
  419. }\
  420. if (error) {\
  421. spprintf(error, 0, msg, fname);\
  422. }\
  423. return FAILURE;
  424. #define MAPPHAR_FAIL(msg) \
  425. efree(savebuf);\
  426. if (mydata) {\
  427. phar_destroy_phar_data(mydata);\
  428. }\
  429. if (signature) {\
  430. pefree(signature, PHAR_G(persist));\
  431. }\
  432. MAPPHAR_ALLOC_FAIL(msg)
  433. #ifdef WORDS_BIGENDIAN
  434. # define PHAR_GET_32(buffer, var) \
  435. var = ((((unsigned char*)(buffer))[3]) << 24) \
  436. | ((((unsigned char*)(buffer))[2]) << 16) \
  437. | ((((unsigned char*)(buffer))[1]) << 8) \
  438. | (((unsigned char*)(buffer))[0]); \
  439. (buffer) += 4
  440. # define PHAR_GET_16(buffer, var) \
  441. var = ((((unsigned char*)(buffer))[1]) << 8) \
  442. | (((unsigned char*)(buffer))[0]); \
  443. (buffer) += 2
  444. #else
  445. # define PHAR_GET_32(buffer, var) \
  446. memcpy(&var, buffer, sizeof(var)); \
  447. buffer += 4
  448. # define PHAR_GET_16(buffer, var) \
  449. var = *(uint16_t*)(buffer); \
  450. buffer += 2
  451. #endif
  452. #define PHAR_ZIP_16(var) ((uint16_t)((((uint16_t)var[0]) & 0xff) | \
  453. (((uint16_t)var[1]) & 0xff) << 8))
  454. #define PHAR_ZIP_32(var) ((uint32_t)((((uint32_t)var[0]) & 0xff) | \
  455. (((uint32_t)var[1]) & 0xff) << 8 | \
  456. (((uint32_t)var[2]) & 0xff) << 16 | \
  457. (((uint32_t)var[3]) & 0xff) << 24))
  458. /**
  459. * Open an already loaded phar
  460. */
  461. int phar_open_parsed_phar(char *fname, size_t fname_len, char *alias, size_t alias_len, zend_bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
  462. {
  463. phar_archive_data *phar;
  464. #ifdef PHP_WIN32
  465. char *unixfname;
  466. #endif
  467. if (error) {
  468. *error = NULL;
  469. }
  470. #ifdef PHP_WIN32
  471. unixfname = estrndup(fname, fname_len);
  472. phar_unixify_path_separators(unixfname, fname_len);
  473. if (SUCCESS == phar_get_archive(&phar, unixfname, fname_len, alias, alias_len, error)
  474. && ((alias && fname_len == phar->fname_len
  475. && !strncmp(unixfname, phar->fname, fname_len)) || !alias)
  476. ) {
  477. phar_entry_info *stub;
  478. efree(unixfname);
  479. #else
  480. if (SUCCESS == phar_get_archive(&phar, fname, fname_len, alias, alias_len, error)
  481. && ((alias && fname_len == phar->fname_len
  482. && !strncmp(fname, phar->fname, fname_len)) || !alias)
  483. ) {
  484. phar_entry_info *stub;
  485. #endif
  486. /* logic above is as follows:
  487. If an explicit alias was requested, ensure the filename passed in
  488. matches the phar's filename.
  489. If no alias was passed in, then it can match either and be valid
  490. */
  491. if (!is_data) {
  492. /* prevent any ".phar" without a stub getting through */
  493. if (!phar->halt_offset && !phar->is_brandnew && (phar->is_tar || phar->is_zip)) {
  494. if (PHAR_G(readonly) && NULL == (stub = zend_hash_str_find_ptr(&(phar->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) {
  495. if (error) {
  496. spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname);
  497. }
  498. return FAILURE;
  499. }
  500. }
  501. }
  502. if (pphar) {
  503. *pphar = phar;
  504. }
  505. return SUCCESS;
  506. } else {
  507. #ifdef PHP_WIN32
  508. efree(unixfname);
  509. #endif
  510. if (pphar) {
  511. *pphar = NULL;
  512. }
  513. if (phar && error && !(options & REPORT_ERRORS)) {
  514. efree(error);
  515. }
  516. return FAILURE;
  517. }
  518. }
  519. /* }}}*/
  520. /**
  521. * Parse out metadata from the manifest for a single file
  522. *
  523. * Meta-data is in this format:
  524. * [len32][data...]
  525. *
  526. * data is the serialized zval
  527. */
  528. int phar_parse_metadata(char **buffer, zval *metadata, uint32_t zip_metadata_len) /* {{{ */
  529. {
  530. php_unserialize_data_t var_hash;
  531. if (zip_metadata_len) {
  532. const unsigned char *p;
  533. unsigned char *p_buff = (unsigned char *)estrndup(*buffer, zip_metadata_len);
  534. p = p_buff;
  535. ZVAL_NULL(metadata);
  536. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  537. if (!php_var_unserialize(metadata, &p, p + zip_metadata_len, &var_hash)) {
  538. efree(p_buff);
  539. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  540. zval_ptr_dtor(metadata);
  541. ZVAL_UNDEF(metadata);
  542. return FAILURE;
  543. }
  544. efree(p_buff);
  545. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  546. if (PHAR_G(persist)) {
  547. /* lazy init metadata */
  548. zval_ptr_dtor(metadata);
  549. Z_PTR_P(metadata) = pemalloc(zip_metadata_len, 1);
  550. memcpy(Z_PTR_P(metadata), *buffer, zip_metadata_len);
  551. return SUCCESS;
  552. }
  553. } else {
  554. ZVAL_UNDEF(metadata);
  555. }
  556. return SUCCESS;
  557. }
  558. /* }}}*/
  559. /**
  560. * Size of fixed fields in the manifest.
  561. * See: http://php.net/manual/en/phar.fileformat.phar.php
  562. */
  563. #define MANIFEST_FIXED_LEN 18
  564. #define SAFE_PHAR_GET_32(buffer, endbuffer, var) \
  565. if (UNEXPECTED(buffer + 4 > endbuffer)) { \
  566. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)"); \
  567. } \
  568. PHAR_GET_32(buffer, var);
  569. /**
  570. * Does not check for a previously opened phar in the cache.
  571. *
  572. * Parse a new one and add it to the cache, returning either SUCCESS or
  573. * FAILURE, and setting pphar to the pointer to the manifest entry
  574. *
  575. * This is used by phar_open_from_filename to process the manifest, but can be called
  576. * directly.
  577. */
  578. static int phar_parse_pharfile(php_stream *fp, char *fname, size_t fname_len, char *alias, size_t alias_len, zend_long halt_offset, phar_archive_data** pphar, uint32_t compression, char **error) /* {{{ */
  579. {
  580. char b32[4], *buffer, *endbuffer, *savebuf;
  581. phar_archive_data *mydata = NULL;
  582. phar_entry_info entry;
  583. uint32_t manifest_len, manifest_count, manifest_flags, manifest_index, tmp_len, sig_flags;
  584. uint16_t manifest_ver;
  585. uint32_t len;
  586. zend_long offset;
  587. size_t sig_len;
  588. int register_alias = 0, temp_alias = 0;
  589. char *signature = NULL;
  590. zend_string *str;
  591. if (pphar) {
  592. *pphar = NULL;
  593. }
  594. if (error) {
  595. *error = NULL;
  596. }
  597. /* check for ?>\n and increment accordingly */
  598. if (-1 == php_stream_seek(fp, halt_offset, SEEK_SET)) {
  599. MAPPHAR_ALLOC_FAIL("cannot seek to __HALT_COMPILER(); location in phar \"%s\"")
  600. }
  601. buffer = b32;
  602. if (3 != php_stream_read(fp, buffer, 3)) {
  603. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  604. }
  605. if ((*buffer == ' ' || *buffer == '\n') && *(buffer + 1) == '?' && *(buffer + 2) == '>') {
  606. int nextchar;
  607. halt_offset += 3;
  608. if (EOF == (nextchar = php_stream_getc(fp))) {
  609. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  610. }
  611. if ((char) nextchar == '\r') {
  612. /* if we have an \r we require an \n as well */
  613. if (EOF == (nextchar = php_stream_getc(fp)) || (char)nextchar != '\n') {
  614. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  615. }
  616. ++halt_offset;
  617. }
  618. if ((char) nextchar == '\n') {
  619. ++halt_offset;
  620. }
  621. }
  622. /* make sure we are at the right location to read the manifest */
  623. if (-1 == php_stream_seek(fp, halt_offset, SEEK_SET)) {
  624. MAPPHAR_ALLOC_FAIL("cannot seek to __HALT_COMPILER(); location in phar \"%s\"")
  625. }
  626. /* read in manifest */
  627. buffer = b32;
  628. if (4 != php_stream_read(fp, buffer, 4)) {
  629. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at manifest length)")
  630. }
  631. PHAR_GET_32(buffer, manifest_len);
  632. if (manifest_len > 1048576 * 100) {
  633. /* prevent serious memory issues by limiting manifest to at most 100 MB in length */
  634. MAPPHAR_ALLOC_FAIL("manifest cannot be larger than 100 MB in phar \"%s\"")
  635. }
  636. buffer = (char *)emalloc(manifest_len);
  637. savebuf = buffer;
  638. endbuffer = buffer + manifest_len;
  639. if (manifest_len < MANIFEST_FIXED_LEN || manifest_len != php_stream_read(fp, buffer, manifest_len)) {
  640. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)")
  641. }
  642. /* extract the number of entries */
  643. SAFE_PHAR_GET_32(buffer, endbuffer, manifest_count);
  644. if (manifest_count == 0) {
  645. MAPPHAR_FAIL("in phar \"%s\", manifest claims to have zero entries. Phars must have at least 1 entry");
  646. }
  647. /* extract API version, lowest nibble currently unused */
  648. manifest_ver = (((unsigned char)buffer[0]) << 8)
  649. + ((unsigned char)buffer[1]);
  650. buffer += 2;
  651. if ((manifest_ver & PHAR_API_VER_MASK) < PHAR_API_MIN_READ) {
  652. efree(savebuf);
  653. php_stream_close(fp);
  654. if (error) {
  655. spprintf(error, 0, "phar \"%s\" is API version %1.u.%1.u.%1.u, and cannot be processed", fname, manifest_ver >> 12, (manifest_ver >> 8) & 0xF, (manifest_ver >> 4) & 0x0F);
  656. }
  657. return FAILURE;
  658. }
  659. SAFE_PHAR_GET_32(buffer, endbuffer, manifest_flags);
  660. manifest_flags &= ~PHAR_HDR_COMPRESSION_MASK;
  661. manifest_flags &= ~PHAR_FILE_COMPRESSION_MASK;
  662. /* remember whether this entire phar was compressed with gz/bzip2 */
  663. manifest_flags |= compression;
  664. /* The lowest nibble contains the phar wide flags. The compression flags can */
  665. /* be ignored on reading because it is being generated anyways. */
  666. if (manifest_flags & PHAR_HDR_SIGNATURE) {
  667. char sig_buf[8], *sig_ptr = sig_buf;
  668. zend_off_t read_len;
  669. size_t end_of_phar;
  670. if (-1 == php_stream_seek(fp, -8, SEEK_END)
  671. || (read_len = php_stream_tell(fp)) < 20
  672. || 8 != php_stream_read(fp, sig_buf, 8)
  673. || memcmp(sig_buf+4, "GBMB", 4)) {
  674. efree(savebuf);
  675. php_stream_close(fp);
  676. if (error) {
  677. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  678. }
  679. return FAILURE;
  680. }
  681. PHAR_GET_32(sig_ptr, sig_flags);
  682. switch(sig_flags) {
  683. case PHAR_SIG_OPENSSL: {
  684. uint32_t signature_len;
  685. char *sig;
  686. zend_off_t whence;
  687. /* we store the signature followed by the signature length */
  688. if (-1 == php_stream_seek(fp, -12, SEEK_CUR)
  689. || 4 != php_stream_read(fp, sig_buf, 4)) {
  690. efree(savebuf);
  691. php_stream_close(fp);
  692. if (error) {
  693. spprintf(error, 0, "phar \"%s\" openssl signature length could not be read", fname);
  694. }
  695. return FAILURE;
  696. }
  697. sig_ptr = sig_buf;
  698. PHAR_GET_32(sig_ptr, signature_len);
  699. sig = (char *) emalloc(signature_len);
  700. whence = signature_len + 4;
  701. whence = -whence;
  702. if (-1 == php_stream_seek(fp, whence, SEEK_CUR)
  703. || !(end_of_phar = php_stream_tell(fp))
  704. || signature_len != php_stream_read(fp, sig, signature_len)) {
  705. efree(savebuf);
  706. efree(sig);
  707. php_stream_close(fp);
  708. if (error) {
  709. spprintf(error, 0, "phar \"%s\" openssl signature could not be read", fname);
  710. }
  711. return FAILURE;
  712. }
  713. if (FAILURE == phar_verify_signature(fp, end_of_phar, PHAR_SIG_OPENSSL, sig, signature_len, fname, &signature, &sig_len, error)) {
  714. efree(savebuf);
  715. efree(sig);
  716. php_stream_close(fp);
  717. if (error) {
  718. char *save = *error;
  719. spprintf(error, 0, "phar \"%s\" openssl signature could not be verified: %s", fname, *error);
  720. efree(save);
  721. }
  722. return FAILURE;
  723. }
  724. efree(sig);
  725. }
  726. break;
  727. #if PHAR_HASH_OK
  728. case PHAR_SIG_SHA512: {
  729. unsigned char digest[64];
  730. php_stream_seek(fp, -(8 + 64), SEEK_END);
  731. read_len = php_stream_tell(fp);
  732. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  733. efree(savebuf);
  734. php_stream_close(fp);
  735. if (error) {
  736. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  737. }
  738. return FAILURE;
  739. }
  740. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA512, (char *)digest, 64, fname, &signature, &sig_len, error)) {
  741. efree(savebuf);
  742. php_stream_close(fp);
  743. if (error) {
  744. char *save = *error;
  745. spprintf(error, 0, "phar \"%s\" SHA512 signature could not be verified: %s", fname, *error);
  746. efree(save);
  747. }
  748. return FAILURE;
  749. }
  750. break;
  751. }
  752. case PHAR_SIG_SHA256: {
  753. unsigned char digest[32];
  754. php_stream_seek(fp, -(8 + 32), SEEK_END);
  755. read_len = php_stream_tell(fp);
  756. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  757. efree(savebuf);
  758. php_stream_close(fp);
  759. if (error) {
  760. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  761. }
  762. return FAILURE;
  763. }
  764. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA256, (char *)digest, 32, fname, &signature, &sig_len, error)) {
  765. efree(savebuf);
  766. php_stream_close(fp);
  767. if (error) {
  768. char *save = *error;
  769. spprintf(error, 0, "phar \"%s\" SHA256 signature could not be verified: %s", fname, *error);
  770. efree(save);
  771. }
  772. return FAILURE;
  773. }
  774. break;
  775. }
  776. #else
  777. case PHAR_SIG_SHA512:
  778. case PHAR_SIG_SHA256:
  779. efree(savebuf);
  780. php_stream_close(fp);
  781. if (error) {
  782. spprintf(error, 0, "phar \"%s\" has a unsupported signature", fname);
  783. }
  784. return FAILURE;
  785. #endif
  786. case PHAR_SIG_SHA1: {
  787. unsigned char digest[20];
  788. php_stream_seek(fp, -(8 + 20), SEEK_END);
  789. read_len = php_stream_tell(fp);
  790. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  791. efree(savebuf);
  792. php_stream_close(fp);
  793. if (error) {
  794. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  795. }
  796. return FAILURE;
  797. }
  798. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA1, (char *)digest, 20, fname, &signature, &sig_len, error)) {
  799. efree(savebuf);
  800. php_stream_close(fp);
  801. if (error) {
  802. char *save = *error;
  803. spprintf(error, 0, "phar \"%s\" SHA1 signature could not be verified: %s", fname, *error);
  804. efree(save);
  805. }
  806. return FAILURE;
  807. }
  808. break;
  809. }
  810. case PHAR_SIG_MD5: {
  811. unsigned char digest[16];
  812. php_stream_seek(fp, -(8 + 16), SEEK_END);
  813. read_len = php_stream_tell(fp);
  814. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  815. efree(savebuf);
  816. php_stream_close(fp);
  817. if (error) {
  818. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  819. }
  820. return FAILURE;
  821. }
  822. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_MD5, (char *)digest, 16, fname, &signature, &sig_len, error)) {
  823. efree(savebuf);
  824. php_stream_close(fp);
  825. if (error) {
  826. char *save = *error;
  827. spprintf(error, 0, "phar \"%s\" MD5 signature could not be verified: %s", fname, *error);
  828. efree(save);
  829. }
  830. return FAILURE;
  831. }
  832. break;
  833. }
  834. default:
  835. efree(savebuf);
  836. php_stream_close(fp);
  837. if (error) {
  838. spprintf(error, 0, "phar \"%s\" has a broken or unsupported signature", fname);
  839. }
  840. return FAILURE;
  841. }
  842. } else if (PHAR_G(require_hash)) {
  843. efree(savebuf);
  844. php_stream_close(fp);
  845. if (error) {
  846. spprintf(error, 0, "phar \"%s\" does not have a signature", fname);
  847. }
  848. return FAILURE;
  849. } else {
  850. sig_flags = 0;
  851. sig_len = 0;
  852. }
  853. /* extract alias */
  854. SAFE_PHAR_GET_32(buffer, endbuffer, tmp_len);
  855. if (buffer + tmp_len > endbuffer) {
  856. MAPPHAR_FAIL("internal corruption of phar \"%s\" (buffer overrun)");
  857. }
  858. if (manifest_len < MANIFEST_FIXED_LEN + tmp_len) {
  859. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)")
  860. }
  861. /* tmp_len = 0 says alias length is 0, which means the alias is not stored in the phar */
  862. if (tmp_len) {
  863. /* if the alias is stored we enforce it (implicit overrides explicit) */
  864. if (alias && alias_len && (alias_len != tmp_len || strncmp(alias, buffer, tmp_len)))
  865. {
  866. php_stream_close(fp);
  867. if (signature) {
  868. efree(signature);
  869. }
  870. if (error) {
  871. spprintf(error, 0, "cannot load phar \"%s\" with implicit alias \"%.*s\" under different alias \"%s\"", fname, tmp_len, buffer, alias);
  872. }
  873. efree(savebuf);
  874. return FAILURE;
  875. }
  876. alias_len = tmp_len;
  877. alias = buffer;
  878. buffer += tmp_len;
  879. register_alias = 1;
  880. } else if (!alias_len || !alias) {
  881. /* if we neither have an explicit nor an implicit alias, we use the filename */
  882. alias = NULL;
  883. alias_len = 0;
  884. register_alias = 0;
  885. } else if (alias_len) {
  886. register_alias = 1;
  887. temp_alias = 1;
  888. }
  889. /* we have 5 32-bit items plus 1 byte at least */
  890. if (manifest_count > ((manifest_len - MANIFEST_FIXED_LEN - tmp_len) / (5 * 4 + 1))) {
  891. /* prevent serious memory issues */
  892. MAPPHAR_FAIL("internal corruption of phar \"%s\" (too many manifest entries for size of manifest)")
  893. }
  894. mydata = pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
  895. mydata->is_persistent = PHAR_G(persist);
  896. /* check whether we have meta data, zero check works regardless of byte order */
  897. SAFE_PHAR_GET_32(buffer, endbuffer, len);
  898. if (mydata->is_persistent) {
  899. mydata->metadata_len = len;
  900. if (!len) {
  901. /* FIXME: not sure why this is needed but removing it breaks tests */
  902. SAFE_PHAR_GET_32(buffer, endbuffer, len);
  903. }
  904. }
  905. if(len > (size_t)(endbuffer - buffer)) {
  906. MAPPHAR_FAIL("internal corruption of phar \"%s\" (trying to read past buffer end)");
  907. }
  908. if (phar_parse_metadata(&buffer, &mydata->metadata, len) == FAILURE) {
  909. MAPPHAR_FAIL("unable to read phar metadata in .phar file \"%s\"");
  910. }
  911. buffer += len;
  912. /* set up our manifest */
  913. zend_hash_init(&mydata->manifest, manifest_count,
  914. zend_get_hash_value, destroy_phar_manifest_entry, (zend_bool)mydata->is_persistent);
  915. zend_hash_init(&mydata->mounted_dirs, 5,
  916. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  917. zend_hash_init(&mydata->virtual_dirs, manifest_count * 2,
  918. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  919. mydata->fname = pestrndup(fname, fname_len, mydata->is_persistent);
  920. #ifdef PHP_WIN32
  921. phar_unixify_path_separators(mydata->fname, fname_len);
  922. #endif
  923. mydata->fname_len = fname_len;
  924. offset = halt_offset + manifest_len + 4;
  925. memset(&entry, 0, sizeof(phar_entry_info));
  926. entry.phar = mydata;
  927. entry.fp_type = PHAR_FP;
  928. entry.is_persistent = mydata->is_persistent;
  929. for (manifest_index = 0; manifest_index < manifest_count; ++manifest_index) {
  930. if (buffer + 28 > endbuffer) {
  931. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)")
  932. }
  933. PHAR_GET_32(buffer, entry.filename_len);
  934. if (entry.filename_len == 0) {
  935. MAPPHAR_FAIL("zero-length filename encountered in phar \"%s\"");
  936. }
  937. if (entry.is_persistent) {
  938. entry.manifest_pos = manifest_index;
  939. }
  940. if (entry.filename_len > (size_t)(endbuffer - buffer - 24)) {
  941. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)");
  942. }
  943. if ((manifest_ver & PHAR_API_VER_MASK) >= PHAR_API_MIN_DIR && buffer[entry.filename_len - 1] == '/') {
  944. entry.is_dir = 1;
  945. } else {
  946. entry.is_dir = 0;
  947. }
  948. phar_add_virtual_dirs(mydata, buffer, entry.filename_len);
  949. entry.filename = pestrndup(buffer, entry.filename_len, entry.is_persistent);
  950. buffer += entry.filename_len;
  951. PHAR_GET_32(buffer, entry.uncompressed_filesize);
  952. PHAR_GET_32(buffer, entry.timestamp);
  953. if (offset == halt_offset + manifest_len + 4) {
  954. mydata->min_timestamp = entry.timestamp;
  955. mydata->max_timestamp = entry.timestamp;
  956. } else {
  957. if (mydata->min_timestamp > entry.timestamp) {
  958. mydata->min_timestamp = entry.timestamp;
  959. } else if (mydata->max_timestamp < entry.timestamp) {
  960. mydata->max_timestamp = entry.timestamp;
  961. }
  962. }
  963. PHAR_GET_32(buffer, entry.compressed_filesize);
  964. PHAR_GET_32(buffer, entry.crc32);
  965. PHAR_GET_32(buffer, entry.flags);
  966. if (entry.is_dir) {
  967. entry.filename_len--;
  968. entry.flags |= PHAR_ENT_PERM_DEF_DIR;
  969. }
  970. PHAR_GET_32(buffer, len);
  971. if (entry.is_persistent) {
  972. entry.metadata_len = len;
  973. } else {
  974. entry.metadata_len = 0;
  975. }
  976. if (len > (size_t)(endbuffer - buffer)) {
  977. pefree(entry.filename, entry.is_persistent);
  978. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)");
  979. }
  980. if (phar_parse_metadata(&buffer, &entry.metadata, len) == FAILURE) {
  981. pefree(entry.filename, entry.is_persistent);
  982. MAPPHAR_FAIL("unable to read file metadata in .phar file \"%s\"");
  983. }
  984. buffer += len;
  985. entry.offset = entry.offset_abs = offset;
  986. offset += entry.compressed_filesize;
  987. switch (entry.flags & PHAR_ENT_COMPRESSION_MASK) {
  988. case PHAR_ENT_COMPRESSED_GZ:
  989. if (!PHAR_G(has_zlib)) {
  990. if (Z_TYPE(entry.metadata) != IS_UNDEF) {
  991. if (entry.is_persistent) {
  992. free(Z_PTR(entry.metadata));
  993. } else {
  994. zval_ptr_dtor(&entry.metadata);
  995. }
  996. }
  997. pefree(entry.filename, entry.is_persistent);
  998. MAPPHAR_FAIL("zlib extension is required for gz compressed .phar file \"%s\"");
  999. }
  1000. break;
  1001. case PHAR_ENT_COMPRESSED_BZ2:
  1002. if (!PHAR_G(has_bz2)) {
  1003. if (Z_TYPE(entry.metadata) != IS_UNDEF) {
  1004. if (entry.is_persistent) {
  1005. free(Z_PTR(entry.metadata));
  1006. } else {
  1007. zval_ptr_dtor(&entry.metadata);
  1008. }
  1009. }
  1010. pefree(entry.filename, entry.is_persistent);
  1011. MAPPHAR_FAIL("bz2 extension is required for bzip2 compressed .phar file \"%s\"");
  1012. }
  1013. break;
  1014. default:
  1015. if (entry.uncompressed_filesize != entry.compressed_filesize) {
  1016. if (Z_TYPE(entry.metadata) != IS_UNDEF) {
  1017. if (entry.is_persistent) {
  1018. free(Z_PTR(entry.metadata));
  1019. } else {
  1020. zval_ptr_dtor(&entry.metadata);
  1021. }
  1022. }
  1023. pefree(entry.filename, entry.is_persistent);
  1024. MAPPHAR_FAIL("internal corruption of phar \"%s\" (compressed and uncompressed size does not match for uncompressed entry)");
  1025. }
  1026. break;
  1027. }
  1028. manifest_flags |= (entry.flags & PHAR_ENT_COMPRESSION_MASK);
  1029. /* if signature matched, no need to check CRC32 for each file */
  1030. entry.is_crc_checked = (manifest_flags & PHAR_HDR_SIGNATURE ? 1 : 0);
  1031. phar_set_inode(&entry);
  1032. if (mydata->is_persistent) {
  1033. str = zend_string_init_interned(entry.filename, entry.filename_len, 1);
  1034. } else {
  1035. str = zend_string_init(entry.filename, entry.filename_len, 0);
  1036. }
  1037. zend_hash_add_mem(&mydata->manifest, str, (void*)&entry, sizeof(phar_entry_info));
  1038. zend_string_release(str);
  1039. }
  1040. snprintf(mydata->version, sizeof(mydata->version), "%u.%u.%u", manifest_ver >> 12, (manifest_ver >> 8) & 0xF, (manifest_ver >> 4) & 0xF);
  1041. mydata->internal_file_start = halt_offset + manifest_len + 4;
  1042. mydata->halt_offset = halt_offset;
  1043. mydata->flags = manifest_flags;
  1044. endbuffer = strrchr(mydata->fname, '/');
  1045. if (endbuffer) {
  1046. mydata->ext = memchr(endbuffer, '.', (mydata->fname + fname_len) - endbuffer);
  1047. if (mydata->ext == endbuffer) {
  1048. mydata->ext = memchr(endbuffer + 1, '.', (mydata->fname + fname_len) - endbuffer - 1);
  1049. }
  1050. if (mydata->ext) {
  1051. mydata->ext_len = (mydata->fname + mydata->fname_len) - mydata->ext;
  1052. }
  1053. }
  1054. mydata->alias = alias ?
  1055. pestrndup(alias, alias_len, mydata->is_persistent) :
  1056. pestrndup(mydata->fname, fname_len, mydata->is_persistent);
  1057. mydata->alias_len = alias ? alias_len : fname_len;
  1058. mydata->sig_flags = sig_flags;
  1059. mydata->fp = fp;
  1060. mydata->sig_len = sig_len;
  1061. mydata->signature = signature;
  1062. phar_request_initialize();
  1063. if (register_alias) {
  1064. phar_archive_data *fd_ptr;
  1065. mydata->is_temporary_alias = temp_alias;
  1066. if (!phar_validate_alias(mydata->alias, mydata->alias_len)) {
  1067. signature = NULL;
  1068. fp = NULL;
  1069. MAPPHAR_FAIL("Cannot open archive \"%s\", invalid alias");
  1070. }
  1071. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
  1072. if (SUCCESS != phar_free_alias(fd_ptr, alias, alias_len)) {
  1073. signature = NULL;
  1074. fp = NULL;
  1075. MAPPHAR_FAIL("Cannot open archive \"%s\", alias is already in use by existing archive");
  1076. }
  1077. }
  1078. if (mydata->is_persistent) {
  1079. str = zend_string_init_interned(alias, alias_len, 1);
  1080. } else {
  1081. str = zend_string_init(alias, alias_len, 0);
  1082. }
  1083. zend_hash_add_ptr(&(PHAR_G(phar_alias_map)), str, mydata);
  1084. zend_string_release(str);
  1085. } else {
  1086. mydata->is_temporary_alias = 1;
  1087. }
  1088. if (mydata->is_persistent) {
  1089. str = zend_string_init_interned(mydata->fname, fname_len, 1);
  1090. } else {
  1091. str = zend_string_init(mydata->fname, fname_len, 0);
  1092. }
  1093. zend_hash_add_ptr(&(PHAR_G(phar_fname_map)), str, mydata);
  1094. zend_string_release(str);
  1095. efree(savebuf);
  1096. if (pphar) {
  1097. *pphar = mydata;
  1098. }
  1099. return SUCCESS;
  1100. }
  1101. /* }}} */
  1102. /**
  1103. * Create or open a phar for writing
  1104. */
  1105. int phar_open_or_create_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, zend_bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
  1106. {
  1107. const char *ext_str, *z;
  1108. char *my_error;
  1109. size_t ext_len;
  1110. phar_archive_data **test, *unused = NULL;
  1111. test = &unused;
  1112. if (error) {
  1113. *error = NULL;
  1114. }
  1115. /* first try to open an existing file */
  1116. if (phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 0, 1) == SUCCESS) {
  1117. goto check_file;
  1118. }
  1119. /* next try to create a new file */
  1120. if (FAILURE == phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 1, 1)) {
  1121. if (error) {
  1122. if (ext_len == -2) {
  1123. spprintf(error, 0, "Cannot create a phar archive from a URL like \"%s\". Phar objects can only be created from local files", fname);
  1124. } else {
  1125. spprintf(error, 0, "Cannot create phar '%s', file extension (or combination) not recognised or the directory does not exist", fname);
  1126. }
  1127. }
  1128. return FAILURE;
  1129. }
  1130. check_file:
  1131. if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, test, &my_error) == SUCCESS) {
  1132. if (pphar) {
  1133. *pphar = *test;
  1134. }
  1135. if ((*test)->is_data && !(*test)->is_tar && !(*test)->is_zip) {
  1136. if (error) {
  1137. spprintf(error, 0, "Cannot open '%s' as a PharData object. Use Phar::__construct() for executable archives", fname);
  1138. }
  1139. return FAILURE;
  1140. }
  1141. if (PHAR_G(readonly) && !(*test)->is_data && ((*test)->is_tar || (*test)->is_zip)) {
  1142. phar_entry_info *stub;
  1143. if (NULL == (stub = zend_hash_str_find_ptr(&((*test)->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) {
  1144. spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname);
  1145. return FAILURE;
  1146. }
  1147. }
  1148. if (!PHAR_G(readonly) || (*test)->is_data) {
  1149. (*test)->is_writeable = 1;
  1150. }
  1151. return SUCCESS;
  1152. } else if (my_error) {
  1153. if (error) {
  1154. *error = my_error;
  1155. } else {
  1156. efree(my_error);
  1157. }
  1158. return FAILURE;
  1159. }
  1160. if (ext_len > 3 && (z = memchr(ext_str, 'z', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ip", 2)) {
  1161. /* assume zip-based phar */
  1162. return phar_open_or_create_zip(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
  1163. }
  1164. if (ext_len > 3 && (z = memchr(ext_str, 't', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ar", 2)) {
  1165. /* assume tar-based phar */
  1166. return phar_open_or_create_tar(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
  1167. }
  1168. return phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
  1169. }
  1170. /* }}} */
  1171. int phar_create_or_parse_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, zend_bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
  1172. {
  1173. phar_archive_data *mydata;
  1174. php_stream *fp;
  1175. zend_string *actual = NULL;
  1176. char *p;
  1177. if (!pphar) {
  1178. pphar = &mydata;
  1179. }
  1180. if (php_check_open_basedir(fname)) {
  1181. return FAILURE;
  1182. }
  1183. /* first open readonly so it won't be created if not present */
  1184. fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|0, &actual);
  1185. if (actual) {
  1186. fname = ZSTR_VAL(actual);
  1187. fname_len = ZSTR_LEN(actual);
  1188. }
  1189. if (fp) {
  1190. if (phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error) == SUCCESS) {
  1191. if ((*pphar)->is_data || !PHAR_G(readonly)) {
  1192. (*pphar)->is_writeable = 1;
  1193. }
  1194. if (actual) {
  1195. zend_string_release_ex(actual, 0);
  1196. }
  1197. return SUCCESS;
  1198. } else {
  1199. /* file exists, but is either corrupt or not a phar archive */
  1200. if (actual) {
  1201. zend_string_release_ex(actual, 0);
  1202. }
  1203. return FAILURE;
  1204. }
  1205. }
  1206. if (actual) {
  1207. zend_string_release_ex(actual, 0);
  1208. }
  1209. if (PHAR_G(readonly) && !is_data) {
  1210. if (options & REPORT_ERRORS) {
  1211. if (error) {
  1212. spprintf(error, 0, "creating archive \"%s\" disabled by the php.ini setting phar.readonly", fname);
  1213. }
  1214. }
  1215. return FAILURE;
  1216. }
  1217. /* set up our manifest */
  1218. mydata = ecalloc(1, sizeof(phar_archive_data));
  1219. mydata->fname = expand_filepath(fname, NULL);
  1220. if (mydata->fname == NULL) {
  1221. efree(mydata);
  1222. return FAILURE;
  1223. }
  1224. fname_len = strlen(mydata->fname);
  1225. #ifdef PHP_WIN32
  1226. phar_unixify_path_separators(mydata->fname, fname_len);
  1227. #endif
  1228. p = strrchr(mydata->fname, '/');
  1229. if (p) {
  1230. mydata->ext = memchr(p, '.', (mydata->fname + fname_len) - p);
  1231. if (mydata->ext == p) {
  1232. mydata->ext = memchr(p + 1, '.', (mydata->fname + fname_len) - p - 1);
  1233. }
  1234. if (mydata->ext) {
  1235. mydata->ext_len = (mydata->fname + fname_len) - mydata->ext;
  1236. }
  1237. }
  1238. if (pphar) {
  1239. *pphar = mydata;
  1240. }
  1241. zend_hash_init(&mydata->manifest, sizeof(phar_entry_info),
  1242. zend_get_hash_value, destroy_phar_manifest_entry, 0);
  1243. zend_hash_init(&mydata->mounted_dirs, sizeof(char *),
  1244. zend_get_hash_value, NULL, 0);
  1245. zend_hash_init(&mydata->virtual_dirs, sizeof(char *),
  1246. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  1247. mydata->fname_len = fname_len;
  1248. snprintf(mydata->version, sizeof(mydata->version), "%s", PHP_PHAR_API_VERSION);
  1249. mydata->is_temporary_alias = alias ? 0 : 1;
  1250. mydata->internal_file_start = -1;
  1251. mydata->fp = NULL;
  1252. mydata->is_writeable = 1;
  1253. mydata->is_brandnew = 1;
  1254. phar_request_initialize();
  1255. zend_hash_str_add_ptr(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len, mydata);
  1256. if (is_data) {
  1257. alias = NULL;
  1258. alias_len = 0;
  1259. mydata->is_data = 1;
  1260. /* assume tar format, PharData can specify other */
  1261. mydata->is_tar = 1;
  1262. } else {
  1263. phar_archive_data *fd_ptr;
  1264. if (alias && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
  1265. if (SUCCESS != phar_free_alias(fd_ptr, alias, alias_len)) {
  1266. if (error) {
  1267. spprintf(error, 4096, "phar error: phar \"%s\" cannot set alias \"%s\", already in use by another phar archive", mydata->fname, alias);
  1268. }
  1269. zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
  1270. if (pphar) {
  1271. *pphar = NULL;
  1272. }
  1273. return FAILURE;
  1274. }
  1275. }
  1276. mydata->alias = alias ? estrndup(alias, alias_len) : estrndup(mydata->fname, fname_len);
  1277. mydata->alias_len = alias ? alias_len : fname_len;
  1278. }
  1279. if (alias_len && alias) {
  1280. if (NULL == zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, mydata)) {
  1281. if (options & REPORT_ERRORS) {
  1282. if (error) {
  1283. spprintf(error, 0, "archive \"%s\" cannot be associated with alias \"%s\", already in use", fname, alias);
  1284. }
  1285. }
  1286. zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
  1287. if (pphar) {
  1288. *pphar = NULL;
  1289. }
  1290. return FAILURE;
  1291. }
  1292. }
  1293. return SUCCESS;
  1294. }
  1295. /* }}}*/
  1296. /**
  1297. * Return an already opened filename.
  1298. *
  1299. * Or scan a phar file for the required __HALT_COMPILER(); ?> token and verify
  1300. * that the manifest is proper, then pass it to phar_parse_pharfile(). SUCCESS
  1301. * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
  1302. */
  1303. int phar_open_from_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
  1304. {
  1305. php_stream *fp;
  1306. zend_string *actual;
  1307. int ret, is_data = 0;
  1308. if (error) {
  1309. *error = NULL;
  1310. }
  1311. if (!strstr(fname, ".phar")) {
  1312. is_data = 1;
  1313. }
  1314. if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, pphar, error) == SUCCESS) {
  1315. return SUCCESS;
  1316. } else if (error && *error) {
  1317. return FAILURE;
  1318. }
  1319. if (php_check_open_basedir(fname)) {
  1320. return FAILURE;
  1321. }
  1322. fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);
  1323. if (!fp) {
  1324. if (options & REPORT_ERRORS) {
  1325. if (error) {
  1326. spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
  1327. }
  1328. }
  1329. if (actual) {
  1330. zend_string_release_ex(actual, 0);
  1331. }
  1332. return FAILURE;
  1333. }
  1334. if (actual) {
  1335. fname = ZSTR_VAL(actual);
  1336. fname_len = ZSTR_LEN(actual);
  1337. }
  1338. ret = phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error);
  1339. if (actual) {
  1340. zend_string_release_ex(actual, 0);
  1341. }
  1342. return ret;
  1343. }
  1344. /* }}}*/
  1345. static inline char *phar_strnstr(const char *buf, int buf_len, const char *search, int search_len) /* {{{ */
  1346. {
  1347. const char *c;
  1348. ptrdiff_t so_far = 0;
  1349. if (buf_len < search_len) {
  1350. return NULL;
  1351. }
  1352. c = buf - 1;
  1353. do {
  1354. if (!(c = memchr(c + 1, search[0], buf_len - search_len - so_far))) {
  1355. return (char *) NULL;
  1356. }
  1357. so_far = c - buf;
  1358. if (so_far >= (buf_len - search_len)) {
  1359. return (char *) NULL;
  1360. }
  1361. if (!memcmp(c, search, search_len)) {
  1362. return (char *) c;
  1363. }
  1364. } while (1);
  1365. }
  1366. /* }}} */
  1367. /**
  1368. * Scan an open fp for the required __HALT_COMPILER(); ?> token and verify
  1369. * that the manifest is proper, then pass it to phar_parse_pharfile(). SUCCESS
  1370. * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
  1371. */
  1372. static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char *alias, size_t alias_len, uint32_t options, phar_archive_data** pphar, int is_data, char **error) /* {{{ */
  1373. {
  1374. const char token[] = "__HALT_COMPILER();";
  1375. const char zip_magic[] = "PK\x03\x04";
  1376. const char gz_magic[] = "\x1f\x8b\x08";
  1377. const char bz_magic[] = "BZh";
  1378. char *pos, test = '\0';
  1379. const int window_size = 1024;
  1380. char buffer[1024 + sizeof(token)]; /* a 1024 byte window + the size of the halt_compiler token (moving window) */
  1381. const zend_long readsize = sizeof(buffer) - sizeof(token);
  1382. const zend_long tokenlen = sizeof(token) - 1;
  1383. zend_long halt_offset;
  1384. size_t got;
  1385. uint32_t compression = PHAR_FILE_COMPRESSED_NONE;
  1386. if (error) {
  1387. *error = NULL;
  1388. }
  1389. if (-1 == php_stream_rewind(fp)) {
  1390. MAPPHAR_ALLOC_FAIL("cannot rewind phar \"%s\"")
  1391. }
  1392. buffer[sizeof(buffer)-1] = '\0';
  1393. memset(buffer, 32, sizeof(token));
  1394. halt_offset = 0;
  1395. /* Maybe it's better to compile the file instead of just searching, */
  1396. /* but we only want the offset. So we want a .re scanner to find it. */
  1397. while(!php_stream_eof(fp)) {
  1398. if ((got = php_stream_read(fp, buffer+tokenlen, readsize)) < (size_t) tokenlen) {
  1399. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
  1400. }
  1401. if (!test) {
  1402. test = '\1';
  1403. pos = buffer+tokenlen;
  1404. if (!memcmp(pos, gz_magic, 3)) {
  1405. char err = 0;
  1406. php_stream_filter *filter;
  1407. php_stream *temp;
  1408. /* to properly decompress, we have to tell zlib to look for a zlib or gzip header */
  1409. zval filterparams;
  1410. if (!PHAR_G(has_zlib)) {
  1411. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file, enable zlib extension in php.ini")
  1412. }
  1413. array_init(&filterparams);
  1414. /* this is defined in zlib's zconf.h */
  1415. #ifndef MAX_WBITS
  1416. #define MAX_WBITS 15
  1417. #endif
  1418. add_assoc_long_ex(&filterparams, "window", sizeof("window") - 1, MAX_WBITS + 32);
  1419. /* entire file is gzip-compressed, uncompress to temporary file */
  1420. if (!(temp = php_stream_fopen_tmpfile())) {
  1421. MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar archive \"%s\"")
  1422. }
  1423. php_stream_rewind(fp);
  1424. filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp));
  1425. if (!filter) {
  1426. err = 1;
  1427. add_assoc_long_ex(&filterparams, "window", sizeof("window") - 1, MAX_WBITS);
  1428. filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp));
  1429. zend_array_destroy(Z_ARR(filterparams));
  1430. if (!filter) {
  1431. php_stream_close(temp);
  1432. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
  1433. }
  1434. } else {
  1435. zend_array_destroy(Z_ARR(filterparams));
  1436. }
  1437. php_stream_filter_append(&temp->writefilters, filter);
  1438. if (SUCCESS != php_stream_copy_to_stream_ex(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
  1439. if (err) {
  1440. php_stream_close(temp);
  1441. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
  1442. }
  1443. php_stream_close(temp);
  1444. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file")
  1445. }
  1446. php_stream_filter_flush(filter, 1);
  1447. php_stream_filter_remove(filter, 1);
  1448. php_stream_close(fp);
  1449. fp = temp;
  1450. php_stream_rewind(fp);
  1451. compression = PHAR_FILE_COMPRESSED_GZ;
  1452. /* now, start over */
  1453. test = '\0';
  1454. continue;
  1455. } else if (!memcmp(pos, bz_magic, 3)) {
  1456. php_stream_filter *filter;
  1457. php_stream *temp;
  1458. if (!PHAR_G(has_bz2)) {
  1459. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file, enable bz2 extension in php.ini")
  1460. }
  1461. /* entire file is bzip-compressed, uncompress to temporary file */
  1462. if (!(temp = php_stream_fopen_tmpfile())) {
  1463. MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of bzipped phar archive \"%s\"")
  1464. }
  1465. php_stream_rewind(fp);
  1466. filter = php_stream_filter_create("bzip2.decompress", NULL, php_stream_is_persistent(fp));
  1467. if (!filter) {
  1468. php_stream_close(temp);
  1469. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\", filter creation failed")
  1470. }
  1471. php_stream_filter_append(&temp->writefilters, filter);
  1472. if (SUCCESS != php_stream_copy_to_stream_ex(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
  1473. php_stream_close(temp);
  1474. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file")
  1475. }
  1476. php_stream_filter_flush(filter, 1);
  1477. php_stream_filter_remove(filter, 1);
  1478. php_stream_close(fp);
  1479. fp = temp;
  1480. php_stream_rewind(fp);
  1481. compression = PHAR_FILE_COMPRESSED_BZ2;
  1482. /* now, start over */
  1483. test = '\0';
  1484. continue;
  1485. }
  1486. if (!memcmp(pos, zip_magic, 4)) {
  1487. php_stream_seek(fp, 0, SEEK_END);
  1488. return phar_parse_zipfile(fp, fname, fname_len, alias, alias_len, pphar, error);
  1489. }
  1490. if (got > 512) {
  1491. if (phar_is_tar(pos, fname)) {
  1492. php_stream_rewind(fp);
  1493. return phar_parse_tarfile(fp, fname, fname_len, alias, alias_len, pphar, is_data, compression, error);
  1494. }
  1495. }
  1496. }
  1497. if (got > 0 && (pos = phar_strnstr(buffer, got + sizeof(token), token, sizeof(token)-1)) != NULL) {
  1498. halt_offset += (pos - buffer); /* no -tokenlen+tokenlen here */
  1499. return phar_parse_pharfile(fp, fname, fname_len, alias, alias_len, halt_offset, pphar, compression, error);
  1500. }
  1501. halt_offset += got;
  1502. memmove(buffer, buffer + window_size, tokenlen); /* move the memory buffer by the size of the window */
  1503. }
  1504. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (__HALT_COMPILER(); not found)")
  1505. }
  1506. /* }}} */
  1507. /*
  1508. * given the location of the file extension and the start of the file path,
  1509. * determine the end of the portion of the path (i.e. /path/to/file.ext/blah
  1510. * grabs "/path/to/file.ext" as does the straight /path/to/file.ext),
  1511. * stat it to determine if it exists.
  1512. * if so, check to see if it is a directory and fail if so
  1513. * if not, check to see if its dirname() exists (i.e. "/path/to") and is a directory
  1514. * succeed if we are creating the file, otherwise fail.
  1515. */
  1516. static int phar_analyze_path(const char *fname, const char *ext, size_t ext_len, int for_create) /* {{{ */
  1517. {
  1518. php_stream_statbuf ssb;
  1519. char *realpath;
  1520. char *filename = estrndup(fname, (ext - fname) + ext_len);
  1521. if ((realpath = expand_filepath(filename, NULL))) {
  1522. #ifdef PHP_WIN32
  1523. phar_unixify_path_separators(realpath, strlen(realpath));
  1524. #endif
  1525. if (zend_hash_str_exists(&(PHAR_G(phar_fname_map)), realpath, strlen(realpath))) {
  1526. efree(realpath);
  1527. efree(filename);
  1528. return SUCCESS;
  1529. }
  1530. if (PHAR_G(manifest_cached) && zend_hash_str_exists(&cached_phars, realpath, strlen(realpath))) {
  1531. efree(realpath);
  1532. efree(filename);
  1533. return SUCCESS;
  1534. }
  1535. efree(realpath);
  1536. }
  1537. if (SUCCESS == php_stream_stat_path((char *) filename, &ssb)) {
  1538. efree(filename);
  1539. if (ssb.sb.st_mode & S_IFDIR) {
  1540. return FAILURE;
  1541. }
  1542. if (for_create == 1) {
  1543. return FAILURE;
  1544. }
  1545. return SUCCESS;
  1546. } else {
  1547. char *slash;
  1548. if (!for_create) {
  1549. efree(filename);
  1550. return FAILURE;
  1551. }
  1552. slash = (char *) strrchr(filename, '/');
  1553. if (slash) {
  1554. *slash = '\0';
  1555. }
  1556. if (SUCCESS != php_stream_stat_path((char *) filename, &ssb)) {
  1557. if (!slash) {
  1558. if (!(realpath = expand_filepath(filename, NULL))) {
  1559. efree(filename);
  1560. return FAILURE;
  1561. }
  1562. #ifdef PHP_WIN32
  1563. phar_unixify_path_separators(realpath, strlen(realpath));
  1564. #endif
  1565. slash = strstr(realpath, filename);
  1566. if (slash) {
  1567. slash += ((ext - fname) + ext_len);
  1568. *slash = '\0';
  1569. }
  1570. slash = strrchr(realpath, '/');
  1571. if (slash) {
  1572. *slash = '\0';
  1573. } else {
  1574. efree(realpath);
  1575. efree(filename);
  1576. return FAILURE;
  1577. }
  1578. if (SUCCESS != php_stream_stat_path(realpath, &ssb)) {
  1579. efree(realpath);
  1580. efree(filename);
  1581. return FAILURE;
  1582. }
  1583. efree(realpath);
  1584. if (ssb.sb.st_mode & S_IFDIR) {
  1585. efree(filename);
  1586. return SUCCESS;
  1587. }
  1588. }
  1589. efree(filename);
  1590. return FAILURE;
  1591. }
  1592. efree(filename);
  1593. if (ssb.sb.st_mode & S_IFDIR) {
  1594. return SUCCESS;
  1595. }
  1596. return FAILURE;
  1597. }
  1598. }
  1599. /* }}} */
  1600. /* check for ".phar" in extension */
  1601. static int phar_check_str(const char *fname, const char *ext_str, size_t ext_len, int executable, int for_create) /* {{{ */
  1602. {
  1603. const char *pos;
  1604. if (ext_len >= 50) {
  1605. return FAILURE;
  1606. }
  1607. if (executable == 1) {
  1608. /* executable phars must contain ".phar" as a valid extension (phar://.pharmy/oops is invalid) */
  1609. /* (phar://hi/there/.phar/oops is also invalid) */
  1610. pos = strstr(ext_str, ".phar");
  1611. if (!pos
  1612. || (pos != ext_str && (*(pos - 1) == '/'))
  1613. || (ext_len - (pos - ext_str)) < 5
  1614. || !(pos += 5)
  1615. || !(*pos == '\0' || *pos == '/' || *pos == '.')) {
  1616. return FAILURE;
  1617. }
  1618. return phar_analyze_path(fname, ext_str, ext_len, for_create);
  1619. }
  1620. /* data phars need only contain a single non-"." to be valid */
  1621. if (!executable) {
  1622. pos = strstr(ext_str, ".phar");
  1623. if (!(pos && (*(pos - 1) != '/')
  1624. && (pos += 5) && (*pos == '\0' || *pos == '/' || *pos == '.')) && *(ext_str + 1) != '.' && *(ext_str + 1) != '/' && *(ext_str + 1) != '\0') {
  1625. return phar_analyze_path(fname, ext_str, ext_len, for_create);
  1626. }
  1627. } else {
  1628. if (*(ext_str + 1) != '.' && *(ext_str + 1) != '/' && *(ext_str + 1) != '\0') {
  1629. return phar_analyze_path(fname, ext_str, ext_len, for_create);
  1630. }
  1631. }
  1632. return FAILURE;
  1633. }
  1634. /* }}} */
  1635. /*
  1636. * if executable is 1, only returns SUCCESS if the extension is one of the tar/zip .phar extensions
  1637. * if executable is 0, it returns SUCCESS only if the filename does *not* contain ".phar" anywhere, and treats
  1638. * the first extension as the filename extension
  1639. *
  1640. * if an extension is found, it sets ext_str to the location of the file extension in filename,
  1641. * and ext_len to the length of the extension.
  1642. * for urls like "phar://alias/oops" it instead sets ext_len to -1 and returns FAILURE, which tells
  1643. * the calling function to use "alias" as the phar alias
  1644. *
  1645. * the last parameter should be set to tell the thing to assume that filename is the full path, and only to check the
  1646. * extension rules, not to iterate.
  1647. */
  1648. int phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, int is_complete) /* {{{ */
  1649. {
  1650. const char *pos, *slash;
  1651. *ext_str = NULL;
  1652. *ext_len = 0;
  1653. if (!filename_len || filename_len == 1) {
  1654. return FAILURE;
  1655. }
  1656. phar_request_initialize();
  1657. /* first check for alias in first segment */
  1658. pos = memchr(filename, '/', filename_len);
  1659. if (pos && pos != filename) {
  1660. /* check for url like http:// or phar:// */
  1661. if (*(pos - 1) == ':' && (size_t)(pos - filename) < filename_len - 1 && *(pos + 1) == '/') {
  1662. *ext_len = -2;
  1663. *ext_str = NULL;
  1664. return FAILURE;
  1665. }
  1666. if (zend_hash_str_exists(&(PHAR_G(phar_alias_map)), (char *) filename, pos - filename)) {
  1667. *ext_str = pos;
  1668. *ext_len = -1;
  1669. return FAILURE;
  1670. }
  1671. if (PHAR_G(manifest_cached) && zend_hash_str_exists(&cached_alias, (char *) filename, pos - filename)) {
  1672. *ext_str = pos;
  1673. *ext_len = -1;
  1674. return FAILURE;
  1675. }
  1676. }
  1677. if (zend_hash_num_elements(&(PHAR_G(phar_fname_map))) || PHAR_G(manifest_cached)) {
  1678. phar_archive_data *pphar;
  1679. if (is_complete) {
  1680. if (NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), (char *) filename, filename_len))) {
  1681. *ext_str = filename + (filename_len - pphar->ext_len);
  1682. woohoo:
  1683. *ext_len = pphar->ext_len;
  1684. if (executable == 2) {
  1685. return SUCCESS;
  1686. }
  1687. if (executable == 1 && !pphar->is_data) {
  1688. return SUCCESS;
  1689. }
  1690. if (!executable && pphar->is_data) {
  1691. return SUCCESS;
  1692. }
  1693. return FAILURE;
  1694. }
  1695. if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, (char *) filename, filename_len))) {
  1696. *ext_str = filename + (filename_len - pphar->ext_len);
  1697. goto woohoo;
  1698. }
  1699. } else {
  1700. zend_string *str_key;
  1701. zend_ulong unused;
  1702. for (zend_hash_internal_pointer_reset(&(PHAR_G(phar_fname_map)));
  1703. HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(&(PHAR_G(phar_fname_map)), &str_key, &unused);
  1704. zend_hash_move_forward(&(PHAR_G(phar_fname_map)))
  1705. ) {
  1706. if (ZSTR_LEN(str_key) > (uint32_t) filename_len) {
  1707. continue;
  1708. }
  1709. if (!memcmp(filename, ZSTR_VAL(str_key), ZSTR_LEN(str_key)) && ((uint32_t)filename_len == ZSTR_LEN(str_key)
  1710. || filename[ZSTR_LEN(str_key)] == '/' || filename[ZSTR_LEN(str_key)] == '\0')) {
  1711. if (NULL == (pphar = zend_hash_get_current_data_ptr(&(PHAR_G(phar_fname_map))))) {
  1712. break;
  1713. }
  1714. *ext_str = filename + (ZSTR_LEN(str_key) - pphar->ext_len);
  1715. goto woohoo;
  1716. }
  1717. }
  1718. if (PHAR_G(manifest_cached)) {
  1719. for (zend_hash_internal_pointer_reset(&cached_phars);
  1720. HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(&cached_phars, &str_key, &unused);
  1721. zend_hash_move_forward(&cached_phars)
  1722. ) {
  1723. if (ZSTR_LEN(str_key) > (uint32_t) filename_len) {
  1724. continue;
  1725. }
  1726. if (!memcmp(filename, ZSTR_VAL(str_key), ZSTR_LEN(str_key)) && ((uint32_t)filename_len == ZSTR_LEN(str_key)
  1727. || filename[ZSTR_LEN(str_key)] == '/' || filename[ZSTR_LEN(str_key)] == '\0')) {
  1728. if (NULL == (pphar = zend_hash_get_current_data_ptr(&cached_phars))) {
  1729. break;
  1730. }
  1731. *ext_str = filename + (ZSTR_LEN(str_key) - pphar->ext_len);
  1732. goto woohoo;
  1733. }
  1734. }
  1735. }
  1736. }
  1737. }
  1738. pos = memchr(filename + 1, '.', filename_len);
  1739. next_extension:
  1740. if (!pos) {
  1741. return FAILURE;
  1742. }
  1743. while (pos != filename && (*(pos - 1) == '/' || *(pos - 1) == '\0')) {
  1744. pos = memchr(pos + 1, '.', filename_len - (pos - filename) - 1);
  1745. if (!pos) {
  1746. return FAILURE;
  1747. }
  1748. }
  1749. slash = memchr(pos, '/', filename_len - (pos - filename));
  1750. if (!slash) {
  1751. /* this is a url like "phar://blah.phar" with no directory */
  1752. *ext_str = pos;
  1753. *ext_len = strlen(pos);
  1754. /* file extension must contain "phar" */
  1755. switch (phar_check_str(filename, *ext_str, *ext_len, executable, for_create)) {
  1756. case SUCCESS:
  1757. return SUCCESS;
  1758. case FAILURE:
  1759. /* we are at the end of the string, so we fail */
  1760. return FAILURE;
  1761. }
  1762. }
  1763. /* we've found an extension that ends at a directory separator */
  1764. *ext_str = pos;
  1765. *ext_len = slash - pos;
  1766. switch (phar_check_str(filename, *ext_str, *ext_len, executable, for_create)) {
  1767. case SUCCESS:
  1768. return SUCCESS;
  1769. case FAILURE:
  1770. /* look for more extensions */
  1771. pos = strchr(pos + 1, '.');
  1772. if (pos) {
  1773. *ext_str = NULL;
  1774. *ext_len = 0;
  1775. }
  1776. goto next_extension;
  1777. }
  1778. return FAILURE;
  1779. }
  1780. /* }}} */
  1781. static int php_check_dots(const char *element, size_t n) /* {{{ */
  1782. {
  1783. for(n-- ; n != SIZE_MAX; --n) {
  1784. if (element[n] != '.') {
  1785. return 1;
  1786. }
  1787. }
  1788. return 0;
  1789. }
  1790. /* }}} */
  1791. #define IS_DIRECTORY_UP(element, len) \
  1792. (len >= 2 && !php_check_dots(element, len))
  1793. #define IS_DIRECTORY_CURRENT(element, len) \
  1794. (len == 1 && element[0] == '.')
  1795. #define IS_BACKSLASH(c) ((c) == '/')
  1796. /**
  1797. * Remove .. and . references within a phar filename
  1798. */
  1799. char *phar_fix_filepath(char *path, size_t *new_len, int use_cwd) /* {{{ */
  1800. {
  1801. char *newpath;
  1802. size_t newpath_len;
  1803. char *ptr;
  1804. char *tok;
  1805. size_t ptr_length, path_length = *new_len;
  1806. if (PHAR_G(cwd_len) && use_cwd && path_length > 2 && path[0] == '.' && path[1] == '/') {
  1807. newpath_len = PHAR_G(cwd_len);
  1808. newpath = emalloc(strlen(path) + newpath_len + 1);
  1809. memcpy(newpath, PHAR_G(cwd), newpath_len);
  1810. } else {
  1811. newpath = emalloc(strlen(path) + 2);
  1812. newpath[0] = '/';
  1813. newpath_len = 1;
  1814. }
  1815. ptr = path;
  1816. if (*ptr == '/') {
  1817. ++ptr;
  1818. }
  1819. tok = ptr;
  1820. do {
  1821. ptr = memchr(ptr, '/', path_length - (ptr - path));
  1822. } while (ptr && ptr - tok == 0 && *ptr == '/' && ++ptr && ++tok);
  1823. if (!ptr && (path_length - (tok - path))) {
  1824. switch (path_length - (tok - path)) {
  1825. case 1:
  1826. if (*tok == '.') {
  1827. efree(path);
  1828. *new_len = 1;
  1829. efree(newpath);
  1830. return estrndup("/", 1);
  1831. }
  1832. break;
  1833. case 2:
  1834. if (tok[0] == '.' && tok[1] == '.') {
  1835. efree(path);
  1836. *new_len = 1;
  1837. efree(newpath);
  1838. return estrndup("/", 1);
  1839. }
  1840. }
  1841. efree(newpath);
  1842. return path;
  1843. }
  1844. while (ptr) {
  1845. ptr_length = ptr - tok;
  1846. last_time:
  1847. if (IS_DIRECTORY_UP(tok, ptr_length)) {
  1848. #define PREVIOUS newpath[newpath_len - 1]
  1849. while (newpath_len > 1 && !IS_BACKSLASH(PREVIOUS)) {
  1850. newpath_len--;
  1851. }
  1852. if (newpath[0] != '/') {
  1853. newpath[newpath_len] = '\0';
  1854. } else if (newpath_len > 1) {
  1855. --newpath_len;
  1856. }
  1857. } else if (!IS_DIRECTORY_CURRENT(tok, ptr_length)) {
  1858. if (newpath_len > 1) {
  1859. newpath[newpath_len++] = '/';
  1860. memcpy(newpath + newpath_len, tok, ptr_length+1);
  1861. } else {
  1862. memcpy(newpath + newpath_len, tok, ptr_length+1);
  1863. }
  1864. newpath_len += ptr_length;
  1865. }
  1866. if (ptr == path + path_length) {
  1867. break;
  1868. }
  1869. tok = ++ptr;
  1870. do {
  1871. ptr = memchr(ptr, '/', path_length - (ptr - path));
  1872. } while (ptr && ptr - tok == 0 && *ptr == '/' && ++ptr && ++tok);
  1873. if (!ptr && (path_length - (tok - path))) {
  1874. ptr_length = path_length - (tok - path);
  1875. ptr = path + path_length;
  1876. goto last_time;
  1877. }
  1878. }
  1879. efree(path);
  1880. *new_len = newpath_len;
  1881. newpath[newpath_len] = '\0';
  1882. return erealloc(newpath, newpath_len + 1);
  1883. }
  1884. /* }}} */
  1885. /**
  1886. * Process a phar stream name, ensuring we can handle any of:
  1887. *
  1888. * - whatever.phar
  1889. * - whatever.phar.gz
  1890. * - whatever.phar.bz2
  1891. * - whatever.phar.php
  1892. *
  1893. * Optionally the name might start with 'phar://'
  1894. *
  1895. * This is used by phar_parse_url()
  1896. */
  1897. int phar_split_fname(const char *filename, size_t filename_len, char **arch, size_t *arch_len, char **entry, size_t *entry_len, int executable, int for_create) /* {{{ */
  1898. {
  1899. const char *ext_str;
  1900. #ifdef PHP_WIN32
  1901. char *save;
  1902. #endif
  1903. size_t ext_len;
  1904. if (CHECK_NULL_PATH(filename, filename_len)) {
  1905. return FAILURE;
  1906. }
  1907. if (!strncasecmp(filename, "phar://", 7)) {
  1908. filename += 7;
  1909. filename_len -= 7;
  1910. }
  1911. ext_len = 0;
  1912. #ifdef PHP_WIN32
  1913. save = (char *)filename;
  1914. filename = estrndup(filename, filename_len);
  1915. phar_unixify_path_separators((char *)filename, filename_len);
  1916. #endif
  1917. if (phar_detect_phar_fname_ext(filename, filename_len, &ext_str, &ext_len, executable, for_create, 0) == FAILURE) {
  1918. if (ext_len != -1) {
  1919. if (!ext_str) {
  1920. /* no / detected, restore arch for error message */
  1921. #ifdef PHP_WIN32
  1922. *arch = save;
  1923. #else
  1924. *arch = (char*)filename;
  1925. #endif
  1926. }
  1927. #ifdef PHP_WIN32
  1928. efree((char *)filename);
  1929. #endif
  1930. return FAILURE;
  1931. }
  1932. ext_len = 0;
  1933. /* no extension detected - instead we are dealing with an alias */
  1934. }
  1935. *arch_len = ext_str - filename + ext_len;
  1936. *arch = estrndup(filename, *arch_len);
  1937. if (ext_str[ext_len]) {
  1938. *entry_len = filename_len - *arch_len;
  1939. *entry = estrndup(ext_str+ext_len, *entry_len);
  1940. #ifdef PHP_WIN32
  1941. phar_unixify_path_separators(*entry, *entry_len);
  1942. #endif
  1943. *entry = phar_fix_filepath(*entry, entry_len, 0);
  1944. } else {
  1945. *entry_len = 1;
  1946. *entry = estrndup("/", 1);
  1947. }
  1948. #ifdef PHP_WIN32
  1949. efree((char *)filename);
  1950. #endif
  1951. return SUCCESS;
  1952. }
  1953. /* }}} */
  1954. /**
  1955. * Invoked when a user calls Phar::mapPhar() from within an executing .phar
  1956. * to set up its manifest directly
  1957. */
  1958. int phar_open_executed_filename(char *alias, size_t alias_len, char **error) /* {{{ */
  1959. {
  1960. char *fname;
  1961. php_stream *fp;
  1962. size_t fname_len;
  1963. zend_string *actual = NULL;
  1964. int ret;
  1965. if (error) {
  1966. *error = NULL;
  1967. }
  1968. fname = (char*)zend_get_executed_filename();
  1969. fname_len = strlen(fname);
  1970. if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, 0, REPORT_ERRORS, NULL, 0) == SUCCESS) {
  1971. return SUCCESS;
  1972. }
  1973. if (!strcmp(fname, "[no active file]")) {
  1974. if (error) {
  1975. spprintf(error, 0, "cannot initialize a phar outside of PHP execution");
  1976. }
  1977. return FAILURE;
  1978. }
  1979. if (0 == zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
  1980. if (error) {
  1981. spprintf(error, 0, "__HALT_COMPILER(); must be declared in a phar");
  1982. }
  1983. return FAILURE;
  1984. }
  1985. if (php_check_open_basedir(fname)) {
  1986. return FAILURE;
  1987. }
  1988. fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, &actual);
  1989. if (!fp) {
  1990. if (error) {
  1991. spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
  1992. }
  1993. if (actual) {
  1994. zend_string_release_ex(actual, 0);
  1995. }
  1996. return FAILURE;
  1997. }
  1998. if (actual) {
  1999. fname = ZSTR_VAL(actual);
  2000. fname_len = ZSTR_LEN(actual);
  2001. }
  2002. ret = phar_open_from_fp(fp, fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, 0, error);
  2003. if (actual) {
  2004. zend_string_release_ex(actual, 0);
  2005. }
  2006. return ret;
  2007. }
  2008. /* }}} */
  2009. /**
  2010. * Validate the CRC32 of a file opened from within the phar
  2011. */
  2012. int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip) /* {{{ */
  2013. {
  2014. uint32_t crc = ~0;
  2015. int len = idata->internal_file->uncompressed_filesize;
  2016. php_stream *fp = idata->fp;
  2017. phar_entry_info *entry = idata->internal_file;
  2018. if (error) {
  2019. *error = NULL;
  2020. }
  2021. if (entry->is_zip && process_zip > 0) {
  2022. /* verify local file header */
  2023. phar_zip_file_header local;
  2024. phar_zip_data_desc desc;
  2025. if (SUCCESS != phar_open_archive_fp(idata->phar)) {
  2026. spprintf(error, 0, "phar error: unable to open zip-based phar archive \"%s\" to verify local file header for file \"%s\"", idata->phar->fname, entry->filename);
  2027. return FAILURE;
  2028. }
  2029. php_stream_seek(phar_get_entrypfp(idata->internal_file), entry->header_offset, SEEK_SET);
  2030. if (sizeof(local) != php_stream_read(phar_get_entrypfp(idata->internal_file), (char *) &local, sizeof(local))) {
  2031. spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local file header for file \"%s\")", idata->phar->fname, entry->filename);
  2032. return FAILURE;
  2033. }
  2034. /* check for data descriptor */
  2035. if (((PHAR_ZIP_16(local.flags)) & 0x8) == 0x8) {
  2036. php_stream_seek(phar_get_entrypfp(idata->internal_file),
  2037. entry->header_offset + sizeof(local) +
  2038. PHAR_ZIP_16(local.filename_len) +
  2039. PHAR_ZIP_16(local.extra_len) +
  2040. entry->compressed_filesize, SEEK_SET);
  2041. if (sizeof(desc) != php_stream_read(phar_get_entrypfp(idata->internal_file),
  2042. (char *) &desc, sizeof(desc))) {
  2043. spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local data descriptor for file \"%s\")", idata->phar->fname, entry->filename);
  2044. return FAILURE;
  2045. }
  2046. if (desc.signature[0] == 'P' && desc.signature[1] == 'K') {
  2047. memcpy(&(local.crc32), &(desc.crc32), 12);
  2048. } else {
  2049. /* old data descriptors have no signature */
  2050. memcpy(&(local.crc32), &desc, 12);
  2051. }
  2052. }
  2053. /* verify local header */
  2054. if (entry->filename_len != PHAR_ZIP_16(local.filename_len) || entry->crc32 != PHAR_ZIP_32(local.crc32) || entry->uncompressed_filesize != PHAR_ZIP_32(local.uncompsize) || entry->compressed_filesize != PHAR_ZIP_32(local.compsize)) {
  2055. spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (local header of file \"%s\" does not match central directory)", idata->phar->fname, entry->filename);
  2056. return FAILURE;
  2057. }
  2058. /* construct actual offset to file start - local extra_len can be different from central extra_len */
  2059. entry->offset = entry->offset_abs =
  2060. sizeof(local) + entry->header_offset + PHAR_ZIP_16(local.filename_len) + PHAR_ZIP_16(local.extra_len);
  2061. if (idata->zero && idata->zero != entry->offset_abs) {
  2062. idata->zero = entry->offset_abs;
  2063. }
  2064. }
  2065. if (process_zip == 1) {
  2066. return SUCCESS;
  2067. }
  2068. php_stream_seek(fp, idata->zero, SEEK_SET);
  2069. while (len--) {
  2070. CRC32(crc, php_stream_getc(fp));
  2071. }
  2072. php_stream_seek(fp, idata->zero, SEEK_SET);
  2073. if (~crc == crc32) {
  2074. entry->is_crc_checked = 1;
  2075. return SUCCESS;
  2076. } else {
  2077. spprintf(error, 0, "phar error: internal corruption of phar \"%s\" (crc32 mismatch on file \"%s\")", idata->phar->fname, entry->filename);
  2078. return FAILURE;
  2079. }
  2080. }
  2081. /* }}} */
  2082. static inline void phar_set_32(char *buffer, uint32_t var) /* {{{ */
  2083. {
  2084. #ifdef WORDS_BIGENDIAN
  2085. *((buffer) + 3) = (unsigned char) (((var) >> 24) & 0xFF);
  2086. *((buffer) + 2) = (unsigned char) (((var) >> 16) & 0xFF);
  2087. *((buffer) + 1) = (unsigned char) (((var) >> 8) & 0xFF);
  2088. *((buffer) + 0) = (unsigned char) ((var) & 0xFF);
  2089. #else
  2090. memcpy(buffer, &var, sizeof(var));
  2091. #endif
  2092. } /* }}} */
  2093. static int phar_flush_clean_deleted_apply(zval *zv) /* {{{ */
  2094. {
  2095. phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
  2096. if (entry->fp_refcount <= 0 && entry->is_deleted) {
  2097. return ZEND_HASH_APPLY_REMOVE;
  2098. } else {
  2099. return ZEND_HASH_APPLY_KEEP;
  2100. }
  2101. }
  2102. /* }}} */
  2103. #include "stub.h"
  2104. zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error) /* {{{ */
  2105. {
  2106. size_t index_len, web_len;
  2107. if (error) {
  2108. *error = NULL;
  2109. }
  2110. if (!index_php) {
  2111. index_php = "index.php";
  2112. }
  2113. if (!web_index) {
  2114. web_index = "index.php";
  2115. }
  2116. index_len = strlen(index_php);
  2117. web_len = strlen(web_index);
  2118. if (index_len > 400) {
  2119. /* ridiculous size not allowed for index.php startup filename */
  2120. if (error) {
  2121. spprintf(error, 0, "Illegal filename passed in for stub creation, was %zd characters long, and only 400 or less is allowed", index_len);
  2122. return NULL;
  2123. }
  2124. }
  2125. if (web_len > 400) {
  2126. /* ridiculous size not allowed for index.php startup filename */
  2127. if (error) {
  2128. spprintf(error, 0, "Illegal web filename passed in for stub creation, was %zd characters long, and only 400 or less is allowed", web_len);
  2129. return NULL;
  2130. }
  2131. }
  2132. return phar_get_stub(index_php, web_index, index_len+1, web_len+1);
  2133. }
  2134. /* }}} */
  2135. /**
  2136. * Save phar contents to disk
  2137. *
  2138. * user_stub contains either a string, or a resource pointer, if len is a negative length.
  2139. * user_stub and len should be both 0 if the default or existing stub should be used
  2140. */
  2141. int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int convert, char **error) /* {{{ */
  2142. {
  2143. char halt_stub[] = "__HALT_COMPILER();";
  2144. zend_string *newstub;
  2145. char *tmp;
  2146. phar_entry_info *entry, *newentry;
  2147. size_t halt_offset;
  2148. int restore_alias_len, global_flags = 0, closeoldfile;
  2149. char *pos, has_dirs = 0;
  2150. char manifest[18], entry_buffer[24];
  2151. zend_off_t manifest_ftell;
  2152. zend_long offset;
  2153. size_t wrote;
  2154. uint32_t manifest_len, mytime, loc, new_manifest_count;
  2155. uint32_t newcrc32;
  2156. php_stream *file, *oldfile, *newfile, *stubfile;
  2157. php_stream_filter *filter;
  2158. php_serialize_data_t metadata_hash;
  2159. smart_str main_metadata_str = {0};
  2160. int free_user_stub, free_fp = 1, free_ufp = 1;
  2161. int manifest_hack = 0;
  2162. if (phar->is_persistent) {
  2163. if (error) {
  2164. spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname);
  2165. }
  2166. return EOF;
  2167. }
  2168. if (error) {
  2169. *error = NULL;
  2170. }
  2171. if (!zend_hash_num_elements(&phar->manifest) && !user_stub) {
  2172. return EOF;
  2173. }
  2174. zend_hash_clean(&phar->virtual_dirs);
  2175. if (phar->is_zip) {
  2176. return phar_zip_flush(phar, user_stub, len, convert, error);
  2177. }
  2178. if (phar->is_tar) {
  2179. return phar_tar_flush(phar, user_stub, len, convert, error);
  2180. }
  2181. if (PHAR_G(readonly)) {
  2182. return EOF;
  2183. }
  2184. if (phar->fp && !phar->is_brandnew) {
  2185. oldfile = phar->fp;
  2186. closeoldfile = 0;
  2187. php_stream_rewind(oldfile);
  2188. } else {
  2189. oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
  2190. closeoldfile = oldfile != NULL;
  2191. }
  2192. newfile = php_stream_fopen_tmpfile();
  2193. if (!newfile) {
  2194. if (error) {
  2195. spprintf(error, 0, "unable to create temporary file");
  2196. }
  2197. if (closeoldfile) {
  2198. php_stream_close(oldfile);
  2199. }
  2200. return EOF;
  2201. }
  2202. if (user_stub) {
  2203. zend_string *suser_stub;
  2204. if (len < 0) {
  2205. /* resource passed in */
  2206. if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) {
  2207. if (closeoldfile) {
  2208. php_stream_close(oldfile);
  2209. }
  2210. php_stream_close(newfile);
  2211. if (error) {
  2212. spprintf(error, 0, "unable to access resource to copy stub to new phar \"%s\"", phar->fname);
  2213. }
  2214. return EOF;
  2215. }
  2216. if (len == -1) {
  2217. len = PHP_STREAM_COPY_ALL;
  2218. } else {
  2219. len = -len;
  2220. }
  2221. user_stub = 0;
  2222. if (!(suser_stub = php_stream_copy_to_mem(stubfile, len, 0))) {
  2223. if (closeoldfile) {
  2224. php_stream_close(oldfile);
  2225. }
  2226. php_stream_close(newfile);
  2227. if (error) {
  2228. spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", phar->fname);
  2229. }
  2230. return EOF;
  2231. }
  2232. free_user_stub = 1;
  2233. user_stub = ZSTR_VAL(suser_stub);
  2234. len = ZSTR_LEN(suser_stub);
  2235. } else {
  2236. free_user_stub = 0;
  2237. }
  2238. tmp = estrndup(user_stub, len);
  2239. if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
  2240. efree(tmp);
  2241. if (closeoldfile) {
  2242. php_stream_close(oldfile);
  2243. }
  2244. php_stream_close(newfile);
  2245. if (error) {
  2246. spprintf(error, 0, "illegal stub for phar \"%s\"", phar->fname);
  2247. }
  2248. if (free_user_stub) {
  2249. zend_string_free(suser_stub);
  2250. }
  2251. return EOF;
  2252. }
  2253. pos = user_stub + (pos - tmp);
  2254. efree(tmp);
  2255. len = pos - user_stub + 18;
  2256. if ((size_t)len != php_stream_write(newfile, user_stub, len)
  2257. || 5 != php_stream_write(newfile, " ?>\r\n", 5)) {
  2258. if (closeoldfile) {
  2259. php_stream_close(oldfile);
  2260. }
  2261. php_stream_close(newfile);
  2262. if (error) {
  2263. spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", phar->fname);
  2264. }
  2265. if (free_user_stub) {
  2266. zend_string_free(suser_stub);
  2267. }
  2268. return EOF;
  2269. }
  2270. phar->halt_offset = len + 5;
  2271. if (free_user_stub) {
  2272. zend_string_free(suser_stub);
  2273. }
  2274. } else {
  2275. size_t written;
  2276. if (!user_stub && phar->halt_offset && oldfile && !phar->is_brandnew) {
  2277. php_stream_copy_to_stream_ex(oldfile, newfile, phar->halt_offset, &written);
  2278. newstub = NULL;
  2279. } else {
  2280. /* this is either a brand new phar or a default stub overwrite */
  2281. newstub = phar_create_default_stub(NULL, NULL, NULL);
  2282. phar->halt_offset = ZSTR_LEN(newstub);
  2283. written = php_stream_write(newfile, ZSTR_VAL(newstub), phar->halt_offset);
  2284. }
  2285. if (phar->halt_offset != written) {
  2286. if (closeoldfile) {
  2287. php_stream_close(oldfile);
  2288. }
  2289. php_stream_close(newfile);
  2290. if (error) {
  2291. if (newstub) {
  2292. spprintf(error, 0, "unable to create stub in new phar \"%s\"", phar->fname);
  2293. } else {
  2294. spprintf(error, 0, "unable to copy stub of old phar to new phar \"%s\"", phar->fname);
  2295. }
  2296. }
  2297. if (newstub) {
  2298. zend_string_free(newstub);
  2299. }
  2300. return EOF;
  2301. }
  2302. if (newstub) {
  2303. zend_string_free(newstub);
  2304. }
  2305. }
  2306. manifest_ftell = php_stream_tell(newfile);
  2307. halt_offset = manifest_ftell;
  2308. /* Check whether we can get rid of some of the deleted entries which are
  2309. * unused. However some might still be in use so even after this clean-up
  2310. * we need to skip entries marked is_deleted. */
  2311. zend_hash_apply(&phar->manifest, phar_flush_clean_deleted_apply);
  2312. /* compress as necessary, calculate crcs, serialize meta-data, manifest size, and file sizes */
  2313. main_metadata_str.s = NULL;
  2314. if (Z_TYPE(phar->metadata) != IS_UNDEF) {
  2315. PHP_VAR_SERIALIZE_INIT(metadata_hash);
  2316. php_var_serialize(&main_metadata_str, &phar->metadata, &metadata_hash);
  2317. PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
  2318. }
  2319. new_manifest_count = 0;
  2320. offset = 0;
  2321. for (zend_hash_internal_pointer_reset(&phar->manifest);
  2322. zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
  2323. zend_hash_move_forward(&phar->manifest)) {
  2324. if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
  2325. continue;
  2326. }
  2327. if (entry->cfp) {
  2328. /* did we forget to get rid of cfp last time? */
  2329. php_stream_close(entry->cfp);
  2330. entry->cfp = 0;
  2331. }
  2332. if (entry->is_deleted || entry->is_mounted) {
  2333. /* remove this from the new phar */
  2334. continue;
  2335. }
  2336. if (!entry->is_modified && entry->fp_refcount) {
  2337. /* open file pointers refer to this fp, do not free the stream */
  2338. switch (entry->fp_type) {
  2339. case PHAR_FP:
  2340. free_fp = 0;
  2341. break;
  2342. case PHAR_UFP:
  2343. free_ufp = 0;
  2344. default:
  2345. break;
  2346. }
  2347. }
  2348. /* after excluding deleted files, calculate manifest size in bytes and number of entries */
  2349. ++new_manifest_count;
  2350. phar_add_virtual_dirs(phar, entry->filename, entry->filename_len);
  2351. if (entry->is_dir) {
  2352. /* we use this to calculate API version, 1.1.1 is used for phars with directories */
  2353. has_dirs = 1;
  2354. }
  2355. if (Z_TYPE(entry->metadata) != IS_UNDEF) {
  2356. if (entry->metadata_str.s) {
  2357. smart_str_free(&entry->metadata_str);
  2358. }
  2359. entry->metadata_str.s = NULL;
  2360. PHP_VAR_SERIALIZE_INIT(metadata_hash);
  2361. php_var_serialize(&entry->metadata_str, &entry->metadata, &metadata_hash);
  2362. PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
  2363. } else {
  2364. if (entry->metadata_str.s) {
  2365. smart_str_free(&entry->metadata_str);
  2366. }
  2367. entry->metadata_str.s = NULL;
  2368. }
  2369. /* 32 bits for filename length, length of filename, manifest + metadata, and add 1 for trailing / if a directory */
  2370. offset += 4 + entry->filename_len + sizeof(entry_buffer) + (entry->metadata_str.s ? ZSTR_LEN(entry->metadata_str.s) : 0) + (entry->is_dir ? 1 : 0);
  2371. /* compress and rehash as necessary */
  2372. if ((oldfile && !entry->is_modified) || entry->is_dir) {
  2373. if (entry->fp_type == PHAR_UFP) {
  2374. /* reset so we can copy the compressed data over */
  2375. entry->fp_type = PHAR_FP;
  2376. }
  2377. continue;
  2378. }
  2379. if (!phar_get_efp(entry, 0)) {
  2380. /* re-open internal file pointer just-in-time */
  2381. newentry = phar_open_jit(phar, entry, error);
  2382. if (!newentry) {
  2383. /* major problem re-opening, so we ignore this file and the error */
  2384. efree(*error);
  2385. *error = NULL;
  2386. continue;
  2387. }
  2388. entry = newentry;
  2389. }
  2390. file = phar_get_efp(entry, 0);
  2391. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 1)) {
  2392. if (closeoldfile) {
  2393. php_stream_close(oldfile);
  2394. }
  2395. php_stream_close(newfile);
  2396. if (error) {
  2397. spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2398. }
  2399. return EOF;
  2400. }
  2401. newcrc32 = ~0;
  2402. mytime = entry->uncompressed_filesize;
  2403. for (loc = 0;loc < mytime; ++loc) {
  2404. CRC32(newcrc32, php_stream_getc(file));
  2405. }
  2406. entry->crc32 = ~newcrc32;
  2407. entry->is_crc_checked = 1;
  2408. if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
  2409. /* not compressed */
  2410. entry->compressed_filesize = entry->uncompressed_filesize;
  2411. continue;
  2412. }
  2413. filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0);
  2414. if (!filter) {
  2415. if (closeoldfile) {
  2416. php_stream_close(oldfile);
  2417. }
  2418. php_stream_close(newfile);
  2419. if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
  2420. if (error) {
  2421. spprintf(error, 0, "unable to gzip compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
  2422. }
  2423. } else {
  2424. if (error) {
  2425. spprintf(error, 0, "unable to bzip2 compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
  2426. }
  2427. }
  2428. return EOF;
  2429. }
  2430. /* create new file that holds the compressed version */
  2431. /* work around inability to specify freedom in write and strictness
  2432. in read count */
  2433. entry->cfp = php_stream_fopen_tmpfile();
  2434. if (!entry->cfp) {
  2435. if (error) {
  2436. spprintf(error, 0, "unable to create temporary file");
  2437. }
  2438. if (closeoldfile) {
  2439. php_stream_close(oldfile);
  2440. }
  2441. php_stream_close(newfile);
  2442. return EOF;
  2443. }
  2444. php_stream_flush(file);
  2445. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
  2446. if (closeoldfile) {
  2447. php_stream_close(oldfile);
  2448. }
  2449. php_stream_close(newfile);
  2450. if (error) {
  2451. spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2452. }
  2453. return EOF;
  2454. }
  2455. php_stream_filter_append((&entry->cfp->writefilters), filter);
  2456. if (SUCCESS != php_stream_copy_to_stream_ex(file, entry->cfp, entry->uncompressed_filesize, NULL)) {
  2457. if (closeoldfile) {
  2458. php_stream_close(oldfile);
  2459. }
  2460. php_stream_close(newfile);
  2461. if (error) {
  2462. spprintf(error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2463. }
  2464. return EOF;
  2465. }
  2466. php_stream_filter_flush(filter, 1);
  2467. php_stream_flush(entry->cfp);
  2468. php_stream_filter_remove(filter, 1);
  2469. php_stream_seek(entry->cfp, 0, SEEK_END);
  2470. entry->compressed_filesize = (uint32_t) php_stream_tell(entry->cfp);
  2471. /* generate crc on compressed file */
  2472. php_stream_rewind(entry->cfp);
  2473. entry->old_flags = entry->flags;
  2474. entry->is_modified = 1;
  2475. global_flags |= (entry->flags & PHAR_ENT_COMPRESSION_MASK);
  2476. }
  2477. global_flags |= PHAR_HDR_SIGNATURE;
  2478. /* write out manifest pre-header */
  2479. /* 4: manifest length
  2480. * 4: manifest entry count
  2481. * 2: phar version
  2482. * 4: phar global flags
  2483. * 4: alias length
  2484. * ?: the alias itself
  2485. * 4: phar metadata length
  2486. * ?: phar metadata
  2487. */
  2488. restore_alias_len = phar->alias_len;
  2489. if (phar->is_temporary_alias) {
  2490. phar->alias_len = 0;
  2491. }
  2492. manifest_len = offset + phar->alias_len + sizeof(manifest) + (main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
  2493. phar_set_32(manifest, manifest_len);
  2494. /* Hack - see bug #65028, add padding byte to the end of the manifest */
  2495. if(manifest[0] == '\r' || manifest[0] == '\n') {
  2496. manifest_len++;
  2497. phar_set_32(manifest, manifest_len);
  2498. manifest_hack = 1;
  2499. }
  2500. phar_set_32(manifest+4, new_manifest_count);
  2501. if (has_dirs) {
  2502. *(manifest + 8) = (unsigned char) (((PHAR_API_VERSION) >> 8) & 0xFF);
  2503. *(manifest + 9) = (unsigned char) (((PHAR_API_VERSION) & 0xF0));
  2504. } else {
  2505. *(manifest + 8) = (unsigned char) (((PHAR_API_VERSION_NODIR) >> 8) & 0xFF);
  2506. *(manifest + 9) = (unsigned char) (((PHAR_API_VERSION_NODIR) & 0xF0));
  2507. }
  2508. phar_set_32(manifest+10, global_flags);
  2509. phar_set_32(manifest+14, phar->alias_len);
  2510. /* write the manifest header */
  2511. if (sizeof(manifest) != php_stream_write(newfile, manifest, sizeof(manifest))
  2512. || (size_t)phar->alias_len != php_stream_write(newfile, phar->alias, phar->alias_len)) {
  2513. if (closeoldfile) {
  2514. php_stream_close(oldfile);
  2515. }
  2516. php_stream_close(newfile);
  2517. phar->alias_len = restore_alias_len;
  2518. if (error) {
  2519. spprintf(error, 0, "unable to write manifest header of new phar \"%s\"", phar->fname);
  2520. }
  2521. return EOF;
  2522. }
  2523. phar->alias_len = restore_alias_len;
  2524. phar_set_32(manifest, main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
  2525. if (4 != php_stream_write(newfile, manifest, 4) || ((main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0)
  2526. && ZSTR_LEN(main_metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(main_metadata_str.s), ZSTR_LEN(main_metadata_str.s)))) {
  2527. smart_str_free(&main_metadata_str);
  2528. if (closeoldfile) {
  2529. php_stream_close(oldfile);
  2530. }
  2531. php_stream_close(newfile);
  2532. phar->alias_len = restore_alias_len;
  2533. if (error) {
  2534. spprintf(error, 0, "unable to write manifest meta-data of new phar \"%s\"", phar->fname);
  2535. }
  2536. return EOF;
  2537. }
  2538. smart_str_free(&main_metadata_str);
  2539. /* re-calculate the manifest location to simplify later code */
  2540. manifest_ftell = php_stream_tell(newfile);
  2541. /* now write the manifest */
  2542. for (zend_hash_internal_pointer_reset(&phar->manifest);
  2543. zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
  2544. zend_hash_move_forward(&phar->manifest)) {
  2545. if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
  2546. continue;
  2547. }
  2548. if (entry->is_deleted || entry->is_mounted) {
  2549. /* remove this from the new phar if deleted, ignore if mounted */
  2550. continue;
  2551. }
  2552. if (entry->is_dir) {
  2553. /* add 1 for trailing slash */
  2554. phar_set_32(entry_buffer, entry->filename_len + 1);
  2555. } else {
  2556. phar_set_32(entry_buffer, entry->filename_len);
  2557. }
  2558. if (4 != php_stream_write(newfile, entry_buffer, 4)
  2559. || entry->filename_len != php_stream_write(newfile, entry->filename, entry->filename_len)
  2560. || (entry->is_dir && 1 != php_stream_write(newfile, "/", 1))) {
  2561. if (closeoldfile) {
  2562. php_stream_close(oldfile);
  2563. }
  2564. php_stream_close(newfile);
  2565. if (error) {
  2566. if (entry->is_dir) {
  2567. spprintf(error, 0, "unable to write filename of directory \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
  2568. } else {
  2569. spprintf(error, 0, "unable to write filename of file \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
  2570. }
  2571. }
  2572. return EOF;
  2573. }
  2574. /* set the manifest meta-data:
  2575. 4: uncompressed filesize
  2576. 4: creation timestamp
  2577. 4: compressed filesize
  2578. 4: crc32
  2579. 4: flags
  2580. 4: metadata-len
  2581. +: metadata
  2582. */
  2583. mytime = time(NULL);
  2584. phar_set_32(entry_buffer, entry->uncompressed_filesize);
  2585. phar_set_32(entry_buffer+4, mytime);
  2586. phar_set_32(entry_buffer+8, entry->compressed_filesize);
  2587. phar_set_32(entry_buffer+12, entry->crc32);
  2588. phar_set_32(entry_buffer+16, entry->flags);
  2589. phar_set_32(entry_buffer+20, entry->metadata_str.s ? ZSTR_LEN(entry->metadata_str.s) : 0);
  2590. if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer))
  2591. || (entry->metadata_str.s &&
  2592. ZSTR_LEN(entry->metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(entry->metadata_str.s), ZSTR_LEN(entry->metadata_str.s)))) {
  2593. if (closeoldfile) {
  2594. php_stream_close(oldfile);
  2595. }
  2596. php_stream_close(newfile);
  2597. if (error) {
  2598. spprintf(error, 0, "unable to write temporary manifest of file \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
  2599. }
  2600. return EOF;
  2601. }
  2602. }
  2603. /* Hack - see bug #65028, add padding byte to the end of the manifest */
  2604. if(manifest_hack) {
  2605. if(1 != php_stream_write(newfile, manifest, 1)) {
  2606. if (closeoldfile) {
  2607. php_stream_close(oldfile);
  2608. }
  2609. php_stream_close(newfile);
  2610. if (error) {
  2611. spprintf(error, 0, "unable to write manifest padding byte");
  2612. }
  2613. return EOF;
  2614. }
  2615. }
  2616. /* now copy the actual file data to the new phar */
  2617. offset = php_stream_tell(newfile);
  2618. for (zend_hash_internal_pointer_reset(&phar->manifest);
  2619. zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
  2620. zend_hash_move_forward(&phar->manifest)) {
  2621. if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
  2622. continue;
  2623. }
  2624. if (entry->is_deleted || entry->is_dir || entry->is_mounted) {
  2625. continue;
  2626. }
  2627. if (entry->cfp) {
  2628. file = entry->cfp;
  2629. php_stream_rewind(file);
  2630. } else {
  2631. file = phar_get_efp(entry, 0);
  2632. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
  2633. if (closeoldfile) {
  2634. php_stream_close(oldfile);
  2635. }
  2636. php_stream_close(newfile);
  2637. if (error) {
  2638. spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2639. }
  2640. return EOF;
  2641. }
  2642. }
  2643. if (!file) {
  2644. if (closeoldfile) {
  2645. php_stream_close(oldfile);
  2646. }
  2647. php_stream_close(newfile);
  2648. if (error) {
  2649. spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2650. }
  2651. return EOF;
  2652. }
  2653. /* this will have changed for all files that have either changed compression or been modified */
  2654. entry->offset = entry->offset_abs = offset;
  2655. offset += entry->compressed_filesize;
  2656. if (php_stream_copy_to_stream_ex(file, newfile, entry->compressed_filesize, &wrote) == FAILURE) {
  2657. if (closeoldfile) {
  2658. php_stream_close(oldfile);
  2659. }
  2660. php_stream_close(newfile);
  2661. if (error) {
  2662. spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
  2663. }
  2664. return EOF;
  2665. }
  2666. entry->is_modified = 0;
  2667. if (entry->cfp) {
  2668. php_stream_close(entry->cfp);
  2669. entry->cfp = NULL;
  2670. }
  2671. if (entry->fp_type == PHAR_MOD) {
  2672. /* this fp is in use by a phar_entry_data returned by phar_get_entry_data, it will be closed when the phar_entry_data is phar_entry_delref'ed */
  2673. if (entry->fp_refcount == 0 && entry->fp != phar->fp && entry->fp != phar->ufp) {
  2674. php_stream_close(entry->fp);
  2675. }
  2676. entry->fp = NULL;
  2677. entry->fp_type = PHAR_FP;
  2678. } else if (entry->fp_type == PHAR_UFP) {
  2679. entry->fp_type = PHAR_FP;
  2680. }
  2681. }
  2682. /* append signature */
  2683. if (global_flags & PHAR_HDR_SIGNATURE) {
  2684. char sig_buf[4];
  2685. php_stream_rewind(newfile);
  2686. if (phar->signature) {
  2687. efree(phar->signature);
  2688. phar->signature = NULL;
  2689. }
  2690. switch(phar->sig_flags) {
  2691. #ifndef PHAR_HASH_OK
  2692. case PHAR_SIG_SHA512:
  2693. case PHAR_SIG_SHA256:
  2694. if (closeoldfile) {
  2695. php_stream_close(oldfile);
  2696. }
  2697. php_stream_close(newfile);
  2698. if (error) {
  2699. spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\" with requested hash type", entry->filename, phar->fname);
  2700. }
  2701. return EOF;
  2702. #endif
  2703. default: {
  2704. char *digest = NULL;
  2705. size_t digest_len;
  2706. if (FAILURE == phar_create_signature(phar, newfile, &digest, &digest_len, error)) {
  2707. if (error) {
  2708. char *save = *error;
  2709. spprintf(error, 0, "phar error: unable to write signature: %s", save);
  2710. efree(save);
  2711. }
  2712. if (digest) {
  2713. efree(digest);
  2714. }
  2715. if (closeoldfile) {
  2716. php_stream_close(oldfile);
  2717. }
  2718. php_stream_close(newfile);
  2719. return EOF;
  2720. }
  2721. php_stream_write(newfile, digest, digest_len);
  2722. efree(digest);
  2723. if (phar->sig_flags == PHAR_SIG_OPENSSL) {
  2724. phar_set_32(sig_buf, digest_len);
  2725. php_stream_write(newfile, sig_buf, 4);
  2726. }
  2727. break;
  2728. }
  2729. }
  2730. phar_set_32(sig_buf, phar->sig_flags);
  2731. php_stream_write(newfile, sig_buf, 4);
  2732. php_stream_write(newfile, "GBMB", 4);
  2733. }
  2734. /* finally, close the temp file, rename the original phar,
  2735. move the temp to the old phar, unlink the old phar, and reload it into memory
  2736. */
  2737. if (phar->fp && free_fp) {
  2738. php_stream_close(phar->fp);
  2739. }
  2740. if (phar->ufp) {
  2741. if (free_ufp) {
  2742. php_stream_close(phar->ufp);
  2743. }
  2744. phar->ufp = NULL;
  2745. }
  2746. if (closeoldfile) {
  2747. php_stream_close(oldfile);
  2748. }
  2749. phar->internal_file_start = halt_offset + manifest_len + 4;
  2750. phar->halt_offset = halt_offset;
  2751. phar->is_brandnew = 0;
  2752. php_stream_rewind(newfile);
  2753. if (phar->donotflush) {
  2754. /* deferred flush */
  2755. phar->fp = newfile;
  2756. } else {
  2757. phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
  2758. if (!phar->fp) {
  2759. phar->fp = newfile;
  2760. if (error) {
  2761. spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname);
  2762. }
  2763. return EOF;
  2764. }
  2765. if (phar->flags & PHAR_FILE_COMPRESSED_GZ) {
  2766. /* to properly compress, we have to tell zlib to add a zlib header */
  2767. zval filterparams;
  2768. array_init(&filterparams);
  2769. add_assoc_long(&filterparams, "window", MAX_WBITS+16);
  2770. filter = php_stream_filter_create("zlib.deflate", &filterparams, php_stream_is_persistent(phar->fp));
  2771. zend_array_destroy(Z_ARR(filterparams));
  2772. if (!filter) {
  2773. if (error) {
  2774. spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname);
  2775. }
  2776. return EOF;
  2777. }
  2778. php_stream_filter_append(&phar->fp->writefilters, filter);
  2779. php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  2780. php_stream_filter_flush(filter, 1);
  2781. php_stream_filter_remove(filter, 1);
  2782. php_stream_close(phar->fp);
  2783. /* use the temp stream as our base */
  2784. phar->fp = newfile;
  2785. } else if (phar->flags & PHAR_FILE_COMPRESSED_BZ2) {
  2786. filter = php_stream_filter_create("bzip2.compress", NULL, php_stream_is_persistent(phar->fp));
  2787. php_stream_filter_append(&phar->fp->writefilters, filter);
  2788. php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  2789. php_stream_filter_flush(filter, 1);
  2790. php_stream_filter_remove(filter, 1);
  2791. php_stream_close(phar->fp);
  2792. /* use the temp stream as our base */
  2793. phar->fp = newfile;
  2794. } else {
  2795. php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  2796. /* we could also reopen the file in "rb" mode but there is no need for that */
  2797. php_stream_close(newfile);
  2798. }
  2799. }
  2800. if (-1 == php_stream_seek(phar->fp, phar->halt_offset, SEEK_SET)) {
  2801. if (error) {
  2802. spprintf(error, 0, "unable to seek to __HALT_COMPILER(); in new phar \"%s\"", phar->fname);
  2803. }
  2804. return EOF;
  2805. }
  2806. return EOF;
  2807. }
  2808. /* }}} */
  2809. #ifdef COMPILE_DL_PHAR
  2810. #ifdef ZTS
  2811. ZEND_TSRMLS_CACHE_DEFINE()
  2812. #endif
  2813. ZEND_GET_MODULE(phar)
  2814. #endif
  2815. /* {{{ phar_functions[]
  2816. *
  2817. * Every user visible function must have an entry in phar_functions[].
  2818. */
  2819. static const zend_function_entry phar_functions[] = {
  2820. PHP_FE_END
  2821. };
  2822. /* }}}*/
  2823. static size_t phar_zend_stream_reader(void *handle, char *buf, size_t len) /* {{{ */
  2824. {
  2825. return php_stream_read(phar_get_pharfp((phar_archive_data*)handle), buf, len);
  2826. }
  2827. /* }}} */
  2828. static size_t phar_zend_stream_fsizer(void *handle) /* {{{ */
  2829. {
  2830. return ((phar_archive_data*)handle)->halt_offset + 32;
  2831. } /* }}} */
  2832. zend_op_array *(*phar_orig_compile_file)(zend_file_handle *file_handle, int type);
  2833. #define phar_orig_zend_open zend_stream_open_function
  2834. static zend_string *phar_resolve_path(const char *filename, size_t filename_len)
  2835. {
  2836. return phar_find_in_include_path((char *) filename, filename_len, NULL);
  2837. }
  2838. static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type) /* {{{ */
  2839. {
  2840. zend_op_array *res;
  2841. char *name = NULL;
  2842. int failed;
  2843. phar_archive_data *phar;
  2844. if (!file_handle || !file_handle->filename) {
  2845. return phar_orig_compile_file(file_handle, type);
  2846. }
  2847. if (strstr(file_handle->filename, ".phar") && !strstr(file_handle->filename, "://")) {
  2848. if (SUCCESS == phar_open_from_filename((char*)file_handle->filename, strlen(file_handle->filename), NULL, 0, 0, &phar, NULL)) {
  2849. if (phar->is_zip || phar->is_tar) {
  2850. zend_file_handle f = *file_handle;
  2851. /* zip or tar-based phar */
  2852. spprintf(&name, 4096, "phar://%s/%s", file_handle->filename, ".phar/stub.php");
  2853. if (SUCCESS == phar_orig_zend_open((const char *)name, &f)) {
  2854. efree(name);
  2855. name = NULL;
  2856. f.filename = file_handle->filename;
  2857. if (f.opened_path) {
  2858. efree(f.opened_path);
  2859. }
  2860. f.opened_path = file_handle->opened_path;
  2861. f.free_filename = file_handle->free_filename;
  2862. switch (file_handle->type) {
  2863. case ZEND_HANDLE_STREAM:
  2864. case ZEND_HANDLE_MAPPED:
  2865. if (file_handle->handle.stream.closer && file_handle->handle.stream.handle) {
  2866. file_handle->handle.stream.closer(file_handle->handle.stream.handle);
  2867. }
  2868. file_handle->handle.stream.handle = NULL;
  2869. break;
  2870. default:
  2871. break;
  2872. }
  2873. *file_handle = f;
  2874. }
  2875. } else if (phar->flags & PHAR_FILE_COMPRESSION_MASK) {
  2876. zend_file_handle_dtor(file_handle);
  2877. /* compressed phar */
  2878. file_handle->type = ZEND_HANDLE_STREAM;
  2879. /* we do our own reading directly from the phar, don't change the next line */
  2880. file_handle->handle.stream.handle = phar;
  2881. file_handle->handle.stream.reader = phar_zend_stream_reader;
  2882. file_handle->handle.stream.closer = NULL;
  2883. file_handle->handle.stream.fsizer = phar_zend_stream_fsizer;
  2884. file_handle->handle.stream.isatty = 0;
  2885. phar->is_persistent ?
  2886. php_stream_rewind(PHAR_G(cached_fp)[phar->phar_pos].fp) :
  2887. php_stream_rewind(phar->fp);
  2888. memset(&file_handle->handle.stream.mmap, 0, sizeof(file_handle->handle.stream.mmap));
  2889. }
  2890. }
  2891. }
  2892. zend_try {
  2893. failed = 0;
  2894. CG(zend_lineno) = 0;
  2895. res = phar_orig_compile_file(file_handle, type);
  2896. } zend_catch {
  2897. failed = 1;
  2898. res = NULL;
  2899. } zend_end_try();
  2900. if (name) {
  2901. efree(name);
  2902. }
  2903. if (failed) {
  2904. zend_bailout();
  2905. }
  2906. return res;
  2907. }
  2908. /* }}} */
  2909. typedef zend_op_array* (zend_compile_t)(zend_file_handle*, int);
  2910. typedef zend_compile_t* (compile_hook)(zend_compile_t *ptr);
  2911. static void mime_type_dtor(zval *zv)
  2912. {
  2913. free(Z_PTR_P(zv));
  2914. }
  2915. PHP_GINIT_FUNCTION(phar) /* {{{ */
  2916. {
  2917. #if defined(COMPILE_DL_PHAR) && defined(ZTS)
  2918. ZEND_TSRMLS_CACHE_UPDATE();
  2919. #endif
  2920. phar_mime_type mime;
  2921. memset(phar_globals, 0, sizeof(zend_phar_globals));
  2922. phar_globals->readonly = 1;
  2923. zend_hash_init(&phar_globals->mime_types, 0, NULL, mime_type_dtor, 1);
  2924. #define PHAR_SET_MIME(mimetype, ret, fileext) \
  2925. mime.mime = mimetype; \
  2926. mime.len = sizeof((mimetype))+1; \
  2927. mime.type = ret; \
  2928. zend_hash_str_add_mem(&phar_globals->mime_types, fileext, sizeof(fileext)-1, (void *)&mime, sizeof(phar_mime_type)); \
  2929. PHAR_SET_MIME("text/html", PHAR_MIME_PHPS, "phps")
  2930. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "c")
  2931. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "cc")
  2932. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "cpp")
  2933. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "c++")
  2934. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "dtd")
  2935. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "h")
  2936. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "log")
  2937. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "rng")
  2938. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "txt")
  2939. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "xsd")
  2940. PHAR_SET_MIME("", PHAR_MIME_PHP, "php")
  2941. PHAR_SET_MIME("", PHAR_MIME_PHP, "inc")
  2942. PHAR_SET_MIME("video/avi", PHAR_MIME_OTHER, "avi")
  2943. PHAR_SET_MIME("image/bmp", PHAR_MIME_OTHER, "bmp")
  2944. PHAR_SET_MIME("text/css", PHAR_MIME_OTHER, "css")
  2945. PHAR_SET_MIME("image/gif", PHAR_MIME_OTHER, "gif")
  2946. PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "htm")
  2947. PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "html")
  2948. PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "htmls")
  2949. PHAR_SET_MIME("image/x-ico", PHAR_MIME_OTHER, "ico")
  2950. PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpe")
  2951. PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpg")
  2952. PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpeg")
  2953. PHAR_SET_MIME("application/x-javascript", PHAR_MIME_OTHER, "js")
  2954. PHAR_SET_MIME("audio/midi", PHAR_MIME_OTHER, "midi")
  2955. PHAR_SET_MIME("audio/midi", PHAR_MIME_OTHER, "mid")
  2956. PHAR_SET_MIME("audio/mod", PHAR_MIME_OTHER, "mod")
  2957. PHAR_SET_MIME("movie/quicktime", PHAR_MIME_OTHER, "mov")
  2958. PHAR_SET_MIME("audio/mp3", PHAR_MIME_OTHER, "mp3")
  2959. PHAR_SET_MIME("video/mpeg", PHAR_MIME_OTHER, "mpg")
  2960. PHAR_SET_MIME("video/mpeg", PHAR_MIME_OTHER, "mpeg")
  2961. PHAR_SET_MIME("application/pdf", PHAR_MIME_OTHER, "pdf")
  2962. PHAR_SET_MIME("image/png", PHAR_MIME_OTHER, "png")
  2963. PHAR_SET_MIME("application/shockwave-flash", PHAR_MIME_OTHER, "swf")
  2964. PHAR_SET_MIME("image/tiff", PHAR_MIME_OTHER, "tif")
  2965. PHAR_SET_MIME("image/tiff", PHAR_MIME_OTHER, "tiff")
  2966. PHAR_SET_MIME("audio/wav", PHAR_MIME_OTHER, "wav")
  2967. PHAR_SET_MIME("image/xbm", PHAR_MIME_OTHER, "xbm")
  2968. PHAR_SET_MIME("text/xml", PHAR_MIME_OTHER, "xml")
  2969. phar_restore_orig_functions();
  2970. }
  2971. /* }}} */
  2972. PHP_GSHUTDOWN_FUNCTION(phar) /* {{{ */
  2973. {
  2974. zend_hash_destroy(&phar_globals->mime_types);
  2975. }
  2976. /* }}} */
  2977. PHP_MINIT_FUNCTION(phar) /* {{{ */
  2978. {
  2979. REGISTER_INI_ENTRIES();
  2980. phar_orig_compile_file = zend_compile_file;
  2981. zend_compile_file = phar_compile_file;
  2982. phar_save_resolve_path = zend_resolve_path;
  2983. zend_resolve_path = phar_resolve_path;
  2984. phar_object_init();
  2985. phar_intercept_functions_init();
  2986. phar_save_orig_functions();
  2987. return php_register_url_stream_wrapper("phar", &php_stream_phar_wrapper);
  2988. }
  2989. /* }}} */
  2990. PHP_MSHUTDOWN_FUNCTION(phar) /* {{{ */
  2991. {
  2992. php_unregister_url_stream_wrapper("phar");
  2993. phar_intercept_functions_shutdown();
  2994. if (zend_compile_file == phar_compile_file) {
  2995. zend_compile_file = phar_orig_compile_file;
  2996. }
  2997. if (PHAR_G(manifest_cached)) {
  2998. zend_hash_destroy(&(cached_phars));
  2999. zend_hash_destroy(&(cached_alias));
  3000. }
  3001. return SUCCESS;
  3002. }
  3003. /* }}} */
  3004. void phar_request_initialize(void) /* {{{ */
  3005. {
  3006. if (!PHAR_G(request_init))
  3007. {
  3008. PHAR_G(last_phar) = NULL;
  3009. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  3010. PHAR_G(has_bz2) = zend_hash_str_exists(&module_registry, "bz2", sizeof("bz2")-1);
  3011. PHAR_G(has_zlib) = zend_hash_str_exists(&module_registry, "zlib", sizeof("zlib")-1);
  3012. PHAR_G(request_init) = 1;
  3013. PHAR_G(request_ends) = 0;
  3014. PHAR_G(request_done) = 0;
  3015. zend_hash_init(&(PHAR_G(phar_fname_map)), 5, zend_get_hash_value, destroy_phar_data, 0);
  3016. zend_hash_init(&(PHAR_G(phar_persist_map)), 5, zend_get_hash_value, NULL, 0);
  3017. zend_hash_init(&(PHAR_G(phar_alias_map)), 5, zend_get_hash_value, NULL, 0);
  3018. if (PHAR_G(manifest_cached)) {
  3019. phar_archive_data *pphar;
  3020. phar_entry_fp *stuff = (phar_entry_fp *) ecalloc(zend_hash_num_elements(&cached_phars), sizeof(phar_entry_fp));
  3021. for (zend_hash_internal_pointer_reset(&cached_phars);
  3022. (pphar = zend_hash_get_current_data_ptr(&cached_phars)) != NULL;
  3023. zend_hash_move_forward(&cached_phars)) {
  3024. stuff[pphar->phar_pos].manifest = (phar_entry_fp_info *) ecalloc( zend_hash_num_elements(&(pphar->manifest)), sizeof(phar_entry_fp_info));
  3025. }
  3026. PHAR_G(cached_fp) = stuff;
  3027. }
  3028. PHAR_G(phar_SERVER_mung_list) = 0;
  3029. PHAR_G(cwd) = NULL;
  3030. PHAR_G(cwd_len) = 0;
  3031. PHAR_G(cwd_init) = 0;
  3032. }
  3033. }
  3034. /* }}} */
  3035. PHP_RSHUTDOWN_FUNCTION(phar) /* {{{ */
  3036. {
  3037. uint32_t i;
  3038. PHAR_G(request_ends) = 1;
  3039. if (PHAR_G(request_init))
  3040. {
  3041. phar_release_functions();
  3042. zend_hash_destroy(&(PHAR_G(phar_alias_map)));
  3043. HT_FLAGS(&PHAR_G(phar_alias_map)) = 0;
  3044. zend_hash_destroy(&(PHAR_G(phar_fname_map)));
  3045. HT_FLAGS(&PHAR_G(phar_fname_map)) = 0;
  3046. zend_hash_destroy(&(PHAR_G(phar_persist_map)));
  3047. HT_FLAGS(&PHAR_G(phar_persist_map)) = 0;
  3048. PHAR_G(phar_SERVER_mung_list) = 0;
  3049. if (PHAR_G(cached_fp)) {
  3050. for (i = 0; i < zend_hash_num_elements(&cached_phars); ++i) {
  3051. if (PHAR_G(cached_fp)[i].fp) {
  3052. php_stream_close(PHAR_G(cached_fp)[i].fp);
  3053. }
  3054. if (PHAR_G(cached_fp)[i].ufp) {
  3055. php_stream_close(PHAR_G(cached_fp)[i].ufp);
  3056. }
  3057. efree(PHAR_G(cached_fp)[i].manifest);
  3058. }
  3059. efree(PHAR_G(cached_fp));
  3060. PHAR_G(cached_fp) = 0;
  3061. }
  3062. PHAR_G(request_init) = 0;
  3063. if (PHAR_G(cwd)) {
  3064. efree(PHAR_G(cwd));
  3065. }
  3066. PHAR_G(cwd) = NULL;
  3067. PHAR_G(cwd_len) = 0;
  3068. PHAR_G(cwd_init) = 0;
  3069. }
  3070. PHAR_G(request_done) = 1;
  3071. return SUCCESS;
  3072. }
  3073. /* }}} */
  3074. PHP_MINFO_FUNCTION(phar) /* {{{ */
  3075. {
  3076. phar_request_initialize();
  3077. php_info_print_table_start();
  3078. php_info_print_table_header(2, "Phar: PHP Archive support", "enabled");
  3079. php_info_print_table_row(2, "Phar API version", PHP_PHAR_API_VERSION);
  3080. php_info_print_table_row(2, "Phar-based phar archives", "enabled");
  3081. php_info_print_table_row(2, "Tar-based phar archives", "enabled");
  3082. php_info_print_table_row(2, "ZIP-based phar archives", "enabled");
  3083. if (PHAR_G(has_zlib)) {
  3084. php_info_print_table_row(2, "gzip compression", "enabled");
  3085. } else {
  3086. php_info_print_table_row(2, "gzip compression", "disabled (install ext/zlib)");
  3087. }
  3088. if (PHAR_G(has_bz2)) {
  3089. php_info_print_table_row(2, "bzip2 compression", "enabled");
  3090. } else {
  3091. php_info_print_table_row(2, "bzip2 compression", "disabled (install ext/bz2)");
  3092. }
  3093. #ifdef PHAR_HAVE_OPENSSL
  3094. php_info_print_table_row(2, "Native OpenSSL support", "enabled");
  3095. #else
  3096. if (zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) {
  3097. php_info_print_table_row(2, "OpenSSL support", "enabled");
  3098. } else {
  3099. php_info_print_table_row(2, "OpenSSL support", "disabled (install ext/openssl)");
  3100. }
  3101. #endif
  3102. php_info_print_table_end();
  3103. php_info_print_box_start(0);
  3104. PUTS("Phar based on pear/PHP_Archive, original concept by Davey Shafik.");
  3105. PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
  3106. PUTS("Phar fully realized by Gregory Beaver and Marcus Boerger.");
  3107. PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
  3108. PUTS("Portions of tar implementation Copyright (c) 2003-2009 Tim Kientzle.");
  3109. php_info_print_box_end();
  3110. DISPLAY_INI_ENTRIES();
  3111. }
  3112. /* }}} */
  3113. /* {{{ phar_module_entry
  3114. */
  3115. static const zend_module_dep phar_deps[] = {
  3116. ZEND_MOD_OPTIONAL("apc")
  3117. ZEND_MOD_OPTIONAL("bz2")
  3118. ZEND_MOD_OPTIONAL("openssl")
  3119. ZEND_MOD_OPTIONAL("zlib")
  3120. ZEND_MOD_OPTIONAL("standard")
  3121. #if defined(HAVE_HASH) && !defined(COMPILE_DL_HASH)
  3122. ZEND_MOD_REQUIRED("hash")
  3123. #endif
  3124. ZEND_MOD_REQUIRED("spl")
  3125. ZEND_MOD_END
  3126. };
  3127. zend_module_entry phar_module_entry = {
  3128. STANDARD_MODULE_HEADER_EX, NULL,
  3129. phar_deps,
  3130. "Phar",
  3131. phar_functions,
  3132. PHP_MINIT(phar),
  3133. PHP_MSHUTDOWN(phar),
  3134. NULL,
  3135. PHP_RSHUTDOWN(phar),
  3136. PHP_MINFO(phar),
  3137. PHP_PHAR_VERSION,
  3138. PHP_MODULE_GLOBALS(phar), /* globals descriptor */
  3139. PHP_GINIT(phar), /* globals ctor */
  3140. PHP_GSHUTDOWN(phar), /* globals dtor */
  3141. NULL, /* post deactivate */
  3142. STANDARD_MODULE_PROPERTIES_EX
  3143. };
  3144. /* }}} */
  3145. /*
  3146. * Local variables:
  3147. * tab-width: 4
  3148. * c-basic-offset: 4
  3149. * End:
  3150. * vim600: noet sw=4 ts=4 fdm=marker
  3151. * vim<600: noet sw=4 ts=4
  3152. */