system.tar.inc 88 KB

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