PclZip.php 193 KB

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