pclzip.lib.php 199 KB

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