pclzip.lib.php 214 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. // --------------------------------------------------------------------------------
  8. // PhpConcept Library - Zip Module 2.8.2
  9. // --------------------------------------------------------------------------------
  10. // License GNU/LGPL - Vincent Blavet - August 2009
  11. // http://www.phpconcept.net
  12. // --------------------------------------------------------------------------------
  13. //
  14. // Presentation :
  15. // PclZip is a PHP library that manage ZIP archives.
  16. // So far tests show that archives generated by PclZip are readable by
  17. // WinZip application and other tools.
  18. //
  19. // Description :
  20. // See readme.txt and http://www.phpconcept.net
  21. //
  22. // Warning :
  23. // This library and the associated files are non commercial, non professional
  24. // work.
  25. // It should not have unexpected results. However if any damage is caused by
  26. // this software the author can not be responsible.
  27. // The use of this software is at the risk of the user.
  28. //
  29. // --------------------------------------------------------------------------------
  30. // $Id: pclzip.lib.php,v 1.60 2009/09/30 21:01:04 vblavet Exp $
  31. // --------------------------------------------------------------------------------
  32. // ----- Constants
  33. if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
  34. define('PCLZIP_READ_BLOCK_SIZE', 2048);
  35. }
  36. // ----- File list separator
  37. // In version 1.x of PclZip, the separator for file list is a space
  38. // (which is not a very smart choice, specifically for windows paths !).
  39. // A better separator should be a comma (,). This constant gives you the
  40. // abilty to change that.
  41. // However notice that changing this value, may have impact on existing
  42. // scripts, using space separated filenames.
  43. // Recommanded values for compatibility with older versions :
  44. //define('PCLZIP_SEPARATOR', ' ');
  45. // Recommanded values for smart separation of filenames.
  46. if (!defined('PCLZIP_SEPARATOR')) {
  47. define('PCLZIP_SEPARATOR', ',');
  48. }
  49. // ----- Error configuration
  50. // 0 : PclZip Class integrated error handling
  51. // 1 : PclError external library error handling. By enabling this
  52. // you must ensure that you have included PclError library.
  53. // [2,...] : reserved for futur use
  54. if (!defined('PCLZIP_ERROR_EXTERNAL')) {
  55. define('PCLZIP_ERROR_EXTERNAL', 0);
  56. }
  57. // ----- Optional static temporary directory
  58. // By default temporary files are generated in the script current
  59. // path.
  60. // If defined :
  61. // - MUST BE terminated by a '/'.
  62. // - MUST be a valid, already created directory
  63. // Samples :
  64. // define('PCLZIP_TEMPORARY_DIR', '/temp/');
  65. // define('PCLZIP_TEMPORARY_DIR', 'C:/Temp/');
  66. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  67. define('PCLZIP_TEMPORARY_DIR', '');
  68. }
  69. // ----- Optional threshold ratio for use of temporary files
  70. // Pclzip sense the size of the file to add/extract and decide to
  71. // use or not temporary file. The algorythm is looking for
  72. // memory_limit of PHP and apply a ratio.
  73. // threshold = memory_limit * ratio.
  74. // Recommended values are under 0.5. Default 0.47.
  75. // Samples :
  76. // define('PCLZIP_TEMPORARY_FILE_RATIO', 0.5);
  77. if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
  78. define('PCLZIP_TEMPORARY_FILE_RATIO', 0.47);
  79. }
  80. // --------------------------------------------------------------------------------
  81. // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
  82. // --------------------------------------------------------------------------------
  83. // ----- Global variables
  84. $g_pclzip_version = "2.8.2";
  85. // ----- Error codes
  86. // -1 : Unable to open file in binary write mode
  87. // -2 : Unable to open file in binary read mode
  88. // -3 : Invalid parameters
  89. // -4 : File does not exist
  90. // -5 : Filename is too long (max. 255)
  91. // -6 : Not a valid zip file
  92. // -7 : Invalid extracted file size
  93. // -8 : Unable to create directory
  94. // -9 : Invalid archive extension
  95. // -10 : Invalid archive format
  96. // -11 : Unable to delete file (unlink)
  97. // -12 : Unable to rename file (rename)
  98. // -13 : Invalid header checksum
  99. // -14 : Invalid archive size
  100. define('PCLZIP_ERR_USER_ABORTED', 2);
  101. define('PCLZIP_ERR_NO_ERROR', 0);
  102. define('PCLZIP_ERR_WRITE_OPEN_FAIL', -1);
  103. define('PCLZIP_ERR_READ_OPEN_FAIL', -2);
  104. define('PCLZIP_ERR_INVALID_PARAMETER', -3);
  105. define('PCLZIP_ERR_MISSING_FILE', -4);
  106. define('PCLZIP_ERR_FILENAME_TOO_LONG', -5);
  107. define('PCLZIP_ERR_INVALID_ZIP', -6);
  108. define('PCLZIP_ERR_BAD_EXTRACTED_FILE', -7);
  109. define('PCLZIP_ERR_DIR_CREATE_FAIL', -8);
  110. define('PCLZIP_ERR_BAD_EXTENSION', -9);
  111. define('PCLZIP_ERR_BAD_FORMAT', -10);
  112. define('PCLZIP_ERR_DELETE_FILE_FAIL', -11);
  113. define('PCLZIP_ERR_RENAME_FILE_FAIL', -12);
  114. define('PCLZIP_ERR_BAD_CHECKSUM', -13);
  115. define('PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14);
  116. define('PCLZIP_ERR_MISSING_OPTION_VALUE', -15);
  117. define('PCLZIP_ERR_INVALID_OPTION_VALUE', -16);
  118. define('PCLZIP_ERR_ALREADY_A_DIRECTORY', -17);
  119. define('PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18);
  120. define('PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19);
  121. define('PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20);
  122. define('PCLZIP_ERR_DIRECTORY_RESTRICTION', -21);
  123. // ----- Options values
  124. define('PCLZIP_OPT_PATH', 77001);
  125. define('PCLZIP_OPT_ADD_PATH', 77002);
  126. define('PCLZIP_OPT_REMOVE_PATH', 77003);
  127. define('PCLZIP_OPT_REMOVE_ALL_PATH', 77004);
  128. define('PCLZIP_OPT_SET_CHMOD', 77005);
  129. define('PCLZIP_OPT_EXTRACT_AS_STRING', 77006);
  130. define('PCLZIP_OPT_NO_COMPRESSION', 77007);
  131. define('PCLZIP_OPT_BY_NAME', 77008);
  132. define('PCLZIP_OPT_BY_INDEX', 77009);
  133. define('PCLZIP_OPT_BY_EREG', 77010);
  134. define('PCLZIP_OPT_BY_PREG', 77011);
  135. define('PCLZIP_OPT_COMMENT', 77012);
  136. define('PCLZIP_OPT_ADD_COMMENT', 77013);
  137. define('PCLZIP_OPT_PREPEND_COMMENT', 77014);
  138. define('PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015);
  139. define('PCLZIP_OPT_REPLACE_NEWER', 77016);
  140. define('PCLZIP_OPT_STOP_ON_ERROR', 77017);
  141. // Having big trouble with crypt. Need to multiply 2 long int
  142. // which is not correctly supported by PHP ...
  143. //define('PCLZIP_OPT_CRYPT', 77018);
  144. define('PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019);
  145. define('PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020);
  146. define('PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020); // alias
  147. define('PCLZIP_OPT_TEMP_FILE_ON', 77021);
  148. define('PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021); // alias
  149. define('PCLZIP_OPT_TEMP_FILE_OFF', 77022);
  150. define('PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022); // alias
  151. // ----- File description attributes
  152. define('PCLZIP_ATT_FILE_NAME', 79001);
  153. define('PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002);
  154. define('PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003);
  155. define('PCLZIP_ATT_FILE_MTIME', 79004);
  156. define('PCLZIP_ATT_FILE_CONTENT', 79005);
  157. define('PCLZIP_ATT_FILE_COMMENT', 79006);
  158. // ----- Call backs values
  159. define('PCLZIP_CB_PRE_EXTRACT', 78001);
  160. define('PCLZIP_CB_POST_EXTRACT', 78002);
  161. define('PCLZIP_CB_PRE_ADD', 78003);
  162. define('PCLZIP_CB_POST_ADD', 78004);
  163. /* For futur use
  164. define('PCLZIP_CB_PRE_LIST', 78005);
  165. define('PCLZIP_CB_POST_LIST', 78006);
  166. define('PCLZIP_CB_PRE_DELETE', 78007);
  167. define('PCLZIP_CB_POST_DELETE', 78008);
  168. */
  169. // --------------------------------------------------------------------------------
  170. // Class : PclZip
  171. // Description :
  172. // PclZip is the class that represent a Zip archive.
  173. // The public methods allow the manipulation of the archive.
  174. // Attributes :
  175. // Attributes must not be accessed directly.
  176. // Methods :
  177. // PclZip() : Object creator
  178. // create() : Creates the Zip archive
  179. // listContent() : List the content of the Zip archive
  180. // extract() : Extract the content of the archive
  181. // properties() : List the properties of the archive
  182. // --------------------------------------------------------------------------------
  183. class PclZip
  184. {
  185. // ----- Filename of the zip file
  186. public $zipname = '';
  187. // ----- File descriptor of the zip file
  188. public $zip_fd = 0;
  189. // ----- Internal error handling
  190. public $error_code = 1;
  191. public $error_string = '';
  192. // ----- Current status of the magic_quotes_runtime
  193. // This value store the php configuration for magic_quotes
  194. // The class can then disable the magic_quotes and reset it after
  195. public $magic_quotes_status;
  196. // --------------------------------------------------------------------------------
  197. // Function : PclZip()
  198. // Description :
  199. // Creates a PclZip object and set the name of the associated Zip archive
  200. // filename.
  201. // Note that no real action is taken, if the archive does not exist it is not
  202. // created. Use create() for that.
  203. // --------------------------------------------------------------------------------
  204. public function __construct($p_zipname)
  205. {
  206. // ----- Tests the zlib
  207. if (!function_exists('gzopen')) {
  208. die('Abort '.basename(__FILE__).' : Missing zlib extensions');
  209. }
  210. // ----- Set the attributes
  211. $this->zipname = $p_zipname;
  212. $this->zip_fd = 0;
  213. $this->magic_quotes_status = -1;
  214. // ----- Return
  215. return;
  216. }
  217. // --------------------------------------------------------------------------------
  218. // --------------------------------------------------------------------------------
  219. // Function :
  220. // create($p_filelist, $p_add_dir="", $p_remove_dir="")
  221. // create($p_filelist, $p_option, $p_option_value, ...)
  222. // Description :
  223. // This method supports two different synopsis. The first one is historical.
  224. // This method creates a Zip Archive. The Zip file is created in the
  225. // filesystem. The files and directories indicated in $p_filelist
  226. // are added in the archive. See the parameters description for the
  227. // supported format of $p_filelist.
  228. // When a directory is in the list, the directory and its content is added
  229. // in the archive.
  230. // In this synopsis, the function takes an optional variable list of
  231. // options. See bellow the supported options.
  232. // Parameters :
  233. // $p_filelist : An array containing file or directory names, or
  234. // a string containing one filename or one directory name, or
  235. // a string containing a list of filenames and/or directory
  236. // names separated by spaces.
  237. // $p_add_dir : A path to add before the real path of the archived file,
  238. // in order to have it memorized in the archive.
  239. // $p_remove_dir : A path to remove from the real path of the file to archive,
  240. // in order to have a shorter path memorized in the archive.
  241. // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
  242. // is removed first, before $p_add_dir is added.
  243. // Options :
  244. // PCLZIP_OPT_ADD_PATH :
  245. // PCLZIP_OPT_REMOVE_PATH :
  246. // PCLZIP_OPT_REMOVE_ALL_PATH :
  247. // PCLZIP_OPT_COMMENT :
  248. // PCLZIP_CB_PRE_ADD :
  249. // PCLZIP_CB_POST_ADD :
  250. // Return Values :
  251. // 0 on failure,
  252. // The list of the added files, with a status of the add action.
  253. // (see PclZip::listContent() for list entry format)
  254. // --------------------------------------------------------------------------------
  255. public function create($p_filelist)
  256. {
  257. $v_result=1;
  258. // ----- Reset the error handler
  259. $this->privErrorReset();
  260. // ----- Set default values
  261. $v_options = array();
  262. $v_options[PCLZIP_OPT_NO_COMPRESSION] = false;
  263. // ----- Look for variable options arguments
  264. $v_size = func_num_args();
  265. // ----- Look for arguments
  266. if ($v_size > 1) {
  267. // ----- Get the arguments
  268. $v_arg_list = func_get_args();
  269. // ----- Remove from the options list the first argument
  270. array_shift($v_arg_list);
  271. $v_size--;
  272. // ----- Look for first arg
  273. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  274. // ----- Parse the options
  275. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, array (
  276. PCLZIP_OPT_REMOVE_PATH => 'optional',
  277. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  278. PCLZIP_OPT_ADD_PATH => 'optional',
  279. PCLZIP_CB_PRE_ADD => 'optional',
  280. PCLZIP_CB_POST_ADD => 'optional',
  281. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  282. PCLZIP_OPT_COMMENT => 'optional',
  283. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  284. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  285. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  286. //, PCLZIP_OPT_CRYPT => 'optional'
  287. ));
  288. if ($v_result != 1) {
  289. return 0;
  290. }
  291. } else {
  292. // ----- Look for 2 args
  293. // Here we need to support the first historic synopsis of the
  294. // method.
  295. // ----- Get the first argument
  296. $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
  297. // ----- Look for the optional second argument
  298. if ($v_size == 2) {
  299. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  300. } elseif ($v_size > 2) {
  301. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  302. return 0;
  303. }
  304. }
  305. }
  306. // ----- Look for default option values
  307. $this->privOptionDefaultThreshold($v_options);
  308. // ----- Init
  309. $v_string_list = array();
  310. $v_att_list = array();
  311. $v_filedescr_list = array();
  312. $p_result_list = array();
  313. // ----- Look if the $p_filelist is really an array
  314. if (is_array($p_filelist)) {
  315. // ----- Look if the first element is also an array
  316. // This will mean that this is a file description entry
  317. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  318. $v_att_list = $p_filelist;
  319. } else {
  320. // ----- The list is a list of string names
  321. $v_string_list = $p_filelist;
  322. }
  323. } elseif (is_string($p_filelist)) {
  324. // ----- Look if the $p_filelist is a string
  325. // ----- Create a list from the string
  326. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  327. } else {
  328. // ----- Invalid variable type for $p_filelist
  329. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  330. return 0;
  331. }
  332. // ----- Reformat the string list
  333. if (sizeof($v_string_list) != 0) {
  334. foreach ($v_string_list as $v_string) {
  335. if ($v_string != '') {
  336. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  337. } else {
  338. }
  339. }
  340. }
  341. // ----- For each file in the list check the attributes
  342. $v_supported_attributes = array(
  343. PCLZIP_ATT_FILE_NAME => 'mandatory',
  344. PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional',
  345. PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional',
  346. PCLZIP_ATT_FILE_MTIME => 'optional',
  347. PCLZIP_ATT_FILE_CONTENT => 'optional',
  348. PCLZIP_ATT_FILE_COMMENT => 'optional'
  349. );
  350. foreach ($v_att_list as $v_entry) {
  351. $v_result = $this->privFileDescrParseAtt($v_entry, $v_filedescr_list[], $v_options, $v_supported_attributes);
  352. if ($v_result != 1) {
  353. return 0;
  354. }
  355. }
  356. // ----- Expand the filelist (expand directories)
  357. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  358. if ($v_result != 1) {
  359. return 0;
  360. }
  361. // ----- Call the create fct
  362. $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
  363. if ($v_result != 1) {
  364. return 0;
  365. }
  366. // ----- Return
  367. return $p_result_list;
  368. }
  369. // --------------------------------------------------------------------------------
  370. // --------------------------------------------------------------------------------
  371. // Function :
  372. // add($p_filelist, $p_add_dir="", $p_remove_dir="")
  373. // add($p_filelist, $p_option, $p_option_value, ...)
  374. // Description :
  375. // This method supports two synopsis. The first one is historical.
  376. // This methods add the list of files in an existing archive.
  377. // If a file with the same name already exists, it is added at the end of the
  378. // archive, the first one is still present.
  379. // If the archive does not exist, it is created.
  380. // Parameters :
  381. // $p_filelist : An array containing file or directory names, or
  382. // a string containing one filename or one directory name, or
  383. // a string containing a list of filenames and/or directory
  384. // names separated by spaces.
  385. // $p_add_dir : A path to add before the real path of the archived file,
  386. // in order to have it memorized in the archive.
  387. // $p_remove_dir : A path to remove from the real path of the file to archive,
  388. // in order to have a shorter path memorized in the archive.
  389. // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
  390. // is removed first, before $p_add_dir is added.
  391. // Options :
  392. // PCLZIP_OPT_ADD_PATH :
  393. // PCLZIP_OPT_REMOVE_PATH :
  394. // PCLZIP_OPT_REMOVE_ALL_PATH :
  395. // PCLZIP_OPT_COMMENT :
  396. // PCLZIP_OPT_ADD_COMMENT :
  397. // PCLZIP_OPT_PREPEND_COMMENT :
  398. // PCLZIP_CB_PRE_ADD :
  399. // PCLZIP_CB_POST_ADD :
  400. // Return Values :
  401. // 0 on failure,
  402. // The list of the added files, with a status of the add action.
  403. // (see PclZip::listContent() for list entry format)
  404. // --------------------------------------------------------------------------------
  405. public function add($p_filelist)
  406. {
  407. $v_result=1;
  408. // ----- Reset the error handler
  409. $this->privErrorReset();
  410. // ----- Set default values
  411. $v_options = array();
  412. $v_options[PCLZIP_OPT_NO_COMPRESSION] = false;
  413. // ----- Look for variable options arguments
  414. $v_size = func_num_args();
  415. // ----- Look for arguments
  416. if ($v_size > 1) {
  417. // ----- Get the arguments
  418. $v_arg_list = func_get_args();
  419. // ----- Remove form the options list the first argument
  420. array_shift($v_arg_list);
  421. $v_size--;
  422. // ----- Look for first arg
  423. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  424. // ----- Parse the options
  425. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, array (
  426. PCLZIP_OPT_REMOVE_PATH => 'optional',
  427. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  428. PCLZIP_OPT_ADD_PATH => 'optional',
  429. PCLZIP_CB_PRE_ADD => 'optional',
  430. PCLZIP_CB_POST_ADD => 'optional',
  431. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  432. PCLZIP_OPT_COMMENT => 'optional',
  433. PCLZIP_OPT_ADD_COMMENT => 'optional',
  434. PCLZIP_OPT_PREPEND_COMMENT => 'optional',
  435. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  436. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  437. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  438. //, PCLZIP_OPT_CRYPT => 'optional'
  439. ));
  440. if ($v_result != 1) {
  441. return 0;
  442. }
  443. } else {
  444. // ----- Look for 2 args
  445. // Here we need to support the first historic synopsis of the
  446. // method.
  447. // ----- Get the first argument
  448. $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
  449. // ----- Look for the optional second argument
  450. if ($v_size == 2) {
  451. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  452. } elseif ($v_size > 2) {
  453. // ----- Error log
  454. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  455. // ----- Return
  456. return 0;
  457. }
  458. }
  459. }
  460. // ----- Look for default option values
  461. $this->privOptionDefaultThreshold($v_options);
  462. // ----- Init
  463. $v_string_list = array();
  464. $v_att_list = array();
  465. $v_filedescr_list = array();
  466. $p_result_list = array();
  467. // ----- Look if the $p_filelist is really an array
  468. if (is_array($p_filelist)) {
  469. // ----- Look if the first element is also an array
  470. // This will mean that this is a file description entry
  471. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  472. $v_att_list = $p_filelist;
  473. } else {
  474. // ----- The list is a list of string names
  475. $v_string_list = $p_filelist;
  476. }
  477. } elseif (is_string($p_filelist)) {
  478. // ----- Look if the $p_filelist is a string
  479. // ----- Create a list from the string
  480. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  481. } else {
  482. // ----- Invalid variable type for $p_filelist
  483. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
  484. return 0;
  485. }
  486. // ----- Reformat the string list
  487. if (sizeof($v_string_list) != 0) {
  488. foreach ($v_string_list as $v_string) {
  489. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  490. }
  491. }
  492. // ----- For each file in the list check the attributes
  493. $v_supported_attributes = array(
  494. PCLZIP_ATT_FILE_NAME => 'mandatory',
  495. PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional',
  496. PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional',
  497. PCLZIP_ATT_FILE_MTIME => 'optional',
  498. PCLZIP_ATT_FILE_CONTENT => 'optional',
  499. PCLZIP_ATT_FILE_COMMENT => 'optional',
  500. );
  501. foreach ($v_att_list as $v_entry) {
  502. $v_result = $this->privFileDescrParseAtt($v_entry, $v_filedescr_list[], $v_options, $v_supported_attributes);
  503. if ($v_result != 1) {
  504. return 0;
  505. }
  506. }
  507. // ----- Expand the filelist (expand directories)
  508. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  509. if ($v_result != 1) {
  510. return 0;
  511. }
  512. // ----- Call the create fct
  513. $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
  514. if ($v_result != 1) {
  515. return 0;
  516. }
  517. // ----- Return
  518. return $p_result_list;
  519. }
  520. // --------------------------------------------------------------------------------
  521. // --------------------------------------------------------------------------------
  522. // Function : listContent()
  523. // Description :
  524. // This public method, gives the list of the files and directories, with their
  525. // properties.
  526. // The properties of each entries in the list are (used also in other functions) :
  527. // filename : Name of the file. For a create or add action it is the filename
  528. // given by the user. For an extract function it is the filename
  529. // of the extracted file.
  530. // stored_filename : Name of the file / directory stored in the archive.
  531. // size : Size of the stored file.
  532. // compressed_size : Size of the file's data compressed in the archive
  533. // (without the headers overhead)
  534. // mtime : Last known modification date of the file (UNIX timestamp)
  535. // comment : Comment associated with the file
  536. // folder : true | false
  537. // index : index of the file in the archive
  538. // status : status of the action (depending of the action) :
  539. // Values are :
  540. // ok : OK !
  541. // filtered : the file / dir is not extracted (filtered by user)
  542. // already_a_directory : the file can not be extracted because a
  543. // directory with the same name already exists
  544. // write_protected : the file can not be extracted because a file
  545. // with the same name already exists and is
  546. // write protected
  547. // newer_exist : the file was not extracted because a newer file exists
  548. // path_creation_fail : the file is not extracted because the folder
  549. // does not exist and can not be created
  550. // write_error : the file was not extracted because there was a
  551. // error while writing the file
  552. // read_error : the file was not extracted because there was a error
  553. // while reading the file
  554. // invalid_header : the file was not extracted because of an archive
  555. // format error (bad file header)
  556. // Note that each time a method can continue operating when there
  557. // is an action error on a file, the error is only logged in the file status.
  558. // Return Values :
  559. // 0 on an unrecoverable failure,
  560. // The list of the files in the archive.
  561. // --------------------------------------------------------------------------------
  562. public function listContent()
  563. {
  564. $v_result=1;
  565. // ----- Reset the error handler
  566. $this->privErrorReset();
  567. // ----- Check archive
  568. if (!$this->privCheckFormat()) {
  569. return(0);
  570. }
  571. // ----- Call the extracting fct
  572. $p_list = array();
  573. if (($v_result = $this->privList($p_list)) != 1) {
  574. unset($p_list);
  575. return(0);
  576. }
  577. // ----- Return
  578. return $p_list;
  579. }
  580. // --------------------------------------------------------------------------------
  581. // --------------------------------------------------------------------------------
  582. // Function :
  583. // extract($p_path="./", $p_remove_path="")
  584. // extract([$p_option, $p_option_value, ...])
  585. // Description :
  586. // This method supports two synopsis. The first one is historical.
  587. // This method extract all the files / directories from the archive to the
  588. // folder indicated in $p_path.
  589. // If you want to ignore the 'root' part of path of the memorized files
  590. // you can indicate this in the optional $p_remove_path parameter.
  591. // By default, if a newer file with the same name already exists, the
  592. // file is not extracted.
  593. //
  594. // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
  595. // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
  596. // at the end of the path value of PCLZIP_OPT_PATH.
  597. // Parameters :
  598. // $p_path : Path where the files and directories are to be extracted
  599. // $p_remove_path : First part ('root' part) of the memorized path
  600. // (if any similar) to remove while extracting.
  601. // Options :
  602. // PCLZIP_OPT_PATH :
  603. // PCLZIP_OPT_ADD_PATH :
  604. // PCLZIP_OPT_REMOVE_PATH :
  605. // PCLZIP_OPT_REMOVE_ALL_PATH :
  606. // PCLZIP_CB_PRE_EXTRACT :
  607. // PCLZIP_CB_POST_EXTRACT :
  608. // Return Values :
  609. // 0 or a negative value on failure,
  610. // The list of the extracted files, with a status of the action.
  611. // (see PclZip::listContent() for list entry format)
  612. // --------------------------------------------------------------------------------
  613. public function extract()
  614. {
  615. $v_result=1;
  616. // ----- Reset the error handler
  617. $this->privErrorReset();
  618. // ----- Check archive
  619. if (!$this->privCheckFormat()) {
  620. return(0);
  621. }
  622. // ----- Set default values
  623. $v_options = array();
  624. // $v_path = "./";
  625. $v_path = '';
  626. $v_remove_path = "";
  627. $v_remove_all_path = false;
  628. // ----- Look for variable options arguments
  629. $v_size = func_num_args();
  630. // ----- Default values for option
  631. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = false;
  632. // ----- Look for arguments
  633. if ($v_size > 0) {
  634. // ----- Get the arguments
  635. $v_arg_list = func_get_args();
  636. // ----- Look for first arg
  637. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  638. // ----- Parse the options
  639. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, array (
  640. PCLZIP_OPT_PATH => 'optional',
  641. PCLZIP_OPT_REMOVE_PATH => 'optional',
  642. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  643. PCLZIP_OPT_ADD_PATH => 'optional',
  644. PCLZIP_CB_PRE_EXTRACT => 'optional',
  645. PCLZIP_CB_POST_EXTRACT => 'optional',
  646. PCLZIP_OPT_SET_CHMOD => 'optional',
  647. PCLZIP_OPT_BY_NAME => 'optional',
  648. PCLZIP_OPT_BY_EREG => 'optional',
  649. PCLZIP_OPT_BY_PREG => 'optional',
  650. PCLZIP_OPT_BY_INDEX => 'optional',
  651. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  652. PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
  653. PCLZIP_OPT_REPLACE_NEWER => 'optional',
  654. PCLZIP_OPT_STOP_ON_ERROR => 'optional',
  655. PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  656. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  657. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  658. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  659. ));
  660. if ($v_result != 1) {
  661. return 0;
  662. }
  663. // ----- Set the arguments
  664. if (isset($v_options[PCLZIP_OPT_PATH])) {
  665. $v_path = $v_options[PCLZIP_OPT_PATH];
  666. }
  667. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  668. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  669. }
  670. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  671. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  672. }
  673. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  674. // ----- Check for '/' in last path char
  675. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  676. $v_path .= '/';
  677. }
  678. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  679. }
  680. } else {
  681. // ----- Look for 2 args
  682. // Here we need to support the first historic synopsis of the
  683. // method.
  684. // ----- Get the first argument
  685. $v_path = $v_arg_list[0];
  686. // ----- Look for the optional second argument
  687. if ($v_size == 2) {
  688. $v_remove_path = $v_arg_list[1];
  689. } elseif ($v_size > 2) {
  690. // ----- Error log
  691. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  692. // ----- Return
  693. return 0;
  694. }
  695. }
  696. }
  697. // ----- Look for default option values
  698. $this->privOptionDefaultThreshold($v_options);
  699. // ----- Trace
  700. // ----- Call the extracting fct
  701. $p_list = array();
  702. $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options);
  703. if ($v_result < 1) {
  704. unset($p_list);
  705. return(0);
  706. }
  707. // ----- Return
  708. return $p_list;
  709. }
  710. // --------------------------------------------------------------------------------
  711. // --------------------------------------------------------------------------------
  712. // Function :
  713. // extractByIndex($p_index, $p_path="./", $p_remove_path="")
  714. // extractByIndex($p_index, [$p_option, $p_option_value, ...])
  715. // Description :
  716. // This method supports two synopsis. The first one is historical.
  717. // This method is doing a partial extract of the archive.
  718. // The extracted files or folders are identified by their index in the
  719. // archive (from 0 to n).
  720. // Note that if the index identify a folder, only the folder entry is
  721. // extracted, not all the files included in the archive.
  722. // Parameters :
  723. // $p_index : A single index (integer) or a string of indexes of files to
  724. // extract. The form of the string is "0,4-6,8-12" with only numbers
  725. // and '-' for range or ',' to separate ranges. No spaces or ';'
  726. // are allowed.
  727. // $p_path : Path where the files and directories are to be extracted
  728. // $p_remove_path : First part ('root' part) of the memorized path
  729. // (if any similar) to remove while extracting.
  730. // Options :
  731. // PCLZIP_OPT_PATH :
  732. // PCLZIP_OPT_ADD_PATH :
  733. // PCLZIP_OPT_REMOVE_PATH :
  734. // PCLZIP_OPT_REMOVE_ALL_PATH :
  735. // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
  736. // not as files.
  737. // The resulting content is in a new field 'content' in the file
  738. // structure.
  739. // This option must be used alone (any other options are ignored).
  740. // PCLZIP_CB_PRE_EXTRACT :
  741. // PCLZIP_CB_POST_EXTRACT :
  742. // Return Values :
  743. // 0 on failure,
  744. // The list of the extracted files, with a status of the action.
  745. // (see PclZip::listContent() for list entry format)
  746. // --------------------------------------------------------------------------------
  747. //function extractByIndex($p_index, options...)
  748. public function extractByIndex($p_index)
  749. {
  750. $v_result=1;
  751. // ----- Reset the error handler
  752. $this->privErrorReset();
  753. // ----- Check archive
  754. if (!$this->privCheckFormat()) {
  755. return(0);
  756. }
  757. // ----- Set default values
  758. $v_options = array();
  759. // $v_path = "./";
  760. $v_path = '';
  761. $v_remove_path = "";
  762. $v_remove_all_path = false;
  763. // ----- Look for variable options arguments
  764. $v_size = func_num_args();
  765. // ----- Default values for option
  766. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = false;
  767. // ----- Look for arguments
  768. if ($v_size > 1) {
  769. // ----- Get the arguments
  770. $v_arg_list = func_get_args();
  771. // ----- Remove form the options list the first argument
  772. array_shift($v_arg_list);
  773. $v_size--;
  774. // ----- Look for first arg
  775. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  776. // ----- Parse the options
  777. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, array(
  778. PCLZIP_OPT_PATH => 'optional',
  779. PCLZIP_OPT_REMOVE_PATH => 'optional',
  780. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  781. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  782. PCLZIP_OPT_ADD_PATH => 'optional',
  783. PCLZIP_CB_PRE_EXTRACT => 'optional',
  784. PCLZIP_CB_POST_EXTRACT => 'optional',
  785. PCLZIP_OPT_SET_CHMOD => 'optional',
  786. PCLZIP_OPT_REPLACE_NEWER => 'optional',
  787. PCLZIP_OPT_STOP_ON_ERROR => 'optional',
  788. PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  789. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  790. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  791. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  792. ));
  793. if ($v_result != 1) {
  794. return 0;
  795. }
  796. // ----- Set the arguments
  797. if (isset($v_options[PCLZIP_OPT_PATH])) {
  798. $v_path = $v_options[PCLZIP_OPT_PATH];
  799. }
  800. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  801. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  802. }
  803. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  804. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  805. }
  806. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  807. // ----- Check for '/' in last path char
  808. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  809. $v_path .= '/';
  810. }
  811. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  812. }
  813. if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
  814. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = false;
  815. } else {
  816. }
  817. } else {
  818. // ----- Look for 2 args
  819. // Here we need to support the first historic synopsis of the
  820. // method.
  821. // ----- Get the first argument
  822. $v_path = $v_arg_list[0];
  823. // ----- Look for the optional second argument
  824. if ($v_size == 2) {
  825. $v_remove_path = $v_arg_list[1];
  826. } elseif ($v_size > 2) {
  827. // ----- Error log
  828. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  829. // ----- Return
  830. return 0;
  831. }
  832. }
  833. }
  834. // ----- Trace
  835. // ----- Trick
  836. // Here I want to reuse extractByRule(), so I need to parse the $p_index
  837. // with privParseOptions()
  838. $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
  839. $v_options_trick = array();
  840. $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick, array (PCLZIP_OPT_BY_INDEX => 'optional'));
  841. if ($v_result != 1) {
  842. return 0;
  843. }
  844. $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
  845. // ----- Look for default option values
  846. $this->privOptionDefaultThreshold($v_options);
  847. // ----- Call the extracting fct
  848. if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
  849. return(0);
  850. }
  851. // ----- Return
  852. return $p_list;
  853. }
  854. // --------------------------------------------------------------------------------
  855. // --------------------------------------------------------------------------------
  856. // Function :
  857. // delete([$p_option, $p_option_value, ...])
  858. // Description :
  859. // This method removes files from the archive.
  860. // If no parameters are given, then all the archive is emptied.
  861. // Parameters :
  862. // None or optional arguments.
  863. // Options :
  864. // PCLZIP_OPT_BY_INDEX :
  865. // PCLZIP_OPT_BY_NAME :
  866. // PCLZIP_OPT_BY_EREG :
  867. // PCLZIP_OPT_BY_PREG :
  868. // Return Values :
  869. // 0 on failure,
  870. // The list of the files which are still present in the archive.
  871. // (see PclZip::listContent() for list entry format)
  872. // --------------------------------------------------------------------------------
  873. public function delete()
  874. {
  875. $v_result=1;
  876. // ----- Reset the error handler
  877. $this->privErrorReset();
  878. // ----- Check archive
  879. if (!$this->privCheckFormat()) {
  880. return(0);
  881. }
  882. // ----- Set default values
  883. $v_options = array();
  884. // ----- Look for variable options arguments
  885. $v_size = func_num_args();
  886. // ----- Look for arguments
  887. if ($v_size > 0) {
  888. // ----- Get the arguments
  889. $v_arg_list = func_get_args();
  890. // ----- Parse the options
  891. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, array (
  892. PCLZIP_OPT_BY_NAME => 'optional',
  893. PCLZIP_OPT_BY_EREG => 'optional',
  894. PCLZIP_OPT_BY_PREG => 'optional',
  895. PCLZIP_OPT_BY_INDEX => 'optional'
  896. ));
  897. if ($v_result != 1) {
  898. return 0;
  899. }
  900. }
  901. // ----- Magic quotes trick
  902. $this->privDisableMagicQuotes();
  903. // ----- Call the delete fct
  904. $v_list = array();
  905. if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
  906. $this->privSwapBackMagicQuotes();
  907. unset($v_list);
  908. return(0);
  909. }
  910. // ----- Magic quotes trick
  911. $this->privSwapBackMagicQuotes();
  912. // ----- Return
  913. return $v_list;
  914. }
  915. // --------------------------------------------------------------------------------
  916. // --------------------------------------------------------------------------------
  917. // Function : deleteByIndex()
  918. // Description :
  919. // ***** Deprecated *****
  920. // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
  921. // --------------------------------------------------------------------------------
  922. public function deleteByIndex($p_index)
  923. {
  924. $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  925. // ----- Return
  926. return $p_list;
  927. }
  928. // --------------------------------------------------------------------------------
  929. // --------------------------------------------------------------------------------
  930. // Function : properties()
  931. // Description :
  932. // This method gives the properties of the archive.
  933. // The properties are :
  934. // nb : Number of files in the archive
  935. // comment : Comment associated with the archive file
  936. // status : not_exist, ok
  937. // Parameters :
  938. // None
  939. // Return Values :
  940. // 0 on failure,
  941. // An array with the archive properties.
  942. // --------------------------------------------------------------------------------
  943. public function properties()
  944. {
  945. // ----- Reset the error handler
  946. $this->privErrorReset();
  947. // ----- Magic quotes trick
  948. $this->privDisableMagicQuotes();
  949. // ----- Check archive
  950. if (!$this->privCheckFormat()) {
  951. $this->privSwapBackMagicQuotes();
  952. return(0);
  953. }
  954. // ----- Default properties
  955. $v_prop = array();
  956. $v_prop['comment'] = '';
  957. $v_prop['nb'] = 0;
  958. $v_prop['status'] = 'not_exist';
  959. // ----- Look if file exists
  960. if (@is_file($this->zipname)) {
  961. // ----- Open the zip file
  962. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
  963. $this->privSwapBackMagicQuotes();
  964. // ----- Error log
  965. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  966. // ----- Return
  967. return 0;
  968. }
  969. // ----- Read the central directory informations
  970. $v_central_dir = array();
  971. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  972. $this->privSwapBackMagicQuotes();
  973. return 0;
  974. }
  975. // ----- Close the zip file
  976. $this->privCloseFd();
  977. // ----- Set the user attributes
  978. $v_prop['comment'] = $v_central_dir['comment'];
  979. $v_prop['nb'] = $v_central_dir['entries'];
  980. $v_prop['status'] = 'ok';
  981. }
  982. // ----- Magic quotes trick
  983. $this->privSwapBackMagicQuotes();
  984. // ----- Return
  985. return $v_prop;
  986. }
  987. // --------------------------------------------------------------------------------
  988. // --------------------------------------------------------------------------------
  989. // Function : duplicate()
  990. // Description :
  991. // This method creates an archive by copying the content of an other one. If
  992. // the archive already exist, it is replaced by the new one without any warning.
  993. // Parameters :
  994. // $p_archive : The filename of a valid archive, or
  995. // a valid PclZip object.
  996. // Return Values :
  997. // 1 on success.
  998. // 0 or a negative value on error (error code).
  999. // --------------------------------------------------------------------------------
  1000. public function duplicate($p_archive)
  1001. {
  1002. $v_result = 1;
  1003. // ----- Reset the error handler
  1004. $this->privErrorReset();
  1005. // ----- Look if the $p_archive is a PclZip object
  1006. if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip')) {
  1007. // ----- Duplicate the archive
  1008. $v_result = $this->privDuplicate($p_archive->zipname);
  1009. } elseif (is_string($p_archive)) {
  1010. // ----- Look if the $p_archive is a string (so a filename)
  1011. // ----- Check that $p_archive is a valid zip file
  1012. // TBC : Should also check the archive format
  1013. if (!is_file($p_archive)) {
  1014. // ----- Error log
  1015. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
  1016. $v_result = PCLZIP_ERR_MISSING_FILE;
  1017. } else {
  1018. // ----- Duplicate the archive
  1019. $v_result = $this->privDuplicate($p_archive);
  1020. }
  1021. } else {
  1022. // ----- Invalid variable
  1023. // ----- Error log
  1024. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1025. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1026. }
  1027. // ----- Return
  1028. return $v_result;
  1029. }
  1030. // --------------------------------------------------------------------------------
  1031. // --------------------------------------------------------------------------------
  1032. // Function : merge()
  1033. // Description :
  1034. // This method merge the $p_archive_to_add archive at the end of the current
  1035. // one ($this).
  1036. // If the archive ($this) does not exist, the merge becomes a duplicate.
  1037. // If the $p_archive_to_add archive does not exist, the merge is a success.
  1038. // Parameters :
  1039. // $p_archive_to_add : It can be directly the filename of a valid zip archive,
  1040. // or a PclZip object archive.
  1041. // Return Values :
  1042. // 1 on success,
  1043. // 0 or negative values on error (see below).
  1044. // --------------------------------------------------------------------------------
  1045. public function merge($p_archive_to_add)
  1046. {
  1047. $v_result = 1;
  1048. // ----- Reset the error handler
  1049. $this->privErrorReset();
  1050. // ----- Check archive
  1051. if (!$this->privCheckFormat()) {
  1052. return(0);
  1053. }
  1054. // ----- Look if the $p_archive_to_add is a PclZip object
  1055. if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip')) {
  1056. // ----- Merge the archive
  1057. $v_result = $this->privMerge($p_archive_to_add);
  1058. } elseif (is_string($p_archive_to_add)) {
  1059. // ----- Look if the $p_archive_to_add is a string (so a filename)
  1060. // ----- Create a temporary archive
  1061. $v_object_archive = new PclZip($p_archive_to_add);
  1062. // ----- Merge the archive
  1063. $v_result = $this->privMerge($v_object_archive);
  1064. } else {
  1065. // ----- Invalid variable
  1066. // ----- Error log
  1067. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1068. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1069. }
  1070. // ----- Return
  1071. return $v_result;
  1072. }
  1073. // --------------------------------------------------------------------------------
  1074. // --------------------------------------------------------------------------------
  1075. // Function : errorCode()
  1076. // Description :
  1077. // Parameters :
  1078. // --------------------------------------------------------------------------------
  1079. public function errorCode()
  1080. {
  1081. if (PCLZIP_ERROR_EXTERNAL == 1) {
  1082. return(PclErrorCode());
  1083. } else {
  1084. return($this->error_code);
  1085. }
  1086. }
  1087. // --------------------------------------------------------------------------------
  1088. // --------------------------------------------------------------------------------
  1089. // Function : errorName()
  1090. // Description :
  1091. // Parameters :
  1092. // --------------------------------------------------------------------------------
  1093. public function errorName($p_with_code = false)
  1094. {
  1095. $v_name = array(
  1096. PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
  1097. PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
  1098. PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
  1099. PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
  1100. PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
  1101. PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
  1102. PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
  1103. PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
  1104. PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
  1105. PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
  1106. PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
  1107. PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
  1108. PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
  1109. PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
  1110. PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
  1111. PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
  1112. PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
  1113. PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
  1114. PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION',
  1115. PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE',
  1116. PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION',
  1117. );
  1118. if (isset($v_name[$this->error_code])) {
  1119. $v_value = $v_name[$this->error_code];
  1120. } else {
  1121. $v_value = 'NoName';
  1122. }
  1123. if ($p_with_code) {
  1124. return($v_value.' ('.$this->error_code.')');
  1125. } else {
  1126. return($v_value);
  1127. }
  1128. }
  1129. // --------------------------------------------------------------------------------
  1130. // --------------------------------------------------------------------------------
  1131. // Function : errorInfo()
  1132. // Description :
  1133. // Parameters :
  1134. // --------------------------------------------------------------------------------
  1135. public function errorInfo($p_full = false)
  1136. {
  1137. if (PCLZIP_ERROR_EXTERNAL == 1) {
  1138. return(PclErrorString());
  1139. } else {
  1140. if ($p_full) {
  1141. return($this->errorName(true)." : ".$this->error_string);
  1142. } else {
  1143. return($this->error_string." [code ".$this->error_code."]");
  1144. }
  1145. }
  1146. }
  1147. // --------------------------------------------------------------------------------
  1148. // --------------------------------------------------------------------------------
  1149. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  1150. // ***** *****
  1151. // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
  1152. // --------------------------------------------------------------------------------
  1153. // --------------------------------------------------------------------------------
  1154. // Function : privCheckFormat()
  1155. // Description :
  1156. // This method check that the archive exists and is a valid zip archive.
  1157. // Several level of check exists. (futur)
  1158. // Parameters :
  1159. // $p_level : Level of check. Default 0.
  1160. // 0 : Check the first bytes (magic codes) (default value))
  1161. // 1 : 0 + Check the central directory (futur)
  1162. // 2 : 1 + Check each file header (futur)
  1163. // Return Values :
  1164. // true on success,
  1165. // false on error, the error code is set.
  1166. // --------------------------------------------------------------------------------
  1167. public function privCheckFormat($p_level = 0)
  1168. {
  1169. $v_result = true;
  1170. // ----- Reset the file system cache
  1171. clearstatcache();
  1172. // ----- Reset the error handler
  1173. $this->privErrorReset();
  1174. // ----- Look if the file exits
  1175. if (!is_file($this->zipname)) {
  1176. // ----- Error log
  1177. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
  1178. return(false);
  1179. }
  1180. // ----- Check that the file is readeable
  1181. if (!is_readable($this->zipname)) {
  1182. // ----- Error log
  1183. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
  1184. return(false);
  1185. }
  1186. // ----- Check the magic code
  1187. // TBC
  1188. // ----- Check the central header
  1189. // TBC
  1190. // ----- Check each file header
  1191. // TBC
  1192. // ----- Return
  1193. return $v_result;
  1194. }
  1195. // --------------------------------------------------------------------------------
  1196. // --------------------------------------------------------------------------------
  1197. // Function : privParseOptions()
  1198. // Description :
  1199. // This internal methods reads the variable list of arguments ($p_options_list,
  1200. // $p_size) and generate an array with the options and values ($v_result_list).
  1201. // $v_requested_options contains the options that can be present and those that
  1202. // must be present.
  1203. // $v_requested_options is an array, with the option value as key, and 'optional',
  1204. // or 'mandatory' as value.
  1205. // Parameters :
  1206. // See above.
  1207. // Return Values :
  1208. // 1 on success.
  1209. // 0 on failure.
  1210. // --------------------------------------------------------------------------------
  1211. public function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options = false)
  1212. {
  1213. $v_result=1;
  1214. // ----- Read the options
  1215. $i=0;
  1216. while ($i<$p_size) {
  1217. // ----- Check if the option is supported
  1218. if (!isset($v_requested_options[$p_options_list[$i]])) {
  1219. // ----- Error log
  1220. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
  1221. // ----- Return
  1222. return PclZip::errorCode();
  1223. }
  1224. // ----- Look for next option
  1225. switch ($p_options_list[$i]) {
  1226. // ----- Look for options that request a path value
  1227. case PCLZIP_OPT_PATH:
  1228. case PCLZIP_OPT_REMOVE_PATH:
  1229. case PCLZIP_OPT_ADD_PATH:
  1230. // ----- Check the number of parameters
  1231. if (($i+1) >= $p_size) {
  1232. // ----- Error log
  1233. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1234. // ----- Return
  1235. return PclZip::errorCode();
  1236. }
  1237. // ----- Get the value
  1238. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false);
  1239. $i++;
  1240. break;
  1241. case PCLZIP_OPT_TEMP_FILE_THRESHOLD:
  1242. // ----- Check the number of parameters
  1243. if (($i+1) >= $p_size) {
  1244. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1245. return PclZip::errorCode();
  1246. }
  1247. // ----- Check for incompatible options
  1248. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1249. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1250. return PclZip::errorCode();
  1251. }
  1252. // ----- Check the value
  1253. $v_value = $p_options_list[$i+1];
  1254. if ((!is_integer($v_value)) || ($v_value<0)) {
  1255. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1256. return PclZip::errorCode();
  1257. }
  1258. // ----- Get the value (and convert it in bytes)
  1259. $v_result_list[$p_options_list[$i]] = $v_value*1048576;
  1260. $i++;
  1261. break;
  1262. case PCLZIP_OPT_TEMP_FILE_ON:
  1263. // ----- Check for incompatible options
  1264. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1265. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1266. return PclZip::errorCode();
  1267. }
  1268. $v_result_list[$p_options_list[$i]] = true;
  1269. break;
  1270. case PCLZIP_OPT_TEMP_FILE_OFF:
  1271. // ----- Check for incompatible options
  1272. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
  1273. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
  1274. return PclZip::errorCode();
  1275. }
  1276. // ----- Check for incompatible options
  1277. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1278. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
  1279. return PclZip::errorCode();
  1280. }
  1281. $v_result_list[$p_options_list[$i]] = true;
  1282. break;
  1283. case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION:
  1284. // ----- Check the number of parameters
  1285. if (($i+1) >= $p_size) {
  1286. // ----- Error log
  1287. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1288. // ----- Return
  1289. return PclZip::errorCode();
  1290. }
  1291. // ----- Get the value
  1292. if (is_string($p_options_list[$i+1]) && ($p_options_list[$i+1] != '')) {
  1293. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false);
  1294. $i++;
  1295. } else {
  1296. }
  1297. break;
  1298. // ----- Look for options that request an array of string for value
  1299. case PCLZIP_OPT_BY_NAME:
  1300. // ----- Check the number of parameters
  1301. if (($i+1) >= $p_size) {
  1302. // ----- Error log
  1303. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1304. // ----- Return
  1305. return PclZip::errorCode();
  1306. }
  1307. // ----- Get the value
  1308. if (is_string($p_options_list[$i+1])) {
  1309. $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
  1310. } elseif (is_array($p_options_list[$i+1])) {
  1311. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1312. } else {
  1313. // ----- Error log
  1314. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1315. // ----- Return
  1316. return PclZip::errorCode();
  1317. }
  1318. $i++;
  1319. break;
  1320. // ----- Look for options that request an EREG or PREG expression
  1321. case PCLZIP_OPT_BY_EREG:
  1322. // ereg() is deprecated starting with PHP 5.3. Move PCLZIP_OPT_BY_EREG
  1323. // to PCLZIP_OPT_BY_PREG
  1324. $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
  1325. case PCLZIP_OPT_BY_PREG:
  1326. //case PCLZIP_OPT_CRYPT :
  1327. // ----- Check the number of parameters
  1328. if (($i+1) >= $p_size) {
  1329. // ----- Error log
  1330. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1331. // ----- Return
  1332. return PclZip::errorCode();
  1333. }
  1334. // ----- Get the value
  1335. if (is_string($p_options_list[$i+1])) {
  1336. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1337. } else {
  1338. // ----- Error log
  1339. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1340. // ----- Return
  1341. return PclZip::errorCode();
  1342. }
  1343. $i++;
  1344. break;
  1345. // ----- Look for options that takes a string
  1346. case PCLZIP_OPT_COMMENT:
  1347. case PCLZIP_OPT_ADD_COMMENT:
  1348. case PCLZIP_OPT_PREPEND_COMMENT:
  1349. // ----- Check the number of parameters
  1350. if (($i+1) >= $p_size) {
  1351. // ----- Error log
  1352. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1353. // ----- Return
  1354. return PclZip::errorCode();
  1355. }
  1356. // ----- Get the value
  1357. if (is_string($p_options_list[$i+1])) {
  1358. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1359. } else {
  1360. // ----- Error log
  1361. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" .PclZipUtilOptionText($p_options_list[$i]) ."'");
  1362. // ----- Return
  1363. return PclZip::errorCode();
  1364. }
  1365. $i++;
  1366. break;
  1367. // ----- Look for options that request an array of index
  1368. case PCLZIP_OPT_BY_INDEX:
  1369. // ----- Check the number of parameters
  1370. if (($i+1) >= $p_size) {
  1371. // ----- Error log
  1372. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1373. // ----- Return
  1374. return PclZip::errorCode();
  1375. }
  1376. // ----- Get the value
  1377. $v_work_list = array();
  1378. if (is_string($p_options_list[$i+1])) {
  1379. // ----- Remove spaces
  1380. $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
  1381. // ----- Parse items
  1382. $v_work_list = explode(",", $p_options_list[$i+1]);
  1383. } elseif (is_integer($p_options_list[$i+1])) {
  1384. $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
  1385. } elseif (is_array($p_options_list[$i+1])) {
  1386. $v_work_list = $p_options_list[$i+1];
  1387. } else {
  1388. // ----- Error log
  1389. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1390. // ----- Return
  1391. return PclZip::errorCode();
  1392. }
  1393. // ----- Reduce the index list
  1394. // each index item in the list must be a couple with a start and
  1395. // an end value : [0,3], [5-5], [8-10], ...
  1396. // ----- Check the format of each item
  1397. $v_sort_flag=false;
  1398. $v_sort_value=0;
  1399. for ($j=0; $j<sizeof($v_work_list); $j++) {
  1400. // ----- Explode the item
  1401. $v_item_list = explode("-", $v_work_list[$j]);
  1402. $v_size_item_list = sizeof($v_item_list);
  1403. // ----- TBC : Here we might check that each item is a
  1404. // real integer ...
  1405. // ----- Look for single value
  1406. if ($v_size_item_list == 1) {
  1407. // ----- Set the option value
  1408. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  1409. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  1410. } elseif ($v_size_item_list == 2) {
  1411. // ----- Set the option value
  1412. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  1413. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
  1414. } else {
  1415. // ----- Error log
  1416. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1417. // ----- Return
  1418. return PclZip::errorCode();
  1419. }
  1420. // ----- Look for list sort
  1421. if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  1422. $v_sort_flag=true;
  1423. // ----- TBC : An automatic sort should be writen ...
  1424. // ----- Error log
  1425. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1426. // ----- Return
  1427. return PclZip::errorCode();
  1428. }
  1429. $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  1430. }
  1431. // ----- Sort the items
  1432. if ($v_sort_flag) {
  1433. // TBC : To Be Completed
  1434. }
  1435. // ----- Next option
  1436. $i++;
  1437. break;
  1438. // ----- Look for options that request no value
  1439. case PCLZIP_OPT_REMOVE_ALL_PATH:
  1440. case PCLZIP_OPT_EXTRACT_AS_STRING:
  1441. case PCLZIP_OPT_NO_COMPRESSION:
  1442. case PCLZIP_OPT_EXTRACT_IN_OUTPUT:
  1443. case PCLZIP_OPT_REPLACE_NEWER:
  1444. case PCLZIP_OPT_STOP_ON_ERROR:
  1445. $v_result_list[$p_options_list[$i]] = true;
  1446. break;
  1447. // ----- Look for options that request an octal value
  1448. case PCLZIP_OPT_SET_CHMOD:
  1449. // ----- Check the number of parameters
  1450. if (($i+1) >= $p_size) {
  1451. // ----- Error log
  1452. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1453. // ----- Return
  1454. return PclZip::errorCode();
  1455. }
  1456. // ----- Get the value
  1457. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1458. $i++;
  1459. break;
  1460. // ----- Look for options that request a call-back
  1461. case PCLZIP_CB_PRE_EXTRACT:
  1462. case PCLZIP_CB_POST_EXTRACT:
  1463. case PCLZIP_CB_PRE_ADD:
  1464. case PCLZIP_CB_POST_ADD:
  1465. /* for futur use
  1466. case PCLZIP_CB_PRE_DELETE :
  1467. case PCLZIP_CB_POST_DELETE :
  1468. case PCLZIP_CB_PRE_LIST :
  1469. case PCLZIP_CB_POST_LIST :
  1470. */
  1471. // ----- Check the number of parameters
  1472. if (($i+1) >= $p_size) {
  1473. // ----- Error log
  1474. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1475. // ----- Return
  1476. return PclZip::errorCode();
  1477. }
  1478. // ----- Get the value
  1479. $v_function_name = $p_options_list[$i+1];
  1480. // ----- Check that the value is a valid existing function
  1481. if (!function_exists($v_function_name)) {
  1482. // ----- Error log
  1483. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1484. // ----- Return
  1485. return PclZip::errorCode();
  1486. }
  1487. // ----- Set the attribute
  1488. $v_result_list[$p_options_list[$i]] = $v_function_name;
  1489. $i++;
  1490. break;
  1491. default:
  1492. // ----- Error log
  1493. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Unknown parameter '" .$p_options_list[$i]."'");
  1494. // ----- Return
  1495. return PclZip::errorCode();
  1496. }
  1497. // ----- Next options
  1498. $i++;
  1499. }
  1500. // ----- Look for mandatory options
  1501. if ($v_requested_options !== false) {
  1502. for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  1503. // ----- Look for mandatory option
  1504. if ($v_requested_options[$key] == 'mandatory') {
  1505. // ----- Look if present
  1506. if (!isset($v_result_list[$key])) {
  1507. // ----- Error log
  1508. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1509. // ----- Return
  1510. return PclZip::errorCode();
  1511. }
  1512. }
  1513. }
  1514. }
  1515. // ----- Look for default values
  1516. if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1517. }
  1518. // ----- Return
  1519. return $v_result;
  1520. }
  1521. // --------------------------------------------------------------------------------
  1522. // --------------------------------------------------------------------------------
  1523. // Function : privOptionDefaultThreshold()
  1524. // Description :
  1525. // Parameters :
  1526. // Return Values :
  1527. // --------------------------------------------------------------------------------
  1528. public function privOptionDefaultThreshold(&$p_options)
  1529. {
  1530. $v_result=1;
  1531. if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]) || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1532. return $v_result;
  1533. }
  1534. // ----- Get 'memory_limit' configuration value
  1535. $v_memory_limit = ini_get('memory_limit');
  1536. $v_memory_limit = trim($v_memory_limit);
  1537. $last = strtolower(substr($v_memory_limit, -1));
  1538. if ($last == 'g') {
  1539. //$v_memory_limit = $v_memory_limit*1024*1024*1024;
  1540. $v_memory_limit = $v_memory_limit*1073741824;
  1541. }
  1542. if ($last == 'm') {
  1543. //$v_memory_limit = $v_memory_limit*1024*1024;
  1544. $v_memory_limit = $v_memory_limit*1048576;
  1545. }
  1546. if ($last == 'k') {
  1547. $v_memory_limit = $v_memory_limit*1024;
  1548. }
  1549. $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO);
  1550. // ----- Sanity check : No threshold if value lower than 1M
  1551. if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
  1552. unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
  1553. }
  1554. // ----- Return
  1555. return $v_result;
  1556. }
  1557. // --------------------------------------------------------------------------------
  1558. // --------------------------------------------------------------------------------
  1559. // Function : privFileDescrParseAtt()
  1560. // Description :
  1561. // Parameters :
  1562. // Return Values :
  1563. // 1 on success.
  1564. // 0 on failure.
  1565. // --------------------------------------------------------------------------------
  1566. public function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options = false)
  1567. {
  1568. $v_result=1;
  1569. // ----- For each file in the list check the attributes
  1570. foreach ($p_file_list as $v_key => $v_value) {
  1571. // ----- Check if the option is supported
  1572. if (!isset($v_requested_options[$v_key])) {
  1573. // ----- Error log
  1574. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
  1575. // ----- Return
  1576. return PclZip::errorCode();
  1577. }
  1578. // ----- Look for attribute
  1579. switch ($v_key) {
  1580. case PCLZIP_ATT_FILE_NAME:
  1581. if (!is_string($v_value)) {
  1582. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1583. return PclZip::errorCode();
  1584. }
  1585. $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
  1586. if ($p_filedescr['filename'] == '') {
  1587. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1588. return PclZip::errorCode();
  1589. }
  1590. break;
  1591. case PCLZIP_ATT_FILE_NEW_SHORT_NAME:
  1592. if (!is_string($v_value)) {
  1593. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1594. return PclZip::errorCode();
  1595. }
  1596. $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
  1597. if ($p_filedescr['new_short_name'] == '') {
  1598. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1599. return PclZip::errorCode();
  1600. }
  1601. break;
  1602. case PCLZIP_ATT_FILE_NEW_FULL_NAME:
  1603. if (!is_string($v_value)) {
  1604. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1605. return PclZip::errorCode();
  1606. }
  1607. $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
  1608. if ($p_filedescr['new_full_name'] == '') {
  1609. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1610. return PclZip::errorCode();
  1611. }
  1612. break;
  1613. // ----- Look for options that takes a string
  1614. case PCLZIP_ATT_FILE_COMMENT:
  1615. if (!is_string($v_value)) {
  1616. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1617. return PclZip::errorCode();
  1618. }
  1619. $p_filedescr['comment'] = $v_value;
  1620. break;
  1621. case PCLZIP_ATT_FILE_MTIME:
  1622. if (!is_integer($v_value)) {
  1623. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1624. return PclZip::errorCode();
  1625. }
  1626. $p_filedescr['mtime'] = $v_value;
  1627. break;
  1628. case PCLZIP_ATT_FILE_CONTENT:
  1629. $p_filedescr['content'] = $v_value;
  1630. break;
  1631. default:
  1632. // ----- Error log
  1633. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Unknown parameter '".$v_key."'");
  1634. // ----- Return
  1635. return PclZip::errorCode();
  1636. }
  1637. // ----- Look for mandatory options
  1638. if ($v_requested_options !== false) {
  1639. for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
  1640. // ----- Look for mandatory option
  1641. if ($v_requested_options[$key] == 'mandatory') {
  1642. // ----- Look if present
  1643. if (!isset($p_file_list[$key])) {
  1644. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1645. return PclZip::errorCode();
  1646. }
  1647. }
  1648. }
  1649. }
  1650. }
  1651. // ----- Return
  1652. return $v_result;
  1653. }
  1654. // --------------------------------------------------------------------------------
  1655. // --------------------------------------------------------------------------------
  1656. // Function : privFileDescrExpand()
  1657. // Description :
  1658. // This method look for each item of the list to see if its a file, a folder
  1659. // or a string to be added as file. For any other type of files (link, other)
  1660. // just ignore the item.
  1661. // Then prepare the information that will be stored for that file.
  1662. // When its a folder, expand the folder with all the files that are in that
  1663. // folder (recursively).
  1664. // Parameters :
  1665. // Return Values :
  1666. // 1 on success.
  1667. // 0 on failure.
  1668. // --------------------------------------------------------------------------------
  1669. public function privFileDescrExpand(&$p_filedescr_list, &$p_options)
  1670. {
  1671. $v_result=1;
  1672. // ----- Create a result list
  1673. $v_result_list = array();
  1674. // ----- Look each entry
  1675. for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
  1676. // ----- Get filedescr
  1677. $v_descr = $p_filedescr_list[$i];
  1678. // ----- Reduce the filename
  1679. $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
  1680. $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
  1681. // ----- Look for real file or folder
  1682. if (file_exists($v_descr['filename'])) {
  1683. if (@is_file($v_descr['filename'])) {
  1684. $v_descr['type'] = 'file';
  1685. } elseif (@is_dir($v_descr['filename'])) {
  1686. $v_descr['type'] = 'folder';
  1687. } elseif (@is_link($v_descr['filename'])) {
  1688. // skip
  1689. continue;
  1690. } else {
  1691. // skip
  1692. continue;
  1693. }
  1694. } elseif (isset($v_descr['content'])) {
  1695. // ----- Look for string added as file
  1696. $v_descr['type'] = 'virtual_file';
  1697. } else {
  1698. // ----- Missing file
  1699. // ----- Error log
  1700. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exist");
  1701. // ----- Return
  1702. return PclZip::errorCode();
  1703. }
  1704. // ----- Calculate the stored filename
  1705. $this->privCalculateStoredFilename($v_descr, $p_options);
  1706. // ----- Add the descriptor in result list
  1707. $v_result_list[sizeof($v_result_list)] = $v_descr;
  1708. // ----- Look for folder
  1709. if ($v_descr['type'] == 'folder') {
  1710. // ----- List of items in folder
  1711. $v_dirlist_descr = array();
  1712. $v_dirlist_nb = 0;
  1713. if ($v_folder_handler = @opendir($v_descr['filename'])) {
  1714. while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
  1715. // ----- Skip '.' and '..'
  1716. if (($v_item_handler == '.') || ($v_item_handler == '..')) {
  1717. continue;
  1718. }
  1719. // ----- Compose the full filename
  1720. $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
  1721. // ----- Look for different stored filename
  1722. // Because the name of the folder was changed, the name of the
  1723. // files/sub-folders also change
  1724. if (($v_descr['stored_filename'] != $v_descr['filename'])
  1725. && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
  1726. if ($v_descr['stored_filename'] != '') {
  1727. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
  1728. } else {
  1729. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
  1730. }
  1731. }
  1732. $v_dirlist_nb++;
  1733. }
  1734. @closedir($v_folder_handler);
  1735. } else {
  1736. // TBC : unable to open folder in read mode
  1737. }
  1738. // ----- Expand each element of the list
  1739. if ($v_dirlist_nb != 0) {
  1740. // ----- Expand
  1741. if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
  1742. return $v_result;
  1743. }
  1744. // ----- Concat the resulting list
  1745. $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
  1746. }
  1747. // ----- Free local array
  1748. unset($v_dirlist_descr);
  1749. }
  1750. }
  1751. // ----- Get the result list
  1752. $p_filedescr_list = $v_result_list;
  1753. // ----- Return
  1754. return $v_result;
  1755. }
  1756. // --------------------------------------------------------------------------------
  1757. // --------------------------------------------------------------------------------
  1758. // Function : privCreate()
  1759. // Description :
  1760. // Parameters :
  1761. // Return Values :
  1762. // --------------------------------------------------------------------------------
  1763. public function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
  1764. {
  1765. $v_result=1;
  1766. $v_list_detail = array();
  1767. // ----- Magic quotes trick
  1768. $this->privDisableMagicQuotes();
  1769. // ----- Open the file in write mode
  1770. if (($v_result = $this->privOpenFd('wb')) != 1) {
  1771. // ----- Return
  1772. return $v_result;
  1773. }
  1774. // ----- Add the list of files
  1775. $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
  1776. // ----- Close
  1777. $this->privCloseFd();
  1778. // ----- Magic quotes trick
  1779. $this->privSwapBackMagicQuotes();
  1780. // ----- Return
  1781. return $v_result;
  1782. }
  1783. // --------------------------------------------------------------------------------
  1784. // --------------------------------------------------------------------------------
  1785. // Function : privAdd()
  1786. // Description :
  1787. // Parameters :
  1788. // Return Values :
  1789. // --------------------------------------------------------------------------------
  1790. public function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
  1791. {
  1792. $v_result=1;
  1793. $v_list_detail = array();
  1794. // ----- Look if the archive exists or is empty
  1795. if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0)) {
  1796. // ----- Do a create
  1797. $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
  1798. // ----- Return
  1799. return $v_result;
  1800. }
  1801. // ----- Magic quotes trick
  1802. $this->privDisableMagicQuotes();
  1803. // ----- Open the zip file
  1804. if (($v_result=$this->privOpenFd('rb')) != 1) {
  1805. // ----- Magic quotes trick
  1806. $this->privSwapBackMagicQuotes();
  1807. // ----- Return
  1808. return $v_result;
  1809. }
  1810. // ----- Read the central directory informations
  1811. $v_central_dir = array();
  1812. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  1813. $this->privCloseFd();
  1814. $this->privSwapBackMagicQuotes();
  1815. return $v_result;
  1816. }
  1817. // ----- Go to beginning of File
  1818. @rewind($this->zip_fd);
  1819. // ----- Creates a temporay file
  1820. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  1821. // ----- Open the temporary file in write mode
  1822. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  1823. $this->privCloseFd();
  1824. $this->privSwapBackMagicQuotes();
  1825. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  1826. // ----- Return
  1827. return PclZip::errorCode();
  1828. }
  1829. // ----- Copy the files from the archive to the temporary file
  1830. // TBC : Here I should better append the file and go back to erase the central dir
  1831. $v_size = $v_central_dir['offset'];
  1832. while ($v_size != 0) {
  1833. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1834. $v_buffer = fread($this->zip_fd, $v_read_size);
  1835. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  1836. $v_size -= $v_read_size;
  1837. }
  1838. // ----- Swap the file descriptor
  1839. // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  1840. // the following methods on the temporary fil and not the real archive
  1841. $v_swap = $this->zip_fd;
  1842. $this->zip_fd = $v_zip_temp_fd;
  1843. $v_zip_temp_fd = $v_swap;
  1844. // ----- Add the files
  1845. $v_header_list = array();
  1846. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
  1847. fclose($v_zip_temp_fd);
  1848. $this->privCloseFd();
  1849. @unlink($v_zip_temp_name);
  1850. $this->privSwapBackMagicQuotes();
  1851. // ----- Return
  1852. return $v_result;
  1853. }
  1854. // ----- Store the offset of the central dir
  1855. $v_offset = @ftell($this->zip_fd);
  1856. // ----- Copy the block of file headers from the old archive
  1857. $v_size = $v_central_dir['size'];
  1858. while ($v_size != 0) {
  1859. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1860. $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  1861. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1862. $v_size -= $v_read_size;
  1863. }
  1864. // ----- Create the Central Dir files header
  1865. for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++) {
  1866. // ----- Create the file header
  1867. if ($v_header_list[$i]['status'] == 'ok') {
  1868. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1869. fclose($v_zip_temp_fd);
  1870. $this->privCloseFd();
  1871. @unlink($v_zip_temp_name);
  1872. $this->privSwapBackMagicQuotes();
  1873. // ----- Return
  1874. return $v_result;
  1875. }
  1876. $v_count++;
  1877. }
  1878. // ----- Transform the header to a 'usable' info
  1879. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1880. }
  1881. // ----- Zip file comment
  1882. $v_comment = $v_central_dir['comment'];
  1883. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1884. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1885. }
  1886. if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
  1887. $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
  1888. }
  1889. if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
  1890. $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
  1891. }
  1892. // ----- Calculate the size of the central header
  1893. $v_size = @ftell($this->zip_fd)-$v_offset;
  1894. // ----- Create the central dir footer
  1895. if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1) {
  1896. // ----- Reset the file list
  1897. unset($v_header_list);
  1898. $this->privSwapBackMagicQuotes();
  1899. // ----- Return
  1900. return $v_result;
  1901. }
  1902. // ----- Swap back the file descriptor
  1903. $v_swap = $this->zip_fd;
  1904. $this->zip_fd = $v_zip_temp_fd;
  1905. $v_zip_temp_fd = $v_swap;
  1906. // ----- Close
  1907. $this->privCloseFd();
  1908. // ----- Close the temporary file
  1909. @fclose($v_zip_temp_fd);
  1910. // ----- Magic quotes trick
  1911. $this->privSwapBackMagicQuotes();
  1912. // ----- Delete the zip file
  1913. // TBC : I should test the result ...
  1914. @unlink($this->zipname);
  1915. // ----- Rename the temporary file
  1916. // TBC : I should test the result ...
  1917. //@rename($v_zip_temp_name, $this->zipname);
  1918. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  1919. // ----- Return
  1920. return $v_result;
  1921. }
  1922. // --------------------------------------------------------------------------------
  1923. // --------------------------------------------------------------------------------
  1924. // Function : privOpenFd()
  1925. // Description :
  1926. // Parameters :
  1927. // --------------------------------------------------------------------------------
  1928. public function privOpenFd($p_mode)
  1929. {
  1930. $v_result=1;
  1931. // ----- Look if already open
  1932. if ($this->zip_fd != 0) {
  1933. // ----- Error log
  1934. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
  1935. // ----- Return
  1936. return PclZip::errorCode();
  1937. }
  1938. // ----- Open the zip file
  1939. if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0) {
  1940. // ----- Error log
  1941. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
  1942. // ----- Return
  1943. return PclZip::errorCode();
  1944. }
  1945. // ----- Return
  1946. return $v_result;
  1947. }
  1948. // --------------------------------------------------------------------------------
  1949. // --------------------------------------------------------------------------------
  1950. // Function : privCloseFd()
  1951. // Description :
  1952. // Parameters :
  1953. // --------------------------------------------------------------------------------
  1954. public function privCloseFd()
  1955. {
  1956. $v_result=1;
  1957. if ($this->zip_fd != 0) {
  1958. @fclose($this->zip_fd);
  1959. }
  1960. $this->zip_fd = 0;
  1961. // ----- Return
  1962. return $v_result;
  1963. }
  1964. // --------------------------------------------------------------------------------
  1965. // --------------------------------------------------------------------------------
  1966. // Function : privAddList()
  1967. // Description :
  1968. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  1969. // different from the real path of the file. This is usefull if you want to have PclTar
  1970. // running in any directory, and memorize relative path from an other directory.
  1971. // Parameters :
  1972. // $p_list : An array containing the file or directory names to add in the tar
  1973. // $p_result_list : list of added files with their properties (specially the status field)
  1974. // $p_add_dir : Path to add in the filename path archived
  1975. // $p_remove_dir : Path to remove in the filename path archived
  1976. // Return Values :
  1977. // --------------------------------------------------------------------------------
  1978. // public function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  1979. public function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
  1980. {
  1981. $v_result=1;
  1982. // ----- Add the files
  1983. $v_header_list = array();
  1984. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
  1985. // ----- Return
  1986. return $v_result;
  1987. }
  1988. // ----- Store the offset of the central dir
  1989. $v_offset = @ftell($this->zip_fd);
  1990. // ----- Create the Central Dir files header
  1991. for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++) {
  1992. // ----- Create the file header
  1993. if ($v_header_list[$i]['status'] == 'ok') {
  1994. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1995. // ----- Return
  1996. return $v_result;
  1997. }
  1998. $v_count++;
  1999. }
  2000. // ----- Transform the header to a 'usable' info
  2001. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  2002. }
  2003. // ----- Zip file comment
  2004. $v_comment = '';
  2005. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  2006. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  2007. }
  2008. // ----- Calculate the size of the central header
  2009. $v_size = @ftell($this->zip_fd)-$v_offset;
  2010. // ----- Create the central dir footer
  2011. if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1) {
  2012. // ----- Reset the file list
  2013. unset($v_header_list);
  2014. // ----- Return
  2015. return $v_result;
  2016. }
  2017. // ----- Return
  2018. return $v_result;
  2019. }
  2020. // --------------------------------------------------------------------------------
  2021. // --------------------------------------------------------------------------------
  2022. // Function : privAddFileList()
  2023. // Description :
  2024. // Parameters :
  2025. // $p_filedescr_list : An array containing the file description
  2026. // or directory names to add in the zip
  2027. // $p_result_list : list of added files with their properties (specially the status field)
  2028. // Return Values :
  2029. // --------------------------------------------------------------------------------
  2030. public function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
  2031. {
  2032. $v_result=1;
  2033. $v_header = array();
  2034. // ----- Recuperate the current number of elt in list
  2035. $v_nb = sizeof($p_result_list);
  2036. // ----- Loop on the files
  2037. for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
  2038. // ----- Format the filename
  2039. $p_filedescr_list[$j]['filename'] = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
  2040. // ----- Skip empty file names
  2041. // TBC : Can this be possible ? not checked in DescrParseAtt ?
  2042. if ($p_filedescr_list[$j]['filename'] == "") {
  2043. continue;
  2044. }
  2045. // ----- Check the filename
  2046. if (($p_filedescr_list[$j]['type'] != 'virtual_file') && (!file_exists($p_filedescr_list[$j]['filename']))) {
  2047. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
  2048. return PclZip::errorCode();
  2049. }
  2050. // ----- Look if it is a file or a dir with no all path remove option
  2051. // or a dir with all its path removed
  2052. // if ( (is_file($p_filedescr_list[$j]['filename']))
  2053. // || ( is_dir($p_filedescr_list[$j]['filename'])
  2054. if (($p_filedescr_list[$j]['type'] == 'file') || ($p_filedescr_list[$j]['type'] == 'virtual_file') || (($p_filedescr_list[$j]['type'] == 'folder') && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]) || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
  2055. // ----- Add the file
  2056. $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header, $p_options);
  2057. if ($v_result != 1) {
  2058. return $v_result;
  2059. }
  2060. // ----- Store the file infos
  2061. $p_result_list[$v_nb++] = $v_header;
  2062. }
  2063. }
  2064. // ----- Return
  2065. return $v_result;
  2066. }
  2067. // --------------------------------------------------------------------------------
  2068. // --------------------------------------------------------------------------------
  2069. // Function : privAddFile()
  2070. // Description :
  2071. // Parameters :
  2072. // Return Values :
  2073. // --------------------------------------------------------------------------------
  2074. public function privAddFile($p_filedescr, &$p_header, &$p_options)
  2075. {
  2076. $v_result=1;
  2077. // ----- Working variable
  2078. $p_filename = $p_filedescr['filename'];
  2079. // TBC : Already done in the fileAtt check ... ?
  2080. if ($p_filename == "") {
  2081. // ----- Error log
  2082. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  2083. // ----- Return
  2084. return PclZip::errorCode();
  2085. }
  2086. // ----- Look for a stored different filename
  2087. /* TBC : Removed
  2088. if (isset($p_filedescr['stored_filename'])) {
  2089. $v_stored_filename = $p_filedescr['stored_filename'];
  2090. }
  2091. else {
  2092. $v_stored_filename = $p_filedescr['stored_filename'];
  2093. }
  2094. */
  2095. // ----- Set the file properties
  2096. clearstatcache();
  2097. $p_header['version'] = 20;
  2098. $p_header['version_extracted'] = 10;
  2099. $p_header['flag'] = 0;
  2100. $p_header['compression'] = 0;
  2101. $p_header['crc'] = 0;
  2102. $p_header['compressed_size'] = 0;
  2103. $p_header['filename_len'] = strlen($p_filename);
  2104. $p_header['extra_len'] = 0;
  2105. $p_header['disk'] = 0;
  2106. $p_header['internal'] = 0;
  2107. $p_header['offset'] = 0;
  2108. $p_header['filename'] = $p_filename;
  2109. // TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
  2110. $p_header['stored_filename'] = $p_filedescr['stored_filename'];
  2111. $p_header['extra'] = '';
  2112. $p_header['status'] = 'ok';
  2113. $p_header['index'] = -1;
  2114. // ----- Look for regular file
  2115. if ($p_filedescr['type']=='file') {
  2116. $p_header['external'] = 0x00000000;
  2117. $p_header['size'] = filesize($p_filename);
  2118. } elseif ($p_filedescr['type']=='folder') {
  2119. // ----- Look for regular folder
  2120. $p_header['external'] = 0x00000010;
  2121. $p_header['mtime'] = filemtime($p_filename);
  2122. $p_header['size'] = filesize($p_filename);
  2123. } elseif ($p_filedescr['type'] == 'virtual_file') {
  2124. // ----- Look for virtual file
  2125. $p_header['external'] = 0x00000000;
  2126. $p_header['size'] = strlen($p_filedescr['content']);
  2127. }
  2128. // ----- Look for filetime
  2129. if (isset($p_filedescr['mtime'])) {
  2130. $p_header['mtime'] = $p_filedescr['mtime'];
  2131. } elseif ($p_filedescr['type'] == 'virtual_file') {
  2132. $p_header['mtime'] = time();
  2133. } else {
  2134. $p_header['mtime'] = filemtime($p_filename);
  2135. }
  2136. // ------ Look for file comment
  2137. if (isset($p_filedescr['comment'])) {
  2138. $p_header['comment_len'] = strlen($p_filedescr['comment']);
  2139. $p_header['comment'] = $p_filedescr['comment'];
  2140. } else {
  2141. $p_header['comment_len'] = 0;
  2142. $p_header['comment'] = '';
  2143. }
  2144. // ----- Look for pre-add callback
  2145. if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  2146. // ----- Generate a local information
  2147. $v_local_header = array();
  2148. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2149. // ----- Call the callback
  2150. // Here I do not use call_user_func() because I need to send a reference to the
  2151. // header.
  2152. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
  2153. $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
  2154. if ($v_result == 0) {
  2155. // ----- Change the file status
  2156. $p_header['status'] = "skipped";
  2157. $v_result = 1;
  2158. }
  2159. // ----- Update the informations
  2160. // Only some fields can be modified
  2161. if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  2162. $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  2163. }
  2164. }
  2165. // ----- Look for empty stored filename
  2166. if ($p_header['stored_filename'] == "") {
  2167. $p_header['status'] = "filtered";
  2168. }
  2169. // ----- Check the path length
  2170. if (strlen($p_header['stored_filename']) > 0xFF) {
  2171. $p_header['status'] = 'filename_too_long';
  2172. }
  2173. // ----- Look if no error, or file not skipped
  2174. if ($p_header['status'] == 'ok') {
  2175. // ----- Look for a file
  2176. if ($p_filedescr['type'] == 'file') {
  2177. // ----- Look for using temporary file to zip
  2178. if ((!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON]) || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]) && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])))) {
  2179. $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
  2180. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  2181. return $v_result;
  2182. }
  2183. } else {
  2184. // ----- Use "in memory" zip algo
  2185. // ----- Open the source file
  2186. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2187. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2188. return PclZip::errorCode();
  2189. }
  2190. // ----- Read the file content
  2191. $v_content = @fread($v_file, $p_header['size']);
  2192. // ----- Close the file
  2193. @fclose($v_file);
  2194. // ----- Calculate the CRC
  2195. $p_header['crc'] = @crc32($v_content);
  2196. // ----- Look for no compression
  2197. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2198. // ----- Set header parameters
  2199. $p_header['compressed_size'] = $p_header['size'];
  2200. $p_header['compression'] = 0;
  2201. } else {
  2202. // ----- Look for normal compression
  2203. // ----- Compress the content
  2204. $v_content = @gzdeflate($v_content);
  2205. // ----- Set header parameters
  2206. $p_header['compressed_size'] = strlen($v_content);
  2207. $p_header['compression'] = 8;
  2208. }
  2209. // ----- Call the header generation
  2210. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2211. @fclose($v_file);
  2212. return $v_result;
  2213. }
  2214. // ----- Write the compressed (or not) content
  2215. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2216. }
  2217. } elseif ($p_filedescr['type'] == 'virtual_file') {
  2218. // ----- Look for a virtual file (a file from string)
  2219. $v_content = $p_filedescr['content'];
  2220. // ----- Calculate the CRC
  2221. $p_header['crc'] = @crc32($v_content);
  2222. // ----- Look for no compression
  2223. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2224. // ----- Set header parameters
  2225. $p_header['compressed_size'] = $p_header['size'];
  2226. $p_header['compression'] = 0;
  2227. } else {
  2228. // ----- Look for normal compression
  2229. // ----- Compress the content
  2230. $v_content = @gzdeflate($v_content);
  2231. // ----- Set header parameters
  2232. $p_header['compressed_size'] = strlen($v_content);
  2233. $p_header['compression'] = 8;
  2234. }
  2235. // ----- Call the header generation
  2236. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2237. @fclose($v_file);
  2238. return $v_result;
  2239. }
  2240. // ----- Write the compressed (or not) content
  2241. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2242. } elseif ($p_filedescr['type'] == 'folder') {
  2243. // ----- Look for a directory
  2244. // ----- Look for directory last '/'
  2245. if (@substr($p_header['stored_filename'], -1) != '/') {
  2246. $p_header['stored_filename'] .= '/';
  2247. }
  2248. // ----- Set the file properties
  2249. $p_header['size'] = 0;
  2250. //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
  2251. $p_header['external'] = 0x00000010; // Value for a folder : to be checked
  2252. // ----- Call the header generation
  2253. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2254. return $v_result;
  2255. }
  2256. }
  2257. }
  2258. // ----- Look for post-add callback
  2259. if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  2260. // ----- Generate a local information
  2261. $v_local_header = array();
  2262. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2263. // ----- Call the callback
  2264. // Here I do not use call_user_func() because I need to send a reference to the
  2265. // header.
  2266. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
  2267. $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
  2268. if ($v_result == 0) {
  2269. // ----- Ignored
  2270. $v_result = 1;
  2271. }
  2272. // ----- Update the informations
  2273. // Nothing can be modified
  2274. }
  2275. // ----- Return
  2276. return $v_result;
  2277. }
  2278. // --------------------------------------------------------------------------------
  2279. // --------------------------------------------------------------------------------
  2280. // Function : privAddFileUsingTempFile()
  2281. // Description :
  2282. // Parameters :
  2283. // Return Values :
  2284. // --------------------------------------------------------------------------------
  2285. public function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
  2286. {
  2287. $v_result=PCLZIP_ERR_NO_ERROR;
  2288. // ----- Working variable
  2289. $p_filename = $p_filedescr['filename'];
  2290. // ----- Open the source file
  2291. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2292. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2293. return PclZip::errorCode();
  2294. }
  2295. // ----- Creates a compressed temporary file
  2296. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  2297. if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
  2298. fclose($v_file);
  2299. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  2300. return PclZip::errorCode();
  2301. }
  2302. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  2303. $v_size = filesize($p_filename);
  2304. while ($v_size != 0) {
  2305. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2306. $v_buffer = @fread($v_file, $v_read_size);
  2307. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2308. @gzputs($v_file_compressed, $v_buffer, $v_read_size);
  2309. $v_size -= $v_read_size;
  2310. }
  2311. // ----- Close the file
  2312. @fclose($v_file);
  2313. @gzclose($v_file_compressed);
  2314. // ----- Check the minimum file size
  2315. if (filesize($v_gzip_temp_name) < 18) {
  2316. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
  2317. return PclZip::errorCode();
  2318. }
  2319. // ----- Extract the compressed attributes
  2320. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  2321. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  2322. return PclZip::errorCode();
  2323. }
  2324. // ----- Read the gzip file header
  2325. $v_binary_data = @fread($v_file_compressed, 10);
  2326. $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
  2327. // ----- Check some parameters
  2328. $v_data_header['os'] = bin2hex($v_data_header['os']);
  2329. // ----- Read the gzip file footer
  2330. @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
  2331. $v_binary_data = @fread($v_file_compressed, 8);
  2332. $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
  2333. // ----- Set the attributes
  2334. $p_header['compression'] = ord($v_data_header['cm']);
  2335. //$p_header['mtime'] = $v_data_header['mtime'];
  2336. $p_header['crc'] = $v_data_footer['crc'];
  2337. $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
  2338. // ----- Close the file
  2339. @fclose($v_file_compressed);
  2340. // ----- Call the header generation
  2341. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2342. return $v_result;
  2343. }
  2344. // ----- Add the compressed data
  2345. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  2346. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  2347. return PclZip::errorCode();
  2348. }
  2349. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  2350. fseek($v_file_compressed, 10);
  2351. $v_size = $p_header['compressed_size'];
  2352. while ($v_size != 0) {
  2353. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2354. $v_buffer = @fread($v_file_compressed, $v_read_size);
  2355. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2356. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2357. $v_size -= $v_read_size;
  2358. }
  2359. // ----- Close the file
  2360. @fclose($v_file_compressed);
  2361. // ----- Unlink the temporary file
  2362. @unlink($v_gzip_temp_name);
  2363. // ----- Return
  2364. return $v_result;
  2365. }
  2366. // --------------------------------------------------------------------------------
  2367. // --------------------------------------------------------------------------------
  2368. // Function : privCalculateStoredFilename()
  2369. // Description :
  2370. // Based on file descriptor properties and global options, this method
  2371. // calculate the filename that will be stored in the archive.
  2372. // Parameters :
  2373. // Return Values :
  2374. // --------------------------------------------------------------------------------
  2375. public function privCalculateStoredFilename(&$p_filedescr, &$p_options)
  2376. {
  2377. $v_result=1;
  2378. // ----- Working variables
  2379. $p_filename = $p_filedescr['filename'];
  2380. if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
  2381. $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
  2382. } else {
  2383. $p_add_dir = '';
  2384. }
  2385. if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
  2386. $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
  2387. } else {
  2388. $p_remove_dir = '';
  2389. }
  2390. if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  2391. $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  2392. } else {
  2393. $p_remove_all_dir = 0;
  2394. }
  2395. // ----- Look for full name change
  2396. if (isset($p_filedescr['new_full_name'])) {
  2397. // ----- Remove drive letter if any
  2398. $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
  2399. } else {
  2400. // ----- Look for path and/or short name change
  2401. // ----- Look for short name change
  2402. // Its when we cahnge just the filename but not the path
  2403. if (isset($p_filedescr['new_short_name'])) {
  2404. $v_path_info = pathinfo($p_filename);
  2405. $v_dir = '';
  2406. if ($v_path_info['dirname'] != '') {
  2407. $v_dir = $v_path_info['dirname'].'/';
  2408. }
  2409. $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
  2410. } else {
  2411. // ----- Calculate the stored filename
  2412. $v_stored_filename = $p_filename;
  2413. }
  2414. // ----- Look for all path to remove
  2415. if ($p_remove_all_dir) {
  2416. $v_stored_filename = basename($p_filename);
  2417. } elseif ($p_remove_dir != "") {
  2418. // ----- Look for partial path remove
  2419. if (substr($p_remove_dir, -1) != '/') {
  2420. $p_remove_dir .= "/";
  2421. }
  2422. if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./")) {
  2423. if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./")) {
  2424. $p_remove_dir = "./".$p_remove_dir;
  2425. }
  2426. if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./")) {
  2427. $p_remove_dir = substr($p_remove_dir, 2);
  2428. }
  2429. }
  2430. $v_compare = PclZipUtilPathInclusion($p_remove_dir, $v_stored_filename);
  2431. if ($v_compare > 0) {
  2432. if ($v_compare == 2) {
  2433. $v_stored_filename = "";
  2434. } else {
  2435. $v_stored_filename = substr($v_stored_filename, strlen($p_remove_dir));
  2436. }
  2437. }
  2438. }
  2439. // ----- Remove drive letter if any
  2440. $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
  2441. // ----- Look for path to add
  2442. if ($p_add_dir != "") {
  2443. if (substr($p_add_dir, -1) == "/") {
  2444. $v_stored_filename = $p_add_dir.$v_stored_filename;
  2445. } else {
  2446. $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  2447. }
  2448. }
  2449. }
  2450. // ----- Filename (reduce the path of stored name)
  2451. $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  2452. $p_filedescr['stored_filename'] = $v_stored_filename;
  2453. // ----- Return
  2454. return $v_result;
  2455. }
  2456. // --------------------------------------------------------------------------------
  2457. // --------------------------------------------------------------------------------
  2458. // Function : privWriteFileHeader()
  2459. // Description :
  2460. // Parameters :
  2461. // Return Values :
  2462. // --------------------------------------------------------------------------------
  2463. public function privWriteFileHeader(&$p_header)
  2464. {
  2465. $v_result=1;
  2466. // ----- Store the offset position of the file
  2467. $p_header['offset'] = ftell($this->zip_fd);
  2468. // ----- Transform UNIX mtime to DOS format mdate/mtime
  2469. $v_date = getdate($p_header['mtime']);
  2470. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  2471. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  2472. // ----- Packed data
  2473. $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50, $p_header['version_extracted'], $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'], $p_header['compressed_size'], $p_header['size'], strlen($p_header['stored_filename']), $p_header['extra_len']);
  2474. // ----- Write the first 148 bytes of the header in the archive
  2475. fputs($this->zip_fd, $v_binary_data, 30);
  2476. // ----- Write the variable fields
  2477. if (strlen($p_header['stored_filename']) != 0) {
  2478. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  2479. }
  2480. if ($p_header['extra_len'] != 0) {
  2481. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  2482. }
  2483. // ----- Return
  2484. return $v_result;
  2485. }
  2486. // --------------------------------------------------------------------------------
  2487. // --------------------------------------------------------------------------------
  2488. // Function : privWriteCentralFileHeader()
  2489. // Description :
  2490. // Parameters :
  2491. // Return Values :
  2492. // --------------------------------------------------------------------------------
  2493. public function privWriteCentralFileHeader(&$p_header)
  2494. {
  2495. $v_result=1;
  2496. // TBC
  2497. //for(reset($p_header); $key = key($p_header); next($p_header)) {
  2498. //}
  2499. // ----- Transform UNIX mtime to DOS format mdate/mtime
  2500. $v_date = getdate($p_header['mtime']);
  2501. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  2502. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  2503. // ----- Packed data
  2504. $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50, $p_header['version'], $p_header['version_extracted'], $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'], $p_header['compressed_size'], $p_header['size'], strlen($p_header['stored_filename']), $p_header['extra_len'], $p_header['comment_len'], $p_header['disk'], $p_header['internal'], $p_header['external'], $p_header['offset']);
  2505. // ----- Write the 42 bytes of the header in the zip file
  2506. fputs($this->zip_fd, $v_binary_data, 46);
  2507. // ----- Write the variable fields
  2508. if (strlen($p_header['stored_filename']) != 0) {
  2509. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  2510. }
  2511. if ($p_header['extra_len'] != 0) {
  2512. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  2513. }
  2514. if ($p_header['comment_len'] != 0) {
  2515. fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  2516. }
  2517. // ----- Return
  2518. return $v_result;
  2519. }
  2520. // --------------------------------------------------------------------------------
  2521. // --------------------------------------------------------------------------------
  2522. // Function : privWriteCentralHeader()
  2523. // Description :
  2524. // Parameters :
  2525. // Return Values :
  2526. // --------------------------------------------------------------------------------
  2527. public function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  2528. {
  2529. $v_result = 1;
  2530. // ----- Packed data
  2531. $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries, $p_nb_entries, $p_size, $p_offset, strlen($p_comment));
  2532. // ----- Write the 22 bytes of the header in the zip file
  2533. fputs($this->zip_fd, $v_binary_data, 22);
  2534. // ----- Write the variable fields
  2535. if (strlen($p_comment) != 0) {
  2536. fputs($this->zip_fd, $p_comment, strlen($p_comment));
  2537. }
  2538. // ----- Return
  2539. return $v_result;
  2540. }
  2541. // --------------------------------------------------------------------------------
  2542. // --------------------------------------------------------------------------------
  2543. // Function : privList()
  2544. // Description :
  2545. // Parameters :
  2546. // Return Values :
  2547. // --------------------------------------------------------------------------------
  2548. public function privList(&$p_list)
  2549. {
  2550. $v_result = 1;
  2551. // ----- Magic quotes trick
  2552. $this->privDisableMagicQuotes();
  2553. // ----- Open the zip file
  2554. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
  2555. // ----- Magic quotes trick
  2556. $this->privSwapBackMagicQuotes();
  2557. // ----- Error log
  2558. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  2559. // ----- Return
  2560. return PclZip::errorCode();
  2561. }
  2562. // ----- Read the central directory informations
  2563. $v_central_dir = array();
  2564. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  2565. $this->privSwapBackMagicQuotes();
  2566. return $v_result;
  2567. }
  2568. // ----- Go to beginning of Central Dir
  2569. @rewind($this->zip_fd);
  2570. if (@fseek($this->zip_fd, $v_central_dir['offset'])) {
  2571. $this->privSwapBackMagicQuotes();
  2572. // ----- Error log
  2573. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2574. // ----- Return
  2575. return PclZip::errorCode();
  2576. }
  2577. // ----- Read each entry
  2578. for ($i=0; $i<$v_central_dir['entries']; $i++) {
  2579. // ----- Read the file header
  2580. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
  2581. $this->privSwapBackMagicQuotes();
  2582. return $v_result;
  2583. }
  2584. $v_header['index'] = $i;
  2585. // ----- Get the only interesting attributes
  2586. $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
  2587. unset($v_header);
  2588. }
  2589. // ----- Close the zip file
  2590. $this->privCloseFd();
  2591. // ----- Magic quotes trick
  2592. $this->privSwapBackMagicQuotes();
  2593. // ----- Return
  2594. return $v_result;
  2595. }
  2596. // --------------------------------------------------------------------------------
  2597. // --------------------------------------------------------------------------------
  2598. // Function : privConvertHeader2FileInfo()
  2599. // Description :
  2600. // This function takes the file informations from the central directory
  2601. // entries and extract the interesting parameters that will be given back.
  2602. // The resulting file infos are set in the array $p_info
  2603. // $p_info['filename'] : Filename with full path. Given by user (add),
  2604. // extracted in the filesystem (extract).
  2605. // $p_info['stored_filename'] : Stored filename in the archive.
  2606. // $p_info['size'] = Size of the file.
  2607. // $p_info['compressed_size'] = Compressed size of the file.
  2608. // $p_info['mtime'] = Last modification date of the file.
  2609. // $p_info['comment'] = Comment associated with the file.
  2610. // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
  2611. // $p_info['status'] = status of the action on the file.
  2612. // $p_info['crc'] = CRC of the file content.
  2613. // Parameters :
  2614. // Return Values :
  2615. // --------------------------------------------------------------------------------
  2616. public function privConvertHeader2FileInfo($p_header, &$p_info)
  2617. {
  2618. $v_result=1;
  2619. // ----- Get the interesting attributes
  2620. $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
  2621. $p_info['filename'] = $v_temp_path;
  2622. $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
  2623. $p_info['stored_filename'] = $v_temp_path;
  2624. $p_info['size'] = $p_header['size'];
  2625. $p_info['compressed_size'] = $p_header['compressed_size'];
  2626. $p_info['mtime'] = $p_header['mtime'];
  2627. $p_info['comment'] = $p_header['comment'];
  2628. $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
  2629. $p_info['index'] = $p_header['index'];
  2630. $p_info['status'] = $p_header['status'];
  2631. $p_info['crc'] = $p_header['crc'];
  2632. // ----- Return
  2633. return $v_result;
  2634. }
  2635. // --------------------------------------------------------------------------------
  2636. // --------------------------------------------------------------------------------
  2637. // Function : privExtractByRule()
  2638. // Description :
  2639. // Extract a file or directory depending of rules (by index, by name, ...)
  2640. // Parameters :
  2641. // $p_file_list : An array where will be placed the properties of each
  2642. // extracted file
  2643. // $p_path : Path to add while writing the extracted files
  2644. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  2645. // extracted files. If the path does not match the file path,
  2646. // the file is extracted with its memorized path.
  2647. // $p_remove_path does not apply to 'list' mode.
  2648. // $p_path and $p_remove_path are commulative.
  2649. // Return Values :
  2650. // 1 on success,0 or less on error (see error code list)
  2651. // --------------------------------------------------------------------------------
  2652. public function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  2653. {
  2654. $v_result=1;
  2655. // ----- Magic quotes trick
  2656. $this->privDisableMagicQuotes();
  2657. // ----- Check the path
  2658. if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path, 1, 2)!=":/"))) {
  2659. $p_path = "./".$p_path;
  2660. }
  2661. // ----- Reduce the path last (and duplicated) '/'
  2662. if (($p_path != "./") && ($p_path != "/")) {
  2663. // ----- Look for the path end '/'
  2664. while (substr($p_path, -1) == "/") {
  2665. $p_path = substr($p_path, 0, strlen($p_path)-1);
  2666. }
  2667. }
  2668. // ----- Look for path to remove format (should end by /)
  2669. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/')) {
  2670. $p_remove_path .= '/';
  2671. }
  2672. $p_remove_path_size = strlen($p_remove_path);
  2673. // ----- Open the zip file
  2674. if (($v_result = $this->privOpenFd('rb')) != 1) {
  2675. $this->privSwapBackMagicQuotes();
  2676. return $v_result;
  2677. }
  2678. // ----- Read the central directory informations
  2679. $v_central_dir = array();
  2680. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  2681. // ----- Close the zip file
  2682. $this->privCloseFd();
  2683. $this->privSwapBackMagicQuotes();
  2684. return $v_result;
  2685. }
  2686. // ----- Start at beginning of Central Dir
  2687. $v_pos_entry = $v_central_dir['offset'];
  2688. // ----- Read each entry
  2689. $j_start = 0;
  2690. for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++) {
  2691. // ----- Read next Central dir entry
  2692. @rewind($this->zip_fd);
  2693. if (@fseek($this->zip_fd, $v_pos_entry)) {
  2694. // ----- Close the zip file
  2695. $this->privCloseFd();
  2696. $this->privSwapBackMagicQuotes();
  2697. // ----- Error log
  2698. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2699. // ----- Return
  2700. return PclZip::errorCode();
  2701. }
  2702. // ----- Read the file header
  2703. $v_header = array();
  2704. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
  2705. // ----- Close the zip file
  2706. $this->privCloseFd();
  2707. $this->privSwapBackMagicQuotes();
  2708. return $v_result;
  2709. }
  2710. // ----- Store the index
  2711. $v_header['index'] = $i;
  2712. // ----- Store the file position
  2713. $v_pos_entry = ftell($this->zip_fd);
  2714. // ----- Look for the specific extract rules
  2715. $v_extract = false;
  2716. // ----- Look for extract by name rule
  2717. if ((isset($p_options[PCLZIP_OPT_BY_NAME])) && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  2718. // ----- Look if the filename is in the list
  2719. for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  2720. // ----- Look for a directory
  2721. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  2722. // ----- Look if the directory is in the filename path
  2723. if ((strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  2724. $v_extract = true;
  2725. }
  2726. } elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  2727. // ----- Look for a filename
  2728. $v_extract = true;
  2729. }
  2730. }
  2731. } elseif ((isset($p_options[PCLZIP_OPT_BY_PREG])) && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  2732. // ----- Look for extract by preg rule
  2733. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  2734. $v_extract = true;
  2735. }
  2736. } elseif ((isset($p_options[PCLZIP_OPT_BY_INDEX])) && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  2737. // ----- Look for extract by index rule
  2738. // ----- Look if the index is in the list
  2739. for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  2740. if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  2741. $v_extract = true;
  2742. }
  2743. if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  2744. $j_start = $j+1;
  2745. }
  2746. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  2747. break;
  2748. }
  2749. }
  2750. } else {
  2751. // ----- Look for no rule, which means extract all the archive
  2752. $v_extract = true;
  2753. }
  2754. // ----- Check compression method
  2755. if (($v_extract) && (($v_header['compression'] != 8) && ($v_header['compression'] != 0))) {
  2756. $v_header['status'] = 'unsupported_compression';
  2757. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2758. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2759. $this->privSwapBackMagicQuotes();
  2760. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION, "Filename '".$v_header['stored_filename']."' is compressed by an unsupported compression method (".$v_header['compression'].") ");
  2761. return PclZip::errorCode();
  2762. }
  2763. }
  2764. // ----- Check encrypted files
  2765. if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  2766. $v_header['status'] = 'unsupported_encryption';
  2767. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2768. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2769. $this->privSwapBackMagicQuotes();
  2770. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, "Unsupported encryption for filename '".$v_header['stored_filename']."'");
  2771. return PclZip::errorCode();
  2772. }
  2773. }
  2774. // ----- Look for real extraction
  2775. if (($v_extract) && ($v_header['status'] != 'ok')) {
  2776. $v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++]);
  2777. if ($v_result != 1) {
  2778. $this->privCloseFd();
  2779. $this->privSwapBackMagicQuotes();
  2780. return $v_result;
  2781. }
  2782. $v_extract = false;
  2783. }
  2784. // ----- Look for real extraction
  2785. if ($v_extract) {
  2786. // ----- Go to the file position
  2787. @rewind($this->zip_fd);
  2788. if (@fseek($this->zip_fd, $v_header['offset'])) {
  2789. // ----- Close the zip file
  2790. $this->privCloseFd();
  2791. $this->privSwapBackMagicQuotes();
  2792. // ----- Error log
  2793. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2794. // ----- Return
  2795. return PclZip::errorCode();
  2796. }
  2797. // ----- Look for extraction as string
  2798. if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  2799. $v_string = '';
  2800. // ----- Extracting the file
  2801. $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
  2802. if ($v_result1 < 1) {
  2803. $this->privCloseFd();
  2804. $this->privSwapBackMagicQuotes();
  2805. return $v_result1;
  2806. }
  2807. // ----- Get the only interesting attributes
  2808. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1) {
  2809. // ----- Close the zip file
  2810. $this->privCloseFd();
  2811. $this->privSwapBackMagicQuotes();
  2812. return $v_result;
  2813. }
  2814. // ----- Set the file content
  2815. $p_file_list[$v_nb_extracted]['content'] = $v_string;
  2816. // ----- Next extracted file
  2817. $v_nb_extracted++;
  2818. // ----- Look for user callback abort
  2819. if ($v_result1 == 2) {
  2820. break;
  2821. }
  2822. } elseif ((isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
  2823. // ----- Look for extraction in standard output
  2824. // ----- Extracting the file in standard output
  2825. $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  2826. if ($v_result1 < 1) {
  2827. $this->privCloseFd();
  2828. $this->privSwapBackMagicQuotes();
  2829. return $v_result1;
  2830. }
  2831. // ----- Get the only interesting attributes
  2832. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  2833. $this->privCloseFd();
  2834. $this->privSwapBackMagicQuotes();
  2835. return $v_result;
  2836. }
  2837. // ----- Look for user callback abort
  2838. if ($v_result1 == 2) {
  2839. break;
  2840. }
  2841. } else {
  2842. // ----- Look for normal extraction
  2843. // ----- Extracting the file
  2844. $v_result1 = $this->privExtractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_options);
  2845. if ($v_result1 < 1) {
  2846. $this->privCloseFd();
  2847. $this->privSwapBackMagicQuotes();
  2848. return $v_result1;
  2849. }
  2850. // ----- Get the only interesting attributes
  2851. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  2852. // ----- Close the zip file
  2853. $this->privCloseFd();
  2854. $this->privSwapBackMagicQuotes();
  2855. return $v_result;
  2856. }
  2857. // ----- Look for user callback abort
  2858. if ($v_result1 == 2) {
  2859. break;
  2860. }
  2861. }
  2862. }
  2863. }
  2864. // ----- Close the zip file
  2865. $this->privCloseFd();
  2866. $this->privSwapBackMagicQuotes();
  2867. // ----- Return
  2868. return $v_result;
  2869. }
  2870. // --------------------------------------------------------------------------------
  2871. // --------------------------------------------------------------------------------
  2872. // Function : privExtractFile()
  2873. // Description :
  2874. // Parameters :
  2875. // Return Values :
  2876. //
  2877. // 1 : ... ?
  2878. // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
  2879. // --------------------------------------------------------------------------------
  2880. public function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  2881. {
  2882. $v_result=1;
  2883. // ----- Read the file header
  2884. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  2885. // ----- Return
  2886. return $v_result;
  2887. }
  2888. // ----- Check that the file header is coherent with $p_entry info
  2889. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  2890. // TBC
  2891. }
  2892. // ----- Look for all path to remove
  2893. if ($p_remove_all_path == true) {
  2894. // ----- Look for folder entry that not need to be extracted
  2895. if (($p_entry['external']&0x00000010)==0x00000010) {
  2896. $p_entry['status'] = "filtered";
  2897. return $v_result;
  2898. }
  2899. // ----- Get the basename of the path
  2900. $p_entry['filename'] = basename($p_entry['filename']);
  2901. } elseif ($p_remove_path != "") {
  2902. // ----- Look for path to remove
  2903. if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2) {
  2904. // ----- Change the file status
  2905. $p_entry['status'] = "filtered";
  2906. // ----- Return
  2907. return $v_result;
  2908. }
  2909. $p_remove_path_size = strlen($p_remove_path);
  2910. if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
  2911. // ----- Remove the path
  2912. $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  2913. }
  2914. }
  2915. // ----- Add the path
  2916. if ($p_path != '') {
  2917. $p_entry['filename'] = $p_path."/".$p_entry['filename'];
  2918. }
  2919. // ----- Check a base_dir_restriction
  2920. if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
  2921. $v_inclusion = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION], $p_entry['filename']);
  2922. if ($v_inclusion == 0) {
  2923. PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION, "Filename '".$p_entry['filename']."' is outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
  2924. return PclZip::errorCode();
  2925. }
  2926. }
  2927. // ----- Look for pre-extract callback
  2928. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  2929. // ----- Generate a local information
  2930. $v_local_header = array();
  2931. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2932. // ----- Call the callback
  2933. // Here I do not use call_user_func() because I need to send a reference to the
  2934. // header.
  2935. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  2936. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  2937. if ($v_result == 0) {
  2938. // ----- Change the file status
  2939. $p_entry['status'] = "skipped";
  2940. $v_result = 1;
  2941. }
  2942. // ----- Look for abort result
  2943. if ($v_result == 2) {
  2944. // ----- This status is internal and will be changed in 'skipped'
  2945. $p_entry['status'] = "aborted";
  2946. $v_result = PCLZIP_ERR_USER_ABORTED;
  2947. }
  2948. // ----- Update the informations
  2949. // Only some fields can be modified
  2950. $p_entry['filename'] = $v_local_header['filename'];
  2951. }
  2952. // ----- Look if extraction should be done
  2953. if ($p_entry['status'] == 'ok') {
  2954. // ----- Look for specific actions while the file exist
  2955. if (file_exists($p_entry['filename'])) {
  2956. // ----- Look if file is a directory
  2957. if (is_dir($p_entry['filename'])) {
  2958. // ----- Change the file status
  2959. $p_entry['status'] = "already_a_directory";
  2960. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2961. // For historical reason first PclZip implementation does not stop
  2962. // when this kind of error occurs.
  2963. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  2964. PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY, "Filename '".$p_entry['filename']."' is already used by an existing directory");
  2965. return PclZip::errorCode();
  2966. }
  2967. } elseif (!is_writeable($p_entry['filename'])) {
  2968. // ----- Look if file is write protected
  2969. // ----- Change the file status
  2970. $p_entry['status'] = "write_protected";
  2971. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2972. // For historical reason first PclZip implementation does not stop
  2973. // when this kind of error occurs.
  2974. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2975. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, "Filename '".$p_entry['filename']."' exists and is write protected");
  2976. return PclZip::errorCode();
  2977. }
  2978. } elseif (filemtime($p_entry['filename']) > $p_entry['mtime']) {
  2979. // ----- Look if the extracted file is older
  2980. // ----- Change the file status
  2981. if ((isset($p_options[PCLZIP_OPT_REPLACE_NEWER])) && ($p_options[PCLZIP_OPT_REPLACE_NEWER] === true)) {
  2982. } else {
  2983. $p_entry['status'] = "newer_exist";
  2984. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2985. // For historical reason first PclZip implementation does not stop
  2986. // when this kind of error occurs.
  2987. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2988. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, "Newer version of '".$p_entry['filename']."' exists and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  2989. return PclZip::errorCode();
  2990. }
  2991. }
  2992. } else {
  2993. }
  2994. } else {
  2995. // ----- Check the directory availability and create it if necessary
  2996. if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/')) {
  2997. $v_dir_to_check = $p_entry['filename'];
  2998. } elseif (!strstr($p_entry['filename'], "/")) {
  2999. $v_dir_to_check = "";
  3000. } else {
  3001. $v_dir_to_check = dirname($p_entry['filename']);
  3002. }
  3003. if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
  3004. // ----- Change the file status
  3005. $p_entry['status'] = "path_creation_fail";
  3006. // ----- Return
  3007. //return $v_result;
  3008. $v_result = 1;
  3009. }
  3010. }
  3011. }
  3012. // ----- Look if extraction should be done
  3013. if ($p_entry['status'] == 'ok') {
  3014. // ----- Do the extraction (if not a folder)
  3015. if (!(($p_entry['external']&0x00000010) == 0x00000010)) {
  3016. // ----- Look for not compressed file
  3017. if ($p_entry['compression'] == 0) {
  3018. // ----- Opening destination file
  3019. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3020. // ----- Change the file status
  3021. $p_entry['status'] = "write_error";
  3022. // ----- Return
  3023. return $v_result;
  3024. }
  3025. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3026. $v_size = $p_entry['compressed_size'];
  3027. while ($v_size != 0) {
  3028. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3029. $v_buffer = @fread($this->zip_fd, $v_read_size);
  3030. /* Try to speed up the code
  3031. $v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3032. @fwrite($v_dest_file, $v_binary_data, $v_read_size);
  3033. */
  3034. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3035. $v_size -= $v_read_size;
  3036. }
  3037. // ----- Closing the destination file
  3038. fclose($v_dest_file);
  3039. // ----- Change the file mtime
  3040. touch($p_entry['filename'], $p_entry['mtime']);
  3041. } else {
  3042. // ----- TBC
  3043. // Need to be finished
  3044. if (($p_entry['flag'] & 1) == 1) {
  3045. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
  3046. return PclZip::errorCode();
  3047. }
  3048. // ----- Look for using temporary file to unzip
  3049. if ((!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON]) || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]) && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])))) {
  3050. $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
  3051. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  3052. return $v_result;
  3053. }
  3054. } else {
  3055. // ----- Look for extract in memory
  3056. // ----- Read the compressed file in a buffer (one shot)
  3057. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3058. // ----- Decompress the file
  3059. $v_file_content = @gzinflate($v_buffer);
  3060. unset($v_buffer);
  3061. if ($v_file_content === false) {
  3062. // ----- Change the file status
  3063. // TBC
  3064. $p_entry['status'] = "error";
  3065. return $v_result;
  3066. }
  3067. // ----- Opening destination file
  3068. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3069. // ----- Change the file status
  3070. $p_entry['status'] = "write_error";
  3071. return $v_result;
  3072. }
  3073. // ----- Write the uncompressed data
  3074. @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  3075. unset($v_file_content);
  3076. // ----- Closing the destination file
  3077. @fclose($v_dest_file);
  3078. }
  3079. // ----- Change the file mtime
  3080. @touch($p_entry['filename'], $p_entry['mtime']);
  3081. }
  3082. // ----- Look for chmod option
  3083. if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  3084. // ----- Change the mode of the file
  3085. @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  3086. }
  3087. }
  3088. }
  3089. // ----- Change abort status
  3090. if ($p_entry['status'] == "aborted") {
  3091. $p_entry['status'] = "skipped";
  3092. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3093. // ----- Look for post-extract callback
  3094. // ----- Generate a local information
  3095. $v_local_header = array();
  3096. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3097. // ----- Call the callback
  3098. // Here I do not use call_user_func() because I need to send a reference to the
  3099. // header.
  3100. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3101. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3102. // ----- Look for abort result
  3103. if ($v_result == 2) {
  3104. $v_result = PCLZIP_ERR_USER_ABORTED;
  3105. }
  3106. }
  3107. // ----- Return
  3108. return $v_result;
  3109. }
  3110. // --------------------------------------------------------------------------------
  3111. // --------------------------------------------------------------------------------
  3112. // Function : privExtractFileUsingTempFile()
  3113. // Description :
  3114. // Parameters :
  3115. // Return Values :
  3116. // --------------------------------------------------------------------------------
  3117. public function privExtractFileUsingTempFile(&$p_entry, &$p_options)
  3118. {
  3119. $v_result=1;
  3120. // ----- Creates a temporary file
  3121. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  3122. if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
  3123. fclose($v_file);
  3124. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  3125. return PclZip::errorCode();
  3126. }
  3127. // ----- Write gz file format header
  3128. $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
  3129. @fwrite($v_dest_file, $v_binary_data, 10);
  3130. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3131. $v_size = $p_entry['compressed_size'];
  3132. while ($v_size != 0) {
  3133. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3134. $v_buffer = @fread($this->zip_fd, $v_read_size);
  3135. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3136. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3137. $v_size -= $v_read_size;
  3138. }
  3139. // ----- Write gz file format footer
  3140. $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
  3141. @fwrite($v_dest_file, $v_binary_data, 8);
  3142. // ----- Close the temporary file
  3143. @fclose($v_dest_file);
  3144. // ----- Opening destination file
  3145. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3146. $p_entry['status'] = "write_error";
  3147. return $v_result;
  3148. }
  3149. // ----- Open the temporary gz file
  3150. if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
  3151. @fclose($v_dest_file);
  3152. $p_entry['status'] = "read_error";
  3153. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  3154. return PclZip::errorCode();
  3155. }
  3156. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3157. $v_size = $p_entry['size'];
  3158. while ($v_size != 0) {
  3159. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3160. $v_buffer = @gzread($v_src_file, $v_read_size);
  3161. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3162. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3163. $v_size -= $v_read_size;
  3164. }
  3165. @fclose($v_dest_file);
  3166. @gzclose($v_src_file);
  3167. // ----- Delete the temporary file
  3168. @unlink($v_gzip_temp_name);
  3169. // ----- Return
  3170. return $v_result;
  3171. }
  3172. // --------------------------------------------------------------------------------
  3173. // --------------------------------------------------------------------------------
  3174. // Function : privExtractFileInOutput()
  3175. // Description :
  3176. // Parameters :
  3177. // Return Values :
  3178. // --------------------------------------------------------------------------------
  3179. public function privExtractFileInOutput(&$p_entry, &$p_options)
  3180. {
  3181. $v_result=1;
  3182. // ----- Read the file header
  3183. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  3184. return $v_result;
  3185. }
  3186. // ----- Check that the file header is coherent with $p_entry info
  3187. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3188. // TBC
  3189. }
  3190. // ----- Look for pre-extract callback
  3191. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  3192. // ----- Generate a local information
  3193. $v_local_header = array();
  3194. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3195. // ----- Call the callback
  3196. // Here I do not use call_user_func() because I need to send a reference to the
  3197. // header.
  3198. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  3199. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  3200. if ($v_result == 0) {
  3201. // ----- Change the file status
  3202. $p_entry['status'] = "skipped";
  3203. $v_result = 1;
  3204. }
  3205. // ----- Look for abort result
  3206. if ($v_result == 2) {
  3207. // ----- This status is internal and will be changed in 'skipped'
  3208. $p_entry['status'] = "aborted";
  3209. $v_result = PCLZIP_ERR_USER_ABORTED;
  3210. }
  3211. // ----- Update the informations
  3212. // Only some fields can be modified
  3213. $p_entry['filename'] = $v_local_header['filename'];
  3214. }
  3215. // ----- Trace
  3216. // ----- Look if extraction should be done
  3217. if ($p_entry['status'] == 'ok') {
  3218. // ----- Do the extraction (if not a folder)
  3219. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  3220. // ----- Look for not compressed file
  3221. if ($p_entry['compressed_size'] == $p_entry['size']) {
  3222. // ----- Read the file in a buffer (one shot)
  3223. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3224. // ----- Send the file to the output
  3225. echo $v_buffer;
  3226. unset($v_buffer);
  3227. } else {
  3228. // ----- Read the compressed file in a buffer (one shot)
  3229. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3230. // ----- Decompress the file
  3231. $v_file_content = gzinflate($v_buffer);
  3232. unset($v_buffer);
  3233. // ----- Send the file to the output
  3234. echo $v_file_content;
  3235. unset($v_file_content);
  3236. }
  3237. }
  3238. }
  3239. // ----- Change abort status
  3240. if ($p_entry['status'] == "aborted") {
  3241. $p_entry['status'] = "skipped";
  3242. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3243. // ----- Look for post-extract callback
  3244. // ----- Generate a local information
  3245. $v_local_header = array();
  3246. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3247. // ----- Call the callback
  3248. // Here I do not use call_user_func() because I need to send a reference to the
  3249. // header.
  3250. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3251. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3252. // ----- Look for abort result
  3253. if ($v_result == 2) {
  3254. $v_result = PCLZIP_ERR_USER_ABORTED;
  3255. }
  3256. }
  3257. return $v_result;
  3258. }
  3259. // --------------------------------------------------------------------------------
  3260. // --------------------------------------------------------------------------------
  3261. // Function : privExtractFileAsString()
  3262. // Description :
  3263. // Parameters :
  3264. // Return Values :
  3265. // --------------------------------------------------------------------------------
  3266. public function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
  3267. {
  3268. $v_result=1;
  3269. // ----- Read the file header
  3270. $v_header = array();
  3271. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  3272. // ----- Return
  3273. return $v_result;
  3274. }
  3275. // ----- Check that the file header is coherent with $p_entry info
  3276. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3277. // TBC
  3278. }
  3279. // ----- Look for pre-extract callback
  3280. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  3281. // ----- Generate a local information
  3282. $v_local_header = array();
  3283. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3284. // ----- Call the callback
  3285. // Here I do not use call_user_func() because I need to send a reference to the
  3286. // header.
  3287. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  3288. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  3289. if ($v_result == 0) {
  3290. // ----- Change the file status
  3291. $p_entry['status'] = "skipped";
  3292. $v_result = 1;
  3293. }
  3294. // ----- Look for abort result
  3295. if ($v_result == 2) {
  3296. // ----- This status is internal and will be changed in 'skipped'
  3297. $p_entry['status'] = "aborted";
  3298. $v_result = PCLZIP_ERR_USER_ABORTED;
  3299. }
  3300. // ----- Update the informations
  3301. // Only some fields can be modified
  3302. $p_entry['filename'] = $v_local_header['filename'];
  3303. }
  3304. // ----- Look if extraction should be done
  3305. if ($p_entry['status'] == 'ok') {
  3306. // ----- Do the extraction (if not a folder)
  3307. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  3308. // ----- Look for not compressed file
  3309. // if ($p_entry['compressed_size'] == $p_entry['size'])
  3310. if ($p_entry['compression'] == 0) {
  3311. // ----- Reading the file
  3312. $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
  3313. } else {
  3314. // ----- Reading the file
  3315. $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
  3316. // ----- Decompress the file
  3317. if (($p_string = @gzinflate($v_data)) === false) {
  3318. // TBC
  3319. }
  3320. }
  3321. // ----- Trace
  3322. } else {
  3323. // TBC : error : can not extract a folder in a string
  3324. }
  3325. }
  3326. // ----- Change abort status
  3327. if ($p_entry['status'] == "aborted") {
  3328. $p_entry['status'] = "skipped";
  3329. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3330. // ----- Look for post-extract callback
  3331. // ----- Generate a local information
  3332. $v_local_header = array();
  3333. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3334. // ----- Swap the content to header
  3335. $v_local_header['content'] = $p_string;
  3336. $p_string = '';
  3337. // ----- Call the callback
  3338. // Here I do not use call_user_func() because I need to send a reference to the
  3339. // header.
  3340. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3341. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3342. // ----- Swap back the content to header
  3343. $p_string = $v_local_header['content'];
  3344. unset($v_local_header['content']);
  3345. // ----- Look for abort result
  3346. if ($v_result == 2) {
  3347. $v_result = PCLZIP_ERR_USER_ABORTED;
  3348. }
  3349. }
  3350. // ----- Return
  3351. return $v_result;
  3352. }
  3353. // --------------------------------------------------------------------------------
  3354. // --------------------------------------------------------------------------------
  3355. // Function : privReadFileHeader()
  3356. // Description :
  3357. // Parameters :
  3358. // Return Values :
  3359. // --------------------------------------------------------------------------------
  3360. public function privReadFileHeader(&$p_header)
  3361. {
  3362. $v_result=1;
  3363. // ----- Read the 4 bytes signature
  3364. $v_binary_data = @fread($this->zip_fd, 4);
  3365. $v_data = unpack('Vid', $v_binary_data);
  3366. // ----- Check signature
  3367. if ($v_data['id'] != 0x04034b50) {
  3368. // ----- Error log
  3369. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  3370. // ----- Return
  3371. return PclZip::errorCode();
  3372. }
  3373. // ----- Read the first 42 bytes of the header
  3374. $v_binary_data = fread($this->zip_fd, 26);
  3375. // ----- Look for invalid block size
  3376. if (strlen($v_binary_data) != 26) {
  3377. $p_header['filename'] = "";
  3378. $p_header['status'] = "invalid_header";
  3379. // ----- Error log
  3380. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  3381. // ----- Return
  3382. return PclZip::errorCode();
  3383. }
  3384. // ----- Extract the values
  3385. $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  3386. // ----- Get filename
  3387. $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  3388. // ----- Get extra_fields
  3389. if ($v_data['extra_len'] != 0) {
  3390. $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  3391. } else {
  3392. $p_header['extra'] = '';
  3393. }
  3394. // ----- Extract properties
  3395. $p_header['version_extracted'] = $v_data['version'];
  3396. $p_header['compression'] = $v_data['compression'];
  3397. $p_header['size'] = $v_data['size'];
  3398. $p_header['compressed_size'] = $v_data['compressed_size'];
  3399. $p_header['crc'] = $v_data['crc'];
  3400. $p_header['flag'] = $v_data['flag'];
  3401. $p_header['filename_len'] = $v_data['filename_len'];
  3402. // ----- Recuperate date in UNIX format
  3403. $p_header['mdate'] = $v_data['mdate'];
  3404. $p_header['mtime'] = $v_data['mtime'];
  3405. if ($p_header['mdate'] && $p_header['mtime']) {
  3406. // ----- Extract time
  3407. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  3408. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  3409. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  3410. // ----- Extract date
  3411. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  3412. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  3413. $v_day = $p_header['mdate'] & 0x001F;
  3414. // ----- Get UNIX date format
  3415. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  3416. } else {
  3417. $p_header['mtime'] = time();
  3418. }
  3419. // TBC
  3420. //for(reset($v_data); $key = key($v_data); next($v_data)) {
  3421. //}
  3422. // ----- Set the stored filename
  3423. $p_header['stored_filename'] = $p_header['filename'];
  3424. // ----- Set the status field
  3425. $p_header['status'] = "ok";
  3426. // ----- Return
  3427. return $v_result;
  3428. }
  3429. // --------------------------------------------------------------------------------
  3430. // --------------------------------------------------------------------------------
  3431. // Function : privReadCentralFileHeader()
  3432. // Description :
  3433. // Parameters :
  3434. // Return Values :
  3435. // --------------------------------------------------------------------------------
  3436. public function privReadCentralFileHeader(&$p_header)
  3437. {
  3438. $v_result = 1;
  3439. // ----- Read the 4 bytes signature
  3440. $v_binary_data = @fread($this->zip_fd, 4);
  3441. $v_data = unpack('Vid', $v_binary_data);
  3442. // ----- Check signature
  3443. if ($v_data['id'] != 0x02014b50) {
  3444. // ----- Error log
  3445. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  3446. // ----- Return
  3447. return PclZip::errorCode();
  3448. }
  3449. // ----- Read the first 42 bytes of the header
  3450. $v_binary_data = fread($this->zip_fd, 42);
  3451. // ----- Look for invalid block size
  3452. if (strlen($v_binary_data) != 42) {
  3453. $p_header['filename'] = "";
  3454. $p_header['status'] = "invalid_header";
  3455. // ----- Error log
  3456. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  3457. // ----- Return
  3458. return PclZip::errorCode();
  3459. }
  3460. // ----- Extract the values
  3461. $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);
  3462. // ----- Get filename
  3463. if ($p_header['filename_len'] != 0) {
  3464. $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  3465. } else {
  3466. $p_header['filename'] = '';
  3467. }
  3468. // ----- Get extra
  3469. if ($p_header['extra_len'] != 0) {
  3470. $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  3471. } else {
  3472. $p_header['extra'] = '';
  3473. }
  3474. // ----- Get comment
  3475. if ($p_header['comment_len'] != 0) {
  3476. $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  3477. } else {
  3478. $p_header['comment'] = '';
  3479. }
  3480. // ----- Extract properties
  3481. // ----- Recuperate date in UNIX format
  3482. //if ($p_header['mdate'] && $p_header['mtime'])
  3483. // TBC : bug : this was ignoring time with 0/0/0
  3484. if (1) {
  3485. // ----- Extract time
  3486. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  3487. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  3488. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  3489. // ----- Extract date
  3490. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  3491. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  3492. $v_day = $p_header['mdate'] & 0x001F;
  3493. // ----- Get UNIX date format
  3494. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  3495. } else {
  3496. $p_header['mtime'] = time();
  3497. }
  3498. // ----- Set the stored filename
  3499. $p_header['stored_filename'] = $p_header['filename'];
  3500. // ----- Set default status to ok
  3501. $p_header['status'] = 'ok';
  3502. // ----- Look if it is a directory
  3503. if (substr($p_header['filename'], -1) == '/') {
  3504. //$p_header['external'] = 0x41FF0010;
  3505. $p_header['external'] = 0x00000010;
  3506. }
  3507. // ----- Return
  3508. return $v_result;
  3509. }
  3510. // --------------------------------------------------------------------------------
  3511. // --------------------------------------------------------------------------------
  3512. // Function : privCheckFileHeaders()
  3513. // Description :
  3514. // Parameters :
  3515. // Return Values :
  3516. // 1 on success,
  3517. // 0 on error;
  3518. // --------------------------------------------------------------------------------
  3519. public function privCheckFileHeaders(&$p_local_header, &$p_central_header)
  3520. {
  3521. $v_result=1;
  3522. // ----- Check the static values
  3523. // TBC
  3524. if ($p_local_header['filename'] != $p_central_header['filename']) {
  3525. }
  3526. if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  3527. }
  3528. if ($p_local_header['flag'] != $p_central_header['flag']) {
  3529. }
  3530. if ($p_local_header['compression'] != $p_central_header['compression']) {
  3531. }
  3532. if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  3533. }
  3534. if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  3535. }
  3536. // ----- Look for flag bit 3
  3537. if (($p_local_header['flag'] & 8) == 8) {
  3538. $p_local_header['size'] = $p_central_header['size'];
  3539. $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  3540. $p_local_header['crc'] = $p_central_header['crc'];
  3541. }
  3542. // ----- Return
  3543. return $v_result;
  3544. }
  3545. // --------------------------------------------------------------------------------
  3546. // --------------------------------------------------------------------------------
  3547. // Function : privReadEndCentralDir()
  3548. // Description :
  3549. // Parameters :
  3550. // Return Values :
  3551. // --------------------------------------------------------------------------------
  3552. public function privReadEndCentralDir(&$p_central_dir)
  3553. {
  3554. $v_result=1;
  3555. // ----- Go to the end of the zip file
  3556. $v_size = filesize($this->zipname);
  3557. @fseek($this->zip_fd, $v_size);
  3558. if (@ftell($this->zip_fd) != $v_size) {
  3559. // ----- Error log
  3560. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
  3561. // ----- Return
  3562. return PclZip::errorCode();
  3563. }
  3564. // ----- First try : look if this is an archive with no commentaries (most of the time)
  3565. // in this case the end of central dir is at 22 bytes of the file end
  3566. $v_found = 0;
  3567. if ($v_size > 26) {
  3568. @fseek($this->zip_fd, $v_size-22);
  3569. if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22)) {
  3570. // ----- Error log
  3571. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  3572. // ----- Return
  3573. return PclZip::errorCode();
  3574. }
  3575. // ----- Read for bytes
  3576. $v_binary_data = @fread($this->zip_fd, 4);
  3577. $v_data = @unpack('Vid', $v_binary_data);
  3578. // ----- Check signature
  3579. if ($v_data['id'] == 0x06054b50) {
  3580. $v_found = 1;
  3581. }
  3582. $v_pos = ftell($this->zip_fd);
  3583. }
  3584. // ----- Go back to the maximum possible size of the Central Dir End Record
  3585. if (!$v_found) {
  3586. $v_maximum_size = 65557; // 0xFFFF + 22;
  3587. if ($v_maximum_size > $v_size) {
  3588. $v_maximum_size = $v_size;
  3589. }
  3590. @fseek($this->zip_fd, $v_size-$v_maximum_size);
  3591. if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size)) {
  3592. // ----- Error log
  3593. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  3594. // ----- Return
  3595. return PclZip::errorCode();
  3596. }
  3597. // ----- Read byte per byte in order to find the signature
  3598. $v_pos = ftell($this->zip_fd);
  3599. $v_bytes = 0x00000000;
  3600. while ($v_pos < $v_size) {
  3601. // ----- Read a byte
  3602. $v_byte = @fread($this->zip_fd, 1);
  3603. // ----- Add the byte
  3604. //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
  3605. // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
  3606. // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
  3607. $v_bytes = (($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
  3608. // ----- Compare the bytes
  3609. if ($v_bytes == 0x504b0506) {
  3610. $v_pos++;
  3611. break;
  3612. }
  3613. $v_pos++;
  3614. }
  3615. // ----- Look if not found end of central dir
  3616. if ($v_pos == $v_size) {
  3617. // ----- Error log
  3618. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  3619. // ----- Return
  3620. return PclZip::errorCode();
  3621. }
  3622. }
  3623. // ----- Read the first 18 bytes of the header
  3624. $v_binary_data = fread($this->zip_fd, 18);
  3625. // ----- Look for invalid block size
  3626. if (strlen($v_binary_data) != 18) {
  3627. // ----- Error log
  3628. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
  3629. // ----- Return
  3630. return PclZip::errorCode();
  3631. }
  3632. // ----- Extract the values
  3633. $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  3634. // ----- Check the global size
  3635. if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  3636. // ----- Removed in release 2.2 see readme file
  3637. // The check of the file size is a little too strict.
  3638. // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
  3639. // While decrypted, zip has training 0 bytes
  3640. if (0) {
  3641. // ----- Error log
  3642. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'The central dir is not at the end of the archive. Some trailing bytes exists after the archive.');
  3643. // ----- Return
  3644. return PclZip::errorCode();
  3645. }
  3646. }
  3647. // ----- Get comment
  3648. if ($v_data['comment_size'] != 0) {
  3649. $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  3650. } else {
  3651. $p_central_dir['comment'] = '';
  3652. }
  3653. $p_central_dir['entries'] = $v_data['entries'];
  3654. $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  3655. $p_central_dir['offset'] = $v_data['offset'];
  3656. $p_central_dir['size'] = $v_data['size'];
  3657. $p_central_dir['disk'] = $v_data['disk'];
  3658. $p_central_dir['disk_start'] = $v_data['disk_start'];
  3659. // TBC
  3660. //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
  3661. //}
  3662. // ----- Return
  3663. return $v_result;
  3664. }
  3665. // --------------------------------------------------------------------------------
  3666. // --------------------------------------------------------------------------------
  3667. // Function : privDeleteByRule()
  3668. // Description :
  3669. // Parameters :
  3670. // Return Values :
  3671. // --------------------------------------------------------------------------------
  3672. public function privDeleteByRule(&$p_result_list, &$p_options)
  3673. {
  3674. $v_result=1;
  3675. $v_list_detail = array();
  3676. // ----- Open the zip file
  3677. if (($v_result=$this->privOpenFd('rb')) != 1) {
  3678. // ----- Return
  3679. return $v_result;
  3680. }
  3681. // ----- Read the central directory informations
  3682. $v_central_dir = array();
  3683. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  3684. $this->privCloseFd();
  3685. return $v_result;
  3686. }
  3687. // ----- Go to beginning of File
  3688. @rewind($this->zip_fd);
  3689. // ----- Scan all the files
  3690. // ----- Start at beginning of Central Dir
  3691. $v_pos_entry = $v_central_dir['offset'];
  3692. @rewind($this->zip_fd);
  3693. if (@fseek($this->zip_fd, $v_pos_entry)) {
  3694. // ----- Close the zip file
  3695. $this->privCloseFd();
  3696. // ----- Error log
  3697. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3698. // ----- Return
  3699. return PclZip::errorCode();
  3700. }
  3701. // ----- Read each entry
  3702. $v_header_list = array();
  3703. $j_start = 0;
  3704. for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++) {
  3705. // ----- Read the file header
  3706. $v_header_list[$v_nb_extracted] = array();
  3707. if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1) {
  3708. // ----- Close the zip file
  3709. $this->privCloseFd();
  3710. return $v_result;
  3711. }
  3712. // ----- Store the index
  3713. $v_header_list[$v_nb_extracted]['index'] = $i;
  3714. // ----- Look for the specific extract rules
  3715. $v_found = false;
  3716. // ----- Look for extract by name rule
  3717. if ((isset($p_options[PCLZIP_OPT_BY_NAME])) && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  3718. // ----- Look if the filename is in the list
  3719. for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  3720. // ----- Look for a directory
  3721. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  3722. // ----- Look if the directory is in the filename path
  3723. if ((strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) && (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])) {
  3724. $v_found = true;
  3725. } elseif ((($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */ && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  3726. $v_found = true;
  3727. }
  3728. } elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  3729. // ----- Look for a filename
  3730. $v_found = true;
  3731. }
  3732. }
  3733. } elseif ((isset($p_options[PCLZIP_OPT_BY_PREG])) && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  3734. // ----- Look for extract by preg rule
  3735. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  3736. $v_found = true;
  3737. }
  3738. } elseif ((isset($p_options[PCLZIP_OPT_BY_INDEX])) && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  3739. // ----- Look for extract by index rule
  3740. // ----- Look if the index is in the list
  3741. for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  3742. if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  3743. $v_found = true;
  3744. }
  3745. if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  3746. $j_start = $j+1;
  3747. }
  3748. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  3749. break;
  3750. }
  3751. }
  3752. } else {
  3753. $v_found = true;
  3754. }
  3755. // ----- Look for deletion
  3756. if ($v_found) {
  3757. unset($v_header_list[$v_nb_extracted]);
  3758. } else {
  3759. $v_nb_extracted++;
  3760. }
  3761. }
  3762. // ----- Look if something need to be deleted
  3763. if ($v_nb_extracted > 0) {
  3764. // ----- Creates a temporay file
  3765. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  3766. // ----- Creates a temporary zip archive
  3767. $v_temp_zip = new PclZip($v_zip_temp_name);
  3768. // ----- Open the temporary zip file in write mode
  3769. if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  3770. $this->privCloseFd();
  3771. // ----- Return
  3772. return $v_result;
  3773. }
  3774. // ----- Look which file need to be kept
  3775. for ($i=0; $i<sizeof($v_header_list); $i++) {
  3776. // ----- Calculate the position of the header
  3777. @rewind($this->zip_fd);
  3778. if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
  3779. // ----- Close the zip file
  3780. $this->privCloseFd();
  3781. $v_temp_zip->privCloseFd();
  3782. @unlink($v_zip_temp_name);
  3783. // ----- Error log
  3784. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3785. // ----- Return
  3786. return PclZip::errorCode();
  3787. }
  3788. // ----- Read the file header
  3789. $v_local_header = array();
  3790. if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  3791. // ----- Close the zip file
  3792. $this->privCloseFd();
  3793. $v_temp_zip->privCloseFd();
  3794. @unlink($v_zip_temp_name);
  3795. // ----- Return
  3796. return $v_result;
  3797. }
  3798. // ----- Check that local file header is same as central file header
  3799. if ($this->privCheckFileHeaders($v_local_header, $v_header_list[$i]) != 1) {
  3800. // TBC
  3801. }
  3802. unset($v_local_header);
  3803. // ----- Write the file header
  3804. if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
  3805. // ----- Close the zip file
  3806. $this->privCloseFd();
  3807. $v_temp_zip->privCloseFd();
  3808. @unlink($v_zip_temp_name);
  3809. // ----- Return
  3810. return $v_result;
  3811. }
  3812. // ----- Read/write the data block
  3813. if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  3814. // ----- Close the zip file
  3815. $this->privCloseFd();
  3816. $v_temp_zip->privCloseFd();
  3817. @unlink($v_zip_temp_name);
  3818. // ----- Return
  3819. return $v_result;
  3820. }
  3821. }
  3822. // ----- Store the offset of the central dir
  3823. $v_offset = @ftell($v_temp_zip->zip_fd);
  3824. // ----- Re-Create the Central Dir files header
  3825. for ($i=0; $i<sizeof($v_header_list); $i++) {
  3826. // ----- Create the file header
  3827. if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  3828. $v_temp_zip->privCloseFd();
  3829. $this->privCloseFd();
  3830. @unlink($v_zip_temp_name);
  3831. // ----- Return
  3832. return $v_result;
  3833. }
  3834. // ----- Transform the header to a 'usable' info
  3835. $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  3836. }
  3837. // ----- Zip file comment
  3838. $v_comment = '';
  3839. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  3840. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  3841. }
  3842. // ----- Calculate the size of the central header
  3843. $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
  3844. // ----- Create the central dir footer
  3845. if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
  3846. // ----- Reset the file list
  3847. unset($v_header_list);
  3848. $v_temp_zip->privCloseFd();
  3849. $this->privCloseFd();
  3850. @unlink($v_zip_temp_name);
  3851. // ----- Return
  3852. return $v_result;
  3853. }
  3854. // ----- Close
  3855. $v_temp_zip->privCloseFd();
  3856. $this->privCloseFd();
  3857. // ----- Delete the zip file
  3858. // TBC : I should test the result ...
  3859. @unlink($this->zipname);
  3860. // ----- Rename the temporary file
  3861. // TBC : I should test the result ...
  3862. //@rename($v_zip_temp_name, $this->zipname);
  3863. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  3864. // ----- Destroy the temporary archive
  3865. unset($v_temp_zip);
  3866. } elseif ($v_central_dir['entries'] != 0) {
  3867. // ----- Remove every files : reset the file
  3868. $this->privCloseFd();
  3869. if (($v_result = $this->privOpenFd('wb')) != 1) {
  3870. return $v_result;
  3871. }
  3872. if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  3873. return $v_result;
  3874. }
  3875. $this->privCloseFd();
  3876. }
  3877. // ----- Return
  3878. return $v_result;
  3879. }
  3880. // --------------------------------------------------------------------------------
  3881. // --------------------------------------------------------------------------------
  3882. // Function : privDirCheck()
  3883. // Description :
  3884. // Check if a directory exists, if not it creates it and all the parents directory
  3885. // which may be useful.
  3886. // Parameters :
  3887. // $p_dir : Directory path to check.
  3888. // Return Values :
  3889. // 1 : OK
  3890. // -1 : Unable to create directory
  3891. // --------------------------------------------------------------------------------
  3892. public function privDirCheck($p_dir, $p_is_dir = false)
  3893. {
  3894. $v_result = 1;
  3895. // ----- Remove the final '/'
  3896. if (($p_is_dir) && (substr($p_dir, -1)=='/')) {
  3897. $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
  3898. }
  3899. // ----- Check the directory availability
  3900. if ((is_dir($p_dir)) || ($p_dir == "")) {
  3901. return 1;
  3902. }
  3903. // ----- Extract parent directory
  3904. $p_parent_dir = dirname($p_dir);
  3905. // ----- Just a check
  3906. if ($p_parent_dir != $p_dir) {
  3907. // ----- Look for parent directory
  3908. if ($p_parent_dir != "") {
  3909. if (($v_result = $this->privDirCheck($p_parent_dir)) != 1) {
  3910. return $v_result;
  3911. }
  3912. }
  3913. }
  3914. // ----- Create the directory
  3915. if (!@mkdir($p_dir, 0777)) {
  3916. // ----- Error log
  3917. PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  3918. // ----- Return
  3919. return PclZip::errorCode();
  3920. }
  3921. // ----- Return
  3922. return $v_result;
  3923. }
  3924. // --------------------------------------------------------------------------------
  3925. // --------------------------------------------------------------------------------
  3926. // Function : privMerge()
  3927. // Description :
  3928. // If $p_archive_to_add does not exist, the function exit with a success result.
  3929. // Parameters :
  3930. // Return Values :
  3931. // --------------------------------------------------------------------------------
  3932. public function privMerge(&$p_archive_to_add)
  3933. {
  3934. $v_result=1;
  3935. // ----- Look if the archive_to_add exists
  3936. if (!is_file($p_archive_to_add->zipname)) {
  3937. // ----- Nothing to merge, so merge is a success
  3938. $v_result = 1;
  3939. // ----- Return
  3940. return $v_result;
  3941. }
  3942. // ----- Look if the archive exists
  3943. if (!is_file($this->zipname)) {
  3944. // ----- Do a duplicate
  3945. $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  3946. // ----- Return
  3947. return $v_result;
  3948. }
  3949. // ----- Open the zip file
  3950. if (($v_result=$this->privOpenFd('rb')) != 1) {
  3951. // ----- Return
  3952. return $v_result;
  3953. }
  3954. // ----- Read the central directory informations
  3955. $v_central_dir = array();
  3956. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  3957. $this->privCloseFd();
  3958. return $v_result;
  3959. }
  3960. // ----- Go to beginning of File
  3961. @rewind($this->zip_fd);
  3962. // ----- Open the archive_to_add file
  3963. if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1) {
  3964. $this->privCloseFd();
  3965. // ----- Return
  3966. return $v_result;
  3967. }
  3968. // ----- Read the central directory informations
  3969. $v_central_dir_to_add = array();
  3970. if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1) {
  3971. $this->privCloseFd();
  3972. $p_archive_to_add->privCloseFd();
  3973. return $v_result;
  3974. }
  3975. // ----- Go to beginning of File
  3976. @rewind($p_archive_to_add->zip_fd);
  3977. // ----- Creates a temporay file
  3978. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  3979. // ----- Open the temporary file in write mode
  3980. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  3981. $this->privCloseFd();
  3982. $p_archive_to_add->privCloseFd();
  3983. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  3984. // ----- Return
  3985. return PclZip::errorCode();
  3986. }
  3987. // ----- Copy the files from the archive to the temporary file
  3988. // TBC : Here I should better append the file and go back to erase the central dir
  3989. $v_size = $v_central_dir['offset'];
  3990. while ($v_size != 0) {
  3991. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3992. $v_buffer = fread($this->zip_fd, $v_read_size);
  3993. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3994. $v_size -= $v_read_size;
  3995. }
  3996. // ----- Copy the files from the archive_to_add into the temporary file
  3997. $v_size = $v_central_dir_to_add['offset'];
  3998. while ($v_size != 0) {
  3999. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4000. $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  4001. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4002. $v_size -= $v_read_size;
  4003. }
  4004. // ----- Store the offset of the central dir
  4005. $v_offset = @ftell($v_zip_temp_fd);
  4006. // ----- Copy the block of file headers from the old archive
  4007. $v_size = $v_central_dir['size'];
  4008. while ($v_size != 0) {
  4009. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4010. $v_buffer = @fread($this->zip_fd, $v_read_size);
  4011. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4012. $v_size -= $v_read_size;
  4013. }
  4014. // ----- Copy the block of file headers from the archive_to_add
  4015. $v_size = $v_central_dir_to_add['size'];
  4016. while ($v_size != 0) {
  4017. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4018. $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  4019. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4020. $v_size -= $v_read_size;
  4021. }
  4022. // ----- Merge the file comments
  4023. $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
  4024. // ----- Calculate the size of the (new) central header
  4025. $v_size = @ftell($v_zip_temp_fd)-$v_offset;
  4026. // ----- Swap the file descriptor
  4027. // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  4028. // the following methods on the temporary fil and not the real archive fd
  4029. $v_swap = $this->zip_fd;
  4030. $this->zip_fd = $v_zip_temp_fd;
  4031. $v_zip_temp_fd = $v_swap;
  4032. // ----- Create the central dir footer
  4033. if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1) {
  4034. $this->privCloseFd();
  4035. $p_archive_to_add->privCloseFd();
  4036. @fclose($v_zip_temp_fd);
  4037. $this->zip_fd = null;
  4038. // ----- Reset the file list
  4039. unset($v_header_list);
  4040. // ----- Return
  4041. return $v_result;
  4042. }
  4043. // ----- Swap back the file descriptor
  4044. $v_swap = $this->zip_fd;
  4045. $this->zip_fd = $v_zip_temp_fd;
  4046. $v_zip_temp_fd = $v_swap;
  4047. // ----- Close
  4048. $this->privCloseFd();
  4049. $p_archive_to_add->privCloseFd();
  4050. // ----- Close the temporary file
  4051. @fclose($v_zip_temp_fd);
  4052. // ----- Delete the zip file
  4053. // TBC : I should test the result ...
  4054. @unlink($this->zipname);
  4055. // ----- Rename the temporary file
  4056. // TBC : I should test the result ...
  4057. //@rename($v_zip_temp_name, $this->zipname);
  4058. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  4059. // ----- Return
  4060. return $v_result;
  4061. }
  4062. // --------------------------------------------------------------------------------
  4063. // --------------------------------------------------------------------------------
  4064. // Function : privDuplicate()
  4065. // Description :
  4066. // Parameters :
  4067. // Return Values :
  4068. // --------------------------------------------------------------------------------
  4069. public function privDuplicate($p_archive_filename)
  4070. {
  4071. $v_result=1;
  4072. // ----- Look if the $p_archive_filename exists
  4073. if (!is_file($p_archive_filename)) {
  4074. // ----- Nothing to duplicate, so duplicate is a success.
  4075. $v_result = 1;
  4076. // ----- Return
  4077. return $v_result;
  4078. }
  4079. // ----- Open the zip file
  4080. if (($v_result=$this->privOpenFd('wb')) != 1) {
  4081. // ----- Return
  4082. return $v_result;
  4083. }
  4084. // ----- Open the temporary file in write mode
  4085. if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
  4086. $this->privCloseFd();
  4087. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
  4088. // ----- Return
  4089. return PclZip::errorCode();
  4090. }
  4091. // ----- Copy the files from the archive to the temporary file
  4092. // TBC : Here I should better append the file and go back to erase the central dir
  4093. $v_size = filesize($p_archive_filename);
  4094. while ($v_size != 0) {
  4095. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4096. $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  4097. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  4098. $v_size -= $v_read_size;
  4099. }
  4100. // ----- Close
  4101. $this->privCloseFd();
  4102. // ----- Close the temporary file
  4103. @fclose($v_zip_temp_fd);
  4104. // ----- Return
  4105. return $v_result;
  4106. }
  4107. // --------------------------------------------------------------------------------
  4108. // --------------------------------------------------------------------------------
  4109. // Function : privErrorLog()
  4110. // Description :
  4111. // Parameters :
  4112. // --------------------------------------------------------------------------------
  4113. public function privErrorLog($p_error_code = 0, $p_error_string = '')
  4114. {
  4115. if (PCLZIP_ERROR_EXTERNAL == 1) {
  4116. PclError($p_error_code, $p_error_string);
  4117. } else {
  4118. $this->error_code = $p_error_code;
  4119. $this->error_string = $p_error_string;
  4120. }
  4121. }
  4122. // --------------------------------------------------------------------------------
  4123. // --------------------------------------------------------------------------------
  4124. // Function : privErrorReset()
  4125. // Description :
  4126. // Parameters :
  4127. // --------------------------------------------------------------------------------
  4128. public function privErrorReset()
  4129. {
  4130. if (PCLZIP_ERROR_EXTERNAL == 1) {
  4131. PclErrorReset();
  4132. } else {
  4133. $this->error_code = 0;
  4134. $this->error_string = '';
  4135. }
  4136. }
  4137. // --------------------------------------------------------------------------------
  4138. // --------------------------------------------------------------------------------
  4139. // Function : privDisableMagicQuotes()
  4140. // Description :
  4141. // Parameters :
  4142. // Return Values :
  4143. // --------------------------------------------------------------------------------
  4144. public function privDisableMagicQuotes()
  4145. {
  4146. $v_result=1;
  4147. // ----- Look if function exists
  4148. if ((!function_exists("get_magic_quotes_runtime")) || (!function_exists("set_magic_quotes_runtime"))) {
  4149. return $v_result;
  4150. }
  4151. // ----- Look if already done
  4152. if ($this->magic_quotes_status != -1) {
  4153. return $v_result;
  4154. }
  4155. // ----- Get and memorize the magic_quote value
  4156. $this->magic_quotes_status = @get_magic_quotes_runtime();
  4157. // ----- Disable magic_quotes
  4158. if ($this->magic_quotes_status == 1) {
  4159. @set_magic_quotes_runtime(0);
  4160. }
  4161. // ----- Return
  4162. return $v_result;
  4163. }
  4164. // --------------------------------------------------------------------------------
  4165. // --------------------------------------------------------------------------------
  4166. // Function : privSwapBackMagicQuotes()
  4167. // Description :
  4168. // Parameters :
  4169. // Return Values :
  4170. // --------------------------------------------------------------------------------
  4171. public function privSwapBackMagicQuotes()
  4172. {
  4173. $v_result=1;
  4174. // ----- Look if function exists
  4175. if ((!function_exists("get_magic_quotes_runtime")) || (!function_exists("set_magic_quotes_runtime"))) {
  4176. return $v_result;
  4177. }
  4178. // ----- Look if something to do
  4179. if ($this->magic_quotes_status != -1) {
  4180. return $v_result;
  4181. }
  4182. // ----- Swap back magic_quotes
  4183. if ($this->magic_quotes_status == 1) {
  4184. @set_magic_quotes_runtime($this->magic_quotes_status);
  4185. }
  4186. // ----- Return
  4187. return $v_result;
  4188. }
  4189. // --------------------------------------------------------------------------------
  4190. }
  4191. // End of class
  4192. // --------------------------------------------------------------------------------
  4193. // --------------------------------------------------------------------------------
  4194. // Function : PclZipUtilPathReduction()
  4195. // Description :
  4196. // Parameters :
  4197. // Return Values :
  4198. // --------------------------------------------------------------------------------
  4199. function PclZipUtilPathReduction($p_dir)
  4200. {
  4201. $v_result = "";
  4202. // ----- Look for not empty path
  4203. if ($p_dir != "") {
  4204. // ----- Explode path by directory names
  4205. $v_list = explode("/", $p_dir);
  4206. // ----- Study directories from last to first
  4207. $v_skip = 0;
  4208. for ($i=sizeof($v_list)-1; $i>=0; $i--) {
  4209. // ----- Look for current path
  4210. if ($v_list[$i] == ".") {
  4211. // ----- Ignore this directory
  4212. // Should be the first $i=0, but no check is done
  4213. } elseif ($v_list[$i] == "..") {
  4214. $v_skip++;
  4215. } elseif ($v_list[$i] == "") {
  4216. // ----- First '/' i.e. root slash
  4217. if ($i == 0) {
  4218. $v_result = "/".$v_result;
  4219. if ($v_skip > 0) {
  4220. // ----- It is an invalid path, so the path is not modified
  4221. // TBC
  4222. $v_result = $p_dir;
  4223. $v_skip = 0;
  4224. }
  4225. } elseif ($i == (sizeof($v_list)-1)) {
  4226. // ----- Last '/' i.e. indicates a directory
  4227. $v_result = $v_list[$i];
  4228. } else {
  4229. // ----- Double '/' inside the path
  4230. // ----- Ignore only the double '//' in path,
  4231. // but not the first and last '/'
  4232. }
  4233. } else {
  4234. // ----- Look for item to skip
  4235. if ($v_skip > 0) {
  4236. $v_skip--;
  4237. } else {
  4238. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  4239. }
  4240. }
  4241. }
  4242. // ----- Look for skip
  4243. if ($v_skip > 0) {
  4244. while ($v_skip > 0) {
  4245. $v_result = '../'.$v_result;
  4246. $v_skip--;
  4247. }
  4248. }
  4249. }
  4250. // ----- Return
  4251. return $v_result;
  4252. }
  4253. // --------------------------------------------------------------------------------
  4254. // --------------------------------------------------------------------------------
  4255. // Function : PclZipUtilPathInclusion()
  4256. // Description :
  4257. // This function indicates if the path $p_path is under the $p_dir tree. Or,
  4258. // said in an other way, if the file or sub-dir $p_path is inside the dir
  4259. // $p_dir.
  4260. // The function indicates also if the path is exactly the same as the dir.
  4261. // This function supports path with duplicated '/' like '//', but does not
  4262. // support '.' or '..' statements.
  4263. // Parameters :
  4264. // Return Values :
  4265. // 0 if $p_path is not inside directory $p_dir
  4266. // 1 if $p_path is inside directory $p_dir
  4267. // 2 if $p_path is exactly the same as $p_dir
  4268. // --------------------------------------------------------------------------------
  4269. function PclZipUtilPathInclusion($p_dir, $p_path)
  4270. {
  4271. $v_result = 1;
  4272. // ----- Look for path beginning by ./
  4273. if (($p_dir == '.') || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
  4274. $p_dir = PclZipUtilTranslateWinPath(getcwd(), false).'/'.substr($p_dir, 1);
  4275. }
  4276. if (($p_path == '.') || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
  4277. $p_path = PclZipUtilTranslateWinPath(getcwd(), false).'/'.substr($p_path, 1);
  4278. }
  4279. // ----- Explode dir and path by directory separator
  4280. $v_list_dir = explode("/", $p_dir);
  4281. $v_list_dir_size = sizeof($v_list_dir);
  4282. $v_list_path = explode("/", $p_path);
  4283. $v_list_path_size = sizeof($v_list_path);
  4284. // ----- Study directories paths
  4285. $i = 0;
  4286. $j = 0;
  4287. while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  4288. // ----- Look for empty dir (path reduction)
  4289. if ($v_list_dir[$i] == '') {
  4290. $i++;
  4291. continue;
  4292. }
  4293. if ($v_list_path[$j] == '') {
  4294. $j++;
  4295. continue;
  4296. }
  4297. // ----- Compare the items
  4298. if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ($v_list_path[$j] != '')) {
  4299. $v_result = 0;
  4300. }
  4301. // ----- Next items
  4302. $i++;
  4303. $j++;
  4304. }
  4305. // ----- Look if everything seems to be the same
  4306. if ($v_result) {
  4307. // ----- Skip all the empty items
  4308. while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) {
  4309. $j++;
  4310. }
  4311. while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) {
  4312. $i++;
  4313. }
  4314. if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  4315. // ----- There are exactly the same
  4316. $v_result = 2;
  4317. } elseif ($i < $v_list_dir_size) {
  4318. // ----- The path is shorter than the dir
  4319. $v_result = 0;
  4320. }
  4321. }
  4322. // ----- Return
  4323. return $v_result;
  4324. }
  4325. // --------------------------------------------------------------------------------
  4326. // --------------------------------------------------------------------------------
  4327. // Function : PclZipUtilCopyBlock()
  4328. // Description :
  4329. // Parameters :
  4330. // $p_mode : read/write compression mode
  4331. // 0 : src & dest normal
  4332. // 1 : src gzip, dest normal
  4333. // 2 : src normal, dest gzip
  4334. // 3 : src & dest gzip
  4335. // Return Values :
  4336. // --------------------------------------------------------------------------------
  4337. function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode = 0)
  4338. {
  4339. $v_result = 1;
  4340. if ($p_mode==0) {
  4341. while ($p_size != 0) {
  4342. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4343. $v_buffer = @fread($p_src, $v_read_size);
  4344. @fwrite($p_dest, $v_buffer, $v_read_size);
  4345. $p_size -= $v_read_size;
  4346. }
  4347. } elseif ($p_mode==1) {
  4348. while ($p_size != 0) {
  4349. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4350. $v_buffer = @gzread($p_src, $v_read_size);
  4351. @fwrite($p_dest, $v_buffer, $v_read_size);
  4352. $p_size -= $v_read_size;
  4353. }
  4354. } elseif ($p_mode==2) {
  4355. while ($p_size != 0) {
  4356. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4357. $v_buffer = @fread($p_src, $v_read_size);
  4358. @gzwrite($p_dest, $v_buffer, $v_read_size);
  4359. $p_size -= $v_read_size;
  4360. }
  4361. } elseif ($p_mode==3) {
  4362. while ($p_size != 0) {
  4363. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4364. $v_buffer = @gzread($p_src, $v_read_size);
  4365. @gzwrite($p_dest, $v_buffer, $v_read_size);
  4366. $p_size -= $v_read_size;
  4367. }
  4368. }
  4369. // ----- Return
  4370. return $v_result;
  4371. }
  4372. // --------------------------------------------------------------------------------
  4373. // --------------------------------------------------------------------------------
  4374. // Function : PclZipUtilRename()
  4375. // Description :
  4376. // This function tries to do a simple rename() function. If it fails, it
  4377. // tries to copy the $p_src file in a new $p_dest file and then unlink the
  4378. // first one.
  4379. // Parameters :
  4380. // $p_src : Old filename
  4381. // $p_dest : New filename
  4382. // Return Values :
  4383. // 1 on success, 0 on failure.
  4384. // --------------------------------------------------------------------------------
  4385. function PclZipUtilRename($p_src, $p_dest)
  4386. {
  4387. $v_result = 1;
  4388. // ----- Try to rename the files
  4389. if (!@rename($p_src, $p_dest)) {
  4390. // ----- Try to copy & unlink the src
  4391. if (!@copy($p_src, $p_dest)) {
  4392. $v_result = 0;
  4393. } elseif (!@unlink($p_src)) {
  4394. $v_result = 0;
  4395. }
  4396. }
  4397. // ----- Return
  4398. return $v_result;
  4399. }
  4400. // --------------------------------------------------------------------------------
  4401. // --------------------------------------------------------------------------------
  4402. // Function : PclZipUtilOptionText()
  4403. // Description :
  4404. // Translate option value in text. Mainly for debug purpose.
  4405. // Parameters :
  4406. // $p_option : the option value.
  4407. // Return Values :
  4408. // The option text value.
  4409. // --------------------------------------------------------------------------------
  4410. function PclZipUtilOptionText($p_option)
  4411. {
  4412. $v_list = get_defined_constants();
  4413. for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  4414. $v_prefix = substr($v_key, 0, 10);
  4415. if ((($v_prefix == 'PCLZIP_OPT') || ($v_prefix == 'PCLZIP_CB_') || ($v_prefix == 'PCLZIP_ATT')) && ($v_list[$v_key] == $p_option)) {
  4416. return $v_key;
  4417. }
  4418. }
  4419. $v_result = 'Unknown';
  4420. return $v_result;
  4421. }
  4422. // --------------------------------------------------------------------------------
  4423. // --------------------------------------------------------------------------------
  4424. // Function : PclZipUtilTranslateWinPath()
  4425. // Description :
  4426. // Translate windows path by replacing '\' by '/' and optionally removing
  4427. // drive letter.
  4428. // Parameters :
  4429. // $p_path : path to translate.
  4430. // $p_remove_disk_letter : true | false
  4431. // Return Values :
  4432. // The path translated.
  4433. // --------------------------------------------------------------------------------
  4434. function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter = true)
  4435. {
  4436. if (stristr(php_uname(), 'windows')) {
  4437. // ----- Look for potential disk letter
  4438. if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
  4439. $p_path = substr($p_path, $v_position+1);
  4440. }
  4441. // ----- Change potential windows directory separator
  4442. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
  4443. $p_path = strtr($p_path, '\\', '/');
  4444. }
  4445. }
  4446. return $p_path;
  4447. }