pclzip.lib.php 191 KB

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