system.tar.inc 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  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 != '' && (bool) preg_match('/^tar[[:alnum:]]*\.tmp$/', $this->_temp_tarname)) {
  258. @drupal_unlink($this->_temp_tarname);
  259. }
  260. }
  261. // Drupal addition from PEAR.php.
  262. /**
  263. * OS independent PHP extension load. Remember to take care
  264. * on the correct extension name for case sensitive OSes.
  265. *
  266. * @param string $ext The extension name
  267. * @return bool Success or not on the dl() call
  268. */
  269. public static function loadExtension($ext)
  270. {
  271. if (extension_loaded($ext)) {
  272. return true;
  273. }
  274. // if either returns true dl() will produce a FATAL error, stop that
  275. if (
  276. function_exists('dl') === false ||
  277. ini_get('enable_dl') != 1
  278. ) {
  279. return false;
  280. }
  281. if (OS_WINDOWS) {
  282. $suffix = '.dll';
  283. } elseif (PHP_OS == 'HP-UX') {
  284. $suffix = '.sl';
  285. } elseif (PHP_OS == 'AIX') {
  286. $suffix = '.a';
  287. } elseif (PHP_OS == 'OSX') {
  288. $suffix = '.bundle';
  289. } else {
  290. $suffix = '.so';
  291. }
  292. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  293. }
  294. /**
  295. * This method creates the archive file and add the files / directories
  296. * that are listed in $p_filelist.
  297. * If a file with the same name exist and is writable, it is replaced
  298. * by the new tar.
  299. * The method return false and a PEAR error text.
  300. * The $p_filelist parameter can be an array of string, each string
  301. * representing a filename or a directory name with their path if
  302. * needed. It can also be a single string with names separated by a
  303. * single blank.
  304. * For each directory added in the archive, the files and
  305. * sub-directories are also added.
  306. * See also createModify() method for more details.
  307. *
  308. * @param array $p_filelist An array of filenames and directory names, or a
  309. * single string with names separated by a single
  310. * blank space.
  311. *
  312. * @return 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->_isMaliciousFilename($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 _isMaliciousFilename($file)
  1664. {
  1665. if (strpos($file, '://') !== false) {
  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->_isMaliciousFilename($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 (strpos(realpath(dirname($v_header['link'])), realpath($p_path)) !== 0) {
  1956. $this->_error(
  1957. 'Out-of-path file extraction {'
  1958. . $v_header['filename'] . ' --> ' .
  1959. $v_header['link'] . '}'
  1960. );
  1961. return false;
  1962. }
  1963. if (!$p_symlinks) {
  1964. $this->_warning('Symbolic links are not allowed. '
  1965. . 'Unable to extract {'
  1966. . $v_header['filename'] . '}'
  1967. );
  1968. return false;
  1969. }
  1970. if (@file_exists($v_header['filename'])) {
  1971. @drupal_unlink($v_header['filename']);
  1972. }
  1973. if (!@symlink($v_header['link'], $v_header['filename'])) {
  1974. $this->_error(
  1975. 'Unable to extract symbolic link {'
  1976. . $v_header['filename'] . '}'
  1977. );
  1978. return false;
  1979. }
  1980. } else {
  1981. if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
  1982. $this->_error(
  1983. 'Error while opening {' . $v_header['filename']
  1984. . '} in write binary mode'
  1985. );
  1986. return false;
  1987. } else {
  1988. $n = floor($v_header['size'] / 512);
  1989. for ($i = 0; $i < $n; $i++) {
  1990. $v_content = $this->_readBlock();
  1991. fwrite($v_dest_file, $v_content, 512);
  1992. }
  1993. if (($v_header['size'] % 512) != 0) {
  1994. $v_content = $this->_readBlock();
  1995. fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
  1996. }
  1997. @fclose($v_dest_file);
  1998. if ($p_preserve) {
  1999. @chown($v_header['filename'], $v_header['uid']);
  2000. @chgrp($v_header['filename'], $v_header['gid']);
  2001. }
  2002. // ----- Change the file mode, mtime
  2003. @touch($v_header['filename'], $v_header['mtime']);
  2004. if ($v_header['mode'] & 0111) {
  2005. // make file executable, obey umask
  2006. $mode = fileperms($v_header['filename']) | (~umask() & 0111);
  2007. @chmod($v_header['filename'], $mode);
  2008. }
  2009. }
  2010. // ----- Check the file size
  2011. clearstatcache();
  2012. if (!is_file($v_header['filename'])) {
  2013. $this->_error(
  2014. 'Extracted file ' . $v_header['filename']
  2015. . 'does not exist. Archive may be corrupted.'
  2016. );
  2017. return false;
  2018. }
  2019. $filesize = filesize($v_header['filename']);
  2020. if ($filesize != $v_header['size']) {
  2021. $this->_error(
  2022. 'Extracted file ' . $v_header['filename']
  2023. . ' does not have the correct file size \''
  2024. . $filesize
  2025. . '\' (' . $v_header['size']
  2026. . ' expected). Archive may be corrupted.'
  2027. );
  2028. return false;
  2029. }
  2030. }
  2031. } else {
  2032. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  2033. }
  2034. } else {
  2035. $this->_jumpBlock(ceil(($v_header['size'] / 512)));
  2036. }
  2037. /* TBC : Seems to be unused ...
  2038. if ($this->_compress)
  2039. $v_end_of_file = @gzeof($this->_file);
  2040. else
  2041. $v_end_of_file = @feof($this->_file);
  2042. */
  2043. if ($v_listing || $v_extract_file || $v_extraction_stopped) {
  2044. // ----- Log extracted files
  2045. if (($v_file_dir = dirname($v_header['filename']))
  2046. == $v_header['filename']
  2047. ) {
  2048. $v_file_dir = '';
  2049. }
  2050. if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == '')) {
  2051. $v_file_dir = '/';
  2052. }
  2053. $p_list_detail[$v_nb++] = $v_header;
  2054. if (is_array($p_file_list) && (count($p_list_detail) == count($p_file_list))) {
  2055. return true;
  2056. }
  2057. }
  2058. }
  2059. return true;
  2060. }
  2061. /**
  2062. * @return bool
  2063. */
  2064. public function _openAppend()
  2065. {
  2066. if (filesize($this->_tarname) == 0) {
  2067. return $this->_openWrite();
  2068. }
  2069. if ($this->_compress) {
  2070. $this->_close();
  2071. if (!@rename($this->_tarname, $this->_tarname . ".tmp")) {
  2072. $this->_error(
  2073. 'Error while renaming \'' . $this->_tarname
  2074. . '\' to temporary file \'' . $this->_tarname
  2075. . '.tmp\''
  2076. );
  2077. return false;
  2078. }
  2079. if ($this->_compress_type == 'gz') {
  2080. $v_temp_tar = @gzopen($this->_tarname . ".tmp", "rb");
  2081. } elseif ($this->_compress_type == 'bz2') {
  2082. $v_temp_tar = @bzopen($this->_tarname . ".tmp", "r");
  2083. } elseif ($this->_compress_type == 'lzma2') {
  2084. $v_temp_tar = @xzopen($this->_tarname . ".tmp", "r");
  2085. }
  2086. if ($v_temp_tar == 0) {
  2087. $this->_error(
  2088. 'Unable to open file \'' . $this->_tarname
  2089. . '.tmp\' in binary read mode'
  2090. );
  2091. @rename($this->_tarname . ".tmp", $this->_tarname);
  2092. return false;
  2093. }
  2094. if (!$this->_openWrite()) {
  2095. @rename($this->_tarname . ".tmp", $this->_tarname);
  2096. return false;
  2097. }
  2098. if ($this->_compress_type == 'gz') {
  2099. $end_blocks = 0;
  2100. while (!@gzeof($v_temp_tar)) {
  2101. $v_buffer = @gzread($v_temp_tar, 512);
  2102. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2103. $end_blocks++;
  2104. // do not copy end blocks, we will re-make them
  2105. // after appending
  2106. continue;
  2107. } elseif ($end_blocks > 0) {
  2108. for ($i = 0; $i < $end_blocks; $i++) {
  2109. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2110. }
  2111. $end_blocks = 0;
  2112. }
  2113. $v_binary_data = pack("a512", $v_buffer);
  2114. $this->_writeBlock($v_binary_data);
  2115. }
  2116. @gzclose($v_temp_tar);
  2117. } elseif ($this->_compress_type == 'bz2') {
  2118. $end_blocks = 0;
  2119. while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) {
  2120. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2121. $end_blocks++;
  2122. // do not copy end blocks, we will re-make them
  2123. // after appending
  2124. continue;
  2125. } elseif ($end_blocks > 0) {
  2126. for ($i = 0; $i < $end_blocks; $i++) {
  2127. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2128. }
  2129. $end_blocks = 0;
  2130. }
  2131. $v_binary_data = pack("a512", $v_buffer);
  2132. $this->_writeBlock($v_binary_data);
  2133. }
  2134. @bzclose($v_temp_tar);
  2135. } elseif ($this->_compress_type == 'lzma2') {
  2136. $end_blocks = 0;
  2137. while (strlen($v_buffer = @xzread($v_temp_tar, 512)) > 0) {
  2138. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  2139. $end_blocks++;
  2140. // do not copy end blocks, we will re-make them
  2141. // after appending
  2142. continue;
  2143. } elseif ($end_blocks > 0) {
  2144. for ($i = 0; $i < $end_blocks; $i++) {
  2145. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  2146. }
  2147. $end_blocks = 0;
  2148. }
  2149. $v_binary_data = pack("a512", $v_buffer);
  2150. $this->_writeBlock($v_binary_data);
  2151. }
  2152. @xzclose($v_temp_tar);
  2153. }
  2154. if (!@drupal_unlink($this->_tarname . ".tmp")) {
  2155. $this->_error(
  2156. 'Error while deleting temporary file \''
  2157. . $this->_tarname . '.tmp\''
  2158. );
  2159. }
  2160. } else {
  2161. // ----- For not compressed tar, just add files before the last
  2162. // one or two 512 bytes block
  2163. if (!$this->_openReadWrite()) {
  2164. return false;
  2165. }
  2166. clearstatcache();
  2167. $v_size = filesize($this->_tarname);
  2168. // We might have zero, one or two end blocks.
  2169. // The standard is two, but we should try to handle
  2170. // other cases.
  2171. fseek($this->_file, $v_size - 1024);
  2172. if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  2173. fseek($this->_file, $v_size - 1024);
  2174. } elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  2175. fseek($this->_file, $v_size - 512);
  2176. }
  2177. }
  2178. return true;
  2179. }
  2180. /**
  2181. * @param $p_filelist
  2182. * @param string $p_add_dir
  2183. * @param string $p_remove_dir
  2184. * @return bool
  2185. */
  2186. public function _append($p_filelist, $p_add_dir = '', $p_remove_dir = '')
  2187. {
  2188. if (!$this->_openAppend()) {
  2189. return false;
  2190. }
  2191. if ($this->_addList($p_filelist, $p_add_dir, $p_remove_dir)) {
  2192. $this->_writeFooter();
  2193. }
  2194. $this->_close();
  2195. return true;
  2196. }
  2197. /**
  2198. * Check if a directory exists and create it (including parent
  2199. * dirs) if not.
  2200. *
  2201. * @param string $p_dir directory to check
  2202. *
  2203. * @return bool true if the directory exists or was created
  2204. */
  2205. public function _dirCheck($p_dir)
  2206. {
  2207. clearstatcache();
  2208. if ((@is_dir($p_dir)) || ($p_dir == '')) {
  2209. return true;
  2210. }
  2211. $p_parent_dir = dirname($p_dir);
  2212. if (($p_parent_dir != $p_dir) &&
  2213. ($p_parent_dir != '') &&
  2214. (!$this->_dirCheck($p_parent_dir))
  2215. ) {
  2216. return false;
  2217. }
  2218. if (!@mkdir($p_dir, 0777)) {
  2219. $this->_error("Unable to create directory '$p_dir'");
  2220. return false;
  2221. }
  2222. return true;
  2223. }
  2224. /**
  2225. * Compress path by changing for example "/dir/foo/../bar" to "/dir/bar",
  2226. * rand emove double slashes.
  2227. *
  2228. * @param string $p_dir path to reduce
  2229. *
  2230. * @return string reduced path
  2231. */
  2232. private function _pathReduction($p_dir)
  2233. {
  2234. $v_result = '';
  2235. // ----- Look for not empty path
  2236. if ($p_dir != '') {
  2237. // ----- Explode path by directory names
  2238. $v_list = explode('/', $p_dir);
  2239. // ----- Study directories from last to first
  2240. for ($i = sizeof($v_list) - 1; $i >= 0; $i--) {
  2241. // ----- Look for current path
  2242. if ($v_list[$i] == ".") {
  2243. // ----- Ignore this directory
  2244. // Should be the first $i=0, but no check is done
  2245. } else {
  2246. if ($v_list[$i] == "..") {
  2247. // ----- Ignore it and ignore the $i-1
  2248. $i--;
  2249. } else {
  2250. if (($v_list[$i] == '')
  2251. && ($i != (sizeof($v_list) - 1))
  2252. && ($i != 0)
  2253. ) {
  2254. // ----- Ignore only the double '//' in path,
  2255. // but not the first and last /
  2256. } else {
  2257. $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? '/'
  2258. . $v_result : '');
  2259. }
  2260. }
  2261. }
  2262. }
  2263. }
  2264. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  2265. $v_result = strtr($v_result, '\\', '/');
  2266. }
  2267. return $v_result;
  2268. }
  2269. /**
  2270. * @param $p_path
  2271. * @param bool $p_remove_disk_letter
  2272. * @return string
  2273. */
  2274. public function _translateWinPath($p_path, $p_remove_disk_letter = true)
  2275. {
  2276. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  2277. // ----- Look for potential disk letter
  2278. if (($p_remove_disk_letter)
  2279. && (($v_position = strpos($p_path, ':')) != false)
  2280. ) {
  2281. $p_path = substr($p_path, $v_position + 1);
  2282. }
  2283. // ----- Change potential windows directory separator
  2284. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
  2285. $p_path = strtr($p_path, '\\', '/');
  2286. }
  2287. }
  2288. return $p_path;
  2289. }
  2290. }