pclzip.lib.php 210 KB

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