system.tar.inc 83 KB

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