pclzip.lib.php 243 KB

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