zip.php 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786
  1. <?php
  2. if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
  3. define('PCLZIP_READ_BLOCK_SIZE', 2048);
  4. }
  5. if (!defined('PCLZIP_SEPARATOR')) {
  6. define('PCLZIP_SEPARATOR', ',');
  7. }
  8. if (!defined('PCLZIP_ERROR_EXTERNAL')) {
  9. define('PCLZIP_ERROR_EXTERNAL', 0);
  10. }
  11. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  12. define('PCLZIP_TEMPORARY_DIR', '');
  13. }
  14. if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
  15. define('PCLZIP_TEMPORARY_FILE_RATIO', 0.47);
  16. }
  17. $g_pclzip_version = "2.8.2";
  18. define('PCLZIP_ERR_USER_ABORTED', 2);
  19. define('PCLZIP_ERR_NO_ERROR', 0);
  20. define('PCLZIP_ERR_WRITE_OPEN_FAIL', -1);
  21. define('PCLZIP_ERR_READ_OPEN_FAIL', -2);
  22. define('PCLZIP_ERR_INVALID_PARAMETER', -3);
  23. define('PCLZIP_ERR_MISSING_FILE', -4);
  24. define('PCLZIP_ERR_FILENAME_TOO_LONG', -5);
  25. define('PCLZIP_ERR_INVALID_ZIP', -6);
  26. define('PCLZIP_ERR_BAD_EXTRACTED_FILE', -7);
  27. define('PCLZIP_ERR_DIR_CREATE_FAIL', -8);
  28. define('PCLZIP_ERR_BAD_EXTENSION', -9);
  29. define('PCLZIP_ERR_BAD_FORMAT', -10);
  30. define('PCLZIP_ERR_DELETE_FILE_FAIL', -11);
  31. define('PCLZIP_ERR_RENAME_FILE_FAIL', -12);
  32. define('PCLZIP_ERR_BAD_CHECKSUM', -13);
  33. define('PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14);
  34. define('PCLZIP_ERR_MISSING_OPTION_VALUE', -15);
  35. define('PCLZIP_ERR_INVALID_OPTION_VALUE', -16);
  36. define('PCLZIP_ERR_ALREADY_A_DIRECTORY', -17);
  37. define('PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18);
  38. define('PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19);
  39. define('PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20);
  40. define('PCLZIP_ERR_DIRECTORY_RESTRICTION', -21);
  41. define('PCLZIP_OPT_PATH', 77001);
  42. define('PCLZIP_OPT_ADD_PATH', 77002);
  43. define('PCLZIP_OPT_REMOVE_PATH', 77003);
  44. define('PCLZIP_OPT_REMOVE_ALL_PATH', 77004);
  45. define('PCLZIP_OPT_SET_CHMOD', 77005);
  46. define('PCLZIP_OPT_EXTRACT_AS_STRING', 77006);
  47. define('PCLZIP_OPT_NO_COMPRESSION', 77007);
  48. define('PCLZIP_OPT_BY_NAME', 77008);
  49. define('PCLZIP_OPT_BY_INDEX', 77009);
  50. define('PCLZIP_OPT_BY_EREG', 77010);
  51. define('PCLZIP_OPT_BY_PREG', 77011);
  52. define('PCLZIP_OPT_COMMENT', 77012);
  53. define('PCLZIP_OPT_ADD_COMMENT', 77013);
  54. define('PCLZIP_OPT_PREPEND_COMMENT', 77014);
  55. define('PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015);
  56. define('PCLZIP_OPT_REPLACE_NEWER', 77016);
  57. define('PCLZIP_OPT_STOP_ON_ERROR', 77017);
  58. define('PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019);
  59. define('PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020);
  60. define('PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020);
  61. define('PCLZIP_OPT_TEMP_FILE_ON', 77021);
  62. define('PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021);
  63. define('PCLZIP_OPT_TEMP_FILE_OFF', 77022);
  64. define('PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022);
  65. define('PCLZIP_ATT_FILE_NAME', 79001);
  66. define('PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002);
  67. define('PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003);
  68. define('PCLZIP_ATT_FILE_MTIME', 79004);
  69. define('PCLZIP_ATT_FILE_CONTENT', 79005);
  70. define('PCLZIP_ATT_FILE_COMMENT', 79006);
  71. define('PCLZIP_CB_PRE_EXTRACT', 78001);
  72. define('PCLZIP_CB_POST_EXTRACT', 78002);
  73. define('PCLZIP_CB_PRE_ADD', 78003);
  74. define('PCLZIP_CB_POST_ADD', 78004);
  75. class PclZip
  76. {
  77. var $zipname = '';
  78. var $zip_fd = 0;
  79. var $error_code = 1;
  80. var $error_string = '';
  81. var $magic_quotes_status;
  82. function __construct($p_zipname)
  83. {
  84. if (!function_exists('gzopen')) {
  85. die('Abort ' . basename(__FILE__) . ' : Missing zlib extensions');
  86. }
  87. $this->zipname = $p_zipname;
  88. $this->zip_fd = 0;
  89. $this->magic_quotes_status = -1;
  90. return;
  91. }
  92. function create($p_filelist)
  93. {
  94. $v_result = 1;
  95. $this->privErrorReset();
  96. $v_options = array();
  97. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  98. $v_size = func_num_args();
  99. if ($v_size > 1) {
  100. $v_arg_list = func_get_args();
  101. array_shift($v_arg_list);
  102. $v_size--;
  103. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  104. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  105. array(PCLZIP_OPT_REMOVE_PATH => 'optional',
  106. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  107. PCLZIP_OPT_ADD_PATH => 'optional',
  108. PCLZIP_CB_PRE_ADD => 'optional',
  109. PCLZIP_CB_POST_ADD => 'optional',
  110. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  111. PCLZIP_OPT_COMMENT => 'optional',
  112. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  113. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  114. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  115. ));
  116. if ($v_result != 1) {
  117. return 0;
  118. }
  119. } else {
  120. $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
  121. if ($v_size == 2) {
  122. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  123. } else if ($v_size > 2) {
  124. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  125. "Invalid number / type of arguments");
  126. return 0;
  127. }
  128. }
  129. }
  130. $this->privOptionDefaultThreshold($v_options);
  131. $v_string_list = array();
  132. $v_att_list = array();
  133. $v_filedescr_list = array();
  134. $p_result_list = array();
  135. if (is_array($p_filelist)) {
  136. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  137. $v_att_list = $p_filelist;
  138. } else {
  139. $v_string_list = $p_filelist;
  140. }
  141. } else if (is_string($p_filelist)) {
  142. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  143. } else {
  144. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  145. return 0;
  146. }
  147. if (sizeof($v_string_list) != 0) {
  148. foreach ($v_string_list as $v_string) {
  149. if ($v_string != '') {
  150. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  151. } else {
  152. }
  153. }
  154. }
  155. $v_supported_attributes
  156. = array(PCLZIP_ATT_FILE_NAME => 'mandatory'
  157. , PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
  158. , PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
  159. , PCLZIP_ATT_FILE_MTIME => 'optional'
  160. , PCLZIP_ATT_FILE_CONTENT => 'optional'
  161. , PCLZIP_ATT_FILE_COMMENT => 'optional'
  162. );
  163. foreach ($v_att_list as $v_entry) {
  164. $v_result = $this->privFileDescrParseAtt($v_entry,
  165. $v_filedescr_list[],
  166. $v_options,
  167. $v_supported_attributes);
  168. if ($v_result != 1) {
  169. return 0;
  170. }
  171. }
  172. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  173. if ($v_result != 1) {
  174. return 0;
  175. }
  176. $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
  177. if ($v_result != 1) {
  178. return 0;
  179. }
  180. return $p_result_list;
  181. }
  182. function add($p_filelist)
  183. {
  184. $v_result = 1;
  185. $this->privErrorReset();
  186. $v_options = array();
  187. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  188. $v_size = func_num_args();
  189. if ($v_size > 1) {
  190. $v_arg_list = func_get_args();
  191. array_shift($v_arg_list);
  192. $v_size--;
  193. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  194. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  195. array(PCLZIP_OPT_REMOVE_PATH => 'optional',
  196. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  197. PCLZIP_OPT_ADD_PATH => 'optional',
  198. PCLZIP_CB_PRE_ADD => 'optional',
  199. PCLZIP_CB_POST_ADD => 'optional',
  200. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  201. PCLZIP_OPT_COMMENT => 'optional',
  202. PCLZIP_OPT_ADD_COMMENT => 'optional',
  203. PCLZIP_OPT_PREPEND_COMMENT => 'optional',
  204. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  205. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  206. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  207. ));
  208. if ($v_result != 1) {
  209. return 0;
  210. }
  211. } else {
  212. $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
  213. if ($v_size == 2) {
  214. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  215. } else if ($v_size > 2) {
  216. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  217. return 0;
  218. }
  219. }
  220. }
  221. $this->privOptionDefaultThreshold($v_options);
  222. $v_string_list = array();
  223. $v_att_list = array();
  224. $v_filedescr_list = array();
  225. $p_result_list = array();
  226. if (is_array($p_filelist)) {
  227. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  228. $v_att_list = $p_filelist;
  229. } else {
  230. $v_string_list = $p_filelist;
  231. }
  232. } else if (is_string($p_filelist)) {
  233. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  234. } else {
  235. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '" . gettype($p_filelist) . "' for p_filelist");
  236. return 0;
  237. }
  238. if (sizeof($v_string_list) != 0) {
  239. foreach ($v_string_list as $v_string) {
  240. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  241. }
  242. }
  243. $v_supported_attributes
  244. = array(PCLZIP_ATT_FILE_NAME => 'mandatory'
  245. , PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
  246. , PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
  247. , PCLZIP_ATT_FILE_MTIME => 'optional'
  248. , PCLZIP_ATT_FILE_CONTENT => 'optional'
  249. , PCLZIP_ATT_FILE_COMMENT => 'optional'
  250. );
  251. foreach ($v_att_list as $v_entry) {
  252. $v_result = $this->privFileDescrParseAtt($v_entry,
  253. $v_filedescr_list[],
  254. $v_options,
  255. $v_supported_attributes);
  256. if ($v_result != 1) {
  257. return 0;
  258. }
  259. }
  260. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  261. if ($v_result != 1) {
  262. return 0;
  263. }
  264. $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
  265. if ($v_result != 1) {
  266. return 0;
  267. }
  268. return $p_result_list;
  269. }
  270. function listContent()
  271. {
  272. $v_result = 1;
  273. $this->privErrorReset();
  274. if (!$this->privCheckFormat()) {
  275. return (0);
  276. }
  277. $p_list = array();
  278. if (($v_result = $this->privList($p_list)) != 1) {
  279. unset($p_list);
  280. return (0);
  281. }
  282. return $p_list;
  283. }
  284. function extract()
  285. {
  286. $v_result = 1;
  287. $this->privErrorReset();
  288. if (!$this->privCheckFormat()) {
  289. return (0);
  290. }
  291. $v_options = array();
  292. $v_path = '';
  293. $v_remove_path = "";
  294. $v_remove_all_path = false;
  295. $v_size = func_num_args();
  296. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  297. if ($v_size > 0) {
  298. $v_arg_list = func_get_args();
  299. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  300. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  301. array(PCLZIP_OPT_PATH => 'optional',
  302. PCLZIP_OPT_REMOVE_PATH => 'optional',
  303. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  304. PCLZIP_OPT_ADD_PATH => 'optional',
  305. PCLZIP_CB_PRE_EXTRACT => 'optional',
  306. PCLZIP_CB_POST_EXTRACT => 'optional',
  307. PCLZIP_OPT_SET_CHMOD => 'optional',
  308. PCLZIP_OPT_BY_NAME => 'optional',
  309. PCLZIP_OPT_BY_EREG => 'optional',
  310. PCLZIP_OPT_BY_PREG => 'optional',
  311. PCLZIP_OPT_BY_INDEX => 'optional',
  312. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  313. PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
  314. PCLZIP_OPT_REPLACE_NEWER => 'optional'
  315. , PCLZIP_OPT_STOP_ON_ERROR => 'optional'
  316. , PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  317. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  318. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  319. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  320. ));
  321. if ($v_result != 1) {
  322. return 0;
  323. }
  324. if (isset($v_options[PCLZIP_OPT_PATH])) {
  325. $v_path = $v_options[PCLZIP_OPT_PATH];
  326. }
  327. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  328. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  329. }
  330. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  331. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  332. }
  333. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  334. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  335. $v_path .= '/';
  336. }
  337. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  338. }
  339. } else {
  340. $v_path = $v_arg_list[0];
  341. if ($v_size == 2) {
  342. $v_remove_path = $v_arg_list[1];
  343. } else if ($v_size > 2) {
  344. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  345. return 0;
  346. }
  347. }
  348. }
  349. $this->privOptionDefaultThreshold($v_options);
  350. $p_list = array();
  351. $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
  352. $v_remove_all_path, $v_options);
  353. if ($v_result < 1) {
  354. unset($p_list);
  355. return (0);
  356. }
  357. return $p_list;
  358. }
  359. function extractByIndex($p_index)
  360. {
  361. $v_result = 1;
  362. $this->privErrorReset();
  363. if (!$this->privCheckFormat()) {
  364. return (0);
  365. }
  366. $v_options = array();
  367. $v_path = '';
  368. $v_remove_path = "";
  369. $v_remove_all_path = false;
  370. $v_size = func_num_args();
  371. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  372. if ($v_size > 1) {
  373. $v_arg_list = func_get_args();
  374. array_shift($v_arg_list);
  375. $v_size--;
  376. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  377. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  378. array(PCLZIP_OPT_PATH => 'optional',
  379. PCLZIP_OPT_REMOVE_PATH => 'optional',
  380. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  381. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  382. PCLZIP_OPT_ADD_PATH => 'optional',
  383. PCLZIP_CB_PRE_EXTRACT => 'optional',
  384. PCLZIP_CB_POST_EXTRACT => 'optional',
  385. PCLZIP_OPT_SET_CHMOD => 'optional',
  386. PCLZIP_OPT_REPLACE_NEWER => 'optional'
  387. , PCLZIP_OPT_STOP_ON_ERROR => 'optional'
  388. , PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  389. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  390. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  391. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  392. ));
  393. if ($v_result != 1) {
  394. return 0;
  395. }
  396. if (isset($v_options[PCLZIP_OPT_PATH])) {
  397. $v_path = $v_options[PCLZIP_OPT_PATH];
  398. }
  399. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  400. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  401. }
  402. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  403. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  404. }
  405. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  406. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  407. $v_path .= '/';
  408. }
  409. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  410. }
  411. if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
  412. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  413. } else {
  414. }
  415. } else {
  416. $v_path = $v_arg_list[0];
  417. if ($v_size == 2) {
  418. $v_remove_path = $v_arg_list[1];
  419. } else if ($v_size > 2) {
  420. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  421. return 0;
  422. }
  423. }
  424. }
  425. $v_arg_trick = array(PCLZIP_OPT_BY_INDEX, $p_index);
  426. $v_options_trick = array();
  427. $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
  428. array(PCLZIP_OPT_BY_INDEX => 'optional'));
  429. if ($v_result != 1) {
  430. return 0;
  431. }
  432. $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
  433. $this->privOptionDefaultThreshold($v_options);
  434. if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
  435. return (0);
  436. }
  437. return $p_list;
  438. }
  439. function delete()
  440. {
  441. $v_result = 1;
  442. $this->privErrorReset();
  443. if (!$this->privCheckFormat()) {
  444. return (0);
  445. }
  446. $v_options = array();
  447. $v_size = func_num_args();
  448. if ($v_size > 0) {
  449. $v_arg_list = func_get_args();
  450. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  451. array(PCLZIP_OPT_BY_NAME => 'optional',
  452. PCLZIP_OPT_BY_EREG => 'optional',
  453. PCLZIP_OPT_BY_PREG => 'optional',
  454. PCLZIP_OPT_BY_INDEX => 'optional'));
  455. if ($v_result != 1) {
  456. return 0;
  457. }
  458. }
  459. $this->privDisableMagicQuotes();
  460. $v_list = array();
  461. if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
  462. $this->privSwapBackMagicQuotes();
  463. unset($v_list);
  464. return (0);
  465. }
  466. $this->privSwapBackMagicQuotes();
  467. return $v_list;
  468. }
  469. function deleteByIndex($p_index)
  470. {
  471. $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  472. return $p_list;
  473. }
  474. function properties()
  475. {
  476. $this->privErrorReset();
  477. $this->privDisableMagicQuotes();
  478. if (!$this->privCheckFormat()) {
  479. $this->privSwapBackMagicQuotes();
  480. return (0);
  481. }
  482. $v_prop = array();
  483. $v_prop['comment'] = '';
  484. $v_prop['nb'] = 0;
  485. $v_prop['status'] = 'not_exist';
  486. if (@is_file($this->zipname)) {
  487. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
  488. $this->privSwapBackMagicQuotes();
  489. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in binary read mode');
  490. return 0;
  491. }
  492. $v_central_dir = array();
  493. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  494. $this->privSwapBackMagicQuotes();
  495. return 0;
  496. }
  497. $this->privCloseFd();
  498. $v_prop['comment'] = $v_central_dir['comment'];
  499. $v_prop['nb'] = $v_central_dir['entries'];
  500. $v_prop['status'] = 'ok';
  501. }
  502. $this->privSwapBackMagicQuotes();
  503. return $v_prop;
  504. }
  505. function duplicate($p_archive)
  506. {
  507. $v_result = 1;
  508. $this->privErrorReset();
  509. if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip')) {
  510. $v_result = $this->privDuplicate($p_archive->zipname);
  511. } else if (is_string($p_archive)) {
  512. if (!is_file($p_archive)) {
  513. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '" . $p_archive . "'");
  514. $v_result = PCLZIP_ERR_MISSING_FILE;
  515. } else {
  516. $v_result = $this->privDuplicate($p_archive);
  517. }
  518. } else {
  519. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  520. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  521. }
  522. return $v_result;
  523. }
  524. function merge($p_archive_to_add)
  525. {
  526. $v_result = 1;
  527. $this->privErrorReset();
  528. if (!$this->privCheckFormat()) {
  529. return (0);
  530. }
  531. if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip')) {
  532. $v_result = $this->privMerge($p_archive_to_add);
  533. } else if (is_string($p_archive_to_add)) {
  534. $v_object_archive = new PclZip($p_archive_to_add);
  535. $v_result = $this->privMerge($v_object_archive);
  536. } else {
  537. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  538. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  539. }
  540. return $v_result;
  541. }
  542. function errorCode()
  543. {
  544. if (PCLZIP_ERROR_EXTERNAL == 1) {
  545. return (PclErrorCode());
  546. } else {
  547. return ($this->error_code);
  548. }
  549. }
  550. function errorName($p_with_code = false)
  551. {
  552. $v_name = array(PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
  553. PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
  554. PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
  555. PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
  556. PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
  557. PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
  558. PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
  559. PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
  560. PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
  561. PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
  562. PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
  563. PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
  564. PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
  565. PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
  566. PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
  567. PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
  568. PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
  569. PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
  570. PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
  571. , PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
  572. , PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
  573. );
  574. if (isset($v_name[$this->error_code])) {
  575. $v_value = $v_name[$this->error_code];
  576. } else {
  577. $v_value = 'NoName';
  578. }
  579. if ($p_with_code) {
  580. return ($v_value . ' (' . $this->error_code . ')');
  581. } else {
  582. return ($v_value);
  583. }
  584. }
  585. function errorInfo($p_full = false)
  586. {
  587. if (PCLZIP_ERROR_EXTERNAL == 1) {
  588. return (PclErrorString());
  589. } else {
  590. if ($p_full) {
  591. return ($this->errorName(true) . " : " . $this->error_string);
  592. } else {
  593. return ($this->error_string . " [code " . $this->error_code . "]");
  594. }
  595. }
  596. }
  597. function privCheckFormat($p_level = 0)
  598. {
  599. $v_result = true;
  600. clearstatcache();
  601. $this->privErrorReset();
  602. if (!is_file($this->zipname)) {
  603. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '" . $this->zipname . "'");
  604. return (false);
  605. }
  606. if (!is_readable($this->zipname)) {
  607. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '" . $this->zipname . "'");
  608. return (false);
  609. }
  610. return $v_result;
  611. }
  612. function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options = false)
  613. {
  614. $v_result = 1;
  615. $i = 0;
  616. while ($i < $p_size) {
  617. if (!isset($v_requested_options[$p_options_list[$i]])) {
  618. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '" . $p_options_list[$i] . "' for this method");
  619. return PclZip::errorCode();
  620. }
  621. switch ($p_options_list[$i]) {
  622. case PCLZIP_OPT_PATH :
  623. case PCLZIP_OPT_REMOVE_PATH :
  624. case PCLZIP_OPT_ADD_PATH :
  625. if (($i + 1) >= $p_size) {
  626. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  627. return PclZip::errorCode();
  628. }
  629. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i + 1], FALSE);
  630. $i++;
  631. break;
  632. case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
  633. if (($i + 1) >= $p_size) {
  634. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  635. return PclZip::errorCode();
  636. }
  637. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  638. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  639. return PclZip::errorCode();
  640. }
  641. $v_value = $p_options_list[$i + 1];
  642. if ((!is_integer($v_value)) || ($v_value < 0)) {
  643. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  644. return PclZip::errorCode();
  645. }
  646. $v_result_list[$p_options_list[$i]] = $v_value * 1048576;
  647. $i++;
  648. break;
  649. case PCLZIP_OPT_TEMP_FILE_ON :
  650. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  651. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  652. return PclZip::errorCode();
  653. }
  654. $v_result_list[$p_options_list[$i]] = true;
  655. break;
  656. case PCLZIP_OPT_TEMP_FILE_OFF :
  657. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
  658. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
  659. return PclZip::errorCode();
  660. }
  661. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  662. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
  663. return PclZip::errorCode();
  664. }
  665. $v_result_list[$p_options_list[$i]] = true;
  666. break;
  667. case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
  668. if (($i + 1) >= $p_size) {
  669. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  670. return PclZip::errorCode();
  671. }
  672. if (is_string($p_options_list[$i + 1])
  673. && ($p_options_list[$i + 1] != '')) {
  674. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i + 1], FALSE);
  675. $i++;
  676. } else {
  677. }
  678. break;
  679. case PCLZIP_OPT_BY_NAME :
  680. if (($i + 1) >= $p_size) {
  681. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  682. return PclZip::errorCode();
  683. }
  684. if (is_string($p_options_list[$i + 1])) {
  685. $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i + 1];
  686. } else if (is_array($p_options_list[$i + 1])) {
  687. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  688. } else {
  689. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  690. return PclZip::errorCode();
  691. }
  692. $i++;
  693. break;
  694. case PCLZIP_OPT_BY_EREG :
  695. $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
  696. case PCLZIP_OPT_BY_PREG :
  697. if (($i + 1) >= $p_size) {
  698. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  699. return PclZip::errorCode();
  700. }
  701. if (is_string($p_options_list[$i + 1])) {
  702. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  703. } else {
  704. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  705. return PclZip::errorCode();
  706. }
  707. $i++;
  708. break;
  709. case PCLZIP_OPT_COMMENT :
  710. case PCLZIP_OPT_ADD_COMMENT :
  711. case PCLZIP_OPT_PREPEND_COMMENT :
  712. if (($i + 1) >= $p_size) {
  713. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
  714. "Missing parameter value for option '"
  715. . PclZipUtilOptionText($p_options_list[$i])
  716. . "'");
  717. return PclZip::errorCode();
  718. }
  719. if (is_string($p_options_list[$i + 1])) {
  720. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  721. } else {
  722. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
  723. "Wrong parameter value for option '"
  724. . PclZipUtilOptionText($p_options_list[$i])
  725. . "'");
  726. return PclZip::errorCode();
  727. }
  728. $i++;
  729. break;
  730. case PCLZIP_OPT_BY_INDEX :
  731. if (($i + 1) >= $p_size) {
  732. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  733. return PclZip::errorCode();
  734. }
  735. $v_work_list = array();
  736. if (is_string($p_options_list[$i + 1])) {
  737. $p_options_list[$i + 1] = strtr($p_options_list[$i + 1], ' ', '');
  738. $v_work_list = explode(",", $p_options_list[$i + 1]);
  739. } else if (is_integer($p_options_list[$i + 1])) {
  740. $v_work_list[0] = $p_options_list[$i + 1] . '-' . $p_options_list[$i + 1];
  741. } else if (is_array($p_options_list[$i + 1])) {
  742. $v_work_list = $p_options_list[$i + 1];
  743. } else {
  744. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  745. return PclZip::errorCode();
  746. }
  747. $v_sort_flag = false;
  748. $v_sort_value = 0;
  749. for ($j = 0; $j < sizeof($v_work_list); $j++) {
  750. $v_item_list = explode("-", $v_work_list[$j]);
  751. $v_size_item_list = sizeof($v_item_list);
  752. if ($v_size_item_list == 1) {
  753. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  754. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  755. } elseif ($v_size_item_list == 2) {
  756. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  757. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
  758. } else {
  759. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  760. return PclZip::errorCode();
  761. }
  762. if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  763. $v_sort_flag = true;
  764. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  765. return PclZip::errorCode();
  766. }
  767. $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  768. }
  769. if ($v_sort_flag) {
  770. }
  771. $i++;
  772. break;
  773. case PCLZIP_OPT_REMOVE_ALL_PATH :
  774. case PCLZIP_OPT_EXTRACT_AS_STRING :
  775. case PCLZIP_OPT_NO_COMPRESSION :
  776. case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
  777. case PCLZIP_OPT_REPLACE_NEWER :
  778. case PCLZIP_OPT_STOP_ON_ERROR :
  779. $v_result_list[$p_options_list[$i]] = true;
  780. break;
  781. case PCLZIP_OPT_SET_CHMOD :
  782. if (($i + 1) >= $p_size) {
  783. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  784. return PclZip::errorCode();
  785. }
  786. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  787. $i++;
  788. break;
  789. case PCLZIP_CB_PRE_EXTRACT :
  790. case PCLZIP_CB_POST_EXTRACT :
  791. case PCLZIP_CB_PRE_ADD :
  792. case PCLZIP_CB_POST_ADD :
  793. if (($i + 1) >= $p_size) {
  794. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  795. return PclZip::errorCode();
  796. }
  797. $v_function_name = $p_options_list[$i + 1];
  798. if (!function_exists($v_function_name)) {
  799. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '" . $v_function_name . "()' is not an existing function for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  800. return PclZip::errorCode();
  801. }
  802. $v_result_list[$p_options_list[$i]] = $v_function_name;
  803. $i++;
  804. break;
  805. default :
  806. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  807. "Unknown parameter '"
  808. . $p_options_list[$i] . "'");
  809. return PclZip::errorCode();
  810. }
  811. $i++;
  812. }
  813. if ($v_requested_options !== false) {
  814. for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
  815. if ($v_requested_options[$key] == 'mandatory') {
  816. if (!isset($v_result_list[$key])) {
  817. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter " . PclZipUtilOptionText($key) . "(" . $key . ")");
  818. return PclZip::errorCode();
  819. }
  820. }
  821. }
  822. }
  823. if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  824. }
  825. return $v_result;
  826. }
  827. function privOptionDefaultThreshold(&$p_options)
  828. {
  829. $v_result = 1;
  830. if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  831. || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
  832. return $v_result;
  833. }
  834. $v_memory_limit = ini_get('memory_limit');
  835. $v_memory_limit = intval($v_memory_limit);
  836. $last = strtolower(substr($v_memory_limit, -1));
  837. if ($last == 'g')
  838. $v_memory_limit = intval($v_memory_limit) * 1073741824;
  839. if ($last == 'm')
  840. $v_memory_limit = intval($v_memory_limit) * 1048576;
  841. if ($last == 'k')
  842. $v_memory_limit = intval($v_memory_limit) * 1024;
  843. $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor(intval($v_memory_limit) * PCLZIP_TEMPORARY_FILE_RATIO);
  844. if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
  845. unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
  846. }
  847. return $v_result;
  848. }
  849. function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options = false)
  850. {
  851. $v_result = 1;
  852. foreach ($p_file_list as $v_key => $v_value) {
  853. if (!isset($v_requested_options[$v_key])) {
  854. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '" . $v_key . "' for this file");
  855. return PclZip::errorCode();
  856. }
  857. switch ($v_key) {
  858. case PCLZIP_ATT_FILE_NAME :
  859. if (!is_string($v_value)) {
  860. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  861. return PclZip::errorCode();
  862. }
  863. $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
  864. if ($p_filedescr['filename'] == '') {
  865. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  866. return PclZip::errorCode();
  867. }
  868. break;
  869. case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
  870. if (!is_string($v_value)) {
  871. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  872. return PclZip::errorCode();
  873. }
  874. $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
  875. if ($p_filedescr['new_short_name'] == '') {
  876. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  877. return PclZip::errorCode();
  878. }
  879. break;
  880. case PCLZIP_ATT_FILE_NEW_FULL_NAME :
  881. if (!is_string($v_value)) {
  882. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  883. return PclZip::errorCode();
  884. }
  885. $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
  886. if ($p_filedescr['new_full_name'] == '') {
  887. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  888. return PclZip::errorCode();
  889. }
  890. break;
  891. case PCLZIP_ATT_FILE_COMMENT :
  892. if (!is_string($v_value)) {
  893. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  894. return PclZip::errorCode();
  895. }
  896. $p_filedescr['comment'] = $v_value;
  897. break;
  898. case PCLZIP_ATT_FILE_MTIME :
  899. if (!is_integer($v_value)) {
  900. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". Integer expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  901. return PclZip::errorCode();
  902. }
  903. $p_filedescr['mtime'] = $v_value;
  904. break;
  905. case PCLZIP_ATT_FILE_CONTENT :
  906. $p_filedescr['content'] = $v_value;
  907. break;
  908. default :
  909. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  910. "Unknown parameter '" . $v_key . "'");
  911. return PclZip::errorCode();
  912. }
  913. if ($v_requested_options !== false) {
  914. for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
  915. if ($v_requested_options[$key] == 'mandatory') {
  916. if (!isset($p_file_list[$key])) {
  917. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter " . PclZipUtilOptionText($key) . "(" . $key . ")");
  918. return PclZip::errorCode();
  919. }
  920. }
  921. }
  922. }
  923. }
  924. return $v_result;
  925. }
  926. function privFileDescrExpand(&$p_filedescr_list, &$p_options)
  927. {
  928. $v_result = 1;
  929. $v_result_list = array();
  930. for ($i = 0; $i < sizeof($p_filedescr_list); $i++) {
  931. $v_descr = $p_filedescr_list[$i];
  932. $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
  933. $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
  934. if (file_exists($v_descr['filename'])) {
  935. if (@is_file($v_descr['filename'])) {
  936. $v_descr['type'] = 'file';
  937. } else if (@is_dir($v_descr['filename'])) {
  938. $v_descr['type'] = 'folder';
  939. } else if (@is_link($v_descr['filename'])) {
  940. continue;
  941. } else {
  942. continue;
  943. }
  944. } else if (isset($v_descr['content'])) {
  945. $v_descr['type'] = 'virtual_file';
  946. } else {
  947. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '" . $v_descr['filename'] . "' does not exist");
  948. return PclZip::errorCode();
  949. }
  950. $this->privCalculateStoredFilename($v_descr, $p_options);
  951. $v_result_list[sizeof($v_result_list)] = $v_descr;
  952. if ($v_descr['type'] == 'folder') {
  953. $v_dirlist_descr = array();
  954. $v_dirlist_nb = 0;
  955. if ($v_folder_handler = @opendir($v_descr['filename'])) {
  956. while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
  957. if (($v_item_handler == '.') || ($v_item_handler == '..')) {
  958. continue;
  959. }
  960. $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'] . '/' . $v_item_handler;
  961. if (($v_descr['stored_filename'] != $v_descr['filename'])
  962. && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
  963. if ($v_descr['stored_filename'] != '') {
  964. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'] . '/' . $v_item_handler;
  965. } else {
  966. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
  967. }
  968. }
  969. $v_dirlist_nb++;
  970. }
  971. @closedir($v_folder_handler);
  972. } else {
  973. }
  974. if ($v_dirlist_nb != 0) {
  975. if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
  976. return $v_result;
  977. }
  978. $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
  979. } else {
  980. }
  981. unset($v_dirlist_descr);
  982. }
  983. }
  984. $p_filedescr_list = $v_result_list;
  985. return $v_result;
  986. }
  987. function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
  988. {
  989. $v_result = 1;
  990. $v_list_detail = array();
  991. $this->privDisableMagicQuotes();
  992. if (($v_result = $this->privOpenFd('wb')) != 1) {
  993. return $v_result;
  994. }
  995. $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
  996. $this->privCloseFd();
  997. $this->privSwapBackMagicQuotes();
  998. return $v_result;
  999. }
  1000. function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
  1001. {
  1002. $v_result = 1;
  1003. $v_list_detail = array();
  1004. if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0)) {
  1005. $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
  1006. return $v_result;
  1007. }
  1008. $this->privDisableMagicQuotes();
  1009. if (($v_result = $this->privOpenFd('rb')) != 1) {
  1010. $this->privSwapBackMagicQuotes();
  1011. return $v_result;
  1012. }
  1013. $v_central_dir = array();
  1014. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  1015. $this->privCloseFd();
  1016. $this->privSwapBackMagicQuotes();
  1017. return $v_result;
  1018. }
  1019. @rewind($this->zip_fd);
  1020. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  1021. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  1022. $this->privCloseFd();
  1023. $this->privSwapBackMagicQuotes();
  1024. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');
  1025. return PclZip::errorCode();
  1026. }
  1027. $v_size = $v_central_dir['offset'];
  1028. while ($v_size != 0) {
  1029. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1030. $v_buffer = fread($this->zip_fd, $v_read_size);
  1031. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  1032. $v_size -= $v_read_size;
  1033. }
  1034. $v_swap = $this->zip_fd;
  1035. $this->zip_fd = $v_zip_temp_fd;
  1036. $v_zip_temp_fd = $v_swap;
  1037. $v_header_list = array();
  1038. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
  1039. fclose($v_zip_temp_fd);
  1040. $this->privCloseFd();
  1041. @unlink($v_zip_temp_name);
  1042. $this->privSwapBackMagicQuotes();
  1043. return $v_result;
  1044. }
  1045. $v_offset = @ftell($this->zip_fd);
  1046. $v_size = $v_central_dir['size'];
  1047. while ($v_size != 0) {
  1048. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1049. $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  1050. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1051. $v_size -= $v_read_size;
  1052. }
  1053. for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); $i++) {
  1054. if ($v_header_list[$i]['status'] == 'ok') {
  1055. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1056. fclose($v_zip_temp_fd);
  1057. $this->privCloseFd();
  1058. @unlink($v_zip_temp_name);
  1059. $this->privSwapBackMagicQuotes();
  1060. return $v_result;
  1061. }
  1062. $v_count++;
  1063. }
  1064. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1065. }
  1066. $v_comment = $v_central_dir['comment'];
  1067. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1068. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1069. }
  1070. if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
  1071. $v_comment = $v_comment . $p_options[PCLZIP_OPT_ADD_COMMENT];
  1072. }
  1073. if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
  1074. $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT] . $v_comment;
  1075. }
  1076. $v_size = @ftell($this->zip_fd) - $v_offset;
  1077. if (($v_result = $this->privWriteCentralHeader($v_count + $v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1) {
  1078. unset($v_header_list);
  1079. $this->privSwapBackMagicQuotes();
  1080. return $v_result;
  1081. }
  1082. $v_swap = $this->zip_fd;
  1083. $this->zip_fd = $v_zip_temp_fd;
  1084. $v_zip_temp_fd = $v_swap;
  1085. $this->privCloseFd();
  1086. @fclose($v_zip_temp_fd);
  1087. $this->privSwapBackMagicQuotes();
  1088. @unlink($this->zipname);
  1089. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  1090. return $v_result;
  1091. }
  1092. function privOpenFd($p_mode)
  1093. {
  1094. $v_result = 1;
  1095. if ($this->zip_fd != 0) {
  1096. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \'' . $this->zipname . '\' already open');
  1097. return PclZip::errorCode();
  1098. }
  1099. if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0) {
  1100. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in ' . $p_mode . ' mode');
  1101. return PclZip::errorCode();
  1102. }
  1103. return $v_result;
  1104. }
  1105. function privCloseFd()
  1106. {
  1107. $v_result = 1;
  1108. if ($this->zip_fd != 0)
  1109. @fclose($this->zip_fd);
  1110. $this->zip_fd = 0;
  1111. return $v_result;
  1112. }
  1113. function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
  1114. {
  1115. $v_result = 1;
  1116. $v_header_list = array();
  1117. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
  1118. return $v_result;
  1119. }
  1120. $v_offset = @ftell($this->zip_fd);
  1121. for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); $i++) {
  1122. if ($v_header_list[$i]['status'] == 'ok') {
  1123. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1124. return $v_result;
  1125. }
  1126. $v_count++;
  1127. }
  1128. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1129. }
  1130. $v_comment = '';
  1131. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1132. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1133. }
  1134. $v_size = @ftell($this->zip_fd) - $v_offset;
  1135. if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1) {
  1136. unset($v_header_list);
  1137. return $v_result;
  1138. }
  1139. return $v_result;
  1140. }
  1141. function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
  1142. {
  1143. $v_result = 1;
  1144. $v_header = array();
  1145. $v_nb = sizeof($p_result_list);
  1146. for ($j = 0; ($j < sizeof($p_filedescr_list)) && ($v_result == 1); $j++) {
  1147. $p_filedescr_list[$j]['filename']
  1148. = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
  1149. if ($p_filedescr_list[$j]['filename'] == "") {
  1150. continue;
  1151. }
  1152. if (($p_filedescr_list[$j]['type'] != 'virtual_file')
  1153. && (!file_exists($p_filedescr_list[$j]['filename']))) {
  1154. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '" . $p_filedescr_list[$j]['filename'] . "' does not exist");
  1155. return PclZip::errorCode();
  1156. }
  1157. if (($p_filedescr_list[$j]['type'] == 'file')
  1158. || ($p_filedescr_list[$j]['type'] == 'virtual_file')
  1159. || (($p_filedescr_list[$j]['type'] == 'folder')
  1160. && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])
  1161. || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))
  1162. ) {
  1163. $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
  1164. $p_options);
  1165. if ($v_result != 1) {
  1166. return $v_result;
  1167. }
  1168. $p_result_list[$v_nb++] = $v_header;
  1169. }
  1170. }
  1171. return $v_result;
  1172. }
  1173. function privAddFile($p_filedescr, &$p_header, &$p_options)
  1174. {
  1175. $v_result = 1;
  1176. $p_filename = $p_filedescr['filename'];
  1177. if ($p_filename == "") {
  1178. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  1179. return PclZip::errorCode();
  1180. }
  1181. clearstatcache();
  1182. $p_header['version'] = 20;
  1183. $p_header['version_extracted'] = 10;
  1184. $p_header['flag'] = 0;
  1185. $p_header['compression'] = 0;
  1186. $p_header['crc'] = 0;
  1187. $p_header['compressed_size'] = 0;
  1188. $p_header['filename_len'] = strlen($p_filename);
  1189. $p_header['extra_len'] = 0;
  1190. $p_header['disk'] = 0;
  1191. $p_header['internal'] = 0;
  1192. $p_header['offset'] = 0;
  1193. $p_header['filename'] = $p_filename;
  1194. $p_header['stored_filename'] = $p_filedescr['stored_filename'];
  1195. $p_header['extra'] = '';
  1196. $p_header['status'] = 'ok';
  1197. $p_header['index'] = -1;
  1198. if ($p_filedescr['type'] == 'file') {
  1199. $p_header['external'] = 0x00000000;
  1200. $p_header['size'] = filesize($p_filename);
  1201. } else if ($p_filedescr['type'] == 'folder') {
  1202. $p_header['external'] = 0x00000010;
  1203. $p_header['mtime'] = filemtime($p_filename);
  1204. $p_header['size'] = filesize($p_filename);
  1205. } else if ($p_filedescr['type'] == 'virtual_file') {
  1206. $p_header['external'] = 0x00000000;
  1207. $p_header['size'] = strlen($p_filedescr['content']);
  1208. }
  1209. if (isset($p_filedescr['mtime'])) {
  1210. $p_header['mtime'] = $p_filedescr['mtime'];
  1211. } else if ($p_filedescr['type'] == 'virtual_file') {
  1212. $p_header['mtime'] = time();
  1213. } else {
  1214. $p_header['mtime'] = filemtime($p_filename);
  1215. }
  1216. if (isset($p_filedescr['comment'])) {
  1217. $p_header['comment_len'] = strlen($p_filedescr['comment']);
  1218. $p_header['comment'] = $p_filedescr['comment'];
  1219. } else {
  1220. $p_header['comment_len'] = 0;
  1221. $p_header['comment'] = '';
  1222. }
  1223. if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  1224. $v_local_header = array();
  1225. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  1226. $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
  1227. if ($v_result == 0) {
  1228. $p_header['status'] = "skipped";
  1229. $v_result = 1;
  1230. }
  1231. if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  1232. $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  1233. }
  1234. }
  1235. if ($p_header['stored_filename'] == "") {
  1236. $p_header['status'] = "filtered";
  1237. }
  1238. if (strlen($p_header['stored_filename']) > 0xFF) {
  1239. $p_header['status'] = 'filename_too_long';
  1240. }
  1241. if ($p_header['status'] == 'ok') {
  1242. if ($p_filedescr['type'] == 'file') {
  1243. if ((!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  1244. && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  1245. || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1246. && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])))) {
  1247. $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
  1248. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  1249. return $v_result;
  1250. }
  1251. } else {
  1252. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1253. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1254. return PclZip::errorCode();
  1255. }
  1256. $v_content = @fread($v_file, $p_header['size']);
  1257. @fclose($v_file);
  1258. $p_header['crc'] = @crc32($v_content);
  1259. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  1260. $p_header['compressed_size'] = $p_header['size'];
  1261. $p_header['compression'] = 0;
  1262. } else {
  1263. $v_content = @gzdeflate($v_content);
  1264. $p_header['compressed_size'] = strlen($v_content);
  1265. $p_header['compression'] = 8;
  1266. }
  1267. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1268. @fclose($v_file);
  1269. return $v_result;
  1270. }
  1271. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  1272. }
  1273. } else if ($p_filedescr['type'] == 'virtual_file') {
  1274. $v_content = $p_filedescr['content'];
  1275. $p_header['crc'] = @crc32($v_content);
  1276. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  1277. $p_header['compressed_size'] = $p_header['size'];
  1278. $p_header['compression'] = 0;
  1279. } else {
  1280. $v_content = @gzdeflate($v_content);
  1281. $p_header['compressed_size'] = strlen($v_content);
  1282. $p_header['compression'] = 8;
  1283. }
  1284. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1285. @fclose($v_file);
  1286. return $v_result;
  1287. }
  1288. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  1289. } else if ($p_filedescr['type'] == 'folder') {
  1290. if (@substr($p_header['stored_filename'], -1) != '/') {
  1291. $p_header['stored_filename'] .= '/';
  1292. }
  1293. $p_header['size'] = 0;
  1294. $p_header['external'] = 0x00000010;
  1295. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1296. return $v_result;
  1297. }
  1298. }
  1299. }
  1300. if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  1301. $v_local_header = array();
  1302. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  1303. $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
  1304. if ($v_result == 0) {
  1305. $v_result = 1;
  1306. }
  1307. }
  1308. return $v_result;
  1309. }
  1310. function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
  1311. {
  1312. $v_result = PCLZIP_ERR_NO_ERROR;
  1313. $p_filename = $p_filedescr['filename'];
  1314. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1315. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1316. return PclZip::errorCode();
  1317. }
  1318. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
  1319. if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
  1320. fclose($v_file);
  1321. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
  1322. return PclZip::errorCode();
  1323. }
  1324. $v_size = filesize($p_filename);
  1325. while ($v_size != 0) {
  1326. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1327. $v_buffer = @fread($v_file, $v_read_size);
  1328. @gzputs($v_file_compressed, $v_buffer, $v_read_size);
  1329. $v_size -= $v_read_size;
  1330. }
  1331. @fclose($v_file);
  1332. @gzclose($v_file_compressed);
  1333. if (filesize($v_gzip_temp_name) < 18) {
  1334. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \'' . $v_gzip_temp_name . '\' has invalid filesize - should be minimum 18 bytes');
  1335. return PclZip::errorCode();
  1336. }
  1337. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  1338. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  1339. return PclZip::errorCode();
  1340. }
  1341. $v_binary_data = @fread($v_file_compressed, 10);
  1342. $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
  1343. $v_data_header['os'] = bin2hex($v_data_header['os']);
  1344. @fseek($v_file_compressed, filesize($v_gzip_temp_name) - 8);
  1345. $v_binary_data = @fread($v_file_compressed, 8);
  1346. $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
  1347. $p_header['compression'] = ord($v_data_header['cm']);
  1348. $p_header['crc'] = $v_data_footer['crc'];
  1349. $p_header['compressed_size'] = filesize($v_gzip_temp_name) - 18;
  1350. @fclose($v_file_compressed);
  1351. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1352. return $v_result;
  1353. }
  1354. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  1355. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  1356. return PclZip::errorCode();
  1357. }
  1358. fseek($v_file_compressed, 10);
  1359. $v_size = $p_header['compressed_size'];
  1360. while ($v_size != 0) {
  1361. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1362. $v_buffer = @fread($v_file_compressed, $v_read_size);
  1363. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1364. $v_size -= $v_read_size;
  1365. }
  1366. @fclose($v_file_compressed);
  1367. @unlink($v_gzip_temp_name);
  1368. return $v_result;
  1369. }
  1370. function privCalculateStoredFilename(&$p_filedescr, &$p_options)
  1371. {
  1372. $v_result = 1;
  1373. $p_filename = $p_filedescr['filename'];
  1374. if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
  1375. $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
  1376. } else {
  1377. $p_add_dir = '';
  1378. }
  1379. if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
  1380. $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
  1381. } else {
  1382. $p_remove_dir = '';
  1383. }
  1384. if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  1385. $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  1386. } else {
  1387. $p_remove_all_dir = 0;
  1388. }
  1389. if (isset($p_filedescr['new_full_name'])) {
  1390. $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
  1391. } else {
  1392. if (isset($p_filedescr['new_short_name'])) {
  1393. $v_path_info = pathinfo($p_filename);
  1394. $v_dir = '';
  1395. if ($v_path_info['dirname'] != '') {
  1396. $v_dir = $v_path_info['dirname'] . '/';
  1397. }
  1398. $v_stored_filename = $v_dir . $p_filedescr['new_short_name'];
  1399. } else {
  1400. $v_stored_filename = $p_filename;
  1401. }
  1402. if ($p_remove_all_dir) {
  1403. $v_stored_filename = basename($p_filename);
  1404. } else if ($p_remove_dir != "") {
  1405. if (substr($p_remove_dir, -1) != '/')
  1406. $p_remove_dir .= "/";
  1407. if ((substr($p_filename, 0, 2) == "./")
  1408. || (substr($p_remove_dir, 0, 2) == "./")) {
  1409. if ((substr($p_filename, 0, 2) == "./")
  1410. && (substr($p_remove_dir, 0, 2) != "./")) {
  1411. $p_remove_dir = "./" . $p_remove_dir;
  1412. }
  1413. if ((substr($p_filename, 0, 2) != "./")
  1414. && (substr($p_remove_dir, 0, 2) == "./")) {
  1415. $p_remove_dir = substr($p_remove_dir, 2);
  1416. }
  1417. }
  1418. $v_compare = PclZipUtilPathInclusion($p_remove_dir,
  1419. $v_stored_filename);
  1420. if ($v_compare > 0) {
  1421. if ($v_compare == 2) {
  1422. $v_stored_filename = "";
  1423. } else {
  1424. $v_stored_filename = substr($v_stored_filename,
  1425. strlen($p_remove_dir));
  1426. }
  1427. }
  1428. }
  1429. $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
  1430. if ($p_add_dir != "") {
  1431. if (substr($p_add_dir, -1) == "/")
  1432. $v_stored_filename = $p_add_dir . $v_stored_filename;
  1433. else
  1434. $v_stored_filename = $p_add_dir . "/" . $v_stored_filename;
  1435. }
  1436. }
  1437. $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  1438. $p_filedescr['stored_filename'] = $v_stored_filename;
  1439. return $v_result;
  1440. }
  1441. function privWriteFileHeader(&$p_header)
  1442. {
  1443. $v_result = 1;
  1444. $p_header['offset'] = ftell($this->zip_fd);
  1445. $v_date = getdate($p_header['mtime']);
  1446. $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
  1447. $v_mdate = (($v_date['year'] - 1980) << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
  1448. $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
  1449. $p_header['version_extracted'], $p_header['flag'],
  1450. $p_header['compression'], $v_mtime, $v_mdate,
  1451. $p_header['crc'], $p_header['compressed_size'],
  1452. $p_header['size'],
  1453. strlen($p_header['stored_filename']),
  1454. $p_header['extra_len']);
  1455. fputs($this->zip_fd, $v_binary_data, 30);
  1456. if (strlen($p_header['stored_filename']) != 0) {
  1457. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1458. }
  1459. if ($p_header['extra_len'] != 0) {
  1460. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  1461. }
  1462. return $v_result;
  1463. }
  1464. function privWriteCentralFileHeader(&$p_header)
  1465. {
  1466. $v_result = 1;
  1467. $v_date = getdate($p_header['mtime']);
  1468. $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
  1469. $v_mdate = (($v_date['year'] - 1980) << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
  1470. $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
  1471. $p_header['version'], $p_header['version_extracted'],
  1472. $p_header['flag'], $p_header['compression'],
  1473. $v_mtime, $v_mdate, $p_header['crc'],
  1474. $p_header['compressed_size'], $p_header['size'],
  1475. strlen($p_header['stored_filename']),
  1476. $p_header['extra_len'], $p_header['comment_len'],
  1477. $p_header['disk'], $p_header['internal'],
  1478. $p_header['external'], $p_header['offset']);
  1479. fputs($this->zip_fd, $v_binary_data, 46);
  1480. if (strlen($p_header['stored_filename']) != 0) {
  1481. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1482. }
  1483. if ($p_header['extra_len'] != 0) {
  1484. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  1485. }
  1486. if ($p_header['comment_len'] != 0) {
  1487. fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  1488. }
  1489. return $v_result;
  1490. }
  1491. function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  1492. {
  1493. $v_result = 1;
  1494. $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
  1495. $p_nb_entries, $p_size,
  1496. $p_offset, strlen($p_comment));
  1497. fputs($this->zip_fd, $v_binary_data, 22);
  1498. if (strlen($p_comment) != 0) {
  1499. fputs($this->zip_fd, $p_comment, strlen($p_comment));
  1500. }
  1501. return $v_result;
  1502. }
  1503. function privList(&$p_list)
  1504. {
  1505. $v_result = 1;
  1506. $this->privDisableMagicQuotes();
  1507. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
  1508. $this->privSwapBackMagicQuotes();
  1509. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in binary read mode');
  1510. return PclZip::errorCode();
  1511. }
  1512. $v_central_dir = array();
  1513. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  1514. $this->privSwapBackMagicQuotes();
  1515. return $v_result;
  1516. }
  1517. @rewind($this->zip_fd);
  1518. if (@fseek($this->zip_fd, $v_central_dir['offset'])) {
  1519. $this->privSwapBackMagicQuotes();
  1520. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1521. return PclZip::errorCode();
  1522. }
  1523. for ($i = 0; $i < $v_central_dir['entries']; $i++) {
  1524. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
  1525. $this->privSwapBackMagicQuotes();
  1526. return $v_result;
  1527. }
  1528. $v_header['index'] = $i;
  1529. $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
  1530. unset($v_header);
  1531. }
  1532. $this->privCloseFd();
  1533. $this->privSwapBackMagicQuotes();
  1534. return $v_result;
  1535. }
  1536. function privConvertHeader2FileInfo($p_header, &$p_info)
  1537. {
  1538. $v_result = 1;
  1539. $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
  1540. $p_info['filename'] = $v_temp_path;
  1541. $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
  1542. $p_info['stored_filename'] = $v_temp_path;
  1543. $p_info['size'] = $p_header['size'];
  1544. $p_info['compressed_size'] = $p_header['compressed_size'];
  1545. $p_info['mtime'] = $p_header['mtime'];
  1546. $p_info['comment'] = $p_header['comment'];
  1547. $p_info['folder'] = (($p_header['external'] & 0x00000010) == 0x00000010);
  1548. $p_info['index'] = $p_header['index'];
  1549. $p_info['status'] = $p_header['status'];
  1550. $p_info['crc'] = $p_header['crc'];
  1551. return $v_result;
  1552. }
  1553. function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  1554. {
  1555. $v_result = 1;
  1556. $this->privDisableMagicQuotes();
  1557. if (($p_path == "")
  1558. || ((substr($p_path, 0, 1) != "/")
  1559. && (substr($p_path, 0, 3) != "../")
  1560. && (substr($p_path, 1, 2) != ":/")))
  1561. $p_path = "./" . $p_path;
  1562. if (($p_path != "./") && ($p_path != "/")) {
  1563. while (substr($p_path, -1) == "/") {
  1564. $p_path = substr($p_path, 0, strlen($p_path) - 1);
  1565. }
  1566. }
  1567. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/')) {
  1568. $p_remove_path .= '/';
  1569. }
  1570. $p_remove_path_size = strlen($p_remove_path);
  1571. if (($v_result = $this->privOpenFd('rb')) != 1) {
  1572. $this->privSwapBackMagicQuotes();
  1573. return $v_result;
  1574. }
  1575. $v_central_dir = array();
  1576. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  1577. $this->privCloseFd();
  1578. $this->privSwapBackMagicQuotes();
  1579. return $v_result;
  1580. }
  1581. $v_pos_entry = $v_central_dir['offset'];
  1582. $j_start = 0;
  1583. for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
  1584. @rewind($this->zip_fd);
  1585. if (@fseek($this->zip_fd, $v_pos_entry)) {
  1586. $this->privCloseFd();
  1587. $this->privSwapBackMagicQuotes();
  1588. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1589. return PclZip::errorCode();
  1590. }
  1591. $v_header = array();
  1592. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
  1593. $this->privCloseFd();
  1594. $this->privSwapBackMagicQuotes();
  1595. return $v_result;
  1596. }
  1597. $v_header['index'] = $i;
  1598. $v_pos_entry = ftell($this->zip_fd);
  1599. $v_extract = false;
  1600. if ((isset($p_options[PCLZIP_OPT_BY_NAME]))
  1601. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  1602. for ($j = 0; ($j < sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  1603. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  1604. if ((strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  1605. && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  1606. $v_extract = true;
  1607. }
  1608. } elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  1609. $v_extract = true;
  1610. }
  1611. }
  1612. } else if ((isset($p_options[PCLZIP_OPT_BY_PREG]))
  1613. && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  1614. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  1615. $v_extract = true;
  1616. }
  1617. } else if ((isset($p_options[PCLZIP_OPT_BY_INDEX]))
  1618. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  1619. for ($j = $j_start; ($j < sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  1620. if (($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  1621. $v_extract = true;
  1622. }
  1623. if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  1624. $j_start = $j + 1;
  1625. }
  1626. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
  1627. break;
  1628. }
  1629. }
  1630. } else {
  1631. $v_extract = true;
  1632. }
  1633. if (($v_extract)
  1634. && (($v_header['compression'] != 8)
  1635. && ($v_header['compression'] != 0))) {
  1636. $v_header['status'] = 'unsupported_compression';
  1637. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1638. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  1639. $this->privSwapBackMagicQuotes();
  1640. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
  1641. "Filename '" . $v_header['stored_filename'] . "' is "
  1642. . "compressed by an unsupported compression "
  1643. . "method (" . $v_header['compression'] . ") ");
  1644. return PclZip::errorCode();
  1645. }
  1646. }
  1647. if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  1648. $v_header['status'] = 'unsupported_encryption';
  1649. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1650. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  1651. $this->privSwapBackMagicQuotes();
  1652. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
  1653. "Unsupported encryption for "
  1654. . " filename '" . $v_header['stored_filename']
  1655. . "'");
  1656. return PclZip::errorCode();
  1657. }
  1658. }
  1659. if (($v_extract) && ($v_header['status'] != 'ok')) {
  1660. $v_result = $this->privConvertHeader2FileInfo($v_header,
  1661. $p_file_list[$v_nb_extracted++]);
  1662. if ($v_result != 1) {
  1663. $this->privCloseFd();
  1664. $this->privSwapBackMagicQuotes();
  1665. return $v_result;
  1666. }
  1667. $v_extract = false;
  1668. }
  1669. if ($v_extract) {
  1670. @rewind($this->zip_fd);
  1671. if (@fseek($this->zip_fd, $v_header['offset'])) {
  1672. $this->privCloseFd();
  1673. $this->privSwapBackMagicQuotes();
  1674. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1675. return PclZip::errorCode();
  1676. }
  1677. if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  1678. $v_string = '';
  1679. $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
  1680. if ($v_result1 < 1) {
  1681. $this->privCloseFd();
  1682. $this->privSwapBackMagicQuotes();
  1683. return $v_result1;
  1684. }
  1685. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1) {
  1686. $this->privCloseFd();
  1687. $this->privSwapBackMagicQuotes();
  1688. return $v_result;
  1689. }
  1690. $p_file_list[$v_nb_extracted]['content'] = $v_string;
  1691. $v_nb_extracted++;
  1692. if ($v_result1 == 2) {
  1693. break;
  1694. }
  1695. } elseif ((isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
  1696. && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
  1697. $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  1698. if ($v_result1 < 1) {
  1699. $this->privCloseFd();
  1700. $this->privSwapBackMagicQuotes();
  1701. return $v_result1;
  1702. }
  1703. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  1704. $this->privCloseFd();
  1705. $this->privSwapBackMagicQuotes();
  1706. return $v_result;
  1707. }
  1708. if ($v_result1 == 2) {
  1709. break;
  1710. }
  1711. } else {
  1712. $v_result1 = $this->privExtractFile($v_header,
  1713. $p_path, $p_remove_path,
  1714. $p_remove_all_path,
  1715. $p_options);
  1716. if ($v_result1 < 1) {
  1717. $this->privCloseFd();
  1718. $this->privSwapBackMagicQuotes();
  1719. return $v_result1;
  1720. }
  1721. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  1722. $this->privCloseFd();
  1723. $this->privSwapBackMagicQuotes();
  1724. return $v_result;
  1725. }
  1726. if ($v_result1 == 2) {
  1727. break;
  1728. }
  1729. }
  1730. }
  1731. }
  1732. $this->privCloseFd();
  1733. $this->privSwapBackMagicQuotes();
  1734. return $v_result;
  1735. }
  1736. function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  1737. {
  1738. $v_result = 1;
  1739. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  1740. return $v_result;
  1741. }
  1742. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  1743. }
  1744. if ($p_remove_all_path == true) {
  1745. if (($p_entry['external'] & 0x00000010) == 0x00000010) {
  1746. $p_entry['status'] = "filtered";
  1747. return $v_result;
  1748. }
  1749. $p_entry['filename'] = basename($p_entry['filename']);
  1750. } else if ($p_remove_path != "") {
  1751. if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2) {
  1752. $p_entry['status'] = "filtered";
  1753. return $v_result;
  1754. }
  1755. $p_remove_path_size = strlen($p_remove_path);
  1756. if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
  1757. $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  1758. }
  1759. }
  1760. if ($p_path != '') {
  1761. $p_entry['filename'] = $p_path . "/" . $p_entry['filename'];
  1762. }
  1763. if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
  1764. $v_inclusion
  1765. = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
  1766. $p_entry['filename']);
  1767. if ($v_inclusion == 0) {
  1768. PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
  1769. "Filename '" . $p_entry['filename'] . "' is "
  1770. . "outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
  1771. return PclZip::errorCode();
  1772. }
  1773. }
  1774. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  1775. $v_local_header = array();
  1776. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1777. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  1778. if ($v_result == 0) {
  1779. $p_entry['status'] = "skipped";
  1780. $v_result = 1;
  1781. }
  1782. if ($v_result == 2) {
  1783. $p_entry['status'] = "aborted";
  1784. $v_result = PCLZIP_ERR_USER_ABORTED;
  1785. }
  1786. $p_entry['filename'] = $v_local_header['filename'];
  1787. }
  1788. if ($p_entry['status'] == 'ok') {
  1789. if (file_exists($p_entry['filename'])) {
  1790. if (is_dir($p_entry['filename'])) {
  1791. $p_entry['status'] = "already_a_directory";
  1792. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1793. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  1794. PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
  1795. "Filename '" . $p_entry['filename'] . "' is "
  1796. . "already used by an existing directory");
  1797. return PclZip::errorCode();
  1798. }
  1799. } else if (!is_writeable($p_entry['filename'])) {
  1800. $p_entry['status'] = "write_protected";
  1801. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1802. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  1803. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  1804. "Filename '" . $p_entry['filename'] . "' exists "
  1805. . "and is write protected");
  1806. return PclZip::errorCode();
  1807. }
  1808. } else if (filemtime($p_entry['filename']) > $p_entry['mtime']) {
  1809. if ((isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
  1810. && ($p_options[PCLZIP_OPT_REPLACE_NEWER] === true)) {
  1811. } else {
  1812. $p_entry['status'] = "newer_exist";
  1813. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1814. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  1815. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  1816. "Newer version of '" . $p_entry['filename'] . "' exists "
  1817. . "and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  1818. return PclZip::errorCode();
  1819. }
  1820. }
  1821. } else {
  1822. }
  1823. } else {
  1824. if ((($p_entry['external'] & 0x00000010) == 0x00000010) || (substr($p_entry['filename'], -1) == '/'))
  1825. $v_dir_to_check = $p_entry['filename'];
  1826. else if (!strstr($p_entry['filename'], "/"))
  1827. $v_dir_to_check = "";
  1828. else
  1829. $v_dir_to_check = dirname($p_entry['filename']);
  1830. if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external'] & 0x00000010) == 0x00000010))) != 1) {
  1831. $p_entry['status'] = "path_creation_fail";
  1832. $v_result = 1;
  1833. }
  1834. }
  1835. }
  1836. if ($p_entry['status'] == 'ok') {
  1837. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  1838. if ($p_entry['compression'] == 0) {
  1839. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1840. $p_entry['status'] = "write_error";
  1841. return $v_result;
  1842. }
  1843. $v_size = $p_entry['compressed_size'];
  1844. while ($v_size != 0) {
  1845. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1846. $v_buffer = @fread($this->zip_fd, $v_read_size);
  1847. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  1848. $v_size -= $v_read_size;
  1849. }
  1850. fclose($v_dest_file);
  1851. touch($p_entry['filename'], $p_entry['mtime']);
  1852. } else {
  1853. if (($p_entry['flag'] & 1) == 1) {
  1854. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \'' . $p_entry['filename'] . '\' is encrypted. Encrypted files are not supported.');
  1855. return PclZip::errorCode();
  1856. }
  1857. if ((!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  1858. && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  1859. || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1860. && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])))) {
  1861. $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
  1862. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  1863. return $v_result;
  1864. }
  1865. } else {
  1866. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1867. $v_file_content = @gzinflate($v_buffer);
  1868. unset($v_buffer);
  1869. if ($v_file_content === FALSE) {
  1870. $p_entry['status'] = "error";
  1871. return $v_result;
  1872. }
  1873. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1874. $p_entry['status'] = "write_error";
  1875. return $v_result;
  1876. }
  1877. @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  1878. unset($v_file_content);
  1879. @fclose($v_dest_file);
  1880. }
  1881. @touch($p_entry['filename'], $p_entry['mtime']);
  1882. }
  1883. if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  1884. @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  1885. }
  1886. }
  1887. }
  1888. if ($p_entry['status'] == "aborted") {
  1889. $p_entry['status'] = "skipped";
  1890. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  1891. $v_local_header = array();
  1892. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1893. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  1894. if ($v_result == 2) {
  1895. $v_result = PCLZIP_ERR_USER_ABORTED;
  1896. }
  1897. }
  1898. return $v_result;
  1899. }
  1900. function privExtractFileUsingTempFile(&$p_entry, &$p_options)
  1901. {
  1902. $v_result = 1;
  1903. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
  1904. if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
  1905. fclose($v_file);
  1906. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
  1907. return PclZip::errorCode();
  1908. }
  1909. $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
  1910. @fwrite($v_dest_file, $v_binary_data, 10);
  1911. $v_size = $p_entry['compressed_size'];
  1912. while ($v_size != 0) {
  1913. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1914. $v_buffer = @fread($this->zip_fd, $v_read_size);
  1915. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  1916. $v_size -= $v_read_size;
  1917. }
  1918. $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
  1919. @fwrite($v_dest_file, $v_binary_data, 8);
  1920. @fclose($v_dest_file);
  1921. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1922. $p_entry['status'] = "write_error";
  1923. return $v_result;
  1924. }
  1925. if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
  1926. @fclose($v_dest_file);
  1927. $p_entry['status'] = "read_error";
  1928. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  1929. return PclZip::errorCode();
  1930. }
  1931. $v_size = $p_entry['size'];
  1932. while ($v_size != 0) {
  1933. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1934. $v_buffer = @gzread($v_src_file, $v_read_size);
  1935. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  1936. $v_size -= $v_read_size;
  1937. }
  1938. @fclose($v_dest_file);
  1939. @gzclose($v_src_file);
  1940. @unlink($v_gzip_temp_name);
  1941. return $v_result;
  1942. }
  1943. function privExtractFileInOutput(&$p_entry, &$p_options)
  1944. {
  1945. $v_result = 1;
  1946. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  1947. return $v_result;
  1948. }
  1949. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  1950. }
  1951. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  1952. $v_local_header = array();
  1953. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1954. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  1955. if ($v_result == 0) {
  1956. $p_entry['status'] = "skipped";
  1957. $v_result = 1;
  1958. }
  1959. if ($v_result == 2) {
  1960. $p_entry['status'] = "aborted";
  1961. $v_result = PCLZIP_ERR_USER_ABORTED;
  1962. }
  1963. $p_entry['filename'] = $v_local_header['filename'];
  1964. }
  1965. if ($p_entry['status'] == 'ok') {
  1966. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  1967. if ($p_entry['compressed_size'] == $p_entry['size']) {
  1968. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1969. echo $v_buffer;
  1970. unset($v_buffer);
  1971. } else {
  1972. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1973. $v_file_content = gzinflate($v_buffer);
  1974. unset($v_buffer);
  1975. echo $v_file_content;
  1976. unset($v_file_content);
  1977. }
  1978. }
  1979. }
  1980. if ($p_entry['status'] == "aborted") {
  1981. $p_entry['status'] = "skipped";
  1982. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  1983. $v_local_header = array();
  1984. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1985. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  1986. if ($v_result == 2) {
  1987. $v_result = PCLZIP_ERR_USER_ABORTED;
  1988. }
  1989. }
  1990. return $v_result;
  1991. }
  1992. function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
  1993. {
  1994. $v_result = 1;
  1995. $v_header = array();
  1996. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  1997. return $v_result;
  1998. }
  1999. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  2000. }
  2001. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  2002. $v_local_header = array();
  2003. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2004. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  2005. if ($v_result == 0) {
  2006. $p_entry['status'] = "skipped";
  2007. $v_result = 1;
  2008. }
  2009. if ($v_result == 2) {
  2010. $p_entry['status'] = "aborted";
  2011. $v_result = PCLZIP_ERR_USER_ABORTED;
  2012. }
  2013. $p_entry['filename'] = $v_local_header['filename'];
  2014. }
  2015. if ($p_entry['status'] == 'ok') {
  2016. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  2017. if ($p_entry['compression'] == 0) {
  2018. $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
  2019. } else {
  2020. $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
  2021. if (($p_string = @gzinflate($v_data)) === FALSE) {
  2022. }
  2023. }
  2024. } else {
  2025. }
  2026. }
  2027. if ($p_entry['status'] == "aborted") {
  2028. $p_entry['status'] = "skipped";
  2029. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  2030. $v_local_header = array();
  2031. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2032. $v_local_header['content'] = $p_string;
  2033. $p_string = '';
  2034. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  2035. $p_string = $v_local_header['content'];
  2036. unset($v_local_header['content']);
  2037. if ($v_result == 2) {
  2038. $v_result = PCLZIP_ERR_USER_ABORTED;
  2039. }
  2040. }
  2041. return $v_result;
  2042. }
  2043. function privReadFileHeader(&$p_header)
  2044. {
  2045. $v_result = 1;
  2046. $v_binary_data = @fread($this->zip_fd, 4);
  2047. $v_data = unpack('Vid', $v_binary_data);
  2048. if ($v_data['id'] != 0x04034b50) {
  2049. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  2050. return PclZip::errorCode();
  2051. }
  2052. $v_binary_data = fread($this->zip_fd, 26);
  2053. if (strlen($v_binary_data) != 26) {
  2054. $p_header['filename'] = "";
  2055. $p_header['status'] = "invalid_header";
  2056. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));
  2057. return PclZip::errorCode();
  2058. }
  2059. $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  2060. $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  2061. if ($v_data['extra_len'] != 0) {
  2062. $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  2063. } else {
  2064. $p_header['extra'] = '';
  2065. }
  2066. $p_header['version_extracted'] = $v_data['version'];
  2067. $p_header['compression'] = $v_data['compression'];
  2068. $p_header['size'] = $v_data['size'];
  2069. $p_header['compressed_size'] = $v_data['compressed_size'];
  2070. $p_header['crc'] = $v_data['crc'];
  2071. $p_header['flag'] = $v_data['flag'];
  2072. $p_header['filename_len'] = $v_data['filename_len'];
  2073. $p_header['mdate'] = $v_data['mdate'];
  2074. $p_header['mtime'] = $v_data['mtime'];
  2075. if ($p_header['mdate'] && $p_header['mtime']) {
  2076. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  2077. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  2078. $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
  2079. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  2080. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  2081. $v_day = $p_header['mdate'] & 0x001F;
  2082. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  2083. } else {
  2084. $p_header['mtime'] = time();
  2085. }
  2086. $p_header['stored_filename'] = $p_header['filename'];
  2087. $p_header['status'] = "ok";
  2088. return $v_result;
  2089. }
  2090. function privReadCentralFileHeader(&$p_header)
  2091. {
  2092. $v_result = 1;
  2093. $v_binary_data = @fread($this->zip_fd, 4);
  2094. $v_data = unpack('Vid', $v_binary_data);
  2095. if ($v_data['id'] != 0x02014b50) {
  2096. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  2097. return PclZip::errorCode();
  2098. }
  2099. $v_binary_data = fread($this->zip_fd, 42);
  2100. if (strlen($v_binary_data) != 42) {
  2101. $p_header['filename'] = "";
  2102. $p_header['status'] = "invalid_header";
  2103. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));
  2104. return PclZip::errorCode();
  2105. }
  2106. $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
  2107. if ($p_header['filename_len'] != 0)
  2108. $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  2109. else
  2110. $p_header['filename'] = '';
  2111. if ($p_header['extra_len'] != 0)
  2112. $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  2113. else
  2114. $p_header['extra'] = '';
  2115. if ($p_header['comment_len'] != 0)
  2116. $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  2117. else
  2118. $p_header['comment'] = '';
  2119. if (1) {
  2120. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  2121. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  2122. $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
  2123. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  2124. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  2125. $v_day = $p_header['mdate'] & 0x001F;
  2126. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  2127. } else {
  2128. $p_header['mtime'] = time();
  2129. }
  2130. $p_header['stored_filename'] = $p_header['filename'];
  2131. $p_header['status'] = 'ok';
  2132. if (substr($p_header['filename'], -1) == '/') {
  2133. $p_header['external'] = 0x00000010;
  2134. }
  2135. return $v_result;
  2136. }
  2137. function privCheckFileHeaders(&$p_local_header, &$p_central_header)
  2138. {
  2139. $v_result = 1;
  2140. if ($p_local_header['filename'] != $p_central_header['filename']) {
  2141. }
  2142. if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  2143. }
  2144. if ($p_local_header['flag'] != $p_central_header['flag']) {
  2145. }
  2146. if ($p_local_header['compression'] != $p_central_header['compression']) {
  2147. }
  2148. if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  2149. }
  2150. if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  2151. }
  2152. if (($p_local_header['flag'] & 8) == 8) {
  2153. $p_local_header['size'] = $p_central_header['size'];
  2154. $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  2155. $p_local_header['crc'] = $p_central_header['crc'];
  2156. }
  2157. return $v_result;
  2158. }
  2159. function privReadEndCentralDir(&$p_central_dir)
  2160. {
  2161. $v_result = 1;
  2162. $v_size = filesize($this->zipname);
  2163. @fseek($this->zip_fd, $v_size);
  2164. if (@ftell($this->zip_fd) != $v_size) {
  2165. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \'' . $this->zipname . '\'');
  2166. return PclZip::errorCode();
  2167. }
  2168. $v_found = 0;
  2169. if ($v_size > 26) {
  2170. @fseek($this->zip_fd, $v_size - 22);
  2171. if (($v_pos = @ftell($this->zip_fd)) != ($v_size - 22)) {
  2172. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
  2173. return PclZip::errorCode();
  2174. }
  2175. $v_binary_data = @fread($this->zip_fd, 4);
  2176. $v_data = @unpack('Vid', $v_binary_data);
  2177. if ($v_data['id'] == 0x06054b50) {
  2178. $v_found = 1;
  2179. }
  2180. $v_pos = ftell($this->zip_fd);
  2181. }
  2182. if (!$v_found) {
  2183. $v_maximum_size = 65557;
  2184. if ($v_maximum_size > $v_size)
  2185. $v_maximum_size = $v_size;
  2186. @fseek($this->zip_fd, $v_size - $v_maximum_size);
  2187. if (@ftell($this->zip_fd) != ($v_size - $v_maximum_size)) {
  2188. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
  2189. return PclZip::errorCode();
  2190. }
  2191. $v_pos = ftell($this->zip_fd);
  2192. $v_bytes = 0x00000000;
  2193. while ($v_pos < $v_size) {
  2194. $v_byte = @fread($this->zip_fd, 1);
  2195. $v_bytes = (($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
  2196. if ($v_bytes == 0x504b0506) {
  2197. $v_pos++;
  2198. break;
  2199. }
  2200. $v_pos++;
  2201. }
  2202. if ($v_pos == $v_size) {
  2203. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  2204. return PclZip::errorCode();
  2205. }
  2206. }
  2207. $v_binary_data = fread($this->zip_fd, 18);
  2208. if (strlen($v_binary_data) != 18) {
  2209. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : " . strlen($v_binary_data));
  2210. return PclZip::errorCode();
  2211. }
  2212. $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  2213. if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  2214. if (0) {
  2215. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
  2216. 'The central dir is not at the end of the archive.'
  2217. . ' Some trailing bytes exists after the archive.');
  2218. return PclZip::errorCode();
  2219. }
  2220. }
  2221. if ($v_data['comment_size'] != 0) {
  2222. $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  2223. } else
  2224. $p_central_dir['comment'] = '';
  2225. $p_central_dir['entries'] = $v_data['entries'];
  2226. $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  2227. $p_central_dir['offset'] = $v_data['offset'];
  2228. $p_central_dir['size'] = $v_data['size'];
  2229. $p_central_dir['disk'] = $v_data['disk'];
  2230. $p_central_dir['disk_start'] = $v_data['disk_start'];
  2231. return $v_result;
  2232. }
  2233. function privDeleteByRule(&$p_result_list, &$p_options)
  2234. {
  2235. $v_result = 1;
  2236. $v_list_detail = array();
  2237. if (($v_result = $this->privOpenFd('rb')) != 1) {
  2238. return $v_result;
  2239. }
  2240. $v_central_dir = array();
  2241. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  2242. $this->privCloseFd();
  2243. return $v_result;
  2244. }
  2245. @rewind($this->zip_fd);
  2246. $v_pos_entry = $v_central_dir['offset'];
  2247. @rewind($this->zip_fd);
  2248. if (@fseek($this->zip_fd, $v_pos_entry)) {
  2249. $this->privCloseFd();
  2250. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2251. return PclZip::errorCode();
  2252. }
  2253. $v_header_list = array();
  2254. $j_start = 0;
  2255. for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
  2256. $v_header_list[$v_nb_extracted] = array();
  2257. if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1) {
  2258. $this->privCloseFd();
  2259. return $v_result;
  2260. }
  2261. $v_header_list[$v_nb_extracted]['index'] = $i;
  2262. $v_found = false;
  2263. if ((isset($p_options[PCLZIP_OPT_BY_NAME]))
  2264. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  2265. for ($j = 0; ($j < sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  2266. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  2267. if ((strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  2268. && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  2269. $v_found = true;
  2270. } elseif ((($v_header_list[$v_nb_extracted]['external'] & 0x00000010) == 0x00000010)
  2271. && ($v_header_list[$v_nb_extracted]['stored_filename'] . '/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  2272. $v_found = true;
  2273. }
  2274. } elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  2275. $v_found = true;
  2276. }
  2277. }
  2278. } else if ((isset($p_options[PCLZIP_OPT_BY_PREG]))
  2279. && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  2280. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  2281. $v_found = true;
  2282. }
  2283. } else if ((isset($p_options[PCLZIP_OPT_BY_INDEX]))
  2284. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  2285. for ($j = $j_start; ($j < sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  2286. if (($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  2287. $v_found = true;
  2288. }
  2289. if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  2290. $j_start = $j + 1;
  2291. }
  2292. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
  2293. break;
  2294. }
  2295. }
  2296. } else {
  2297. $v_found = true;
  2298. }
  2299. if ($v_found) {
  2300. unset($v_header_list[$v_nb_extracted]);
  2301. } else {
  2302. $v_nb_extracted++;
  2303. }
  2304. }
  2305. if ($v_nb_extracted > 0) {
  2306. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  2307. $v_temp_zip = new PclZip($v_zip_temp_name);
  2308. if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  2309. $this->privCloseFd();
  2310. return $v_result;
  2311. }
  2312. for ($i = 0; $i < sizeof($v_header_list); $i++) {
  2313. @rewind($this->zip_fd);
  2314. if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
  2315. $this->privCloseFd();
  2316. $v_temp_zip->privCloseFd();
  2317. @unlink($v_zip_temp_name);
  2318. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2319. return PclZip::errorCode();
  2320. }
  2321. $v_local_header = array();
  2322. if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  2323. $this->privCloseFd();
  2324. $v_temp_zip->privCloseFd();
  2325. @unlink($v_zip_temp_name);
  2326. return $v_result;
  2327. }
  2328. if ($this->privCheckFileHeaders($v_local_header,
  2329. $v_header_list[$i]) != 1) {
  2330. }
  2331. unset($v_local_header);
  2332. if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
  2333. $this->privCloseFd();
  2334. $v_temp_zip->privCloseFd();
  2335. @unlink($v_zip_temp_name);
  2336. return $v_result;
  2337. }
  2338. if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  2339. $this->privCloseFd();
  2340. $v_temp_zip->privCloseFd();
  2341. @unlink($v_zip_temp_name);
  2342. return $v_result;
  2343. }
  2344. }
  2345. $v_offset = @ftell($v_temp_zip->zip_fd);
  2346. for ($i = 0; $i < sizeof($v_header_list); $i++) {
  2347. if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  2348. $v_temp_zip->privCloseFd();
  2349. $this->privCloseFd();
  2350. @unlink($v_zip_temp_name);
  2351. return $v_result;
  2352. }
  2353. $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  2354. }
  2355. $v_comment = '';
  2356. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  2357. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  2358. }
  2359. $v_size = @ftell($v_temp_zip->zip_fd) - $v_offset;
  2360. if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
  2361. unset($v_header_list);
  2362. $v_temp_zip->privCloseFd();
  2363. $this->privCloseFd();
  2364. @unlink($v_zip_temp_name);
  2365. return $v_result;
  2366. }
  2367. $v_temp_zip->privCloseFd();
  2368. $this->privCloseFd();
  2369. @unlink($this->zipname);
  2370. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  2371. unset($v_temp_zip);
  2372. } else if ($v_central_dir['entries'] != 0) {
  2373. $this->privCloseFd();
  2374. if (($v_result = $this->privOpenFd('wb')) != 1) {
  2375. return $v_result;
  2376. }
  2377. if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  2378. return $v_result;
  2379. }
  2380. $this->privCloseFd();
  2381. }
  2382. return $v_result;
  2383. }
  2384. function privDirCheck($p_dir, $p_is_dir = false)
  2385. {
  2386. $v_result = 1;
  2387. if (($p_is_dir) && (substr($p_dir, -1) == '/')) {
  2388. $p_dir = substr($p_dir, 0, strlen($p_dir) - 1);
  2389. }
  2390. if ((is_dir($p_dir)) || ($p_dir == "")) {
  2391. return 1;
  2392. }
  2393. $p_parent_dir = dirname($p_dir);
  2394. if ($p_parent_dir != $p_dir) {
  2395. if ($p_parent_dir != "") {
  2396. if (($v_result = $this->privDirCheck($p_parent_dir)) != 1) {
  2397. return $v_result;
  2398. }
  2399. }
  2400. }
  2401. if (!@mkdir($p_dir, 0777)) {
  2402. PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  2403. return PclZip::errorCode();
  2404. }
  2405. return $v_result;
  2406. }
  2407. function privMerge(&$p_archive_to_add)
  2408. {
  2409. $v_result = 1;
  2410. if (!is_file($p_archive_to_add->zipname)) {
  2411. $v_result = 1;
  2412. return $v_result;
  2413. }
  2414. if (!is_file($this->zipname)) {
  2415. $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  2416. return $v_result;
  2417. }
  2418. if (($v_result = $this->privOpenFd('rb')) != 1) {
  2419. return $v_result;
  2420. }
  2421. $v_central_dir = array();
  2422. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  2423. $this->privCloseFd();
  2424. return $v_result;
  2425. }
  2426. @rewind($this->zip_fd);
  2427. if (($v_result = $p_archive_to_add->privOpenFd('rb')) != 1) {
  2428. $this->privCloseFd();
  2429. return $v_result;
  2430. }
  2431. $v_central_dir_to_add = array();
  2432. if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1) {
  2433. $this->privCloseFd();
  2434. $p_archive_to_add->privCloseFd();
  2435. return $v_result;
  2436. }
  2437. @rewind($p_archive_to_add->zip_fd);
  2438. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  2439. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  2440. $this->privCloseFd();
  2441. $p_archive_to_add->privCloseFd();
  2442. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');
  2443. return PclZip::errorCode();
  2444. }
  2445. $v_size = $v_central_dir['offset'];
  2446. while ($v_size != 0) {
  2447. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2448. $v_buffer = fread($this->zip_fd, $v_read_size);
  2449. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2450. $v_size -= $v_read_size;
  2451. }
  2452. $v_size = $v_central_dir_to_add['offset'];
  2453. while ($v_size != 0) {
  2454. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2455. $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  2456. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2457. $v_size -= $v_read_size;
  2458. }
  2459. $v_offset = @ftell($v_zip_temp_fd);
  2460. $v_size = $v_central_dir['size'];
  2461. while ($v_size != 0) {
  2462. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2463. $v_buffer = @fread($this->zip_fd, $v_read_size);
  2464. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2465. $v_size -= $v_read_size;
  2466. }
  2467. $v_size = $v_central_dir_to_add['size'];
  2468. while ($v_size != 0) {
  2469. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2470. $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  2471. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2472. $v_size -= $v_read_size;
  2473. }
  2474. $v_comment = $v_central_dir['comment'] . ' ' . $v_central_dir_to_add['comment'];
  2475. $v_size = @ftell($v_zip_temp_fd) - $v_offset;
  2476. $v_swap = $this->zip_fd;
  2477. $this->zip_fd = $v_zip_temp_fd;
  2478. $v_zip_temp_fd = $v_swap;
  2479. if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries'] + $v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1) {
  2480. $this->privCloseFd();
  2481. $p_archive_to_add->privCloseFd();
  2482. @fclose($v_zip_temp_fd);
  2483. $this->zip_fd = null;
  2484. unset($v_header_list);
  2485. return $v_result;
  2486. }
  2487. $v_swap = $this->zip_fd;
  2488. $this->zip_fd = $v_zip_temp_fd;
  2489. $v_zip_temp_fd = $v_swap;
  2490. $this->privCloseFd();
  2491. $p_archive_to_add->privCloseFd();
  2492. @fclose($v_zip_temp_fd);
  2493. @unlink($this->zipname);
  2494. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  2495. return $v_result;
  2496. }
  2497. function privDuplicate($p_archive_filename)
  2498. {
  2499. $v_result = 1;
  2500. if (!is_file($p_archive_filename)) {
  2501. $v_result = 1;
  2502. return $v_result;
  2503. }
  2504. if (($v_result = $this->privOpenFd('wb')) != 1) {
  2505. return $v_result;
  2506. }
  2507. if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
  2508. $this->privCloseFd();
  2509. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \'' . $p_archive_filename . '\' in binary write mode');
  2510. return PclZip::errorCode();
  2511. }
  2512. $v_size = filesize($p_archive_filename);
  2513. while ($v_size != 0) {
  2514. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2515. $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  2516. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2517. $v_size -= $v_read_size;
  2518. }
  2519. $this->privCloseFd();
  2520. @fclose($v_zip_temp_fd);
  2521. return $v_result;
  2522. }
  2523. function privErrorLog($p_error_code = 0, $p_error_string = '')
  2524. {
  2525. if (PCLZIP_ERROR_EXTERNAL == 1) {
  2526. PclError($p_error_code, $p_error_string);
  2527. } else {
  2528. $this->error_code = $p_error_code;
  2529. $this->error_string = $p_error_string;
  2530. }
  2531. }
  2532. function privErrorReset()
  2533. {
  2534. if (PCLZIP_ERROR_EXTERNAL == 1) {
  2535. PclErrorReset();
  2536. } else {
  2537. $this->error_code = 0;
  2538. $this->error_string = '';
  2539. }
  2540. }
  2541. function privDisableMagicQuotes()
  2542. {
  2543. $v_result = 1;
  2544. if ((!function_exists("get_magic_quotes_runtime"))
  2545. || (!function_exists("set_magic_quotes_runtime"))) {
  2546. return $v_result;
  2547. }
  2548. if ($this->magic_quotes_status != -1) {
  2549. return $v_result;
  2550. }
  2551. $this->magic_quotes_status = @get_magic_quotes_runtime();
  2552. if ($this->magic_quotes_status == 1) {
  2553. @set_magic_quotes_runtime(0);
  2554. }
  2555. return $v_result;
  2556. }
  2557. function privSwapBackMagicQuotes()
  2558. {
  2559. $v_result = 1;
  2560. if ((!function_exists("get_magic_quotes_runtime"))
  2561. || (!function_exists("set_magic_quotes_runtime"))) {
  2562. return $v_result;
  2563. }
  2564. if ($this->magic_quotes_status != -1) {
  2565. return $v_result;
  2566. }
  2567. if ($this->magic_quotes_status == 1) {
  2568. @set_magic_quotes_runtime($this->magic_quotes_status);
  2569. }
  2570. return $v_result;
  2571. }
  2572. }
  2573. function PclZipUtilPathReduction($p_dir)
  2574. {
  2575. $v_result = "";
  2576. if ($p_dir != "") {
  2577. $v_list = explode("/", $p_dir);
  2578. $v_skip = 0;
  2579. for ($i = sizeof($v_list) - 1; $i >= 0; $i--) {
  2580. if ($v_list[$i] == ".") {
  2581. } else if ($v_list[$i] == "..") {
  2582. $v_skip++;
  2583. } else if ($v_list[$i] == "") {
  2584. if ($i == 0) {
  2585. $v_result = "/" . $v_result;
  2586. if ($v_skip > 0) {
  2587. $v_result = $p_dir;
  2588. $v_skip = 0;
  2589. }
  2590. } else if ($i == (sizeof($v_list) - 1)) {
  2591. $v_result = $v_list[$i];
  2592. } else {
  2593. }
  2594. } else {
  2595. if ($v_skip > 0) {
  2596. $v_skip--;
  2597. } else {
  2598. $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? "/" . $v_result : "");
  2599. }
  2600. }
  2601. }
  2602. if ($v_skip > 0) {
  2603. while ($v_skip > 0) {
  2604. $v_result = '../' . $v_result;
  2605. $v_skip--;
  2606. }
  2607. }
  2608. }
  2609. return $v_result;
  2610. }
  2611. function PclZipUtilPathInclusion($p_dir, $p_path)
  2612. {
  2613. $v_result = 1;
  2614. if (($p_dir == '.')
  2615. || ((strlen($p_dir) >= 2) && (substr($p_dir, 0, 2) == './'))) {
  2616. $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE) . '/' . substr($p_dir, 1);
  2617. }
  2618. if (($p_path == '.')
  2619. || ((strlen($p_path) >= 2) && (substr($p_path, 0, 2) == './'))) {
  2620. $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE) . '/' . substr($p_path, 1);
  2621. }
  2622. $v_list_dir = explode("/", $p_dir);
  2623. $v_list_dir_size = sizeof($v_list_dir);
  2624. $v_list_path = explode("/", $p_path);
  2625. $v_list_path_size = sizeof($v_list_path);
  2626. $i = 0;
  2627. $j = 0;
  2628. while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  2629. if ($v_list_dir[$i] == '') {
  2630. $i++;
  2631. continue;
  2632. }
  2633. if ($v_list_path[$j] == '') {
  2634. $j++;
  2635. continue;
  2636. }
  2637. if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ($v_list_path[$j] != '')) {
  2638. $v_result = 0;
  2639. }
  2640. $i++;
  2641. $j++;
  2642. }
  2643. if ($v_result) {
  2644. while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
  2645. while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
  2646. if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  2647. $v_result = 2;
  2648. } else if ($i < $v_list_dir_size) {
  2649. $v_result = 0;
  2650. }
  2651. }
  2652. return $v_result;
  2653. }
  2654. function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode = 0)
  2655. {
  2656. $v_result = 1;
  2657. if ($p_mode == 0) {
  2658. while ($p_size != 0) {
  2659. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2660. $v_buffer = @fread($p_src, $v_read_size);
  2661. @fwrite($p_dest, $v_buffer, $v_read_size);
  2662. $p_size -= $v_read_size;
  2663. }
  2664. } else if ($p_mode == 1) {
  2665. while ($p_size != 0) {
  2666. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2667. $v_buffer = @gzread($p_src, $v_read_size);
  2668. @fwrite($p_dest, $v_buffer, $v_read_size);
  2669. $p_size -= $v_read_size;
  2670. }
  2671. } else if ($p_mode == 2) {
  2672. while ($p_size != 0) {
  2673. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2674. $v_buffer = @fread($p_src, $v_read_size);
  2675. @gzwrite($p_dest, $v_buffer, $v_read_size);
  2676. $p_size -= $v_read_size;
  2677. }
  2678. } else if ($p_mode == 3) {
  2679. while ($p_size != 0) {
  2680. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2681. $v_buffer = @gzread($p_src, $v_read_size);
  2682. @gzwrite($p_dest, $v_buffer, $v_read_size);
  2683. $p_size -= $v_read_size;
  2684. }
  2685. }
  2686. return $v_result;
  2687. }
  2688. function PclZipUtilRename($p_src, $p_dest)
  2689. {
  2690. $v_result = 1;
  2691. if (!@rename($p_src, $p_dest)) {
  2692. if (!@copy($p_src, $p_dest)) {
  2693. $v_result = 0;
  2694. } else if (!@unlink($p_src)) {
  2695. $v_result = 0;
  2696. }
  2697. }
  2698. return $v_result;
  2699. }
  2700. function PclZipUtilOptionText($p_option)
  2701. {
  2702. $v_list = get_defined_constants();
  2703. for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  2704. $v_prefix = substr($v_key, 0, 10);
  2705. if ((($v_prefix == 'PCLZIP_OPT')
  2706. || ($v_prefix == 'PCLZIP_CB_')
  2707. || ($v_prefix == 'PCLZIP_ATT'))
  2708. && ($v_list[$v_key] == $p_option)) {
  2709. return $v_key;
  2710. }
  2711. }
  2712. $v_result = 'Unknown';
  2713. return $v_result;
  2714. }
  2715. function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter = true)
  2716. {
  2717. if (stristr(php_uname(), 'windows')) {
  2718. if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
  2719. $p_path = substr($p_path, $v_position + 1);
  2720. }
  2721. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
  2722. $p_path = strtr($p_path, '\\', '/');
  2723. }
  2724. }
  2725. return $p_path;
  2726. }
  2727. ?>