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