ArchiveTar.php 83 KB

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