Tar.php 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473
  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$
  38. * @link http://pear.php.net/package/Archive_Tar
  39. */
  40. // If the PEAR class cannot be loaded via the autoloader,
  41. // then try to require_once it from the PHP include path.
  42. if (!class_exists('PEAR')) {
  43. require_once 'PEAR.php';
  44. }
  45. define('ARCHIVE_TAR_ATT_SEPARATOR', 90001);
  46. define('ARCHIVE_TAR_END_BLOCK', pack("a512", ''));
  47. if (!function_exists('gzopen') && function_exists('gzopen64')) {
  48. function gzopen($filename, $mode, $use_include_path = 0)
  49. {
  50. return gzopen64($filename, $mode, $use_include_path);
  51. }
  52. }
  53. if (!function_exists('gztell') && function_exists('gztell64')) {
  54. function gztell($zp)
  55. {
  56. return gztell64($zp);
  57. }
  58. }
  59. if (!function_exists('gzseek') && function_exists('gzseek64')) {
  60. function gzseek($zp, $offset, $whence = SEEK_SET)
  61. {
  62. return gzseek64($zp, $offset, $whence);
  63. }
  64. }
  65. /**
  66. * Creates a (compressed) Tar archive
  67. *
  68. * @package Archive_Tar
  69. * @author Vincent Blavet <vincent@phpconcept.net>
  70. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  71. * @version $Revision$
  72. */
  73. class Archive_Tar extends PEAR
  74. {
  75. /**
  76. * @var string Name of the Tar
  77. */
  78. public $_tarname = '';
  79. /**
  80. * @var boolean if true, the Tar file will be gzipped
  81. */
  82. public $_compress = false;
  83. /**
  84. * @var string Type of compression : 'none', 'gz', 'bz2' or 'lzma2'
  85. */
  86. public $_compress_type = 'none';
  87. /**
  88. * @var string Explode separator
  89. */
  90. public $_separator = ' ';
  91. /**
  92. * @var file descriptor
  93. */
  94. public $_file = 0;
  95. /**
  96. * @var string Local Tar name of a remote Tar (http:// or ftp://)
  97. */
  98. public $_temp_tarname = '';
  99. /**
  100. * @var string regular expression for ignoring files or directories
  101. */
  102. public $_ignore_regexp = '';
  103. /**
  104. * @var object PEAR_Error object
  105. */
  106. public $error_object = null;
  107. /**
  108. * Format for data extraction
  109. *
  110. * @var string
  111. */
  112. public $_fmt = '';
  113. /**
  114. * @var int Length of the read buffer in bytes
  115. */
  116. protected $buffer_length;
  117. /**
  118. * Archive_Tar Class constructor. This flavour of the constructor only
  119. * declare a new Archive_Tar object, identifying it by the name of the
  120. * tar file.
  121. * If the compress argument is set the tar will be read or created as a
  122. * gzip or bz2 compressed TAR file.
  123. *
  124. * @param string $p_tarname The name of the tar archive to create
  125. * @param string $p_compress can be null, 'gz', 'bz2' or 'lzma2'. This
  126. * parameter indicates if gzip, bz2 or lzma2 compression
  127. * is required. For compatibility reason the
  128. * boolean value 'true' means 'gz'.
  129. * @param int $buffer_length Length of the read buffer in bytes
  130. *
  131. * @return bool
  132. */
  133. public function __construct($p_tarname, $p_compress = null, $buffer_length = 512)
  134. {
  135. parent::__construct();
  136. $this->_compress = false;
  137. $this->_compress_type = 'none';
  138. if (($p_compress === null) || ($p_compress == '')) {
  139. if (@file_exists($p_tarname)) {
  140. if ($fp = @fopen($p_tarname, "rb")) {
  141. // look for gzip magic cookie
  142. $data = fread($fp, 2);
  143. fclose($fp);
  144. if ($data == "\37\213") {
  145. $this->_compress = true;
  146. $this->_compress_type = 'gz';
  147. // No sure it's enought for a magic code ....
  148. } elseif ($data == "BZ") {
  149. $this->_compress = true;
  150. $this->_compress_type = 'bz2';
  151. } elseif (file_get_contents($p_tarname, false, null, 1, 4) == '7zXZ') {
  152. $this->_compress = true;
  153. $this->_compress_type = 'lzma2';
  154. }
  155. }
  156. } else {
  157. // probably a remote file or some file accessible
  158. // through a stream interface
  159. if (substr($p_tarname, -2) == 'gz') {
  160. $this->_compress = true;
  161. $this->_compress_type = 'gz';
  162. } elseif ((substr($p_tarname, -3) == 'bz2') ||
  163. (substr($p_tarname, -2) == 'bz')
  164. ) {
  165. $this->_compress = true;
  166. $this->_compress_type = 'bz2';
  167. } else {
  168. if (substr($p_tarname, -2) == 'xz') {
  169. $this->_compress = true;
  170. $this->_compress_type = 'lzma2';
  171. }
  172. }
  173. }
  174. } else {
  175. if (($p_compress === true) || ($p_compress == 'gz')) {
  176. $this->_compress = true;
  177. $this->_compress_type = 'gz';
  178. } else {
  179. if ($p_compress == 'bz2') {
  180. $this->_compress = true;
  181. $this->_compress_type = 'bz2';
  182. } else {
  183. if ($p_compress == 'lzma2') {
  184. $this->_compress = true;
  185. $this->_compress_type = 'lzma2';
  186. } else {
  187. $this->_error(
  188. "Unsupported compression type '$p_compress'\n" .
  189. "Supported types are 'gz', 'bz2' and 'lzma2'.\n"
  190. );
  191. return false;
  192. }
  193. }
  194. }
  195. }
  196. $this->_tarname = $p_tarname;
  197. if ($this->_compress) { // assert zlib or bz2 or xz extension support
  198. if ($this->_compress_type == 'gz') {
  199. $extname = 'zlib';
  200. } else {
  201. if ($this->_compress_type == 'bz2') {
  202. $extname = 'bz2';
  203. } else {
  204. if ($this->_compress_type == 'lzma2') {
  205. $extname = 'xz';
  206. }
  207. }
  208. }
  209. if (!extension_loaded($extname)) {
  210. PEAR::loadExtension($extname);
  211. }
  212. if (!extension_loaded($extname)) {
  213. $this->_error(
  214. "The extension '$extname' couldn't be found.\n" .
  215. "Please make sure your version of PHP was built " .
  216. "with '$extname' support.\n"
  217. );
  218. return false;
  219. }
  220. }
  221. if (version_compare(PHP_VERSION, "5.5.0-dev") < 0) {
  222. $this->_fmt = "a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" .
  223. "a8checksum/a1typeflag/a100link/a6magic/a2version/" .
  224. "a32uname/a32gname/a8devmajor/a8devminor/a131prefix";
  225. } else {
  226. $this->_fmt = "Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/" .
  227. "Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/" .
  228. "Z32uname/Z32gname/Z8devmajor/Z8devminor/Z131prefix";
  229. }
  230. $this->buffer_length = $buffer_length;
  231. }
  232. public function __destruct()
  233. {
  234. $this->_close();
  235. // ----- Look for a local copy to delete
  236. if ($this->_temp_tarname != '') {
  237. @unlink($this->_temp_tarname);
  238. }
  239. }
  240. /**
  241. * This method creates the archive file and add the files / directories
  242. * that are listed in $p_filelist.
  243. * If a file with the same name exist and is writable, it is replaced
  244. * by the new tar.
  245. * The method return false and a PEAR error text.
  246. * The $p_filelist parameter can be an array of string, each string
  247. * representing a filename or a directory name with their path if
  248. * needed. It can also be a single string with names separated by a
  249. * single blank.
  250. * For each directory added in the archive, the files and
  251. * sub-directories are also added.
  252. * See also createModify() method for more details.
  253. *
  254. * @param array $p_filelist An array of filenames and directory names, or a
  255. * single string with names separated by a single
  256. * blank space.
  257. *
  258. * @return true on success, false on error.
  259. * @see createModify()
  260. */
  261. public function create($p_filelist)
  262. {
  263. return $this->createModify($p_filelist, '', '');
  264. }
  265. /**
  266. * This method add the files / directories that are listed in $p_filelist in
  267. * the archive. If the archive does not exist it is created.
  268. * The method return false and a PEAR error text.
  269. * The files and directories listed are only added at the end of the archive,
  270. * even if a file with the same name is already archived.
  271. * See also createModify() method for more details.
  272. *
  273. * @param array $p_filelist An array of filenames and directory names, or a
  274. * single string with names separated by a single
  275. * blank space.
  276. *
  277. * @return true on success, false on error.
  278. * @see createModify()
  279. * @access public
  280. */
  281. public function add($p_filelist)
  282. {
  283. return $this->addModify($p_filelist, '', '');
  284. }
  285. /**
  286. * @param string $p_path
  287. * @param bool $p_preserve
  288. * @return bool
  289. */
  290. public function extract($p_path = '', $p_preserve = false)
  291. {
  292. return $this->extractModify($p_path, '', $p_preserve);
  293. }
  294. /**
  295. * @return array|int
  296. */
  297. public function listContent()
  298. {
  299. $v_list_detail = array();
  300. if ($this->_openRead()) {
  301. if (!$this->_extractList('', $v_list_detail, "list", '', '')) {
  302. unset($v_list_detail);
  303. $v_list_detail = 0;
  304. }
  305. $this->_close();
  306. }
  307. return $v_list_detail;
  308. }
  309. /**
  310. * This method creates the archive file and add the files / directories
  311. * that are listed in $p_filelist.
  312. * If the file already exists and is writable, it is replaced by the
  313. * new tar. It is a create and not an add. If the file exists and is
  314. * read-only or is a directory it is not replaced. The method return
  315. * false and a PEAR error text.
  316. * The $p_filelist parameter can be an array of string, each string
  317. * representing a filename or a directory name with their path if
  318. * needed. It can also be a single string with names separated by a
  319. * single blank.
  320. * The path indicated in $p_remove_dir will be removed from the
  321. * memorized path of each file / directory listed when this path
  322. * exists. By default nothing is removed (empty path '')
  323. * The path indicated in $p_add_dir will be added at the beginning of
  324. * the memorized path of each file / directory listed. However it can
  325. * be set to empty ''. The adding of a path is done after the removing
  326. * of path.
  327. * The path add/remove ability enables the user to prepare an archive
  328. * for extraction in a different path than the origin files are.
  329. * See also addModify() method for file adding properties.
  330. *
  331. * @param array $p_filelist An array of filenames and directory names,
  332. * or a single string with names separated by
  333. * a single blank space.
  334. * @param string $p_add_dir A string which contains a path to be added
  335. * to the memorized path of each element in
  336. * the list.
  337. * @param string $p_remove_dir A string which contains a path to be
  338. * removed from the memorized path of each
  339. * element in the list, when relevant.
  340. *
  341. * @return boolean true on success, false on error.
  342. * @see addModify()
  343. */
  344. public function createModify($p_filelist, $p_add_dir, $p_remove_dir = '')
  345. {
  346. $v_result = true;
  347. if (!$this->_openWrite()) {
  348. return false;
  349. }
  350. if ($p_filelist != '') {
  351. if (is_array($p_filelist)) {
  352. $v_list = $p_filelist;
  353. } elseif (is_string($p_filelist)) {
  354. $v_list = explode($this->_separator, $p_filelist);
  355. } else {
  356. $this->_cleanFile();
  357. $this->_error('Invalid file list');
  358. return false;
  359. }
  360. $v_result = $this->_addList($v_list, $p_add_dir, $p_remove_dir);
  361. }
  362. if ($v_result) {
  363. $this->_writeFooter();
  364. $this->_close();
  365. } else {
  366. $this->_cleanFile();
  367. }
  368. return $v_result;
  369. }
  370. /**
  371. * This method add the files / directories listed in $p_filelist at the
  372. * end of the existing archive. If the archive does not yet exists it
  373. * is created.
  374. * The $p_filelist parameter can be an array of string, each string
  375. * representing a filename or a directory name with their path if
  376. * needed. It can also be a single string with names separated by a
  377. * single blank.
  378. * The path indicated in $p_remove_dir will be removed from the
  379. * memorized path of each file / directory listed when this path
  380. * exists. By default nothing is removed (empty path '')
  381. * The path indicated in $p_add_dir will be added at the beginning of
  382. * the memorized path of each file / directory listed. However it can
  383. * be set to empty ''. The adding of a path is done after the removing
  384. * of path.
  385. * The path add/remove ability enables the user to prepare an archive
  386. * for extraction in a different path than the origin files are.
  387. * If a file/dir is already in the archive it will only be added at the
  388. * end of the archive. There is no update of the existing archived
  389. * file/dir. However while extracting the archive, the last file will
  390. * replace the first one. This results in a none optimization of the
  391. * archive size.
  392. * If a file/dir does not exist the file/dir is ignored. However an
  393. * error text is send to PEAR error.
  394. * If a file/dir is not readable the file/dir is ignored. However an
  395. * error text is send to PEAR error.
  396. *
  397. * @param array $p_filelist An array of filenames and directory
  398. * names, or a single string with names
  399. * separated by a single blank space.
  400. * @param string $p_add_dir A string which contains a path to be
  401. * added to the memorized path of each
  402. * element in the list.
  403. * @param string $p_remove_dir A string which contains a path to be
  404. * removed from the memorized path of
  405. * each element in the list, when
  406. * relevant.
  407. *
  408. * @return true on success, false on error.
  409. */
  410. public function addModify($p_filelist, $p_add_dir, $p_remove_dir = '')
  411. {
  412. $v_result = true;
  413. if (!$this->_isArchive()) {
  414. $v_result = $this->createModify(
  415. $p_filelist,
  416. $p_add_dir,
  417. $p_remove_dir
  418. );
  419. } else {
  420. if (is_array($p_filelist)) {
  421. $v_list = $p_filelist;
  422. } elseif (is_string($p_filelist)) {
  423. $v_list = explode($this->_separator, $p_filelist);
  424. } else {
  425. $this->_error('Invalid file list');
  426. return false;
  427. }
  428. $v_result = $this->_append($v_list, $p_add_dir, $p_remove_dir);
  429. }
  430. return $v_result;
  431. }
  432. /**
  433. * This method add a single string as a file at the
  434. * end of the existing archive. If the archive does not yet exists it
  435. * is created.
  436. *
  437. * @param string $p_filename A string which contains the full
  438. * filename path that will be associated
  439. * with the string.
  440. * @param string $p_string The content of the file added in
  441. * the archive.
  442. * @param bool|int $p_datetime A custom date/time (unix timestamp)
  443. * for the file (optional).
  444. * @param array $p_params An array of optional params:
  445. * stamp => the datetime (replaces
  446. * datetime above if it exists)
  447. * mode => the permissions on the
  448. * file (600 by default)
  449. * type => is this a link? See the
  450. * tar specification for details.
  451. * (default = regular file)
  452. * uid => the user ID of the file
  453. * (default = 0 = root)
  454. * gid => the group ID of the file
  455. * (default = 0 = root)
  456. *
  457. * @return true on success, false on error.
  458. */
  459. public function addString($p_filename, $p_string, $p_datetime = false, $p_params = array())
  460. {
  461. $p_stamp = @$p_params["stamp"] ? $p_params["stamp"] : ($p_datetime ? $p_datetime : time());
  462. $p_mode = @$p_params["mode"] ? $p_params["mode"] : 0600;
  463. $p_type = @$p_params["type"] ? $p_params["type"] : "";
  464. $p_uid = @$p_params["uid"] ? $p_params["uid"] : "";
  465. $p_gid = @$p_params["gid"] ? $p_params["gid"] : "";
  466. $v_result = true;
  467. if (!$this->_isArchive()) {
  468. if (!$this->_openWrite()) {
  469. return false;
  470. }
  471. $this->_close();
  472. }
  473. if (!$this->_openAppend()) {
  474. return false;
  475. }
  476. // Need to check the get back to the temporary file ? ....
  477. $v_result = $this->_addString($p_filename, $p_string, $p_datetime, $p_params);
  478. $this->_writeFooter();
  479. $this->_close();
  480. return $v_result;
  481. }
  482. /**
  483. * This method extract all the content of the archive in the directory
  484. * indicated by $p_path. When relevant the memorized path of the
  485. * files/dir can be modified by removing the $p_remove_path path at the
  486. * beginning of the file/dir path.
  487. * While extracting a file, if the directory path does not exists it is
  488. * created.
  489. * While extracting a file, if the file already exists it is replaced
  490. * without looking for last modification date.
  491. * While extracting a file, if the file already exists and is write
  492. * protected, the extraction is aborted.
  493. * While extracting a file, if a directory with the same name already
  494. * exists, the extraction is aborted.
  495. * While extracting a directory, if a file with the same name already
  496. * exists, the extraction is aborted.
  497. * While extracting a file/directory if the destination directory exist
  498. * and is write protected, or does not exist but can not be created,
  499. * the extraction is aborted.
  500. * If after extraction an extracted file does not show the correct
  501. * stored file size, the extraction is aborted.
  502. * When the extraction is aborted, a PEAR error text is set and false
  503. * is returned. However the result can be a partial extraction that may
  504. * need to be manually cleaned.
  505. *
  506. * @param string $p_path The path of the directory where the
  507. * files/dir need to by extracted.
  508. * @param string $p_remove_path Part of the memorized path that can be
  509. * removed if present at the beginning of
  510. * the file/dir path.
  511. * @param boolean $p_preserve Preserve user/group ownership of files
  512. *
  513. * @return boolean true on success, false on error.
  514. * @see extractList()
  515. */
  516. public function extractModify($p_path, $p_remove_path, $p_preserve = false)
  517. {
  518. $v_result = true;
  519. $v_list_detail = array();
  520. if ($v_result = $this->_openRead()) {
  521. $v_result = $this->_extractList(
  522. $p_path,
  523. $v_list_detail,
  524. "complete",
  525. 0,
  526. $p_remove_path,
  527. $p_preserve
  528. );
  529. $this->_close();
  530. }
  531. return $v_result;
  532. }
  533. /**
  534. * This method extract from the archive one file identified by $p_filename.
  535. * The return value is a string with the file content, or NULL on error.
  536. *
  537. * @param string $p_filename The path of the file to extract in a string.
  538. *
  539. * @return a string with the file content or NULL.
  540. */
  541. public function extractInString($p_filename)
  542. {
  543. if ($this->_openRead()) {
  544. $v_result = $this->_extractInString($p_filename);
  545. $this->_close();
  546. } else {
  547. $v_result = null;
  548. }
  549. return $v_result;
  550. }
  551. /**
  552. * This method extract from the archive only the files indicated in the
  553. * $p_filelist. These files are extracted in the current directory or
  554. * in the directory indicated by the optional $p_path parameter.
  555. * If indicated the $p_remove_path can be used in the same way as it is
  556. * used in extractModify() method.
  557. *
  558. * @param array $p_filelist An array of filenames and directory names,
  559. * or a single string with names separated
  560. * by a single blank space.
  561. * @param string $p_path The path of the directory where the
  562. * files/dir need to by extracted.
  563. * @param string $p_remove_path Part of the memorized path that can be
  564. * removed if present at the beginning of
  565. * the file/dir path.
  566. * @param boolean $p_preserve Preserve user/group ownership of files
  567. *
  568. * @return true on success, false on error.
  569. * @see extractModify()
  570. */
  571. public function extractList($p_filelist, $p_path = '', $p_remove_path = '', $p_preserve = false)
  572. {
  573. $v_result = true;
  574. $v_list_detail = array();
  575. if (is_array($p_filelist)) {
  576. $v_list = $p_filelist;
  577. } elseif (is_string($p_filelist)) {
  578. $v_list = explode($this->_separator, $p_filelist);
  579. } else {
  580. $this->_error('Invalid string list');
  581. return false;
  582. }
  583. if ($v_result = $this->_openRead()) {
  584. $v_result = $this->_extractList(
  585. $p_path,
  586. $v_list_detail,
  587. "partial",
  588. $v_list,
  589. $p_remove_path,
  590. $p_preserve
  591. );
  592. $this->_close();
  593. }
  594. return $v_result;
  595. }
  596. /**
  597. * This method set specific attributes of the archive. It uses a variable
  598. * list of parameters, in the format attribute code + attribute values :
  599. * $arch->setAttribute(ARCHIVE_TAR_ATT_SEPARATOR, ',');
  600. *
  601. * @return true on success, false on error.
  602. */
  603. public function setAttribute()
  604. {
  605. $v_result = true;
  606. // ----- Get the number of variable list of arguments
  607. if (($v_size = func_num_args()) == 0) {
  608. return true;
  609. }
  610. // ----- Get the arguments
  611. $v_att_list = func_get_args();
  612. // ----- Read the attributes
  613. $i = 0;
  614. while ($i < $v_size) {
  615. // ----- Look for next option
  616. switch ($v_att_list[$i]) {
  617. // ----- Look for options that request a string value
  618. case ARCHIVE_TAR_ATT_SEPARATOR :
  619. // ----- Check the number of parameters
  620. if (($i + 1) >= $v_size) {
  621. $this->_error(
  622. 'Invalid number of parameters for '
  623. . 'attribute ARCHIVE_TAR_ATT_SEPARATOR'
  624. );
  625. return false;
  626. }
  627. // ----- Get the value
  628. $this->_separator = $v_att_list[$i + 1];
  629. $i++;
  630. break;
  631. default :
  632. $this->_error('Unknown attribute code ' . $v_att_list[$i] . '');
  633. return false;
  634. }
  635. // ----- Next attribute
  636. $i++;
  637. }
  638. return $v_result;
  639. }
  640. /**
  641. * This method sets the regular expression for ignoring files and directories
  642. * at import, for example:
  643. * $arch->setIgnoreRegexp("#CVS|\.svn#");
  644. *
  645. * @param string $regexp regular expression defining which files or directories to ignore
  646. */
  647. public function setIgnoreRegexp($regexp)
  648. {
  649. $this->_ignore_regexp = $regexp;
  650. }
  651. /**
  652. * This method sets the regular expression for ignoring all files and directories
  653. * matching the filenames in the array list at import, for example:
  654. * $arch->setIgnoreList(array('CVS', '.svn', 'bin/tool'));
  655. *
  656. * @param array $list a list of file or directory names to ignore
  657. *
  658. * @access public
  659. */
  660. public function setIgnoreList($list)
  661. {
  662. $regexp = str_replace(array('#', '.', '^', '$'), array('\#', '\.', '\^', '\$'), $list);
  663. $regexp = '#/' . join('$|/', $list) . '#';
  664. $this->setIgnoreRegexp($regexp);
  665. }
  666. /**
  667. * @param string $p_message
  668. */
  669. public function _error($p_message)
  670. {
  671. $this->error_object = $this->raiseError($p_message);
  672. }
  673. /**
  674. * @param string $p_message
  675. */
  676. public function _warning($p_message)
  677. {
  678. $this->error_object = $this->raiseError($p_message);
  679. }
  680. /**
  681. * @param string $p_filename
  682. * @return bool
  683. */
  684. public function _isArchive($p_filename = null)
  685. {
  686. if ($p_filename == null) {
  687. $p_filename = $this->_tarname;
  688. }
  689. clearstatcache();
  690. return @is_file($p_filename) && !@is_link($p_filename);
  691. }
  692. /**
  693. * @return bool
  694. */
  695. public function _openWrite()
  696. {
  697. if ($this->_compress_type == 'gz' && function_exists('gzopen')) {
  698. $this->_file = @gzopen($this->_tarname, "wb9");
  699. } else {
  700. if ($this->_compress_type == 'bz2' && function_exists('bzopen')) {
  701. $this->_file = @bzopen($this->_tarname, "w");
  702. } else {
  703. if ($this->_compress_type == 'lzma2' && function_exists('xzopen')) {
  704. $this->_file = @xzopen($this->_tarname, 'w');
  705. } else {
  706. if ($this->_compress_type == 'none') {
  707. $this->_file = @fopen($this->_tarname, "wb");
  708. } else {
  709. $this->_error(
  710. 'Unknown or missing compression type ('
  711. . $this->_compress_type . ')'
  712. );
  713. return false;
  714. }
  715. }
  716. }
  717. }
  718. if ($this->_file == 0) {
  719. $this->_error(
  720. 'Unable to open in write mode \''
  721. . $this->_tarname . '\''
  722. );
  723. return false;
  724. }
  725. return true;
  726. }
  727. /**
  728. * @return bool
  729. */
  730. public function _openRead()
  731. {
  732. if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') {
  733. // ----- Look if a local copy need to be done
  734. if ($this->_temp_tarname == '') {
  735. $this->_temp_tarname = uniqid('tar') . '.tmp';
  736. if (!$v_file_from = @fopen($this->_tarname, 'rb')) {
  737. $this->_error(
  738. 'Unable to open in read mode \''
  739. . $this->_tarname . '\''
  740. );
  741. $this->_temp_tarname = '';
  742. return false;
  743. }
  744. if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) {
  745. $this->_error(
  746. 'Unable to open in write mode \''
  747. . $this->_temp_tarname . '\''
  748. );
  749. $this->_temp_tarname = '';
  750. return false;
  751. }
  752. while ($v_data = @fread($v_file_from, 1024)) {
  753. @fwrite($v_file_to, $v_data);
  754. }
  755. @fclose($v_file_from);
  756. @fclose($v_file_to);
  757. }
  758. // ----- File to open if the local copy
  759. $v_filename = $this->_temp_tarname;
  760. } else {
  761. // ----- File to open if the normal Tar file
  762. $v_filename = $this->_tarname;
  763. }
  764. if ($this->_compress_type == 'gz' && function_exists('gzopen')) {
  765. $this->_file = @gzopen($v_filename, "rb");
  766. } else {
  767. if ($this->_compress_type == 'bz2' && function_exists('bzopen')) {
  768. $this->_file = @bzopen($v_filename, "r");
  769. } else {
  770. if ($this->_compress_type == 'lzma2' && function_exists('xzopen')) {
  771. $this->_file = @xzopen($v_filename, "r");
  772. } else {
  773. if ($this->_compress_type == 'none') {
  774. $this->_file = @fopen($v_filename, "rb");
  775. } else {
  776. $this->_error(
  777. 'Unknown or missing compression type ('
  778. . $this->_compress_type . ')'
  779. );
  780. return false;
  781. }
  782. }
  783. }
  784. }
  785. if ($this->_file == 0) {
  786. $this->_error('Unable to open in read mode \'' . $v_filename . '\'');
  787. return false;
  788. }
  789. return true;
  790. }
  791. /**
  792. * @return bool
  793. */
  794. public function _openReadWrite()
  795. {
  796. if ($this->_compress_type == 'gz') {
  797. $this->_file = @gzopen($this->_tarname, "r+b");
  798. } else {
  799. if ($this->_compress_type == 'bz2') {
  800. $this->_error(
  801. 'Unable to open bz2 in read/write mode \''
  802. . $this->_tarname . '\' (limitation of bz2 extension)'
  803. );
  804. return false;
  805. } else {
  806. if ($this->_compress_type == 'lzma2') {
  807. $this->_error(
  808. 'Unable to open lzma2 in read/write mode \''
  809. . $this->_tarname . '\' (limitation of lzma2 extension)'
  810. );
  811. return false;
  812. } else {
  813. if ($this->_compress_type == 'none') {
  814. $this->_file = @fopen($this->_tarname, "r+b");
  815. } else {
  816. $this->_error(
  817. 'Unknown or missing compression type ('
  818. . $this->_compress_type . ')'
  819. );
  820. return false;
  821. }
  822. }
  823. }
  824. }
  825. if ($this->_file == 0) {
  826. $this->_error(
  827. 'Unable to open in read/write mode \''
  828. . $this->_tarname . '\''
  829. );
  830. return false;
  831. }
  832. return true;
  833. }
  834. /**
  835. * @return bool
  836. */
  837. public function _close()
  838. {
  839. //if (isset($this->_file)) {
  840. if (is_resource($this->_file)) {
  841. if ($this->_compress_type == 'gz') {
  842. @gzclose($this->_file);
  843. } else {
  844. if ($this->_compress_type == 'bz2') {
  845. @bzclose($this->_file);
  846. } else {
  847. if ($this->_compress_type == 'lzma2') {
  848. @xzclose($this->_file);
  849. } else {
  850. if ($this->_compress_type == 'none') {
  851. @fclose($this->_file);
  852. } else {
  853. $this->_error(
  854. 'Unknown or missing compression type ('
  855. . $this->_compress_type . ')'
  856. );
  857. }
  858. }
  859. }
  860. }
  861. $this->_file = 0;
  862. }
  863. // ----- Look if a local copy need to be erase
  864. // Note that it might be interesting to keep the url for a time : ToDo
  865. if ($this->_temp_tarname != '') {
  866. @unlink($this->_temp_tarname);
  867. $this->_temp_tarname = '';
  868. }
  869. return true;
  870. }
  871. /**
  872. * @return bool
  873. */
  874. public function _cleanFile()
  875. {
  876. $this->_close();
  877. // ----- Look for a local copy
  878. if ($this->_temp_tarname != '') {
  879. // ----- Remove the local copy but not the remote tarname
  880. @unlink($this->_temp_tarname);
  881. $this->_temp_tarname = '';
  882. } else {
  883. // ----- Remove the local tarname file
  884. @unlink($this->_tarname);
  885. }
  886. $this->_tarname = '';
  887. return true;
  888. }
  889. /**
  890. * @param mixed $p_binary_data
  891. * @param integer $p_len
  892. * @return bool
  893. */
  894. public function _writeBlock($p_binary_data, $p_len = null)
  895. {
  896. if (is_resource($this->_file)) {
  897. if ($p_len === null) {
  898. if ($this->_compress_type == 'gz') {
  899. @gzputs($this->_file, $p_binary_data);
  900. } else {
  901. if ($this->_compress_type == 'bz2') {
  902. @bzwrite($this->_file, $p_binary_data);
  903. } else {
  904. if ($this->_compress_type == 'lzma2') {
  905. @xzwrite($this->_file, $p_binary_data);
  906. } else {
  907. if ($this->_compress_type == 'none') {
  908. @fputs($this->_file, $p_binary_data);
  909. } else {
  910. $this->_error(
  911. 'Unknown or missing compression type ('
  912. . $this->_compress_type . ')'
  913. );
  914. }
  915. }
  916. }
  917. }
  918. } else {
  919. if ($this->_compress_type == 'gz') {
  920. @gzputs($this->_file, $p_binary_data, $p_len);
  921. } else {
  922. if ($this->_compress_type == 'bz2') {
  923. @bzwrite($this->_file, $p_binary_data, $p_len);
  924. } else {
  925. if ($this->_compress_type == 'lzma2') {
  926. @xzwrite($this->_file, $p_binary_data, $p_len);
  927. } else {
  928. if ($this->_compress_type == 'none') {
  929. @fputs($this->_file, $p_binary_data, $p_len);
  930. } else {
  931. $this->_error(
  932. 'Unknown or missing compression type ('
  933. . $this->_compress_type . ')'
  934. );
  935. }
  936. }
  937. }
  938. }
  939. }
  940. }
  941. return true;
  942. }
  943. /**
  944. * @return null|string
  945. */
  946. public function _readBlock()
  947. {
  948. $v_block = null;
  949. if (is_resource($this->_file)) {
  950. if ($this->_compress_type == 'gz') {
  951. $v_block = @gzread($this->_file, 512);
  952. } else {
  953. if ($this->_compress_type == 'bz2') {
  954. $v_block = @bzread($this->_file, 512);
  955. } else {
  956. if ($this->_compress_type == 'lzma2') {
  957. $v_block = @xzread($this->_file, 512);
  958. } else {
  959. if ($this->_compress_type == 'none') {
  960. $v_block = @fread($this->_file, 512);
  961. } else {
  962. $this->_error(
  963. 'Unknown or missing compression type ('
  964. . $this->_compress_type . ')'
  965. );
  966. }
  967. }
  968. }
  969. }
  970. }
  971. return $v_block;
  972. }
  973. /**
  974. * @param null $p_len
  975. * @return bool
  976. */
  977. public function _jumpBlock($p_len = null)
  978. {
  979. if (is_resource($this->_file)) {
  980. if ($p_len === null) {
  981. $p_len = 1;
  982. }
  983. if ($this->_compress_type == 'gz') {
  984. @gzseek($this->_file, gztell($this->_file) + ($p_len * 512));
  985. } else {
  986. if ($this->_compress_type == 'bz2') {
  987. // ----- Replace missing bztell() and bzseek()
  988. for ($i = 0; $i < $p_len; $i++) {
  989. $this->_readBlock();
  990. }
  991. } else {
  992. if ($this->_compress_type == 'lzma2') {
  993. // ----- Replace missing xztell() and xzseek()
  994. for ($i = 0; $i < $p_len; $i++) {
  995. $this->_readBlock();
  996. }
  997. } else {
  998. if ($this->_compress_type == 'none') {
  999. @fseek($this->_file, $p_len * 512, SEEK_CUR);
  1000. } else {
  1001. $this->_error(
  1002. 'Unknown or missing compression type ('
  1003. . $this->_compress_type . ')'
  1004. );
  1005. }
  1006. }
  1007. }
  1008. }
  1009. }
  1010. return true;
  1011. }
  1012. /**
  1013. * @return bool
  1014. */
  1015. public function _writeFooter()
  1016. {
  1017. if (is_resource($this->_file)) {
  1018. // ----- Write the last 0 filled block for end of archive
  1019. $v_binary_data = pack('a1024', '');
  1020. $this->_writeBlock($v_binary_data);
  1021. }
  1022. return true;
  1023. }
  1024. /**
  1025. * @param array $p_list
  1026. * @param string $p_add_dir
  1027. * @param string $p_remove_dir
  1028. * @return bool
  1029. */
  1030. public function _addList($p_list, $p_add_dir, $p_remove_dir)
  1031. {
  1032. $v_result = true;
  1033. $v_header = array();
  1034. // ----- Remove potential windows directory separator
  1035. $p_add_dir = $this->_translateWinPath($p_add_dir);
  1036. $p_remove_dir = $this->_translateWinPath($p_remove_dir, false);
  1037. if (!$this->_file) {
  1038. $this->_error('Invalid file descriptor');
  1039. return false;
  1040. }
  1041. if (sizeof($p_list) == 0) {
  1042. return true;
  1043. }
  1044. foreach ($p_list as $v_filename) {
  1045. if (!$v_result) {
  1046. break;
  1047. }
  1048. // ----- Skip the current tar name
  1049. if ($v_filename == $this->_tarname) {
  1050. continue;
  1051. }
  1052. if ($v_filename == '') {
  1053. continue;
  1054. }
  1055. // ----- ignore files and directories matching the ignore regular expression
  1056. if ($this->_ignore_regexp && preg_match($this->_ignore_regexp, '/' . $v_filename)) {
  1057. $this->_warning("File '$v_filename' ignored");
  1058. continue;
  1059. }
  1060. if (!file_exists($v_filename) && !is_link($v_filename)) {
  1061. $this->_warning("File '$v_filename' does not exist");
  1062. continue;
  1063. }
  1064. // ----- Add the file or directory header
  1065. if (!$this->_addFile($v_filename, $v_header, $p_add_dir, $p_remove_dir)) {
  1066. return false;
  1067. }
  1068. if (@is_dir($v_filename) && !@is_link($v_filename)) {
  1069. if (!($p_hdir = opendir($v_filename))) {
  1070. $this->_warning("Directory '$v_filename' can not be read");
  1071. continue;
  1072. }
  1073. while (false !== ($p_hitem = readdir($p_hdir))) {
  1074. if (($p_hitem != '.') && ($p_hitem != '..')) {
  1075. if ($v_filename != ".") {
  1076. $p_temp_list[0] = $v_filename . '/' . $p_hitem;
  1077. } else {
  1078. $p_temp_list[0] = $p_hitem;
  1079. }
  1080. $v_result = $this->_addList(
  1081. $p_temp_list,
  1082. $p_add_dir,
  1083. $p_remove_dir
  1084. );
  1085. }
  1086. }
  1087. unset($p_temp_list);
  1088. unset($p_hdir);
  1089. unset($p_hitem);
  1090. }
  1091. }
  1092. return $v_result;
  1093. }
  1094. /**
  1095. * @param string $p_filename
  1096. * @param mixed $p_header
  1097. * @param string $p_add_dir
  1098. * @param string $p_remove_dir
  1099. * @param null $v_stored_filename
  1100. * @return bool
  1101. */
  1102. public function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $v_stored_filename = null)
  1103. {
  1104. if (!$this->_file) {
  1105. $this->_error('Invalid file descriptor');
  1106. return false;
  1107. }
  1108. if ($p_filename == '') {
  1109. $this->_error('Invalid file name');
  1110. return false;
  1111. }
  1112. if (is_null($v_stored_filename)) {
  1113. // ----- Calculate the stored filename
  1114. $p_filename = $this->_translateWinPath($p_filename, false);
  1115. $v_stored_filename = $p_filename;
  1116. if (strcmp($p_filename, $p_remove_dir) == 0) {
  1117. return true;
  1118. }
  1119. if ($p_remove_dir != '') {
  1120. if (substr($p_remove_dir, -1) != '/') {
  1121. $p_remove_dir .= '/';
  1122. }
  1123. if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir) {
  1124. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  1125. }
  1126. }
  1127. $v_stored_filename = $this->_translateWinPath($v_stored_filename);
  1128. if ($p_add_dir != '') {
  1129. if (substr($p_add_dir, -1) == '/') {
  1130. $v_stored_filename = $p_add_dir . $v_stored_filename;
  1131. } else {
  1132. $v_stored_filename = $p_add_dir . '/' . $v_stored_filename;
  1133. }
  1134. }
  1135. $v_stored_filename = $this->_pathReduction($v_stored_filename);
  1136. }
  1137. if ($this->_isArchive($p_filename)) {
  1138. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1139. $this->_warning(
  1140. "Unable to open file '" . $p_filename
  1141. . "' in binary read mode"
  1142. );
  1143. return true;
  1144. }
  1145. if (!$this->_writeHeader($p_filename, $v_stored_filename)) {
  1146. return false;
  1147. }
  1148. while (($v_buffer = fread($v_file, $this->buffer_length)) != '') {
  1149. $buffer_length = strlen("$v_buffer");
  1150. if ($buffer_length != $this->buffer_length) {
  1151. $pack_size = ((int)($buffer_length / 512) + 1) * 512;
  1152. $pack_format = sprintf('a%d', $pack_size);
  1153. } else {
  1154. $pack_format = sprintf('a%d', $this->buffer_length);
  1155. }
  1156. $v_binary_data = pack($pack_format, "$v_buffer");
  1157. $this->_writeBlock($v_binary_data);
  1158. }
  1159. fclose($v_file);
  1160. } else {
  1161. // ----- Only header for dir
  1162. if (!$this->_writeHeader($p_filename, $v_stored_filename)) {
  1163. return false;
  1164. }
  1165. }
  1166. return true;
  1167. }
  1168. /**
  1169. * @param string $p_filename
  1170. * @param string $p_string
  1171. * @param bool $p_datetime
  1172. * @param array $p_params
  1173. * @return bool
  1174. */
  1175. public function _addString($p_filename, $p_string, $p_datetime = false, $p_params = array())
  1176. {
  1177. $p_stamp = @$p_params["stamp"] ? $p_params["stamp"] : ($p_datetime ? $p_datetime : time());
  1178. $p_mode = @$p_params["mode"] ? $p_params["mode"] : 0600;
  1179. $p_type = @$p_params["type"] ? $p_params["type"] : "";
  1180. $p_uid = @$p_params["uid"] ? $p_params["uid"] : 0;
  1181. $p_gid = @$p_params["gid"] ? $p_params["gid"] : 0;
  1182. if (!$this->_file) {
  1183. $this->_error('Invalid file descriptor');
  1184. return false;
  1185. }
  1186. if ($p_filename == '') {
  1187. $this->_error('Invalid file name');
  1188. return false;
  1189. }
  1190. // ----- Calculate the stored filename
  1191. $p_filename = $this->_translateWinPath($p_filename, false);
  1192. // ----- If datetime is not specified, set current time
  1193. if ($p_datetime === false) {
  1194. $p_datetime = time();
  1195. }
  1196. if (!$this->_writeHeaderBlock(
  1197. $p_filename,
  1198. strlen($p_string),
  1199. $p_stamp,
  1200. $p_mode,
  1201. $p_type,
  1202. $p_uid,
  1203. $p_gid
  1204. )
  1205. ) {
  1206. return false;
  1207. }
  1208. $i = 0;
  1209. while (($v_buffer = substr($p_string, (($i++) * 512), 512)) != '') {
  1210. $v_binary_data = pack("a512", $v_buffer);
  1211. $this->_writeBlock($v_binary_data);
  1212. }
  1213. return true;
  1214. }
  1215. /**
  1216. * @param string $p_filename
  1217. * @param string $p_stored_filename
  1218. * @return bool
  1219. */
  1220. public function _writeHeader($p_filename, $p_stored_filename)
  1221. {
  1222. if ($p_stored_filename == '') {
  1223. $p_stored_filename = $p_filename;
  1224. }
  1225. $v_reduced_filename = $this->_pathReduction($p_stored_filename);
  1226. if (strlen($v_reduced_filename) > 99) {
  1227. if (!$this->_writeLongHeader($v_reduced_filename, false)) {
  1228. return false;
  1229. }
  1230. }
  1231. $v_linkname = '';
  1232. if (@is_link($p_filename)) {
  1233. $v_linkname = readlink($p_filename);
  1234. }
  1235. if (strlen($v_linkname) > 99) {
  1236. if (!$this->_writeLongHeader($v_linkname, true)) {
  1237. return false;
  1238. }
  1239. }
  1240. $v_info = lstat($p_filename);
  1241. $v_uid = sprintf("%07s", DecOct($v_info[4]));
  1242. $v_gid = sprintf("%07s", DecOct($v_info[5]));
  1243. $v_perms = sprintf("%07s", DecOct($v_info['mode'] & 000777));
  1244. $v_mtime = sprintf("%011s", DecOct($v_info['mtime']));
  1245. if (@is_link($p_filename)) {
  1246. $v_typeflag = '2';
  1247. $v_size = sprintf("%011s", DecOct(0));
  1248. } elseif (@is_dir($p_filename)) {
  1249. $v_typeflag = "5";
  1250. $v_size = sprintf("%011s", DecOct(0));
  1251. } else {
  1252. $v_typeflag = '0';
  1253. clearstatcache();
  1254. $v_size = sprintf("%011s", DecOct($v_info['size']));
  1255. }
  1256. $v_magic = 'ustar ';
  1257. $v_version = ' ';
  1258. if (function_exists('posix_getpwuid')) {
  1259. $userinfo = posix_getpwuid($v_info[4]);
  1260. $groupinfo = posix_getgrgid($v_info[5]);
  1261. $v_uname = $userinfo['name'];
  1262. $v_gname = $groupinfo['name'];
  1263. } else {
  1264. $v_uname = '';
  1265. $v_gname = '';
  1266. }
  1267. $v_devmajor = '';
  1268. $v_devminor = '';
  1269. $v_prefix = '';
  1270. $v_binary_data_first = pack(
  1271. "a100a8a8a8a12a12",
  1272. $v_reduced_filename,
  1273. $v_perms,
  1274. $v_uid,
  1275. $v_gid,
  1276. $v_size,
  1277. $v_mtime
  1278. );
  1279. $v_binary_data_last = pack(
  1280. "a1a100a6a2a32a32a8a8a155a12",
  1281. $v_typeflag,
  1282. $v_linkname,
  1283. $v_magic,
  1284. $v_version,
  1285. $v_uname,
  1286. $v_gname,
  1287. $v_devmajor,
  1288. $v_devminor,
  1289. $v_prefix,
  1290. ''
  1291. );
  1292. // ----- Calculate the checksum
  1293. $v_checksum = 0;
  1294. // ..... First part of the header
  1295. for ($i = 0; $i < 148; $i++) {
  1296. $v_checksum += ord(substr($v_binary_data_first, $i, 1));
  1297. }
  1298. // ..... Ignore the checksum value and replace it by ' ' (space)
  1299. for ($i = 148; $i < 156; $i++) {
  1300. $v_checksum += ord(' ');
  1301. }
  1302. // ..... Last part of the header
  1303. for ($i = 156, $j = 0; $i < 512; $i++, $j++) {
  1304. $v_checksum += ord(substr($v_binary_data_last, $j, 1));
  1305. }
  1306. // ----- Write the first 148 bytes of the header in the archive
  1307. $this->_writeBlock($v_binary_data_first, 148);
  1308. // ----- Write the calculated checksum
  1309. $v_checksum = sprintf("%06s\0 ", DecOct($v_checksum));
  1310. $v_binary_data = pack("a8", $v_checksum);
  1311. $this->_writeBlock($v_binary_data, 8);
  1312. // ----- Write the last 356 bytes of the header in the archive
  1313. $this->_writeBlock($v_binary_data_last, 356);
  1314. return true;
  1315. }
  1316. /**
  1317. * @param string $p_filename
  1318. * @param int $p_size
  1319. * @param int $p_mtime
  1320. * @param int $p_perms
  1321. * @param string $p_type
  1322. * @param int $p_uid
  1323. * @param int $p_gid
  1324. * @return bool
  1325. */
  1326. public function _writeHeaderBlock(
  1327. $p_filename,
  1328. $p_size,
  1329. $p_mtime = 0,
  1330. $p_perms = 0,
  1331. $p_type = '',
  1332. $p_uid = 0,
  1333. $p_gid = 0
  1334. )
  1335. {
  1336. $p_filename = $this->_pathReduction($p_filename);
  1337. if (strlen($p_filename) > 99) {
  1338. if (!$this->_writeLongHeader($p_filename, false)) {
  1339. return false;
  1340. }
  1341. }
  1342. if ($p_type == "5") {
  1343. $v_size = sprintf("%011s", DecOct(0));
  1344. } else {
  1345. $v_size = sprintf("%011s", DecOct($p_size));
  1346. }
  1347. $v_uid = sprintf("%07s", DecOct($p_uid));
  1348. $v_gid = sprintf("%07s", DecOct($p_gid));
  1349. $v_perms = sprintf("%07s", DecOct($p_perms & 000777));
  1350. $v_mtime = sprintf("%11s", DecOct($p_mtime));
  1351. $v_linkname = '';
  1352. $v_magic = 'ustar ';
  1353. $v_version = ' ';
  1354. if (function_exists('posix_getpwuid')) {
  1355. $userinfo = posix_getpwuid($p_uid);
  1356. $groupinfo = posix_getgrgid($p_gid);
  1357. $v_uname = $userinfo['name'];
  1358. $v_gname = $groupinfo['name'];
  1359. } else {
  1360. $v_uname = '';
  1361. $v_gname = '';
  1362. }
  1363. $v_devmajor = '';
  1364. $v_devminor = '';
  1365. $v_prefix = '';
  1366. $v_binary_data_first = pack(
  1367. "a100a8a8a8a12A12",
  1368. $p_filename,
  1369. $v_perms,
  1370. $v_uid,
  1371. $v_gid,
  1372. $v_size,
  1373. $v_mtime
  1374. );
  1375. $v_binary_data_last = pack(
  1376. "a1a100a6a2a32a32a8a8a155a12",
  1377. $p_type,
  1378. $v_linkname,
  1379. $v_magic,
  1380. $v_version,
  1381. $v_uname,
  1382. $v_gname,
  1383. $v_devmajor,
  1384. $v_devminor,
  1385. $v_prefix,
  1386. ''
  1387. );
  1388. // ----- Calculate the checksum
  1389. $v_checksum = 0;
  1390. // ..... First part of the header
  1391. for ($i = 0; $i < 148; $i++) {
  1392. $v_checksum += ord(substr($v_binary_data_first, $i, 1));
  1393. }
  1394. // ..... Ignore the checksum value and replace it by ' ' (space)
  1395. for ($i = 148; $i < 156; $i++) {
  1396. $v_checksum += ord(' ');
  1397. }
  1398. // ..... Last part of the header
  1399. for ($i = 156, $j = 0; $i < 512; $i++, $j++) {
  1400. $v_checksum += ord(substr($v_binary_data_last, $j, 1));
  1401. }
  1402. // ----- Write the first 148 bytes of the header in the archive
  1403. $this->_writeBlock($v_binary_data_first, 148);
  1404. // ----- Write the calculated checksum
  1405. $v_checksum = sprintf("%06s ", DecOct($v_checksum));
  1406. $v_binary_data = pack("a8", $v_checksum);
  1407. $this->_writeBlock($v_binary_data, 8);
  1408. // ----- Write the last 356 bytes of the header in the archive
  1409. $this->_writeBlock($v_binary_data_last, 356);
  1410. return true;
  1411. }
  1412. /**
  1413. * @param string $p_filename
  1414. * @return bool
  1415. */
  1416. public function _writeLongHeader($p_filename, $is_link = false)
  1417. {
  1418. $v_uid = sprintf("%07s", 0);
  1419. $v_gid = sprintf("%07s", 0);
  1420. $v_perms = sprintf("%07s", 0);
  1421. $v_size = sprintf("%'011s", DecOct(strlen($p_filename)));
  1422. $v_mtime = sprintf("%011s", 0);
  1423. $v_typeflag = ($is_link ? 'K' : 'L');
  1424. $v_linkname = '';
  1425. $v_magic = 'ustar ';
  1426. $v_version = ' ';
  1427. $v_uname = '';
  1428. $v_gname = '';
  1429. $v_devmajor = '';
  1430. $v_devminor = '';
  1431. $v_prefix = '';
  1432. $v_binary_data_first = pack(
  1433. "a100a8a8a8a12a12",
  1434. '././@LongLink',
  1435. $v_perms,
  1436. $v_uid,
  1437. $v_gid,
  1438. $v_size,
  1439. $v_mtime
  1440. );
  1441. $v_binary_data_last = pack(
  1442. "a1a100a6a2a32a32a8a8a155a12",
  1443. $v_typeflag,
  1444. $v_linkname,
  1445. $v_magic,
  1446. $v_version,
  1447. $v_uname,
  1448. $v_gname,
  1449. $v_devmajor,
  1450. $v_devminor,
  1451. $v_prefix,
  1452. ''
  1453. );
  1454. // ----- Calculate the checksum
  1455. $v_checksum = 0;
  1456. // ..... First part of the header
  1457. for ($i = 0; $i < 148; $i++) {
  1458. $v_checksum += ord(substr($v_binary_data_first, $i, 1));
  1459. }
  1460. // ..... Ignore the checksum value and replace it by ' ' (space)
  1461. for ($i = 148; $i < 156; $i++) {
  1462. $v_checksum += ord(' ');
  1463. }
  1464. // ..... Last part of the header
  1465. for ($i = 156, $j = 0; $i < 512; $i++, $j++) {
  1466. $v_checksum += ord(substr($v_binary_data_last, $j, 1));
  1467. }
  1468. // ----- Write the first 148 bytes of the header in the archive
  1469. $this->_writeBlock($v_binary_data_first, 148);
  1470. // ----- Write the calculated checksum
  1471. $v_checksum = sprintf("%06s\0 ", DecOct($v_checksum));
  1472. $v_binary_data = pack("a8", $v_checksum);
  1473. $this->_writeBlock($v_binary_data, 8);
  1474. // ----- Write the last 356 bytes of the header in the archive
  1475. $this->_writeBlock($v_binary_data_last, 356);
  1476. // ----- Write the filename as content of the block
  1477. $i = 0;
  1478. while (($v_buffer = substr($p_filename, (($i++) * 512), 512)) != '') {
  1479. $v_binary_data = pack("a512", "$v_buffer");
  1480. $this->_writeBlock($v_binary_data);
  1481. }
  1482. return true;
  1483. }
  1484. /**
  1485. * @param mixed $v_binary_data
  1486. * @param mixed $v_header
  1487. * @return bool
  1488. */
  1489. public function _readHeader($v_binary_data, &$v_header)
  1490. {
  1491. if (strlen($v_binary_data) == 0) {
  1492. $v_header['filename'] = '';
  1493. return true;
  1494. }
  1495. if (strlen($v_binary_data) != 512) {
  1496. $v_header['filename'] = '';
  1497. $this->_error('Invalid block size : ' . strlen($v_binary_data));
  1498. return false;
  1499. }
  1500. if (!is_array($v_header)) {
  1501. $v_header = array();
  1502. }
  1503. // ----- Calculate the checksum
  1504. $v_checksum = 0;
  1505. // ..... First part of the header
  1506. $v_binary_split = str_split($v_binary_data);
  1507. $v_checksum += array_sum(array_map('ord', array_slice($v_binary_split, 0, 148)));
  1508. $v_checksum += array_sum(array_map('ord', array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',)));
  1509. $v_checksum += array_sum(array_map('ord', array_slice($v_binary_split, 156, 512)));
  1510. $v_data = unpack($this->_fmt, $v_binary_data);
  1511. if (strlen($v_data["prefix"]) > 0) {
  1512. $v_data["filename"] = "$v_data[prefix]/$v_data[filename]";
  1513. }
  1514. // ----- Extract the checksum
  1515. $v_data_checksum = trim($v_data['checksum']);
  1516. if (!preg_match('/^[0-7]*$/', $v_data_checksum)) {
  1517. $this->_error(
  1518. 'Invalid checksum for file "' . $v_data['filename']
  1519. . '" : ' . $v_data_checksum . ' extracted'
  1520. );
  1521. return false;
  1522. }
  1523. $v_header['checksum'] = OctDec($v_data_checksum);
  1524. if ($v_header['checksum'] != $v_checksum) {
  1525. $v_header['filename'] = '';
  1526. // ----- Look for last block (empty block)
  1527. if (($v_checksum == 256) && ($v_header['checksum'] == 0)) {
  1528. return true;
  1529. }
  1530. $this->_error(
  1531. 'Invalid checksum for file "' . $v_data['filename']
  1532. . '" : ' . $v_checksum . ' calculated, '
  1533. . $v_header['checksum'] . ' expected'
  1534. );
  1535. return false;
  1536. }
  1537. // ----- Extract the properties
  1538. $v_header['filename'] = rtrim($v_data['filename'], "\0");
  1539. if ($this->_maliciousFilename($v_header['filename'])) {
  1540. $this->_error(
  1541. 'Malicious .tar detected, file "' . $v_header['filename'] .
  1542. '" will not install in desired directory tree'
  1543. );
  1544. return false;
  1545. }
  1546. $v_header['mode'] = OctDec(trim($v_data['mode']));
  1547. $v_header['uid'] = OctDec(trim($v_data['uid']));
  1548. $v_header['gid'] = OctDec(trim($v_data['gid']));
  1549. $v_header['size'] = $this->_tarRecToSize($v_data['size']);
  1550. $v_header['mtime'] = OctDec(trim($v_data['mtime']));
  1551. if (($v_header['typeflag'] = $v_data['typeflag']) == "5") {
  1552. $v_header['size'] = 0;
  1553. }
  1554. $v_header['link'] = trim($v_data['link']);
  1555. /* ----- All these fields are removed form the header because
  1556. they do not carry interesting info
  1557. $v_header[magic] = trim($v_data[magic]);
  1558. $v_header[version] = trim($v_data[version]);
  1559. $v_header[uname] = trim($v_data[uname]);
  1560. $v_header[gname] = trim($v_data[gname]);
  1561. $v_header[devmajor] = trim($v_data[devmajor]);
  1562. $v_header[devminor] = trim($v_data[devminor]);
  1563. */
  1564. return true;
  1565. }
  1566. /**
  1567. * Convert Tar record size to actual size
  1568. *
  1569. * @param string $tar_size
  1570. * @return size of tar record in bytes
  1571. */
  1572. private function _tarRecToSize($tar_size)
  1573. {
  1574. /*
  1575. * First byte of size has a special meaning if bit 7 is set.
  1576. *
  1577. * Bit 7 indicates base-256 encoding if set.
  1578. * Bit 6 is the sign bit.
  1579. * Bits 5:0 are most significant value bits.
  1580. */
  1581. $ch = ord($tar_size[0]);
  1582. if ($ch & 0x80) {
  1583. // Full 12-bytes record is required.
  1584. $rec_str = $tar_size . "\x00";
  1585. $size = ($ch & 0x40) ? -1 : 0;
  1586. $size = ($size << 6) | ($ch & 0x3f);
  1587. for ($num_ch = 1; $num_ch < 12; ++$num_ch) {
  1588. $size = ($size * 256) + ord($rec_str[$num_ch]);
  1589. }
  1590. return $size;
  1591. } else {
  1592. return OctDec(trim($tar_size));
  1593. }
  1594. }
  1595. /**
  1596. * Detect and report a malicious file name
  1597. *
  1598. * @param string $file
  1599. *
  1600. * @return bool
  1601. */
  1602. private function _maliciousFilename($file)
  1603. {
  1604. if (strpos($file, 'phar://') === 0) {
  1605. return true;
  1606. }
  1607. if (strpos($file, '../') !== false || strpos($file, '..\\') !== false) {
  1608. return true;
  1609. }
  1610. return false;
  1611. }
  1612. /**
  1613. * @param $v_header
  1614. * @return bool
  1615. */
  1616. public function _readLongHeader(&$v_header)
  1617. {
  1618. $v_filename = '';
  1619. $v_filesize = $v_header['size'];
  1620. $n = floor($v_header['size'] / 512);
  1621. for ($i = 0; $i < $n; $i++) {
  1622. $v_content = $this->_readBlock();
  1623. $v_filename .= $v_content;
  1624. }
  1625. if (($v_header['size'] % 512) != 0) {
  1626. $v_content = $this->_readBlock();
  1627. $v_filename .= $v_content;
  1628. }
  1629. // ----- Read the next header
  1630. $v_binary_data = $this->_readBlock();
  1631. if (!$this->_readHeader($v_binary_data, $v_header)) {
  1632. return false;
  1633. }
  1634. $v_filename = rtrim(substr($v_filename, 0, $v_filesize), "\0");
  1635. $v_header['filename'] = $v_filename;
  1636. if ($this->_maliciousFilename($v_filename)) {
  1637. $this->_error(
  1638. 'Malicious .tar detected, file "' . $v_filename .
  1639. '" will not install in desired directory tree'
  1640. );
  1641. return false;
  1642. }
  1643. return true;
  1644. }
  1645. /**
  1646. * This method extract from the archive one file identified by $p_filename.
  1647. * The return value is a string with the file content, or null on error.
  1648. *
  1649. * @param string $p_filename The path of the file to extract in a string.
  1650. *
  1651. * @return a string with the file content or null.
  1652. */
  1653. private function _extractInString($p_filename)
  1654. {
  1655. $v_result_str = "";
  1656. while (strlen($v_binary_data = $this->_readBlock()) != 0) {
  1657. if (!$this->_readHeader($v_binary_data, $v_header)) {
  1658. return null;
  1659. }
  1660. if ($v_header['filename'] == '') {
  1661. continue;
  1662. }
  1663. switch ($v_header['typeflag']) {
  1664. case 'L':
  1665. {
  1666. if (!$this->_readLongHeader($v_header)) {
  1667. return null;
  1668. }
  1669. }
  1670. break;
  1671. case 'K':
  1672. {
  1673. $v_link_header = $v_header;
  1674. if (!$this->_readLongHeader($v_link_header)) {
  1675. return null;
  1676. }
  1677. $v_header['link'] = $v_link_header['filename'];
  1678. }
  1679. break;
  1680. }
  1681. if ($v_header['filename'] == $p_filename) {
  1682. if ($v_header['typeflag'] == "5") {
  1683. $this->_error(
  1684. 'Unable to extract in string a directory '
  1685. . 'entry {' . $v_header['filename'] . '}'
  1686. );
  1687. return null;
  1688. } else {
  1689. $n = floor($v_header['size'] / 512);
  1690. for ($i = 0; $i < $n; $i++) {
  1691. $v_result_str .= $this->_readBlock();
  1692. }
  1693. if (($v_header['size'] % 512) != 0) {
  1694. $v_content = $this->_readBlock();
  1695. $v_result_str .= substr(
  1696. $v_content,
  1697. 0,
  1698. ($v_header['size'] % 512)
  1699. );
  1700. }
  1701. return $v_result_str;
  1702. }
  1703. } else {
  1704. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  1705. }
  1706. }
  1707. return null;
  1708. }
  1709. /**
  1710. * @param string $p_path
  1711. * @param string $p_list_detail
  1712. * @param string $p_mode
  1713. * @param string $p_file_list
  1714. * @param string $p_remove_path
  1715. * @param bool $p_preserve
  1716. * @return bool
  1717. */
  1718. public function _extractList(
  1719. $p_path,
  1720. &$p_list_detail,
  1721. $p_mode,
  1722. $p_file_list,
  1723. $p_remove_path,
  1724. $p_preserve = false
  1725. )
  1726. {
  1727. $v_result = true;
  1728. $v_nb = 0;
  1729. $v_extract_all = true;
  1730. $v_listing = false;
  1731. $p_path = $this->_translateWinPath($p_path, false);
  1732. if ($p_path == '' || (substr($p_path, 0, 1) != '/'
  1733. && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))
  1734. ) {
  1735. $p_path = "./" . $p_path;
  1736. }
  1737. $p_remove_path = $this->_translateWinPath($p_remove_path);
  1738. // ----- Look for path to remove format (should end by /)
  1739. if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/')) {
  1740. $p_remove_path .= '/';
  1741. }
  1742. $p_remove_path_size = strlen($p_remove_path);
  1743. switch ($p_mode) {
  1744. case "complete" :
  1745. $v_extract_all = true;
  1746. $v_listing = false;
  1747. break;
  1748. case "partial" :
  1749. $v_extract_all = false;
  1750. $v_listing = false;
  1751. break;
  1752. case "list" :
  1753. $v_extract_all = false;
  1754. $v_listing = true;
  1755. break;
  1756. default :
  1757. $this->_error('Invalid extract mode (' . $p_mode . ')');
  1758. return false;
  1759. }
  1760. clearstatcache();
  1761. while (strlen($v_binary_data = $this->_readBlock()) != 0) {
  1762. $v_extract_file = false;
  1763. $v_extraction_stopped = 0;
  1764. if (!$this->_readHeader($v_binary_data, $v_header)) {
  1765. return false;
  1766. }
  1767. if ($v_header['filename'] == '') {
  1768. continue;
  1769. }
  1770. switch ($v_header['typeflag']) {
  1771. case 'L':
  1772. {
  1773. if (!$this->_readLongHeader($v_header)) {
  1774. return null;
  1775. }
  1776. }
  1777. break;
  1778. case 'K':
  1779. {
  1780. $v_link_header = $v_header;
  1781. if (!$this->_readLongHeader($v_link_header)) {
  1782. return null;
  1783. }
  1784. $v_header['link'] = $v_link_header['filename'];
  1785. }
  1786. break;
  1787. }
  1788. // ignore extended / pax headers
  1789. if ($v_header['typeflag'] == 'x' || $v_header['typeflag'] == 'g') {
  1790. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  1791. continue;
  1792. }
  1793. if ((!$v_extract_all) && (is_array($p_file_list))) {
  1794. // ----- By default no unzip if the file is not found
  1795. $v_extract_file = false;
  1796. for ($i = 0; $i < sizeof($p_file_list); $i++) {
  1797. // ----- Look if it is a directory
  1798. if (substr($p_file_list[$i], -1) == '/') {
  1799. // ----- Look if the directory is in the filename path
  1800. if ((strlen($v_header['filename']) > strlen($p_file_list[$i]))
  1801. && (substr($v_header['filename'], 0, strlen($p_file_list[$i]))
  1802. == $p_file_list[$i])
  1803. ) {
  1804. $v_extract_file = true;
  1805. break;
  1806. }
  1807. } // ----- It is a file, so compare the file names
  1808. elseif ($p_file_list[$i] == $v_header['filename']) {
  1809. $v_extract_file = true;
  1810. break;
  1811. }
  1812. }
  1813. } else {
  1814. $v_extract_file = true;
  1815. }
  1816. // ----- Look if this file need to be extracted
  1817. if (($v_extract_file) && (!$v_listing)) {
  1818. if (($p_remove_path != '')
  1819. && (substr($v_header['filename'] . '/', 0, $p_remove_path_size)
  1820. == $p_remove_path)
  1821. ) {
  1822. $v_header['filename'] = substr(
  1823. $v_header['filename'],
  1824. $p_remove_path_size
  1825. );
  1826. if ($v_header['filename'] == '') {
  1827. continue;
  1828. }
  1829. }
  1830. if (($p_path != './') && ($p_path != '/')) {
  1831. while (substr($p_path, -1) == '/') {
  1832. $p_path = substr($p_path, 0, strlen($p_path) - 1);
  1833. }
  1834. if (substr($v_header['filename'], 0, 1) == '/') {
  1835. $v_header['filename'] = $p_path . $v_header['filename'];
  1836. } else {
  1837. $v_header['filename'] = $p_path . '/' . $v_header['filename'];
  1838. }
  1839. }
  1840. if (file_exists($v_header['filename'])) {
  1841. if ((@is_dir($v_header['filename']))
  1842. && ($v_header['typeflag'] == '')
  1843. ) {
  1844. $this->_error(
  1845. 'File ' . $v_header['filename']
  1846. . ' already exists as a directory'
  1847. );
  1848. return false;
  1849. }
  1850. if (($this->_isArchive($v_header['filename']))
  1851. && ($v_header['typeflag'] == "5")
  1852. ) {
  1853. $this->_error(
  1854. 'Directory ' . $v_header['filename']
  1855. . ' already exists as a file'
  1856. );
  1857. return false;
  1858. }
  1859. if (!is_writeable($v_header['filename'])) {
  1860. $this->_error(
  1861. 'File ' . $v_header['filename']
  1862. . ' already exists and is write protected'
  1863. );
  1864. return false;
  1865. }
  1866. if (filemtime($v_header['filename']) > $v_header['mtime']) {
  1867. // To be completed : An error or silent no replace ?
  1868. }
  1869. } // ----- Check the directory availability and create it if necessary
  1870. elseif (($v_result
  1871. = $this->_dirCheck(
  1872. ($v_header['typeflag'] == "5"
  1873. ? $v_header['filename']
  1874. : dirname($v_header['filename']))
  1875. )) != 1
  1876. ) {
  1877. $this->_error('Unable to create path for ' . $v_header['filename']);
  1878. return false;
  1879. }
  1880. if ($v_extract_file) {
  1881. if ($v_header['typeflag'] == "5") {
  1882. if (!@file_exists($v_header['filename'])) {
  1883. if (!@mkdir($v_header['filename'], 0777)) {
  1884. $this->_error(
  1885. 'Unable to create directory {'
  1886. . $v_header['filename'] . '}'
  1887. );
  1888. return false;
  1889. }
  1890. }
  1891. } elseif ($v_header['typeflag'] == "2") {
  1892. if (@file_exists($v_header['filename'])) {
  1893. @unlink($v_header['filename']);
  1894. }
  1895. if (!@symlink($v_header['link'], $v_header['filename'])) {
  1896. $this->_error(
  1897. 'Unable to extract symbolic link {'
  1898. . $v_header['filename'] . '}'
  1899. );
  1900. return false;
  1901. }
  1902. } else {
  1903. if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
  1904. $this->_error(
  1905. 'Error while opening {' . $v_header['filename']
  1906. . '} in write binary mode'
  1907. );
  1908. return false;
  1909. } else {
  1910. $n = floor($v_header['size'] / 512);
  1911. for ($i = 0; $i < $n; $i++) {
  1912. $v_content = $this->_readBlock();
  1913. fwrite($v_dest_file, $v_content, 512);
  1914. }
  1915. if (($v_header['size'] % 512) != 0) {
  1916. $v_content = $this->_readBlock();
  1917. fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
  1918. }
  1919. @fclose($v_dest_file);
  1920. if ($p_preserve) {
  1921. @chown($v_header['filename'], $v_header['uid']);
  1922. @chgrp($v_header['filename'], $v_header['gid']);
  1923. }
  1924. // ----- Change the file mode, mtime
  1925. @touch($v_header['filename'], $v_header['mtime']);
  1926. if ($v_header['mode'] & 0111) {
  1927. // make file executable, obey umask
  1928. $mode = fileperms($v_header['filename']) | (~umask() & 0111);
  1929. @chmod($v_header['filename'], $mode);
  1930. }
  1931. }
  1932. // ----- Check the file size
  1933. clearstatcache();
  1934. if (!is_file($v_header['filename'])) {
  1935. $this->_error(
  1936. 'Extracted file ' . $v_header['filename']
  1937. . 'does not exist. Archive may be corrupted.'
  1938. );
  1939. return false;
  1940. }
  1941. $filesize = filesize($v_header['filename']);
  1942. if ($filesize != $v_header['size']) {
  1943. $this->_error(
  1944. 'Extracted file ' . $v_header['filename']
  1945. . ' does not have the correct file size \''
  1946. . $filesize
  1947. . '\' (' . $v_header['size']
  1948. . ' expected). Archive may be corrupted.'
  1949. );
  1950. return false;
  1951. }
  1952. }
  1953. } else {
  1954. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  1955. }
  1956. } else {
  1957. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  1958. }
  1959. /* TBC : Seems to be unused ...
  1960. if ($this->_compress)
  1961. $v_end_of_file = @gzeof($this->_file);
  1962. else
  1963. $v_end_of_file = @feof($this->_file);
  1964. */
  1965. if ($v_listing || $v_extract_file || $v_extraction_stopped) {
  1966. // ----- Log extracted files
  1967. if (($v_file_dir = dirname($v_header['filename']))
  1968. == $v_header['filename']
  1969. ) {
  1970. $v_file_dir = '';
  1971. }
  1972. if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == '')) {
  1973. $v_file_dir = '/';
  1974. }
  1975. $p_list_detail[$v_nb++] = $v_header;
  1976. if (is_array($p_file_list) && (count($p_list_detail) == count($p_file_list))) {
  1977. return true;
  1978. }
  1979. }
  1980. }
  1981. return true;
  1982. }
  1983. /**
  1984. * @return bool
  1985. */
  1986. public function _openAppend()
  1987. {
  1988. if (filesize($this->_tarname) == 0) {
  1989. return $this->_openWrite();
  1990. }
  1991. if ($this->_compress) {
  1992. $this->_close();
  1993. if (!@rename($this->_tarname, $this->_tarname . ".tmp")) {
  1994. $this->_error(
  1995. 'Error while renaming \'' . $this->_tarname
  1996. . '\' to temporary file \'' . $this->_tarname
  1997. . '.tmp\''
  1998. );
  1999. return false;
  2000. }
  2001. if ($this->_compress_type == 'gz') {
  2002. $v_temp_tar = @gzopen($this->_tarname . ".tmp", "rb");
  2003. } elseif ($this->_compress_type == 'bz2') {
  2004. $v_temp_tar = @bzopen($this->_tarname . ".tmp", "r");
  2005. } elseif ($this->_compress_type == 'lzma2') {
  2006. $v_temp_tar = @xzopen($this->_tarname . ".tmp", "r");
  2007. }
  2008. if ($v_temp_tar == 0) {
  2009. $this->_error(
  2010. 'Unable to open file \'' . $this->_tarname
  2011. . '.tmp\' in binary read mode'
  2012. );
  2013. @rename($this->_tarname . ".tmp", $this->_tarname);
  2014. return false;
  2015. }
  2016. if (!$this->_openWrite()) {
  2017. @rename($this->_tarname . ".tmp", $this->_tarname);
  2018. return false;
  2019. }
  2020. if ($this->_compress_type == 'gz') {
  2021. $end_blocks = 0;
  2022. while (!@gzeof($v_temp_tar)) {
  2023. $v_buffer = @gzread($v_temp_tar, 512);
  2024. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2025. $end_blocks++;
  2026. // do not copy end blocks, we will re-make them
  2027. // after appending
  2028. continue;
  2029. } elseif ($end_blocks > 0) {
  2030. for ($i = 0; $i < $end_blocks; $i++) {
  2031. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2032. }
  2033. $end_blocks = 0;
  2034. }
  2035. $v_binary_data = pack("a512", $v_buffer);
  2036. $this->_writeBlock($v_binary_data);
  2037. }
  2038. @gzclose($v_temp_tar);
  2039. } elseif ($this->_compress_type == 'bz2') {
  2040. $end_blocks = 0;
  2041. while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) {
  2042. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2043. $end_blocks++;
  2044. // do not copy end blocks, we will re-make them
  2045. // after appending
  2046. continue;
  2047. } elseif ($end_blocks > 0) {
  2048. for ($i = 0; $i < $end_blocks; $i++) {
  2049. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2050. }
  2051. $end_blocks = 0;
  2052. }
  2053. $v_binary_data = pack("a512", $v_buffer);
  2054. $this->_writeBlock($v_binary_data);
  2055. }
  2056. @bzclose($v_temp_tar);
  2057. } elseif ($this->_compress_type == 'lzma2') {
  2058. $end_blocks = 0;
  2059. while (strlen($v_buffer = @xzread($v_temp_tar, 512)) > 0) {
  2060. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2061. $end_blocks++;
  2062. // do not copy end blocks, we will re-make them
  2063. // after appending
  2064. continue;
  2065. } elseif ($end_blocks > 0) {
  2066. for ($i = 0; $i < $end_blocks; $i++) {
  2067. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2068. }
  2069. $end_blocks = 0;
  2070. }
  2071. $v_binary_data = pack("a512", $v_buffer);
  2072. $this->_writeBlock($v_binary_data);
  2073. }
  2074. @xzclose($v_temp_tar);
  2075. }
  2076. if (!@unlink($this->_tarname . ".tmp")) {
  2077. $this->_error(
  2078. 'Error while deleting temporary file \''
  2079. . $this->_tarname . '.tmp\''
  2080. );
  2081. }
  2082. } else {
  2083. // ----- For not compressed tar, just add files before the last
  2084. // one or two 512 bytes block
  2085. if (!$this->_openReadWrite()) {
  2086. return false;
  2087. }
  2088. clearstatcache();
  2089. $v_size = filesize($this->_tarname);
  2090. // We might have zero, one or two end blocks.
  2091. // The standard is two, but we should try to handle
  2092. // other cases.
  2093. fseek($this->_file, $v_size - 1024);
  2094. if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  2095. fseek($this->_file, $v_size - 1024);
  2096. } elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  2097. fseek($this->_file, $v_size - 512);
  2098. }
  2099. }
  2100. return true;
  2101. }
  2102. /**
  2103. * @param $p_filelist
  2104. * @param string $p_add_dir
  2105. * @param string $p_remove_dir
  2106. * @return bool
  2107. */
  2108. public function _append($p_filelist, $p_add_dir = '', $p_remove_dir = '')
  2109. {
  2110. if (!$this->_openAppend()) {
  2111. return false;
  2112. }
  2113. if ($this->_addList($p_filelist, $p_add_dir, $p_remove_dir)) {
  2114. $this->_writeFooter();
  2115. }
  2116. $this->_close();
  2117. return true;
  2118. }
  2119. /**
  2120. * Check if a directory exists and create it (including parent
  2121. * dirs) if not.
  2122. *
  2123. * @param string $p_dir directory to check
  2124. *
  2125. * @return bool true if the directory exists or was created
  2126. */
  2127. public function _dirCheck($p_dir)
  2128. {
  2129. clearstatcache();
  2130. if ((@is_dir($p_dir)) || ($p_dir == '')) {
  2131. return true;
  2132. }
  2133. $p_parent_dir = dirname($p_dir);
  2134. if (($p_parent_dir != $p_dir) &&
  2135. ($p_parent_dir != '') &&
  2136. (!$this->_dirCheck($p_parent_dir))
  2137. ) {
  2138. return false;
  2139. }
  2140. if (!@mkdir($p_dir, 0777)) {
  2141. $this->_error("Unable to create directory '$p_dir'");
  2142. return false;
  2143. }
  2144. return true;
  2145. }
  2146. /**
  2147. * Compress path by changing for example "/dir/foo/../bar" to "/dir/bar",
  2148. * rand emove double slashes.
  2149. *
  2150. * @param string $p_dir path to reduce
  2151. *
  2152. * @return string reduced path
  2153. */
  2154. private function _pathReduction($p_dir)
  2155. {
  2156. $v_result = '';
  2157. // ----- Look for not empty path
  2158. if ($p_dir != '') {
  2159. // ----- Explode path by directory names
  2160. $v_list = explode('/', $p_dir);
  2161. // ----- Study directories from last to first
  2162. for ($i = sizeof($v_list) - 1; $i >= 0; $i--) {
  2163. // ----- Look for current path
  2164. if ($v_list[$i] == ".") {
  2165. // ----- Ignore this directory
  2166. // Should be the first $i=0, but no check is done
  2167. } else {
  2168. if ($v_list[$i] == "..") {
  2169. // ----- Ignore it and ignore the $i-1
  2170. $i--;
  2171. } else {
  2172. if (($v_list[$i] == '')
  2173. && ($i != (sizeof($v_list) - 1))
  2174. && ($i != 0)
  2175. ) {
  2176. // ----- Ignore only the double '//' in path,
  2177. // but not the first and last /
  2178. } else {
  2179. $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? '/'
  2180. . $v_result : '');
  2181. }
  2182. }
  2183. }
  2184. }
  2185. }
  2186. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  2187. $v_result = strtr($v_result, '\\', '/');
  2188. }
  2189. return $v_result;
  2190. }
  2191. /**
  2192. * @param $p_path
  2193. * @param bool $p_remove_disk_letter
  2194. * @return string
  2195. */
  2196. public function _translateWinPath($p_path, $p_remove_disk_letter = true)
  2197. {
  2198. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  2199. // ----- Look for potential disk letter
  2200. if (($p_remove_disk_letter)
  2201. && (($v_position = strpos($p_path, ':')) != false)
  2202. ) {
  2203. $p_path = substr($p_path, $v_position + 1);
  2204. }
  2205. // ----- Change potential windows directory separator
  2206. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
  2207. $p_path = strtr($p_path, '\\', '/');
  2208. }
  2209. }
  2210. return $p_path;
  2211. }
  2212. }