system.tar.inc 85 KB

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