Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tar.php 65KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  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. require_once __DIR__ . "/../PEAR.php";
  41. define('ARCHIVE_TAR_ATT_SEPARATOR', 90001);
  42. define('ARCHIVE_TAR_END_BLOCK', pack("a512", ''));
  43. /**
  44. * Creates a (compressed) Tar archive
  45. *
  46. * @package Archive_Tar
  47. * @author Vincent Blavet <vincent@phpconcept.net>
  48. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  49. * @version $Revision$
  50. */
  51. class Archive_Tar extends PEAR
  52. {
  53. /**
  54. * @var string Name of the Tar
  55. */
  56. var $_tarname='';
  57. /**
  58. * @var boolean if true, the Tar file will be gzipped
  59. */
  60. var $_compress=false;
  61. /**
  62. * @var string Type of compression : 'none', 'gz' or 'bz2'
  63. */
  64. var $_compress_type='none';
  65. /**
  66. * @var string Explode separator
  67. */
  68. var $_separator=' ';
  69. /**
  70. * @var file descriptor
  71. */
  72. var $_file=0;
  73. /**
  74. * @var string Local Tar name of a remote Tar (http:// or ftp://)
  75. */
  76. var $_temp_tarname='';
  77. /**
  78. * @var string regular expression for ignoring files or directories
  79. */
  80. var $_ignore_regexp='';
  81. /**
  82. * @var object PEAR_Error object
  83. */
  84. var $error_object=null;
  85. // {{{ constructor
  86. /**
  87. * Archive_Tar Class constructor. This flavour of the constructor only
  88. * declare a new Archive_Tar object, identifying it by the name of the
  89. * tar file.
  90. * If the compress argument is set the tar will be read or created as a
  91. * gzip or bz2 compressed TAR file.
  92. *
  93. * @param string $p_tarname The name of the tar archive to create
  94. * @param string $p_compress can be null, 'gz' or 'bz2'. This
  95. * parameter indicates if gzip or bz2 compression
  96. * is required. For compatibility reason the
  97. * boolean value 'true' means 'gz'.
  98. *
  99. * @access public
  100. */
  101. function Archive_Tar($p_tarname, $p_compress = null)
  102. {
  103. $this->PEAR();
  104. $this->_compress = false;
  105. $this->_compress_type = 'none';
  106. if (($p_compress === null) || ($p_compress == '')) {
  107. if (@file_exists($p_tarname)) {
  108. if ($fp = @fopen($p_tarname, "rb")) {
  109. // look for gzip magic cookie
  110. $data = fread($fp, 2);
  111. fclose($fp);
  112. if ($data == "\37\213") {
  113. $this->_compress = true;
  114. $this->_compress_type = 'gz';
  115. // No sure it's enought for a magic code ....
  116. } elseif ($data == "BZ") {
  117. $this->_compress = true;
  118. $this->_compress_type = 'bz2';
  119. }
  120. }
  121. } else {
  122. // probably a remote file or some file accessible
  123. // through a stream interface
  124. if (substr($p_tarname, -2) == 'gz') {
  125. $this->_compress = true;
  126. $this->_compress_type = 'gz';
  127. } elseif ((substr($p_tarname, -3) == 'bz2') ||
  128. (substr($p_tarname, -2) == 'bz')) {
  129. $this->_compress = true;
  130. $this->_compress_type = 'bz2';
  131. }
  132. }
  133. } else {
  134. if (($p_compress === true) || ($p_compress == 'gz')) {
  135. $this->_compress = true;
  136. $this->_compress_type = 'gz';
  137. } else if ($p_compress == 'bz2') {
  138. $this->_compress = true;
  139. $this->_compress_type = 'bz2';
  140. } else {
  141. $this->_error("Unsupported compression type '$p_compress'\n".
  142. "Supported types are 'gz' and 'bz2'.\n");
  143. return false;
  144. }
  145. }
  146. $this->_tarname = $p_tarname;
  147. if ($this->_compress) { // assert zlib or bz2 extension support
  148. if ($this->_compress_type == 'gz')
  149. $extname = 'zlib';
  150. else if ($this->_compress_type == 'bz2')
  151. $extname = 'bz2';
  152. if (!extension_loaded($extname)) {
  153. PEAR::loadExtension($extname);
  154. }
  155. if (!extension_loaded($extname)) {
  156. $this->_error("The extension '$extname' couldn't be found.\n".
  157. "Please make sure your version of PHP was built ".
  158. "with '$extname' support.\n");
  159. return false;
  160. }
  161. }
  162. }
  163. // }}}
  164. // {{{ destructor
  165. function _Archive_Tar()
  166. {
  167. $this->_close();
  168. // ----- Look for a local copy to delete
  169. if ($this->_temp_tarname != '')
  170. @unlink($this->_temp_tarname);
  171. $this->_PEAR();
  172. }
  173. // }}}
  174. // {{{ create()
  175. /**
  176. * This method creates the archive file and add the files / directories
  177. * that are listed in $p_filelist.
  178. * If a file with the same name exist and is writable, it is replaced
  179. * by the new tar.
  180. * The method return false and a PEAR error text.
  181. * The $p_filelist parameter can be an array of string, each string
  182. * representing a filename or a directory name with their path if
  183. * needed. It can also be a single string with names separated by a
  184. * single blank.
  185. * For each directory added in the archive, the files and
  186. * sub-directories are also added.
  187. * See also createModify() method for more details.
  188. *
  189. * @param array $p_filelist An array of filenames and directory names, or a
  190. * single string with names separated by a single
  191. * blank space.
  192. *
  193. * @return true on success, false on error.
  194. * @see createModify()
  195. * @access public
  196. */
  197. function create($p_filelist)
  198. {
  199. return $this->createModify($p_filelist, '', '');
  200. }
  201. // }}}
  202. // {{{ add()
  203. /**
  204. * This method add the files / directories that are listed in $p_filelist in
  205. * the archive. If the archive does not exist it is created.
  206. * The method return false and a PEAR error text.
  207. * The files and directories listed are only added at the end of the archive,
  208. * even if a file with the same name is already archived.
  209. * See also createModify() method for more details.
  210. *
  211. * @param array $p_filelist An array of filenames and directory names, or a
  212. * single string with names separated by a single
  213. * blank space.
  214. *
  215. * @return true on success, false on error.
  216. * @see createModify()
  217. * @access public
  218. */
  219. function add($p_filelist)
  220. {
  221. return $this->addModify($p_filelist, '', '');
  222. }
  223. // }}}
  224. // {{{ extract()
  225. function extract($p_path='', $p_preserve=false)
  226. {
  227. return $this->extractModify($p_path, '', $p_preserve);
  228. }
  229. // }}}
  230. // {{{ listContent()
  231. function listContent()
  232. {
  233. $v_list_detail = array();
  234. if ($this->_openRead()) {
  235. if (!$this->_extractList('', $v_list_detail, "list", '', '')) {
  236. unset($v_list_detail);
  237. $v_list_detail = 0;
  238. }
  239. $this->_close();
  240. }
  241. return $v_list_detail;
  242. }
  243. // }}}
  244. // {{{ createModify()
  245. /**
  246. * This method creates the archive file and add the files / directories
  247. * that are listed in $p_filelist.
  248. * If the file already exists and is writable, it is replaced by the
  249. * new tar. It is a create and not an add. If the file exists and is
  250. * read-only or is a directory it is not replaced. The method return
  251. * false and a PEAR error text.
  252. * The $p_filelist parameter can be an array of string, each string
  253. * representing a filename or a directory name with their path if
  254. * needed. It can also be a single string with names separated by a
  255. * single blank.
  256. * The path indicated in $p_remove_dir will be removed from the
  257. * memorized path of each file / directory listed when this path
  258. * exists. By default nothing is removed (empty path '')
  259. * The path indicated in $p_add_dir will be added at the beginning of
  260. * the memorized path of each file / directory listed. However it can
  261. * be set to empty ''. The adding of a path is done after the removing
  262. * of path.
  263. * The path add/remove ability enables the user to prepare an archive
  264. * for extraction in a different path than the origin files are.
  265. * See also addModify() method for file adding properties.
  266. *
  267. * @param array $p_filelist An array of filenames and directory names,
  268. * or a single string with names separated by
  269. * a single blank space.
  270. * @param string $p_add_dir A string which contains a path to be added
  271. * to the memorized path of each element in
  272. * the list.
  273. * @param string $p_remove_dir A string which contains a path to be
  274. * removed from the memorized path of each
  275. * element in the list, when relevant.
  276. *
  277. * @return boolean true on success, false on error.
  278. * @access public
  279. * @see addModify()
  280. */
  281. function createModify($p_filelist, $p_add_dir, $p_remove_dir='')
  282. {
  283. $v_result = true;
  284. if (!$this->_openWrite())
  285. return false;
  286. if ($p_filelist != '') {
  287. if (is_array($p_filelist))
  288. $v_list = $p_filelist;
  289. elseif (is_string($p_filelist))
  290. $v_list = explode($this->_separator, $p_filelist);
  291. else {
  292. $this->_cleanFile();
  293. $this->_error('Invalid file list');
  294. return false;
  295. }
  296. $v_result = $this->_addList($v_list, $p_add_dir, $p_remove_dir);
  297. }
  298. if ($v_result) {
  299. $this->_writeFooter();
  300. $this->_close();
  301. } else
  302. $this->_cleanFile();
  303. return $v_result;
  304. }
  305. // }}}
  306. // {{{ addModify()
  307. /**
  308. * This method add the files / directories listed in $p_filelist at the
  309. * end of the existing archive. If the archive does not yet exists it
  310. * is created.
  311. * The $p_filelist parameter can be an array of string, each string
  312. * representing a filename or a directory name with their path if
  313. * needed. It can also be a single string with names separated by a
  314. * single blank.
  315. * The path indicated in $p_remove_dir will be removed from the
  316. * memorized path of each file / directory listed when this path
  317. * exists. By default nothing is removed (empty path '')
  318. * The path indicated in $p_add_dir will be added at the beginning of
  319. * the memorized path of each file / directory listed. However it can
  320. * be set to empty ''. The adding of a path is done after the removing
  321. * of path.
  322. * The path add/remove ability enables the user to prepare an archive
  323. * for extraction in a different path than the origin files are.
  324. * If a file/dir is already in the archive it will only be added at the
  325. * end of the archive. There is no update of the existing archived
  326. * file/dir. However while extracting the archive, the last file will
  327. * replace the first one. This results in a none optimization of the
  328. * archive size.
  329. * If a file/dir does not exist the file/dir is ignored. However an
  330. * error text is send to PEAR error.
  331. * If a file/dir is not readable the file/dir is ignored. However an
  332. * error text is send to PEAR error.
  333. *
  334. * @param array $p_filelist An array of filenames and directory
  335. * names, or a single string with names
  336. * separated by a single blank space.
  337. * @param string $p_add_dir A string which contains a path to be
  338. * added to the memorized path of each
  339. * element in the list.
  340. * @param string $p_remove_dir A string which contains a path to be
  341. * removed from the memorized path of
  342. * each element in the list, when
  343. * relevant.
  344. *
  345. * @return true on success, false on error.
  346. * @access public
  347. */
  348. function addModify($p_filelist, $p_add_dir, $p_remove_dir='')
  349. {
  350. $v_result = true;
  351. if (!$this->_isArchive())
  352. $v_result = $this->createModify($p_filelist, $p_add_dir,
  353. $p_remove_dir);
  354. else {
  355. if (is_array($p_filelist))
  356. $v_list = $p_filelist;
  357. elseif (is_string($p_filelist))
  358. $v_list = explode($this->_separator, $p_filelist);
  359. else {
  360. $this->_error('Invalid file list');
  361. return false;
  362. }
  363. $v_result = $this->_append($v_list, $p_add_dir, $p_remove_dir);
  364. }
  365. return $v_result;
  366. }
  367. // }}}
  368. // {{{ addString()
  369. /**
  370. * This method add a single string as a file at the
  371. * end of the existing archive. If the archive does not yet exists it
  372. * is created.
  373. *
  374. * @param string $p_filename A string which contains the full
  375. * filename path that will be associated
  376. * with the string.
  377. * @param string $p_string The content of the file added in
  378. * the archive.
  379. * @param int $p_datetime A custom date/time (unix timestamp)
  380. * for the file (optional).
  381. *
  382. * @return true on success, false on error.
  383. * @access public
  384. */
  385. function addString($p_filename, $p_string, $p_datetime = false)
  386. {
  387. $v_result = true;
  388. if (!$this->_isArchive()) {
  389. if (!$this->_openWrite()) {
  390. return false;
  391. }
  392. $this->_close();
  393. }
  394. if (!$this->_openAppend())
  395. return false;
  396. // Need to check the get back to the temporary file ? ....
  397. $v_result = $this->_addString($p_filename, $p_string, $p_datetime);
  398. $this->_writeFooter();
  399. $this->_close();
  400. return $v_result;
  401. }
  402. // }}}
  403. // {{{ extractModify()
  404. /**
  405. * This method extract all the content of the archive in the directory
  406. * indicated by $p_path. When relevant the memorized path of the
  407. * files/dir can be modified by removing the $p_remove_path path at the
  408. * beginning of the file/dir path.
  409. * While extracting a file, if the directory path does not exists it is
  410. * created.
  411. * While extracting a file, if the file already exists it is replaced
  412. * without looking for last modification date.
  413. * While extracting a file, if the file already exists and is write
  414. * protected, the extraction is aborted.
  415. * While extracting a file, if a directory with the same name already
  416. * exists, the extraction is aborted.
  417. * While extracting a directory, if a file with the same name already
  418. * exists, the extraction is aborted.
  419. * While extracting a file/directory if the destination directory exist
  420. * and is write protected, or does not exist but can not be created,
  421. * the extraction is aborted.
  422. * If after extraction an extracted file does not show the correct
  423. * stored file size, the extraction is aborted.
  424. * When the extraction is aborted, a PEAR error text is set and false
  425. * is returned. However the result can be a partial extraction that may
  426. * need to be manually cleaned.
  427. *
  428. * @param string $p_path The path of the directory where the
  429. * files/dir need to by extracted.
  430. * @param string $p_remove_path Part of the memorized path that can be
  431. * removed if present at the beginning of
  432. * the file/dir path.
  433. * @param boolean $p_preserve Preserve user/group ownership of files
  434. *
  435. * @return boolean true on success, false on error.
  436. * @access public
  437. * @see extractList()
  438. */
  439. function extractModify($p_path, $p_remove_path, $p_preserve=false)
  440. {
  441. $v_result = true;
  442. $v_list_detail = array();
  443. if ($v_result = $this->_openRead()) {
  444. $v_result = $this->_extractList($p_path, $v_list_detail,
  445. "complete", 0, $p_remove_path, $p_preserve);
  446. $this->_close();
  447. }
  448. return $v_result;
  449. }
  450. // }}}
  451. // {{{ extractInString()
  452. /**
  453. * This method extract from the archive one file identified by $p_filename.
  454. * The return value is a string with the file content, or NULL on error.
  455. *
  456. * @param string $p_filename The path of the file to extract in a string.
  457. *
  458. * @return a string with the file content or NULL.
  459. * @access public
  460. */
  461. function extractInString($p_filename)
  462. {
  463. if ($this->_openRead()) {
  464. $v_result = $this->_extractInString($p_filename);
  465. $this->_close();
  466. } else {
  467. $v_result = null;
  468. }
  469. return $v_result;
  470. }
  471. // }}}
  472. // {{{ extractList()
  473. /**
  474. * This method extract from the archive only the files indicated in the
  475. * $p_filelist. These files are extracted in the current directory or
  476. * in the directory indicated by the optional $p_path parameter.
  477. * If indicated the $p_remove_path can be used in the same way as it is
  478. * used in extractModify() method.
  479. *
  480. * @param array $p_filelist An array of filenames and directory names,
  481. * or a single string with names separated
  482. * by a single blank space.
  483. * @param string $p_path The path of the directory where the
  484. * files/dir need to by extracted.
  485. * @param string $p_remove_path Part of the memorized path that can be
  486. * removed if present at the beginning of
  487. * the file/dir path.
  488. * @param boolean $p_preserve Preserve user/group ownership of files
  489. *
  490. * @return true on success, false on error.
  491. * @access public
  492. * @see extractModify()
  493. */
  494. function extractList($p_filelist, $p_path='', $p_remove_path='', $p_preserve=false)
  495. {
  496. $v_result = true;
  497. $v_list_detail = array();
  498. if (is_array($p_filelist))
  499. $v_list = $p_filelist;
  500. elseif (is_string($p_filelist))
  501. $v_list = explode($this->_separator, $p_filelist);
  502. else {
  503. $this->_error('Invalid string list');
  504. return false;
  505. }
  506. if ($v_result = $this->_openRead()) {
  507. $v_result = $this->_extractList($p_path, $v_list_detail, "partial",
  508. $v_list, $p_remove_path, $p_preserve);
  509. $this->_close();
  510. }
  511. return $v_result;
  512. }
  513. // }}}
  514. // {{{ setAttribute()
  515. /**
  516. * This method set specific attributes of the archive. It uses a variable
  517. * list of parameters, in the format attribute code + attribute values :
  518. * $arch->setAttribute(ARCHIVE_TAR_ATT_SEPARATOR, ',');
  519. *
  520. * @param mixed $argv variable list of attributes and values
  521. *
  522. * @return true on success, false on error.
  523. * @access public
  524. */
  525. function setAttribute()
  526. {
  527. $v_result = true;
  528. // ----- Get the number of variable list of arguments
  529. if (($v_size = func_num_args()) == 0) {
  530. return true;
  531. }
  532. // ----- Get the arguments
  533. $v_att_list = &func_get_args();
  534. // ----- Read the attributes
  535. $i=0;
  536. while ($i<$v_size) {
  537. // ----- Look for next option
  538. switch ($v_att_list[$i]) {
  539. // ----- Look for options that request a string value
  540. case ARCHIVE_TAR_ATT_SEPARATOR :
  541. // ----- Check the number of parameters
  542. if (($i+1) >= $v_size) {
  543. $this->_error('Invalid number of parameters for '
  544. .'attribute ARCHIVE_TAR_ATT_SEPARATOR');
  545. return false;
  546. }
  547. // ----- Get the value
  548. $this->_separator = $v_att_list[$i+1];
  549. $i++;
  550. break;
  551. default :
  552. $this->_error('Unknow attribute code '.$v_att_list[$i].'');
  553. return false;
  554. }
  555. // ----- Next attribute
  556. $i++;
  557. }
  558. return $v_result;
  559. }
  560. // }}}
  561. // {{{ setIgnoreRegexp()
  562. /**
  563. * This method sets the regular expression for ignoring files and directories
  564. * at import, for example:
  565. * $arch->setIgnoreRegexp("#CVS|\.svn#");
  566. *
  567. * @param string $regexp regular expression defining which files or directories to ignore
  568. *
  569. * @access public
  570. */
  571. function setIgnoreRegexp($regexp)
  572. {
  573. $this->_ignore_regexp = $regexp;
  574. }
  575. // }}}
  576. // {{{ setIgnoreList()
  577. /**
  578. * This method sets the regular expression for ignoring all files and directories
  579. * matching the filenames in the array list at import, for example:
  580. * $arch->setIgnoreList(array('CVS', '.svn', 'bin/tool'));
  581. *
  582. * @param array $list a list of file or directory names to ignore
  583. *
  584. * @access public
  585. */
  586. function setIgnoreList($list)
  587. {
  588. $regexp = str_replace(array('#', '.', '^', '$'), array('\#', '\.', '\^', '\$'), $list);
  589. $regexp = '#/'.join('$|/', $list).'#';
  590. $this->setIgnoreRegexp($regexp);
  591. }
  592. // }}}
  593. // {{{ _error()
  594. function _error($p_message)
  595. {
  596. $this->error_object = &$this->raiseError($p_message);
  597. }
  598. // }}}
  599. // {{{ _warning()
  600. function _warning($p_message)
  601. {
  602. $this->error_object = &$this->raiseError($p_message);
  603. }
  604. // }}}
  605. // {{{ _isArchive()
  606. function _isArchive($p_filename=null)
  607. {
  608. if ($p_filename == null) {
  609. $p_filename = $this->_tarname;
  610. }
  611. clearstatcache();
  612. return @is_file($p_filename) && !@is_link($p_filename);
  613. }
  614. // }}}
  615. // {{{ _openWrite()
  616. function _openWrite()
  617. {
  618. if ($this->_compress_type == 'gz' && function_exists('gzopen'))
  619. $this->_file = @gzopen($this->_tarname, "wb9");
  620. else if ($this->_compress_type == 'bz2' && function_exists('bzopen'))
  621. $this->_file = @bzopen($this->_tarname, "w");
  622. else if ($this->_compress_type == 'none')
  623. $this->_file = @fopen($this->_tarname, "wb");
  624. else {
  625. $this->_error('Unknown or missing compression type ('
  626. .$this->_compress_type.')');
  627. return false;
  628. }
  629. if ($this->_file == 0) {
  630. $this->_error('Unable to open in write mode \''
  631. .$this->_tarname.'\'');
  632. return false;
  633. }
  634. return true;
  635. }
  636. // }}}
  637. // {{{ _openRead()
  638. function _openRead()
  639. {
  640. if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') {
  641. // ----- Look if a local copy need to be done
  642. if ($this->_temp_tarname == '') {
  643. $this->_temp_tarname = uniqid('tar').'.tmp';
  644. if (!$v_file_from = @fopen($this->_tarname, 'rb')) {
  645. $this->_error('Unable to open in read mode \''
  646. .$this->_tarname.'\'');
  647. $this->_temp_tarname = '';
  648. return false;
  649. }
  650. if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) {
  651. $this->_error('Unable to open in write mode \''
  652. .$this->_temp_tarname.'\'');
  653. $this->_temp_tarname = '';
  654. return false;
  655. }
  656. while ($v_data = @fread($v_file_from, 1024))
  657. @fwrite($v_file_to, $v_data);
  658. @fclose($v_file_from);
  659. @fclose($v_file_to);
  660. }
  661. // ----- File to open if the local copy
  662. $v_filename = $this->_temp_tarname;
  663. } else
  664. // ----- File to open if the normal Tar file
  665. $v_filename = $this->_tarname;
  666. if ($this->_compress_type == 'gz' && function_exists('gzopen'))
  667. $this->_file = @gzopen($v_filename, "rb");
  668. else if ($this->_compress_type == 'bz2' && function_exists('bzopen'))
  669. $this->_file = @bzopen($v_filename, "r");
  670. else if ($this->_compress_type == 'none')
  671. $this->_file = @fopen($v_filename, "rb");
  672. else {
  673. $this->_error('Unknown or missing compression type ('
  674. .$this->_compress_type.')');
  675. return false;
  676. }
  677. if ($this->_file == 0) {
  678. $this->_error('Unable to open in read mode \''.$v_filename.'\'');
  679. return false;
  680. }
  681. return true;
  682. }
  683. // }}}
  684. // {{{ _openReadWrite()
  685. function _openReadWrite()
  686. {
  687. if ($this->_compress_type == 'gz')
  688. $this->_file = @gzopen($this->_tarname, "r+b");
  689. else if ($this->_compress_type == 'bz2') {
  690. $this->_error('Unable to open bz2 in read/write mode \''
  691. .$this->_tarname.'\' (limitation of bz2 extension)');
  692. return false;
  693. } else if ($this->_compress_type == 'none')
  694. $this->_file = @fopen($this->_tarname, "r+b");
  695. else {
  696. $this->_error('Unknown or missing compression type ('
  697. .$this->_compress_type.')');
  698. return false;
  699. }
  700. if ($this->_file == 0) {
  701. $this->_error('Unable to open in read/write mode \''
  702. .$this->_tarname.'\'');
  703. return false;
  704. }
  705. return true;
  706. }
  707. // }}}
  708. // {{{ _close()
  709. function _close()
  710. {
  711. //if (isset($this->_file)) {
  712. if (is_resource($this->_file)) {
  713. if ($this->_compress_type == 'gz')
  714. @gzclose($this->_file);
  715. else if ($this->_compress_type == 'bz2')
  716. @bzclose($this->_file);
  717. else if ($this->_compress_type == 'none')
  718. @fclose($this->_file);
  719. else
  720. $this->_error('Unknown or missing compression type ('
  721. .$this->_compress_type.')');
  722. $this->_file = 0;
  723. }
  724. // ----- Look if a local copy need to be erase
  725. // Note that it might be interesting to keep the url for a time : ToDo
  726. if ($this->_temp_tarname != '') {
  727. @unlink($this->_temp_tarname);
  728. $this->_temp_tarname = '';
  729. }
  730. return true;
  731. }
  732. // }}}
  733. // {{{ _cleanFile()
  734. function _cleanFile()
  735. {
  736. $this->_close();
  737. // ----- Look for a local copy
  738. if ($this->_temp_tarname != '') {
  739. // ----- Remove the local copy but not the remote tarname
  740. @unlink($this->_temp_tarname);
  741. $this->_temp_tarname = '';
  742. } else {
  743. // ----- Remove the local tarname file
  744. @unlink($this->_tarname);
  745. }
  746. $this->_tarname = '';
  747. return true;
  748. }
  749. // }}}
  750. // {{{ _writeBlock()
  751. function _writeBlock($p_binary_data, $p_len=null)
  752. {
  753. if (is_resource($this->_file)) {
  754. if ($p_len === null) {
  755. if ($this->_compress_type == 'gz')
  756. @gzputs($this->_file, $p_binary_data);
  757. else if ($this->_compress_type == 'bz2')
  758. @bzwrite($this->_file, $p_binary_data);
  759. else if ($this->_compress_type == 'none')
  760. @fputs($this->_file, $p_binary_data);
  761. else
  762. $this->_error('Unknown or missing compression type ('
  763. .$this->_compress_type.')');
  764. } else {
  765. if ($this->_compress_type == 'gz')
  766. @gzputs($this->_file, $p_binary_data, $p_len);
  767. else if ($this->_compress_type == 'bz2')
  768. @bzwrite($this->_file, $p_binary_data, $p_len);
  769. else if ($this->_compress_type == 'none')
  770. @fputs($this->_file, $p_binary_data, $p_len);
  771. else
  772. $this->_error('Unknown or missing compression type ('
  773. .$this->_compress_type.')');
  774. }
  775. }
  776. return true;
  777. }
  778. // }}}
  779. // {{{ _readBlock()
  780. function _readBlock()
  781. {
  782. $v_block = null;
  783. if (is_resource($this->_file)) {
  784. if ($this->_compress_type == 'gz')
  785. $v_block = @gzread($this->_file, 512);
  786. else if ($this->_compress_type == 'bz2')
  787. $v_block = @bzread($this->_file, 512);
  788. else if ($this->_compress_type == 'none')
  789. $v_block = @fread($this->_file, 512);
  790. else
  791. $this->_error('Unknown or missing compression type ('
  792. .$this->_compress_type.')');
  793. }
  794. return $v_block;
  795. }
  796. // }}}
  797. // {{{ _jumpBlock()
  798. function _jumpBlock($p_len=null)
  799. {
  800. if (is_resource($this->_file)) {
  801. if ($p_len === null)
  802. $p_len = 1;
  803. if ($this->_compress_type == 'gz') {
  804. @gzseek($this->_file, gztell($this->_file)+($p_len*512));
  805. }
  806. else if ($this->_compress_type == 'bz2') {
  807. // ----- Replace missing bztell() and bzseek()
  808. for ($i=0; $i<$p_len; $i++)
  809. $this->_readBlock();
  810. } else if ($this->_compress_type == 'none')
  811. @fseek($this->_file, $p_len*512, SEEK_CUR);
  812. else
  813. $this->_error('Unknown or missing compression type ('
  814. .$this->_compress_type.')');
  815. }
  816. return true;
  817. }
  818. // }}}
  819. // {{{ _writeFooter()
  820. function _writeFooter()
  821. {
  822. if (is_resource($this->_file)) {
  823. // ----- Write the last 0 filled block for end of archive
  824. $v_binary_data = pack('a1024', '');
  825. $this->_writeBlock($v_binary_data);
  826. }
  827. return true;
  828. }
  829. // }}}
  830. // {{{ _addList()
  831. function _addList($p_list, $p_add_dir, $p_remove_dir)
  832. {
  833. $v_result=true;
  834. $v_header = array();
  835. // ----- Remove potential windows directory separator
  836. $p_add_dir = $this->_translateWinPath($p_add_dir);
  837. $p_remove_dir = $this->_translateWinPath($p_remove_dir, false);
  838. if (!$this->_file) {
  839. $this->_error('Invalid file descriptor');
  840. return false;
  841. }
  842. if (sizeof($p_list) == 0)
  843. return true;
  844. foreach ($p_list as $v_filename) {
  845. if (!$v_result) {
  846. break;
  847. }
  848. // ----- Skip the current tar name
  849. if ($v_filename == $this->_tarname)
  850. continue;
  851. if ($v_filename == '')
  852. continue;
  853. // ----- ignore files and directories matching the ignore regular expression
  854. if ($this->_ignore_regexp && preg_match($this->_ignore_regexp, '/'.$v_filename)) {
  855. $this->_warning("File '$v_filename' ignored");
  856. continue;
  857. }
  858. if (!file_exists($v_filename) && !is_link($v_filename)) {
  859. $this->_warning("File '$v_filename' does not exist");
  860. continue;
  861. }
  862. // ----- Add the file or directory header
  863. if (!$this->_addFile($v_filename, $v_header, $p_add_dir, $p_remove_dir))
  864. return false;
  865. if (@is_dir($v_filename) && !@is_link($v_filename)) {
  866. if (!($p_hdir = opendir($v_filename))) {
  867. $this->_warning("Directory '$v_filename' can not be read");
  868. continue;
  869. }
  870. while (false !== ($p_hitem = readdir($p_hdir))) {
  871. if (($p_hitem != '.') && ($p_hitem != '..')) {
  872. if ($v_filename != ".")
  873. $p_temp_list[0] = $v_filename.'/'.$p_hitem;
  874. else
  875. $p_temp_list[0] = $p_hitem;
  876. $v_result = $this->_addList($p_temp_list,
  877. $p_add_dir,
  878. $p_remove_dir);
  879. }
  880. }
  881. unset($p_temp_list);
  882. unset($p_hdir);
  883. unset($p_hitem);
  884. }
  885. }
  886. return $v_result;
  887. }
  888. // }}}
  889. // {{{ _addFile()
  890. function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir)
  891. {
  892. if (!$this->_file) {
  893. $this->_error('Invalid file descriptor');
  894. return false;
  895. }
  896. if ($p_filename == '') {
  897. $this->_error('Invalid file name');
  898. return false;
  899. }
  900. // ----- Calculate the stored filename
  901. $p_filename = $this->_translateWinPath($p_filename, false);;
  902. $v_stored_filename = $p_filename;
  903. if (strcmp($p_filename, $p_remove_dir) == 0) {
  904. return true;
  905. }
  906. if ($p_remove_dir != '') {
  907. if (substr($p_remove_dir, -1) != '/')
  908. $p_remove_dir .= '/';
  909. if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
  910. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  911. }
  912. $v_stored_filename = $this->_translateWinPath($v_stored_filename);
  913. if ($p_add_dir != '') {
  914. if (substr($p_add_dir, -1) == '/')
  915. $v_stored_filename = $p_add_dir.$v_stored_filename;
  916. else
  917. $v_stored_filename = $p_add_dir.'/'.$v_stored_filename;
  918. }
  919. $v_stored_filename = $this->_pathReduction($v_stored_filename);
  920. if ($this->_isArchive($p_filename)) {
  921. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  922. $this->_warning("Unable to open file '".$p_filename
  923. ."' in binary read mode");
  924. return true;
  925. }
  926. if (!$this->_writeHeader($p_filename, $v_stored_filename))
  927. return false;
  928. while (($v_buffer = fread($v_file, 512)) != '') {
  929. $v_binary_data = pack("a512", "$v_buffer");
  930. $this->_writeBlock($v_binary_data);
  931. }
  932. fclose($v_file);
  933. } else {
  934. // ----- Only header for dir
  935. if (!$this->_writeHeader($p_filename, $v_stored_filename))
  936. return false;
  937. }
  938. return true;
  939. }
  940. // }}}
  941. // {{{ _addString()
  942. function _addString($p_filename, $p_string, $p_datetime = false)
  943. {
  944. if (!$this->_file) {
  945. $this->_error('Invalid file descriptor');
  946. return false;
  947. }
  948. if ($p_filename == '') {
  949. $this->_error('Invalid file name');
  950. return false;
  951. }
  952. // ----- Calculate the stored filename
  953. $p_filename = $this->_translateWinPath($p_filename, false);;
  954. // ----- If datetime is not specified, set current time
  955. if ($p_datetime === false) {
  956. $p_datetime = time();
  957. }
  958. if (!$this->_writeHeaderBlock($p_filename, strlen($p_string),
  959. $p_datetime, 384, "", 0, 0))
  960. return false;
  961. $i=0;
  962. while (($v_buffer = substr($p_string, (($i++)*512), 512)) != '') {
  963. $v_binary_data = pack("a512", $v_buffer);
  964. $this->_writeBlock($v_binary_data);
  965. }
  966. return true;
  967. }
  968. // }}}
  969. // {{{ _writeHeader()
  970. function _writeHeader($p_filename, $p_stored_filename)
  971. {
  972. if ($p_stored_filename == '')
  973. $p_stored_filename = $p_filename;
  974. $v_reduce_filename = $this->_pathReduction($p_stored_filename);
  975. if (strlen($v_reduce_filename) > 99) {
  976. if (!$this->_writeLongHeader($v_reduce_filename))
  977. return false;
  978. }
  979. $v_info = lstat($p_filename);
  980. $v_uid = sprintf("%07s", DecOct($v_info[4]));
  981. $v_gid = sprintf("%07s", DecOct($v_info[5]));
  982. $v_perms = sprintf("%07s", DecOct($v_info['mode'] & 000777));
  983. $v_mtime = sprintf("%011s", DecOct($v_info['mtime']));
  984. $v_linkname = '';
  985. if (@is_link($p_filename)) {
  986. $v_typeflag = '2';
  987. $v_linkname = readlink($p_filename);
  988. $v_size = sprintf("%011s", DecOct(0));
  989. } elseif (@is_dir($p_filename)) {
  990. $v_typeflag = "5";
  991. $v_size = sprintf("%011s", DecOct(0));
  992. } else {
  993. $v_typeflag = '0';
  994. clearstatcache();
  995. $v_size = sprintf("%011s", DecOct($v_info['size']));
  996. }
  997. $v_magic = 'ustar ';
  998. $v_version = ' ';
  999. if (function_exists('posix_getpwuid'))
  1000. {
  1001. $userinfo = posix_getpwuid($v_info[4]);
  1002. $groupinfo = posix_getgrgid($v_info[5]);
  1003. $v_uname = $userinfo['name'];
  1004. $v_gname = $groupinfo['name'];
  1005. }
  1006. else
  1007. {
  1008. $v_uname = '';
  1009. $v_gname = '';
  1010. }
  1011. $v_devmajor = '';
  1012. $v_devminor = '';
  1013. $v_prefix = '';
  1014. $v_binary_data_first = pack("a100a8a8a8a12a12",
  1015. $v_reduce_filename, $v_perms, $v_uid,
  1016. $v_gid, $v_size, $v_mtime);
  1017. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1018. $v_typeflag, $v_linkname, $v_magic,
  1019. $v_version, $v_uname, $v_gname,
  1020. $v_devmajor, $v_devminor, $v_prefix, '');
  1021. // ----- Calculate the checksum
  1022. $v_checksum = 0;
  1023. // ..... First part of the header
  1024. for ($i=0; $i<148; $i++)
  1025. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1026. // ..... Ignore the checksum value and replace it by ' ' (space)
  1027. for ($i=148; $i<156; $i++)
  1028. $v_checksum += ord(' ');
  1029. // ..... Last part of the header
  1030. for ($i=156, $j=0; $i<512; $i++, $j++)
  1031. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1032. // ----- Write the first 148 bytes of the header in the archive
  1033. $this->_writeBlock($v_binary_data_first, 148);
  1034. // ----- Write the calculated checksum
  1035. $v_checksum = sprintf("%06s ", DecOct($v_checksum));
  1036. $v_binary_data = pack("a8", $v_checksum);
  1037. $this->_writeBlock($v_binary_data, 8);
  1038. // ----- Write the last 356 bytes of the header in the archive
  1039. $this->_writeBlock($v_binary_data_last, 356);
  1040. return true;
  1041. }
  1042. // }}}
  1043. // {{{ _writeHeaderBlock()
  1044. function _writeHeaderBlock($p_filename, $p_size, $p_mtime=0, $p_perms=0,
  1045. $p_type='', $p_uid=0, $p_gid=0)
  1046. {
  1047. $p_filename = $this->_pathReduction($p_filename);
  1048. if (strlen($p_filename) > 99) {
  1049. if (!$this->_writeLongHeader($p_filename))
  1050. return false;
  1051. }
  1052. if ($p_type == "5") {
  1053. $v_size = sprintf("%011s", DecOct(0));
  1054. } else {
  1055. $v_size = sprintf("%011s", DecOct($p_size));
  1056. }
  1057. $v_uid = sprintf("%07s", DecOct($p_uid));
  1058. $v_gid = sprintf("%07s", DecOct($p_gid));
  1059. $v_perms = sprintf("%07s", DecOct($p_perms & 000777));
  1060. $v_mtime = sprintf("%11s", DecOct($p_mtime));
  1061. $v_linkname = '';
  1062. $v_magic = 'ustar ';
  1063. $v_version = ' ';
  1064. if (function_exists('posix_getpwuid'))
  1065. {
  1066. $userinfo = posix_getpwuid($p_uid);
  1067. $groupinfo = posix_getgrgid($p_gid);
  1068. $v_uname = $userinfo['name'];
  1069. $v_gname = $groupinfo['name'];
  1070. }
  1071. else
  1072. {
  1073. $v_uname = '';
  1074. $v_gname = '';
  1075. }
  1076. $v_devmajor = '';
  1077. $v_devminor = '';
  1078. $v_prefix = '';
  1079. $v_binary_data_first = pack("a100a8a8a8a12A12",
  1080. $p_filename, $v_perms, $v_uid, $v_gid,
  1081. $v_size, $v_mtime);
  1082. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1083. $p_type, $v_linkname, $v_magic,
  1084. $v_version, $v_uname, $v_gname,
  1085. $v_devmajor, $v_devminor, $v_prefix, '');
  1086. // ----- Calculate the checksum
  1087. $v_checksum = 0;
  1088. // ..... First part of the header
  1089. for ($i=0; $i<148; $i++)
  1090. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1091. // ..... Ignore the checksum value and replace it by ' ' (space)
  1092. for ($i=148; $i<156; $i++)
  1093. $v_checksum += ord(' ');
  1094. // ..... Last part of the header
  1095. for ($i=156, $j=0; $i<512; $i++, $j++)
  1096. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1097. // ----- Write the first 148 bytes of the header in the archive
  1098. $this->_writeBlock($v_binary_data_first, 148);
  1099. // ----- Write the calculated checksum
  1100. $v_checksum = sprintf("%06s ", DecOct($v_checksum));
  1101. $v_binary_data = pack("a8", $v_checksum);
  1102. $this->_writeBlock($v_binary_data, 8);
  1103. // ----- Write the last 356 bytes of the header in the archive
  1104. $this->_writeBlock($v_binary_data_last, 356);
  1105. return true;
  1106. }
  1107. // }}}
  1108. // {{{ _writeLongHeader()
  1109. function _writeLongHeader($p_filename)
  1110. {
  1111. $v_size = sprintf("%11s ", DecOct(strlen($p_filename)));
  1112. $v_typeflag = 'L';
  1113. $v_linkname = '';
  1114. $v_magic = '';
  1115. $v_version = '';
  1116. $v_uname = '';
  1117. $v_gname = '';
  1118. $v_devmajor = '';
  1119. $v_devminor = '';
  1120. $v_prefix = '';
  1121. $v_binary_data_first = pack("a100a8a8a8a12a12",
  1122. '././@LongLink', 0, 0, 0, $v_size, 0);
  1123. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1124. $v_typeflag, $v_linkname, $v_magic,
  1125. $v_version, $v_uname, $v_gname,
  1126. $v_devmajor, $v_devminor, $v_prefix, '');
  1127. // ----- Calculate the checksum
  1128. $v_checksum = 0;
  1129. // ..... First part of the header
  1130. for ($i=0; $i<148; $i++)
  1131. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1132. // ..... Ignore the checksum value and replace it by ' ' (space)
  1133. for ($i=148; $i<156; $i++)
  1134. $v_checksum += ord(' ');
  1135. // ..... Last part of the header
  1136. for ($i=156, $j=0; $i<512; $i++, $j++)
  1137. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1138. // ----- Write the first 148 bytes of the header in the archive
  1139. $this->_writeBlock($v_binary_data_first, 148);
  1140. // ----- Write the calculated checksum
  1141. $v_checksum = sprintf("%06s ", DecOct($v_checksum));
  1142. $v_binary_data = pack("a8", $v_checksum);
  1143. $this->_writeBlock($v_binary_data, 8);
  1144. // ----- Write the last 356 bytes of the header in the archive
  1145. $this->_writeBlock($v_binary_data_last, 356);
  1146. // ----- Write the filename as content of the block
  1147. $i=0;
  1148. while (($v_buffer = substr($p_filename, (($i++)*512), 512)) != '') {
  1149. $v_binary_data = pack("a512", "$v_buffer");
  1150. $this->_writeBlock($v_binary_data);
  1151. }
  1152. return true;
  1153. }
  1154. // }}}
  1155. // {{{ _readHeader()
  1156. function _readHeader($v_binary_data, &$v_header)
  1157. {
  1158. if (strlen($v_binary_data)==0) {
  1159. $v_header['filename'] = '';
  1160. return true;
  1161. }
  1162. if (strlen($v_binary_data) != 512) {
  1163. $v_header['filename'] = '';
  1164. $this->_error('Invalid block size : '.strlen($v_binary_data));
  1165. return false;
  1166. }
  1167. if (!is_array($v_header)) {
  1168. $v_header = array();
  1169. }
  1170. // ----- Calculate the checksum
  1171. $v_checksum = 0;
  1172. // ..... First part of the header
  1173. for ($i=0; $i<148; $i++)
  1174. $v_checksum+=ord(substr($v_binary_data,$i,1));
  1175. // ..... Ignore the checksum value and replace it by ' ' (space)
  1176. for ($i=148; $i<156; $i++)
  1177. $v_checksum += ord(' ');
  1178. // ..... Last part of the header
  1179. for ($i=156; $i<512; $i++)
  1180. $v_checksum+=ord(substr($v_binary_data,$i,1));
  1181. if (version_compare(PHP_VERSION,"5.5.0-dev")<0) {
  1182. $fmt = "a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" .
  1183. "a8checksum/a1typeflag/a100link/a6magic/a2version/" .
  1184. "a32uname/a32gname/a8devmajor/a8devminor/a131prefix";
  1185. } else {
  1186. $fmt = "Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/" .
  1187. "Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/" .
  1188. "Z32uname/Z32gname/Z8devmajor/Z8devminor/Z131prefix";
  1189. }
  1190. $v_data = unpack($fmt, $v_binary_data);
  1191. if (strlen($v_data["prefix"]) > 0) {
  1192. $v_data["filename"] = "$v_data[prefix]/$v_data[filename]";
  1193. }
  1194. // ----- Extract the checksum
  1195. $v_header['checksum'] = OctDec(trim($v_data['checksum']));
  1196. if ($v_header['checksum'] != $v_checksum) {
  1197. $v_header['filename'] = '';
  1198. // ----- Look for last block (empty block)
  1199. if (($v_checksum == 256) && ($v_header['checksum'] == 0))
  1200. return true;
  1201. $this->_error('Invalid checksum for file "'.$v_data['filename']
  1202. .'" : '.$v_checksum.' calculated, '
  1203. .$v_header['checksum'].' expected');
  1204. return false;
  1205. }
  1206. // ----- Extract the properties
  1207. $v_header['filename'] = $v_data['filename'];
  1208. if ($this->_maliciousFilename($v_header['filename'])) {
  1209. $this->_error('Malicious .tar detected, file "' . $v_header['filename'] .
  1210. '" will not install in desired directory tree');
  1211. return false;
  1212. }
  1213. $v_header['mode'] = OctDec(trim($v_data['mode']));
  1214. $v_header['uid'] = OctDec(trim($v_data['uid']));
  1215. $v_header['gid'] = OctDec(trim($v_data['gid']));
  1216. $v_header['size'] = OctDec(trim($v_data['size']));
  1217. $v_header['mtime'] = OctDec(trim($v_data['mtime']));
  1218. if (($v_header['typeflag'] = $v_data['typeflag']) == "5") {
  1219. $v_header['size'] = 0;
  1220. }
  1221. $v_header['link'] = trim($v_data['link']);
  1222. /* ----- All these fields are removed form the header because
  1223. they do not carry interesting info
  1224. $v_header[magic] = trim($v_data[magic]);
  1225. $v_header[version] = trim($v_data[version]);
  1226. $v_header[uname] = trim($v_data[uname]);
  1227. $v_header[gname] = trim($v_data[gname]);
  1228. $v_header[devmajor] = trim($v_data[devmajor]);
  1229. $v_header[devminor] = trim($v_data[devminor]);
  1230. */
  1231. return true;
  1232. }
  1233. // }}}
  1234. // {{{ _maliciousFilename()
  1235. /**
  1236. * Detect and report a malicious file name
  1237. *
  1238. * @param string $file
  1239. *
  1240. * @return bool
  1241. * @access private
  1242. */
  1243. function _maliciousFilename($file)
  1244. {
  1245. if (strpos($file, '/../') !== false) {
  1246. return true;
  1247. }
  1248. if (strpos($file, '../') === 0) {
  1249. return true;
  1250. }
  1251. return false;
  1252. }
  1253. // }}}
  1254. // {{{ _readLongHeader()
  1255. function _readLongHeader(&$v_header)
  1256. {
  1257. $v_filename = '';
  1258. $n = floor($v_header['size']/512);
  1259. for ($i=0; $i<$n; $i++) {
  1260. $v_content = $this->_readBlock();
  1261. $v_filename .= $v_content;
  1262. }
  1263. if (($v_header['size'] % 512) != 0) {
  1264. $v_content = $this->_readBlock();
  1265. $v_filename .= trim($v_content);
  1266. }
  1267. // ----- Read the next header
  1268. $v_binary_data = $this->_readBlock();
  1269. if (!$this->_readHeader($v_binary_data, $v_header))
  1270. return false;
  1271. $v_filename = trim($v_filename);
  1272. $v_header['filename'] = $v_filename;
  1273. if ($this->_maliciousFilename($v_filename)) {
  1274. $this->_error('Malicious .tar detected, file "' . $v_filename .
  1275. '" will not install in desired directory tree');
  1276. return false;
  1277. }
  1278. return true;
  1279. }
  1280. // }}}
  1281. // {{{ _extractInString()
  1282. /**
  1283. * This method extract from the archive one file identified by $p_filename.
  1284. * The return value is a string with the file content, or null on error.
  1285. *
  1286. * @param string $p_filename The path of the file to extract in a string.
  1287. *
  1288. * @return a string with the file content or null.
  1289. * @access private
  1290. */
  1291. function _extractInString($p_filename)
  1292. {
  1293. $v_result_str = "";
  1294. While (strlen($v_binary_data = $this->_readBlock()) != 0)
  1295. {
  1296. if (!$this->_readHeader($v_binary_data, $v_header))
  1297. return null;
  1298. if ($v_header['filename'] == '')
  1299. continue;
  1300. // ----- Look for long filename
  1301. if ($v_header['typeflag'] == 'L') {
  1302. if (!$this->_readLongHeader($v_header))
  1303. return null;
  1304. }
  1305. if ($v_header['filename'] == $p_filename) {
  1306. if ($v_header['typeflag'] == "5") {
  1307. $this->_error('Unable to extract in string a directory '
  1308. .'entry {'.$v_header['filename'].'}');
  1309. return null;
  1310. } else {
  1311. $n = floor($v_header['size']/512);
  1312. for ($i=0; $i<$n; $i++) {
  1313. $v_result_str .= $this->_readBlock();
  1314. }
  1315. if (($v_header['size'] % 512) != 0) {
  1316. $v_content = $this->_readBlock();
  1317. $v_result_str .= substr($v_content, 0,
  1318. ($v_header['size'] % 512));
  1319. }
  1320. return $v_result_str;
  1321. }
  1322. } else {
  1323. $this->_jumpBlock(ceil(($v_header['size']/512)));
  1324. }
  1325. }
  1326. return null;
  1327. }
  1328. // }}}
  1329. // {{{ _extractList()
  1330. function _extractList($p_path, &$p_list_detail, $p_mode,
  1331. $p_file_list, $p_remove_path, $p_preserve=false)
  1332. {
  1333. $v_result=true;
  1334. $v_nb = 0;
  1335. $v_extract_all = true;
  1336. $v_listing = false;
  1337. $p_path = $this->_translateWinPath($p_path, false);
  1338. if ($p_path == '' || (substr($p_path, 0, 1) != '/'
  1339. && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))) {
  1340. $p_path = "./".$p_path;
  1341. }
  1342. $p_remove_path = $this->_translateWinPath($p_remove_path);
  1343. // ----- Look for path to remove format (should end by /)
  1344. if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/'))
  1345. $p_remove_path .= '/';
  1346. $p_remove_path_size = strlen($p_remove_path);
  1347. switch ($p_mode) {
  1348. case "complete" :
  1349. $v_extract_all = true;
  1350. $v_listing = false;
  1351. break;
  1352. case "partial" :
  1353. $v_extract_all = false;
  1354. $v_listing = false;
  1355. break;
  1356. case "list" :
  1357. $v_extract_all = false;
  1358. $v_listing = true;
  1359. break;
  1360. default :
  1361. $this->_error('Invalid extract mode ('.$p_mode.')');
  1362. return false;
  1363. }
  1364. clearstatcache();
  1365. while (strlen($v_binary_data = $this->_readBlock()) != 0)
  1366. {
  1367. $v_extract_file = FALSE;
  1368. $v_extraction_stopped = 0;
  1369. if (!$this->_readHeader($v_binary_data, $v_header))
  1370. return false;
  1371. if ($v_header['filename'] == '') {
  1372. continue;
  1373. }
  1374. // ----- Look for long filename
  1375. if ($v_header['typeflag'] == 'L') {
  1376. if (!$this->_readLongHeader($v_header))
  1377. return false;
  1378. }
  1379. if ((!$v_extract_all) && (is_array($p_file_list))) {
  1380. // ----- By default no unzip if the file is not found
  1381. $v_extract_file = false;
  1382. for ($i=0; $i<sizeof($p_file_list); $i++) {
  1383. // ----- Look if it is a directory
  1384. if (substr($p_file_list[$i], -1) == '/') {
  1385. // ----- Look if the directory is in the filename path
  1386. if ((strlen($v_header['filename']) > strlen($p_file_list[$i]))
  1387. && (substr($v_header['filename'], 0, strlen($p_file_list[$i]))
  1388. == $p_file_list[$i])) {
  1389. $v_extract_file = true;
  1390. break;
  1391. }
  1392. }
  1393. // ----- It is a file, so compare the file names
  1394. elseif ($p_file_list[$i] == $v_header['filename']) {
  1395. $v_extract_file = true;
  1396. break;
  1397. }
  1398. }
  1399. } else {
  1400. $v_extract_file = true;
  1401. }
  1402. // ----- Look if this file need to be extracted
  1403. if (($v_extract_file) && (!$v_listing))
  1404. {
  1405. if (($p_remove_path != '')
  1406. && (substr($v_header['filename'].'/', 0, $p_remove_path_size)
  1407. == $p_remove_path)) {
  1408. $v_header['filename'] = substr($v_header['filename'],
  1409. $p_remove_path_size);
  1410. if( $v_header['filename'] == '' ){
  1411. continue;
  1412. }
  1413. }
  1414. if (($p_path != './') && ($p_path != '/')) {
  1415. while (substr($p_path, -1) == '/')
  1416. $p_path = substr($p_path, 0, strlen($p_path)-1);
  1417. if (substr($v_header['filename'], 0, 1) == '/')
  1418. $v_header['filename'] = $p_path.$v_header['filename'];
  1419. else
  1420. $v_header['filename'] = $p_path.'/'.$v_header['filename'];
  1421. }
  1422. if (file_exists($v_header['filename'])) {
  1423. if ( (@is_dir($v_header['filename']))
  1424. && ($v_header['typeflag'] == '')) {
  1425. $this->_error('File '.$v_header['filename']
  1426. .' already exists as a directory');
  1427. return false;
  1428. }
  1429. if ( ($this->_isArchive($v_header['filename']))
  1430. && ($v_header['typeflag'] == "5")) {
  1431. $this->_error('Directory '.$v_header['filename']
  1432. .' already exists as a file');
  1433. return false;
  1434. }
  1435. if (!is_writeable($v_header['filename'])) {
  1436. $this->_error('File '.$v_header['filename']
  1437. .' already exists and is write protected');
  1438. return false;
  1439. }
  1440. if (filemtime($v_header['filename']) > $v_header['mtime']) {
  1441. // To be completed : An error or silent no replace ?
  1442. }
  1443. }
  1444. // ----- Check the directory availability and create it if necessary
  1445. elseif (($v_result
  1446. = $this->_dirCheck(($v_header['typeflag'] == "5"
  1447. ?$v_header['filename']
  1448. :dirname($v_header['filename'])))) != 1) {
  1449. $this->_error('Unable to create path for '.$v_header['filename']);
  1450. return false;
  1451. }
  1452. if ($v_extract_file) {
  1453. if ($v_header['typeflag'] == "5") {
  1454. if (!@file_exists($v_header['filename'])) {
  1455. if (!@mkdir($v_header['filename'], 0777)) {
  1456. $this->_error('Unable to create directory {'
  1457. .$v_header['filename'].'}');
  1458. return false;
  1459. }
  1460. }
  1461. } elseif ($v_header['typeflag'] == "2") {
  1462. if (@file_exists($v_header['filename'])) {
  1463. @unlink($v_header['filename']);
  1464. }
  1465. if (!@symlink($v_header['link'], $v_header['filename'])) {
  1466. $this->_error('Unable to extract symbolic link {'
  1467. .$v_header['filename'].'}');
  1468. return false;
  1469. }
  1470. } else {
  1471. if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
  1472. $this->_error('Error while opening {'.$v_header['filename']
  1473. .'} in write binary mode');
  1474. return false;
  1475. } else {
  1476. $n = floor($v_header['size']/512);
  1477. for ($i=0; $i<$n; $i++) {
  1478. $v_content = $this->_readBlock();
  1479. fwrite($v_dest_file, $v_content, 512);
  1480. }
  1481. if (($v_header['size'] % 512) != 0) {
  1482. $v_content = $this->_readBlock();
  1483. fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
  1484. }
  1485. @fclose($v_dest_file);
  1486. if ($p_preserve) {
  1487. @chown($v_header['filename'], $v_header['uid']);
  1488. @chgrp($v_header['filename'], $v_header['gid']);
  1489. }
  1490. // ----- Change the file mode, mtime
  1491. @touch($v_header['filename'], $v_header['mtime']);
  1492. if ($v_header['mode'] & 0111) {
  1493. // make file executable, obey umask
  1494. $mode = fileperms($v_header['filename']) | (~umask() & 0111);
  1495. @chmod($v_header['filename'], $mode);
  1496. }
  1497. }
  1498. // ----- Check the file size
  1499. clearstatcache();
  1500. if (!is_file($v_header['filename'])) {
  1501. $this->_error('Extracted file '.$v_header['filename']
  1502. .'does not exist. Archive may be corrupted.');
  1503. return false;
  1504. }
  1505. $filesize = filesize($v_header['filename']);
  1506. if ($filesize != $v_header['size']) {
  1507. $this->_error('Extracted file '.$v_header['filename']
  1508. .' does not have the correct file size \''
  1509. .$filesize
  1510. .'\' ('.$v_header['size']
  1511. .' expected). Archive may be corrupted.');
  1512. return false;
  1513. }
  1514. }
  1515. } else {
  1516. $this->_jumpBlock(ceil(($v_header['size']/512)));
  1517. }
  1518. } else {
  1519. $this->_jumpBlock(ceil(($v_header['size']/512)));
  1520. }
  1521. /* TBC : Seems to be unused ...
  1522. if ($this->_compress)
  1523. $v_end_of_file = @gzeof($this->_file);
  1524. else
  1525. $v_end_of_file = @feof($this->_file);
  1526. */
  1527. if ($v_listing || $v_extract_file || $v_extraction_stopped) {
  1528. // ----- Log extracted files
  1529. if (($v_file_dir = dirname($v_header['filename']))
  1530. == $v_header['filename'])
  1531. $v_file_dir = '';
  1532. if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == ''))
  1533. $v_file_dir = '/';
  1534. $p_list_detail[$v_nb++] = $v_header;
  1535. if (is_array($p_file_list) && (count($p_list_detail) == count($p_file_list))) {
  1536. return true;
  1537. }
  1538. }
  1539. }
  1540. return true;
  1541. }
  1542. // }}}
  1543. // {{{ _openAppend()
  1544. function _openAppend()
  1545. {
  1546. if (filesize($this->_tarname) == 0)
  1547. return $this->_openWrite();
  1548. if ($this->_compress) {
  1549. $this->_close();
  1550. if (!@rename($this->_tarname, $this->_tarname.".tmp")) {
  1551. $this->_error('Error while renaming \''.$this->_tarname
  1552. .'\' to temporary file \''.$this->_tarname
  1553. .'.tmp\'');
  1554. return false;
  1555. }
  1556. if ($this->_compress_type == 'gz')
  1557. $v_temp_tar = @gzopen($this->_tarname.".tmp", "rb");
  1558. elseif ($this->_compress_type == 'bz2')
  1559. $v_temp_tar = @bzopen($this->_tarname.".tmp", "r");
  1560. if ($v_temp_tar == 0) {
  1561. $this->_error('Unable to open file \''.$this->_tarname
  1562. .'.tmp\' in binary read mode');
  1563. @rename($this->_tarname.".tmp", $this->_tarname);
  1564. return false;
  1565. }
  1566. if (!$this->_openWrite()) {
  1567. @rename($this->_tarname.".tmp", $this->_tarname);
  1568. return false;
  1569. }
  1570. if ($this->_compress_type == 'gz') {
  1571. $end_blocks = 0;
  1572. while (!@gzeof($v_temp_tar)) {
  1573. $v_buffer = @gzread($v_temp_tar, 512);
  1574. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  1575. $end_blocks++;
  1576. // do not copy end blocks, we will re-make them
  1577. // after appending
  1578. continue;
  1579. } elseif ($end_blocks > 0) {
  1580. for ($i = 0; $i < $end_blocks; $i++) {
  1581. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  1582. }
  1583. $end_blocks = 0;
  1584. }
  1585. $v_binary_data = pack("a512", $v_buffer);
  1586. $this->_writeBlock($v_binary_data);
  1587. }
  1588. @gzclose($v_temp_tar);
  1589. }
  1590. elseif ($this->_compress_type == 'bz2') {
  1591. $end_blocks = 0;
  1592. while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) {
  1593. if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
  1594. $end_blocks++;
  1595. // do not copy end blocks, we will re-make them
  1596. // after appending
  1597. continue;
  1598. } elseif ($end_blocks > 0) {
  1599. for ($i = 0; $i < $end_blocks; $i++) {
  1600. $this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
  1601. }
  1602. $end_blocks = 0;
  1603. }
  1604. $v_binary_data = pack("a512", $v_buffer);
  1605. $this->_writeBlock($v_binary_data);
  1606. }
  1607. @bzclose($v_temp_tar);
  1608. }
  1609. if (!@unlink($this->_tarname.".tmp")) {
  1610. $this->_error('Error while deleting temporary file \''
  1611. .$this->_tarname.'.tmp\'');
  1612. }
  1613. } else {
  1614. // ----- For not compressed tar, just add files before the last
  1615. // one or two 512 bytes block
  1616. if (!$this->_openReadWrite())
  1617. return false;
  1618. clearstatcache();
  1619. $v_size = filesize($this->_tarname);
  1620. // We might have zero, one or two end blocks.
  1621. // The standard is two, but we should try to handle
  1622. // other cases.
  1623. fseek($this->_file, $v_size - 1024);
  1624. if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  1625. fseek($this->_file, $v_size - 1024);
  1626. }
  1627. elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
  1628. fseek($this->_file, $v_size - 512);
  1629. }
  1630. }
  1631. return true;
  1632. }
  1633. // }}}
  1634. // {{{ _append()
  1635. function _append($p_filelist, $p_add_dir='', $p_remove_dir='')
  1636. {
  1637. if (!$this->_openAppend())
  1638. return false;
  1639. if ($this->_addList($p_filelist, $p_add_dir, $p_remove_dir))
  1640. $this->_writeFooter();
  1641. $this->_close();
  1642. return true;
  1643. }
  1644. // }}}
  1645. // {{{ _dirCheck()
  1646. /**
  1647. * Check if a directory exists and create it (including parent
  1648. * dirs) if not.
  1649. *
  1650. * @param string $p_dir directory to check
  1651. *
  1652. * @return bool true if the directory exists or was created
  1653. */
  1654. function _dirCheck($p_dir)
  1655. {
  1656. clearstatcache();
  1657. if ((@is_dir($p_dir)) || ($p_dir == ''))
  1658. return true;
  1659. $p_parent_dir = dirname($p_dir);
  1660. if (($p_parent_dir != $p_dir) &&
  1661. ($p_parent_dir != '') &&
  1662. (!$this->_dirCheck($p_parent_dir)))
  1663. return false;
  1664. if (!@mkdir($p_dir, 0777)) {
  1665. $this->_error("Unable to create directory '$p_dir'");
  1666. return false;
  1667. }
  1668. return true;
  1669. }
  1670. // }}}
  1671. // {{{ _pathReduction()
  1672. /**
  1673. * Compress path by changing for example "/dir/foo/../bar" to "/dir/bar",
  1674. * rand emove double slashes.
  1675. *
  1676. * @param string $p_dir path to reduce
  1677. *
  1678. * @return string reduced path
  1679. *
  1680. * @access private
  1681. *
  1682. */
  1683. function _pathReduction($p_dir)
  1684. {
  1685. $v_result = '';
  1686. // ----- Look for not empty path
  1687. if ($p_dir != '') {
  1688. // ----- Explode path by directory names
  1689. $v_list = explode('/', $p_dir);
  1690. // ----- Study directories from last to first
  1691. for ($i=sizeof($v_list)-1; $i>=0; $i--) {
  1692. // ----- Look for current path
  1693. if ($v_list[$i] == ".") {
  1694. // ----- Ignore this directory
  1695. // Should be the first $i=0, but no check is done
  1696. }
  1697. else if ($v_list[$i] == "..") {
  1698. // ----- Ignore it and ignore the $i-1
  1699. $i--;
  1700. }
  1701. else if ( ($v_list[$i] == '')
  1702. && ($i!=(sizeof($v_list)-1))
  1703. && ($i!=0)) {
  1704. // ----- Ignore only the double '//' in path,
  1705. // but not the first and last /
  1706. } else {
  1707. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?'/'
  1708. .$v_result:'');
  1709. }
  1710. }
  1711. }
  1712. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  1713. $v_result = strtr($v_result, '\\', '/');
  1714. }
  1715. return $v_result;
  1716. }
  1717. // }}}
  1718. // {{{ _translateWinPath()
  1719. function _translateWinPath($p_path, $p_remove_disk_letter=true)
  1720. {
  1721. if (defined('OS_WINDOWS') && OS_WINDOWS) {
  1722. // ----- Look for potential disk letter
  1723. if ( ($p_remove_disk_letter)
  1724. && (($v_position = strpos($p_path, ':')) != false)) {
  1725. $p_path = substr($p_path, $v_position+1);
  1726. }
  1727. // ----- Change potential windows directory separator
  1728. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
  1729. $p_path = strtr($p_path, '\\', '/');
  1730. }
  1731. }
  1732. return $p_path;
  1733. }
  1734. // }}}
  1735. }
  1736. ?>