class-pclzip.php 192 KB

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