pclzip.lib.php 192 KB

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