zip.class.php 243 KB

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