pclzip.lib.php 210 KB

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