pcltar.lib.php 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572
  1. <?php
  2. // --------------------------------------------------------------------------------
  3. // PhpConcept Library - Tar Module 1.3
  4. // --------------------------------------------------------------------------------
  5. // License GNU/GPL - Vincent Blavet - August 2001
  6. // http://www.phpconcept.net
  7. // --------------------------------------------------------------------------------
  8. //
  9. // Presentation :
  10. // PclTar is a library that allow you to create a GNU TAR + GNU ZIP archive,
  11. // to add files or directories, to extract all the archive or a part of it.
  12. // So far tests show that the files generated by PclTar are readable by
  13. // gzip tools and WinZip application.
  14. //
  15. // Description :
  16. // See readme.txt (English & Français) and http://www.phpconcept.net
  17. //
  18. // Warning :
  19. // This library and the associated files are non commercial, non professional
  20. // work.
  21. // It should not have unexpected results. However if any damage is caused by
  22. // this software the author can not be responsible.
  23. // The use of this software is at the risk of the user.
  24. //
  25. // --------------------------------------------------------------------------------
  26. // ----- Look for double include
  27. if (!defined("PCL_TAR"))
  28. {
  29. define( "PCL_TAR", 1 );
  30. // ----- Configuration variable
  31. // Theses values may be changed by the user of PclTar library
  32. if (!isset($g_pcltar_lib_dir))
  33. $g_pcltar_lib_dir = "lib";
  34. // ----- Error codes
  35. // -1 : Unable to open file in binary write mode
  36. // -2 : Unable to open file in binary read mode
  37. // -3 : Invalid parameters
  38. // -4 : File does not exist
  39. // -5 : Filename is too long (max. 99)
  40. // -6 : Not a valid tar file
  41. // -7 : Invalid extracted file size
  42. // -8 : Unable to create directory
  43. // -9 : Invalid archive extension
  44. // -10 : Invalid archive format
  45. // -11 : Unable to delete file (unlink)
  46. // -12 : Unable to rename file (rename)
  47. // -13 : Invalid header checksum
  48. // --------------------------------------------------------------------------------
  49. // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
  50. // --------------------------------------------------------------------------------
  51. // ----- Global variables
  52. $g_pcltar_version = "1.3";
  53. // ----- Extract extension type (.php3/.php/...)
  54. // NET2FTP
  55. // $g_pcltar_extension = substr(strrchr(basename($PATH_TRANSLATED), '.'), 1);
  56. $g_pcltar_extension = substr(strrchr(basename(__FILE__), '.'), 1);
  57. // ----- Include other libraries
  58. // This library should be called by each script before the include of PhpZip
  59. // Library in order to limit the potential 'lib' directory path problem.
  60. // NET2FTP
  61. // Do not include the 2 other libraries again, they are already included from
  62. // net2ftp's main.inc.php file
  63. // if (!defined("PCLERROR_LIB"))
  64. // {
  65. // include($g_pcltar_lib_dir."/pclerror.lib.".$g_pcltar_extension);
  66. // }
  67. // if (!defined("PCLTRACE_LIB"))
  68. // {
  69. // include($g_pcltar_lib_dir."/pcltrace.lib.".$g_pcltar_extension);
  70. // }
  71. // --------------------------------------------------------------------------------
  72. // Function : PclTarCreate()
  73. // Description :
  74. // Creates a new archive with name $p_tarname containing the files and/or
  75. // directories indicated in $p_list. If the tar filename extension is
  76. // ".tar", the file will not be compressed. If it is ".tar.gz" or ".tgz"
  77. // it will be a gzip compressed tar archive.
  78. // If you want to use an other extension, you must indicate the mode in
  79. // $p_mode ("tar" or "tgz").
  80. // $p_add_dir and $p_remove_dir give you the ability to store a path
  81. // which is not the real path of the files.
  82. // Parameters :
  83. // $p_tarname : Name of an existing tar file
  84. // $p_filelist : An array containing file or directory names, or
  85. // a string containing one filename or directory name, or
  86. // a string containing a list of filenames and/or directory
  87. // names separated by spaces.
  88. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive,
  89. // if $p_mode is not specified, it will be determined by the extension.
  90. // $p_add_dir : Path to add in the filename path archived
  91. // $p_remove_dir : Path to remove in the filename path archived
  92. // Return Values :
  93. // 1 on success, or an error code (see table at the beginning).
  94. // --------------------------------------------------------------------------------
  95. function PclTarCreate($p_tarname, $p_filelist="", $p_mode="", $p_add_dir="", $p_remove_dir="")
  96. {
  97. TrFctStart(__FILE__, __LINE__, "PclTarCreate", "tar=$p_tarname, file='$p_filelist', mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  98. $v_result=1;
  99. // ----- Look for default mode
  100. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  101. {
  102. // ----- Extract the tar format from the extension
  103. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  104. {
  105. // ----- Return
  106. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  107. return PclErrorCode();
  108. }
  109. // ----- Trace
  110. TrFctMessage(__FILE__, __LINE__, 1, "Auto mode selected : found $p_mode");
  111. }
  112. // ----- Look if the $p_filelist is really an array
  113. if (is_array($p_filelist))
  114. {
  115. // ----- Call the create fct
  116. $v_result = PclTarHandleCreate($p_tarname, $p_filelist, $p_mode, $p_add_dir, $p_remove_dir);
  117. }
  118. // ----- Look if the $p_filelist is a string
  119. else if (is_string($p_filelist))
  120. {
  121. // ----- Create a list with the elements from the string
  122. $v_list = explode(" ", $p_filelist);
  123. // ----- Call the create fct
  124. $v_result = PclTarHandleCreate($p_tarname, $v_list, $p_mode, $p_add_dir, $p_remove_dir);
  125. }
  126. // ----- Invalid variable
  127. else
  128. {
  129. // ----- Error log
  130. PclErrorLog(-3, "Invalid variable type p_filelist");
  131. $v_result = -3;
  132. }
  133. // ----- Return
  134. TrFctEnd(__FILE__, __LINE__, $v_result);
  135. return $v_result;
  136. }
  137. // --------------------------------------------------------------------------------
  138. // --------------------------------------------------------------------------------
  139. // Function : PclTarAdd()
  140. // Description :
  141. // PLEASE DO NOT USE ANY MORE THIS FUNCTION. Use PclTarAddList().
  142. //
  143. // This function is maintained only for compatibility reason
  144. //
  145. // Parameters :
  146. // $p_tarname : Name of an existing tar file
  147. // $p_filelist : An array containing file or directory names, or
  148. // a string containing one filename or directory name, or
  149. // a string containing a list of filenames and/or directory
  150. // names separated by spaces.
  151. // Return Values :
  152. // 1 on success,
  153. // Or an error code (see list on top).
  154. // --------------------------------------------------------------------------------
  155. function PclTarAdd($p_tarname, $p_filelist)
  156. {
  157. TrFctStart(__FILE__, __LINE__, "PclTarAdd", "tar=$p_tarname, file=$p_filelist");
  158. $v_result=1;
  159. $v_list_detail = array();
  160. // ----- Extract the tar format from the extension
  161. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  162. {
  163. // ----- Return
  164. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  165. return PclErrorCode();
  166. }
  167. // ----- Look if the $p_filelist is really an array
  168. if (is_array($p_filelist))
  169. {
  170. // ----- Call the add fct
  171. $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $v_list_detail, "", "");
  172. }
  173. // ----- Look if the $p_filelist is a string
  174. else if (is_string($p_filelist))
  175. {
  176. // ----- Create a list with the elements from the string
  177. $v_list = explode(" ", $p_filelist);
  178. // ----- Call the add fct
  179. $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $v_list_detail, "", "");
  180. }
  181. // ----- Invalid variable
  182. else
  183. {
  184. // ----- Error log
  185. PclErrorLog(-3, "Invalid variable type p_filelist");
  186. $v_result = -3;
  187. }
  188. // ----- Cleaning
  189. unset($v_list_detail);
  190. // ----- Return
  191. TrFctEnd(__FILE__, __LINE__, $v_result);
  192. return $v_result;
  193. }
  194. // --------------------------------------------------------------------------------
  195. // --------------------------------------------------------------------------------
  196. // Function : PclTarAddList()
  197. // Description :
  198. // Add a list of files or directories ($p_filelist) in the tar archive $p_tarname.
  199. // The list can be an array of file/directory names or a string with names
  200. // separated by one space.
  201. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  202. // different from the real path of the file. This is usefull if you want to have PclTar
  203. // running in any directory, and memorize relative path from an other directory.
  204. // If $p_mode is not set it will be automatically computed from the $p_tarname
  205. // extension (.tar, .tar.gz or .tgz).
  206. // Parameters :
  207. // $p_tarname : Name of an existing tar file
  208. // $p_filelist : An array containing file or directory names, or
  209. // a string containing one filename or directory name, or
  210. // a string containing a list of filenames and/or directory
  211. // names separated by spaces.
  212. // $p_add_dir : Path to add in the filename path archived
  213. // $p_remove_dir : Path to remove in the filename path archived
  214. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  215. // Return Values :
  216. // 1 on success,
  217. // Or an error code (see list on top).
  218. // --------------------------------------------------------------------------------
  219. function PclTarAddList($p_tarname, $p_filelist, $p_add_dir="", $p_remove_dir="", $p_mode="")
  220. {
  221. TrFctStart(__FILE__, __LINE__, "PclTarAddList", "tar=$p_tarname, file=$p_filelist, p_add_dir='$p_add_dir', p_remove_dir='$p_remove_dir', mode=$p_mode");
  222. $v_result=1;
  223. $p_list_detail = array();
  224. // ----- Extract the tar format from the extension
  225. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  226. {
  227. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  228. {
  229. // ----- Return
  230. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  231. return PclErrorCode();
  232. }
  233. }
  234. // ----- Look if the $p_filelist is really an array
  235. if (is_array($p_filelist))
  236. {
  237. // ----- Call the add fct
  238. $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
  239. }
  240. // ----- Look if the $p_filelist is a string
  241. else if (is_string($p_filelist))
  242. {
  243. // ----- Create a list with the elements from the string
  244. $v_list = explode(" ", $p_filelist);
  245. // ----- Call the add fct
  246. $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
  247. }
  248. // ----- Invalid variable
  249. else
  250. {
  251. // ----- Error log
  252. PclErrorLog(-3, "Invalid variable type p_filelist");
  253. $v_result = -3;
  254. }
  255. // ----- Return
  256. if ($v_result != 1)
  257. {
  258. TrFctEnd(__FILE__, __LINE__, 0);
  259. return 0;
  260. }
  261. TrFctEnd(__FILE__, __LINE__, $p_list_detail);
  262. return $p_list_detail;
  263. }
  264. // --------------------------------------------------------------------------------
  265. // --------------------------------------------------------------------------------
  266. // Function : PclTarList()
  267. // Description :
  268. // Gives the list of all the files present in the tar archive $p_tarname.
  269. // The list is the function result, it will be 0 on error.
  270. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  271. // function will determine the type of the archive.
  272. // Parameters :
  273. // $p_tarname : Name of an existing tar file
  274. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  275. // Return Values :
  276. // 0 on error (Use PclErrorCode() and PclErrorString() for more info)
  277. // or
  278. // An array containing file properties. Each file properties is an array of
  279. // properties.
  280. // The properties (array field names) are :
  281. // filename, size, mode, uid, gid, mtime, typeflag, status
  282. // Exemple : $v_list = PclTarList("my.tar");
  283. // for ($i=0; $i<sizeof($v_list); $i++)
  284. // echo "Filename :'".$v_list[$i][filename]."'<br>";
  285. // --------------------------------------------------------------------------------
  286. function PclTarList($p_tarname, $p_mode="")
  287. {
  288. TrFctStart(__FILE__, __LINE__, "PclTarList", "tar=$p_tarname, mode='$p_mode'");
  289. $v_result=1;
  290. // ----- Extract the tar format from the extension
  291. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  292. {
  293. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  294. {
  295. // ----- Return
  296. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  297. return 0;
  298. }
  299. }
  300. // ----- Call the extracting fct
  301. $p_list = array();
  302. if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "list", "", $p_mode, "")) != 1)
  303. {
  304. unset($p_list);
  305. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  306. return(0);
  307. }
  308. // ----- Return
  309. TrFctEnd(__FILE__, __LINE__, $p_list);
  310. return $p_list;
  311. }
  312. // --------------------------------------------------------------------------------
  313. // --------------------------------------------------------------------------------
  314. // Function : PclTarExtract()
  315. // Description :
  316. // Extract all the files present in the archive $p_tarname, in the directory
  317. // $p_path. The relative path of the archived files are keep and become
  318. // relative to $p_path.
  319. // If a file with the same name already exists it will be replaced.
  320. // If the path to the file does not exist, it will be created.
  321. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  322. // function will determine the type of the archive.
  323. // Parameters :
  324. // $p_tarname : Name of an existing tar file.
  325. // $p_path : Path where the files will be extracted. The files will use
  326. // their memorized path from $p_path.
  327. // If $p_path is "", files will be extracted in "./".
  328. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  329. // extracted files. If the path does not match the file path,
  330. // the file is extracted with its memorized path.
  331. // $p_path and $p_remove_path are commulative.
  332. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  333. // Return Values :
  334. // Same as PclTarList()
  335. // --------------------------------------------------------------------------------
  336. function PclTarExtract($p_tarname, $p_path="./", $p_remove_path="", $p_mode="")
  337. {
  338. TrFctStart(__FILE__, __LINE__, "PclTarExtract", "tar='$p_tarname', path='$p_path', remove_path='$p_remove_path', mode='$p_mode'");
  339. $v_result=1;
  340. // ----- Extract the tar format from the extension
  341. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  342. {
  343. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  344. {
  345. // ----- Return
  346. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  347. return 0;
  348. }
  349. }
  350. // ----- Call the extracting fct
  351. if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "complete", $p_path, $v_tar_mode, $p_remove_path)) != 1)
  352. {
  353. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  354. return(0);
  355. }
  356. // ----- Return
  357. TrFctEnd(__FILE__, __LINE__, $p_list);
  358. return $p_list;
  359. }
  360. // --------------------------------------------------------------------------------
  361. // --------------------------------------------------------------------------------
  362. // Function : PclTarExtractList()
  363. // Description :
  364. // Extract the files present in the archive $p_tarname and specified in
  365. // $p_filelist, in the directory
  366. // $p_path. The relative path of the archived files are keep and become
  367. // relative to $p_path.
  368. // If a directory is spécified in the list, all the files from this directory
  369. // will be extracted.
  370. // If a file with the same name already exists it will be replaced.
  371. // If the path to the file does not exist, it will be created.
  372. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  373. // function will determine the type of the archive.
  374. // Parameters :
  375. // $p_tarname : Name of an existing tar file
  376. // $p_filelist : An array containing file or directory names, or
  377. // a string containing one filename or directory name, or
  378. // a string containing a list of filenames and/or directory
  379. // names separated by spaces.
  380. // $p_path : Path where the files will be extracted. The files will use
  381. // their memorized path from $p_path.
  382. // If $p_path is "", files will be extracted in "./".
  383. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  384. // extracted files. If the path does not match the file path,
  385. // the file is extracted with its memorized path.
  386. // $p_path and $p_remove_path are commulative.
  387. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  388. // Return Values :
  389. // Same as PclTarList()
  390. // --------------------------------------------------------------------------------
  391. function PclTarExtractList($p_tarname, $p_filelist, $p_path="./", $p_remove_path="", $p_mode="")
  392. {
  393. TrFctStart(__FILE__, __LINE__, "PclTarExtractList", "tar=$p_tarname, list, path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
  394. $v_result=1;
  395. // ----- Extract the tar format from the extension
  396. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  397. {
  398. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  399. {
  400. // ----- Return
  401. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  402. return 0;
  403. }
  404. }
  405. // ----- Look if the $p_filelist is really an array
  406. if (is_array($p_filelist))
  407. {
  408. // ----- Call the extracting fct
  409. if (($v_result = PclTarHandleExtract($p_tarname, $p_filelist, $p_list, "partial", $p_path, $v_tar_mode, $p_remove_path)) != 1)
  410. {
  411. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  412. return(0);
  413. }
  414. }
  415. // ----- Look if the $p_filelist is a string
  416. else if (is_string($p_filelist))
  417. {
  418. // ----- Create a list with the elements from the string
  419. $v_list = explode(" ", $p_filelist);
  420. // ----- Call the extracting fct
  421. if (($v_result = PclTarHandleExtract($p_tarname, $v_list, $p_list, "partial", $p_path, $v_tar_mode, $p_remove_path)) != 1)
  422. {
  423. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  424. return(0);
  425. }
  426. }
  427. // ----- Invalid variable
  428. else
  429. {
  430. // ----- Error log
  431. PclErrorLog(-3, "Invalid variable type p_filelist");
  432. // ----- Return
  433. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  434. return 0;
  435. }
  436. // ----- Return
  437. TrFctEnd(__FILE__, __LINE__, $p_list);
  438. return $p_list;
  439. }
  440. // --------------------------------------------------------------------------------
  441. // --------------------------------------------------------------------------------
  442. // Function : PclTarExtractIndex()
  443. // Description :
  444. // Extract the files present in the archive $p_tarname and specified at
  445. // the indexes in $p_index, in the directory
  446. // $p_path. The relative path of the archived files are keep and become
  447. // relative to $p_path.
  448. // If a directory is specified in the list, the directory only is created. All
  449. // the file stored in this archive for this directory
  450. // are not extracted.
  451. // If a file with the same name already exists it will be replaced.
  452. // If the path to the file does not exist, it will be created.
  453. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  454. // function will determine the type of the archive.
  455. // Parameters :
  456. // $p_tarname : Name of an existing tar file
  457. // $p_index : A single index (integer) or a string of indexes of files to
  458. // extract. The form of the string is "0,4-6,8-12" with only numbers
  459. // and '-' for range or ',' to separate ranges. No spaces or ';'
  460. // are allowed.
  461. // $p_path : Path where the files will be extracted. The files will use
  462. // their memorized path from $p_path.
  463. // If $p_path is "", files will be extracted in "./".
  464. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  465. // extracted files. If the path does not match the file path,
  466. // the file is extracted with its memorized path.
  467. // $p_path and $p_remove_path are commulative.
  468. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  469. // Return Values :
  470. // Same as PclTarList()
  471. // --------------------------------------------------------------------------------
  472. function PclTarExtractIndex($p_tarname, $p_index, $p_path="./", $p_remove_path="", $p_mode="")
  473. {
  474. TrFctStart(__FILE__, __LINE__, "PclTarExtractIndex", "tar=$p_tarname, index='$p_index', path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
  475. $v_result=1;
  476. // ----- Extract the tar format from the extension
  477. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  478. {
  479. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  480. {
  481. // ----- Return
  482. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  483. return 0;
  484. }
  485. }
  486. // ----- Look if the $p_index is really an integer
  487. if (is_integer($p_index))
  488. {
  489. // ----- Call the extracting fct
  490. if (($v_result = PclTarHandleExtractByIndexList($p_tarname, "$p_index", $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1)
  491. {
  492. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  493. return(0);
  494. }
  495. }
  496. // ----- Look if the $p_filelist is a string
  497. else if (is_string($p_index))
  498. {
  499. // ----- Call the extracting fct
  500. if (($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1)
  501. {
  502. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  503. return(0);
  504. }
  505. }
  506. // ----- Invalid variable
  507. else
  508. {
  509. // ----- Error log
  510. PclErrorLog(-3, "Invalid variable type $p_index");
  511. // ----- Return
  512. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  513. return 0;
  514. }
  515. // ----- Return
  516. TrFctEnd(__FILE__, __LINE__, $p_list);
  517. return $p_list;
  518. }
  519. // --------------------------------------------------------------------------------
  520. // --------------------------------------------------------------------------------
  521. // Function : PclTarDelete()
  522. // Description :
  523. // This function deletes from the archive $p_tarname the files which are listed
  524. // in $p_filelist. $p_filelist can be a string with file names separated by
  525. // spaces, or an array containing the file names.
  526. // Parameters :
  527. // $p_tarname : Name of an existing tar file
  528. // $p_filelist : An array or a string containing file names to remove from the
  529. // archive.
  530. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  531. // Return Values :
  532. // List of the files which are kept in the archive (same format as PclTarList())
  533. // --------------------------------------------------------------------------------
  534. function PclTarDelete($p_tarname, $p_filelist, $p_mode="")
  535. {
  536. TrFctStart(__FILE__, __LINE__, "PclTarDelete", "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
  537. $v_result=1;
  538. // ----- Extract the tar format from the extension
  539. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  540. {
  541. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  542. {
  543. // ----- Return
  544. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  545. return 0;
  546. }
  547. }
  548. // ----- Look if the $p_filelist is really an array
  549. if (is_array($p_filelist))
  550. {
  551. // ----- Call the extracting fct
  552. if (($v_result = PclTarHandleDelete($p_tarname, $p_filelist, $p_list, $p_mode)) != 1)
  553. {
  554. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  555. return(0);
  556. }
  557. }
  558. // ----- Look if the $p_filelist is a string
  559. else if (is_string($p_filelist))
  560. {
  561. // ----- Create a list with the elements from the string
  562. $v_list = explode(" ", $p_filelist);
  563. // ----- Call the extracting fct
  564. if (($v_result = PclTarHandleDelete($p_tarname, $v_list, $p_list, $p_mode)) != 1)
  565. {
  566. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  567. return(0);
  568. }
  569. }
  570. // ----- Invalid variable
  571. else
  572. {
  573. // ----- Error log
  574. PclErrorLog(-3, "Invalid variable type p_filelist");
  575. // ----- Return
  576. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  577. return 0;
  578. }
  579. // ----- Return
  580. TrFctEnd(__FILE__, __LINE__, $p_list);
  581. return $p_list;
  582. }
  583. // --------------------------------------------------------------------------------
  584. // --------------------------------------------------------------------------------
  585. // Function : PclTarUpdate()
  586. // Description :
  587. // This function updates the files in $p_filelist which are already in the
  588. // $p_tarname archive with an older last modified date. If the file does not
  589. // exist, it is added at the end of the archive.
  590. // Parameters :
  591. // $p_tarname : Name of an existing tar file
  592. // $p_filelist : An array or a string containing file names to update from the
  593. // archive.
  594. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  595. // Return Values :
  596. // List of the files contained in the archive. The field status contains
  597. // "updated", "not_updated", "added" or "ok" for the files not concerned.
  598. // --------------------------------------------------------------------------------
  599. function PclTarUpdate($p_tarname, $p_filelist, $p_mode="", $p_add_dir="", $p_remove_dir="")
  600. {
  601. TrFctStart(__FILE__, __LINE__, "PclTarUpdate", "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
  602. $v_result=1;
  603. // ----- Extract the tar format from the extension
  604. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  605. {
  606. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  607. {
  608. // ----- Return
  609. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  610. return 0;
  611. }
  612. }
  613. // ----- Look if the $p_filelist is really an array
  614. if (is_array($p_filelist))
  615. {
  616. // ----- Call the extracting fct
  617. if (($v_result = PclTarHandleUpdate($p_tarname, $p_filelist, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1)
  618. {
  619. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  620. return(0);
  621. }
  622. }
  623. // ----- Look if the $p_filelist is a string
  624. else if (is_string($p_filelist))
  625. {
  626. // ----- Create a list with the elements from the string
  627. $v_list = explode(" ", $p_filelist);
  628. // ----- Call the extracting fct
  629. if (($v_result = PclTarHandleUpdate($p_tarname, $v_list, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1)
  630. {
  631. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  632. return(0);
  633. }
  634. }
  635. // ----- Invalid variable
  636. else
  637. {
  638. // ----- Error log
  639. PclErrorLog(-3, "Invalid variable type p_filelist");
  640. // ----- Return
  641. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  642. return 0;
  643. }
  644. // ----- Return
  645. TrFctEnd(__FILE__, __LINE__, $p_list);
  646. return $p_list;
  647. }
  648. // --------------------------------------------------------------------------------
  649. // --------------------------------------------------------------------------------
  650. // Function : PclTarMerge()
  651. // Description :
  652. // This function add the content of $p_tarname_add at the end of $p_tarname.
  653. // Parameters :
  654. // $p_tarname : Name of an existing tar file
  655. // $p_tarname_add : Name of an existing tar file taht will be added at the end
  656. // of $p_tarname.
  657. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  658. // $p_mode_add : 'tar' or 'tgz', if not set, will be determined by $p_tarname_add
  659. // extension
  660. // Return Values :
  661. // List of the files contained in the archive. The field status contains
  662. // "updated", "not_updated", "added" or "ok" for the files not concerned.
  663. // --------------------------------------------------------------------------------
  664. function PclTarMerge($p_tarname, $p_tarname_add, $p_mode="", $p_mode_add="")
  665. {
  666. TrFctStart(__FILE__, __LINE__, "PclTarMerge", "tar='$p_tarname', tar_add='$p_tarname_add', mode='$p_mode', mode_add='$p_mode_add'");
  667. $v_result=1;
  668. // ----- Check the parameters
  669. if (($p_tarname == "") || ($p_tarname_add == ""))
  670. {
  671. // ----- Error log
  672. PclErrorLog(-3, "Invalid empty archive name");
  673. // ----- Return
  674. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  675. return PclErrorCode();
  676. }
  677. // ----- Extract the tar format from the extension
  678. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  679. {
  680. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  681. {
  682. // ----- Return
  683. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  684. return 0;
  685. }
  686. }
  687. if (($p_mode_add == "") || (($p_mode_add!="tar") && ($p_mode_add!="tgz")))
  688. {
  689. if (($p_mode_add = PclTarHandleExtension($p_tarname_add)) == "")
  690. {
  691. // ----- Return
  692. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  693. return 0;
  694. }
  695. }
  696. // ----- Clear filecache
  697. clearstatcache();
  698. // ----- Check the file size
  699. if ((!is_file($p_tarname)) ||
  700. (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar")))
  701. {
  702. // ----- Error log
  703. if (!is_file($p_tarname))
  704. PclErrorLog(-4, "Archive '$p_tarname' does not exist");
  705. else
  706. PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)");
  707. // ----- Return
  708. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  709. return PclErrorCode();
  710. }
  711. if ((!is_file($p_tarname_add)) ||
  712. (((($v_size_add = filesize($p_tarname_add)) % 512) != 0) && ($p_mode_add=="tar")))
  713. {
  714. // ----- Error log
  715. if (!is_file($p_tarname_add))
  716. PclErrorLog(-4, "Archive '$p_tarname_add' does not exist");
  717. else
  718. PclErrorLog(-6, "Archive '$p_tarname_add' has invalid size ".filesize($p_tarname_add)."(not a 512 block multiple)");
  719. // ----- Return
  720. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  721. return PclErrorCode();
  722. }
  723. // ----- Look for compressed archive
  724. if ($p_mode == "tgz")
  725. {
  726. // ----- Open the file in read mode
  727. if (($p_tar = @gzopen($p_tarname, "rb")) == 0)
  728. {
  729. // ----- Error log
  730. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  731. // ----- Return
  732. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  733. return PclErrorCode();
  734. }
  735. // ----- Open a temporary file in write mode
  736. $v_temp_tarname = uniqid("pcltar-").".tmp";
  737. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  738. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  739. {
  740. // ----- Close tar file
  741. gzclose($p_tar);
  742. // ----- Error log
  743. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  744. // ----- Return
  745. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  746. return PclErrorCode();
  747. }
  748. // ----- Read the first 512 bytes block
  749. $v_buffer = gzread($p_tar, 512);
  750. // ----- Read the following blocks but not the last one
  751. if (!gzeof($p_tar))
  752. {
  753. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  754. $i=1;
  755. // ----- Read new 512 block and write the already read
  756. do{
  757. // ----- Write the already read block
  758. $v_binary_data = pack("a512", "$v_buffer");
  759. gzputs($v_temp_tar, $v_binary_data);
  760. $i++;
  761. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  762. // ----- Read next block
  763. $v_buffer = gzread($p_tar, 512);
  764. } while (!gzeof($p_tar));
  765. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  766. }
  767. }
  768. // ----- Look for uncompressed tar file
  769. else if ($p_mode=="tar")
  770. {
  771. // ----- Open the tar file
  772. if (($p_tar = fopen($p_tarname, "r+b")) == 0)
  773. {
  774. // ----- Error log
  775. PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
  776. // ----- Return
  777. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  778. return PclErrorCode();
  779. }
  780. // ----- Go to the beginning of last block
  781. TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  782. fseek($p_tar, $v_size-512);
  783. TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  784. }
  785. // ----- Look for unknown type
  786. else
  787. {
  788. // ----- Error log
  789. PclErrorLog(-3, "Invalid tar mode $p_mode");
  790. // ----- Return
  791. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  792. return PclErrorCode();
  793. }
  794. // ----- Look for type of archive to add
  795. if ($p_mode_add == "tgz")
  796. {
  797. TrFctMessage(__FILE__, __LINE__, 4, "Opening file $p_tarname_add");
  798. // ----- Open the file in read mode
  799. if (($p_tar_add = @gzopen($p_tarname_add, "rb")) == 0)
  800. {
  801. // ----- Error log
  802. PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
  803. // ----- Return
  804. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  805. return PclErrorCode();
  806. }
  807. // ----- Read the first 512 bytes block
  808. $v_buffer = gzread($p_tar_add, 512);
  809. // ----- Read the following blocks but not the last one
  810. if (!gzeof($p_tar_add))
  811. {
  812. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  813. $i=1;
  814. // ----- Read new 512 block and write the already read
  815. do{
  816. // ----- Write the already read block
  817. $v_binary_data = pack("a512", "$v_buffer");
  818. if ($p_mode=="tar")
  819. fputs($p_tar, $v_binary_data);
  820. else
  821. gzputs($v_temp_tar, $v_binary_data);
  822. $i++;
  823. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  824. // ----- Read next block
  825. $v_buffer = gzread($p_tar_add, 512);
  826. } while (!gzeof($p_tar_add));
  827. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  828. }
  829. // ----- Close the files
  830. gzclose($p_tar_add);
  831. }
  832. // ----- Look for uncompressed tar file
  833. else if ($p_mode=="tar")
  834. {
  835. // ----- Open the file in read mode
  836. if (($p_tar_add = @fopen($p_tarname_add, "rb")) == 0)
  837. {
  838. // ----- Error log
  839. PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
  840. // ----- Return
  841. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  842. return PclErrorCode();
  843. }
  844. // ----- Read the first 512 bytes block
  845. $v_buffer = fread($p_tar_add, 512);
  846. // ----- Read the following blocks but not the last one
  847. if (!feof($p_tar_add))
  848. {
  849. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  850. $i=1;
  851. // ----- Read new 512 block and write the already read
  852. do{
  853. // ----- Write the already read block
  854. $v_binary_data = pack("a512", "$v_buffer");
  855. if ($p_mode=="tar")
  856. fputs($p_tar, $v_binary_data);
  857. else
  858. gzputs($v_temp_tar, $v_binary_data);
  859. $i++;
  860. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  861. // ----- Read next block
  862. $v_buffer = fread($p_tar_add, 512);
  863. } while (!feof($p_tar_add));
  864. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  865. }
  866. // ----- Close the files
  867. fclose($p_tar_add);
  868. }
  869. // ----- Call the footer of the tar archive
  870. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  871. // ----- Look for closing compressed archive
  872. if ($p_mode == "tgz")
  873. {
  874. // ----- Close the files
  875. gzclose($p_tar);
  876. gzclose($v_temp_tar);
  877. // ----- Unlink tar file
  878. if (!@unlink($p_tarname))
  879. {
  880. // ----- Error log
  881. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  882. }
  883. // ----- Rename tar file
  884. if (!@rename($v_temp_tarname, $p_tarname))
  885. {
  886. // ----- Error log
  887. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  888. // ----- Return
  889. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  890. return PclErrorCode();
  891. }
  892. // ----- Return
  893. TrFctEnd(__FILE__, __LINE__, $v_result);
  894. return $v_result;
  895. }
  896. // ----- Look for closing uncompressed tar file
  897. else if ($p_mode=="tar")
  898. {
  899. // ----- Close the tarfile
  900. fclose($p_tar);
  901. }
  902. // ----- Return
  903. TrFctEnd(__FILE__, __LINE__, $v_result);
  904. return $v_result;
  905. }
  906. // --------------------------------------------------------------------------------
  907. // --------------------------------------------------------------------------------
  908. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  909. // ***** *****
  910. // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
  911. // --------------------------------------------------------------------------------
  912. // --------------------------------------------------------------------------------
  913. // Function : PclTarHandleCreate()
  914. // Description :
  915. // Parameters :
  916. // $p_tarname : Name of the tar file
  917. // $p_list : An array containing the file or directory names to add in the tar
  918. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  919. // Return Values :
  920. // --------------------------------------------------------------------------------
  921. function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir="", $p_remove_dir="")
  922. {
  923. TrFctStart(__FILE__, __LINE__, "PclTarHandleCreate", "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  924. $v_result=1;
  925. $v_list_detail = array();
  926. // ----- Check the parameters
  927. if (($p_tarname == "") || (($p_mode != "tar") && ($p_mode != "tgz")))
  928. {
  929. // ----- Error log
  930. if ($p_tarname == "")
  931. PclErrorLog(-3, "Invalid empty archive name");
  932. else
  933. PclErrorLog(-3, "Unknown mode '$p_mode'");
  934. // ----- Return
  935. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  936. return PclErrorCode();
  937. }
  938. // ----- Look for tar file
  939. if ($p_mode == "tar")
  940. {
  941. // ----- Open the tar file
  942. if (($p_tar = fopen($p_tarname, "wb")) == 0)
  943. {
  944. // ----- Error log
  945. PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
  946. // ----- Return
  947. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  948. return PclErrorCode();
  949. }
  950. // ----- Call the adding fct inside the tar
  951. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  952. {
  953. // ----- Call the footer of the tar archive
  954. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  955. }
  956. // ----- Close the tarfile
  957. fclose($p_tar);
  958. }
  959. // ----- Look for tgz file
  960. else
  961. {
  962. // ----- Open the tar file
  963. if (($p_tar = @gzopen($p_tarname, "wb")) == 0)
  964. {
  965. // ----- Error log
  966. PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
  967. // ----- Return
  968. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  969. return PclErrorCode();
  970. }
  971. // ----- Call the adding fct inside the tar
  972. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  973. {
  974. // ----- Call the footer of the tar archive
  975. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  976. }
  977. // ----- Close the tarfile
  978. gzclose($p_tar);
  979. }
  980. // ----- Return
  981. TrFctEnd(__FILE__, __LINE__, $v_result);
  982. return $v_result;
  983. }
  984. // --------------------------------------------------------------------------------
  985. // --------------------------------------------------------------------------------
  986. // Function : PclTarHandleAppend()
  987. // Description :
  988. // Parameters :
  989. // $p_tarname : Name of the tar file
  990. // $p_list : An array containing the file or directory names to add in the tar
  991. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  992. // Return Values :
  993. // --------------------------------------------------------------------------------
  994. function PclTarHandleAppend($p_tarname, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
  995. {
  996. TrFctStart(__FILE__, __LINE__, "PclTarHandleAppend", "tar=$p_tarname, list, mode=$p_mode");
  997. $v_result=1;
  998. // ----- Check the parameters
  999. if ($p_tarname == "")
  1000. {
  1001. // ----- Error log
  1002. PclErrorLog(-3, "Invalid empty archive name");
  1003. // ----- Return
  1004. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1005. return PclErrorCode();
  1006. }
  1007. clearstatcache();
  1008. // ----- Check the file size
  1009. if ((!is_file($p_tarname)) ||
  1010. (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar")))
  1011. {
  1012. // ----- Error log
  1013. if (!is_file($p_tarname))
  1014. PclErrorLog(-4, "Archive '$p_tarname' does not exist");
  1015. else
  1016. PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)");
  1017. // ----- Return
  1018. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1019. return PclErrorCode();
  1020. }
  1021. // ----- Look for compressed archive
  1022. if ($p_mode == "tgz")
  1023. {
  1024. // ----- Open the file in read mode
  1025. if (($p_tar = @gzopen($p_tarname, "rb")) == 0)
  1026. {
  1027. // ----- Error log
  1028. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  1029. // ----- Return
  1030. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1031. return PclErrorCode();
  1032. }
  1033. // ----- Open a temporary file in write mode
  1034. $v_temp_tarname = uniqid("pcltar-").".tmp";
  1035. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  1036. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  1037. {
  1038. // ----- Close tar file
  1039. gzclose($p_tar);
  1040. // ----- Error log
  1041. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  1042. // ----- Return
  1043. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1044. return PclErrorCode();
  1045. }
  1046. // ----- Read the first 512 bytes block
  1047. $v_buffer = gzread($p_tar, 512);
  1048. // ----- Read the following blocks but not the last one
  1049. if (!gzeof($p_tar))
  1050. {
  1051. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  1052. $i=1;
  1053. // ----- Read new 512 block and write the already read
  1054. do{
  1055. // ----- Write the already read block
  1056. $v_binary_data = pack("a512", "$v_buffer");
  1057. gzputs($v_temp_tar, $v_binary_data);
  1058. $i++;
  1059. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  1060. // ----- Read next block
  1061. $v_buffer = gzread($p_tar, 512);
  1062. } while (!gzeof($p_tar));
  1063. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  1064. }
  1065. // ----- Call the adding fct inside the tar
  1066. if (($v_result = PclTarHandleAddList($v_temp_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  1067. {
  1068. // ----- Call the footer of the tar archive
  1069. $v_result = PclTarHandleFooter($v_temp_tar, $p_mode);
  1070. }
  1071. // ----- Close the files
  1072. gzclose($p_tar);
  1073. gzclose($v_temp_tar);
  1074. // ----- Unlink tar file
  1075. if (!@unlink($p_tarname))
  1076. {
  1077. // ----- Error log
  1078. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  1079. }
  1080. // ----- Rename tar file
  1081. if (!@rename($v_temp_tarname, $p_tarname))
  1082. {
  1083. // ----- Error log
  1084. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  1085. // ----- Return
  1086. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1087. return PclErrorCode();
  1088. }
  1089. // ----- Return
  1090. TrFctEnd(__FILE__, __LINE__, $v_result);
  1091. return $v_result;
  1092. }
  1093. // ----- Look for uncompressed tar file
  1094. else if ($p_mode=="tar")
  1095. {
  1096. // ----- Open the tar file
  1097. if (($p_tar = fopen($p_tarname, "r+b")) == 0)
  1098. {
  1099. // ----- Error log
  1100. PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
  1101. // ----- Return
  1102. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1103. return PclErrorCode();
  1104. }
  1105. // ----- Go to the beginning of last block
  1106. TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1107. fseek($p_tar, $v_size-512);
  1108. TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1109. // ----- Call the adding fct inside the tar
  1110. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  1111. {
  1112. // ----- Call the footer of the tar archive
  1113. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  1114. }
  1115. // ----- Close the tarfile
  1116. fclose($p_tar);
  1117. }
  1118. // ----- Look for unknown type
  1119. else
  1120. {
  1121. // ----- Error log
  1122. PclErrorLog(-3, "Invalid tar mode $p_mode");
  1123. // ----- Return
  1124. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1125. return PclErrorCode();
  1126. }
  1127. // ----- Return
  1128. TrFctEnd(__FILE__, __LINE__, $v_result);
  1129. return $v_result;
  1130. }
  1131. // --------------------------------------------------------------------------------
  1132. // --------------------------------------------------------------------------------
  1133. // Function : PclTarHandleAddList()
  1134. // Description :
  1135. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  1136. // different from the real path of the file. This is usefull if you want to have PclTar
  1137. // running in any directory, and memorize relative path from an other directory.
  1138. // Parameters :
  1139. // $p_tar : File descriptor of the tar archive
  1140. // $p_list : An array containing the file or directory names to add in the tar
  1141. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  1142. // $p_list_detail : list of added files with their properties (specially the status field)
  1143. // $p_add_dir : Path to add in the filename path archived
  1144. // $p_remove_dir : Path to remove in the filename path archived
  1145. // Return Values :
  1146. // --------------------------------------------------------------------------------
  1147. function PclTarHandleAddList($p_tar, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
  1148. {
  1149. TrFctStart(__FILE__, __LINE__, "PclTarHandleAddList", "tar='$p_tar', list, mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  1150. $v_result=1;
  1151. $v_header = array();
  1152. // ----- Recuperate the current number of elt in list
  1153. $v_nb = sizeof($p_list_detail);
  1154. // ----- Check the parameters
  1155. if ($p_tar == 0)
  1156. {
  1157. // ----- Error log
  1158. PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
  1159. // ----- Return
  1160. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1161. return PclErrorCode();
  1162. }
  1163. // ----- Check the arguments
  1164. if (sizeof($p_list) == 0)
  1165. {
  1166. // ----- Error log
  1167. PclErrorLog(-3, "Invalid file list parameter (invalid or empty list)");
  1168. // ----- Return
  1169. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1170. return PclErrorCode();
  1171. }
  1172. // ----- Loop on the files
  1173. for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++)
  1174. {
  1175. // ----- Recuperate the filename
  1176. $p_filename = $p_list[$j];
  1177. TrFctMessage(__FILE__, __LINE__, 2, "Looking for file [$p_filename]");
  1178. // ----- Skip empty file names
  1179. if ($p_filename == "")
  1180. {
  1181. TrFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
  1182. continue;
  1183. }
  1184. // ----- Check the filename
  1185. if (!file_exists($p_filename))
  1186. {
  1187. // ----- Error log
  1188. TrFctMessage(__FILE__, __LINE__, 2, "File '$p_filename' does not exists");
  1189. PclErrorLog(-4, "File '$p_filename' does not exists");
  1190. // ----- Return
  1191. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1192. return PclErrorCode();
  1193. }
  1194. // ----- Check the path length
  1195. if (strlen($p_filename) > 99)
  1196. {
  1197. // ----- Error log
  1198. PclErrorLog(-5, "File name is too long (max. 99) : '$p_filename'");
  1199. // ----- Return
  1200. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1201. return PclErrorCode();
  1202. }
  1203. TrFctMessage(__FILE__, __LINE__, 4, "File position before header =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1204. // ----- Add the file
  1205. if (($v_result = PclTarHandleAddFile($p_tar, $p_filename, $p_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
  1206. {
  1207. // ----- Return status
  1208. TrFctEnd(__FILE__, __LINE__, $v_result);
  1209. return $v_result;
  1210. }
  1211. // ----- Store the file infos
  1212. $p_list_detail[$v_nb++] = $v_header;
  1213. // ----- Look for directory
  1214. if (is_dir($p_filename))
  1215. {
  1216. TrFctMessage(__FILE__, __LINE__, 2, "$p_filename is a directory");
  1217. // ----- Look for path
  1218. if ($p_filename != ".")
  1219. $v_path = $p_filename."/";
  1220. else
  1221. $v_path = "";
  1222. // ----- Read the directory for files and sub-directories
  1223. $p_hdir = opendir($p_filename);
  1224. $p_hitem = readdir($p_hdir); // '.' directory
  1225. $p_hitem = readdir($p_hdir); // '..' directory
  1226. while ($p_hitem = readdir($p_hdir))
  1227. {
  1228. // ----- Look for a file
  1229. if (is_file($v_path.$p_hitem))
  1230. {
  1231. TrFctMessage(__FILE__, __LINE__, 4, "Add the file '".$v_path.$p_hitem."'");
  1232. // ----- Add the file
  1233. if (($v_result = PclTarHandleAddFile($p_tar, $v_path.$p_hitem, $p_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
  1234. {
  1235. // ----- Return status
  1236. TrFctEnd(__FILE__, __LINE__, $v_result);
  1237. return $v_result;
  1238. }
  1239. // ----- Store the file infos
  1240. $p_list_detail[$v_nb++] = $v_header;
  1241. }
  1242. // ----- Recursive call to PclTarHandleAddFile()
  1243. else
  1244. {
  1245. TrFctMessage(__FILE__, __LINE__, 4, "'".$v_path.$p_hitem."' is a directory");
  1246. // ----- Need an array as parameter
  1247. $p_temp_list[0] = $v_path.$p_hitem;
  1248. $v_result = PclTarHandleAddList($p_tar, $p_temp_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
  1249. }
  1250. }
  1251. // ----- Free memory for the recursive loop
  1252. unset($p_temp_list);
  1253. unset($p_hdir);
  1254. unset($p_hitem);
  1255. }
  1256. else
  1257. {
  1258. TrFctMessage(__FILE__, __LINE__, 4, "File position after blocks =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1259. }
  1260. }
  1261. // ----- Return
  1262. TrFctEnd(__FILE__, __LINE__, $v_result);
  1263. return $v_result;
  1264. }
  1265. // --------------------------------------------------------------------------------
  1266. // --------------------------------------------------------------------------------
  1267. // Function : PclTarHandleAddFile()
  1268. // Description :
  1269. // Parameters :
  1270. // Return Values :
  1271. // --------------------------------------------------------------------------------
  1272. function PclTarHandleAddFile($p_tar, $p_filename, $p_mode, &$p_header, $p_add_dir, $p_remove_dir)
  1273. {
  1274. TrFctStart(__FILE__, __LINE__, "PclTarHandleAddFile", "tar='$p_tar', filename='$p_filename', p_mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  1275. $v_result=1;
  1276. // ----- Check the parameters
  1277. if ($p_tar == 0)
  1278. {
  1279. // ----- Error log
  1280. PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
  1281. // ----- Return
  1282. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1283. return PclErrorCode();
  1284. }
  1285. // ----- Skip empty file names
  1286. if ($p_filename == "")
  1287. {
  1288. // ----- Error log
  1289. PclErrorLog(-3, "Invalid file list parameter (invalid or empty list)");
  1290. // ----- Return
  1291. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1292. return PclErrorCode();
  1293. }
  1294. // ----- Calculate the stored filename
  1295. $v_stored_filename = $p_filename;
  1296. if ($p_remove_dir != "")
  1297. {
  1298. if (substr($p_remove_dir, -1) != '/')
  1299. $p_remove_dir .= "/";
  1300. if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./"))
  1301. {
  1302. if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./"))
  1303. $p_remove_dir = "./".$p_remove_dir;
  1304. if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./"))
  1305. $p_remove_dir = substr($p_remove_dir, 2);
  1306. }
  1307. if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
  1308. {
  1309. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  1310. TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_filename' = '$v_stored_filename'");
  1311. }
  1312. }
  1313. if ($p_add_dir != "")
  1314. {
  1315. if (substr($p_add_dir, -1) == "/")
  1316. $v_stored_filename = $p_add_dir.$v_stored_filename;
  1317. else
  1318. $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  1319. TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
  1320. }
  1321. // ----- Check the path length
  1322. if (strlen($v_stored_filename) > 99)
  1323. {
  1324. // ----- Error log
  1325. PclErrorLog(-5, "Stored file name is too long (max. 99) : '$v_stored_filename'");
  1326. // ----- Return
  1327. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1328. return PclErrorCode();
  1329. }
  1330. // ----- Look for a file
  1331. if (is_file($p_filename))
  1332. {
  1333. // ----- Open the source file
  1334. if (($v_file = fopen($p_filename, "rb")) == 0)
  1335. {
  1336. // ----- Error log
  1337. PclErrorLog(-2, "Unable to open file '$p_filename' in binary read mode");
  1338. // ----- Return
  1339. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1340. return PclErrorCode();
  1341. }
  1342. // ----- Call the header generation
  1343. if (($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename)) != 1)
  1344. {
  1345. // ----- Return status
  1346. TrFctEnd(__FILE__, __LINE__, $v_result);
  1347. return $v_result;
  1348. }
  1349. TrFctMessage(__FILE__, __LINE__, 4, "File position after header =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1350. // ----- Read the file by 512 octets blocks
  1351. $i=0;
  1352. while (($v_buffer = fread($v_file, 512)) != "")
  1353. {
  1354. $v_binary_data = pack("a512", "$v_buffer");
  1355. if ($p_mode == "tar")
  1356. fputs($p_tar, $v_binary_data);
  1357. else
  1358. gzputs($p_tar, $v_binary_data);
  1359. $i++;
  1360. }
  1361. TrFctMessage(__FILE__, __LINE__, 2, "$i 512 bytes blocks");
  1362. // ----- Close the file
  1363. fclose($v_file);
  1364. TrFctMessage(__FILE__, __LINE__, 4, "File position after blocks =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1365. }
  1366. // ----- Look for a directory
  1367. else
  1368. {
  1369. // ----- Call the header generation
  1370. if (($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename)) != 1)
  1371. {
  1372. // ----- Return status
  1373. TrFctEnd(__FILE__, __LINE__, $v_result);
  1374. return $v_result;
  1375. }
  1376. TrFctMessage(__FILE__, __LINE__, 4, "File position after header =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1377. }
  1378. // ----- Return
  1379. TrFctEnd(__FILE__, __LINE__, $v_result);
  1380. return $v_result;
  1381. }
  1382. // --------------------------------------------------------------------------------
  1383. // --------------------------------------------------------------------------------
  1384. // Function : PclTarHandleHeader()
  1385. // Description :
  1386. // This function creates in the TAR $p_tar, the TAR header for the file
  1387. // $p_filename.
  1388. //
  1389. // 1. The informations needed to compose the header are recuperated and formatted
  1390. // 2. Two binary strings are composed for the first part of the header, before
  1391. // and after checksum field.
  1392. // 3. The checksum is calculated from the two binary strings
  1393. // 4. The header is write in the tar file (first binary string, binary string
  1394. // for checksum and last binary string).
  1395. // Parameters :
  1396. // $p_tar : a valid file descriptor, opened in write mode,
  1397. // $p_filename : The name of the file the header is for,
  1398. // $p_mode : The mode of the archive ("tar" or "tgz").
  1399. // $p_header : A pointer to a array where will be set the file properties
  1400. // Return Values :
  1401. // --------------------------------------------------------------------------------
  1402. function PclTarHandleHeader($p_tar, $p_filename, $p_mode, &$p_header, $p_stored_filename)
  1403. {
  1404. TrFctStart(__FILE__, __LINE__, "PclTarHandleHeader", "tar=$p_tar, file='$p_filename', mode='$p_mode', stored_filename='$p_stored_filename'");
  1405. $v_result=1;
  1406. // ----- Check the parameters
  1407. if (($p_tar == 0) || ($p_filename == ""))
  1408. {
  1409. // ----- Error log
  1410. PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
  1411. // ----- Return
  1412. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1413. return PclErrorCode();
  1414. }
  1415. // ----- Filename (reduce the path of stored name)
  1416. if ($p_stored_filename == "")
  1417. $p_stored_filename = $p_filename;
  1418. $v_reduce_filename = PclTarHandlePathReduction($p_stored_filename);
  1419. TrFctMessage(__FILE__, __LINE__, 2, "Filename (reduced) '$v_reduce_filename', strlen ".strlen($v_reduce_filename));
  1420. // ----- Get file info
  1421. $v_info = stat($p_filename);
  1422. $v_uid = sprintf("%6s ", DecOct($v_info[4]));
  1423. $v_gid = sprintf("%6s ", DecOct($v_info[5]));
  1424. TrFctMessage(__FILE__, __LINE__, 3, "uid=$v_uid, gid=$v_gid");
  1425. $v_perms = sprintf("%6s ", DecOct(fileperms($p_filename)));
  1426. TrFctMessage(__FILE__, __LINE__, 3, "file permissions $v_perms");
  1427. // ----- File mtime
  1428. $v_mtime_data = filemtime($p_filename);
  1429. TrFctMessage(__FILE__, __LINE__, 2, "File mtime : $v_mtime_data");
  1430. $v_mtime = sprintf("%11s", DecOct($v_mtime_data));
  1431. // ----- File typeflag
  1432. // '0' or '\0' is the code for regular file
  1433. // '5' is directory
  1434. if (is_dir($p_filename))
  1435. {
  1436. $v_typeflag = "5";
  1437. $v_size = 0;
  1438. }
  1439. else
  1440. {
  1441. $v_typeflag = "";
  1442. // ----- Get the file size
  1443. clearstatcache();
  1444. $v_size = filesize($p_filename);
  1445. }
  1446. TrFctMessage(__FILE__, __LINE__, 2, "File size : $v_size");
  1447. $v_size = sprintf("%11s ", DecOct($v_size));
  1448. TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : $v_typeflag");
  1449. // ----- Linkname
  1450. $v_linkname = "";
  1451. // ----- Magic
  1452. $v_magic = "";
  1453. // ----- Version
  1454. $v_version = "";
  1455. // ----- uname
  1456. $v_uname = "";
  1457. // ----- gname
  1458. $v_gname = "";
  1459. // ----- devmajor
  1460. $v_devmajor = "";
  1461. // ----- devminor
  1462. $v_devminor = "";
  1463. // ----- prefix
  1464. $v_prefix = "";
  1465. // ----- Compose the binary string of the header in two parts arround the checksum position
  1466. $v_binary_data_first = pack("a100a8a8a8a12A12", $v_reduce_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
  1467. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, "");
  1468. // ----- Calculate the checksum
  1469. $v_checksum = 0;
  1470. // ..... First part of the header
  1471. for ($i=0; $i<148; $i++)
  1472. {
  1473. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1474. }
  1475. // ..... Ignore the checksum value and replace it by ' ' (space)
  1476. for ($i=148; $i<156; $i++)
  1477. {
  1478. $v_checksum += ord(' ');
  1479. }
  1480. // ..... Last part of the header
  1481. for ($i=156, $j=0; $i<512; $i++, $j++)
  1482. {
  1483. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1484. }
  1485. TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
  1486. // ----- Write the first 148 bytes of the header in the archive
  1487. if ($p_mode == "tar")
  1488. fputs($p_tar, $v_binary_data_first, 148);
  1489. else
  1490. gzputs($p_tar, $v_binary_data_first, 148);
  1491. // ----- Write the calculated checksum
  1492. $v_checksum = sprintf("%6s ", DecOct($v_checksum));
  1493. $v_binary_data = pack("a8", $v_checksum);
  1494. if ($p_mode == "tar")
  1495. fputs($p_tar, $v_binary_data, 8);
  1496. else
  1497. gzputs($p_tar, $v_binary_data, 8);
  1498. // ----- Write the last 356 bytes of the header in the archive
  1499. if ($p_mode == "tar")
  1500. fputs($p_tar, $v_binary_data_last, 356);
  1501. else
  1502. gzputs($p_tar, $v_binary_data_last, 356);
  1503. // ----- Set the properties in the header "structure"
  1504. $p_header[filename] = $v_reduce_filename;
  1505. $p_header[mode] = $v_perms;
  1506. $p_header[uid] = $v_uid;
  1507. $p_header[gid] = $v_gid;
  1508. $p_header[size] = $v_size;
  1509. $p_header[mtime] = $v_mtime;
  1510. $p_header[typeflag] = $v_typeflag;
  1511. $p_header[status] = "added";
  1512. // ----- Return
  1513. TrFctEnd(__FILE__, __LINE__, $v_result);
  1514. return $v_result;
  1515. }
  1516. // --------------------------------------------------------------------------------
  1517. // --------------------------------------------------------------------------------
  1518. // Function : PclTarHandleFooter()
  1519. // Description :
  1520. // Parameters :
  1521. // Return Values :
  1522. // --------------------------------------------------------------------------------
  1523. function PclTarHandleFooter($p_tar, $p_mode)
  1524. {
  1525. TrFctStart(__FILE__, __LINE__, "PclTarHandleFooter", "tar='$p_tar', p_mode=$p_mode");
  1526. $v_result=1;
  1527. // ----- Write the last 0 filled block for end of archive
  1528. $v_binary_data = pack("a512", "");
  1529. if ($p_mode == "tar")
  1530. fputs($p_tar, $v_binary_data);
  1531. else
  1532. gzputs($p_tar, $v_binary_data);
  1533. // ----- Return
  1534. TrFctEnd(__FILE__, __LINE__, $v_result);
  1535. return $v_result;
  1536. }
  1537. // --------------------------------------------------------------------------------
  1538. // --------------------------------------------------------------------------------
  1539. // Function : PclTarHandleExtract()
  1540. // Description :
  1541. // Parameters :
  1542. // $p_tarname : Filename of the tar (or tgz) archive
  1543. // $p_file_list : An array which contains the list of files to extract, this
  1544. // array may be empty when $p_mode is 'complete'
  1545. // $p_list_detail : An array where will be placed the properties of each extracted/listed file
  1546. // $p_mode : 'complete' will extract all files from the archive,
  1547. // 'partial' will look for files in $p_file_list
  1548. // 'list' will only list the files from the archive without any extract
  1549. // $p_path : Path to add while writing the extracted files
  1550. // $p_tar_mode : 'tar' for GNU TAR archive, 'tgz' for compressed archive
  1551. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  1552. // extracted files. If the path does not match the file path,
  1553. // the file is extracted with its memorized path.
  1554. // $p_remove_path does not apply to 'list' mode.
  1555. // $p_path and $p_remove_path are commulative.
  1556. // Return Values :
  1557. // --------------------------------------------------------------------------------
  1558. function PclTarHandleExtract($p_tarname, $p_file_list, &$p_list_detail, $p_mode, $p_path, $p_tar_mode, $p_remove_path)
  1559. {
  1560. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtract", "archive='$p_tarname', list, mode=$p_mode, path=$p_path, tar_mode=$p_tar_mode, remove_path='$p_remove_path'");
  1561. $v_result=1;
  1562. $v_nb = 0;
  1563. $v_extract_all = TRUE;
  1564. $v_listing = FALSE;
  1565. // ----- Check the path
  1566. if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../")))
  1567. // NET2FTP
  1568. // $p_path = "./".$p_path;
  1569. // ----- Look for path to remove format (should end by /)
  1570. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  1571. {
  1572. $p_remove_path .= '/';
  1573. }
  1574. $p_remove_path_size = strlen($p_remove_path);
  1575. // ----- Study the mode
  1576. switch ($p_mode) {
  1577. case "complete" :
  1578. // ----- Flag extract of all files
  1579. $v_extract_all = TRUE;
  1580. $v_listing = FALSE;
  1581. break;
  1582. case "partial" :
  1583. // ----- Flag extract of specific files
  1584. $v_extract_all = FALSE;
  1585. $v_listing = FALSE;
  1586. break;
  1587. case "list" :
  1588. // ----- Flag list of all files
  1589. $v_extract_all = FALSE;
  1590. $v_listing = TRUE;
  1591. break;
  1592. default :
  1593. // ----- Error log
  1594. PclErrorLog(-3, "Invalid extract mode ($p_mode)");
  1595. // ----- Return
  1596. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1597. return PclErrorCode();
  1598. }
  1599. // ----- Open the tar file
  1600. if ($p_tar_mode == "tar")
  1601. {
  1602. TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  1603. $v_tar = fopen($p_tarname, "rb");
  1604. }
  1605. else
  1606. {
  1607. TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode");
  1608. $v_tar = @gzopen($p_tarname, "rb");
  1609. }
  1610. // ----- Check that the archive is open
  1611. if ($v_tar == 0)
  1612. {
  1613. // ----- Error log
  1614. PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
  1615. // ----- Return
  1616. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1617. return PclErrorCode();
  1618. }
  1619. // ----- Read the blocks
  1620. While (!($v_end_of_file = ($p_tar_mode == "tar"?feof($v_tar):gzeof($v_tar))))
  1621. {
  1622. TrFctMessage(__FILE__, __LINE__, 3, "Looking for next header ...");
  1623. // ----- Clear cache of file infos
  1624. clearstatcache();
  1625. // ----- Reset extract tag
  1626. $v_extract_file = FALSE;
  1627. $v_extraction_stopped = 0;
  1628. // ----- Read the 512 bytes header
  1629. if ($p_tar_mode == "tar")
  1630. $v_binary_data = fread($v_tar, 512);
  1631. else
  1632. $v_binary_data = gzread($v_tar, 512);
  1633. // ----- Read the header properties
  1634. if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1)
  1635. {
  1636. // ----- Close the archive file
  1637. if ($p_tar_mode == "tar")
  1638. fclose($v_tar);
  1639. else
  1640. gzclose($v_tar);
  1641. // ----- Return
  1642. TrFctEnd(__FILE__, __LINE__, $v_result);
  1643. return $v_result;
  1644. }
  1645. // ----- Look for empty blocks to skip
  1646. if ($v_header[filename] == "")
  1647. {
  1648. TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?");
  1649. continue;
  1650. }
  1651. TrFctMessage(__FILE__, __LINE__, 2, "Found file '$v_header[filename]', size '$v_header[size]'");
  1652. // ----- Look for partial extract
  1653. if ((!$v_extract_all) && (is_array($p_file_list)))
  1654. {
  1655. TrFctMessage(__FILE__, __LINE__, 2, "Look if the file '$v_header[filename]' need to be extracted");
  1656. // ----- By default no unzip if the file is not found
  1657. $v_extract_file = FALSE;
  1658. // ----- Look into the file list
  1659. for ($i=0; $i<sizeof($p_file_list); $i++)
  1660. {
  1661. TrFctMessage(__FILE__, __LINE__, 2, "Compare archived file '$v_header[filename]' from asked list file '".$p_file_list[$i]."'");
  1662. // ----- Look if it is a directory
  1663. if (substr($p_file_list[$i], -1) == "/")
  1664. {
  1665. TrFctMessage(__FILE__, __LINE__, 3, "Compare file '$v_header[filename]' with directory '$p_file_list[$i]'");
  1666. // ----- Look if the directory is in the filename path
  1667. if ((strlen($v_header[filename]) > strlen($p_file_list[$i])) && (substr($v_header[filename], 0, strlen($p_file_list[$i])) == $p_file_list[$i]))
  1668. {
  1669. // ----- The file is in the directory, so extract it
  1670. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' is in directory '$p_file_list[$i]' : extract it");
  1671. $v_extract_file = TRUE;
  1672. // ----- End of loop
  1673. break;
  1674. }
  1675. }
  1676. // ----- It is a file, so compare the file names
  1677. else if ($p_file_list[$i] == $v_header[filename])
  1678. {
  1679. // ----- File found
  1680. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' should be extracted");
  1681. $v_extract_file = TRUE;
  1682. // ----- End of loop
  1683. break;
  1684. }
  1685. }
  1686. // ----- Trace
  1687. if (!$v_extract_file)
  1688. {
  1689. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' should not be extracted");
  1690. }
  1691. }
  1692. else
  1693. {
  1694. // ----- All files need to be extracted
  1695. $v_extract_file = TRUE;
  1696. }
  1697. // ----- Look if this file need to be extracted
  1698. if (($v_extract_file) && (!$v_listing))
  1699. {
  1700. // ----- Look for path to remove
  1701. if (($p_remove_path != "")
  1702. && (substr($v_header[filename], 0, $p_remove_path_size) == $p_remove_path))
  1703. {
  1704. TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '$v_header[filename]'");
  1705. // ----- Remove the path
  1706. $v_header[filename] = substr($v_header[filename], $p_remove_path_size);
  1707. TrFctMessage(__FILE__, __LINE__, 3, "Reslting file is '$v_header[filename]'");
  1708. }
  1709. // ----- Add the path to the file
  1710. if (($p_path != "./") && ($p_path != "/"))
  1711. {
  1712. // ----- Look for the path end '/'
  1713. while (substr($p_path, -1) == "/")
  1714. {
  1715. TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
  1716. $p_path = substr($p_path, 0, strlen($p_path)-1);
  1717. TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
  1718. }
  1719. // ----- Add the path
  1720. if (substr($v_header[filename], 0, 1) == "/")
  1721. $v_header[filename] = $p_path.$v_header[filename];
  1722. else
  1723. $v_header[filename] = $p_path."/".$v_header[filename];
  1724. }
  1725. // ----- Trace
  1726. TrFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '$v_header[filename]', size '$v_header[size]'");
  1727. // ----- Check that the file does not exists
  1728. if (file_exists($v_header[filename]))
  1729. {
  1730. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' already exists");
  1731. // ----- Look if file is a directory
  1732. if (is_dir($v_header[filename]))
  1733. {
  1734. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is a directory");
  1735. // ----- Change the file status
  1736. $v_header[status] = "already_a_directory";
  1737. // ----- Skip the extract
  1738. $v_extraction_stopped = 1;
  1739. $v_extract_file = 0;
  1740. }
  1741. // ----- Look if file is write protected
  1742. else if (!is_writeable($v_header[filename]))
  1743. {
  1744. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is write protected");
  1745. // ----- Change the file status
  1746. $v_header[status] = "write_protected";
  1747. // ----- Skip the extract
  1748. $v_extraction_stopped = 1;
  1749. $v_extract_file = 0;
  1750. }
  1751. // ----- Look if the extracted file is older
  1752. else if (filemtime($v_header[filename]) > $v_header[mtime])
  1753. {
  1754. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is newer (".date("l dS of F Y h:i:s A", filemtime($v_header[filename])).") than the extracted file (".date("l dS of F Y h:i:s A", $v_header[mtime]).")");
  1755. // ----- Change the file status
  1756. $v_header[status] = "newer_exist";
  1757. // ----- Skip the extract
  1758. $v_extraction_stopped = 1;
  1759. $v_extract_file = 0;
  1760. }
  1761. }
  1762. // ----- Check the directory availability and create it if necessary
  1763. else
  1764. {
  1765. if ($v_header[typeflag]=="5")
  1766. $v_dir_to_check = $v_header[filename];
  1767. else if (!strstr($v_header[filename], "/"))
  1768. $v_dir_to_check = "";
  1769. else
  1770. $v_dir_to_check = dirname($v_header[filename]);
  1771. if (($v_result = PclTarHandlerDirCheck($v_dir_to_check)) != 1)
  1772. {
  1773. TrFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '$v_header[filename]'");
  1774. // ----- Change the file status
  1775. $v_header[status] = "path_creation_fail";
  1776. // ----- Skip the extract
  1777. $v_extraction_stopped = 1;
  1778. $v_extract_file = 0;
  1779. }
  1780. }
  1781. // ----- Do the extraction
  1782. if (($v_extract_file) && ($v_header[typeflag]!="5"))
  1783. {
  1784. // ----- Open the destination file in write mode
  1785. if (($v_dest_file = @fopen($v_header[filename], "wb")) == 0)
  1786. {
  1787. TrFctMessage(__FILE__, __LINE__, 2, "Error while opening '$v_header[filename]' in write binary mode");
  1788. // ----- Change the file status
  1789. $v_header[status] = "write_error";
  1790. // ----- Jump to next file
  1791. TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file");
  1792. if ($p_tar_mode == "tar")
  1793. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  1794. else
  1795. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  1796. }
  1797. else
  1798. {
  1799. TrFctMessage(__FILE__, __LINE__, 2, "Start extraction of '$v_header[filename]'");
  1800. // ----- Read data
  1801. $n = floor($v_header[size]/512);
  1802. for ($i=0; $i<$n; $i++)
  1803. {
  1804. TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number ".($i+1));
  1805. if ($p_tar_mode == "tar")
  1806. $v_content = fread($v_tar, 512);
  1807. else
  1808. $v_content = gzread($v_tar, 512);
  1809. fwrite($v_dest_file, $v_content, 512);
  1810. }
  1811. if (($v_header[size] % 512) != 0)
  1812. {
  1813. TrFctMessage(__FILE__, __LINE__, 3, "Read last ".($v_header[size] % 512)." bytes in a 512 block");
  1814. if ($p_tar_mode == "tar")
  1815. $v_content = fread($v_tar, 512);
  1816. else
  1817. $v_content = gzread($v_tar, 512);
  1818. fwrite($v_dest_file, $v_content, ($v_header[size] % 512));
  1819. }
  1820. // ----- Close the destination file
  1821. fclose($v_dest_file);
  1822. // ----- Change the file mode, mtime
  1823. touch($v_header[filename], $v_header[mtime]);
  1824. //chmod($v_header[filename], DecOct($v_header[mode]));
  1825. }
  1826. // ----- Check the file size
  1827. clearstatcache();
  1828. if (filesize($v_header[filename]) != $v_header[size])
  1829. {
  1830. // ----- Close the archive file
  1831. if ($p_tar_mode == "tar")
  1832. fclose($v_tar);
  1833. else
  1834. gzclose($v_tar);
  1835. // ----- Error log
  1836. PclErrorLog(-7, "Extracted file '$v_header[filename]' does not have the correct file size '".filesize($v_filename)."' ('$v_header[size]' expected). Archive may be corrupted.");
  1837. // ----- Return
  1838. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1839. return PclErrorCode();
  1840. }
  1841. // ----- Trace
  1842. TrFctMessage(__FILE__, __LINE__, 2, "Extraction done");
  1843. }
  1844. else
  1845. {
  1846. TrFctMessage(__FILE__, __LINE__, 2, "Extraction of file '$v_header[filename]' skipped.");
  1847. // ----- Jump to next file
  1848. TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file");
  1849. if ($p_tar_mode == "tar")
  1850. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  1851. else
  1852. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  1853. }
  1854. }
  1855. // ----- Look for file that is not to be unzipped
  1856. else
  1857. {
  1858. // ----- Trace
  1859. TrFctMessage(__FILE__, __LINE__, 2, "Jump file '$v_header[filename]'");
  1860. TrFctMessage(__FILE__, __LINE__, 4, "Position avant jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  1861. // ----- Jump to next file
  1862. if ($p_tar_mode == "tar")
  1863. fseek($v_tar, ($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))+(ceil(($v_header[size]/512))*512));
  1864. else
  1865. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  1866. TrFctMessage(__FILE__, __LINE__, 4, "Position après jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  1867. }
  1868. if ($p_tar_mode == "tar")
  1869. $v_end_of_file = feof($v_tar);
  1870. else
  1871. $v_end_of_file = gzeof($v_tar);
  1872. // ----- File name and properties are logged if listing mode or file is extracted
  1873. if ($v_listing || $v_extract_file || $v_extraction_stopped)
  1874. {
  1875. TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '$v_header[filename]'");
  1876. // ----- Log extracted files
  1877. if (($v_file_dir = dirname($v_header[filename])) == $v_header[filename])
  1878. $v_file_dir = "";
  1879. if ((substr($v_header[filename], 0, 1) == "/") && ($v_file_dir == ""))
  1880. $v_file_dir = "/";
  1881. // ----- Add the array describing the file into the list
  1882. $p_list_detail[$v_nb] = $v_header;
  1883. // ----- Increment
  1884. $v_nb++;
  1885. }
  1886. }
  1887. // ----- Close the tarfile
  1888. if ($p_tar_mode == "tar")
  1889. fclose($v_tar);
  1890. else
  1891. gzclose($v_tar);
  1892. // ----- Return
  1893. TrFctEnd(__FILE__, __LINE__, $v_result);
  1894. return $v_result;
  1895. }
  1896. // --------------------------------------------------------------------------------
  1897. // --------------------------------------------------------------------------------
  1898. // Function : PclTarHandleExtractByIndexList()
  1899. // Description :
  1900. // Extract the files which are at the indexes specified. If the 'file' at the
  1901. // index is a directory, the directory only is created, not all the files stored
  1902. // for that directory.
  1903. // Parameters :
  1904. // $p_index_string : String of indexes of files to extract. The form of the
  1905. // string is "0,4-6,8-12" with only numbers and '-' for
  1906. // for range, and ',' to separate ranges. No spaces or ';'
  1907. // are allowed.
  1908. // Return Values :
  1909. // --------------------------------------------------------------------------------
  1910. function PclTarHandleExtractByIndexList($p_tarname, $p_index_string, &$p_list_detail, $p_path, $p_remove_path, $p_tar_mode)
  1911. {
  1912. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtractByIndexList", "archive='$p_tarname', index_string='$p_index_string', list, path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
  1913. $v_result=1;
  1914. $v_nb = 0;
  1915. // ----- TBC : I should check the string by a regexp
  1916. // ----- Check the path
  1917. if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path, 0, 2) != "./")))
  1918. $p_path = "./".$p_path;
  1919. // ----- Look for path to remove format (should end by /)
  1920. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  1921. {
  1922. $p_remove_path .= '/';
  1923. }
  1924. $p_remove_path_size = strlen($p_remove_path);
  1925. // ----- Open the tar file
  1926. if ($p_tar_mode == "tar")
  1927. {
  1928. TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  1929. $v_tar = @fopen($p_tarname, "rb");
  1930. }
  1931. else
  1932. {
  1933. TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode");
  1934. $v_tar = @gzopen($p_tarname, "rb");
  1935. }
  1936. // ----- Check that the archive is open
  1937. if ($v_tar == 0)
  1938. {
  1939. // ----- Error log
  1940. PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
  1941. // ----- Return
  1942. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1943. return PclErrorCode();
  1944. }
  1945. // ----- Manipulate the index list
  1946. $v_list = explode(",", $p_index_string);
  1947. sort($v_list);
  1948. // ----- Loop on the index list
  1949. $v_index=0;
  1950. for ($i=0; ($i<sizeof($v_list)) && ($v_result); $i++)
  1951. {
  1952. TrFctMessage(__FILE__, __LINE__, 3, "Looking for index part '$v_list[$i]'");
  1953. // ----- Extract range
  1954. $v_index_list = explode("-", $v_list[$i]);
  1955. $v_size_index_list = sizeof($v_index_list);
  1956. if ($v_size_index_list == 1)
  1957. {
  1958. TrFctMessage(__FILE__, __LINE__, 3, "Only one index '$v_index_list[0]'");
  1959. // ----- Do the extraction
  1960. $v_result = PclTarHandleExtractByIndex($v_tar, $v_index, $v_index_list[0], $v_index_list[0], $p_list_detail, $p_path, $p_remove_path, $p_tar_mode);
  1961. }
  1962. else if ($v_size_index_list == 2)
  1963. {
  1964. TrFctMessage(__FILE__, __LINE__, 3, "Two indexes '$v_index_list[0]' and '$v_index_list[1]'");
  1965. // ----- Do the extraction
  1966. $v_result = PclTarHandleExtractByIndex($v_tar, $v_index, $v_index_list[0], $v_index_list[1], $p_list_detail, $p_path, $p_remove_path, $p_tar_mode);
  1967. }
  1968. }
  1969. // ----- Close the tarfile
  1970. if ($p_tar_mode == "tar")
  1971. fclose($v_tar);
  1972. else
  1973. gzclose($v_tar);
  1974. // ----- Return
  1975. TrFctEnd(__FILE__, __LINE__, $v_result);
  1976. return $v_result;
  1977. }
  1978. // --------------------------------------------------------------------------------
  1979. // --------------------------------------------------------------------------------
  1980. // Function : PclTarHandleExtractByIndex()
  1981. // Description :
  1982. // Parameters :
  1983. // Return Values :
  1984. // --------------------------------------------------------------------------------
  1985. function PclTarHandleExtractByIndex($p_tar, &$p_index_current, $p_index_start, $p_index_stop, &$p_list_detail, $p_path, $p_remove_path, $p_tar_mode)
  1986. {
  1987. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtractByIndex", "archive_descr='$p_tar', index_current=$p_index_current, index_start='$p_index_start', index_stop='$p_index_stop', list, path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
  1988. $v_result=1;
  1989. $v_nb = 0;
  1990. // TBC : I should replace all $v_tar by $p_tar in this function ....
  1991. $v_tar = $p_tar;
  1992. // ----- Look the number of elements already in $p_list_detail
  1993. $v_nb = sizeof($p_list_detail);
  1994. // ----- Read the blocks
  1995. While (!($v_end_of_file = ($p_tar_mode == "tar"?feof($v_tar):gzeof($v_tar))))
  1996. {
  1997. TrFctMessage(__FILE__, __LINE__, 3, "Looking for next file ...");
  1998. TrFctMessage(__FILE__, __LINE__, 3, "Index current=$p_index_current, range=[$p_index_start, $p_index_stop])");
  1999. if ($p_index_current > $p_index_stop)
  2000. {
  2001. TrFctMessage(__FILE__, __LINE__, 2, "Stop extraction, past stop index");
  2002. break;
  2003. }
  2004. // ----- Clear cache of file infos
  2005. clearstatcache();
  2006. // ----- Reset extract tag
  2007. $v_extract_file = FALSE;
  2008. $v_extraction_stopped = 0;
  2009. // ----- Read the 512 bytes header
  2010. if ($p_tar_mode == "tar")
  2011. $v_binary_data = fread($v_tar, 512);
  2012. else
  2013. $v_binary_data = gzread($v_tar, 512);
  2014. // ----- Read the header properties
  2015. if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1)
  2016. {
  2017. // ----- Return
  2018. TrFctEnd(__FILE__, __LINE__, $v_result);
  2019. return $v_result;
  2020. }
  2021. // ----- Look for empty blocks to skip
  2022. if ($v_header[filename] == "")
  2023. {
  2024. TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?");
  2025. continue;
  2026. }
  2027. TrFctMessage(__FILE__, __LINE__, 2, "Found file '$v_header[filename]', size '$v_header[size]'");
  2028. // ----- Look if file is in the range to be extracted
  2029. if (($p_index_current >= $p_index_start) && ($p_index_current <= $p_index_stop))
  2030. {
  2031. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' is in the range to be extracted");
  2032. $v_extract_file = TRUE;
  2033. }
  2034. else
  2035. {
  2036. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' is out of the range");
  2037. $v_extract_file = FALSE;
  2038. }
  2039. // ----- Look if this file need to be extracted
  2040. if ($v_extract_file)
  2041. {
  2042. if (($v_result = PclTarHandleExtractFile($v_tar, $v_header, $p_path, $p_remove_path, $p_tar_mode)) != 1)
  2043. {
  2044. // ----- Return
  2045. TrFctEnd(__FILE__, __LINE__, $v_result);
  2046. return $v_result;
  2047. }
  2048. }
  2049. // ----- Look for file that is not to be extracted
  2050. else
  2051. {
  2052. // ----- Trace
  2053. TrFctMessage(__FILE__, __LINE__, 2, "Jump file '$v_header[filename]'");
  2054. TrFctMessage(__FILE__, __LINE__, 4, "Position avant jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  2055. // ----- Jump to next file
  2056. if ($p_tar_mode == "tar")
  2057. fseek($v_tar, ($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))+(ceil(($v_header[size]/512))*512));
  2058. else
  2059. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  2060. TrFctMessage(__FILE__, __LINE__, 4, "Position après jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  2061. }
  2062. if ($p_tar_mode == "tar")
  2063. $v_end_of_file = feof($v_tar);
  2064. else
  2065. $v_end_of_file = gzeof($v_tar);
  2066. // ----- File name and properties are logged if listing mode or file is extracted
  2067. if ($v_extract_file)
  2068. {
  2069. TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '$v_header[filename]'");
  2070. // ----- Log extracted files
  2071. if (($v_file_dir = dirname($v_header[filename])) == $v_header[filename])
  2072. $v_file_dir = "";
  2073. if ((substr($v_header[filename], 0, 1) == "/") && ($v_file_dir == ""))
  2074. $v_file_dir = "/";
  2075. // ----- Add the array describing the file into the list
  2076. $p_list_detail[$v_nb] = $v_header;
  2077. // ----- Increment
  2078. $v_nb++;
  2079. }
  2080. // ----- Increment the current file index
  2081. $p_index_current++;
  2082. }
  2083. // ----- Return
  2084. TrFctEnd(__FILE__, __LINE__, $v_result);
  2085. return $v_result;
  2086. }
  2087. // --------------------------------------------------------------------------------
  2088. // --------------------------------------------------------------------------------
  2089. // Function : PclTarHandleExtractFile()
  2090. // Description :
  2091. // Parameters :
  2092. // Return Values :
  2093. // --------------------------------------------------------------------------------
  2094. function PclTarHandleExtractFile($p_tar, &$v_header, $p_path, $p_remove_path, $p_tar_mode)
  2095. {
  2096. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtractFile", "archive_descr='$p_tar', path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
  2097. $v_result=1;
  2098. // TBC : I should replace all $v_tar by $p_tar in this function ....
  2099. $v_tar = $p_tar;
  2100. $v_extract_file = 1;
  2101. $p_remove_path_size = strlen($p_remove_path);
  2102. // ----- Look for path to remove
  2103. if (($p_remove_path != "")
  2104. && (substr($v_header[filename], 0, $p_remove_path_size) == $p_remove_path))
  2105. {
  2106. TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '$v_header[filename]'");
  2107. // ----- Remove the path
  2108. $v_header[filename] = substr($v_header[filename], $p_remove_path_size);
  2109. TrFctMessage(__FILE__, __LINE__, 3, "Resulting file is '$v_header[filename]'");
  2110. }
  2111. // ----- Add the path to the file
  2112. if (($p_path != "./") && ($p_path != "/"))
  2113. {
  2114. // ----- Look for the path end '/'
  2115. while (substr($p_path, -1) == "/")
  2116. {
  2117. TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
  2118. $p_path = substr($p_path, 0, strlen($p_path)-1);
  2119. TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
  2120. }
  2121. // ----- Add the path
  2122. if (substr($v_header[filename], 0, 1) == "/")
  2123. $v_header[filename] = $p_path.$v_header[filename];
  2124. else
  2125. $v_header[filename] = $p_path."/".$v_header[filename];
  2126. }
  2127. // ----- Trace
  2128. TrFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '$v_header[filename]', size '$v_header[size]'");
  2129. // ----- Check that the file does not exists
  2130. if (file_exists($v_header[filename]))
  2131. {
  2132. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' already exists");
  2133. // ----- Look if file is a directory
  2134. if (is_dir($v_header[filename]))
  2135. {
  2136. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is a directory");
  2137. // ----- Change the file status
  2138. $v_header[status] = "already_a_directory";
  2139. // ----- Skip the extract
  2140. $v_extraction_stopped = 1;
  2141. $v_extract_file = 0;
  2142. }
  2143. // ----- Look if file is write protected
  2144. else if (!is_writeable($v_header[filename]))
  2145. if (!is_writeable($v_header[filename]))
  2146. {
  2147. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is write protected");
  2148. // ----- Change the file status
  2149. $v_header[status] = "write_protected";
  2150. // ----- Skip the extract
  2151. $v_extraction_stopped = 1;
  2152. $v_extract_file = 0;
  2153. }
  2154. // ----- Look if the extracted file is older
  2155. else if (filemtime($v_header[filename]) > $v_header[mtime])
  2156. {
  2157. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is newer (".date("l dS of F Y h:i:s A", filemtime($v_header[filename])).") than the extracted file (".date("l dS of F Y h:i:s A", $v_header[mtime]).")");
  2158. // ----- Change the file status
  2159. $v_header[status] = "newer_exist";
  2160. // ----- Skip the extract
  2161. $v_extraction_stopped = 1;
  2162. $v_extract_file = 0;
  2163. }
  2164. }
  2165. // ----- Check the directory availability and create it if necessary
  2166. else
  2167. {
  2168. if ($v_header[typeflag]=="5")
  2169. $v_dir_to_check = $v_header[filename];
  2170. else if (!strstr($v_header[filename], "/"))
  2171. $v_dir_to_check = "";
  2172. else
  2173. $v_dir_to_check = dirname($v_header[filename]);
  2174. if (($v_result = PclTarHandlerDirCheck($v_dir_to_check)) != 1)
  2175. {
  2176. TrFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '$v_header[filename]'");
  2177. // ----- Change the file status
  2178. $v_header[status] = "path_creation_fail";
  2179. // ----- Skip the extract
  2180. $v_extraction_stopped = 1;
  2181. $v_extract_file = 0;
  2182. }
  2183. }
  2184. // ----- Do the real bytes extraction (if not a directory)
  2185. if (($v_extract_file) && ($v_header[typeflag]!="5"))
  2186. {
  2187. // ----- Open the destination file in write mode
  2188. if (($v_dest_file = @fopen($v_header[filename], "wb")) == 0)
  2189. {
  2190. TrFctMessage(__FILE__, __LINE__, 2, "Error while opening '$v_header[filename]' in write binary mode");
  2191. // ----- Change the file status
  2192. $v_header[status] = "write_error";
  2193. // ----- Jump to next file
  2194. TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file");
  2195. if ($p_tar_mode == "tar")
  2196. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  2197. else
  2198. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  2199. }
  2200. else
  2201. {
  2202. TrFctMessage(__FILE__, __LINE__, 2, "Start extraction of '$v_header[filename]'");
  2203. // ----- Read data
  2204. $n = floor($v_header[size]/512);
  2205. for ($i=0; $i<$n; $i++)
  2206. {
  2207. TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number ".($i+1));
  2208. if ($p_tar_mode == "tar")
  2209. $v_content = fread($v_tar, 512);
  2210. else
  2211. $v_content = gzread($v_tar, 512);
  2212. fwrite($v_dest_file, $v_content, 512);
  2213. }
  2214. if (($v_header[size] % 512) != 0)
  2215. {
  2216. TrFctMessage(__FILE__, __LINE__, 3, "Read last ".($v_header[size] % 512)." bytes in a 512 block");
  2217. if ($p_tar_mode == "tar")
  2218. $v_content = fread($v_tar, 512);
  2219. else
  2220. $v_content = gzread($v_tar, 512);
  2221. fwrite($v_dest_file, $v_content, ($v_header[size] % 512));
  2222. }
  2223. // ----- Close the destination file
  2224. fclose($v_dest_file);
  2225. // ----- Change the file mode, mtime
  2226. touch($v_header[filename], $v_header[mtime]);
  2227. //chmod($v_header[filename], DecOct($v_header[mode]));
  2228. }
  2229. // ----- Check the file size
  2230. clearstatcache();
  2231. if (filesize($v_header[filename]) != $v_header[size])
  2232. {
  2233. // ----- Error log
  2234. PclErrorLog(-7, "Extracted file '$v_header[filename]' does not have the correct file size '".filesize($v_filename)."' ('$v_header[size]' expected). Archive may be corrupted.");
  2235. // ----- Return
  2236. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2237. return PclErrorCode();
  2238. }
  2239. // ----- Trace
  2240. TrFctMessage(__FILE__, __LINE__, 2, "Extraction done");
  2241. }
  2242. else
  2243. {
  2244. TrFctMessage(__FILE__, __LINE__, 2, "Extraction of file '$v_header[filename]' skipped.");
  2245. // ----- Jump to next file
  2246. TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file");
  2247. if ($p_tar_mode == "tar")
  2248. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  2249. else
  2250. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  2251. }
  2252. // ----- Return
  2253. TrFctEnd(__FILE__, __LINE__, $v_result);
  2254. return $v_result;
  2255. }
  2256. // --------------------------------------------------------------------------------
  2257. // --------------------------------------------------------------------------------
  2258. // Function : PclTarHandleDelete()
  2259. // Description :
  2260. // Parameters :
  2261. // Return Values :
  2262. // --------------------------------------------------------------------------------
  2263. function PclTarHandleDelete($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode)
  2264. {
  2265. TrFctStart(__FILE__, __LINE__, "PclTarHandleDelete", "archive='$p_tarname', list, tar_mode=$p_tar_mode");
  2266. $v_result=1;
  2267. $v_nb=0;
  2268. // ----- Look for regular tar file
  2269. if ($p_tar_mode == "tar")
  2270. {
  2271. // ----- Open file
  2272. TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  2273. if (($v_tar = @fopen($p_tarname, "rb")) == 0)
  2274. {
  2275. // ----- Error log
  2276. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  2277. // ----- Return
  2278. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2279. return PclErrorCode();
  2280. }
  2281. // ----- Open a temporary file in write mode
  2282. $v_temp_tarname = uniqid("pcltar-").".tmp";
  2283. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  2284. if (($v_temp_tar = @fopen($v_temp_tarname, "wb")) == 0)
  2285. {
  2286. // ----- Close tar file
  2287. fclose($v_tar);
  2288. // ----- Error log
  2289. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  2290. // ----- Return
  2291. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2292. return PclErrorCode();
  2293. }
  2294. }
  2295. // ----- Look for compressed tar file
  2296. else
  2297. {
  2298. // ----- Open the file in read mode
  2299. TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode");
  2300. if (($v_tar = @gzopen($p_tarname, "rb")) == 0)
  2301. {
  2302. // ----- Error log
  2303. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  2304. // ----- Return
  2305. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2306. return PclErrorCode();
  2307. }
  2308. // ----- Open a temporary file in write mode
  2309. $v_temp_tarname = uniqid("pcltar-").".tmp";
  2310. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  2311. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  2312. {
  2313. // ----- Close tar file
  2314. gzclose($v_tar);
  2315. // ----- Error log
  2316. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  2317. // ----- Return
  2318. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2319. return PclErrorCode();
  2320. }
  2321. }
  2322. // ----- Read the blocks
  2323. While (!($v_end_of_file = ($p_tar_mode == "tar"?feof($v_tar):gzeof($v_tar))))
  2324. {
  2325. TrFctMessage(__FILE__, __LINE__, 3, "Looking for next header ...");
  2326. // ----- Clear cache of file infos
  2327. clearstatcache();
  2328. // ----- Reset delete tag
  2329. $v_delete_file = FALSE;
  2330. // ----- Read the first 512 block header
  2331. if ($p_tar_mode == "tar")
  2332. $v_binary_data = fread($v_tar, 512);
  2333. else
  2334. $v_binary_data = gzread($v_tar, 512);
  2335. // ----- Read the header properties
  2336. if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1)
  2337. {
  2338. // ----- Close the archive file
  2339. if ($p_tar_mode == "tar")
  2340. {
  2341. fclose($v_tar);
  2342. fclose($v_temp_tar);
  2343. }
  2344. else
  2345. {
  2346. gzclose($v_tar);
  2347. gzclose($v_temp_tar);
  2348. }
  2349. @unlink($v_temp_tarname);
  2350. // ----- Return
  2351. TrFctEnd(__FILE__, __LINE__, $v_result);
  2352. return $v_result;
  2353. }
  2354. // ----- Look for empty blocks to skip
  2355. if ($v_header[filename] == "")
  2356. {
  2357. TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?");
  2358. continue;
  2359. }
  2360. TrFctMessage(__FILE__, __LINE__, 2, "Found file '$v_header[filename]', size '$v_header[size]'");
  2361. // ----- Look for filenames to delete
  2362. for ($i=0, $v_delete_file=FALSE; ($i<sizeof($p_file_list)) && (!$v_delete_file); $i++)
  2363. {
  2364. // ----- Compare the file names
  2365. // if ($p_file_list[$i] == $v_header[filename])
  2366. if (($v_len = strcmp($p_file_list[$i], $v_header[filename])) <= 0)
  2367. {
  2368. if ($v_len==0)
  2369. {
  2370. TrFctMessage(__FILE__, __LINE__, 3, "Found that '$v_header[filename]' need to be deleted");
  2371. $v_delete_file = TRUE;
  2372. }
  2373. else
  2374. {
  2375. TrFctMessage(__FILE__, __LINE__, 3, "Look if '$v_header[filename]' is a file in $p_file_list[$i]");
  2376. if (substr($v_header[filename], strlen($p_file_list[$i]), 1) == "/")
  2377. {
  2378. TrFctMessage(__FILE__, __LINE__, 3, "'$v_header[filename]' is a file in $p_file_list[$i]");
  2379. $v_delete_file = TRUE;
  2380. }
  2381. }
  2382. }
  2383. }
  2384. // ----- Copy files that do not need to be deleted
  2385. if (!$v_delete_file)
  2386. {
  2387. TrFctMessage(__FILE__, __LINE__, 2, "Keep file '$v_header[filename]'");
  2388. // ----- Write the file header
  2389. if ($p_tar_mode == "tar")
  2390. {
  2391. fputs($v_temp_tar, $v_binary_data, 512);
  2392. }
  2393. else
  2394. {
  2395. gzputs($v_temp_tar, $v_binary_data, 512);
  2396. }
  2397. // ----- Write the file data
  2398. $n = ceil($v_header[size]/512);
  2399. for ($i=0; $i<$n; $i++)
  2400. {
  2401. TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number ".($i+1));
  2402. if ($p_tar_mode == "tar")
  2403. {
  2404. $v_content = fread($v_tar, 512);
  2405. fwrite($v_temp_tar, $v_content, 512);
  2406. }
  2407. else
  2408. {
  2409. $v_content = gzread($v_tar, 512);
  2410. gzwrite($v_temp_tar, $v_content, 512);
  2411. }
  2412. }
  2413. // ----- File name and properties are logged if listing mode or file is extracted
  2414. TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '$v_header[filename]'");
  2415. // ----- Add the array describing the file into the list
  2416. $p_list_detail[$v_nb] = $v_header;
  2417. $p_list_detail[$v_nb][status] = "ok";
  2418. // ----- Increment
  2419. $v_nb++;
  2420. }
  2421. // ----- Look for file that is to be deleted
  2422. else
  2423. {
  2424. // ----- Trace
  2425. TrFctMessage(__FILE__, __LINE__, 2, "Start deletion of '$v_header[filename]'");
  2426. TrFctMessage(__FILE__, __LINE__, 4, "Position avant jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  2427. // ----- Jump to next file
  2428. if ($p_tar_mode == "tar")
  2429. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  2430. else
  2431. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  2432. TrFctMessage(__FILE__, __LINE__, 4, "Position après jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  2433. }
  2434. // ----- Look for end of file
  2435. if ($p_tar_mode == "tar")
  2436. $v_end_of_file = feof($v_tar);
  2437. else
  2438. $v_end_of_file = gzeof($v_tar);
  2439. }
  2440. // ----- Write the last empty buffer
  2441. PclTarHandleFooter($v_temp_tar, $p_tar_mode);
  2442. // ----- Close the tarfile
  2443. if ($p_tar_mode == "tar")
  2444. {
  2445. fclose($v_tar);
  2446. fclose($v_temp_tar);
  2447. }
  2448. else
  2449. {
  2450. gzclose($v_tar);
  2451. gzclose($v_temp_tar);
  2452. }
  2453. // ----- Unlink tar file
  2454. if (!@unlink($p_tarname))
  2455. {
  2456. // ----- Error log
  2457. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  2458. }
  2459. // ----- Rename tar file
  2460. if (!@rename($v_temp_tarname, $p_tarname))
  2461. {
  2462. // ----- Error log
  2463. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  2464. // ----- Return
  2465. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2466. return PclErrorCode();
  2467. }
  2468. // ----- Return
  2469. TrFctEnd(__FILE__, __LINE__, $v_result);
  2470. return $v_result;
  2471. }
  2472. // --------------------------------------------------------------------------------
  2473. // --------------------------------------------------------------------------------
  2474. // Function : PclTarHandleUpdate()
  2475. // Description :
  2476. // Parameters :
  2477. // Return Values :
  2478. // --------------------------------------------------------------------------------
  2479. function PclTarHandleUpdate($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode, $p_add_dir, $p_remove_dir)
  2480. {
  2481. TrFctStart(__FILE__, __LINE__, "PclTarHandleUpdate", "archive='$p_tarname', list, tar_mode=$p_tar_mode");
  2482. $v_result=1;
  2483. $v_nb=0;
  2484. $v_found_list = array();
  2485. // ----- Look for regular tar file
  2486. if ($p_tar_mode == "tar")
  2487. {
  2488. // ----- Open file
  2489. TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  2490. if (($v_tar = @fopen($p_tarname, "rb")) == 0)
  2491. {
  2492. // ----- Error log
  2493. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  2494. // ----- Return
  2495. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2496. return PclErrorCode();
  2497. }
  2498. // ----- Open a temporary file in write mode
  2499. $v_temp_tarname = uniqid("pcltar-").".tmp";
  2500. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  2501. if (($v_temp_tar = @fopen($v_temp_tarname, "wb")) == 0)
  2502. {
  2503. // ----- Close tar file
  2504. fclose($v_tar);
  2505. // ----- Error log
  2506. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  2507. // ----- Return
  2508. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2509. return PclErrorCode();
  2510. }
  2511. }
  2512. // ----- Look for compressed tar file
  2513. else
  2514. {
  2515. // ----- Open the file in read mode
  2516. TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode");
  2517. if (($v_tar = @gzopen($p_tarname, "rb")) == 0)
  2518. {
  2519. // ----- Error log
  2520. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  2521. // ----- Return
  2522. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2523. return PclErrorCode();
  2524. }
  2525. // ----- Open a temporary file in write mode
  2526. $v_temp_tarname = uniqid("pcltar-").".tmp";
  2527. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  2528. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  2529. {
  2530. // ----- Close tar file
  2531. gzclose($v_tar);
  2532. // ----- Error log
  2533. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  2534. // ----- Return
  2535. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2536. return PclErrorCode();
  2537. }
  2538. }
  2539. // ----- Prepare the list of files
  2540. for ($i=0; $i<sizeof($p_file_list); $i++)
  2541. {
  2542. // ----- Reset the found list
  2543. $v_found_list[$i] = 0;
  2544. // ----- Calculate the stored filename
  2545. $v_stored_list[$i] = $p_file_list[$i];
  2546. if ($p_remove_dir != "")
  2547. {
  2548. if (substr($p_file_list[$i], -1) != '/')
  2549. $p_remove_dir .= "/";
  2550. if (substr($p_file_list[$i], 0, strlen($p_remove_dir)) == $p_remove_dir)
  2551. {
  2552. $v_stored_list[$i] = substr($p_file_list[$i], strlen($p_remove_dir));
  2553. TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
  2554. }
  2555. }
  2556. if ($p_add_dir != "")
  2557. {
  2558. if (substr($p_add_dir, -1) == "/")
  2559. $v_stored_list[$i] = $p_add_dir.$v_stored_list[$i];
  2560. else
  2561. $v_stored_list[$i] = $p_add_dir."/".$v_stored_list[$i];
  2562. TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
  2563. }
  2564. $v_stored_list[$i] = PclTarHandlePathReduction($v_stored_list[$i]);
  2565. TrFctMessage(__FILE__, __LINE__, 3, "After reduction '$v_stored_list[$i]'");
  2566. }
  2567. // ----- Update file cache
  2568. clearstatcache();
  2569. // ----- Read the blocks
  2570. While (!($v_end_of_file = ($p_tar_mode == "tar"?feof($v_tar):gzeof($v_tar))))
  2571. {
  2572. TrFctMessage(__FILE__, __LINE__, 3, "Looking for next header ...");
  2573. // ----- Clear cache of file infos
  2574. clearstatcache();
  2575. // ----- Reset current found filename
  2576. $v_current_filename = "";
  2577. // ----- Reset delete tag
  2578. $v_delete_file = FALSE;
  2579. // ----- Read the first 512 block header
  2580. if ($p_tar_mode == "tar")
  2581. $v_binary_data = fread($v_tar, 512);
  2582. else
  2583. $v_binary_data = gzread($v_tar, 512);
  2584. // ----- Read the header properties
  2585. if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1)
  2586. {
  2587. // ----- Close the archive file
  2588. if ($p_tar_mode == "tar")
  2589. {
  2590. fclose($v_tar);
  2591. fclose($v_temp_tar);
  2592. }
  2593. else
  2594. {
  2595. gzclose($v_tar);
  2596. gzclose($v_temp_tar);
  2597. }
  2598. @unlink($v_temp_tarname);
  2599. // ----- Return
  2600. TrFctEnd(__FILE__, __LINE__, $v_result);
  2601. return $v_result;
  2602. }
  2603. // ----- Look for empty blocks to skip
  2604. if ($v_header[filename] == "")
  2605. {
  2606. TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?");
  2607. continue;
  2608. }
  2609. TrFctMessage(__FILE__, __LINE__, 2, "Found file '$v_header[filename]', size '$v_header[size]'");
  2610. // ----- Look for filenames to update
  2611. for ($i=0, $v_update_file=FALSE, $v_found_file=FALSE; ($i<sizeof($v_stored_list)) && (!$v_update_file); $i++)
  2612. {
  2613. TrFctMessage(__FILE__, __LINE__, 4, "Compare with file '$v_stored_list[$i]'");
  2614. // ----- Compare the file names
  2615. if ($v_stored_list[$i] == $v_header[filename])
  2616. {
  2617. TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' is present in archive");
  2618. TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' mtime=".filemtime($p_file_list[$i])." ".date("l dS of F Y h:i:s A", filemtime($p_file_list[$i])));
  2619. TrFctMessage(__FILE__, __LINE__, 3, "Archived mtime=".$v_header[mtime]." ".date("l dS of F Y h:i:s A", $v_header[mtime]));
  2620. // ----- Store found informations
  2621. $v_found_file = TRUE;
  2622. $v_current_filename = $p_file_list[$i];
  2623. // ----- Look if the file need to be updated
  2624. if (filemtime($p_file_list[$i]) > $v_header[mtime])
  2625. {
  2626. TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be updated");
  2627. $v_update_file = TRUE;
  2628. }
  2629. else
  2630. {
  2631. TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' does not need to be updated");
  2632. $v_update_file = FALSE;
  2633. }
  2634. // ----- Flag the name in order not to add the file at the end
  2635. $v_found_list[$i] = 1;
  2636. }
  2637. else
  2638. {
  2639. TrFctMessage(__FILE__, __LINE__, 4, "File '$p_file_list[$i]' is not '$v_header[filename]'");
  2640. }
  2641. }
  2642. // ----- Copy files that do not need to be updated
  2643. if (!$v_update_file)
  2644. {
  2645. TrFctMessage(__FILE__, __LINE__, 2, "Keep file '$v_header[filename]'");
  2646. // ----- Write the file header
  2647. if ($p_tar_mode == "tar")
  2648. {
  2649. fputs($v_temp_tar, $v_binary_data, 512);
  2650. }
  2651. else
  2652. {
  2653. gzputs($v_temp_tar, $v_binary_data, 512);
  2654. }
  2655. // ----- Write the file data
  2656. $n = ceil($v_header[size]/512);
  2657. for ($j=0; $j<$n; $j++)
  2658. {
  2659. TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number ".($j+1));
  2660. if ($p_tar_mode == "tar")
  2661. {
  2662. $v_content = fread($v_tar, 512);
  2663. fwrite($v_temp_tar, $v_content, 512);
  2664. }
  2665. else
  2666. {
  2667. $v_content = gzread($v_tar, 512);
  2668. gzwrite($v_temp_tar, $v_content, 512);
  2669. }
  2670. }
  2671. // ----- File name and properties are logged if listing mode or file is extracted
  2672. TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '$v_header[filename]'");
  2673. // ----- Add the array describing the file into the list
  2674. $p_list_detail[$v_nb] = $v_header;
  2675. $p_list_detail[$v_nb][status] = ($v_found_file?"not_updated":"ok");
  2676. // ----- Increment
  2677. $v_nb++;
  2678. }
  2679. // ----- Look for file that need to be updated
  2680. else
  2681. {
  2682. // ----- Trace
  2683. TrFctMessage(__FILE__, __LINE__, 2, "Start update of file '$v_current_filename'");
  2684. // ----- Store the old file size
  2685. $v_old_size = $v_header[size];
  2686. // ----- Add the file
  2687. if (($v_result = PclTarHandleAddFile($v_temp_tar, $v_current_filename, $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
  2688. {
  2689. // ----- Close the tarfile
  2690. if ($p_tar_mode == "tar")
  2691. {
  2692. fclose($v_tar);
  2693. fclose($v_temp_tar);
  2694. }
  2695. else
  2696. {
  2697. gzclose($v_tar);
  2698. gzclose($v_temp_tar);
  2699. }
  2700. @unlink($p_temp_tarname);
  2701. // ----- Return status
  2702. TrFctEnd(__FILE__, __LINE__, $v_result);
  2703. return $v_result;
  2704. }
  2705. // ----- Trace
  2706. TrFctMessage(__FILE__, __LINE__, 2, "Skip old file '$v_header[filename]'");
  2707. // ----- Jump to next file
  2708. if ($p_tar_mode == "tar")
  2709. fseek($v_tar, ftell($v_tar)+(ceil(($v_old_size/512))*512));
  2710. else
  2711. gzseek($v_tar, gztell($v_tar)+(ceil(($v_old_size/512))*512));
  2712. // ----- Add the array describing the file into the list
  2713. $p_list_detail[$v_nb] = $v_header;
  2714. $p_list_detail[$v_nb][status] = "updated";
  2715. // ----- Increment
  2716. $v_nb++;
  2717. }
  2718. // ----- Look for end of file
  2719. if ($p_tar_mode == "tar")
  2720. $v_end_of_file = feof($v_tar);
  2721. else
  2722. $v_end_of_file = gzeof($v_tar);
  2723. }
  2724. // ----- Look for files that does not exists in the archive and need to be added
  2725. for ($i=0; $i<sizeof($p_file_list); $i++)
  2726. {
  2727. // ----- Look if file not found in the archive
  2728. if (!$v_found_list[$i])
  2729. {
  2730. TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be added");
  2731. // ----- Add the file
  2732. if (($v_result = PclTarHandleAddFile($v_temp_tar, $p_file_list[$i], $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
  2733. {
  2734. // ----- Close the tarfile
  2735. if ($p_tar_mode == "tar")
  2736. {
  2737. fclose($v_tar);
  2738. fclose($v_temp_tar);
  2739. }
  2740. else
  2741. {
  2742. gzclose($v_tar);
  2743. gzclose($v_temp_tar);
  2744. }
  2745. @unlink($p_temp_tarname);
  2746. // ----- Return status
  2747. TrFctEnd(__FILE__, __LINE__, $v_result);
  2748. return $v_result;
  2749. }
  2750. // ----- Add the array describing the file into the list
  2751. $p_list_detail[$v_nb] = $v_header;
  2752. $p_list_detail[$v_nb][status] = "added";
  2753. // ----- Increment
  2754. $v_nb++;
  2755. }
  2756. else
  2757. {
  2758. TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' was already updated if needed");
  2759. }
  2760. }
  2761. // ----- Write the last empty buffer
  2762. PclTarHandleFooter($v_temp_tar, $p_tar_mode);
  2763. // ----- Close the tarfile
  2764. if ($p_tar_mode == "tar")
  2765. {
  2766. fclose($v_tar);
  2767. fclose($v_temp_tar);
  2768. }
  2769. else
  2770. {
  2771. gzclose($v_tar);
  2772. gzclose($v_temp_tar);
  2773. }
  2774. // ----- Unlink tar file
  2775. if (!@unlink($p_tarname))
  2776. {
  2777. // ----- Error log
  2778. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  2779. }
  2780. // ----- Rename tar file
  2781. if (!@rename($v_temp_tarname, $p_tarname))
  2782. {
  2783. // ----- Error log
  2784. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  2785. // ----- Return
  2786. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2787. return PclErrorCode();
  2788. }
  2789. // ----- Return
  2790. TrFctEnd(__FILE__, __LINE__, $v_result);
  2791. return $v_result;
  2792. }
  2793. // --------------------------------------------------------------------------------
  2794. // --------------------------------------------------------------------------------
  2795. // Function : PclTarHandleReadHeader()
  2796. // Description :
  2797. // Parameters :
  2798. // Return Values :
  2799. // --------------------------------------------------------------------------------
  2800. function PclTarHandleReadHeader($v_binary_data, &$v_header)
  2801. {
  2802. TrFctStart(__FILE__, __LINE__, "PclTarHandleReadHeader", "");
  2803. $v_result=1;
  2804. // ----- Read the 512 bytes header
  2805. /*
  2806. if ($p_tar_mode == "tar")
  2807. $v_binary_data = fread($p_tar, 512);
  2808. else
  2809. $v_binary_data = gzread($p_tar, 512);
  2810. */
  2811. // ----- Look for no more block
  2812. if (strlen($v_binary_data)==0)
  2813. {
  2814. $v_header[filename] = "";
  2815. $v_header[status] = "empty";
  2816. // ----- Return
  2817. TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found");
  2818. return $v_result;
  2819. }
  2820. // ----- Look for invalid block size
  2821. if (strlen($v_binary_data) != 512)
  2822. {
  2823. $v_header[filename] = "";
  2824. $v_header[status] = "invalid_header";
  2825. TrFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
  2826. // ----- Error log
  2827. PclErrorLog(-10, "Invalid block size : ".strlen($v_binary_data));
  2828. // ----- Return
  2829. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2830. return PclErrorCode();
  2831. }
  2832. // ----- Calculate the checksum
  2833. $v_checksum = 0;
  2834. // ..... First part of the header
  2835. for ($i=0; $i<148; $i++)
  2836. {
  2837. $v_checksum+=ord(substr($v_binary_data,$i,1));
  2838. }
  2839. // ..... Ignore the checksum value and replace it by ' ' (space)
  2840. for ($i=148; $i<156; $i++)
  2841. {
  2842. $v_checksum += ord(' ');
  2843. }
  2844. // ..... Last part of the header
  2845. for ($i=156; $i<512; $i++)
  2846. {
  2847. $v_checksum+=ord(substr($v_binary_data,$i,1));
  2848. }
  2849. TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
  2850. // ----- Extract the values
  2851. TrFctMessage(__FILE__, __LINE__, 2, "Header : '$v_binary_data'");
  2852. $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data);
  2853. // ----- Extract the checksum for check
  2854. $v_header[checksum] = OctDec(trim($v_data[checksum]));
  2855. TrFctMessage(__FILE__, __LINE__, 3, "File checksum : $v_header[checksum]");
  2856. if ($v_header[checksum] != $v_checksum)
  2857. {
  2858. TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : $v_checksum calculated, $v_header[checksum] expected");
  2859. $v_header[filename] = "";
  2860. $v_header[status] = "invalid_header";
  2861. // ----- Look for last block (empty block)
  2862. if (($v_checksum == 256) && ($v_header[checksum] == 0))
  2863. {
  2864. $v_header[status] = "empty";
  2865. // ----- Return
  2866. TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found");
  2867. return $v_result;
  2868. }
  2869. // ----- Error log
  2870. PclErrorLog(-13, "Invalid checksum : $v_checksum calculated, $v_header[checksum] expected");
  2871. // ----- Return
  2872. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2873. return PclErrorCode();
  2874. }
  2875. TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ($v_checksum)");
  2876. // ----- Extract the properties
  2877. $v_header[filename] = trim($v_data[filename]);
  2878. TrFctMessage(__FILE__, __LINE__, 2, "Name : '$v_header[filename]'");
  2879. $v_header[mode] = OctDec(trim($v_data[mode]));
  2880. TrFctMessage(__FILE__, __LINE__, 2, "Mode : '".DecOct($v_header[mode])."'");
  2881. $v_header[uid] = OctDec(trim($v_data[uid]));
  2882. TrFctMessage(__FILE__, __LINE__, 2, "Uid : '$v_header[uid]'");
  2883. $v_header[gid] = OctDec(trim($v_data[gid]));
  2884. TrFctMessage(__FILE__, __LINE__, 2, "Gid : '$v_header[gid]'");
  2885. $v_header[size] = OctDec(trim($v_data[size]));
  2886. TrFctMessage(__FILE__, __LINE__, 2, "Size : '$v_header[size]'");
  2887. $v_header[mtime] = OctDec(trim($v_data[mtime]));
  2888. TrFctMessage(__FILE__, __LINE__, 2, "Date : ".date("l dS of F Y h:i:s A", $v_header[mtime]));
  2889. if (($v_header[typeflag] = $v_data[typeflag]) == "5")
  2890. {
  2891. $v_header[size] = 0;
  2892. TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '$v_header[size]'");
  2893. }
  2894. TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : $v_header[typeflag]");
  2895. /* ----- All these fields are removed form the header because they do not carry interesting info
  2896. $v_header[link] = trim($v_data[link]);
  2897. TrFctMessage(__FILE__, __LINE__, 2, "Linkname : $v_header[linkname]");
  2898. $v_header[magic] = trim($v_data[magic]);
  2899. TrFctMessage(__FILE__, __LINE__, 2, "Magic : $v_header[magic]");
  2900. $v_header[version] = trim($v_data[version]);
  2901. TrFctMessage(__FILE__, __LINE__, 2, "Version : $v_header[version]");
  2902. $v_header[uname] = trim($v_data[uname]);
  2903. TrFctMessage(__FILE__, __LINE__, 2, "Uname : $v_header[uname]");
  2904. $v_header[gname] = trim($v_data[gname]);
  2905. TrFctMessage(__FILE__, __LINE__, 2, "Gname : $v_header[gname]");
  2906. $v_header[devmajor] = trim($v_data[devmajor]);
  2907. TrFctMessage(__FILE__, __LINE__, 2, "Devmajor : $v_header[devmajor]");
  2908. $v_header[devminor] = trim($v_data[devminor]);
  2909. TrFctMessage(__FILE__, __LINE__, 2, "Devminor : $v_header[devminor]");
  2910. */
  2911. // ----- Set the status field
  2912. $v_header[status] = "ok";
  2913. // ----- Return
  2914. TrFctEnd(__FILE__, __LINE__, $v_result);
  2915. return $v_result;
  2916. }
  2917. // --------------------------------------------------------------------------------
  2918. // --------------------------------------------------------------------------------
  2919. // Function : PclTarHandlerDirCheck()
  2920. // Description :
  2921. // Check if a directory exists, if not it creates it and all the parents directory
  2922. // which may be useful.
  2923. // Parameters :
  2924. // $p_dir : Directory path to check (without / at the end).
  2925. // Return Values :
  2926. // 1 : OK
  2927. // -1 : Unable to create directory
  2928. // --------------------------------------------------------------------------------
  2929. function PclTarHandlerDirCheck($p_dir)
  2930. {
  2931. $v_result = 1;
  2932. TrFctStart(__FILE__, __LINE__, "PclTarHandlerDirCheck", "$p_dir");
  2933. // ----- Check the directory availability
  2934. if ((is_dir($p_dir)) || ($p_dir == ""))
  2935. {
  2936. TrFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
  2937. return 1;
  2938. }
  2939. // ----- Look for file alone
  2940. /*
  2941. if (!strstr("$p_dir", "/"))
  2942. {
  2943. TrFctEnd(__FILE__, __LINE__, "'$p_dir' is a file with no directory");
  2944. return 1;
  2945. }
  2946. */
  2947. // ----- Extract parent directory
  2948. $p_parent_dir = dirname($p_dir);
  2949. TrFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
  2950. // ----- Just a check
  2951. if ($p_parent_dir != $p_dir)
  2952. {
  2953. // ----- Look for parent directory
  2954. if ($p_parent_dir != "")
  2955. {
  2956. if (($v_result = PclTarHandlerDirCheck($p_parent_dir)) != 1)
  2957. {
  2958. TrFctEnd(__FILE__, __LINE__, $v_result);
  2959. return $v_result;
  2960. }
  2961. }
  2962. }
  2963. // ----- Create the directory
  2964. TrFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
  2965. if (!@mkdir($p_dir, 0777))
  2966. {
  2967. // ----- Error log
  2968. PclErrorLog(-8, "Unable to create directory '$p_dir'");
  2969. // ----- Return
  2970. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2971. return PclErrorCode();
  2972. }
  2973. // ----- Return
  2974. TrFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
  2975. return $v_result;
  2976. }
  2977. // --------------------------------------------------------------------------------
  2978. // --------------------------------------------------------------------------------
  2979. // Function : PclTarHandleExtension()
  2980. // Description :
  2981. // Parameters :
  2982. // Return Values :
  2983. // --------------------------------------------------------------------------------
  2984. function PclTarHandleExtension($p_tarname)
  2985. {
  2986. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtension", "tar=$p_tarname");
  2987. // ----- Look for file extension
  2988. // NET2FTP
  2989. // This is because net2ftp adds a .txt extension to all uploaded files while they reside on the *web* server
  2990. $p_tarname = substr($p_tarname, 0, strlen($p_tarname)-4);
  2991. if ((substr($p_tarname, -7) == ".tar.gz") || (substr($p_tarname, -4) == ".tgz"))
  2992. {
  2993. TrFctMessage(__FILE__, __LINE__, 2, "Archive is a gzip tar");
  2994. $v_tar_mode = "tgz";
  2995. }
  2996. else if (substr($p_tarname, -4) == ".tar")
  2997. {
  2998. TrFctMessage(__FILE__, __LINE__, 2, "Archive is a tar");
  2999. $v_tar_mode = "tar";
  3000. }
  3001. else
  3002. {
  3003. // ----- Error log
  3004. PclErrorLog(-9, "Invalid archive extension");
  3005. TrFctMessage(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  3006. $v_tar_mode = "";
  3007. }
  3008. // ----- Return
  3009. TrFctEnd(__FILE__, __LINE__, $v_tar_mode);
  3010. return $v_tar_mode;
  3011. }
  3012. // --------------------------------------------------------------------------------
  3013. // --------------------------------------------------------------------------------
  3014. // Function : PclTarHandlePathReduction()
  3015. // Description :
  3016. // Parameters :
  3017. // Return Values :
  3018. // --------------------------------------------------------------------------------
  3019. function PclTarHandlePathReduction($p_dir)
  3020. {
  3021. TrFctStart(__FILE__, __LINE__, "PclTarHandlePathReduction", "dir='$p_dir'");
  3022. $v_result = "";
  3023. // ----- Look for not empty path
  3024. if ($p_dir != "")
  3025. {
  3026. // ----- Explode path by directory names
  3027. $v_list = explode("/", $p_dir);
  3028. // ----- Study directories from last to first
  3029. for ($i=sizeof($v_list)-1; $i>=0; $i--)
  3030. {
  3031. // ----- Look for current path
  3032. if ($v_list[$i] == ".")
  3033. {
  3034. // ----- Ignore this directory
  3035. // Should be the first $i=0, but no check is done
  3036. }
  3037. else if ($v_list[$i] == "..")
  3038. {
  3039. // ----- Ignore it and ignore the $i-1
  3040. $i--;
  3041. }
  3042. else if (($v_list[$i] == "") && ($i!=(sizeof($v_list)-1)) && ($i!=0))
  3043. {
  3044. // ----- Ignore only the double '//' in path,
  3045. // but not the first and last '/'
  3046. }
  3047. else
  3048. {
  3049. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  3050. }
  3051. }
  3052. }
  3053. // ----- Return
  3054. TrFctEnd(__FILE__, __LINE__, $v_result);
  3055. return $v_result;
  3056. }
  3057. // --------------------------------------------------------------------------------
  3058. // ----- End of double include look
  3059. }
  3060. ?>