Tar.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * File::CSV
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * Copyright (c) 1997-2008,
  9. * Vincent Blavet <vincent@phpconcept.net>
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * @category File_Formats
  33. * @package Archive_Tar
  34. * @author Vincent Blavet <vincent@phpconcept.net>
  35. * @copyright 1997-2010 The Authors
  36. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  37. * @version CVS: $Id: Tar.php 323476 2012-02-24 15:27:26Z mrook $
  38. * @link http://pear.php.net/package/Archive_Tar
  39. */
  40. require_once 'PEAR.php';
  41. define('ARCHIVE_TAR_ATT_SEPARATOR', 90001);
  42. define('ARCHIVE_TAR_END_BLOCK', pack("a512", ''));
  43. /**
  44. * Creates a (compressed) Tar archive
  45. *
  46. * @package Archive_Tar
  47. * @author Vincent Blavet <vincent@phpconcept.net>
  48. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  49. * @version $Revision: 323476 $
  50. */
  51. class Archive_Tar extends PEAR
  52. {
  53. /**
  54. * @var string Name of the Tar
  55. */
  56. var $_tarname='';
  57. /**
  58. * @var boolean if true, the Tar file will be gzipped
  59. */
  60. var $_compress=false;
  61. /**
  62. * @var string Type of compression : 'none', 'gz' or 'bz2'
  63. */
  64. var $_compress_type='none';
  65. /**
  66. * @var string Explode separator
  67. */
  68. var $_separator=' ';
  69. /**
  70. * @var file descriptor
  71. */
  72. var $_file=0;
  73. /**
  74. * @var string Local Tar name of a remote Tar (http:// or ftp://)
  75. */
  76. var $_temp_tarname='';
  77. /**
  78. * @var string regular expression for ignoring files or directories
  79. */
  80. var $_ignore_regexp='';
  81. /**
  82. * @var object PEAR_Error object
  83. */
  84. var $error_object=null;
  85. // {{{ constructor
  86. /**
  87. * Archive_Tar Class constructor. This flavour of the constructor only
  88. * declare a new Archive_Tar object, identifying it by the name of the
  89. * tar file.
  90. * If the compress argument is set the tar will be read or created as a
  91. * gzip or bz2 compressed TAR file.
  92. *
  93. * @param string $p_tarname The name of the tar archive to create
  94. * @param string $p_compress can be null, 'gz' or 'bz2'. This
  95. * parameter indicates if gzip or bz2 compression
  96. * is required. For compatibility reason the
  97. * boolean value 'true' means 'gz'.
  98. *
  99. * @access public
  100. */
  101. function Archive_Tar($p_tarname, $p_compress = null)
  102. {
  103. $this->PEAR();
  104. $this->_compress = false;
  105. $this->_compress_type = 'none';
  106. if (($p_compress === null) || ($p_compress == '')) {
  107. if (@file_exists($p_tarname)) {
  108. if ($fp = @fopen($p_tarname, "rb")) {
  109. // look for gzip magic cookie
  110. $data = fread($fp, 2);
  111. fclose($fp);
  112. if ($data == "\37\213") {
  113. $this->_compress = true;
  114. $this->_compress_type = 'gz';
  115. // No sure it's enought for a magic code ....
  116. } elseif ($data == "BZ") {
  117. $this->_compress = true;
  118. $this->_compress_type = 'bz2';
  119. }
  120. }
  121. } else {
  122. // probably a remote file or some file accessible
  123. // through a stream interface
  124. if (substr($p_tarname, -2) == 'gz') {
  125. $this->_compress = true;
  126. $this->_compress_type = 'gz';
  127. } elseif ((substr($p_tarname, -3) == 'bz2') ||
  128. (substr($p_tarname, -2) == 'bz')) {
  129. $this->_compress = true;
  130. $this->_compress_type = 'bz2';
  131. }
  132. }
  133. } else {
  134. if (($p_compress === true) || ($p_compress == 'gz')) {
  135. $this->_compress = true;
  136. $this->_compress_type = 'gz';
  137. } else if ($p_compress == 'bz2') {
  138. $this->_compress = true;
  139. $this->_compress_type = 'bz2';
  140. } else {
  141. $this->_error("Unsupported compression type '$p_compress'\n".
  142. "Supported types are 'gz' and 'bz2'.\n");
  143. return false;
  144. }
  145. }
  146. $this->_tarname = $p_tarname;
  147. if ($this->_compress) { // assert zlib or bz2 extension support
  148. if ($this->_compress_type == 'gz')
  149. $extname = 'zlib';
  150. else if ($this->_compress_type == 'bz2')
  151. $extname = 'bz2';
  152. if (!extension_loaded($extname)) {
  153. PEAR::loadExtension($extname);
  154. }
  155. if (!extension_loaded($extname)) {
  156. $this->_error("The extension '$extname' couldn't be found.\n".
  157. "Please make sure your version of PHP was built ".
  158. "with '$extname' support.\n");
  159. return false;
  160. }
  161. }
  162. }
  163. // }}}
  164. // {{{ destructor
  165. function _Archive_Tar()
  166. {
  167. $this->_close();
  168. // ----- Look for a local copy to delete
  169. if ($this->_temp_tarname != '')
  170. @unlink($this->_temp_tarname);
  171. $this->_PEAR();
  172. }
  173. // }}}
  174. // {{{ create()
  175. /**
  176. * This method creates the archive file and add the files / directories
  177. * that are listed in $p_filelist.
  178. * If a file with the same name exist and is writable, it is replaced
  179. * by the new tar.
  180. * The method return false and a PEAR error text.
  181. * The $p_filelist parameter can be an array of string, each string
  182. * representing a filename or a directory name with their path if
  183. * needed. It can also be a single string with names separated by a
  184. * single blank.
  185. * For each directory added in the archive, the files and
  186. * sub-directories are also added.
  187. * See also createModify() method for more details.
  188. *
  189. * @param array $p_filelist An array of filenames and directory names, or a
  190. * single string with names separated by a single
  191. * blank space.
  192. *
  193. * @return true on success, false on error.
  194. * @see createModify()
  195. * @access public
  196. */
  197. function create($p_filelist)
  198. {
  199. return $this->createModify($p_filelist, '', '');
  200. }
  201. // }}}
  202. // {{{ add()
  203. /**
  204. * This method add the files / directories that are listed in $p_filelist in
  205. * the archive. If the archive does not exist it is created.
  206. * The method return false and a PEAR error text.
  207. * The files and directories listed are only added at the end of the archive,
  208. * even if a file with the same name is already archived.
  209. * See also createModify() method for more details.
  210. *
  211. * @param array $p_filelist An array of filenames and directory names, or a
  212. * single string with names separated by a single
  213. * blank space.
  214. *
  215. * @return true on success, false on error.
  216. * @see createModify()
  217. * @access public
  218. */
  219. function add($p_filelist)
  220. {
  221. return $this->addModify($p_filelist, '', '');
  222. }
  223. // }}}
  224. // {{{ extract()
  225. function extract($p_path='', $p_preserve=false)
  226. {
  227. return $this->extractModify($p_path, '', $p_preserve);
  228. }
  229. // }}}
  230. // {{{ listContent()
  231. function listContent()
  232. {
  233. $v_list_detail = array();
  234. if ($this->_openRead()) {
  235. if (!$this->_extractList('', $v_list_detail, "list", '', '')) {
  236. unset($v_list_detail);
  237. $v_list_detail = 0;
  238. }
  239. $this->_close();
  240. }
  241. return $v_list_detail;
  242. }
  243. // }}}
  244. // {{{ createModify()
  245. /**
  246. * This method creates the archive file and add the files / directories
  247. * that are listed in $p_filelist.
  248. * If the file already exists and is writable, it is replaced by the
  249. * new tar. It is a create and not an add. If the file exists and is
  250. * read-only or is a directory it is not replaced. The method return
  251. * false and a PEAR error text.
  252. * The $p_filelist parameter can be an array of string, each string
  253. * representing a filename or a directory name with their path if
  254. * needed. It can also be a single string with names separated by a
  255. * single blank.
  256. * The path indicated in $p_remove_dir will be removed from the
  257. * memorized path of each file / directory listed when this path
  258. * exists. By default nothing is removed (empty path '')
  259. * The path indicated in $p_add_dir will be added at the beginning of
  260. * the memorized path of each file / directory listed. However it can
  261. * be set to empty ''. The adding of a path is done after the removing
  262. * of path.
  263. * The path add/remove ability enables the user to prepare an archive
  264. * for extraction in a different path than the origin files are.
  265. * See also addModify() method for file adding properties.
  266. *
  267. * @param array $p_filelist An array of filenames and directory names,
  268. * or a single string with names separated by
  269. * a single blank space.
  270. * @param string $p_add_dir A string which contains a path to be added
  271. * to the memorized path of each element in
  272. * the list.
  273. * @param string $p_remove_dir A string which contains a path to be
  274. * removed from the memorized path of each
  275. * element in the list, when relevant.
  276. *
  277. * @return boolean true on success, false on error.
  278. * @access public
  279. * @see addModify()
  280. */
  281. function createModify($p_filelist, $p_add_dir, $p_remove_dir='')
  282. {
  283. $v_result = true;
  284. if (!$this->_openWrite())
  285. return false;
  286. if ($p_filelist != '') {
  287. if (is_array($p_filelist))
  288. $v_list = $p_filelist;
  289. elseif (is_string($p_filelist))
  290. $v_list = explode($this->_separator, $p_filelist);
  291. else {
  292. $this->_cleanFile();
  293. $this->_error('Invalid file list');
  294. return false;
  295. }
  296. $v_result = $this->_addList($v_list, $p_add_dir, $p_remove_dir);
  297. }
  298. if ($v_result) {
  299. $this->_writeFooter();
  300. $this->_close();
  301. } else
  302. $this->_cleanFile();
  303. return $v_result;
  304. }
  305. // }}}
  306. // {{{ addModify()
  307. /**
  308. * This method add the files / directories listed in $p_filelist at the
  309. * end of the existing archive. If the archive does not yet exists it
  310. * is created.
  311. * The $p_filelist parameter can be an array of string, each string
  312. * representing a filename or a directory name with their path if
  313. * needed. It can also be a single string with names separated by a
  314. * single blank.
  315. * The path indicated in $p_remove_dir will be removed from the
  316. * memorized path of each file / directory listed when this path
  317. * exists. By default nothing is removed (empty path '')
  318. * The path indicated in $p_add_dir will be added at the beginning of
  319. * the memorized path of each file / directory listed. However it can
  320. * be set to empty ''. The adding of a path is done after the removing
  321. * of path.
  322. * The path add/remove ability enables the user to prepare an archive
  323. * for extraction in a different path than the origin files are.
  324. * If a file/dir is already in the archive it will only be added at the
  325. * end of the archive. There is no update of the existing archived
  326. * file/dir. However while extracting the archive, the last file will
  327. * replace the first one. This results in a none optimization of the
  328. * archive size.
  329. * If a file/dir does not exist the file/dir is ignored. However an
  330. * error text is send to PEAR error.
  331. * If a file/dir is not readable the file/dir is ignored. However an
  332. * error text is send to PEAR error.
  333. *
  334. * @param array $p_filelist An array of filenames and directory
  335. * names, or a single string with names
  336. * separated by a single blank space.
  337. * @param string $p_add_dir A string which contains a path to be
  338. * added to the memorized path of each
  339. * element in the list.
  340. * @param string $p_remove_dir A string which contains a path to be
  341. * removed from the memorized path of
  342. * each element in the list, when
  343. * relevant.
  344. *
  345. * @return true on success, false on error.
  346. * @access public
  347. */
  348. function addModify($p_filelist, $p_add_dir, $p_remove_dir='')
  349. {
  350. $v_result = true;
  351. if (!$this->_isArchive())
  352. $v_result = $this->createModify($p_filelist, $p_add_dir,
  353. $p_remove_dir);
  354. else {
  355. if (is_array($p_filelist))
  356. $v_list = $p_filelist;
  357. elseif (is_string($p_filelist))
  358. $v_list = explode($this->_separator, $p_filelist);
  359. else {
  360. $this->_error('Invalid file list');
  361. return false;
  362. }
  363. $v_result = $this->_append($v_list, $p_add_dir, $p_remove_dir);
  364. }
  365. return $v_result;
  366. }
  367. // }}}
  368. // {{{ addString()
  369. /**
  370. * This method add a single string as a file at the
  371. * end of the existing archive. If the archive does not yet exists it
  372. * is created.
  373. *
  374. * @param string $p_filename A string which contains the full
  375. * filename path that will be associated
  376. * with the string.
  377. * @param string $p_string The content of the file added in
  378. * the archive.
  379. *
  380. * @return true on success, false on error.
  381. * @access public
  382. */
  383. function addString($p_filename, $p_string)
  384. {
  385. $v_result = true;
  386. if (!$this->_isArchive()) {
  387. if (!$this->_openWrite()) {
  388. return false;
  389. }
  390. $this->_close();
  391. }
  392. if (!$this->_openAppend())
  393. return false;
  394. // Need to check the get back to the temporary file ? ....
  395. $v_result = $this->_addString($p_filename, $p_string);
  396. $this->_writeFooter();
  397. $this->_close();
  398. return $v_result;
  399. }
  400. // }}}
  401. // {{{ extractModify()
  402. /**
  403. * This method extract all the content of the archive in the directory
  404. * indicated by $p_path. When relevant the memorized path of the
  405. * files/dir can be modified by removing the $p_remove_path path at the
  406. * beginning of the file/dir path.
  407. * While extracting a file, if the directory path does not exists it is
  408. * created.
  409. * While extracting a file, if the file already exists it is replaced
  410. * without looking for last modification date.
  411. * While extracting a file, if the file already exists and is write
  412. * protected, the extraction is aborted.
  413. * While extracting a file, if a directory with the same name already
  414. * exists, the extraction is aborted.
  415. * While extracting a directory, if a file with the same name already
  416. * exists, the extraction is aborted.
  417. * While extracting a file/directory if the destination directory exist
  418. * and is write protected, or does not exist but can not be created,
  419. * the extraction is aborted.
  420. * If after extraction an extracted file does not show the correct
  421. * stored file size, the extraction is aborted.
  422. * When the extraction is aborted, a PEAR error text is set and false
  423. * is returned. However the result can be a partial extraction that may
  424. * need to be manually cleaned.
  425. *
  426. * @param string $p_path The path of the directory where the
  427. * files/dir need to by extracted.
  428. * @param string $p_remove_path Part of the memorized path that can be
  429. * removed if present at the beginning of
  430. * the file/dir path.
  431. * @param boolean $p_preserve Preserve user/group ownership of files
  432. *
  433. * @return boolean true on success, false on error.
  434. * @access public
  435. * @see extractList()
  436. */
  437. function extractModify($p_path, $p_remove_path, $p_preserve=false)
  438. {
  439. $v_result = true;
  440. $v_list_detail = array();
  441. if ($v_result = $this->_openRead()) {
  442. $v_result = $this->_extractList($p_path, $v_list_detail,
  443. "complete", 0, $p_remove_path, $p_preserve);
  444. $this->_close();
  445. }
  446. return $v_result;
  447. }
  448. // }}}
  449. // {{{ extractInString()
  450. /**
  451. * This method extract from the archive one file identified by $p_filename.
  452. * The return value is a string with the file content, or NULL on error.
  453. *
  454. * @param string $p_filename The path of the file to extract in a string.
  455. *
  456. * @return a string with the file content or NULL.
  457. * @access public
  458. */
  459. function extractInString($p_filename)
  460. {
  461. if ($this->_openRead()) {
  462. $v_result = $this->_extractInString($p_filename);
  463. $this->_close();
  464. } else {
  465. $v_result = null;
  466. }
  467. return $v_result;
  468. }
  469. // }}}
  470. // {{{ extractList()
  471. /**
  472. * This method extract from the archive only the files indicated in the
  473. * $p_filelist. These files are extracted in the current directory or
  474. * in the directory indicated by the optional $p_path parameter.
  475. * If indicated the $p_remove_path can be used in the same way as it is
  476. * used in extractModify() method.
  477. *
  478. * @param array $p_filelist An array of filenames and directory names,
  479. * or a single string with names separated
  480. * by a single blank space.
  481. * @param string $p_path The path of the directory where the
  482. * files/dir need to by extracted.
  483. * @param string $p_remove_path Part of the memorized path that can be
  484. * removed if present at the beginning of
  485. * the file/dir path.
  486. * @param boolean $p_preserve Preserve user/group ownership of files
  487. *
  488. * @return true on success, false on error.
  489. * @access public
  490. * @see extractModify()
  491. */
  492. function extractList($p_filelist, $p_path='', $p_remove_path='', $p_preserve=false)
  493. {
  494. $v_result = true;
  495. $v_list_detail = array();
  496. if (is_array($p_filelist))
  497. $v_list = $p_filelist;
  498. elseif (is_string($p_filelist))
  499. $v_list = explode($this->_separator, $p_filelist);
  500. else {
  501. $this->_error('Invalid string list');
  502. return false;
  503. }
  504. if ($v_result = $this->_openRead()) {
  505. $v_result = $this->_extractList($p_path, $v_list_detail, "partial",
  506. $v_list, $p_remove_path, $p_preserve);
  507. $this->_close();
  508. }
  509. return $v_result;
  510. }
  511. // }}}
  512. // {{{ setAttribute()
  513. /**
  514. * This method set specific attributes of the archive. It uses a variable
  515. * list of parameters, in the format attribute code + attribute values :
  516. * $arch->setAttribute(ARCHIVE_TAR_ATT_SEPARATOR, ',');
  517. *
  518. * @param mixed $argv variable list of attributes and values
  519. *
  520. * @return true on success, false on error.
  521. * @access public
  522. */
  523. function setAttribute()
  524. {
  525. $v_result = true;
  526. // ----- Get the number of variable list of arguments
  527. if (($v_size = func_num_args()) == 0) {
  528. return true;
  529. }
  530. // ----- Get the arguments
  531. $v_att_list = &func_get_args();
  532. // ----- Read the attributes
  533. $i=0;
  534. while ($i<$v_size) {
  535. // ----- Look for next option
  536. switch ($v_att_list[$i]) {
  537. // ----- Look for options that request a string value
  538. case ARCHIVE_TAR_ATT_SEPARATOR :
  539. // ----- Check the number of parameters
  540. if (($i+1) >= $v_size) {
  541. $this->_error('Invalid number of parameters for '
  542. .'attribute ARCHIVE_TAR_ATT_SEPARATOR');
  543. return false;
  544. }
  545. // ----- Get the value
  546. $this->_separator = $v_att_list[$i+1];
  547. $i++;
  548. break;
  549. default :
  550. $this->_error('Unknow attribute code '.$v_att_list[$i].'');
  551. return false;
  552. }
  553. // ----- Next attribute
  554. $i++;
  555. }
  556. return $v_result;
  557. }
  558. // }}}
  559. // {{{ setIgnoreRegexp()
  560. /**
  561. * This method sets the regular expression for ignoring files and directories
  562. * at import, for example:
  563. * $arch->setIgnoreRegexp("#CVS|\.svn#");
  564. *
  565. * @param string $regexp regular expression defining which files or directories to ignore
  566. *
  567. * @access public
  568. */
  569. function setIgnoreRegexp($regexp)
  570. {
  571. $this->_ignore_regexp = $regexp;
  572. }
  573. // }}}
  574. // {{{ setIgnoreList()
  575. /**
  576. * This method sets the regular expression for ignoring all files and directories
  577. * matching the filenames in the array list at import, for example:
  578. * $arch->setIgnoreList(array('CVS', '.svn', 'bin/tool'));
  579. *
  580. * @param array $list a list of file or directory names to ignore
  581. *
  582. * @access public
  583. */
  584. function setIgnoreList($list)
  585. {
  586. $regexp = str_replace(array('#', '.', '^', '$'), array('\#', '\.', '\^', '\$'), $list);
  587. $regexp = '#/'.join('$|/', $list).'#';
  588. $this->setIgnoreRegexp($regexp);
  589. }
  590. // }}}
  591. // {{{ _error()
  592. function _error($p_message)
  593. {
  594. $this->error_object = $this->raiseError($p_message);
  595. }
  596. // }}}
  597. // {{{ _warning()
  598. function _warning($p_message)
  599. {
  600. $this->error_object = $this->raiseError($p_message);
  601. }
  602. // }}}
  603. // {{{ _isArchive()
  604. function _isArchive($p_filename=null)
  605. {
  606. if ($p_filename == null) {
  607. $p_filename = $this->_tarname;
  608. }
  609. clearstatcache();
  610. return @is_file($p_filename) && !@is_link($p_filename);
  611. }
  612. // }}}
  613. // {{{ _openWrite()
  614. function _openWrite()
  615. {
  616. if ($this->_compress_type == 'gz' && function_exists('gzopen'))
  617. $this->_file = @gzopen($this->_tarname, "wb9");
  618. else if ($this->_compress_type == 'bz2' && function_exists('bzopen'))
  619. $this->_file = @bzopen($this->_tarname, "w");
  620. else if ($this->_compress_type == 'none')
  621. $this->_file = @fopen($this->_tarname, "wb");
  622. else
  623. $this->_error('Unknown or missing compression type ('
  624. .$this->_compress_type.')');
  625. if ($this->_file == 0) {
  626. $this->_error('Unable to open in write mode \''
  627. .$this->_tarname.'\'');
  628. return false;
  629. }
  630. return true;
  631. }
  632. // }}}
  633. // {{{ _openRead()
  634. function _openRead()
  635. {
  636. if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') {
  637. // ----- Look if a local copy need to be done
  638. if ($this->_temp_tarname == '') {
  639. $this->_temp_tarname = uniqid('tar').'.tmp';
  640. if (!$v_file_from = @fopen($this->_tarname, 'rb')) {
  641. $this->_error('Unable to open in read mode \''
  642. .$this->_tarname.'\'');
  643. $this->_temp_tarname = '';
  644. return false;
  645. }
  646. if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) {
  647. $this->_error('Unable to open in write mode \''
  648. .$this->_temp_tarname.'\'');
  649. $this->_temp_tarname = '';
  650. return false;
  651. }
  652. while ($v_data = @fread($v_file_from, 1024))
  653. @fwrite($v_file_to, $v_data);
  654. @fclose($v_file_from);
  655. @fclose($v_file_to);
  656. }
  657. // ----- File to open if the local copy
  658. $v_filename = $this->_temp_tarname;
  659. } else
  660. // ----- File to open if the normal Tar file
  661. $v_filename = $this->_tarname;
  662. if ($this->_compress_type == 'gz')
  663. $this->_file = @gzopen($v_filename, "rb");
  664. else if ($this->_compress_type == 'bz2')
  665. $this->_file = @bzopen($v_filename, "r");
  666. else if ($this->_compress_type == 'none')
  667. $this->_file = @fopen($v_filename, "rb");
  668. else
  669. $this->_error('Unknown or missing compression type ('
  670. .$this->_compress_type.')');
  671. if ($this->_file == 0) {
  672. $this->_error('Unable to open in read mode \''.$v_filename.'\'');
  673. return false;
  674. }
  675. return true;
  676. }
  677. // }}}
  678. // {{{ _openReadWrite()
  679. function _openReadWrite()
  680. {
  681. if ($this->_compress_type == 'gz')
  682. $this->_file = @gzopen($this->_tarname, "r+b");
  683. else if ($this->_compress_type == 'bz2') {
  684. $this->_error('Unable to open bz2 in read/write mode \''
  685. .$this->_tarname.'\' (limitation of bz2 extension)');
  686. return false;
  687. } else if ($this->_compress_type == 'none')
  688. $this->_file = @fopen($this->_tarname, "r+b");
  689. else
  690. $this->_error('Unknown or missing compression type ('
  691. .$this->_compress_type.')');
  692. if ($this->_file == 0) {
  693. $this->_error('Unable to open in read/write mode \''
  694. .$this->_tarname.'\'');
  695. return false;
  696. }
  697. return true;
  698. }
  699. // }}}
  700. // {{{ _close()
  701. function _close()
  702. {
  703. //if (isset($this->_file)) {
  704. if (is_resource($this->_file)) {
  705. if ($this->_compress_type == 'gz')
  706. @gzclose($this->_file);
  707. else if ($this->_compress_type == 'bz2')
  708. @bzclose($this->_file);
  709. else if ($this->_compress_type == 'none')
  710. @fclose($this->_file);
  711. else
  712. $this->_error('Unknown or missing compression type ('
  713. .$this->_compress_type.')');
  714. $this->_file = 0;
  715. }
  716. // ----- Look if a local copy need to be erase
  717. // Note that it might be interesting to keep the url for a time : ToDo
  718. if ($this->_temp_tarname != '') {
  719. @unlink($this->_temp_tarname);
  720. $this->_temp_tarname = '';
  721. }
  722. return true;
  723. }
  724. // }}}
  725. // {{{ _cleanFile()
  726. function _cleanFile()
  727. {
  728. $this->_close();
  729. // ----- Look for a local copy
  730. if ($this->_temp_tarname != '') {
  731. // ----- Remove the local copy but not the remote tarname
  732. @unlink($this->_temp_tarname);
  733. $this->_temp_tarname = '';
  734. } else {
  735. // ----- Remove the local tarname file
  736. @unlink($this->_tarname);
  737. }
  738. $this->_tarname = '';
  739. return true;
  740. }
  741. // }}}
  742. // {{{ _writeBlock()
  743. function _writeBlock($p_binary_data, $p_len=null)
  744. {
  745. if (is_resource($this->_file)) {
  746. if ($p_len === null) {
  747. if ($this->_compress_type == 'gz')
  748. @gzputs($this->_file, $p_binary_data);
  749. else if ($this->_compress_type == 'bz2')
  750. @bzwrite($this->_file, $p_binary_data);
  751. else if ($this->_compress_type == 'none')
  752. @fputs($this->_file, $p_binary_data);
  753. else
  754. $this->_error('Unknown or missing compression type ('
  755. .$this->_compress_type.')');
  756. } else {
  757. if ($this->_compress_type == 'gz')
  758. @gzputs($this->_file, $p_binary_data, $p_len);
  759. else if ($this->_compress_type == 'bz2')
  760. @bzwrite($this->_file, $p_binary_data, $p_len);
  761. else if ($this->_compress_type == 'none')
  762. @fputs($this->_file, $p_binary_data, $p_len);
  763. else
  764. $this->_error('Unknown or missing compression type ('
  765. .$this->_compress_type.')');
  766. }
  767. }
  768. return true;
  769. }
  770. // }}}
  771. // {{{ _readBlock()
  772. function _readBlock()
  773. {
  774. $v_block = null;
  775. if (is_resource($this->_file)) {
  776. if ($this->_compress_type == 'gz')
  777. $v_block = @gzread($this->_file, 512);
  778. else if ($this->_compress_type == 'bz2')
  779. $v_block = @bzread($this->_file, 512);
  780. else if ($this->_compress_type == 'none')
  781. $v_block = @fread($this->_file, 512);
  782. else
  783. $this->_error('Unknown or missing compression type ('
  784. .$this->_compress_type.')');
  785. }
  786. return $v_block;
  787. }
  788. // }}}
  789. // {{{ _jumpBlock()
  790. function _jumpBlock($p_len=null)
  791. {
  792. if (is_resource($this->_file)) {
  793. if ($p_len === null)
  794. $p_len = 1;
  795. if ($this->_compress_type == 'gz') {
  796. @gzseek($this->_file, gztell($this->_file)+($p_len*512));
  797. }
  798. else if ($this->_compress_type == 'bz2') {
  799. // ----- Replace missing bztell() and bzseek()
  800. for ($i=0; $i<$p_len; $i++)
  801. $this->_readBlock();
  802. } else if ($this->_compress_type == 'none')
  803. @fseek($this->_file, $p_len*512, SEEK_CUR);
  804. else
  805. $this->_error('Unknown or missing compression type ('
  806. .$this->_compress_type.')');
  807. }
  808. return true;
  809. }
  810. // }}}
  811. // {{{ _writeFooter()
  812. function _writeFooter()
  813. {
  814. if (is_resource($this->_file)) {
  815. // ----- Write the last 0 filled block for end of archive
  816. $v_binary_data = pack('a1024', '');
  817. $this->_writeBlock($v_binary_data);
  818. }
  819. return true;
  820. }
  821. // }}}
  822. // {{{ _addList()
  823. function _addList($p_list, $p_add_dir, $p_remove_dir)
  824. {
  825. $v_result=true;
  826. $v_header = array();
  827. // ----- Remove potential windows directory separator
  828. $p_add_dir = $this->_translateWinPath($p_add_dir);
  829. $p_remove_dir = $this->_translateWinPath($p_remove_dir, false);
  830. if (!$this->_file) {
  831. $this->_error('Invalid file descriptor');
  832. return false;
  833. }
  834. if (sizeof($p_list) == 0)
  835. return true;
  836. foreach ($p_list as $v_filename) {
  837. if (!$v_result) {
  838. break;
  839. }
  840. // ----- Skip the current tar name
  841. if ($v_filename == $this->_tarname)
  842. continue;
  843. if ($v_filename == '')
  844. continue;
  845. // ----- ignore files and directories matching the ignore regular expression
  846. if ($this->_ignore_regexp && preg_match($this->_ignore_regexp, '/'.$v_filename)) {
  847. $this->_warning("File '$v_filename' ignored");
  848. continue;
  849. }
  850. if (!file_exists($v_filename) && !is_link($v_filename)) {
  851. $this->_warning("File '$v_filename' does not exist");
  852. continue;
  853. }
  854. // ----- Add the file or directory header
  855. if (!$this->_addFile($v_filename, $v_header, $p_add_dir, $p_remove_dir))
  856. return false;
  857. if (@is_dir($v_filename) && !@is_link($v_filename)) {
  858. if (!($p_hdir = opendir($v_filename))) {
  859. $this->_warning("Directory '$v_filename' can not be read");
  860. continue;
  861. }
  862. while (false !== ($p_hitem = readdir($p_hdir))) {
  863. if (($p_hitem != '.') && ($p_hitem != '..')) {
  864. if ($v_filename != ".")
  865. $p_temp_list[0] = $v_filename.'/'.$p_hitem;
  866. else
  867. $p_temp_list[0] = $p_hitem;
  868. $v_result = $this->_addList($p_temp_list,
  869. $p_add_dir,
  870. $p_remove_dir);
  871. }
  872. }
  873. unset($p_temp_list);
  874. unset($p_hdir);
  875. unset($p_hitem);
  876. }
  877. }
  878. return $v_result;
  879. }
  880. // }}}
  881. // {{{ _addFile()
  882. function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir,$v_stored_filename=null)
  883. {
  884. if (!$this->_file) {
  885. $this->_error('Invalid file descriptor');
  886. return false;
  887. }
  888. if ($p_filename == '') {
  889. $this->_error('Invalid file name');
  890. return false;
  891. }
  892. if(is_null($v_stored_filename)){
  893. // ----- Calculate the stored filename
  894. $p_filename = $this->_translateWinPath($p_filename, false);
  895. $v_stored_filename = $p_filename;
  896. if (strcmp($p_filename, $p_remove_dir) == 0) {
  897. return true;
  898. }
  899. if ($p_remove_dir != '') {
  900. if (substr($p_remove_dir, -1) != '/')
  901. $p_remove_dir .= '/';
  902. if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
  903. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  904. }
  905. $v_stored_filename = $this->_translateWinPath($v_stored_filename);
  906. if ($p_add_dir != '') {
  907. if (substr($p_add_dir, -1) == '/')
  908. $v_stored_filename = $p_add_dir.$v_stored_filename;
  909. else
  910. $v_stored_filename = $p_add_dir.'/'.$v_stored_filename;
  911. }
  912. $v_stored_filename = $this->_pathReduction($v_stored_filename);
  913. }
  914. if ($this->_isArchive($p_filename)) {
  915. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  916. $this->_warning("Unable to open file '".$p_filename
  917. ."' in binary read mode");
  918. return true;
  919. }
  920. if (!$this->_writeHeader($p_filename, $v_stored_filename))
  921. return false;
  922. while (($v_buffer = fread($v_file, 512)) != '') {
  923. $v_binary_data = pack("a512", "$v_buffer");
  924. $this->_writeBlock($v_binary_data);
  925. }
  926. fclose($v_file);
  927. } else {
  928. // ----- Only header for dir
  929. if (!$this->_writeHeader($p_filename, $v_stored_filename))
  930. return false;
  931. }
  932. return true;
  933. }
  934. // }}}
  935. // {{{ _addString()
  936. function _addString($p_filename, $p_string)
  937. {
  938. if (!$this->_file) {
  939. $this->_error('Invalid file descriptor');
  940. return false;
  941. }
  942. if ($p_filename == '') {
  943. $this->_error('Invalid file name');
  944. return false;
  945. }
  946. // ----- Calculate the stored filename
  947. $p_filename = $this->_translateWinPath($p_filename, false);;
  948. if (!$this->_writeHeaderBlock($p_filename, strlen($p_string),
  949. time(), 384, "", 0, 0))
  950. return false;
  951. $i=0;
  952. while (($v_buffer = substr($p_string, (($i++)*512), 512)) != '') {
  953. $v_binary_data = pack("a512", $v_buffer);
  954. $this->_writeBlock($v_binary_data);
  955. }
  956. return true;
  957. }
  958. // }}}
  959. // {{{ _writeHeader()
  960. function _writeHeader($p_filename, $p_stored_filename)
  961. {
  962. if ($p_stored_filename == '')
  963. $p_stored_filename = $p_filename;
  964. $v_reduce_filename = $this->_pathReduction($p_stored_filename);
  965. if (strlen($v_reduce_filename) > 99) {
  966. if (!$this->_writeLongHeader($v_reduce_filename))
  967. return false;
  968. }
  969. $v_info = lstat($p_filename);
  970. $v_uid = sprintf("%07s", DecOct($v_info[4]));
  971. $v_gid = sprintf("%07s", DecOct($v_info[5]));
  972. $v_perms = sprintf("%07s", DecOct($v_info['mode'] & 000777));
  973. $v_mtime = sprintf("%011s", DecOct($v_info['mtime']));
  974. $v_linkname = '';
  975. if (@is_link($p_filename)) {
  976. $v_typeflag = '2';
  977. $v_linkname = readlink($p_filename);
  978. $v_size = sprintf("%011s", DecOct(0));
  979. } elseif (@is_dir($p_filename)) {
  980. $v_typeflag = "5";
  981. $v_size = sprintf("%011s", DecOct(0));
  982. } else {
  983. $v_typeflag = '0';
  984. clearstatcache();
  985. $v_size = sprintf("%011s", DecOct($v_info['size']));
  986. }
  987. $v_magic = 'ustar ';
  988. $v_version = ' ';
  989. if (function_exists('posix_getpwuid'))
  990. {
  991. $userinfo = posix_getpwuid($v_info[4]);
  992. $groupinfo = posix_getgrgid($v_info[5]);
  993. $v_uname = $userinfo['name'];
  994. $v_gname = $groupinfo['name'];
  995. }
  996. else
  997. {
  998. $v_uname = '';
  999. $v_gname = '';
  1000. }
  1001. $v_devmajor = '';
  1002. $v_devminor = '';
  1003. $v_prefix = '';
  1004. $v_binary_data_first = pack("a100a8a8a8a12a12",
  1005. $v_reduce_filename, $v_perms, $v_uid,
  1006. $v_gid, $v_size, $v_mtime);
  1007. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1008. $v_typeflag, $v_linkname, $v_magic,
  1009. $v_version, $v_uname, $v_gname,
  1010. $v_devmajor, $v_devminor, $v_prefix, '');
  1011. // ----- Calculate the checksum
  1012. $v_checksum = 0;
  1013. // ..... First part of the header
  1014. for ($i=0; $i<148; $i++)
  1015. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1016. // ..... Ignore the checksum value and replace it by ' ' (space)
  1017. for ($i=148; $i<156; $i++)
  1018. $v_checksum += ord(' ');
  1019. // ..... Last part of the header
  1020. for ($i=156, $j=0; $i<512; $i++, $j++)
  1021. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1022. // ----- Write the first 148 bytes of the header in the archive
  1023. $this->_writeBlock($v_binary_data_first, 148);
  1024. // ----- Write the calculated checksum
  1025. $v_checksum = sprintf("%06s ", DecOct($v_checksum));
  1026. $v_binary_data = pack("a8", $v_checksum);
  1027. $this->_writeBlock($v_binary_data, 8);
  1028. // ----- Write the last 356 bytes of the header in the archive
  1029. $this->_writeBlock($v_binary_data_last, 356);
  1030. return true;
  1031. }
  1032. // }}}
  1033. // {{{ _writeHeaderBlock()
  1034. function _writeHeaderBlock($p_filename, $p_size, $p_mtime=0, $p_perms=0,
  1035. $p_type='', $p_uid=0, $p_gid=0)
  1036. {
  1037. $p_filename = $this->_pathReduction($p_filename);
  1038. if (strlen($p_filename) > 99) {
  1039. if (!$this->_writeLongHeader($p_filename))
  1040. return false;
  1041. }
  1042. if ($p_type == "5") {
  1043. $v_size = sprintf("%011s", DecOct(0));
  1044. } else {
  1045. $v_size = sprintf("%011s", DecOct($p_size));
  1046. }
  1047. $v_uid = sprintf("%07s", DecOct($p_uid));
  1048. $v_gid = sprintf("%07s", DecOct($p_gid));
  1049. $v_perms = sprintf("%07s", DecOct($p_perms & 000777));
  1050. $v_mtime = sprintf("%11s", DecOct($p_mtime));
  1051. $v_linkname = '';
  1052. $v_magic = 'ustar ';
  1053. $v_version = ' ';
  1054. if (function_exists('posix_getpwuid'))
  1055. {
  1056. $userinfo = posix_getpwuid($p_uid);
  1057. $groupinfo = posix_getgrgid($p_gid);
  1058. $v_uname = $userinfo['name'];
  1059. $v_gname = $groupinfo['name'];
  1060. }
  1061. else
  1062. {
  1063. $v_uname = '';
  1064. $v_gname = '';
  1065. }
  1066. $v_devmajor = '';
  1067. $v_devminor = '';
  1068. $v_prefix = '';
  1069. $v_binary_data_first = pack("a100a8a8a8a12A12",
  1070. $p_filename, $v_perms, $v_uid, $v_gid,
  1071. $v_size, $v_mtime);
  1072. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1073. $p_type, $v_linkname, $v_magic,
  1074. $v_version, $v_uname, $v_gname,
  1075. $v_devmajor, $v_devminor, $v_prefix, '');
  1076. // ----- Calculate the checksum
  1077. $v_checksum = 0;
  1078. // ..... First part of the header
  1079. for ($i=0; $i<148; $i++)
  1080. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1081. // ..... Ignore the checksum value and replace it by ' ' (space)
  1082. for ($i=148; $i<156; $i++)
  1083. $v_checksum += ord(' ');
  1084. // ..... Last part of the header
  1085. for ($i=156, $j=0; $i<512; $i++, $j++)
  1086. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1087. // ----- Write the first 148 bytes of the header in the archive
  1088. $this->_writeBlock($v_binary_data_first, 148);
  1089. // ----- Write the calculated checksum
  1090. $v_checksum = sprintf("%06s ", DecOct($v_checksum));
  1091. $v_binary_data = pack("a8", $v_checksum);
  1092. $this->_writeBlock($v_binary_data, 8);
  1093. // ----- Write the last 356 bytes of the header in the archive
  1094. $this->_writeBlock($v_binary_data_last, 356);
  1095. return true;
  1096. }
  1097. // }}}
  1098. // {{{ _writeLongHeader()
  1099. function _writeLongHeader($p_filename)
  1100. {
  1101. $v_size = sprintf("%11s ", DecOct(strlen($p_filename)));
  1102. $v_typeflag = 'L';
  1103. $v_linkname = '';
  1104. $v_magic = '';
  1105. $v_version = '';
  1106. $v_uname = '';
  1107. $v_gname = '';
  1108. $v_devmajor = '';
  1109. $v_devminor = '';
  1110. $v_prefix = '';
  1111. $v_binary_data_first = pack("a100a8a8a8a12a12",
  1112. '././@LongLink', 0, 0, 0, $v_size, 0);
  1113. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1114. $v_typeflag, $v_linkname, $v_magic,
  1115. $v_version, $v_uname, $v_gname,
  1116. $v_devmajor, $v_devminor, $v_prefix, '');
  1117. // ----- Calculate the checksum
  1118. $v_checksum = 0;
  1119. // ..... First part of the header
  1120. for ($i=0; $i<148; $i++)
  1121. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1122. // ..... Ignore the checksum value and replace it by ' ' (space)
  1123. for ($i=148; $i<156; $i++)
  1124. $v_checksum += ord(' ');
  1125. // ..... Last part of the header
  1126. for ($i=156, $j=0; $i<512; $i++, $j++)
  1127. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1128. // ----- Write the first 148 bytes of the header in the archive
  1129. $this->_writeBlock($v_binary_data_first, 148);
  1130. // ----- Write the calculated checksum
  1131. $v_checksum = sprintf("%06s ", DecOct($v_checksum));
  1132. $v_binary_data = pack("a8", $v_checksum);
  1133. $this->_writeBlock($v_binary_data, 8);
  1134. // ----- Write the last 356 bytes of the header in the archive
  1135. $this->_writeBlock($v_binary_data_last, 356);
  1136. // ----- Write the filename as content of the block
  1137. $i=0;
  1138. while (($v_buffer = substr($p_filename, (($i++)*512), 512)) != '') {
  1139. $v_binary_data = pack("a512", "$v_buffer");
  1140. $this->_writeBlock($v_binary_data);
  1141. }
  1142. return true;
  1143. }
  1144. // }}}
  1145. // {{{ _readHeader()
  1146. function _readHeader($v_binary_data, &$v_header)
  1147. {
  1148. if (strlen($v_binary_data)==0) {
  1149. $v_header['filename'] = '';
  1150. return true;
  1151. }
  1152. if (strlen($v_binary_data) != 512) {
  1153. $v_header['filename'] = '';
  1154. $this->_error('Invalid block size : '.strlen($v_binary_data));
  1155. return false;
  1156. }
  1157. if (!is_array($v_header)) {
  1158. $v_header = array();
  1159. }
  1160. // ----- Calculate the checksum
  1161. $v_checksum = 0;
  1162. // ..... First part of the header
  1163. for ($i=0; $i<148; $i++)
  1164. $v_checksum+=ord(substr($v_binary_data,$i,1));
  1165. // ..... Ignore the checksum value and replace it by ' ' (space)
  1166. for ($i=148; $i<156; $i++)
  1167. $v_checksum += ord(' ');
  1168. // ..... Last part of the header
  1169. for ($i=156; $i<512; $i++)
  1170. $v_checksum+=ord(substr($v_binary_data,$i,1));
  1171. $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" .
  1172. "a8checksum/a1typeflag/a100link/a6magic/a2version/" .
  1173. "a32uname/a32gname/a8devmajor/a8devminor/a131prefix",
  1174. $v_binary_data);
  1175. if (strlen($v_data["prefix"]) > 0) {
  1176. $v_data["filename"] = "$v_data[prefix]/$v_data[filename]";
  1177. }
  1178. // ----- Extract the checksum
  1179. $v_header['checksum'] = OctDec(trim($v_data['checksum']));
  1180. if ($v_header['checksum'] != $v_checksum) {
  1181. $v_header['filename'] = '';
  1182. // ----- Look for last block (empty block)
  1183. if (($v_checksum == 256) && ($v_header['checksum'] == 0))
  1184. return true;
  1185. $this->_error('Invalid checksum for file "'.$v_data['filename']
  1186. .'" : '.$v_checksum.' calculated, '
  1187. .$v_header['checksum'].' expected');
  1188. return false;
  1189. }
  1190. // ----- Extract the properties
  1191. $v_header['filename'] = $v_data['filename'];
  1192. if ($this->_maliciousFilename($v_header['filename'])) {
  1193. $this->_error('Malicious .tar detected, file "' . $v_header['filename'] .
  1194. '" will not install in desired directory tree');
  1195. return false;
  1196. }
  1197. $v_header['mode'] = OctDec(trim($v_data['mode']));
  1198. $v_header['uid'] = OctDec(trim($v_data['uid']));
  1199. $v_header['gid'] = OctDec(trim($v_data['gid']));
  1200. $v_header['size'] = OctDec(trim($v_data['size']));
  1201. $v_header['mtime'] = OctDec(trim($v_data['mtime']));
  1202. if (($v_header['typeflag'] = $v_data['typeflag']) == "5") {
  1203. $v_header['size'] = 0;
  1204. }
  1205. $v_header['link'] = trim($v_data['link']);
  1206. /* ----- All these fields are removed form the header because
  1207. they do not carry interesting info
  1208. $v_header[magic] = trim($v_data[magic]);
  1209. $v_header[version] = trim($v_data[version]);
  1210. $v_header[uname] = trim($v_data[uname]);
  1211. $v_header[gname] = trim($v_data[gname]);
  1212. $v_header[devmajor] = trim($v_data[devmajor]);
  1213. $v_header[devminor] = trim($v_data[devminor]);
  1214. */
  1215. return true;
  1216. }
  1217. // }}}
  1218. // {{{ _maliciousFilename()
  1219. /**
  1220. * Detect and report a malicious file name
  1221. *
  1222. * @param string $file
  1223. *
  1224. * @return bool
  1225. * @access private
  1226. */
  1227. function _maliciousFilename($file)
  1228. {
  1229. if (strpos($file, '/../') !== false) {
  1230. return true;
  1231. }
  1232. if (strpos($file, '../') === 0) {
  1233. return true;
  1234. }
  1235. return false;
  1236. }
  1237. // }}}
  1238. // {{{ _readLongHeader()
  1239. function _readLongHeader(&$v_header)
  1240. {
  1241. $v_filename = '';
  1242. $n = floor($v_header['size']/512);
  1243. for ($i=0; $i<$n; $i++) {
  1244. $v_content = $this->_readBlock();
  1245. $v_filename .= $v_content;
  1246. }
  1247. if (($v_header['size'] % 512) != 0) {
  1248. $v_content = $this->_readBlock();
  1249. $v_filename .= trim($v_content);
  1250. }
  1251. // ----- Read the next header
  1252. $v_binary_data = $this->_readBlock();
  1253. if (!$this->_readHeader($v_binary_data, $v_header))
  1254. return false;
  1255. $v_filename = trim($v_filename);
  1256. $v_header['filename'] = $v_filename;
  1257. if ($this->_maliciousFilename($v_filename)) {
  1258. $this->_error('Malicious .tar detected, file "' . $v_filename .
  1259. '" will not install in desired directory tree');
  1260. return false;
  1261. }
  1262. return true;
  1263. }
  1264. // }}}
  1265. // {{{ _extractInString()
  1266. /**
  1267. * This method extract from the archive one file identified by $p_filename.
  1268. * The return value is a string with the file content, or null on error.
  1269. *
  1270. * @param string $p_filename The path of the file to extract in a string.
  1271. *
  1272. * @return a string with the file content or null.
  1273. * @access private
  1274. */
  1275. function _extractInString($p_filename)
  1276. {
  1277. $v_result_str = "";
  1278. While (strlen($v_binary_data = $this->_readBlock()) != 0)
  1279. {
  1280. if (!$this->_readHeader($v_binary_data, $v_header))
  1281. return null;
  1282. if ($v_header['filename'] == '')
  1283. continue;
  1284. // ----- Look for long filename
  1285. if ($v_header['typeflag'] == 'L') {
  1286. if (!$this->_readLongHeader($v_header))
  1287. return null;
  1288. }
  1289. if ($v_header['filename'] == $p_filename) {
  1290. if ($v_header['typeflag'] == "5") {
  1291. $this->_error('Unable to extract in string a directory '
  1292. .'entry {'.$v_header['filename'].'}');
  1293. return null;
  1294. } else {
  1295. $n = floor($v_header['size']/512);
  1296. for ($i=0; $i<$n; $i++) {
  1297. $v_result_str .= $this->_readBlock();
  1298. }
  1299. if (($v_header['size'] % 512) != 0) {
  1300. $v_content = $this->_readBlock();
  1301. $v_result_str .= substr($v_content, 0,
  1302. ($v_header['size'] % 512));
  1303. }
  1304. return $v_result_str;
  1305. }
  1306. } else {
  1307. $this->_jumpBlock(ceil(($v_header['size']/512)));
  1308. }
  1309. }
  1310. return null;
  1311. }
  1312. // }}}
  1313. // {{{ _extractList()
  1314. function _extractList($p_path, &$p_list_detail, $p_mode,
  1315. $p_file_list, $p_remove_path, $p_preserve=false)
  1316. {
  1317. $v_result=true;
  1318. $v_nb = 0;
  1319. $v_extract_all = true;
  1320. $v_listing = false;
  1321. $p_path = $this->_translateWinPath($p_path, false);
  1322. if ($p_path == '' || (substr($p_path, 0, 1) != '/'
  1323. && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))) {
  1324. $p_path = "./".$p_path;
  1325. }
  1326. $p_remove_path = $this->_translateWinPath($p_remove_path);
  1327. // ----- Look for path to remove format (should end by /)
  1328. if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/'))
  1329. $p_remove_path .= '/';
  1330. $p_remove_path_size = strlen($p_remove_path);
  1331. switch ($p_mode) {
  1332. case "complete" :
  1333. $v_extract_all = true;
  1334. $v_listing = false;
  1335. break;
  1336. case "partial" :
  1337. $v_extract_all = false;
  1338. $v_listing = false;
  1339. break;
  1340. case "list" :
  1341. $v_extract_all = false;
  1342. $v_listing = true;
  1343. break;
  1344. default :
  1345. $this->_error('Invalid extract mode ('.$p_mode.')');
  1346. return false;
  1347. }
  1348. clearstatcache();
  1349. while (strlen($v_binary_data = $this->_readBlock()) != 0)
  1350. {
  1351. $v_extract_file = FALSE;
  1352. $v_extraction_stopped = 0;
  1353. if (!$this->_readHeader($v_binary_data, $v_header))
  1354. return false;
  1355. if ($v_header['filename'] == '') {
  1356. continue;
  1357. }
  1358. // ----- Look for long filename
  1359. if ($v_header['typeflag'] == 'L') {
  1360. if (!$this->_readLongHeader($v_header))
  1361. return false;
  1362. }
  1363. if ((!$v_extract_all) && (is_array($p_file_list))) {
  1364. // ----- By default no unzip if the file is not found
  1365. $v_extract_file = false;
  1366. for ($i=0; $i<sizeof($p_file_list); $i++) {
  1367. // ----- Look if it is a directory
  1368. if (substr($p_file_list[$i], -1) == '/') {
  1369. // ----- Look if the directory is in the filename path
  1370. if ((strlen($v_header['filename']) > strlen($p_file_list[$i]))
  1371. && (substr($v_header['filename'], 0, strlen($p_file_list[$i]))
  1372. == $p_file_list[$i])) {
  1373. $v_extract_file = true;
  1374. break;
  1375. }
  1376. }
  1377. // ----- It is a file, so compare the file names
  1378. elseif ($p_file_list[$i] == $v_header['filename']) {
  1379. $v_extract_file = true;
  1380. break;
  1381. }
  1382. }
  1383. } else {
  1384. $v_extract_file = true;
  1385. }
  1386. // ----- Look if this file need to be extracted
  1387. if (($v_extract_file) && (!$v_listing))
  1388. {
  1389. if (($p_remove_path != '')
  1390. && (substr($v_header['filename'], 0, $p_remove_path_size)
  1391. == $p_remove_path))
  1392. $v_header['filename'] = substr($v_header['filename'],
  1393. $p_remove_path_size);
  1394. if (($p_path != './') && ($p_path != '/')) {
  1395. while (substr($p_path, -1) == '/')
  1396. $p_path = substr($p_path, 0, strlen($p_path)-1);
  1397. if (substr($v_header['filename'], 0, 1) == '/')
  1398. $v_header['filename'] = $p_path.$v_header['filename'];
  1399. else
  1400. $v_header['filename'] = $p_path.'/'.$v_header['filename'];
  1401. }
  1402. if (file_exists($v_header['filename'])) {
  1403. if ( (@is_dir($v_header['filename']))
  1404. && ($v_header['typeflag'] == '')) {
  1405. $this->_error('File '.$v_header['filename']
  1406. .' already exists as a directory');
  1407. return false;
  1408. }
  1409. if ( ($this->_isArchive($v_header['filename']))
  1410. && ($v_header['typeflag'] == "5")) {
  1411. $this->_error('Directory '.$v_header['filename']
  1412. .' already exists as a file');
  1413. return false;
  1414. }
  1415. if (!is_writeable($v_header['filename'])) {
  1416. $this->_error('File '.$v_header['filename']
  1417. .' already exists and is write protected');
  1418. return false;
  1419. }
  1420. if (filemtime($v_header['filename']) > $v_header['mtime']) {
  1421. // To be completed : An error or silent no replace ?
  1422. }
  1423. }
  1424. // ----- Check the directory availability and create it if necessary
  1425. elseif (($v_result
  1426. = $this->_dirCheck(($v_header['typeflag'] == "5"
  1427. ?$v_header['filename']
  1428. :dirname($v_header['filename'])))) != 1) {
  1429. $this->_error('Unable to create path for '.$v_header['filename']);
  1430. return false;
  1431. }
  1432. if ($v_extract_file) {
  1433. if ($v_header['typeflag'] == "5") {
  1434. if (!@file_exists($v_header['filename'])) {
  1435. if (!@mkdir($v_header['filename'], 0777)) {
  1436. $this->_error('Unable to create directory {'
  1437. .$v_header['filename'].'}');
  1438. return false;
  1439. }
  1440. }
  1441. } elseif ($v_header['typeflag'] == "2") {
  1442. if (@file_exists($v_header['filename'])) {
  1443. @unlink($v_header['filename']);
  1444. }
  1445. if (!@symlink($v_header['link'], $v_header['filename'])) {
  1446. $this->_error('Unable to extract symbolic link {'
  1447. .$v_header['filename'].'}');
  1448. return false;
  1449. }
  1450. } else {
  1451. if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
  1452. $this->_error('Error while opening {'.$v_header['filename']
  1453. .'} in write binary mode');
  1454. return false;
  1455. } else {
  1456. $n = floor($v_header['size']/512);
  1457. for ($i=0; $i<$n; $i++) {
  1458. $v_content = $this->_readBlock();
  1459. fwrite($v_dest_file, $v_content, 512);
  1460. }
  1461. if (($v_header['size'] % 512) != 0) {
  1462. $v_content = $this->_readBlock();
  1463. fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
  1464. }
  1465. @fclose($v_dest_file);
  1466. if ($p_preserve) {
  1467. @chown($v_header['filename'], $v_header['uid']);
  1468. @chgrp($v_header['filename'], $v_header['gid']);
  1469. }
  1470. // ----- Change the file mode, mtime
  1471. @touch($v_header['filename'], $v_header['mtime']);
  1472. if ($v_header['mode'] & 0111) {
  1473. // make file executable, obey umask
  1474. $mode = fileperms($v_header['filename']) | (~umask() & 0111);
  1475. @chmod($v_header['filename'], $mode);
  1476. }
  1477. }
  1478. // ----- Check the file size
  1479. clearstatcache();
  1480. if (!is_file($v_header['filename'])) {
  1481. $this->_error('Extracted file '.$v_header['filename']
  1482. .'does not exist. Archive may be corrupted.');
  1483. return false;
  1484. }
  1485. $filesize = filesize($v_header['filename']);
  1486. if ($filesize != $v_header['size']) {
  1487. $this->_error('Extracted file '.$v_header['filename']
  1488. .' does not have the correct file size \''
  1489. .$filesize
  1490. .'\' ('.$v_header['size']
  1491. .' expected). Archive may be corrupted.');
  1492. return false;
  1493. }
  1494. }
  1495. } else {
  1496. $this->_jumpBlock(ceil(($v_header['size']/512)));
  1497. }
  1498. } else {
  1499. $this->_jumpBlock(ceil(($v_header['size']/512)));
  1500. }
  1501. /* TBC : Seems to be unused ...
  1502. if ($this->_compress)
  1503. $v_end_of_file = @gzeof($this->_file);
  1504. else
  1505. $v_end_of_file = @feof($this->_file);
  1506. */
  1507. if ($v_listing || $v_extract_file || $v_extraction_stopped) {
  1508. // ----- Log extracted files
  1509. if (($v_file_dir = dirname($v_header['filename']))
  1510. == $v_header['filename'])
  1511. $v_file_dir = '';
  1512. if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == ''))
  1513. $v_file_dir = '/';
  1514. $p_list_detail[$v_nb++] = $v_header;
  1515. if (is_array($p_file_list) && (count($p_list_detail) == count($p_file_list))) {
  1516. return true;
  1517. }
  1518. }
  1519. }
  1520. return true;
  1521. }
  1522. // }}}
  1523. // {{{ _openAppend()
  1524. function _openAppend()
  1525. {
  1526. if (filesize($this->_tarname) == 0)
  1527. return $this->_openWrite();
  1528. if ($this->_compress) {
  1529. $this->_close();
  1530. if (!@rename($this->_tarname, $this->_tarname.".tmp")) {
  1531. $this->_error('Error while renaming \''.$this->_tarname
  1532. .'\' to temporary file \''.$this->_tarname
  1533. .'.tmp\'');
  1534. return false;
  1535. }
  1536. if ($this->_compress_type == 'gz')
  1537. $v_temp_tar = @gzopen($this->_tarname.".tmp", "rb");
  1538. elseif ($this->_compress_type == 'bz2')
  1539. $v_temp_tar = @bzopen($this->_tarname.".tmp", "r");
  1540. if ($v_temp_tar == 0) {
  1541. $this->_error('Unable to open file \''.$this->_tarname
  1542. .'.tmp\' in binary read mode');
  1543. @rename($this->_tarname.".tmp", $this->_tarname);
  1544. return false;
  1545. }
  1546. if (!$this->_openWrite()) {
  1547. @rename($this->_tarname.".tmp", $this->_tarname);
  1548. return false;
  1549. }
  1550. if ($this->_compress_type == 'gz') {
  1551. while (!@gzeof($v_temp_tar)) {
  1552. $v_buffer = @gzread($v_temp_tar, 512);
  1553. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  1554. // do not copy end blocks, we will re-make them
  1555. // after appending
  1556. continue;
  1557. }
  1558. $v_binary_data = pack("a512", $v_buffer);
  1559. $this->_writeBlock($v_binary_data);
  1560. }
  1561. @gzclose($v_temp_tar);
  1562. }
  1563. elseif ($this->_compress_type == 'bz2') {
  1564. while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) {
  1565. if ($v_buffer == ARCHIVE_TAR_END_BLOCK) {
  1566. continue;
  1567. }
  1568. $v_binary_data = pack("a512", $v_buffer);
  1569. $this->_writeBlock($v_binary_data);
  1570. }
  1571. @bzclose($v_temp_tar);
  1572. }
  1573. if (!@unlink($this->_tarname.".tmp")) {
  1574. $this->_error('Error while deleting temporary file \''
  1575. .$this->_tarname.'.tmp\'');
  1576. }
  1577. } else {
  1578. // ----- For not compressed tar, just add files before the last
  1579. // one or two 512 bytes block
  1580. if (!$this->_openReadWrite())
  1581. return false;
  1582. clearstatcache();
  1583. $v_size = filesize($this->_tarname);
  1584. // We might have zero, one or two end blocks.
  1585. // The standard is two, but we should try to handle
  1586. // other cases.
  1587. fseek($this->_file, $v_size - 1024);
  1588. if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  1589. fseek($this->_file, $v_size - 1024);
  1590. }
  1591. elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  1592. fseek($this->_file, $v_size - 512);
  1593. }
  1594. }
  1595. return true;
  1596. }
  1597. // }}}
  1598. // {{{ _append()
  1599. function _append($p_filelist, $p_add_dir='', $p_remove_dir='')
  1600. {
  1601. if (!$this->_openAppend())
  1602. return false;
  1603. if ($this->_addList($p_filelist, $p_add_dir, $p_remove_dir))
  1604. $this->_writeFooter();
  1605. $this->_close();
  1606. return true;
  1607. }
  1608. // }}}
  1609. // {{{ _dirCheck()
  1610. /**
  1611. * Check if a directory exists and create it (including parent
  1612. * dirs) if not.
  1613. *
  1614. * @param string $p_dir directory to check
  1615. *
  1616. * @return bool true if the directory exists or was created
  1617. */
  1618. function _dirCheck($p_dir)
  1619. {
  1620. clearstatcache();
  1621. if ((@is_dir($p_dir)) || ($p_dir == ''))
  1622. return true;
  1623. $p_parent_dir = dirname($p_dir);
  1624. if (($p_parent_dir != $p_dir) &&
  1625. ($p_parent_dir != '') &&
  1626. (!$this->_dirCheck($p_parent_dir)))
  1627. return false;
  1628. if (!@mkdir($p_dir, 0777)) {
  1629. $this->_error("Unable to create directory '$p_dir'");
  1630. return false;
  1631. }
  1632. return true;
  1633. }
  1634. // }}}
  1635. // {{{ _pathReduction()
  1636. /**
  1637. * Compress path by changing for example "/dir/foo/../bar" to "/dir/bar",
  1638. * rand emove double slashes.
  1639. *
  1640. * @param string $p_dir path to reduce
  1641. *
  1642. * @return string reduced path
  1643. *
  1644. * @access private
  1645. *
  1646. */
  1647. function _pathReduction($p_dir)
  1648. {
  1649. $v_result = '';
  1650. // ----- Look for not empty path
  1651. if ($p_dir != '') {
  1652. // ----- Explode path by directory names
  1653. $v_list = explode('/', $p_dir);
  1654. // ----- Study directories from last to first
  1655. for ($i=sizeof($v_list)-1; $i>=0; $i--) {
  1656. // ----- Look for current path
  1657. if ($v_list[$i] == ".") {
  1658. // ----- Ignore this directory
  1659. // Should be the first $i=0, but no check is done
  1660. }
  1661. else if ($v_list[$i] == "..") {
  1662. // ----- Ignore it and ignore the $i-1
  1663. $i--;
  1664. }
  1665. else if ( ($v_list[$i] == '')
  1666. && ($i!=(sizeof($v_list)-1))
  1667. && ($i!=0)) {
  1668. // ----- Ignore only the double '//' in path,
  1669. // but not the first and last /
  1670. } else {
  1671. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?'/'
  1672. .$v_result:'');
  1673. }
  1674. }
  1675. }
  1676. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  1677. $v_result = strtr($v_result, '\\', '/');
  1678. }
  1679. return $v_result;
  1680. }
  1681. // }}}
  1682. // {{{ _translateWinPath()
  1683. function _translateWinPath($p_path, $p_remove_disk_letter=true)
  1684. {
  1685. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  1686. // ----- Look for potential disk letter
  1687. if ( ($p_remove_disk_letter)
  1688. && (($v_position = strpos($p_path, ':')) != false)) {
  1689. $p_path = substr($p_path, $v_position+1);
  1690. }
  1691. // ----- Change potential windows directory separator
  1692. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
  1693. $p_path = strtr($p_path, '\\', '/');
  1694. }
  1695. }
  1696. return $p_path;
  1697. }
  1698. // }}}
  1699. }
  1700. ?>