pclzip.lib.php 198 KB

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