system.tar.inc 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  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.9 (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 != '') {
  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 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 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 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 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 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 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. $regexp = 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) + 1) * 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. if (function_exists('posix_getpwuid')) {
  1320. $userinfo = posix_getpwuid($v_info[4]);
  1321. $groupinfo = posix_getgrgid($v_info[5]);
  1322. $v_uname = $userinfo['name'];
  1323. $v_gname = $groupinfo['name'];
  1324. } else {
  1325. $v_uname = '';
  1326. $v_gname = '';
  1327. }
  1328. $v_devmajor = '';
  1329. $v_devminor = '';
  1330. $v_prefix = '';
  1331. $v_binary_data_first = pack(
  1332. "a100a8a8a8a12a12",
  1333. $v_reduced_filename,
  1334. $v_perms,
  1335. $v_uid,
  1336. $v_gid,
  1337. $v_size,
  1338. $v_mtime
  1339. );
  1340. $v_binary_data_last = pack(
  1341. "a1a100a6a2a32a32a8a8a155a12",
  1342. $v_typeflag,
  1343. $v_linkname,
  1344. $v_magic,
  1345. $v_version,
  1346. $v_uname,
  1347. $v_gname,
  1348. $v_devmajor,
  1349. $v_devminor,
  1350. $v_prefix,
  1351. ''
  1352. );
  1353. // ----- Calculate the checksum
  1354. $v_checksum = 0;
  1355. // ..... First part of the header
  1356. for ($i = 0; $i < 148; $i++) {
  1357. $v_checksum += ord(substr($v_binary_data_first, $i, 1));
  1358. }
  1359. // ..... Ignore the checksum value and replace it by ' ' (space)
  1360. for ($i = 148; $i < 156; $i++) {
  1361. $v_checksum += ord(' ');
  1362. }
  1363. // ..... Last part of the header
  1364. for ($i = 156, $j = 0; $i < 512; $i++, $j++) {
  1365. $v_checksum += ord(substr($v_binary_data_last, $j, 1));
  1366. }
  1367. // ----- Write the first 148 bytes of the header in the archive
  1368. $this->_writeBlock($v_binary_data_first, 148);
  1369. // ----- Write the calculated checksum
  1370. $v_checksum = sprintf("%06s\0 ", DecOct($v_checksum));
  1371. $v_binary_data = pack("a8", $v_checksum);
  1372. $this->_writeBlock($v_binary_data, 8);
  1373. // ----- Write the last 356 bytes of the header in the archive
  1374. $this->_writeBlock($v_binary_data_last, 356);
  1375. return true;
  1376. }
  1377. /**
  1378. * @param string $p_filename
  1379. * @param int $p_size
  1380. * @param int $p_mtime
  1381. * @param int $p_perms
  1382. * @param string $p_type
  1383. * @param int $p_uid
  1384. * @param int $p_gid
  1385. * @return bool
  1386. */
  1387. public function _writeHeaderBlock(
  1388. $p_filename,
  1389. $p_size,
  1390. $p_mtime = 0,
  1391. $p_perms = 0,
  1392. $p_type = '',
  1393. $p_uid = 0,
  1394. $p_gid = 0
  1395. )
  1396. {
  1397. $p_filename = $this->_pathReduction($p_filename);
  1398. if (strlen($p_filename) > 99) {
  1399. if (!$this->_writeLongHeader($p_filename, false)) {
  1400. return false;
  1401. }
  1402. }
  1403. if ($p_type == "5") {
  1404. $v_size = sprintf("%011s", DecOct(0));
  1405. } else {
  1406. $v_size = sprintf("%011s", DecOct($p_size));
  1407. }
  1408. $v_uid = sprintf("%07s", DecOct($p_uid));
  1409. $v_gid = sprintf("%07s", DecOct($p_gid));
  1410. $v_perms = sprintf("%07s", DecOct($p_perms & 000777));
  1411. $v_mtime = sprintf("%11s", DecOct($p_mtime));
  1412. $v_linkname = '';
  1413. $v_magic = 'ustar ';
  1414. $v_version = ' ';
  1415. if (function_exists('posix_getpwuid')) {
  1416. $userinfo = posix_getpwuid($p_uid);
  1417. $groupinfo = posix_getgrgid($p_gid);
  1418. $v_uname = $userinfo['name'];
  1419. $v_gname = $groupinfo['name'];
  1420. } else {
  1421. $v_uname = '';
  1422. $v_gname = '';
  1423. }
  1424. $v_devmajor = '';
  1425. $v_devminor = '';
  1426. $v_prefix = '';
  1427. $v_binary_data_first = pack(
  1428. "a100a8a8a8a12A12",
  1429. $p_filename,
  1430. $v_perms,
  1431. $v_uid,
  1432. $v_gid,
  1433. $v_size,
  1434. $v_mtime
  1435. );
  1436. $v_binary_data_last = pack(
  1437. "a1a100a6a2a32a32a8a8a155a12",
  1438. $p_type,
  1439. $v_linkname,
  1440. $v_magic,
  1441. $v_version,
  1442. $v_uname,
  1443. $v_gname,
  1444. $v_devmajor,
  1445. $v_devminor,
  1446. $v_prefix,
  1447. ''
  1448. );
  1449. // ----- Calculate the checksum
  1450. $v_checksum = 0;
  1451. // ..... First part of the header
  1452. for ($i = 0; $i < 148; $i++) {
  1453. $v_checksum += ord(substr($v_binary_data_first, $i, 1));
  1454. }
  1455. // ..... Ignore the checksum value and replace it by ' ' (space)
  1456. for ($i = 148; $i < 156; $i++) {
  1457. $v_checksum += ord(' ');
  1458. }
  1459. // ..... Last part of the header
  1460. for ($i = 156, $j = 0; $i < 512; $i++, $j++) {
  1461. $v_checksum += ord(substr($v_binary_data_last, $j, 1));
  1462. }
  1463. // ----- Write the first 148 bytes of the header in the archive
  1464. $this->_writeBlock($v_binary_data_first, 148);
  1465. // ----- Write the calculated checksum
  1466. $v_checksum = sprintf("%06s ", DecOct($v_checksum));
  1467. $v_binary_data = pack("a8", $v_checksum);
  1468. $this->_writeBlock($v_binary_data, 8);
  1469. // ----- Write the last 356 bytes of the header in the archive
  1470. $this->_writeBlock($v_binary_data_last, 356);
  1471. return true;
  1472. }
  1473. /**
  1474. * @param string $p_filename
  1475. * @return bool
  1476. */
  1477. public function _writeLongHeader($p_filename, $is_link = false)
  1478. {
  1479. $v_uid = sprintf("%07s", 0);
  1480. $v_gid = sprintf("%07s", 0);
  1481. $v_perms = sprintf("%07s", 0);
  1482. $v_size = sprintf("%'011s", DecOct(strlen($p_filename)));
  1483. $v_mtime = sprintf("%011s", 0);
  1484. $v_typeflag = ($is_link ? 'K' : 'L');
  1485. $v_linkname = '';
  1486. $v_magic = 'ustar ';
  1487. $v_version = ' ';
  1488. $v_uname = '';
  1489. $v_gname = '';
  1490. $v_devmajor = '';
  1491. $v_devminor = '';
  1492. $v_prefix = '';
  1493. $v_binary_data_first = pack(
  1494. "a100a8a8a8a12a12",
  1495. '././@LongLink',
  1496. $v_perms,
  1497. $v_uid,
  1498. $v_gid,
  1499. $v_size,
  1500. $v_mtime
  1501. );
  1502. $v_binary_data_last = pack(
  1503. "a1a100a6a2a32a32a8a8a155a12",
  1504. $v_typeflag,
  1505. $v_linkname,
  1506. $v_magic,
  1507. $v_version,
  1508. $v_uname,
  1509. $v_gname,
  1510. $v_devmajor,
  1511. $v_devminor,
  1512. $v_prefix,
  1513. ''
  1514. );
  1515. // ----- Calculate the checksum
  1516. $v_checksum = 0;
  1517. // ..... First part of the header
  1518. for ($i = 0; $i < 148; $i++) {
  1519. $v_checksum += ord(substr($v_binary_data_first, $i, 1));
  1520. }
  1521. // ..... Ignore the checksum value and replace it by ' ' (space)
  1522. for ($i = 148; $i < 156; $i++) {
  1523. $v_checksum += ord(' ');
  1524. }
  1525. // ..... Last part of the header
  1526. for ($i = 156, $j = 0; $i < 512; $i++, $j++) {
  1527. $v_checksum += ord(substr($v_binary_data_last, $j, 1));
  1528. }
  1529. // ----- Write the first 148 bytes of the header in the archive
  1530. $this->_writeBlock($v_binary_data_first, 148);
  1531. // ----- Write the calculated checksum
  1532. $v_checksum = sprintf("%06s\0 ", DecOct($v_checksum));
  1533. $v_binary_data = pack("a8", $v_checksum);
  1534. $this->_writeBlock($v_binary_data, 8);
  1535. // ----- Write the last 356 bytes of the header in the archive
  1536. $this->_writeBlock($v_binary_data_last, 356);
  1537. // ----- Write the filename as content of the block
  1538. $i = 0;
  1539. while (($v_buffer = substr($p_filename, (($i++) * 512), 512)) != '') {
  1540. $v_binary_data = pack("a512", "$v_buffer");
  1541. $this->_writeBlock($v_binary_data);
  1542. }
  1543. return true;
  1544. }
  1545. /**
  1546. * @param mixed $v_binary_data
  1547. * @param mixed $v_header
  1548. * @return bool
  1549. */
  1550. public function _readHeader($v_binary_data, &$v_header)
  1551. {
  1552. if (strlen($v_binary_data) == 0) {
  1553. $v_header['filename'] = '';
  1554. return true;
  1555. }
  1556. if (strlen($v_binary_data) != 512) {
  1557. $v_header['filename'] = '';
  1558. $this->_error('Invalid block size : ' . strlen($v_binary_data));
  1559. return false;
  1560. }
  1561. if (!is_array($v_header)) {
  1562. $v_header = array();
  1563. }
  1564. // ----- Calculate the checksum
  1565. $v_checksum = 0;
  1566. // ..... First part of the header
  1567. $v_binary_split = str_split($v_binary_data);
  1568. $v_checksum += array_sum(array_map('ord', array_slice($v_binary_split, 0, 148)));
  1569. $v_checksum += array_sum(array_map('ord', array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',)));
  1570. $v_checksum += array_sum(array_map('ord', array_slice($v_binary_split, 156, 512)));
  1571. $v_data = unpack($this->_fmt, $v_binary_data);
  1572. if (strlen($v_data["prefix"]) > 0) {
  1573. $v_data["filename"] = "$v_data[prefix]/$v_data[filename]";
  1574. }
  1575. // ----- Extract the checksum
  1576. $v_data_checksum = trim($v_data['checksum']);
  1577. if (!preg_match('/^[0-7]*$/', $v_data_checksum)) {
  1578. $this->_error(
  1579. 'Invalid checksum for file "' . $v_data['filename']
  1580. . '" : ' . $v_data_checksum . ' extracted'
  1581. );
  1582. return false;
  1583. }
  1584. $v_header['checksum'] = OctDec($v_data_checksum);
  1585. if ($v_header['checksum'] != $v_checksum) {
  1586. $v_header['filename'] = '';
  1587. // ----- Look for last block (empty block)
  1588. if (($v_checksum == 256) && ($v_header['checksum'] == 0)) {
  1589. return true;
  1590. }
  1591. $this->_error(
  1592. 'Invalid checksum for file "' . $v_data['filename']
  1593. . '" : ' . $v_checksum . ' calculated, '
  1594. . $v_header['checksum'] . ' expected'
  1595. );
  1596. return false;
  1597. }
  1598. // ----- Extract the properties
  1599. $v_header['filename'] = rtrim($v_data['filename'], "\0");
  1600. if ($this->_maliciousFilename($v_header['filename'])) {
  1601. $this->_error(
  1602. 'Malicious .tar detected, file "' . $v_header['filename'] .
  1603. '" will not install in desired directory tree'
  1604. );
  1605. return false;
  1606. }
  1607. $v_header['mode'] = OctDec(trim($v_data['mode']));
  1608. $v_header['uid'] = OctDec(trim($v_data['uid']));
  1609. $v_header['gid'] = OctDec(trim($v_data['gid']));
  1610. $v_header['size'] = $this->_tarRecToSize($v_data['size']);
  1611. $v_header['mtime'] = OctDec(trim($v_data['mtime']));
  1612. if (($v_header['typeflag'] = $v_data['typeflag']) == "5") {
  1613. $v_header['size'] = 0;
  1614. }
  1615. $v_header['link'] = trim($v_data['link']);
  1616. /* ----- All these fields are removed form the header because
  1617. they do not carry interesting info
  1618. $v_header[magic] = trim($v_data[magic]);
  1619. $v_header[version] = trim($v_data[version]);
  1620. $v_header[uname] = trim($v_data[uname]);
  1621. $v_header[gname] = trim($v_data[gname]);
  1622. $v_header[devmajor] = trim($v_data[devmajor]);
  1623. $v_header[devminor] = trim($v_data[devminor]);
  1624. */
  1625. return true;
  1626. }
  1627. /**
  1628. * Convert Tar record size to actual size
  1629. *
  1630. * @param string $tar_size
  1631. * @return size of tar record in bytes
  1632. */
  1633. private function _tarRecToSize($tar_size)
  1634. {
  1635. /*
  1636. * First byte of size has a special meaning if bit 7 is set.
  1637. *
  1638. * Bit 7 indicates base-256 encoding if set.
  1639. * Bit 6 is the sign bit.
  1640. * Bits 5:0 are most significant value bits.
  1641. */
  1642. $ch = ord($tar_size[0]);
  1643. if ($ch & 0x80) {
  1644. // Full 12-bytes record is required.
  1645. $rec_str = $tar_size . "\x00";
  1646. $size = ($ch & 0x40) ? -1 : 0;
  1647. $size = ($size << 6) | ($ch & 0x3f);
  1648. for ($num_ch = 1; $num_ch < 12; ++$num_ch) {
  1649. $size = ($size * 256) + ord($rec_str[$num_ch]);
  1650. }
  1651. return $size;
  1652. } else {
  1653. return OctDec(trim($tar_size));
  1654. }
  1655. }
  1656. /**
  1657. * Detect and report a malicious file name
  1658. *
  1659. * @param string $file
  1660. *
  1661. * @return bool
  1662. */
  1663. private function _maliciousFilename($file)
  1664. {
  1665. if (strpos($file, 'phar://') === 0) {
  1666. return true;
  1667. }
  1668. if (strpos($file, '../') !== false || strpos($file, '..\\') !== false) {
  1669. return true;
  1670. }
  1671. return false;
  1672. }
  1673. /**
  1674. * @param $v_header
  1675. * @return bool
  1676. */
  1677. public function _readLongHeader(&$v_header)
  1678. {
  1679. $v_filename = '';
  1680. $v_filesize = $v_header['size'];
  1681. $n = floor($v_header['size'] / 512);
  1682. for ($i = 0; $i < $n; $i++) {
  1683. $v_content = $this->_readBlock();
  1684. $v_filename .= $v_content;
  1685. }
  1686. if (($v_header['size'] % 512) != 0) {
  1687. $v_content = $this->_readBlock();
  1688. $v_filename .= $v_content;
  1689. }
  1690. // ----- Read the next header
  1691. $v_binary_data = $this->_readBlock();
  1692. if (!$this->_readHeader($v_binary_data, $v_header)) {
  1693. return false;
  1694. }
  1695. $v_filename = rtrim(substr($v_filename, 0, $v_filesize), "\0");
  1696. $v_header['filename'] = $v_filename;
  1697. if ($this->_maliciousFilename($v_filename)) {
  1698. $this->_error(
  1699. 'Malicious .tar detected, file "' . $v_filename .
  1700. '" will not install in desired directory tree'
  1701. );
  1702. return false;
  1703. }
  1704. return true;
  1705. }
  1706. /**
  1707. * This method extract from the archive one file identified by $p_filename.
  1708. * The return value is a string with the file content, or null on error.
  1709. *
  1710. * @param string $p_filename The path of the file to extract in a string.
  1711. *
  1712. * @return a string with the file content or null.
  1713. */
  1714. private function _extractInString($p_filename)
  1715. {
  1716. $v_result_str = "";
  1717. while (strlen($v_binary_data = $this->_readBlock()) != 0) {
  1718. if (!$this->_readHeader($v_binary_data, $v_header)) {
  1719. return null;
  1720. }
  1721. if ($v_header['filename'] == '') {
  1722. continue;
  1723. }
  1724. switch ($v_header['typeflag']) {
  1725. case 'L':
  1726. {
  1727. if (!$this->_readLongHeader($v_header)) {
  1728. return null;
  1729. }
  1730. }
  1731. break;
  1732. case 'K':
  1733. {
  1734. $v_link_header = $v_header;
  1735. if (!$this->_readLongHeader($v_link_header)) {
  1736. return null;
  1737. }
  1738. $v_header['link'] = $v_link_header['filename'];
  1739. }
  1740. break;
  1741. }
  1742. if ($v_header['filename'] == $p_filename) {
  1743. if ($v_header['typeflag'] == "5") {
  1744. $this->_error(
  1745. 'Unable to extract in string a directory '
  1746. . 'entry {' . $v_header['filename'] . '}'
  1747. );
  1748. return null;
  1749. } else {
  1750. $n = floor($v_header['size'] / 512);
  1751. for ($i = 0; $i < $n; $i++) {
  1752. $v_result_str .= $this->_readBlock();
  1753. }
  1754. if (($v_header['size'] % 512) != 0) {
  1755. $v_content = $this->_readBlock();
  1756. $v_result_str .= substr(
  1757. $v_content,
  1758. 0,
  1759. ($v_header['size'] % 512)
  1760. );
  1761. }
  1762. return $v_result_str;
  1763. }
  1764. } else {
  1765. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  1766. }
  1767. }
  1768. return null;
  1769. }
  1770. /**
  1771. * @param string $p_path
  1772. * @param string $p_list_detail
  1773. * @param string $p_mode
  1774. * @param string $p_file_list
  1775. * @param string $p_remove_path
  1776. * @param bool $p_preserve
  1777. * @param bool $p_symlinks
  1778. * @return bool
  1779. */
  1780. public function _extractList(
  1781. $p_path,
  1782. &$p_list_detail,
  1783. $p_mode,
  1784. $p_file_list,
  1785. $p_remove_path,
  1786. $p_preserve = false,
  1787. $p_symlinks = true
  1788. )
  1789. {
  1790. $v_result = true;
  1791. $v_nb = 0;
  1792. $v_extract_all = true;
  1793. $v_listing = false;
  1794. $p_path = $this->_translateWinPath($p_path, false);
  1795. if ($p_path == '' || (substr($p_path, 0, 1) != '/'
  1796. && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))
  1797. ) {
  1798. $p_path = "./" . $p_path;
  1799. }
  1800. $p_remove_path = $this->_translateWinPath($p_remove_path);
  1801. // ----- Look for path to remove format (should end by /)
  1802. if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/')) {
  1803. $p_remove_path .= '/';
  1804. }
  1805. $p_remove_path_size = strlen($p_remove_path);
  1806. switch ($p_mode) {
  1807. case "complete" :
  1808. $v_extract_all = true;
  1809. $v_listing = false;
  1810. break;
  1811. case "partial" :
  1812. $v_extract_all = false;
  1813. $v_listing = false;
  1814. break;
  1815. case "list" :
  1816. $v_extract_all = false;
  1817. $v_listing = true;
  1818. break;
  1819. default :
  1820. $this->_error('Invalid extract mode (' . $p_mode . ')');
  1821. return false;
  1822. }
  1823. clearstatcache();
  1824. while (strlen($v_binary_data = $this->_readBlock()) != 0) {
  1825. $v_extract_file = false;
  1826. $v_extraction_stopped = 0;
  1827. if (!$this->_readHeader($v_binary_data, $v_header)) {
  1828. return false;
  1829. }
  1830. if ($v_header['filename'] == '') {
  1831. continue;
  1832. }
  1833. switch ($v_header['typeflag']) {
  1834. case 'L':
  1835. {
  1836. if (!$this->_readLongHeader($v_header)) {
  1837. return null;
  1838. }
  1839. }
  1840. break;
  1841. case 'K':
  1842. {
  1843. $v_link_header = $v_header;
  1844. if (!$this->_readLongHeader($v_link_header)) {
  1845. return null;
  1846. }
  1847. $v_header['link'] = $v_link_header['filename'];
  1848. }
  1849. break;
  1850. }
  1851. // ignore extended / pax headers
  1852. if ($v_header['typeflag'] == 'x' || $v_header['typeflag'] == 'g') {
  1853. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  1854. continue;
  1855. }
  1856. if ((!$v_extract_all) && (is_array($p_file_list))) {
  1857. // ----- By default no unzip if the file is not found
  1858. $v_extract_file = false;
  1859. for ($i = 0; $i < sizeof($p_file_list); $i++) {
  1860. // ----- Look if it is a directory
  1861. if (substr($p_file_list[$i], -1) == '/') {
  1862. // ----- Look if the directory is in the filename path
  1863. if ((strlen($v_header['filename']) > strlen($p_file_list[$i]))
  1864. && (substr($v_header['filename'], 0, strlen($p_file_list[$i]))
  1865. == $p_file_list[$i])
  1866. ) {
  1867. $v_extract_file = true;
  1868. break;
  1869. }
  1870. } // ----- It is a file, so compare the file names
  1871. elseif ($p_file_list[$i] == $v_header['filename']) {
  1872. $v_extract_file = true;
  1873. break;
  1874. }
  1875. }
  1876. } else {
  1877. $v_extract_file = true;
  1878. }
  1879. // ----- Look if this file need to be extracted
  1880. if (($v_extract_file) && (!$v_listing)) {
  1881. if (($p_remove_path != '')
  1882. && (substr($v_header['filename'] . '/', 0, $p_remove_path_size)
  1883. == $p_remove_path)
  1884. ) {
  1885. $v_header['filename'] = substr(
  1886. $v_header['filename'],
  1887. $p_remove_path_size
  1888. );
  1889. if ($v_header['filename'] == '') {
  1890. continue;
  1891. }
  1892. }
  1893. if (($p_path != './') && ($p_path != '/')) {
  1894. while (substr($p_path, -1) == '/') {
  1895. $p_path = substr($p_path, 0, strlen($p_path) - 1);
  1896. }
  1897. if (substr($v_header['filename'], 0, 1) == '/') {
  1898. $v_header['filename'] = $p_path . $v_header['filename'];
  1899. } else {
  1900. $v_header['filename'] = $p_path . '/' . $v_header['filename'];
  1901. }
  1902. }
  1903. if (file_exists($v_header['filename'])) {
  1904. if ((@is_dir($v_header['filename']))
  1905. && ($v_header['typeflag'] == '')
  1906. ) {
  1907. $this->_error(
  1908. 'File ' . $v_header['filename']
  1909. . ' already exists as a directory'
  1910. );
  1911. return false;
  1912. }
  1913. if (($this->_isArchive($v_header['filename']))
  1914. && ($v_header['typeflag'] == "5")
  1915. ) {
  1916. $this->_error(
  1917. 'Directory ' . $v_header['filename']
  1918. . ' already exists as a file'
  1919. );
  1920. return false;
  1921. }
  1922. if (!is_writeable($v_header['filename'])) {
  1923. $this->_error(
  1924. 'File ' . $v_header['filename']
  1925. . ' already exists and is write protected'
  1926. );
  1927. return false;
  1928. }
  1929. if (filemtime($v_header['filename']) > $v_header['mtime']) {
  1930. // To be completed : An error or silent no replace ?
  1931. }
  1932. } // ----- Check the directory availability and create it if necessary
  1933. elseif (($v_result
  1934. = $this->_dirCheck(
  1935. ($v_header['typeflag'] == "5"
  1936. ? $v_header['filename']
  1937. : dirname($v_header['filename']))
  1938. )) != 1
  1939. ) {
  1940. $this->_error('Unable to create path for ' . $v_header['filename']);
  1941. return false;
  1942. }
  1943. if ($v_extract_file) {
  1944. if ($v_header['typeflag'] == "5") {
  1945. if (!@file_exists($v_header['filename'])) {
  1946. if (!@mkdir($v_header['filename'], 0777)) {
  1947. $this->_error(
  1948. 'Unable to create directory {'
  1949. . $v_header['filename'] . '}'
  1950. );
  1951. return false;
  1952. }
  1953. }
  1954. } elseif ($v_header['typeflag'] == "2") {
  1955. if (!$p_symlinks) {
  1956. $this->_warning('Symbolic links are not allowed. '
  1957. . 'Unable to extract {'
  1958. . $v_header['filename'] . '}'
  1959. );
  1960. return false;
  1961. }
  1962. if (@file_exists($v_header['filename'])) {
  1963. @drupal_unlink($v_header['filename']);
  1964. }
  1965. if (!@symlink($v_header['link'], $v_header['filename'])) {
  1966. $this->_error(
  1967. 'Unable to extract symbolic link {'
  1968. . $v_header['filename'] . '}'
  1969. );
  1970. return false;
  1971. }
  1972. } else {
  1973. if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
  1974. $this->_error(
  1975. 'Error while opening {' . $v_header['filename']
  1976. . '} in write binary mode'
  1977. );
  1978. return false;
  1979. } else {
  1980. $n = floor($v_header['size'] / 512);
  1981. for ($i = 0; $i < $n; $i++) {
  1982. $v_content = $this->_readBlock();
  1983. fwrite($v_dest_file, $v_content, 512);
  1984. }
  1985. if (($v_header['size'] % 512) != 0) {
  1986. $v_content = $this->_readBlock();
  1987. fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
  1988. }
  1989. @fclose($v_dest_file);
  1990. if ($p_preserve) {
  1991. @chown($v_header['filename'], $v_header['uid']);
  1992. @chgrp($v_header['filename'], $v_header['gid']);
  1993. }
  1994. // ----- Change the file mode, mtime
  1995. @touch($v_header['filename'], $v_header['mtime']);
  1996. if ($v_header['mode'] & 0111) {
  1997. // make file executable, obey umask
  1998. $mode = fileperms($v_header['filename']) | (~umask() & 0111);
  1999. @chmod($v_header['filename'], $mode);
  2000. }
  2001. }
  2002. // ----- Check the file size
  2003. clearstatcache();
  2004. if (!is_file($v_header['filename'])) {
  2005. $this->_error(
  2006. 'Extracted file ' . $v_header['filename']
  2007. . 'does not exist. Archive may be corrupted.'
  2008. );
  2009. return false;
  2010. }
  2011. $filesize = filesize($v_header['filename']);
  2012. if ($filesize != $v_header['size']) {
  2013. $this->_error(
  2014. 'Extracted file ' . $v_header['filename']
  2015. . ' does not have the correct file size \''
  2016. . $filesize
  2017. . '\' (' . $v_header['size']
  2018. . ' expected). Archive may be corrupted.'
  2019. );
  2020. return false;
  2021. }
  2022. }
  2023. } else {
  2024. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  2025. }
  2026. } else {
  2027. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  2028. }
  2029. /* TBC : Seems to be unused ...
  2030. if ($this->_compress)
  2031. $v_end_of_file = @gzeof($this->_file);
  2032. else
  2033. $v_end_of_file = @feof($this->_file);
  2034. */
  2035. if ($v_listing || $v_extract_file || $v_extraction_stopped) {
  2036. // ----- Log extracted files
  2037. if (($v_file_dir = dirname($v_header['filename']))
  2038. == $v_header['filename']
  2039. ) {
  2040. $v_file_dir = '';
  2041. }
  2042. if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == '')) {
  2043. $v_file_dir = '/';
  2044. }
  2045. $p_list_detail[$v_nb++] = $v_header;
  2046. if (is_array($p_file_list) && (count($p_list_detail) == count($p_file_list))) {
  2047. return true;
  2048. }
  2049. }
  2050. }
  2051. return true;
  2052. }
  2053. /**
  2054. * @return bool
  2055. */
  2056. public function _openAppend()
  2057. {
  2058. if (filesize($this->_tarname) == 0) {
  2059. return $this->_openWrite();
  2060. }
  2061. if ($this->_compress) {
  2062. $this->_close();
  2063. if (!@rename($this->_tarname, $this->_tarname . ".tmp")) {
  2064. $this->_error(
  2065. 'Error while renaming \'' . $this->_tarname
  2066. . '\' to temporary file \'' . $this->_tarname
  2067. . '.tmp\''
  2068. );
  2069. return false;
  2070. }
  2071. if ($this->_compress_type == 'gz') {
  2072. $v_temp_tar = @gzopen($this->_tarname . ".tmp", "rb");
  2073. } elseif ($this->_compress_type == 'bz2') {
  2074. $v_temp_tar = @bzopen($this->_tarname . ".tmp", "r");
  2075. } elseif ($this->_compress_type == 'lzma2') {
  2076. $v_temp_tar = @xzopen($this->_tarname . ".tmp", "r");
  2077. }
  2078. if ($v_temp_tar == 0) {
  2079. $this->_error(
  2080. 'Unable to open file \'' . $this->_tarname
  2081. . '.tmp\' in binary read mode'
  2082. );
  2083. @rename($this->_tarname . ".tmp", $this->_tarname);
  2084. return false;
  2085. }
  2086. if (!$this->_openWrite()) {
  2087. @rename($this->_tarname . ".tmp", $this->_tarname);
  2088. return false;
  2089. }
  2090. if ($this->_compress_type == 'gz') {
  2091. $end_blocks = 0;
  2092. while (!@gzeof($v_temp_tar)) {
  2093. $v_buffer = @gzread($v_temp_tar, 512);
  2094. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2095. $end_blocks++;
  2096. // do not copy end blocks, we will re-make them
  2097. // after appending
  2098. continue;
  2099. } elseif ($end_blocks > 0) {
  2100. for ($i = 0; $i < $end_blocks; $i++) {
  2101. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2102. }
  2103. $end_blocks = 0;
  2104. }
  2105. $v_binary_data = pack("a512", $v_buffer);
  2106. $this->_writeBlock($v_binary_data);
  2107. }
  2108. @gzclose($v_temp_tar);
  2109. } elseif ($this->_compress_type == 'bz2') {
  2110. $end_blocks = 0;
  2111. while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) {
  2112. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2113. $end_blocks++;
  2114. // do not copy end blocks, we will re-make them
  2115. // after appending
  2116. continue;
  2117. } elseif ($end_blocks > 0) {
  2118. for ($i = 0; $i < $end_blocks; $i++) {
  2119. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2120. }
  2121. $end_blocks = 0;
  2122. }
  2123. $v_binary_data = pack("a512", $v_buffer);
  2124. $this->_writeBlock($v_binary_data);
  2125. }
  2126. @bzclose($v_temp_tar);
  2127. } elseif ($this->_compress_type == 'lzma2') {
  2128. $end_blocks = 0;
  2129. while (strlen($v_buffer = @xzread($v_temp_tar, 512)) > 0) {
  2130. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2131. $end_blocks++;
  2132. // do not copy end blocks, we will re-make them
  2133. // after appending
  2134. continue;
  2135. } elseif ($end_blocks > 0) {
  2136. for ($i = 0; $i < $end_blocks; $i++) {
  2137. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2138. }
  2139. $end_blocks = 0;
  2140. }
  2141. $v_binary_data = pack("a512", $v_buffer);
  2142. $this->_writeBlock($v_binary_data);
  2143. }
  2144. @xzclose($v_temp_tar);
  2145. }
  2146. if (!@drupal_unlink($this->_tarname . ".tmp")) {
  2147. $this->_error(
  2148. 'Error while deleting temporary file \''
  2149. . $this->_tarname . '.tmp\''
  2150. );
  2151. }
  2152. } else {
  2153. // ----- For not compressed tar, just add files before the last
  2154. // one or two 512 bytes block
  2155. if (!$this->_openReadWrite()) {
  2156. return false;
  2157. }
  2158. clearstatcache();
  2159. $v_size = filesize($this->_tarname);
  2160. // We might have zero, one or two end blocks.
  2161. // The standard is two, but we should try to handle
  2162. // other cases.
  2163. fseek($this->_file, $v_size - 1024);
  2164. if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  2165. fseek($this->_file, $v_size - 1024);
  2166. } elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  2167. fseek($this->_file, $v_size - 512);
  2168. }
  2169. }
  2170. return true;
  2171. }
  2172. /**
  2173. * @param $p_filelist
  2174. * @param string $p_add_dir
  2175. * @param string $p_remove_dir
  2176. * @return bool
  2177. */
  2178. public function _append($p_filelist, $p_add_dir = '', $p_remove_dir = '')
  2179. {
  2180. if (!$this->_openAppend()) {
  2181. return false;
  2182. }
  2183. if ($this->_addList($p_filelist, $p_add_dir, $p_remove_dir)) {
  2184. $this->_writeFooter();
  2185. }
  2186. $this->_close();
  2187. return true;
  2188. }
  2189. /**
  2190. * Check if a directory exists and create it (including parent
  2191. * dirs) if not.
  2192. *
  2193. * @param string $p_dir directory to check
  2194. *
  2195. * @return bool true if the directory exists or was created
  2196. */
  2197. public function _dirCheck($p_dir)
  2198. {
  2199. clearstatcache();
  2200. if ((@is_dir($p_dir)) || ($p_dir == '')) {
  2201. return true;
  2202. }
  2203. $p_parent_dir = dirname($p_dir);
  2204. if (($p_parent_dir != $p_dir) &&
  2205. ($p_parent_dir != '') &&
  2206. (!$this->_dirCheck($p_parent_dir))
  2207. ) {
  2208. return false;
  2209. }
  2210. if (!@mkdir($p_dir, 0777)) {
  2211. $this->_error("Unable to create directory '$p_dir'");
  2212. return false;
  2213. }
  2214. return true;
  2215. }
  2216. /**
  2217. * Compress path by changing for example "/dir/foo/../bar" to "/dir/bar",
  2218. * rand emove double slashes.
  2219. *
  2220. * @param string $p_dir path to reduce
  2221. *
  2222. * @return string reduced path
  2223. */
  2224. private function _pathReduction($p_dir)
  2225. {
  2226. $v_result = '';
  2227. // ----- Look for not empty path
  2228. if ($p_dir != '') {
  2229. // ----- Explode path by directory names
  2230. $v_list = explode('/', $p_dir);
  2231. // ----- Study directories from last to first
  2232. for ($i = sizeof($v_list) - 1; $i >= 0; $i--) {
  2233. // ----- Look for current path
  2234. if ($v_list[$i] == ".") {
  2235. // ----- Ignore this directory
  2236. // Should be the first $i=0, but no check is done
  2237. } else {
  2238. if ($v_list[$i] == "..") {
  2239. // ----- Ignore it and ignore the $i-1
  2240. $i--;
  2241. } else {
  2242. if (($v_list[$i] == '')
  2243. && ($i != (sizeof($v_list) - 1))
  2244. && ($i != 0)
  2245. ) {
  2246. // ----- Ignore only the double '//' in path,
  2247. // but not the first and last /
  2248. } else {
  2249. $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? '/'
  2250. . $v_result : '');
  2251. }
  2252. }
  2253. }
  2254. }
  2255. }
  2256. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  2257. $v_result = strtr($v_result, '\\', '/');
  2258. }
  2259. return $v_result;
  2260. }
  2261. /**
  2262. * @param $p_path
  2263. * @param bool $p_remove_disk_letter
  2264. * @return string
  2265. */
  2266. public function _translateWinPath($p_path, $p_remove_disk_letter = true)
  2267. {
  2268. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  2269. // ----- Look for potential disk letter
  2270. if (($p_remove_disk_letter)
  2271. && (($v_position = strpos($p_path, ':')) != false)
  2272. ) {
  2273. $p_path = substr($p_path, $v_position + 1);
  2274. }
  2275. // ----- Change potential windows directory separator
  2276. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
  2277. $p_path = strtr($p_path, '\\', '/');
  2278. }
  2279. }
  2280. return $p_path;
  2281. }
  2282. }