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. }
  1084. return ($this->error_code);
  1085. }
  1086. // --------------------------------------------------------------------------------
  1087. // --------------------------------------------------------------------------------
  1088. // Function : errorName()
  1089. // Description :
  1090. // Parameters :
  1091. // --------------------------------------------------------------------------------
  1092. public function errorName($p_with_code = false)
  1093. {
  1094. $v_name = array(
  1095. PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
  1096. PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
  1097. PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
  1098. PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
  1099. PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
  1100. PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
  1101. PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
  1102. PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
  1103. PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
  1104. PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
  1105. PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
  1106. PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
  1107. PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
  1108. PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
  1109. PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
  1110. PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
  1111. PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
  1112. PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
  1113. PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION',
  1114. PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE',
  1115. PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
  1116. );
  1117. if (isset($v_name[$this->error_code])) {
  1118. $v_value = $v_name[$this->error_code];
  1119. } else {
  1120. $v_value = 'NoName';
  1121. }
  1122. if ($p_with_code) {
  1123. return ($v_value . ' (' . $this->error_code . ')');
  1124. }
  1125. return ($v_value);
  1126. }
  1127. // --------------------------------------------------------------------------------
  1128. // --------------------------------------------------------------------------------
  1129. // Function : errorInfo()
  1130. // Description :
  1131. // Parameters :
  1132. // --------------------------------------------------------------------------------
  1133. public function errorInfo($p_full = false)
  1134. {
  1135. if (PCLZIP_ERROR_EXTERNAL == 1) {
  1136. return (PclErrorString());
  1137. }
  1138. if ($p_full) {
  1139. return ($this->errorName(true) . " : " . $this->error_string);
  1140. }
  1141. return ($this->error_string . " [code " . $this->error_code . "]");
  1142. }
  1143. // --------------------------------------------------------------------------------
  1144. // --------------------------------------------------------------------------------
  1145. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  1146. // ***** *****
  1147. // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
  1148. // --------------------------------------------------------------------------------
  1149. // --------------------------------------------------------------------------------
  1150. // Function : privCheckFormat()
  1151. // Description :
  1152. // This method check that the archive exists and is a valid zip archive.
  1153. // Several level of check exists. (futur)
  1154. // Parameters :
  1155. // $p_level : Level of check. Default 0.
  1156. // 0 : Check the first bytes (magic codes) (default value))
  1157. // 1 : 0 + Check the central directory (futur)
  1158. // 2 : 1 + Check each file header (futur)
  1159. // Return Values :
  1160. // true on success,
  1161. // false on error, the error code is set.
  1162. // --------------------------------------------------------------------------------
  1163. public function privCheckFormat($p_level = 0)
  1164. {
  1165. $v_result = true;
  1166. // ----- Reset the file system cache
  1167. clearstatcache();
  1168. // ----- Reset the error handler
  1169. $this->privErrorReset();
  1170. // ----- Look if the file exits
  1171. if (!is_file($this->zipname)) {
  1172. // ----- Error log
  1173. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '" . $this->zipname . "'");
  1174. return (false);
  1175. }
  1176. // ----- Check that the file is readeable
  1177. if (!is_readable($this->zipname)) {
  1178. // ----- Error log
  1179. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '" . $this->zipname . "'");
  1180. return (false);
  1181. }
  1182. // ----- Check the magic code
  1183. // TBC
  1184. // ----- Check the central header
  1185. // TBC
  1186. // ----- Check each file header
  1187. // TBC
  1188. // ----- Return
  1189. return $v_result;
  1190. }
  1191. // --------------------------------------------------------------------------------
  1192. // --------------------------------------------------------------------------------
  1193. // Function : privParseOptions()
  1194. // Description :
  1195. // This internal methods reads the variable list of arguments ($p_options_list,
  1196. // $p_size) and generate an array with the options and values ($v_result_list).
  1197. // $v_requested_options contains the options that can be present and those that
  1198. // must be present.
  1199. // $v_requested_options is an array, with the option value as key, and 'optional',
  1200. // or 'mandatory' as value.
  1201. // Parameters :
  1202. // See above.
  1203. // Return Values :
  1204. // 1 on success.
  1205. // 0 on failure.
  1206. // --------------------------------------------------------------------------------
  1207. public function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options = false)
  1208. {
  1209. $v_result = 1;
  1210. // ----- Read the options
  1211. $i = 0;
  1212. while ($i < $p_size) {
  1213. // ----- Check if the option is supported
  1214. if (!isset($v_requested_options[$p_options_list[$i]])) {
  1215. // ----- Error log
  1216. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '" . $p_options_list[$i] . "' for this method");
  1217. // ----- Return
  1218. return PclZip::errorCode();
  1219. }
  1220. // ----- Look for next option
  1221. switch ($p_options_list[$i]) {
  1222. // ----- Look for options that request a path value
  1223. case PCLZIP_OPT_PATH:
  1224. case PCLZIP_OPT_REMOVE_PATH:
  1225. case PCLZIP_OPT_ADD_PATH:
  1226. // ----- Check the number of parameters
  1227. if (($i + 1) >= $p_size) {
  1228. // ----- Error log
  1229. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1230. // ----- Return
  1231. return PclZip::errorCode();
  1232. }
  1233. // ----- Get the value
  1234. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i + 1], false);
  1235. $i++;
  1236. break;
  1237. case PCLZIP_OPT_TEMP_FILE_THRESHOLD:
  1238. // ----- Check the number of parameters
  1239. if (($i + 1) >= $p_size) {
  1240. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1241. return PclZip::errorCode();
  1242. }
  1243. // ----- Check for incompatible options
  1244. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1245. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1246. return PclZip::errorCode();
  1247. }
  1248. // ----- Check the value
  1249. $v_value = $p_options_list[$i + 1];
  1250. if ((!is_integer($v_value)) || ($v_value < 0)) {
  1251. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1252. return PclZip::errorCode();
  1253. }
  1254. // ----- Get the value (and convert it in bytes)
  1255. $v_result_list[$p_options_list[$i]] = $v_value * 1048576;
  1256. $i++;
  1257. break;
  1258. case PCLZIP_OPT_TEMP_FILE_ON:
  1259. // ----- Check for incompatible options
  1260. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1261. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1262. return PclZip::errorCode();
  1263. }
  1264. $v_result_list[$p_options_list[$i]] = true;
  1265. break;
  1266. case PCLZIP_OPT_TEMP_FILE_OFF:
  1267. // ----- Check for incompatible options
  1268. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
  1269. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
  1270. return PclZip::errorCode();
  1271. }
  1272. // ----- Check for incompatible options
  1273. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1274. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
  1275. return PclZip::errorCode();
  1276. }
  1277. $v_result_list[$p_options_list[$i]] = true;
  1278. break;
  1279. case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION:
  1280. // ----- Check the number of parameters
  1281. if (($i + 1) >= $p_size) {
  1282. // ----- Error log
  1283. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1284. // ----- Return
  1285. return PclZip::errorCode();
  1286. }
  1287. // ----- Get the value
  1288. if (is_string($p_options_list[$i + 1]) && ($p_options_list[$i + 1] != '')) {
  1289. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i + 1], false);
  1290. $i++;
  1291. } else {
  1292. }
  1293. break;
  1294. // ----- Look for options that request an array of string for value
  1295. case PCLZIP_OPT_BY_NAME:
  1296. // ----- Check the number of parameters
  1297. if (($i + 1) >= $p_size) {
  1298. // ----- Error log
  1299. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1300. // ----- Return
  1301. return PclZip::errorCode();
  1302. }
  1303. // ----- Get the value
  1304. if (is_string($p_options_list[$i + 1])) {
  1305. $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i + 1];
  1306. } elseif (is_array($p_options_list[$i + 1])) {
  1307. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  1308. } else {
  1309. // ----- Error log
  1310. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1311. // ----- Return
  1312. return PclZip::errorCode();
  1313. }
  1314. $i++;
  1315. break;
  1316. // ----- Look for options that request an EREG or PREG expression
  1317. case PCLZIP_OPT_BY_EREG:
  1318. $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
  1319. // ereg() is deprecated starting with PHP 5.3. Move PCLZIP_OPT_BY_EREG
  1320. // to PCLZIP_OPT_BY_PREG
  1321. case PCLZIP_OPT_BY_PREG:
  1322. //case PCLZIP_OPT_CRYPT :
  1323. // ----- Check the number of parameters
  1324. if (($i + 1) >= $p_size) {
  1325. // ----- Error log
  1326. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1327. // ----- Return
  1328. return PclZip::errorCode();
  1329. }
  1330. // ----- Get the value
  1331. if (is_string($p_options_list[$i + 1])) {
  1332. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  1333. } else {
  1334. // ----- Error log
  1335. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1336. // ----- Return
  1337. return PclZip::errorCode();
  1338. }
  1339. $i++;
  1340. break;
  1341. // ----- Look for options that takes a string
  1342. case PCLZIP_OPT_COMMENT:
  1343. case PCLZIP_OPT_ADD_COMMENT:
  1344. case PCLZIP_OPT_PREPEND_COMMENT:
  1345. // ----- Check the number of parameters
  1346. if (($i + 1) >= $p_size) {
  1347. // ----- Error log
  1348. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1349. // ----- Return
  1350. return PclZip::errorCode();
  1351. }
  1352. // ----- Get the value
  1353. if (is_string($p_options_list[$i + 1])) {
  1354. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  1355. } else {
  1356. // ----- Error log
  1357. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1358. // ----- Return
  1359. return PclZip::errorCode();
  1360. }
  1361. $i++;
  1362. break;
  1363. // ----- Look for options that request an array of index
  1364. case PCLZIP_OPT_BY_INDEX:
  1365. // ----- Check the number of parameters
  1366. if (($i + 1) >= $p_size) {
  1367. // ----- Error log
  1368. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1369. // ----- Return
  1370. return PclZip::errorCode();
  1371. }
  1372. // ----- Get the value
  1373. $v_work_list = array();
  1374. if (is_string($p_options_list[$i + 1])) {
  1375. // ----- Remove spaces
  1376. $p_options_list[$i + 1] = strtr($p_options_list[$i + 1], ' ', '');
  1377. // ----- Parse items
  1378. $v_work_list = explode(",", $p_options_list[$i + 1]);
  1379. } elseif (is_integer($p_options_list[$i + 1])) {
  1380. $v_work_list[0] = $p_options_list[$i + 1] . '-' . $p_options_list[$i + 1];
  1381. } elseif (is_array($p_options_list[$i + 1])) {
  1382. $v_work_list = $p_options_list[$i + 1];
  1383. } else {
  1384. // ----- Error log
  1385. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1386. // ----- Return
  1387. return PclZip::errorCode();
  1388. }
  1389. // ----- Reduce the index list
  1390. // each index item in the list must be a couple with a start and
  1391. // an end value : [0,3], [5-5], [8-10], ...
  1392. // ----- Check the format of each item
  1393. $v_sort_flag = false;
  1394. $v_sort_value = 0;
  1395. for ($j = 0; $j < sizeof($v_work_list); $j++) {
  1396. // ----- Explode the item
  1397. $v_item_list = explode("-", $v_work_list[$j]);
  1398. $v_size_item_list = sizeof($v_item_list);
  1399. // ----- TBC : Here we might check that each item is a
  1400. // real integer ...
  1401. // ----- Look for single value
  1402. if ($v_size_item_list == 1) {
  1403. // ----- Set the option value
  1404. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  1405. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  1406. } elseif ($v_size_item_list == 2) {
  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[1];
  1410. } else {
  1411. // ----- Error log
  1412. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1413. // ----- Return
  1414. return PclZip::errorCode();
  1415. }
  1416. // ----- Look for list sort
  1417. if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  1418. $v_sort_flag = true;
  1419. // ----- TBC : An automatic sort should be writen ...
  1420. // ----- Error log
  1421. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1422. // ----- Return
  1423. return PclZip::errorCode();
  1424. }
  1425. $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  1426. }
  1427. // ----- Sort the items
  1428. if ($v_sort_flag) {
  1429. // TBC : To Be Completed
  1430. }
  1431. // ----- Next option
  1432. $i++;
  1433. break;
  1434. // ----- Look for options that request no value
  1435. case PCLZIP_OPT_REMOVE_ALL_PATH:
  1436. case PCLZIP_OPT_EXTRACT_AS_STRING:
  1437. case PCLZIP_OPT_NO_COMPRESSION:
  1438. case PCLZIP_OPT_EXTRACT_IN_OUTPUT:
  1439. case PCLZIP_OPT_REPLACE_NEWER:
  1440. case PCLZIP_OPT_STOP_ON_ERROR:
  1441. $v_result_list[$p_options_list[$i]] = true;
  1442. break;
  1443. // ----- Look for options that request an octal value
  1444. case PCLZIP_OPT_SET_CHMOD:
  1445. // ----- Check the number of parameters
  1446. if (($i + 1) >= $p_size) {
  1447. // ----- Error log
  1448. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1449. // ----- Return
  1450. return PclZip::errorCode();
  1451. }
  1452. // ----- Get the value
  1453. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  1454. $i++;
  1455. break;
  1456. // ----- Look for options that request a call-back
  1457. case PCLZIP_CB_PRE_EXTRACT:
  1458. case PCLZIP_CB_POST_EXTRACT:
  1459. case PCLZIP_CB_PRE_ADD:
  1460. case PCLZIP_CB_POST_ADD:
  1461. /* for futur use
  1462. case PCLZIP_CB_PRE_DELETE :
  1463. case PCLZIP_CB_POST_DELETE :
  1464. case PCLZIP_CB_PRE_LIST :
  1465. case PCLZIP_CB_POST_LIST :
  1466. */
  1467. // ----- Check the number of parameters
  1468. if (($i + 1) >= $p_size) {
  1469. // ----- Error log
  1470. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1471. // ----- Return
  1472. return PclZip::errorCode();
  1473. }
  1474. // ----- Get the value
  1475. $v_function_name = $p_options_list[$i + 1];
  1476. // ----- Check that the value is a valid existing function
  1477. if (!function_exists($v_function_name)) {
  1478. // ----- Error log
  1479. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '" . $v_function_name . "()' is not an existing function for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  1480. // ----- Return
  1481. return PclZip::errorCode();
  1482. }
  1483. // ----- Set the attribute
  1484. $v_result_list[$p_options_list[$i]] = $v_function_name;
  1485. $i++;
  1486. break;
  1487. default:
  1488. // ----- Error log
  1489. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Unknown parameter '" . $p_options_list[$i] . "'");
  1490. // ----- Return
  1491. return PclZip::errorCode();
  1492. }
  1493. // ----- Next options
  1494. $i++;
  1495. }
  1496. // ----- Look for mandatory options
  1497. if ($v_requested_options !== false) {
  1498. for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
  1499. // ----- Look for mandatory option
  1500. if ($v_requested_options[$key] == 'mandatory') {
  1501. // ----- Look if present
  1502. if (!isset($v_result_list[$key])) {
  1503. // ----- Error log
  1504. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter " . PclZipUtilOptionText($key) . "(" . $key . ")");
  1505. // ----- Return
  1506. return PclZip::errorCode();
  1507. }
  1508. }
  1509. }
  1510. }
  1511. // ----- Look for default values
  1512. if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1513. }
  1514. // ----- Return
  1515. return $v_result;
  1516. }
  1517. // --------------------------------------------------------------------------------
  1518. // --------------------------------------------------------------------------------
  1519. // Function : privOptionDefaultThreshold()
  1520. // Description :
  1521. // Parameters :
  1522. // Return Values :
  1523. // --------------------------------------------------------------------------------
  1524. public function privOptionDefaultThreshold(&$p_options)
  1525. {
  1526. $v_result = 1;
  1527. if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]) || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1528. return $v_result;
  1529. }
  1530. // ----- Get 'memory_limit' configuration value
  1531. $v_memory_limit = ini_get('memory_limit');
  1532. $v_memory_limit = trim($v_memory_limit);
  1533. $last = strtolower(substr($v_memory_limit, -1));
  1534. $v_memory_limit = preg_replace('/[^0-9,.]/', '', $v_memory_limit);
  1535. if ($last == 'g') {
  1536. //$v_memory_limit = $v_memory_limit*1024*1024*1024;
  1537. $v_memory_limit = $v_memory_limit * 1073741824;
  1538. }
  1539. if ($last == 'm') {
  1540. //$v_memory_limit = $v_memory_limit*1024*1024;
  1541. $v_memory_limit = $v_memory_limit * 1048576;
  1542. }
  1543. if ($last == 'k') {
  1544. $v_memory_limit = $v_memory_limit * 1024;
  1545. }
  1546. $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit * PCLZIP_TEMPORARY_FILE_RATIO);
  1547. // ----- Sanity check : No threshold if value lower than 1M
  1548. if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
  1549. unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
  1550. }
  1551. // ----- Return
  1552. return $v_result;
  1553. }
  1554. // --------------------------------------------------------------------------------
  1555. // --------------------------------------------------------------------------------
  1556. // Function : privFileDescrParseAtt()
  1557. // Description :
  1558. // Parameters :
  1559. // Return Values :
  1560. // 1 on success.
  1561. // 0 on failure.
  1562. // --------------------------------------------------------------------------------
  1563. public function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options = false)
  1564. {
  1565. $v_result = 1;
  1566. // ----- For each file in the list check the attributes
  1567. foreach ($p_file_list as $v_key => $v_value) {
  1568. // ----- Check if the option is supported
  1569. if (!isset($v_requested_options[$v_key])) {
  1570. // ----- Error log
  1571. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '" . $v_key . "' for this file");
  1572. // ----- Return
  1573. return PclZip::errorCode();
  1574. }
  1575. // ----- Look for attribute
  1576. switch ($v_key) {
  1577. case PCLZIP_ATT_FILE_NAME:
  1578. if (!is_string($v_value)) {
  1579. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  1580. return PclZip::errorCode();
  1581. }
  1582. $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
  1583. if ($p_filedescr['filename'] == '') {
  1584. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  1585. return PclZip::errorCode();
  1586. }
  1587. break;
  1588. case PCLZIP_ATT_FILE_NEW_SHORT_NAME:
  1589. if (!is_string($v_value)) {
  1590. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  1591. return PclZip::errorCode();
  1592. }
  1593. $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
  1594. if ($p_filedescr['new_short_name'] == '') {
  1595. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  1596. return PclZip::errorCode();
  1597. }
  1598. break;
  1599. case PCLZIP_ATT_FILE_NEW_FULL_NAME:
  1600. if (!is_string($v_value)) {
  1601. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  1602. return PclZip::errorCode();
  1603. }
  1604. $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
  1605. if ($p_filedescr['new_full_name'] == '') {
  1606. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  1607. return PclZip::errorCode();
  1608. }
  1609. break;
  1610. // ----- Look for options that takes a string
  1611. case PCLZIP_ATT_FILE_COMMENT:
  1612. if (!is_string($v_value)) {
  1613. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  1614. return PclZip::errorCode();
  1615. }
  1616. $p_filedescr['comment'] = $v_value;
  1617. break;
  1618. case PCLZIP_ATT_FILE_MTIME:
  1619. if (!is_integer($v_value)) {
  1620. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". Integer expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  1621. return PclZip::errorCode();
  1622. }
  1623. $p_filedescr['mtime'] = $v_value;
  1624. break;
  1625. case PCLZIP_ATT_FILE_CONTENT:
  1626. $p_filedescr['content'] = $v_value;
  1627. break;
  1628. default:
  1629. // ----- Error log
  1630. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Unknown parameter '" . $v_key . "'");
  1631. // ----- Return
  1632. return PclZip::errorCode();
  1633. }
  1634. // ----- Look for mandatory options
  1635. if ($v_requested_options !== false) {
  1636. for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
  1637. // ----- Look for mandatory option
  1638. if ($v_requested_options[$key] == 'mandatory') {
  1639. // ----- Look if present
  1640. if (!isset($p_file_list[$key])) {
  1641. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter " . PclZipUtilOptionText($key) . "(" . $key . ")");
  1642. return PclZip::errorCode();
  1643. }
  1644. }
  1645. }
  1646. }
  1647. // end foreach
  1648. }
  1649. // ----- Return
  1650. return $v_result;
  1651. }
  1652. // --------------------------------------------------------------------------------
  1653. // --------------------------------------------------------------------------------
  1654. // Function : privFileDescrExpand()
  1655. // Description :
  1656. // This method look for each item of the list to see if its a file, a folder
  1657. // or a string to be added as file. For any other type of files (link, other)
  1658. // just ignore the item.
  1659. // Then prepare the information that will be stored for that file.
  1660. // When its a folder, expand the folder with all the files that are in that
  1661. // folder (recursively).
  1662. // Parameters :
  1663. // Return Values :
  1664. // 1 on success.
  1665. // 0 on failure.
  1666. // --------------------------------------------------------------------------------
  1667. public function privFileDescrExpand(&$p_filedescr_list, &$p_options)
  1668. {
  1669. $v_result = 1;
  1670. // ----- Create a result list
  1671. $v_result_list = array();
  1672. // ----- Look each entry
  1673. for ($i = 0; $i < sizeof($p_filedescr_list); $i++) {
  1674. // ----- Get filedescr
  1675. $v_descr = $p_filedescr_list[$i];
  1676. // ----- Reduce the filename
  1677. $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
  1678. $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
  1679. // ----- Look for real file or folder
  1680. if (file_exists($v_descr['filename'])) {
  1681. if (@is_file($v_descr['filename'])) {
  1682. $v_descr['type'] = 'file';
  1683. } elseif (@is_dir($v_descr['filename'])) {
  1684. $v_descr['type'] = 'folder';
  1685. } elseif (@is_link($v_descr['filename'])) {
  1686. // skip
  1687. continue;
  1688. } else {
  1689. // skip
  1690. continue;
  1691. }
  1692. // ----- Look for string added as file
  1693. } elseif (isset($v_descr['content'])) {
  1694. $v_descr['type'] = 'virtual_file';
  1695. // ----- Missing file
  1696. } else {
  1697. // ----- Error log
  1698. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '" . $v_descr['filename'] . "' does not exist");
  1699. // ----- Return
  1700. return PclZip::errorCode();
  1701. }
  1702. // ----- Calculate the stored filename
  1703. $this->privCalculateStoredFilename($v_descr, $p_options);
  1704. // ----- Add the descriptor in result list
  1705. $v_result_list[sizeof($v_result_list)] = $v_descr;
  1706. // ----- Look for folder
  1707. if ($v_descr['type'] == 'folder') {
  1708. // ----- List of items in folder
  1709. $v_dirlist_descr = array();
  1710. $v_dirlist_nb = 0;
  1711. if ($v_folder_handler = @opendir($v_descr['filename'])) {
  1712. while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
  1713. // ----- Skip '.' and '..'
  1714. if (($v_item_handler == '.') || ($v_item_handler == '..')) {
  1715. continue;
  1716. }
  1717. // ----- Compose the full filename
  1718. $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'] . '/' . $v_item_handler;
  1719. // ----- Look for different stored filename
  1720. // Because the name of the folder was changed, the name of the
  1721. // files/sub-folders also change
  1722. if (($v_descr['stored_filename'] != $v_descr['filename']) && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
  1723. if ($v_descr['stored_filename'] != '') {
  1724. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'] . '/' . $v_item_handler;
  1725. } else {
  1726. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
  1727. }
  1728. }
  1729. $v_dirlist_nb++;
  1730. }
  1731. @closedir($v_folder_handler);
  1732. } else {
  1733. // TBC : unable to open folder in read mode
  1734. }
  1735. // ----- Expand each element of the list
  1736. if ($v_dirlist_nb != 0) {
  1737. // ----- Expand
  1738. if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
  1739. return $v_result;
  1740. }
  1741. // ----- Concat the resulting list
  1742. $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
  1743. } else {
  1744. }
  1745. // ----- Free local array
  1746. unset($v_dirlist_descr);
  1747. }
  1748. }
  1749. // ----- Get the result list
  1750. $p_filedescr_list = $v_result_list;
  1751. // ----- Return
  1752. return $v_result;
  1753. }
  1754. // --------------------------------------------------------------------------------
  1755. // --------------------------------------------------------------------------------
  1756. // Function : privCreate()
  1757. // Description :
  1758. // Parameters :
  1759. // Return Values :
  1760. // --------------------------------------------------------------------------------
  1761. public function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
  1762. {
  1763. $v_result = 1;
  1764. $v_list_detail = array();
  1765. // ----- Magic quotes trick
  1766. $this->privDisableMagicQuotes();
  1767. // ----- Open the file in write mode
  1768. if (($v_result = $this->privOpenFd('wb')) != 1) {
  1769. // ----- Return
  1770. return $v_result;
  1771. }
  1772. // ----- Add the list of files
  1773. $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
  1774. // ----- Close
  1775. $this->privCloseFd();
  1776. // ----- Magic quotes trick
  1777. $this->privSwapBackMagicQuotes();
  1778. // ----- Return
  1779. return $v_result;
  1780. }
  1781. // --------------------------------------------------------------------------------
  1782. // --------------------------------------------------------------------------------
  1783. // Function : privAdd()
  1784. // Description :
  1785. // Parameters :
  1786. // Return Values :
  1787. // --------------------------------------------------------------------------------
  1788. public function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
  1789. {
  1790. $v_result = 1;
  1791. $v_list_detail = array();
  1792. // ----- Look if the archive exists or is empty
  1793. if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0)) {
  1794. // ----- Do a create
  1795. $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
  1796. // ----- Return
  1797. return $v_result;
  1798. }
  1799. // ----- Magic quotes trick
  1800. $this->privDisableMagicQuotes();
  1801. // ----- Open the zip file
  1802. if (($v_result = $this->privOpenFd('rb')) != 1) {
  1803. // ----- Magic quotes trick
  1804. $this->privSwapBackMagicQuotes();
  1805. // ----- Return
  1806. return $v_result;
  1807. }
  1808. // ----- Read the central directory informations
  1809. $v_central_dir = array();
  1810. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  1811. $this->privCloseFd();
  1812. $this->privSwapBackMagicQuotes();
  1813. return $v_result;
  1814. }
  1815. // ----- Go to beginning of File
  1816. @rewind($this->zip_fd);
  1817. // ----- Creates a temporay file
  1818. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  1819. // ----- Open the temporary file in write mode
  1820. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  1821. $this->privCloseFd();
  1822. $this->privSwapBackMagicQuotes();
  1823. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');
  1824. // ----- Return
  1825. return PclZip::errorCode();
  1826. }
  1827. // ----- Copy the files from the archive to the temporary file
  1828. // TBC : Here I should better append the file and go back to erase the central dir
  1829. $v_size = $v_central_dir['offset'];
  1830. while ($v_size != 0) {
  1831. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1832. $v_buffer = fread($this->zip_fd, $v_read_size);
  1833. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  1834. $v_size -= $v_read_size;
  1835. }
  1836. // ----- Swap the file descriptor
  1837. // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  1838. // the following methods on the temporary fil and not the real archive
  1839. $v_swap = $this->zip_fd;
  1840. $this->zip_fd = $v_zip_temp_fd;
  1841. $v_zip_temp_fd = $v_swap;
  1842. // ----- Add the files
  1843. $v_header_list = array();
  1844. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
  1845. fclose($v_zip_temp_fd);
  1846. $this->privCloseFd();
  1847. @unlink($v_zip_temp_name);
  1848. $this->privSwapBackMagicQuotes();
  1849. // ----- Return
  1850. return $v_result;
  1851. }
  1852. // ----- Store the offset of the central dir
  1853. $v_offset = @ftell($this->zip_fd);
  1854. // ----- Copy the block of file headers from the old archive
  1855. $v_size = $v_central_dir['size'];
  1856. while ($v_size != 0) {
  1857. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1858. $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  1859. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1860. $v_size -= $v_read_size;
  1861. }
  1862. // ----- Create the Central Dir files header
  1863. for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); $i++) {
  1864. // ----- Create the file header
  1865. if ($v_header_list[$i]['status'] == 'ok') {
  1866. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1867. fclose($v_zip_temp_fd);
  1868. $this->privCloseFd();
  1869. @unlink($v_zip_temp_name);
  1870. $this->privSwapBackMagicQuotes();
  1871. // ----- Return
  1872. return $v_result;
  1873. }
  1874. $v_count++;
  1875. }
  1876. // ----- Transform the header to a 'usable' info
  1877. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1878. }
  1879. // ----- Zip file comment
  1880. $v_comment = $v_central_dir['comment'];
  1881. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1882. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1883. }
  1884. if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
  1885. $v_comment = $v_comment . $p_options[PCLZIP_OPT_ADD_COMMENT];
  1886. }
  1887. if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
  1888. $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT] . $v_comment;
  1889. }
  1890. // ----- Calculate the size of the central header
  1891. $v_size = @ftell($this->zip_fd) - $v_offset;
  1892. // ----- Create the central dir footer
  1893. if (($v_result = $this->privWriteCentralHeader($v_count + $v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1) {
  1894. // ----- Reset the file list
  1895. unset($v_header_list);
  1896. $this->privSwapBackMagicQuotes();
  1897. // ----- Return
  1898. return $v_result;
  1899. }
  1900. // ----- Swap back the file descriptor
  1901. $v_swap = $this->zip_fd;
  1902. $this->zip_fd = $v_zip_temp_fd;
  1903. $v_zip_temp_fd = $v_swap;
  1904. // ----- Close
  1905. $this->privCloseFd();
  1906. // ----- Close the temporary file
  1907. @fclose($v_zip_temp_fd);
  1908. // ----- Magic quotes trick
  1909. $this->privSwapBackMagicQuotes();
  1910. // ----- Delete the zip file
  1911. // TBC : I should test the result ...
  1912. @unlink($this->zipname);
  1913. // ----- Rename the temporary file
  1914. // TBC : I should test the result ...
  1915. //@rename($v_zip_temp_name, $this->zipname);
  1916. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  1917. // ----- Return
  1918. return $v_result;
  1919. }
  1920. // --------------------------------------------------------------------------------
  1921. // --------------------------------------------------------------------------------
  1922. // Function : privOpenFd()
  1923. // Description :
  1924. // Parameters :
  1925. // --------------------------------------------------------------------------------
  1926. public function privOpenFd($p_mode)
  1927. {
  1928. $v_result = 1;
  1929. // ----- Look if already open
  1930. if ($this->zip_fd != 0) {
  1931. // ----- Error log
  1932. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \'' . $this->zipname . '\' already open');
  1933. // ----- Return
  1934. return PclZip::errorCode();
  1935. }
  1936. // ----- Open the zip file
  1937. if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0) {
  1938. // ----- Error log
  1939. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in ' . $p_mode . ' mode');
  1940. // ----- Return
  1941. return PclZip::errorCode();
  1942. }
  1943. // ----- Return
  1944. return $v_result;
  1945. }
  1946. // --------------------------------------------------------------------------------
  1947. // --------------------------------------------------------------------------------
  1948. // Function : privCloseFd()
  1949. // Description :
  1950. // Parameters :
  1951. // --------------------------------------------------------------------------------
  1952. public function privCloseFd()
  1953. {
  1954. $v_result = 1;
  1955. if ($this->zip_fd != 0) {
  1956. @fclose($this->zip_fd);
  1957. }
  1958. $this->zip_fd = 0;
  1959. // ----- Return
  1960. return $v_result;
  1961. }
  1962. // --------------------------------------------------------------------------------
  1963. // --------------------------------------------------------------------------------
  1964. // Function : privAddList()
  1965. // Description :
  1966. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  1967. // different from the real path of the file. This is usefull if you want to have PclTar
  1968. // running in any directory, and memorize relative path from an other directory.
  1969. // Parameters :
  1970. // $p_list : An array containing the file or directory names to add in the tar
  1971. // $p_result_list : list of added files with their properties (specially the status field)
  1972. // $p_add_dir : Path to add in the filename path archived
  1973. // $p_remove_dir : Path to remove in the filename path archived
  1974. // Return Values :
  1975. // --------------------------------------------------------------------------------
  1976. // function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  1977. public function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
  1978. {
  1979. $v_result = 1;
  1980. // ----- Add the files
  1981. $v_header_list = array();
  1982. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
  1983. // ----- Return
  1984. return $v_result;
  1985. }
  1986. // ----- Store the offset of the central dir
  1987. $v_offset = @ftell($this->zip_fd);
  1988. // ----- Create the Central Dir files header
  1989. for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); $i++) {
  1990. // ----- Create the file header
  1991. if ($v_header_list[$i]['status'] == 'ok') {
  1992. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1993. // ----- Return
  1994. return $v_result;
  1995. }
  1996. $v_count++;
  1997. }
  1998. // ----- Transform the header to a 'usable' info
  1999. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  2000. }
  2001. // ----- Zip file comment
  2002. $v_comment = '';
  2003. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  2004. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  2005. }
  2006. // ----- Calculate the size of the central header
  2007. $v_size = @ftell($this->zip_fd) - $v_offset;
  2008. // ----- Create the central dir footer
  2009. if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1) {
  2010. // ----- Reset the file list
  2011. unset($v_header_list);
  2012. // ----- Return
  2013. return $v_result;
  2014. }
  2015. // ----- Return
  2016. return $v_result;
  2017. }
  2018. // --------------------------------------------------------------------------------
  2019. // --------------------------------------------------------------------------------
  2020. // Function : privAddFileList()
  2021. // Description :
  2022. // Parameters :
  2023. // $p_filedescr_list : An array containing the file description
  2024. // or directory names to add in the zip
  2025. // $p_result_list : list of added files with their properties (specially the status field)
  2026. // Return Values :
  2027. // --------------------------------------------------------------------------------
  2028. public function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
  2029. {
  2030. $v_result = 1;
  2031. $v_header = array();
  2032. // ----- Recuperate the current number of elt in list
  2033. $v_nb = sizeof($p_result_list);
  2034. // ----- Loop on the files
  2035. for ($j = 0; ($j < sizeof($p_filedescr_list)) && ($v_result == 1); $j++) {
  2036. // ----- Format the filename
  2037. $p_filedescr_list[$j]['filename'] = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
  2038. // ----- Skip empty file names
  2039. // TBC : Can this be possible ? not checked in DescrParseAtt ?
  2040. if ($p_filedescr_list[$j]['filename'] == "") {
  2041. continue;
  2042. }
  2043. // ----- Check the filename
  2044. if (($p_filedescr_list[$j]['type'] != 'virtual_file') && (!file_exists($p_filedescr_list[$j]['filename']))) {
  2045. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '" . $p_filedescr_list[$j]['filename'] . "' does not exist");
  2046. return PclZip::errorCode();
  2047. }
  2048. // ----- Look if it is a file or a dir with no all path remove option
  2049. // or a dir with all its path removed
  2050. // if ( (is_file($p_filedescr_list[$j]['filename']))
  2051. // || ( is_dir($p_filedescr_list[$j]['filename'])
  2052. 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]))) {
  2053. // ----- Add the file
  2054. $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header, $p_options);
  2055. if ($v_result != 1) {
  2056. return $v_result;
  2057. }
  2058. // ----- Store the file infos
  2059. $p_result_list[$v_nb++] = $v_header;
  2060. }
  2061. }
  2062. // ----- Return
  2063. return $v_result;
  2064. }
  2065. // --------------------------------------------------------------------------------
  2066. // --------------------------------------------------------------------------------
  2067. // Function : privAddFile()
  2068. // Description :
  2069. // Parameters :
  2070. // Return Values :
  2071. // --------------------------------------------------------------------------------
  2072. public function privAddFile($p_filedescr, &$p_header, &$p_options)
  2073. {
  2074. $v_result = 1;
  2075. // ----- Working variable
  2076. $p_filename = $p_filedescr['filename'];
  2077. // TBC : Already done in the fileAtt check ... ?
  2078. if ($p_filename == "") {
  2079. // ----- Error log
  2080. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  2081. // ----- Return
  2082. return PclZip::errorCode();
  2083. }
  2084. // ----- Look for a stored different filename
  2085. /* TBC : Removed
  2086. if (isset($p_filedescr['stored_filename'])) {
  2087. $v_stored_filename = $p_filedescr['stored_filename'];
  2088. } else {
  2089. $v_stored_filename = $p_filedescr['stored_filename'];
  2090. }
  2091. */
  2092. // ----- Set the file properties
  2093. clearstatcache();
  2094. $p_header['version'] = 20;
  2095. $p_header['version_extracted'] = 10;
  2096. $p_header['flag'] = 0;
  2097. $p_header['compression'] = 0;
  2098. $p_header['crc'] = 0;
  2099. $p_header['compressed_size'] = 0;
  2100. $p_header['filename_len'] = strlen($p_filename);
  2101. $p_header['extra_len'] = 0;
  2102. $p_header['disk'] = 0;
  2103. $p_header['internal'] = 0;
  2104. $p_header['offset'] = 0;
  2105. $p_header['filename'] = $p_filename;
  2106. // TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
  2107. $p_header['stored_filename'] = $p_filedescr['stored_filename'];
  2108. $p_header['extra'] = '';
  2109. $p_header['status'] = 'ok';
  2110. $p_header['index'] = -1;
  2111. // ----- Look for regular file
  2112. if ($p_filedescr['type'] == 'file') {
  2113. $p_header['external'] = 0x00000000;
  2114. $p_header['size'] = filesize($p_filename);
  2115. // ----- Look for regular folder
  2116. } elseif ($p_filedescr['type'] == 'folder') {
  2117. $p_header['external'] = 0x00000010;
  2118. $p_header['mtime'] = filemtime($p_filename);
  2119. $p_header['size'] = filesize($p_filename);
  2120. // ----- Look for virtual file
  2121. } elseif ($p_filedescr['type'] == 'virtual_file') {
  2122. $p_header['external'] = 0x00000000;
  2123. $p_header['size'] = strlen($p_filedescr['content']);
  2124. }
  2125. // ----- Look for filetime
  2126. if (isset($p_filedescr['mtime'])) {
  2127. $p_header['mtime'] = $p_filedescr['mtime'];
  2128. } elseif ($p_filedescr['type'] == 'virtual_file') {
  2129. $p_header['mtime'] = time();
  2130. } else {
  2131. $p_header['mtime'] = filemtime($p_filename);
  2132. }
  2133. // ------ Look for file comment
  2134. if (isset($p_filedescr['comment'])) {
  2135. $p_header['comment_len'] = strlen($p_filedescr['comment']);
  2136. $p_header['comment'] = $p_filedescr['comment'];
  2137. } else {
  2138. $p_header['comment_len'] = 0;
  2139. $p_header['comment'] = '';
  2140. }
  2141. // ----- Look for pre-add callback
  2142. if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  2143. // ----- Generate a local information
  2144. $v_local_header = array();
  2145. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2146. // ----- Call the callback
  2147. // Here I do not use call_user_func() because I need to send a reference to the
  2148. // header.
  2149. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
  2150. $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
  2151. if ($v_result == 0) {
  2152. // ----- Change the file status
  2153. $p_header['status'] = "skipped";
  2154. $v_result = 1;
  2155. }
  2156. // ----- Update the informations
  2157. // Only some fields can be modified
  2158. if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  2159. $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  2160. }
  2161. }
  2162. // ----- Look for empty stored filename
  2163. if ($p_header['stored_filename'] == "") {
  2164. $p_header['status'] = "filtered";
  2165. }
  2166. // ----- Check the path length
  2167. if (strlen($p_header['stored_filename']) > 0xFF) {
  2168. $p_header['status'] = 'filename_too_long';
  2169. }
  2170. // ----- Look if no error, or file not skipped
  2171. if ($p_header['status'] == 'ok') {
  2172. // ----- Look for a file
  2173. if ($p_filedescr['type'] == 'file') {
  2174. // ----- Look for using temporary file to zip
  2175. 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'])))) {
  2176. $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
  2177. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  2178. return $v_result;
  2179. }
  2180. // ----- Use "in memory" zip algo
  2181. } else {
  2182. // ----- Open the source file
  2183. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2184. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2185. return PclZip::errorCode();
  2186. }
  2187. // ----- Read the file content
  2188. $v_content = @fread($v_file, $p_header['size']);
  2189. // ----- Close the file
  2190. @fclose($v_file);
  2191. // ----- Calculate the CRC
  2192. $p_header['crc'] = @crc32($v_content);
  2193. // ----- Look for no compression
  2194. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2195. // ----- Set header parameters
  2196. $p_header['compressed_size'] = $p_header['size'];
  2197. $p_header['compression'] = 0;
  2198. // ----- Look for normal compression
  2199. } else {
  2200. // ----- Compress the content
  2201. $v_content = @gzdeflate($v_content);
  2202. // ----- Set header parameters
  2203. $p_header['compressed_size'] = strlen($v_content);
  2204. $p_header['compression'] = 8;
  2205. }
  2206. // ----- Call the header generation
  2207. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2208. @fclose($v_file);
  2209. return $v_result;
  2210. }
  2211. // ----- Write the compressed (or not) content
  2212. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2213. }
  2214. // ----- Look for a virtual file (a file from string)
  2215. } elseif ($p_filedescr['type'] == 'virtual_file') {
  2216. $v_content = $p_filedescr['content'];
  2217. // ----- Calculate the CRC
  2218. $p_header['crc'] = @crc32($v_content);
  2219. // ----- Look for no compression
  2220. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2221. // ----- Set header parameters
  2222. $p_header['compressed_size'] = $p_header['size'];
  2223. $p_header['compression'] = 0;
  2224. // ----- Look for normal compression
  2225. } else {
  2226. // ----- Compress the content
  2227. $v_content = @gzdeflate($v_content);
  2228. // ----- Set header parameters
  2229. $p_header['compressed_size'] = strlen($v_content);
  2230. $p_header['compression'] = 8;
  2231. }
  2232. // ----- Call the header generation
  2233. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2234. @fclose($v_file);
  2235. return $v_result;
  2236. }
  2237. // ----- Write the compressed (or not) content
  2238. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2239. // ----- Look for a directory
  2240. } elseif ($p_filedescr['type'] == 'folder') {
  2241. // ----- Look for directory last '/'
  2242. if (@substr($p_header['stored_filename'], -1) != '/') {
  2243. $p_header['stored_filename'] .= '/';
  2244. }
  2245. // ----- Set the file properties
  2246. $p_header['size'] = 0;
  2247. //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
  2248. $p_header['external'] = 0x00000010; // Value for a folder : to be checked
  2249. // ----- Call the header generation
  2250. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2251. return $v_result;
  2252. }
  2253. }
  2254. }
  2255. // ----- Look for post-add callback
  2256. if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  2257. // ----- Generate a local information
  2258. $v_local_header = array();
  2259. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2260. // ----- Call the callback
  2261. // Here I do not use call_user_func() because I need to send a reference to the
  2262. // header.
  2263. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
  2264. $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
  2265. if ($v_result == 0) {
  2266. // ----- Ignored
  2267. $v_result = 1;
  2268. }
  2269. // ----- Update the informations
  2270. // Nothing can be modified
  2271. }
  2272. // ----- Return
  2273. return $v_result;
  2274. }
  2275. // --------------------------------------------------------------------------------
  2276. // --------------------------------------------------------------------------------
  2277. // Function : privAddFileUsingTempFile()
  2278. // Description :
  2279. // Parameters :
  2280. // Return Values :
  2281. // --------------------------------------------------------------------------------
  2282. public function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
  2283. {
  2284. $v_result = PCLZIP_ERR_NO_ERROR;
  2285. // ----- Working variable
  2286. $p_filename = $p_filedescr['filename'];
  2287. // ----- Open the source file
  2288. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2289. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2290. return PclZip::errorCode();
  2291. }
  2292. // ----- Creates a compressed temporary file
  2293. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
  2294. if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
  2295. fclose($v_file);
  2296. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
  2297. return PclZip::errorCode();
  2298. }
  2299. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  2300. $v_size = filesize($p_filename);
  2301. while ($v_size != 0) {
  2302. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2303. $v_buffer = @fread($v_file, $v_read_size);
  2304. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2305. @gzputs($v_file_compressed, $v_buffer, $v_read_size);
  2306. $v_size -= $v_read_size;
  2307. }
  2308. // ----- Close the file
  2309. @fclose($v_file);
  2310. @gzclose($v_file_compressed);
  2311. // ----- Check the minimum file size
  2312. if (filesize($v_gzip_temp_name) < 18) {
  2313. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \'' . $v_gzip_temp_name . '\' has invalid filesize - should be minimum 18 bytes');
  2314. return PclZip::errorCode();
  2315. }
  2316. // ----- Extract the compressed attributes
  2317. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  2318. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  2319. return PclZip::errorCode();
  2320. }
  2321. // ----- Read the gzip file header
  2322. $v_binary_data = @fread($v_file_compressed, 10);
  2323. $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
  2324. // ----- Check some parameters
  2325. $v_data_header['os'] = bin2hex($v_data_header['os']);
  2326. // ----- Read the gzip file footer
  2327. @fseek($v_file_compressed, filesize($v_gzip_temp_name) - 8);
  2328. $v_binary_data = @fread($v_file_compressed, 8);
  2329. $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
  2330. // ----- Set the attributes
  2331. $p_header['compression'] = ord($v_data_header['cm']);
  2332. //$p_header['mtime'] = $v_data_header['mtime'];
  2333. $p_header['crc'] = $v_data_footer['crc'];
  2334. $p_header['compressed_size'] = filesize($v_gzip_temp_name) - 18;
  2335. // ----- Close the file
  2336. @fclose($v_file_compressed);
  2337. // ----- Call the header generation
  2338. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2339. return $v_result;
  2340. }
  2341. // ----- Add the compressed data
  2342. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  2343. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  2344. return PclZip::errorCode();
  2345. }
  2346. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  2347. fseek($v_file_compressed, 10);
  2348. $v_size = $p_header['compressed_size'];
  2349. while ($v_size != 0) {
  2350. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2351. $v_buffer = @fread($v_file_compressed, $v_read_size);
  2352. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2353. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2354. $v_size -= $v_read_size;
  2355. }
  2356. // ----- Close the file
  2357. @fclose($v_file_compressed);
  2358. // ----- Unlink the temporary file
  2359. @unlink($v_gzip_temp_name);
  2360. // ----- Return
  2361. return $v_result;
  2362. }
  2363. // --------------------------------------------------------------------------------
  2364. // --------------------------------------------------------------------------------
  2365. // Function : privCalculateStoredFilename()
  2366. // Description :
  2367. // Based on file descriptor properties and global options, this method
  2368. // calculate the filename that will be stored in the archive.
  2369. // Parameters :
  2370. // Return Values :
  2371. // --------------------------------------------------------------------------------
  2372. public function privCalculateStoredFilename(&$p_filedescr, &$p_options)
  2373. {
  2374. $v_result = 1;
  2375. // ----- Working variables
  2376. $p_filename = $p_filedescr['filename'];
  2377. if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
  2378. $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
  2379. } else {
  2380. $p_add_dir = '';
  2381. }
  2382. if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
  2383. $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
  2384. } else {
  2385. $p_remove_dir = '';
  2386. }
  2387. if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  2388. $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  2389. } else {
  2390. $p_remove_all_dir = 0;
  2391. }
  2392. // ----- Look for full name change
  2393. if (isset($p_filedescr['new_full_name'])) {
  2394. // ----- Remove drive letter if any
  2395. $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
  2396. // ----- Look for path and/or short name change
  2397. } else {
  2398. // ----- Look for short name change
  2399. // Its when we cahnge just the filename but not the path
  2400. if (isset($p_filedescr['new_short_name'])) {
  2401. $v_path_info = pathinfo($p_filename);
  2402. $v_dir = '';
  2403. if ($v_path_info['dirname'] != '') {
  2404. $v_dir = $v_path_info['dirname'] . '/';
  2405. }
  2406. $v_stored_filename = $v_dir . $p_filedescr['new_short_name'];
  2407. } else {
  2408. // ----- Calculate the stored filename
  2409. $v_stored_filename = $p_filename;
  2410. }
  2411. // ----- Look for all path to remove
  2412. if ($p_remove_all_dir) {
  2413. $v_stored_filename = basename($p_filename);
  2414. // ----- Look for partial path remove
  2415. } elseif ($p_remove_dir != "") {
  2416. if (substr($p_remove_dir, -1) != '/') {
  2417. $p_remove_dir .= "/";
  2418. }
  2419. if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./")) {
  2420. if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./")) {
  2421. $p_remove_dir = "./" . $p_remove_dir;
  2422. }
  2423. if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./")) {
  2424. $p_remove_dir = substr($p_remove_dir, 2);
  2425. }
  2426. }
  2427. $v_compare = PclZipUtilPathInclusion($p_remove_dir, $v_stored_filename);
  2428. if ($v_compare > 0) {
  2429. if ($v_compare == 2) {
  2430. $v_stored_filename = "";
  2431. } else {
  2432. $v_stored_filename = substr($v_stored_filename, strlen($p_remove_dir));
  2433. }
  2434. }
  2435. }
  2436. // ----- Remove drive letter if any
  2437. $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
  2438. // ----- Look for path to add
  2439. if ($p_add_dir != "") {
  2440. if (substr($p_add_dir, -1) == "/") {
  2441. $v_stored_filename = $p_add_dir . $v_stored_filename;
  2442. } else {
  2443. $v_stored_filename = $p_add_dir . "/" . $v_stored_filename;
  2444. }
  2445. }
  2446. }
  2447. // ----- Filename (reduce the path of stored name)
  2448. $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  2449. $p_filedescr['stored_filename'] = $v_stored_filename;
  2450. // ----- Return
  2451. return $v_result;
  2452. }
  2453. // --------------------------------------------------------------------------------
  2454. // --------------------------------------------------------------------------------
  2455. // Function : privWriteFileHeader()
  2456. // Description :
  2457. // Parameters :
  2458. // Return Values :
  2459. // --------------------------------------------------------------------------------
  2460. public function privWriteFileHeader(&$p_header)
  2461. {
  2462. $v_result = 1;
  2463. // ----- Store the offset position of the file
  2464. $p_header['offset'] = ftell($this->zip_fd);
  2465. // ----- Transform UNIX mtime to DOS format mdate/mtime
  2466. $v_date = getdate($p_header['mtime']);
  2467. $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
  2468. $v_mdate = (($v_date['year'] - 1980) << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
  2469. // ----- Packed data
  2470. $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']);
  2471. // ----- Write the first 148 bytes of the header in the archive
  2472. fputs($this->zip_fd, $v_binary_data, 30);
  2473. // ----- Write the variable fields
  2474. if (strlen($p_header['stored_filename']) != 0) {
  2475. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  2476. }
  2477. if ($p_header['extra_len'] != 0) {
  2478. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  2479. }
  2480. // ----- Return
  2481. return $v_result;
  2482. }
  2483. // --------------------------------------------------------------------------------
  2484. // --------------------------------------------------------------------------------
  2485. // Function : privWriteCentralFileHeader()
  2486. // Description :
  2487. // Parameters :
  2488. // Return Values :
  2489. // --------------------------------------------------------------------------------
  2490. public function privWriteCentralFileHeader(&$p_header)
  2491. {
  2492. $v_result = 1;
  2493. // TBC
  2494. //for (reset($p_header); $key = key($p_header); next($p_header)) {
  2495. //}
  2496. // ----- Transform UNIX mtime to DOS format mdate/mtime
  2497. $v_date = getdate($p_header['mtime']);
  2498. $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
  2499. $v_mdate = (($v_date['year'] - 1980) << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
  2500. // ----- Packed data
  2501. $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']);
  2502. // ----- Write the 42 bytes of the header in the zip file
  2503. fputs($this->zip_fd, $v_binary_data, 46);
  2504. // ----- Write the variable fields
  2505. if (strlen($p_header['stored_filename']) != 0) {
  2506. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  2507. }
  2508. if ($p_header['extra_len'] != 0) {
  2509. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  2510. }
  2511. if ($p_header['comment_len'] != 0) {
  2512. fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  2513. }
  2514. // ----- Return
  2515. return $v_result;
  2516. }
  2517. // --------------------------------------------------------------------------------
  2518. // --------------------------------------------------------------------------------
  2519. // Function : privWriteCentralHeader()
  2520. // Description :
  2521. // Parameters :
  2522. // Return Values :
  2523. // --------------------------------------------------------------------------------
  2524. public function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  2525. {
  2526. $v_result = 1;
  2527. // ----- Packed data
  2528. $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries, $p_nb_entries, $p_size, $p_offset, strlen($p_comment));
  2529. // ----- Write the 22 bytes of the header in the zip file
  2530. fputs($this->zip_fd, $v_binary_data, 22);
  2531. // ----- Write the variable fields
  2532. if (strlen($p_comment) != 0) {
  2533. fputs($this->zip_fd, $p_comment, strlen($p_comment));
  2534. }
  2535. // ----- Return
  2536. return $v_result;
  2537. }
  2538. // --------------------------------------------------------------------------------
  2539. // --------------------------------------------------------------------------------
  2540. // Function : privList()
  2541. // Description :
  2542. // Parameters :
  2543. // Return Values :
  2544. // --------------------------------------------------------------------------------
  2545. public function privList(&$p_list)
  2546. {
  2547. $v_result = 1;
  2548. // ----- Magic quotes trick
  2549. $this->privDisableMagicQuotes();
  2550. // ----- Open the zip file
  2551. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
  2552. // ----- Magic quotes trick
  2553. $this->privSwapBackMagicQuotes();
  2554. // ----- Error log
  2555. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in binary read mode');
  2556. // ----- Return
  2557. return PclZip::errorCode();
  2558. }
  2559. // ----- Read the central directory informations
  2560. $v_central_dir = array();
  2561. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  2562. $this->privSwapBackMagicQuotes();
  2563. return $v_result;
  2564. }
  2565. // ----- Go to beginning of Central Dir
  2566. @rewind($this->zip_fd);
  2567. if (@fseek($this->zip_fd, $v_central_dir['offset'])) {
  2568. $this->privSwapBackMagicQuotes();
  2569. // ----- Error log
  2570. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2571. // ----- Return
  2572. return PclZip::errorCode();
  2573. }
  2574. // ----- Read each entry
  2575. for ($i = 0; $i < $v_central_dir['entries']; $i++) {
  2576. // ----- Read the file header
  2577. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
  2578. $this->privSwapBackMagicQuotes();
  2579. return $v_result;
  2580. }
  2581. $v_header['index'] = $i;
  2582. // ----- Get the only interesting attributes
  2583. $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
  2584. unset($v_header);
  2585. }
  2586. // ----- Close the zip file
  2587. $this->privCloseFd();
  2588. // ----- Magic quotes trick
  2589. $this->privSwapBackMagicQuotes();
  2590. // ----- Return
  2591. return $v_result;
  2592. }
  2593. // --------------------------------------------------------------------------------
  2594. // --------------------------------------------------------------------------------
  2595. // Function : privConvertHeader2FileInfo()
  2596. // Description :
  2597. // This function takes the file informations from the central directory
  2598. // entries and extract the interesting parameters that will be given back.
  2599. // The resulting file infos are set in the array $p_info
  2600. // $p_info['filename'] : Filename with full path. Given by user (add),
  2601. // extracted in the filesystem (extract).
  2602. // $p_info['stored_filename'] : Stored filename in the archive.
  2603. // $p_info['size'] = Size of the file.
  2604. // $p_info['compressed_size'] = Compressed size of the file.
  2605. // $p_info['mtime'] = Last modification date of the file.
  2606. // $p_info['comment'] = Comment associated with the file.
  2607. // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
  2608. // $p_info['status'] = status of the action on the file.
  2609. // $p_info['crc'] = CRC of the file content.
  2610. // Parameters :
  2611. // Return Values :
  2612. // --------------------------------------------------------------------------------
  2613. public function privConvertHeader2FileInfo($p_header, &$p_info)
  2614. {
  2615. $v_result = 1;
  2616. // ----- Get the interesting attributes
  2617. $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
  2618. $p_info['filename'] = $v_temp_path;
  2619. $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
  2620. $p_info['stored_filename'] = $v_temp_path;
  2621. $p_info['size'] = $p_header['size'];
  2622. $p_info['compressed_size'] = $p_header['compressed_size'];
  2623. $p_info['mtime'] = $p_header['mtime'];
  2624. $p_info['comment'] = $p_header['comment'];
  2625. $p_info['folder'] = (($p_header['external'] & 0x00000010) == 0x00000010);
  2626. $p_info['index'] = $p_header['index'];
  2627. $p_info['status'] = $p_header['status'];
  2628. $p_info['crc'] = $p_header['crc'];
  2629. // ----- Return
  2630. return $v_result;
  2631. }
  2632. // --------------------------------------------------------------------------------
  2633. // --------------------------------------------------------------------------------
  2634. // Function : privExtractByRule()
  2635. // Description :
  2636. // Extract a file or directory depending of rules (by index, by name, ...)
  2637. // Parameters :
  2638. // $p_file_list : An array where will be placed the properties of each
  2639. // extracted file
  2640. // $p_path : Path to add while writing the extracted files
  2641. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  2642. // extracted files. If the path does not match the file path,
  2643. // the file is extracted with its memorized path.
  2644. // $p_remove_path does not apply to 'list' mode.
  2645. // $p_path and $p_remove_path are commulative.
  2646. // Return Values :
  2647. // 1 on success,0 or less on error (see error code list)
  2648. // --------------------------------------------------------------------------------
  2649. public function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  2650. {
  2651. $v_result = 1;
  2652. // ----- Magic quotes trick
  2653. $this->privDisableMagicQuotes();
  2654. // ----- Check the path
  2655. if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path, 1, 2) != ":/"))) {
  2656. $p_path = "./" . $p_path;
  2657. }
  2658. // ----- Reduce the path last (and duplicated) '/'
  2659. if (($p_path != "./") && ($p_path != "/")) {
  2660. // ----- Look for the path end '/'
  2661. while (substr($p_path, -1) == "/") {
  2662. $p_path = substr($p_path, 0, strlen($p_path) - 1);
  2663. }
  2664. }
  2665. // ----- Look for path to remove format (should end by /)
  2666. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/')) {
  2667. $p_remove_path .= '/';
  2668. }
  2669. $p_remove_path_size = strlen($p_remove_path);
  2670. // ----- Open the zip file
  2671. if (($v_result = $this->privOpenFd('rb')) != 1) {
  2672. $this->privSwapBackMagicQuotes();
  2673. return $v_result;
  2674. }
  2675. // ----- Read the central directory informations
  2676. $v_central_dir = array();
  2677. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  2678. // ----- Close the zip file
  2679. $this->privCloseFd();
  2680. $this->privSwapBackMagicQuotes();
  2681. return $v_result;
  2682. }
  2683. // ----- Start at beginning of Central Dir
  2684. $v_pos_entry = $v_central_dir['offset'];
  2685. // ----- Read each entry
  2686. $j_start = 0;
  2687. for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
  2688. // ----- Read next Central dir entry
  2689. @rewind($this->zip_fd);
  2690. if (@fseek($this->zip_fd, $v_pos_entry)) {
  2691. // ----- Close the zip file
  2692. $this->privCloseFd();
  2693. $this->privSwapBackMagicQuotes();
  2694. // ----- Error log
  2695. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2696. // ----- Return
  2697. return PclZip::errorCode();
  2698. }
  2699. // ----- Read the file header
  2700. $v_header = array();
  2701. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
  2702. // ----- Close the zip file
  2703. $this->privCloseFd();
  2704. $this->privSwapBackMagicQuotes();
  2705. return $v_result;
  2706. }
  2707. // ----- Store the index
  2708. $v_header['index'] = $i;
  2709. // ----- Store the file position
  2710. $v_pos_entry = ftell($this->zip_fd);
  2711. // ----- Look for the specific extract rules
  2712. $v_extract = false;
  2713. // ----- Look for extract by name rule
  2714. if ((isset($p_options[PCLZIP_OPT_BY_NAME])) && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  2715. // ----- Look if the filename is in the list
  2716. for ($j = 0; ($j < sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  2717. // ----- Look for a directory
  2718. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  2719. // ----- Look if the directory is in the filename path
  2720. 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])) {
  2721. $v_extract = true;
  2722. }
  2723. // ----- Look for a filename
  2724. } elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  2725. $v_extract = true;
  2726. }
  2727. }
  2728. // ----- Look for extract by ereg rule
  2729. // ereg() is deprecated with PHP 5.3
  2730. /*
  2731. elseif ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
  2732. && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  2733. if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
  2734. $v_extract = true;
  2735. }
  2736. }
  2737. */
  2738. // ----- Look for extract by preg rule
  2739. } elseif ((isset($p_options[PCLZIP_OPT_BY_PREG])) && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  2740. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  2741. $v_extract = true;
  2742. }
  2743. // ----- Look for extract by index rule
  2744. } elseif ((isset($p_options[PCLZIP_OPT_BY_INDEX])) && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  2745. // ----- Look if the index is in the list
  2746. for ($j = $j_start; ($j < sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  2747. if (($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  2748. $v_extract = true;
  2749. }
  2750. if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  2751. $j_start = $j + 1;
  2752. }
  2753. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
  2754. break;
  2755. }
  2756. }
  2757. // ----- Look for no rule, which means extract all the archive
  2758. } else {
  2759. $v_extract = true;
  2760. }
  2761. // ----- Check compression method
  2762. if (($v_extract) && (($v_header['compression'] != 8) && ($v_header['compression'] != 0))) {
  2763. $v_header['status'] = 'unsupported_compression';
  2764. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2765. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2766. $this->privSwapBackMagicQuotes();
  2767. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION, "Filename '" . $v_header['stored_filename'] . "' is " . "compressed by an unsupported compression " . "method (" . $v_header['compression'] . ") ");
  2768. return PclZip::errorCode();
  2769. }
  2770. }
  2771. // ----- Check encrypted files
  2772. if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  2773. $v_header['status'] = 'unsupported_encryption';
  2774. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2775. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2776. $this->privSwapBackMagicQuotes();
  2777. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, "Unsupported encryption for " . " filename '" . $v_header['stored_filename'] . "'");
  2778. return PclZip::errorCode();
  2779. }
  2780. }
  2781. // ----- Look for real extraction
  2782. if (($v_extract) && ($v_header['status'] != 'ok')) {
  2783. $v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++]);
  2784. if ($v_result != 1) {
  2785. $this->privCloseFd();
  2786. $this->privSwapBackMagicQuotes();
  2787. return $v_result;
  2788. }
  2789. $v_extract = false;
  2790. }
  2791. // ----- Look for real extraction
  2792. if ($v_extract) {
  2793. // ----- Go to the file position
  2794. @rewind($this->zip_fd);
  2795. if (@fseek($this->zip_fd, $v_header['offset'])) {
  2796. // ----- Close the zip file
  2797. $this->privCloseFd();
  2798. $this->privSwapBackMagicQuotes();
  2799. // ----- Error log
  2800. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2801. // ----- Return
  2802. return PclZip::errorCode();
  2803. }
  2804. // ----- Look for extraction as string
  2805. if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  2806. $v_string = '';
  2807. // ----- Extracting the file
  2808. $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
  2809. if ($v_result1 < 1) {
  2810. $this->privCloseFd();
  2811. $this->privSwapBackMagicQuotes();
  2812. return $v_result1;
  2813. }
  2814. // ----- Get the only interesting attributes
  2815. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1) {
  2816. // ----- Close the zip file
  2817. $this->privCloseFd();
  2818. $this->privSwapBackMagicQuotes();
  2819. return $v_result;
  2820. }
  2821. // ----- Set the file content
  2822. $p_file_list[$v_nb_extracted]['content'] = $v_string;
  2823. // ----- Next extracted file
  2824. $v_nb_extracted++;
  2825. // ----- Look for user callback abort
  2826. if ($v_result1 == 2) {
  2827. break;
  2828. }
  2829. // ----- Look for extraction in standard output
  2830. } elseif ((isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
  2831. // ----- Extracting the file in standard output
  2832. $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  2833. if ($v_result1 < 1) {
  2834. $this->privCloseFd();
  2835. $this->privSwapBackMagicQuotes();
  2836. return $v_result1;
  2837. }
  2838. // ----- Get the only interesting attributes
  2839. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  2840. $this->privCloseFd();
  2841. $this->privSwapBackMagicQuotes();
  2842. return $v_result;
  2843. }
  2844. // ----- Look for user callback abort
  2845. if ($v_result1 == 2) {
  2846. break;
  2847. }
  2848. // ----- Look for normal extraction
  2849. } else {
  2850. // ----- Extracting the file
  2851. $v_result1 = $this->privExtractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_options);
  2852. if ($v_result1 < 1) {
  2853. $this->privCloseFd();
  2854. $this->privSwapBackMagicQuotes();
  2855. return $v_result1;
  2856. }
  2857. // ----- Get the only interesting attributes
  2858. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  2859. // ----- Close the zip file
  2860. $this->privCloseFd();
  2861. $this->privSwapBackMagicQuotes();
  2862. return $v_result;
  2863. }
  2864. // ----- Look for user callback abort
  2865. if ($v_result1 == 2) {
  2866. break;
  2867. }
  2868. }
  2869. }
  2870. }
  2871. // ----- Close the zip file
  2872. $this->privCloseFd();
  2873. $this->privSwapBackMagicQuotes();
  2874. // ----- Return
  2875. return $v_result;
  2876. }
  2877. // --------------------------------------------------------------------------------
  2878. // --------------------------------------------------------------------------------
  2879. // Function : privExtractFile()
  2880. // Description :
  2881. // Parameters :
  2882. // Return Values :
  2883. //
  2884. // 1 : ... ?
  2885. // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
  2886. // --------------------------------------------------------------------------------
  2887. public function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  2888. {
  2889. $v_result = 1;
  2890. // ----- Read the file header
  2891. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  2892. // ----- Return
  2893. return $v_result;
  2894. }
  2895. // ----- Check that the file header is coherent with $p_entry info
  2896. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  2897. // TBC
  2898. }
  2899. // ----- Look for all path to remove
  2900. if ($p_remove_all_path == true) {
  2901. // ----- Look for folder entry that not need to be extracted
  2902. if (($p_entry['external'] & 0x00000010) == 0x00000010) {
  2903. $p_entry['status'] = "filtered";
  2904. return $v_result;
  2905. }
  2906. // ----- Get the basename of the path
  2907. $p_entry['filename'] = basename($p_entry['filename']);
  2908. // ----- Look for path to remove
  2909. } elseif ($p_remove_path != "") {
  2910. if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2) {
  2911. // ----- Change the file status
  2912. $p_entry['status'] = "filtered";
  2913. // ----- Return
  2914. return $v_result;
  2915. }
  2916. $p_remove_path_size = strlen($p_remove_path);
  2917. if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
  2918. // ----- Remove the path
  2919. $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  2920. }
  2921. }
  2922. // ----- Add the path
  2923. if ($p_path != '') {
  2924. $p_entry['filename'] = $p_path . "/" . $p_entry['filename'];
  2925. }
  2926. // ----- Check a base_dir_restriction
  2927. if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
  2928. $v_inclusion = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION], $p_entry['filename']);
  2929. if ($v_inclusion == 0) {
  2930. PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION, "Filename '" . $p_entry['filename'] . "' is " . "outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
  2931. return PclZip::errorCode();
  2932. }
  2933. }
  2934. // ----- Look for pre-extract callback
  2935. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  2936. // ----- Generate a local information
  2937. $v_local_header = array();
  2938. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2939. // ----- Call the callback
  2940. // Here I do not use call_user_func() because I need to send a reference to the
  2941. // header.
  2942. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  2943. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  2944. if ($v_result == 0) {
  2945. // ----- Change the file status
  2946. $p_entry['status'] = "skipped";
  2947. $v_result = 1;
  2948. }
  2949. // ----- Look for abort result
  2950. if ($v_result == 2) {
  2951. // ----- This status is internal and will be changed in 'skipped'
  2952. $p_entry['status'] = "aborted";
  2953. $v_result = PCLZIP_ERR_USER_ABORTED;
  2954. }
  2955. // ----- Update the informations
  2956. // Only some fields can be modified
  2957. $p_entry['filename'] = $v_local_header['filename'];
  2958. }
  2959. // ----- Look if extraction should be done
  2960. if ($p_entry['status'] == 'ok') {
  2961. // ----- Look for specific actions while the file exist
  2962. if (file_exists($p_entry['filename'])) {
  2963. // ----- Look if file is a directory
  2964. if (is_dir($p_entry['filename'])) {
  2965. // ----- Change the file status
  2966. $p_entry['status'] = "already_a_directory";
  2967. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2968. // For historical reason first PclZip implementation does not stop
  2969. // when this kind of error occurs.
  2970. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2971. PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY, "Filename '" . $p_entry['filename'] . "' is " . "already used by an existing directory");
  2972. return PclZip::errorCode();
  2973. }
  2974. // ----- Look if file is write protected
  2975. } elseif (!is_writeable($p_entry['filename'])) {
  2976. // ----- Change the file status
  2977. $p_entry['status'] = "write_protected";
  2978. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2979. // For historical reason first PclZip implementation does not stop
  2980. // when this kind of error occurs.
  2981. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2982. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, "Filename '" . $p_entry['filename'] . "' exists " . "and is write protected");
  2983. return PclZip::errorCode();
  2984. }
  2985. // ----- Look if the extracted file is older
  2986. } elseif (filemtime($p_entry['filename']) > $p_entry['mtime']) {
  2987. // ----- Change the file status
  2988. if ((isset($p_options[PCLZIP_OPT_REPLACE_NEWER])) && ($p_options[PCLZIP_OPT_REPLACE_NEWER] === true)) {
  2989. } else {
  2990. $p_entry['status'] = "newer_exist";
  2991. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2992. // For historical reason first PclZip implementation does not stop
  2993. // when this kind of error occurs.
  2994. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR])) && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)) {
  2995. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, "Newer version of '" . $p_entry['filename'] . "' exists " . "and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  2996. return PclZip::errorCode();
  2997. }
  2998. }
  2999. } else {
  3000. }
  3001. // ----- Check the directory availability and create it if necessary
  3002. } else {
  3003. if ((($p_entry['external'] & 0x00000010) == 0x00000010) || (substr($p_entry['filename'], -1) == '/')) {
  3004. $v_dir_to_check = $p_entry['filename'];
  3005. } elseif (!strstr($p_entry['filename'], "/")) {
  3006. $v_dir_to_check = "";
  3007. } else {
  3008. $v_dir_to_check = dirname($p_entry['filename']);
  3009. }
  3010. if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external'] & 0x00000010) == 0x00000010))) != 1) {
  3011. // ----- Change the file status
  3012. $p_entry['status'] = "path_creation_fail";
  3013. // ----- Return
  3014. //return $v_result;
  3015. $v_result = 1;
  3016. }
  3017. }
  3018. }
  3019. // ----- Look if extraction should be done
  3020. if ($p_entry['status'] == 'ok') {
  3021. // ----- Do the extraction (if not a folder)
  3022. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  3023. // ----- Look for not compressed file
  3024. if ($p_entry['compression'] == 0) {
  3025. // ----- Opening destination file
  3026. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3027. // ----- Change the file status
  3028. $p_entry['status'] = "write_error";
  3029. // ----- Return
  3030. return $v_result;
  3031. }
  3032. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3033. $v_size = $p_entry['compressed_size'];
  3034. while ($v_size != 0) {
  3035. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3036. $v_buffer = @fread($this->zip_fd, $v_read_size);
  3037. /* Try to speed up the code
  3038. $v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3039. @fwrite($v_dest_file, $v_binary_data, $v_read_size);
  3040. */
  3041. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3042. $v_size -= $v_read_size;
  3043. }
  3044. // ----- Closing the destination file
  3045. fclose($v_dest_file);
  3046. // ----- Change the file mtime
  3047. touch($p_entry['filename'], $p_entry['mtime']);
  3048. } else {
  3049. // ----- TBC
  3050. // Need to be finished
  3051. if (($p_entry['flag'] & 1) == 1) {
  3052. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \'' . $p_entry['filename'] . '\' is encrypted. Encrypted files are not supported.');
  3053. return PclZip::errorCode();
  3054. }
  3055. // ----- Look for using temporary file to unzip
  3056. 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'])))) {
  3057. $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
  3058. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  3059. return $v_result;
  3060. }
  3061. // ----- Look for extract in memory
  3062. } else {
  3063. // ----- Read the compressed file in a buffer (one shot)
  3064. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3065. // ----- Decompress the file
  3066. $v_file_content = @gzinflate($v_buffer);
  3067. unset($v_buffer);
  3068. if ($v_file_content === false) {
  3069. // ----- Change the file status
  3070. // TBC
  3071. $p_entry['status'] = "error";
  3072. return $v_result;
  3073. }
  3074. // ----- Opening destination file
  3075. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3076. // ----- Change the file status
  3077. $p_entry['status'] = "write_error";
  3078. return $v_result;
  3079. }
  3080. // ----- Write the uncompressed data
  3081. @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  3082. unset($v_file_content);
  3083. // ----- Closing the destination file
  3084. @fclose($v_dest_file);
  3085. }
  3086. // ----- Change the file mtime
  3087. @touch($p_entry['filename'], $p_entry['mtime']);
  3088. }
  3089. // ----- Look for chmod option
  3090. if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  3091. // ----- Change the mode of the file
  3092. @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  3093. }
  3094. }
  3095. }
  3096. // ----- Change abort status
  3097. if ($p_entry['status'] == "aborted") {
  3098. $p_entry['status'] = "skipped";
  3099. // ----- Look for post-extract callback
  3100. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3101. // ----- Generate a local information
  3102. $v_local_header = array();
  3103. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3104. // ----- Call the callback
  3105. // Here I do not use call_user_func() because I need to send a reference to the
  3106. // header.
  3107. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3108. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3109. // ----- Look for abort result
  3110. if ($v_result == 2) {
  3111. $v_result = PCLZIP_ERR_USER_ABORTED;
  3112. }
  3113. }
  3114. // ----- Return
  3115. return $v_result;
  3116. }
  3117. // --------------------------------------------------------------------------------
  3118. // --------------------------------------------------------------------------------
  3119. // Function : privExtractFileUsingTempFile()
  3120. // Description :
  3121. // Parameters :
  3122. // Return Values :
  3123. // --------------------------------------------------------------------------------
  3124. public function privExtractFileUsingTempFile(&$p_entry, &$p_options)
  3125. {
  3126. $v_result = 1;
  3127. // ----- Creates a temporary file
  3128. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
  3129. if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
  3130. fclose($v_file);
  3131. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
  3132. return PclZip::errorCode();
  3133. }
  3134. // ----- Write gz file format header
  3135. $v_binary_data = pack('va1a1Va1a1', 0x8b1f, chr($p_entry['compression']), chr(0x00), time(), chr(0x00), chr(3));
  3136. @fwrite($v_dest_file, $v_binary_data, 10);
  3137. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3138. $v_size = $p_entry['compressed_size'];
  3139. while ($v_size != 0) {
  3140. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3141. $v_buffer = @fread($this->zip_fd, $v_read_size);
  3142. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3143. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3144. $v_size -= $v_read_size;
  3145. }
  3146. // ----- Write gz file format footer
  3147. $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
  3148. @fwrite($v_dest_file, $v_binary_data, 8);
  3149. // ----- Close the temporary file
  3150. @fclose($v_dest_file);
  3151. // ----- Opening destination file
  3152. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3153. $p_entry['status'] = "write_error";
  3154. return $v_result;
  3155. }
  3156. // ----- Open the temporary gz file
  3157. if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
  3158. @fclose($v_dest_file);
  3159. $p_entry['status'] = "read_error";
  3160. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  3161. return PclZip::errorCode();
  3162. }
  3163. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3164. $v_size = $p_entry['size'];
  3165. while ($v_size != 0) {
  3166. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3167. $v_buffer = @gzread($v_src_file, $v_read_size);
  3168. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3169. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3170. $v_size -= $v_read_size;
  3171. }
  3172. @fclose($v_dest_file);
  3173. @gzclose($v_src_file);
  3174. // ----- Delete the temporary file
  3175. @unlink($v_gzip_temp_name);
  3176. // ----- Return
  3177. return $v_result;
  3178. }
  3179. // --------------------------------------------------------------------------------
  3180. // --------------------------------------------------------------------------------
  3181. // Function : privExtractFileInOutput()
  3182. // Description :
  3183. // Parameters :
  3184. // Return Values :
  3185. // --------------------------------------------------------------------------------
  3186. public function privExtractFileInOutput(&$p_entry, &$p_options)
  3187. {
  3188. $v_result = 1;
  3189. // ----- Read the file header
  3190. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  3191. return $v_result;
  3192. }
  3193. // ----- Check that the file header is coherent with $p_entry info
  3194. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3195. // TBC
  3196. }
  3197. // ----- Look for pre-extract callback
  3198. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  3199. // ----- Generate a local information
  3200. $v_local_header = array();
  3201. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3202. // ----- Call the callback
  3203. // Here I do not use call_user_func() because I need to send a reference to the
  3204. // header.
  3205. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  3206. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  3207. if ($v_result == 0) {
  3208. // ----- Change the file status
  3209. $p_entry['status'] = "skipped";
  3210. $v_result = 1;
  3211. }
  3212. // ----- Look for abort result
  3213. if ($v_result == 2) {
  3214. // ----- This status is internal and will be changed in 'skipped'
  3215. $p_entry['status'] = "aborted";
  3216. $v_result = PCLZIP_ERR_USER_ABORTED;
  3217. }
  3218. // ----- Update the informations
  3219. // Only some fields can be modified
  3220. $p_entry['filename'] = $v_local_header['filename'];
  3221. }
  3222. // ----- Trace
  3223. // ----- Look if extraction should be done
  3224. if ($p_entry['status'] == 'ok') {
  3225. // ----- Do the extraction (if not a folder)
  3226. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  3227. // ----- Look for not compressed file
  3228. if ($p_entry['compressed_size'] == $p_entry['size']) {
  3229. // ----- Read the file in a buffer (one shot)
  3230. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3231. // ----- Send the file to the output
  3232. echo $v_buffer;
  3233. unset($v_buffer);
  3234. } else {
  3235. // ----- Read the compressed file in a buffer (one shot)
  3236. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3237. // ----- Decompress the file
  3238. $v_file_content = gzinflate($v_buffer);
  3239. unset($v_buffer);
  3240. // ----- Send the file to the output
  3241. echo $v_file_content;
  3242. unset($v_file_content);
  3243. }
  3244. }
  3245. }
  3246. // ----- Change abort status
  3247. if ($p_entry['status'] == "aborted") {
  3248. $p_entry['status'] = "skipped";
  3249. // ----- Look for post-extract callback
  3250. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3251. // ----- Generate a local information
  3252. $v_local_header = array();
  3253. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3254. // ----- Call the callback
  3255. // Here I do not use call_user_func() because I need to send a reference to the
  3256. // header.
  3257. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3258. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3259. // ----- Look for abort result
  3260. if ($v_result == 2) {
  3261. $v_result = PCLZIP_ERR_USER_ABORTED;
  3262. }
  3263. }
  3264. return $v_result;
  3265. }
  3266. // --------------------------------------------------------------------------------
  3267. // --------------------------------------------------------------------------------
  3268. // Function : privExtractFileAsString()
  3269. // Description :
  3270. // Parameters :
  3271. // Return Values :
  3272. // --------------------------------------------------------------------------------
  3273. public function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
  3274. {
  3275. $v_result = 1;
  3276. // ----- Read the file header
  3277. $v_header = array();
  3278. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  3279. // ----- Return
  3280. return $v_result;
  3281. }
  3282. // ----- Check that the file header is coherent with $p_entry info
  3283. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3284. // TBC
  3285. }
  3286. // ----- Look for pre-extract callback
  3287. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  3288. // ----- Generate a local information
  3289. $v_local_header = array();
  3290. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3291. // ----- Call the callback
  3292. // Here I do not use call_user_func() because I need to send a reference to the
  3293. // header.
  3294. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  3295. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  3296. if ($v_result == 0) {
  3297. // ----- Change the file status
  3298. $p_entry['status'] = "skipped";
  3299. $v_result = 1;
  3300. }
  3301. // ----- Look for abort result
  3302. if ($v_result == 2) {
  3303. // ----- This status is internal and will be changed in 'skipped'
  3304. $p_entry['status'] = "aborted";
  3305. $v_result = PCLZIP_ERR_USER_ABORTED;
  3306. }
  3307. // ----- Update the informations
  3308. // Only some fields can be modified
  3309. $p_entry['filename'] = $v_local_header['filename'];
  3310. }
  3311. // ----- Look if extraction should be done
  3312. if ($p_entry['status'] == 'ok') {
  3313. // ----- Do the extraction (if not a folder)
  3314. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  3315. // ----- Look for not compressed file
  3316. // if ($p_entry['compressed_size'] == $p_entry['size'])
  3317. if ($p_entry['compression'] == 0) {
  3318. // ----- Reading the file
  3319. $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
  3320. } else {
  3321. // ----- Reading the file
  3322. $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
  3323. // ----- Decompress the file
  3324. if (($p_string = @gzinflate($v_data)) === false) {
  3325. // TBC
  3326. }
  3327. }
  3328. // ----- Trace
  3329. } else {
  3330. // TBC : error : can not extract a folder in a string
  3331. }
  3332. }
  3333. // ----- Change abort status
  3334. if ($p_entry['status'] == "aborted") {
  3335. $p_entry['status'] = "skipped";
  3336. // ----- Look for post-extract callback
  3337. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3338. // ----- Generate a local information
  3339. $v_local_header = array();
  3340. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3341. // ----- Swap the content to header
  3342. $v_local_header['content'] = $p_string;
  3343. $p_string = '';
  3344. // ----- Call the callback
  3345. // Here I do not use call_user_func() because I need to send a reference to the
  3346. // header.
  3347. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3348. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3349. // ----- Swap back the content to header
  3350. $p_string = $v_local_header['content'];
  3351. unset($v_local_header['content']);
  3352. // ----- Look for abort result
  3353. if ($v_result == 2) {
  3354. $v_result = PCLZIP_ERR_USER_ABORTED;
  3355. }
  3356. }
  3357. // ----- Return
  3358. return $v_result;
  3359. }
  3360. // --------------------------------------------------------------------------------
  3361. // --------------------------------------------------------------------------------
  3362. // Function : privReadFileHeader()
  3363. // Description :
  3364. // Parameters :
  3365. // Return Values :
  3366. // --------------------------------------------------------------------------------
  3367. public function privReadFileHeader(&$p_header)
  3368. {
  3369. $v_result = 1;
  3370. // ----- Read the 4 bytes signature
  3371. $v_binary_data = @fread($this->zip_fd, 4);
  3372. $v_data = unpack('Vid', $v_binary_data);
  3373. // ----- Check signature
  3374. if ($v_data['id'] != 0x04034b50) {
  3375. // ----- Error log
  3376. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  3377. // ----- Return
  3378. return PclZip::errorCode();
  3379. }
  3380. // ----- Read the first 42 bytes of the header
  3381. $v_binary_data = fread($this->zip_fd, 26);
  3382. // ----- Look for invalid block size
  3383. if (strlen($v_binary_data) != 26) {
  3384. $p_header['filename'] = "";
  3385. $p_header['status'] = "invalid_header";
  3386. // ----- Error log
  3387. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));
  3388. // ----- Return
  3389. return PclZip::errorCode();
  3390. }
  3391. // ----- Extract the values
  3392. $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  3393. // ----- Get filename
  3394. $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  3395. // ----- Get extra_fields
  3396. if ($v_data['extra_len'] != 0) {
  3397. $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  3398. } else {
  3399. $p_header['extra'] = '';
  3400. }
  3401. // ----- Extract properties
  3402. $p_header['version_extracted'] = $v_data['version'];
  3403. $p_header['compression'] = $v_data['compression'];
  3404. $p_header['size'] = $v_data['size'];
  3405. $p_header['compressed_size'] = $v_data['compressed_size'];
  3406. $p_header['crc'] = $v_data['crc'];
  3407. $p_header['flag'] = $v_data['flag'];
  3408. $p_header['filename_len'] = $v_data['filename_len'];
  3409. // ----- Recuperate date in UNIX format
  3410. $p_header['mdate'] = $v_data['mdate'];
  3411. $p_header['mtime'] = $v_data['mtime'];
  3412. if ($p_header['mdate'] && $p_header['mtime']) {
  3413. // ----- Extract time
  3414. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  3415. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  3416. $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
  3417. // ----- Extract date
  3418. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  3419. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  3420. $v_day = $p_header['mdate'] & 0x001F;
  3421. // ----- Get UNIX date format
  3422. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  3423. } else {
  3424. $p_header['mtime'] = time();
  3425. }
  3426. // TBC
  3427. //for (reset($v_data); $key = key($v_data); next($v_data)) {
  3428. //}
  3429. // ----- Set the stored filename
  3430. $p_header['stored_filename'] = $p_header['filename'];
  3431. // ----- Set the status field
  3432. $p_header['status'] = "ok";
  3433. // ----- Return
  3434. return $v_result;
  3435. }
  3436. // --------------------------------------------------------------------------------
  3437. // --------------------------------------------------------------------------------
  3438. // Function : privReadCentralFileHeader()
  3439. // Description :
  3440. // Parameters :
  3441. // Return Values :
  3442. // --------------------------------------------------------------------------------
  3443. public function privReadCentralFileHeader(&$p_header)
  3444. {
  3445. $v_result = 1;
  3446. // ----- Read the 4 bytes signature
  3447. $v_binary_data = @fread($this->zip_fd, 4);
  3448. $v_data = unpack('Vid', $v_binary_data);
  3449. // ----- Check signature
  3450. if ($v_data['id'] != 0x02014b50) {
  3451. // ----- Error log
  3452. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  3453. // ----- Return
  3454. return PclZip::errorCode();
  3455. }
  3456. // ----- Read the first 42 bytes of the header
  3457. $v_binary_data = fread($this->zip_fd, 42);
  3458. // ----- Look for invalid block size
  3459. if (strlen($v_binary_data) != 42) {
  3460. $p_header['filename'] = "";
  3461. $p_header['status'] = "invalid_header";
  3462. // ----- Error log
  3463. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));
  3464. // ----- Return
  3465. return PclZip::errorCode();
  3466. }
  3467. // ----- Extract the values
  3468. $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);
  3469. // ----- Get filename
  3470. if ($p_header['filename_len'] != 0) {
  3471. $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  3472. } else {
  3473. $p_header['filename'] = '';
  3474. }
  3475. // ----- Get extra
  3476. if ($p_header['extra_len'] != 0) {
  3477. $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  3478. } else {
  3479. $p_header['extra'] = '';
  3480. }
  3481. // ----- Get comment
  3482. if ($p_header['comment_len'] != 0) {
  3483. $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  3484. } else {
  3485. $p_header['comment'] = '';
  3486. }
  3487. // ----- Extract properties
  3488. // ----- Recuperate date in UNIX format
  3489. //if ($p_header['mdate'] && $p_header['mtime'])
  3490. // TBC : bug : this was ignoring time with 0/0/0
  3491. if (1) {
  3492. // ----- Extract time
  3493. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  3494. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  3495. $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
  3496. // ----- Extract date
  3497. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  3498. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  3499. $v_day = $p_header['mdate'] & 0x001F;
  3500. // ----- Get UNIX date format
  3501. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  3502. } else {
  3503. $p_header['mtime'] = time();
  3504. }
  3505. // ----- Set the stored filename
  3506. $p_header['stored_filename'] = $p_header['filename'];
  3507. // ----- Set default status to ok
  3508. $p_header['status'] = 'ok';
  3509. // ----- Look if it is a directory
  3510. if (substr($p_header['filename'], -1) == '/') {
  3511. //$p_header['external'] = 0x41FF0010;
  3512. $p_header['external'] = 0x00000010;
  3513. }
  3514. // ----- Return
  3515. return $v_result;
  3516. }
  3517. // --------------------------------------------------------------------------------
  3518. // --------------------------------------------------------------------------------
  3519. // Function : privCheckFileHeaders()
  3520. // Description :
  3521. // Parameters :
  3522. // Return Values :
  3523. // 1 on success,
  3524. // 0 on error;
  3525. // --------------------------------------------------------------------------------
  3526. public function privCheckFileHeaders(&$p_local_header, &$p_central_header)
  3527. {
  3528. $v_result = 1;
  3529. // ----- Check the static values
  3530. // TBC
  3531. if ($p_local_header['filename'] != $p_central_header['filename']) {
  3532. }
  3533. if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  3534. }
  3535. if ($p_local_header['flag'] != $p_central_header['flag']) {
  3536. }
  3537. if ($p_local_header['compression'] != $p_central_header['compression']) {
  3538. }
  3539. if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  3540. }
  3541. if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  3542. }
  3543. // ----- Look for flag bit 3
  3544. if (($p_local_header['flag'] & 8) == 8) {
  3545. $p_local_header['size'] = $p_central_header['size'];
  3546. $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  3547. $p_local_header['crc'] = $p_central_header['crc'];
  3548. }
  3549. // ----- Return
  3550. return $v_result;
  3551. }
  3552. // --------------------------------------------------------------------------------
  3553. // --------------------------------------------------------------------------------
  3554. // Function : privReadEndCentralDir()
  3555. // Description :
  3556. // Parameters :
  3557. // Return Values :
  3558. // --------------------------------------------------------------------------------
  3559. public function privReadEndCentralDir(&$p_central_dir)
  3560. {
  3561. $v_result = 1;
  3562. // ----- Go to the end of the zip file
  3563. $v_size = filesize($this->zipname);
  3564. @fseek($this->zip_fd, $v_size);
  3565. if (@ftell($this->zip_fd) != $v_size) {
  3566. // ----- Error log
  3567. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \'' . $this->zipname . '\'');
  3568. // ----- Return
  3569. return PclZip::errorCode();
  3570. }
  3571. // ----- First try : look if this is an archive with no commentaries (most of the time)
  3572. // in this case the end of central dir is at 22 bytes of the file end
  3573. $v_found = 0;
  3574. if ($v_size > 26) {
  3575. @fseek($this->zip_fd, $v_size - 22);
  3576. if (($v_pos = @ftell($this->zip_fd)) != ($v_size - 22)) {
  3577. // ----- Error log
  3578. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
  3579. // ----- Return
  3580. return PclZip::errorCode();
  3581. }
  3582. // ----- Read for bytes
  3583. $v_binary_data = @fread($this->zip_fd, 4);
  3584. $v_data = @unpack('Vid', $v_binary_data);
  3585. // ----- Check signature
  3586. if ($v_data['id'] == 0x06054b50) {
  3587. $v_found = 1;
  3588. }
  3589. $v_pos = ftell($this->zip_fd);
  3590. }
  3591. // ----- Go back to the maximum possible size of the Central Dir End Record
  3592. if (!$v_found) {
  3593. $v_maximum_size = 65557; // 0xFFFF + 22;
  3594. if ($v_maximum_size > $v_size) {
  3595. $v_maximum_size = $v_size;
  3596. }
  3597. @fseek($this->zip_fd, $v_size - $v_maximum_size);
  3598. if (@ftell($this->zip_fd) != ($v_size - $v_maximum_size)) {
  3599. // ----- Error log
  3600. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
  3601. // ----- Return
  3602. return PclZip::errorCode();
  3603. }
  3604. // ----- Read byte per byte in order to find the signature
  3605. $v_pos = ftell($this->zip_fd);
  3606. $v_bytes = 0x00000000;
  3607. while ($v_pos < $v_size) {
  3608. // ----- Read a byte
  3609. $v_byte = @fread($this->zip_fd, 1);
  3610. // ----- Add the byte
  3611. //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
  3612. // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
  3613. // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
  3614. $v_bytes = (($v_bytes & 0xFFFFFF) << 8) | ord($v_byte);
  3615. // ----- Compare the bytes
  3616. if ($v_bytes == 0x504b0506) {
  3617. $v_pos++;
  3618. break;
  3619. }
  3620. $v_pos++;
  3621. }
  3622. // ----- Look if not found end of central dir
  3623. if ($v_pos == $v_size) {
  3624. // ----- Error log
  3625. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  3626. // ----- Return
  3627. return PclZip::errorCode();
  3628. }
  3629. }
  3630. // ----- Read the first 18 bytes of the header
  3631. $v_binary_data = fread($this->zip_fd, 18);
  3632. // ----- Look for invalid block size
  3633. if (strlen($v_binary_data) != 18) {
  3634. // ----- Error log
  3635. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : " . strlen($v_binary_data));
  3636. // ----- Return
  3637. return PclZip::errorCode();
  3638. }
  3639. // ----- Extract the values
  3640. $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  3641. // ----- Check the global size
  3642. if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  3643. // ----- Removed in release 2.2 see readme file
  3644. // The check of the file size is a little too strict.
  3645. // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
  3646. // While decrypted, zip has training 0 bytes
  3647. if (0) {
  3648. // ----- Error log
  3649. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'The central dir is not at the end of the archive.' . ' Some trailing bytes exists after the archive.');
  3650. // ----- Return
  3651. return PclZip::errorCode();
  3652. }
  3653. }
  3654. // ----- Get comment
  3655. if ($v_data['comment_size'] != 0) {
  3656. $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  3657. } else {
  3658. $p_central_dir['comment'] = '';
  3659. }
  3660. $p_central_dir['entries'] = $v_data['entries'];
  3661. $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  3662. $p_central_dir['offset'] = $v_data['offset'];
  3663. $p_central_dir['size'] = $v_data['size'];
  3664. $p_central_dir['disk'] = $v_data['disk'];
  3665. $p_central_dir['disk_start'] = $v_data['disk_start'];
  3666. // TBC
  3667. //for (reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
  3668. //}
  3669. // ----- Return
  3670. return $v_result;
  3671. }
  3672. // --------------------------------------------------------------------------------
  3673. // --------------------------------------------------------------------------------
  3674. // Function : privDeleteByRule()
  3675. // Description :
  3676. // Parameters :
  3677. // Return Values :
  3678. // --------------------------------------------------------------------------------
  3679. public function privDeleteByRule(&$p_result_list, &$p_options)
  3680. {
  3681. $v_result = 1;
  3682. $v_list_detail = array();
  3683. // ----- Open the zip file
  3684. if (($v_result = $this->privOpenFd('rb')) != 1) {
  3685. // ----- Return
  3686. return $v_result;
  3687. }
  3688. // ----- Read the central directory informations
  3689. $v_central_dir = array();
  3690. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  3691. $this->privCloseFd();
  3692. return $v_result;
  3693. }
  3694. // ----- Go to beginning of File
  3695. @rewind($this->zip_fd);
  3696. // ----- Scan all the files
  3697. // ----- Start at beginning of Central Dir
  3698. $v_pos_entry = $v_central_dir['offset'];
  3699. @rewind($this->zip_fd);
  3700. if (@fseek($this->zip_fd, $v_pos_entry)) {
  3701. // ----- Close the zip file
  3702. $this->privCloseFd();
  3703. // ----- Error log
  3704. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3705. // ----- Return
  3706. return PclZip::errorCode();
  3707. }
  3708. // ----- Read each entry
  3709. $v_header_list = array();
  3710. $j_start = 0;
  3711. for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
  3712. // ----- Read the file header
  3713. $v_header_list[$v_nb_extracted] = array();
  3714. if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1) {
  3715. // ----- Close the zip file
  3716. $this->privCloseFd();
  3717. return $v_result;
  3718. }
  3719. // ----- Store the index
  3720. $v_header_list[$v_nb_extracted]['index'] = $i;
  3721. // ----- Look for the specific extract rules
  3722. $v_found = false;
  3723. // ----- Look for extract by name rule
  3724. if ((isset($p_options[PCLZIP_OPT_BY_NAME])) && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  3725. // ----- Look if the filename is in the list
  3726. for ($j = 0; ($j < sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  3727. // ----- Look for a directory
  3728. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  3729. // ----- Look if the directory is in the filename path
  3730. 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])) {
  3731. $v_found = true;
  3732. } 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])) {
  3733. $v_found = true;
  3734. }
  3735. // ----- Look for a filename
  3736. } elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  3737. $v_found = true;
  3738. }
  3739. }
  3740. // ----- Look for extract by ereg rule
  3741. // ereg() is deprecated with PHP 5.3
  3742. /*
  3743. elseif ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
  3744. && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  3745. if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  3746. $v_found = true;
  3747. }
  3748. }
  3749. */
  3750. // ----- Look for extract by preg rule
  3751. } elseif ((isset($p_options[PCLZIP_OPT_BY_PREG])) && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  3752. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  3753. $v_found = true;
  3754. }
  3755. // ----- Look for extract by index rule
  3756. } elseif ((isset($p_options[PCLZIP_OPT_BY_INDEX])) && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  3757. // ----- Look if the index is in the list
  3758. for ($j = $j_start; ($j < sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  3759. if (($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  3760. $v_found = true;
  3761. }
  3762. if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  3763. $j_start = $j + 1;
  3764. }
  3765. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
  3766. break;
  3767. }
  3768. }
  3769. } else {
  3770. $v_found = true;
  3771. }
  3772. // ----- Look for deletion
  3773. if ($v_found) {
  3774. unset($v_header_list[$v_nb_extracted]);
  3775. } else {
  3776. $v_nb_extracted++;
  3777. }
  3778. }
  3779. // ----- Look if something need to be deleted
  3780. if ($v_nb_extracted > 0) {
  3781. // ----- Creates a temporay file
  3782. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  3783. // ----- Creates a temporary zip archive
  3784. $v_temp_zip = new PclZip($v_zip_temp_name);
  3785. // ----- Open the temporary zip file in write mode
  3786. if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  3787. $this->privCloseFd();
  3788. // ----- Return
  3789. return $v_result;
  3790. }
  3791. // ----- Look which file need to be kept
  3792. for ($i = 0; $i < sizeof($v_header_list); $i++) {
  3793. // ----- Calculate the position of the header
  3794. @rewind($this->zip_fd);
  3795. if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
  3796. // ----- Close the zip file
  3797. $this->privCloseFd();
  3798. $v_temp_zip->privCloseFd();
  3799. @unlink($v_zip_temp_name);
  3800. // ----- Error log
  3801. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3802. // ----- Return
  3803. return PclZip::errorCode();
  3804. }
  3805. // ----- Read the file header
  3806. $v_local_header = array();
  3807. if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  3808. // ----- Close the zip file
  3809. $this->privCloseFd();
  3810. $v_temp_zip->privCloseFd();
  3811. @unlink($v_zip_temp_name);
  3812. // ----- Return
  3813. return $v_result;
  3814. }
  3815. // ----- Check that local file header is same as central file header
  3816. if ($this->privCheckFileHeaders($v_local_header, $v_header_list[$i]) != 1) {
  3817. // TBC
  3818. }
  3819. unset($v_local_header);
  3820. // ----- Write the file header
  3821. if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
  3822. // ----- Close the zip file
  3823. $this->privCloseFd();
  3824. $v_temp_zip->privCloseFd();
  3825. @unlink($v_zip_temp_name);
  3826. // ----- Return
  3827. return $v_result;
  3828. }
  3829. // ----- Read/write the data block
  3830. if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  3831. // ----- Close the zip file
  3832. $this->privCloseFd();
  3833. $v_temp_zip->privCloseFd();
  3834. @unlink($v_zip_temp_name);
  3835. // ----- Return
  3836. return $v_result;
  3837. }
  3838. }
  3839. // ----- Store the offset of the central dir
  3840. $v_offset = @ftell($v_temp_zip->zip_fd);
  3841. // ----- Re-Create the Central Dir files header
  3842. for ($i = 0; $i < sizeof($v_header_list); $i++) {
  3843. // ----- Create the file header
  3844. if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  3845. $v_temp_zip->privCloseFd();
  3846. $this->privCloseFd();
  3847. @unlink($v_zip_temp_name);
  3848. // ----- Return
  3849. return $v_result;
  3850. }
  3851. // ----- Transform the header to a 'usable' info
  3852. $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  3853. }
  3854. // ----- Zip file comment
  3855. $v_comment = '';
  3856. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  3857. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  3858. }
  3859. // ----- Calculate the size of the central header
  3860. $v_size = @ftell($v_temp_zip->zip_fd) - $v_offset;
  3861. // ----- Create the central dir footer
  3862. if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
  3863. // ----- Reset the file list
  3864. unset($v_header_list);
  3865. $v_temp_zip->privCloseFd();
  3866. $this->privCloseFd();
  3867. @unlink($v_zip_temp_name);
  3868. // ----- Return
  3869. return $v_result;
  3870. }
  3871. // ----- Close
  3872. $v_temp_zip->privCloseFd();
  3873. $this->privCloseFd();
  3874. // ----- Delete the zip file
  3875. // TBC : I should test the result ...
  3876. @unlink($this->zipname);
  3877. // ----- Rename the temporary file
  3878. // TBC : I should test the result ...
  3879. //@rename($v_zip_temp_name, $this->zipname);
  3880. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  3881. // ----- Destroy the temporary archive
  3882. unset($v_temp_zip);
  3883. // ----- Remove every files : reset the file
  3884. } elseif ($v_central_dir['entries'] != 0) {
  3885. $this->privCloseFd();
  3886. if (($v_result = $this->privOpenFd('wb')) != 1) {
  3887. return $v_result;
  3888. }
  3889. if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  3890. return $v_result;
  3891. }
  3892. $this->privCloseFd();
  3893. }
  3894. // ----- Return
  3895. return $v_result;
  3896. }
  3897. // --------------------------------------------------------------------------------
  3898. // --------------------------------------------------------------------------------
  3899. // Function : privDirCheck()
  3900. // Description :
  3901. // Check if a directory exists, if not it creates it and all the parents directory
  3902. // which may be useful.
  3903. // Parameters :
  3904. // $p_dir : Directory path to check.
  3905. // Return Values :
  3906. // 1 : OK
  3907. // -1 : Unable to create directory
  3908. // --------------------------------------------------------------------------------
  3909. public function privDirCheck($p_dir, $p_is_dir = false)
  3910. {
  3911. $v_result = 1;
  3912. // ----- Remove the final '/'
  3913. if (($p_is_dir) && (substr($p_dir, -1) == '/')) {
  3914. $p_dir = substr($p_dir, 0, strlen($p_dir) - 1);
  3915. }
  3916. // ----- Check the directory availability
  3917. if ((is_dir($p_dir)) || ($p_dir == "")) {
  3918. return 1;
  3919. }
  3920. // ----- Extract parent directory
  3921. $p_parent_dir = dirname($p_dir);
  3922. // ----- Just a check
  3923. if ($p_parent_dir != $p_dir) {
  3924. // ----- Look for parent directory
  3925. if ($p_parent_dir != "") {
  3926. if (($v_result = $this->privDirCheck($p_parent_dir)) != 1) {
  3927. return $v_result;
  3928. }
  3929. }
  3930. }
  3931. // ----- Create the directory
  3932. if (!@mkdir($p_dir, 0777)) {
  3933. // ----- Error log
  3934. PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  3935. // ----- Return
  3936. return PclZip::errorCode();
  3937. }
  3938. // ----- Return
  3939. return $v_result;
  3940. }
  3941. // --------------------------------------------------------------------------------
  3942. // --------------------------------------------------------------------------------
  3943. // Function : privMerge()
  3944. // Description :
  3945. // If $p_archive_to_add does not exist, the function exit with a success result.
  3946. // Parameters :
  3947. // Return Values :
  3948. // --------------------------------------------------------------------------------
  3949. public function privMerge(&$p_archive_to_add)
  3950. {
  3951. $v_result = 1;
  3952. // ----- Look if the archive_to_add exists
  3953. if (!is_file($p_archive_to_add->zipname)) {
  3954. // ----- Nothing to merge, so merge is a success
  3955. $v_result = 1;
  3956. // ----- Return
  3957. return $v_result;
  3958. }
  3959. // ----- Look if the archive exists
  3960. if (!is_file($this->zipname)) {
  3961. // ----- Do a duplicate
  3962. $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  3963. // ----- Return
  3964. return $v_result;
  3965. }
  3966. // ----- Open the zip file
  3967. if (($v_result = $this->privOpenFd('rb')) != 1) {
  3968. // ----- Return
  3969. return $v_result;
  3970. }
  3971. // ----- Read the central directory informations
  3972. $v_central_dir = array();
  3973. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  3974. $this->privCloseFd();
  3975. return $v_result;
  3976. }
  3977. // ----- Go to beginning of File
  3978. @rewind($this->zip_fd);
  3979. // ----- Open the archive_to_add file
  3980. if (($v_result = $p_archive_to_add->privOpenFd('rb')) != 1) {
  3981. $this->privCloseFd();
  3982. // ----- Return
  3983. return $v_result;
  3984. }
  3985. // ----- Read the central directory informations
  3986. $v_central_dir_to_add = array();
  3987. if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1) {
  3988. $this->privCloseFd();
  3989. $p_archive_to_add->privCloseFd();
  3990. return $v_result;
  3991. }
  3992. // ----- Go to beginning of File
  3993. @rewind($p_archive_to_add->zip_fd);
  3994. // ----- Creates a temporay file
  3995. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  3996. // ----- Open the temporary file in write mode
  3997. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  3998. $this->privCloseFd();
  3999. $p_archive_to_add->privCloseFd();
  4000. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');
  4001. // ----- Return
  4002. return PclZip::errorCode();
  4003. }
  4004. // ----- Copy the files from the archive to the temporary file
  4005. // TBC : Here I should better append the file and go back to erase the central dir
  4006. $v_size = $v_central_dir['offset'];
  4007. while ($v_size != 0) {
  4008. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4009. $v_buffer = fread($this->zip_fd, $v_read_size);
  4010. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4011. $v_size -= $v_read_size;
  4012. }
  4013. // ----- Copy the files from the archive_to_add into the temporary file
  4014. $v_size = $v_central_dir_to_add['offset'];
  4015. while ($v_size != 0) {
  4016. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4017. $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  4018. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4019. $v_size -= $v_read_size;
  4020. }
  4021. // ----- Store the offset of the central dir
  4022. $v_offset = @ftell($v_zip_temp_fd);
  4023. // ----- Copy the block of file headers from the old archive
  4024. $v_size = $v_central_dir['size'];
  4025. while ($v_size != 0) {
  4026. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4027. $v_buffer = @fread($this->zip_fd, $v_read_size);
  4028. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4029. $v_size -= $v_read_size;
  4030. }
  4031. // ----- Copy the block of file headers from the archive_to_add
  4032. $v_size = $v_central_dir_to_add['size'];
  4033. while ($v_size != 0) {
  4034. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4035. $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  4036. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4037. $v_size -= $v_read_size;
  4038. }
  4039. // ----- Merge the file comments
  4040. $v_comment = $v_central_dir['comment'] . ' ' . $v_central_dir_to_add['comment'];
  4041. // ----- Calculate the size of the (new) central header
  4042. $v_size = @ftell($v_zip_temp_fd) - $v_offset;
  4043. // ----- Swap the file descriptor
  4044. // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  4045. // the following methods on the temporary fil and not the real archive fd
  4046. $v_swap = $this->zip_fd;
  4047. $this->zip_fd = $v_zip_temp_fd;
  4048. $v_zip_temp_fd = $v_swap;
  4049. // ----- Create the central dir footer
  4050. if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries'] + $v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1) {
  4051. $this->privCloseFd();
  4052. $p_archive_to_add->privCloseFd();
  4053. @fclose($v_zip_temp_fd);
  4054. $this->zip_fd = null;
  4055. // ----- Reset the file list
  4056. unset($v_header_list);
  4057. // ----- Return
  4058. return $v_result;
  4059. }
  4060. // ----- Swap back the file descriptor
  4061. $v_swap = $this->zip_fd;
  4062. $this->zip_fd = $v_zip_temp_fd;
  4063. $v_zip_temp_fd = $v_swap;
  4064. // ----- Close
  4065. $this->privCloseFd();
  4066. $p_archive_to_add->privCloseFd();
  4067. // ----- Close the temporary file
  4068. @fclose($v_zip_temp_fd);
  4069. // ----- Delete the zip file
  4070. // TBC : I should test the result ...
  4071. @unlink($this->zipname);
  4072. // ----- Rename the temporary file
  4073. // TBC : I should test the result ...
  4074. //@rename($v_zip_temp_name, $this->zipname);
  4075. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  4076. // ----- Return
  4077. return $v_result;
  4078. }
  4079. // --------------------------------------------------------------------------------
  4080. // --------------------------------------------------------------------------------
  4081. // Function : privDuplicate()
  4082. // Description :
  4083. // Parameters :
  4084. // Return Values :
  4085. // --------------------------------------------------------------------------------
  4086. public function privDuplicate($p_archive_filename)
  4087. {
  4088. $v_result = 1;
  4089. // ----- Look if the $p_archive_filename exists
  4090. if (!is_file($p_archive_filename)) {
  4091. // ----- Nothing to duplicate, so duplicate is a success.
  4092. $v_result = 1;
  4093. // ----- Return
  4094. return $v_result;
  4095. }
  4096. // ----- Open the zip file
  4097. if (($v_result = $this->privOpenFd('wb')) != 1) {
  4098. // ----- Return
  4099. return $v_result;
  4100. }
  4101. // ----- Open the temporary file in write mode
  4102. if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
  4103. $this->privCloseFd();
  4104. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \'' . $p_archive_filename . '\' in binary write mode');
  4105. // ----- Return
  4106. return PclZip::errorCode();
  4107. }
  4108. // ----- Copy the files from the archive to the temporary file
  4109. // TBC : Here I should better append the file and go back to erase the central dir
  4110. $v_size = filesize($p_archive_filename);
  4111. while ($v_size != 0) {
  4112. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4113. $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  4114. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  4115. $v_size -= $v_read_size;
  4116. }
  4117. // ----- Close
  4118. $this->privCloseFd();
  4119. // ----- Close the temporary file
  4120. @fclose($v_zip_temp_fd);
  4121. // ----- Return
  4122. return $v_result;
  4123. }
  4124. // --------------------------------------------------------------------------------
  4125. // --------------------------------------------------------------------------------
  4126. // Function : privErrorLog()
  4127. // Description :
  4128. // Parameters :
  4129. // --------------------------------------------------------------------------------
  4130. public function privErrorLog($p_error_code = 0, $p_error_string = '')
  4131. {
  4132. if (PCLZIP_ERROR_EXTERNAL == 1) {
  4133. PclError($p_error_code, $p_error_string);
  4134. } else {
  4135. $this->error_code = $p_error_code;
  4136. $this->error_string = $p_error_string;
  4137. }
  4138. }
  4139. // --------------------------------------------------------------------------------
  4140. // --------------------------------------------------------------------------------
  4141. // Function : privErrorReset()
  4142. // Description :
  4143. // Parameters :
  4144. // --------------------------------------------------------------------------------
  4145. public function privErrorReset()
  4146. {
  4147. if (PCLZIP_ERROR_EXTERNAL == 1) {
  4148. PclErrorReset();
  4149. } else {
  4150. $this->error_code = 0;
  4151. $this->error_string = '';
  4152. }
  4153. }
  4154. // --------------------------------------------------------------------------------
  4155. // --------------------------------------------------------------------------------
  4156. // Function : privDisableMagicQuotes()
  4157. // Description :
  4158. // Parameters :
  4159. // Return Values :
  4160. // --------------------------------------------------------------------------------
  4161. public function privDisableMagicQuotes()
  4162. {
  4163. $v_result = 1;
  4164. // ----- Look if function exists
  4165. if ((!function_exists("get_magic_quotes_runtime")) || (!function_exists("set_magic_quotes_runtime"))) {
  4166. return $v_result;
  4167. }
  4168. // ----- Look if already done
  4169. if ($this->magic_quotes_status != -1) {
  4170. return $v_result;
  4171. }
  4172. // ----- Get and memorize the magic_quote value
  4173. $this->magic_quotes_status = @get_magic_quotes_runtime();
  4174. // ----- Disable magic_quotes
  4175. if ($this->magic_quotes_status == 1) {
  4176. @set_magic_quotes_runtime(0);
  4177. }
  4178. // ----- Return
  4179. return $v_result;
  4180. }
  4181. // --------------------------------------------------------------------------------
  4182. // --------------------------------------------------------------------------------
  4183. // Function : privSwapBackMagicQuotes()
  4184. // Description :
  4185. // Parameters :
  4186. // Return Values :
  4187. // --------------------------------------------------------------------------------
  4188. public function privSwapBackMagicQuotes()
  4189. {
  4190. $v_result = 1;
  4191. // ----- Look if function exists
  4192. if ((!function_exists("get_magic_quotes_runtime")) || (!function_exists("set_magic_quotes_runtime"))) {
  4193. return $v_result;
  4194. }
  4195. // ----- Look if something to do
  4196. if ($this->magic_quotes_status != -1) {
  4197. return $v_result;
  4198. }
  4199. // ----- Swap back magic_quotes
  4200. if ($this->magic_quotes_status == 1) {
  4201. @set_magic_quotes_runtime($this->magic_quotes_status);
  4202. }
  4203. // ----- Return
  4204. return $v_result;
  4205. }
  4206. // --------------------------------------------------------------------------------
  4207. }
  4208. // End of class
  4209. // --------------------------------------------------------------------------------
  4210. // --------------------------------------------------------------------------------
  4211. // Function : PclZipUtilPathReduction()
  4212. // Description :
  4213. // Parameters :
  4214. // Return Values :
  4215. // --------------------------------------------------------------------------------
  4216. function PclZipUtilPathReduction($p_dir)
  4217. {
  4218. $v_result = "";
  4219. // ----- Look for not empty path
  4220. if ($p_dir != "") {
  4221. // ----- Explode path by directory names
  4222. $v_list = explode("/", $p_dir);
  4223. // ----- Study directories from last to first
  4224. $v_skip = 0;
  4225. for ($i = sizeof($v_list) - 1; $i >= 0; $i--) {
  4226. // ----- Look for current path
  4227. if ($v_list[$i] == ".") {
  4228. // ----- Ignore this directory
  4229. // Should be the first $i=0, but no check is done
  4230. } elseif ($v_list[$i] == "..") {
  4231. $v_skip++;
  4232. } elseif ($v_list[$i] == "") {
  4233. // ----- First '/' i.e. root slash
  4234. if ($i == 0) {
  4235. $v_result = "/" . $v_result;
  4236. if ($v_skip > 0) {
  4237. // ----- It is an invalid path, so the path is not modified
  4238. // TBC
  4239. $v_result = $p_dir;
  4240. $v_skip = 0;
  4241. }
  4242. // ----- Last '/' i.e. indicates a directory
  4243. } elseif ($i == (sizeof($v_list) - 1)) {
  4244. $v_result = $v_list[$i];
  4245. // ----- Double '/' inside the path
  4246. } else {
  4247. // ----- Ignore only the double '//' in path,
  4248. // but not the first and last '/'
  4249. }
  4250. } else {
  4251. // ----- Look for item to skip
  4252. if ($v_skip > 0) {
  4253. $v_skip--;
  4254. } else {
  4255. $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? "/" . $v_result : "");
  4256. }
  4257. }
  4258. }
  4259. // ----- Look for skip
  4260. if ($v_skip > 0) {
  4261. while ($v_skip > 0) {
  4262. $v_result = '../' . $v_result;
  4263. $v_skip--;
  4264. }
  4265. }
  4266. }
  4267. // ----- Return
  4268. return $v_result;
  4269. }
  4270. // --------------------------------------------------------------------------------
  4271. // --------------------------------------------------------------------------------
  4272. // Function : PclZipUtilPathInclusion()
  4273. // Description :
  4274. // This function indicates if the path $p_path is under the $p_dir tree. Or,
  4275. // said in an other way, if the file or sub-dir $p_path is inside the dir
  4276. // $p_dir.
  4277. // The function indicates also if the path is exactly the same as the dir.
  4278. // This function supports path with duplicated '/' like '//', but does not
  4279. // support '.' or '..' statements.
  4280. // Parameters :
  4281. // Return Values :
  4282. // 0 if $p_path is not inside directory $p_dir
  4283. // 1 if $p_path is inside directory $p_dir
  4284. // 2 if $p_path is exactly the same as $p_dir
  4285. // --------------------------------------------------------------------------------
  4286. function PclZipUtilPathInclusion($p_dir, $p_path)
  4287. {
  4288. $v_result = 1;
  4289. // ----- Look for path beginning by ./
  4290. if (($p_dir == '.') || ((strlen($p_dir) >= 2) && (substr($p_dir, 0, 2) == './'))) {
  4291. $p_dir = PclZipUtilTranslateWinPath(getcwd(), false) . '/' . substr($p_dir, 1);
  4292. }
  4293. if (($p_path == '.') || ((strlen($p_path) >= 2) && (substr($p_path, 0, 2) == './'))) {
  4294. $p_path = PclZipUtilTranslateWinPath(getcwd(), false) . '/' . substr($p_path, 1);
  4295. }
  4296. // ----- Explode dir and path by directory separator
  4297. $v_list_dir = explode("/", $p_dir);
  4298. $v_list_dir_size = sizeof($v_list_dir);
  4299. $v_list_path = explode("/", $p_path);
  4300. $v_list_path_size = sizeof($v_list_path);
  4301. // ----- Study directories paths
  4302. $i = 0;
  4303. $j = 0;
  4304. while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  4305. // ----- Look for empty dir (path reduction)
  4306. if ($v_list_dir[$i] == '') {
  4307. $i++;
  4308. continue;
  4309. }
  4310. if ($v_list_path[$j] == '') {
  4311. $j++;
  4312. continue;
  4313. }
  4314. // ----- Compare the items
  4315. if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ($v_list_path[$j] != '')) {
  4316. $v_result = 0;
  4317. }
  4318. // ----- Next items
  4319. $i++;
  4320. $j++;
  4321. }
  4322. // ----- Look if everything seems to be the same
  4323. if ($v_result) {
  4324. // ----- Skip all the empty items
  4325. while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) {
  4326. $j++;
  4327. }
  4328. while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) {
  4329. $i++;
  4330. }
  4331. if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  4332. // ----- There are exactly the same
  4333. $v_result = 2;
  4334. } elseif ($i < $v_list_dir_size) {
  4335. // ----- The path is shorter than the dir
  4336. $v_result = 0;
  4337. }
  4338. }
  4339. // ----- Return
  4340. return $v_result;
  4341. }
  4342. // --------------------------------------------------------------------------------
  4343. // --------------------------------------------------------------------------------
  4344. // Function : PclZipUtilCopyBlock()
  4345. // Description :
  4346. // Parameters :
  4347. // $p_mode : read/write compression mode
  4348. // 0 : src & dest normal
  4349. // 1 : src gzip, dest normal
  4350. // 2 : src normal, dest gzip
  4351. // 3 : src & dest gzip
  4352. // Return Values :
  4353. // --------------------------------------------------------------------------------
  4354. function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode = 0)
  4355. {
  4356. $v_result = 1;
  4357. if ($p_mode == 0) {
  4358. while ($p_size != 0) {
  4359. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4360. $v_buffer = @fread($p_src, $v_read_size);
  4361. @fwrite($p_dest, $v_buffer, $v_read_size);
  4362. $p_size -= $v_read_size;
  4363. }
  4364. } elseif ($p_mode == 1) {
  4365. while ($p_size != 0) {
  4366. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4367. $v_buffer = @gzread($p_src, $v_read_size);
  4368. @fwrite($p_dest, $v_buffer, $v_read_size);
  4369. $p_size -= $v_read_size;
  4370. }
  4371. } elseif ($p_mode == 2) {
  4372. while ($p_size != 0) {
  4373. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4374. $v_buffer = @fread($p_src, $v_read_size);
  4375. @gzwrite($p_dest, $v_buffer, $v_read_size);
  4376. $p_size -= $v_read_size;
  4377. }
  4378. } elseif ($p_mode == 3) {
  4379. while ($p_size != 0) {
  4380. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4381. $v_buffer = @gzread($p_src, $v_read_size);
  4382. @gzwrite($p_dest, $v_buffer, $v_read_size);
  4383. $p_size -= $v_read_size;
  4384. }
  4385. }
  4386. // ----- Return
  4387. return $v_result;
  4388. }
  4389. // --------------------------------------------------------------------------------
  4390. // --------------------------------------------------------------------------------
  4391. // Function : PclZipUtilRename()
  4392. // Description :
  4393. // This function tries to do a simple rename() function. If it fails, it
  4394. // tries to copy the $p_src file in a new $p_dest file and then unlink the
  4395. // first one.
  4396. // Parameters :
  4397. // $p_src : Old filename
  4398. // $p_dest : New filename
  4399. // Return Values :
  4400. // 1 on success, 0 on failure.
  4401. // --------------------------------------------------------------------------------
  4402. function PclZipUtilRename($p_src, $p_dest)
  4403. {
  4404. $v_result = 1;
  4405. // ----- Try to rename the files
  4406. if (!@rename($p_src, $p_dest)) {
  4407. // ----- Try to copy & unlink the src
  4408. if (!@copy($p_src, $p_dest)) {
  4409. $v_result = 0;
  4410. } elseif (!@unlink($p_src)) {
  4411. $v_result = 0;
  4412. }
  4413. }
  4414. // ----- Return
  4415. return $v_result;
  4416. }
  4417. // --------------------------------------------------------------------------------
  4418. // --------------------------------------------------------------------------------
  4419. // Function : PclZipUtilOptionText()
  4420. // Description :
  4421. // Translate option value in text. Mainly for debug purpose.
  4422. // Parameters :
  4423. // $p_option : the option value.
  4424. // Return Values :
  4425. // The option text value.
  4426. // --------------------------------------------------------------------------------
  4427. function PclZipUtilOptionText($p_option)
  4428. {
  4429. $v_list = get_defined_constants();
  4430. for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  4431. $v_prefix = substr($v_key, 0, 10);
  4432. if ((($v_prefix == 'PCLZIP_OPT') || ($v_prefix == 'PCLZIP_CB_') || ($v_prefix == 'PCLZIP_ATT')) && ($v_list[$v_key] == $p_option)) {
  4433. return $v_key;
  4434. }
  4435. }
  4436. $v_result = 'Unknown';
  4437. return $v_result;
  4438. }
  4439. // --------------------------------------------------------------------------------
  4440. // --------------------------------------------------------------------------------
  4441. // Function : PclZipUtilTranslateWinPath()
  4442. // Description :
  4443. // Translate windows path by replacing '\' by '/' and optionally removing
  4444. // drive letter.
  4445. // Parameters :
  4446. // $p_path : path to translate.
  4447. // $p_remove_disk_letter : true | false
  4448. // Return Values :
  4449. // The path translated.
  4450. // --------------------------------------------------------------------------------
  4451. function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter = true)
  4452. {
  4453. if (stristr(php_uname(), 'windows')) {
  4454. // ----- Look for potential disk letter
  4455. if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
  4456. $p_path = substr($p_path, $v_position + 1);
  4457. }
  4458. // ----- Change potential windows directory separator
  4459. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
  4460. $p_path = strtr($p_path, '\\', '/');
  4461. }
  4462. }
  4463. return $p_path;
  4464. }
  4465. // --------------------------------------------------------------------------------