pclzip.lib.php 271 KB

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