PageRenderTime 37ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/includes/pcl/pcltar.lib.php

https://github.com/collymore/test_project
PHP | 3570 lines | 1993 code | 509 blank | 1068 comment | 618 complexity | f5e5c9ed9117354c0853ca53cee74439 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: pcltar.lib.php 10381 2008-06-01 03:35:53Z pasamio $
  4. * @package Joomla
  5. */
  6. // --------------------------------------------------------------------------------
  7. // PhpConcept Library - Tar Module 1.3
  8. // --------------------------------------------------------------------------------
  9. // License GNU/GPL - Vincent Blavet - August 2001
  10. // http://www.phpconcept.net
  11. // --------------------------------------------------------------------------------
  12. //
  13. // Presentation :
  14. // PclTar is a library that allow you to create a GNU TAR + GNU ZIP archive,
  15. // to add files or directories, to extract all the archive or a part of it.
  16. // So far tests show that the files generated by PclTar are readable by
  17. // gzip tools and WinZip application.
  18. //
  19. // Description :
  20. // See readme.txt (English & Fran�ais) and http://www.phpconcept.net
  21. //
  22. // Warning :
  23. // This library and the associated files are non commercial, non professional
  24. // work.
  25. // It should not have unexpected results. However if any damage is caused by
  26. // this software the author can not be responsible.
  27. // The use of this software is at the risk of the user.
  28. //
  29. // --------------------------------------------------------------------------------
  30. // ----- Look for double include
  31. if (!defined("PCL_TAR"))
  32. {
  33. define( "PCL_TAR", 1 );
  34. // ----- Error codes
  35. // -1 : Unable to open file in binary write mode
  36. // -2 : Unable to open file in binary read mode
  37. // -3 : Invalid parameters
  38. // -4 : File does not exist
  39. // -5 : Filename is too long (max. 99)
  40. // -6 : Not a valid tar file
  41. // -7 : Invalid extracted file size
  42. // -8 : Unable to create directory
  43. // -9 : Invalid archive extension
  44. // -10 : Invalid archive format
  45. // -11 : Unable to delete file (unlink)
  46. // -12 : Unable to rename file (rename)
  47. // -13 : Invalid header checksum
  48. // --------------------------------------------------------------------------------
  49. // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
  50. // --------------------------------------------------------------------------------
  51. // ----- Global variables
  52. $g_pcltar_version = "1.3";
  53. // ----- Include other libraries
  54. // This library should be called by each script before the include of PhpZip
  55. // Library in order to limit the potential 'lib' directory path problem.
  56. if (!defined("PCLERROR_LIB"))
  57. {
  58. include(dirname(__FILE__).DIRECTORY_SEPARATOR.'pclerror.lib.php');
  59. }
  60. if (!defined("PCLTRACE_LIB"))
  61. {
  62. include(dirname(__FILE__).DIRECTORY_SEPARATOR.'pcltrace.lib.php');
  63. }
  64. // --------------------------------------------------------------------------------
  65. // Function : PclTarCreate()
  66. // Description :
  67. // Creates a new archive with name $p_tarname containing the files and/or
  68. // directories indicated in $p_list. If the tar filename extension is
  69. // ".tar", the file will not be compressed. If it is ".tar.gz" or ".tgz"
  70. // it will be a gzip compressed tar archive.
  71. // If you want to use an other extension, you must indicate the mode in
  72. // $p_mode ("tar" or "tgz").
  73. // $p_add_dir and $p_remove_dir give you the ability to store a path
  74. // which is not the real path of the files.
  75. // Parameters :
  76. // $p_tarname : Name of an existing tar file
  77. // $p_filelist : An array containing file or directory names, or
  78. // a string containing one filename or directory name, or
  79. // a string containing a list of filenames and/or directory
  80. // names separated by spaces.
  81. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive,
  82. // if $p_mode is not specified, it will be determined by the extension.
  83. // $p_add_dir : Path to add in the filename path archived
  84. // $p_remove_dir : Path to remove in the filename path archived
  85. // Return Values :
  86. // 1 on success, or an error code (see table at the beginning).
  87. // --------------------------------------------------------------------------------
  88. function PclTarCreate($p_tarname, $p_filelist="", $p_mode="", $p_add_dir="", $p_remove_dir="")
  89. {
  90. TrFctStart(__FILE__, __LINE__, "PclTarCreate", "tar=$p_tarname, file='$p_filelist', mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  91. $v_result=1;
  92. // ----- Look for default mode
  93. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  94. {
  95. // ----- Extract the tar format from the extension
  96. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  97. {
  98. // ----- Return
  99. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  100. return PclErrorCode();
  101. }
  102. // ----- Trace
  103. TrFctMessage(__FILE__, __LINE__, 1, "Auto mode selected : found $p_mode");
  104. }
  105. // ----- Look if the $p_filelist is really an array
  106. if (is_array($p_filelist))
  107. {
  108. // ----- Call the create fct
  109. $v_result = PclTarHandleCreate($p_tarname, $p_filelist, $p_mode, $p_add_dir, $p_remove_dir);
  110. }
  111. // ----- Look if the $p_filelist is a string
  112. else if (is_string($p_filelist))
  113. {
  114. // ----- Create a list with the elements from the string
  115. $v_list = explode(" ", $p_filelist);
  116. // ----- Call the create fct
  117. $v_result = PclTarHandleCreate($p_tarname, $v_list, $p_mode, $p_add_dir, $p_remove_dir);
  118. }
  119. // ----- Invalid variable
  120. else
  121. {
  122. // ----- Error log
  123. PclErrorLog(-3, "Invalid variable type p_filelist");
  124. $v_result = -3;
  125. }
  126. // ----- Return
  127. TrFctEnd(__FILE__, __LINE__, $v_result);
  128. return $v_result;
  129. }
  130. // --------------------------------------------------------------------------------
  131. // --------------------------------------------------------------------------------
  132. // Function : PclTarAdd()
  133. // Description :
  134. // PLEASE DO NOT USE ANY MORE THIS FUNCTION. Use PclTarAddList().
  135. //
  136. // This function is maintained only for compatibility reason
  137. //
  138. // Parameters :
  139. // $p_tarname : Name of an existing tar file
  140. // $p_filelist : An array containing file or directory names, or
  141. // a string containing one filename or directory name, or
  142. // a string containing a list of filenames and/or directory
  143. // names separated by spaces.
  144. // Return Values :
  145. // 1 on success,
  146. // Or an error code (see list on top).
  147. // --------------------------------------------------------------------------------
  148. function PclTarAdd($p_tarname, $p_filelist)
  149. {
  150. TrFctStart(__FILE__, __LINE__, "PclTarAdd", "tar=$p_tarname, file=$p_filelist");
  151. $v_result=1;
  152. $v_list_detail = array();
  153. // ----- Extract the tar format from the extension
  154. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  155. {
  156. // ----- Return
  157. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  158. return PclErrorCode();
  159. }
  160. // ----- Look if the $p_filelist is really an array
  161. if (is_array($p_filelist))
  162. {
  163. // ----- Call the add fct
  164. $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $v_list_detail, "", "");
  165. }
  166. // ----- Look if the $p_filelist is a string
  167. else if (is_string($p_filelist))
  168. {
  169. // ----- Create a list with the elements from the string
  170. $v_list = explode(" ", $p_filelist);
  171. // ----- Call the add fct
  172. $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $v_list_detail, "", "");
  173. }
  174. // ----- Invalid variable
  175. else
  176. {
  177. // ----- Error log
  178. PclErrorLog(-3, "Invalid variable type p_filelist");
  179. $v_result = -3;
  180. }
  181. // ----- Cleaning
  182. unset($v_list_detail);
  183. // ----- Return
  184. TrFctEnd(__FILE__, __LINE__, $v_result);
  185. return $v_result;
  186. }
  187. // --------------------------------------------------------------------------------
  188. // --------------------------------------------------------------------------------
  189. // Function : PclTarAddList()
  190. // Description :
  191. // Add a list of files or directories ($p_filelist) in the tar archive $p_tarname.
  192. // The list can be an array of file/directory names or a string with names
  193. // separated by one space.
  194. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  195. // different from the real path of the file. This is usefull if you want to have PclTar
  196. // running in any directory, and memorize relative path from an other directory.
  197. // If $p_mode is not set it will be automatically computed from the $p_tarname
  198. // extension (.tar, .tar.gz or .tgz).
  199. // Parameters :
  200. // $p_tarname : Name of an existing tar file
  201. // $p_filelist : An array containing file or directory names, or
  202. // a string containing one filename or directory name, or
  203. // a string containing a list of filenames and/or directory
  204. // names separated by spaces.
  205. // $p_add_dir : Path to add in the filename path archived
  206. // $p_remove_dir : Path to remove in the filename path archived
  207. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  208. // Return Values :
  209. // 1 on success,
  210. // Or an error code (see list on top).
  211. // --------------------------------------------------------------------------------
  212. function PclTarAddList($p_tarname, $p_filelist, $p_add_dir="", $p_remove_dir="", $p_mode="")
  213. {
  214. TrFctStart(__FILE__, __LINE__, "PclTarAddList", "tar=$p_tarname, file=$p_filelist, p_add_dir='$p_add_dir', p_remove_dir='$p_remove_dir', mode=$p_mode");
  215. $v_result=1;
  216. $p_list_detail = array();
  217. // ----- Extract the tar format from the extension
  218. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  219. {
  220. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  221. {
  222. // ----- Return
  223. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  224. return PclErrorCode();
  225. }
  226. }
  227. // ----- Look if the $p_filelist is really an array
  228. if (is_array($p_filelist))
  229. {
  230. // ----- Call the add fct
  231. $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
  232. }
  233. // ----- Look if the $p_filelist is a string
  234. else if (is_string($p_filelist))
  235. {
  236. // ----- Create a list with the elements from the string
  237. $v_list = explode(" ", $p_filelist);
  238. // ----- Call the add fct
  239. $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
  240. }
  241. // ----- Invalid variable
  242. else
  243. {
  244. // ----- Error log
  245. PclErrorLog(-3, "Invalid variable type p_filelist");
  246. $v_result = -3;
  247. }
  248. // ----- Return
  249. if ($v_result != 1)
  250. {
  251. TrFctEnd(__FILE__, __LINE__, 0);
  252. return 0;
  253. }
  254. TrFctEnd(__FILE__, __LINE__, $p_list_detail);
  255. return $p_list_detail;
  256. }
  257. // --------------------------------------------------------------------------------
  258. // --------------------------------------------------------------------------------
  259. // Function : PclTarList()
  260. // Description :
  261. // Gives the list of all the files present in the tar archive $p_tarname.
  262. // The list is the function result, it will be 0 on error.
  263. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  264. // function will determine the type of the archive.
  265. // Parameters :
  266. // $p_tarname : Name of an existing tar file
  267. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  268. // Return Values :
  269. // 0 on error (Use PclErrorCode() and PclErrorString() for more info)
  270. // or
  271. // An array containing file properties. Each file properties is an array of
  272. // properties.
  273. // The properties (array field names) are :
  274. // filename, size, mode, uid, gid, mtime, typeflag, status
  275. // Exemple : $v_list = PclTarList("my.tar");
  276. // for ($i=0; $i<sizeof($v_list); $i++)
  277. // echo "Filename :'".$v_list[$i][filename]."'<br>";
  278. // --------------------------------------------------------------------------------
  279. function PclTarList($p_tarname, $p_mode="")
  280. {
  281. TrFctStart(__FILE__, __LINE__, "PclTarList", "tar=$p_tarname, mode='$p_mode'");
  282. $v_result=1;
  283. // ----- Extract the tar format from the extension
  284. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  285. {
  286. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  287. {
  288. // ----- Return
  289. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  290. return 0;
  291. }
  292. }
  293. // ----- Call the extracting fct
  294. $p_list = array();
  295. if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "list", "", $p_mode, "")) != 1)
  296. {
  297. unset($p_list);
  298. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  299. return(0);
  300. }
  301. // ----- Return
  302. TrFctEnd(__FILE__, __LINE__, $p_list);
  303. return $p_list;
  304. }
  305. // --------------------------------------------------------------------------------
  306. // --------------------------------------------------------------------------------
  307. // Function : PclTarExtract()
  308. // Description :
  309. // Extract all the files present in the archive $p_tarname, in the directory
  310. // $p_path. The relative path of the archived files are keep and become
  311. // relative to $p_path.
  312. // If a file with the same name already exists it will be replaced.
  313. // If the path to the file does not exist, it will be created.
  314. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  315. // function will determine the type of the archive.
  316. // Parameters :
  317. // $p_tarname : Name of an existing tar file.
  318. // $p_path : Path where the files will be extracted. The files will use
  319. // their memorized path from $p_path.
  320. // If $p_path is "", files will be extracted in "./".
  321. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  322. // extracted files. If the path does not match the file path,
  323. // the file is extracted with its memorized path.
  324. // $p_path and $p_remove_path are commulative.
  325. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  326. // Return Values :
  327. // Same as PclTarList()
  328. // --------------------------------------------------------------------------------
  329. function PclTarExtract($p_tarname, $p_path="./", $p_remove_path="", $p_mode="")
  330. {
  331. TrFctStart(__FILE__, __LINE__, "PclTarExtract", "tar='$p_tarname', path='$p_path', remove_path='$p_remove_path', mode='$p_mode'");
  332. $v_result=1;
  333. // ----- Extract the tar format from the extension
  334. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  335. {
  336. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  337. {
  338. // ----- Return
  339. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  340. return 0;
  341. }
  342. }
  343. // ----- Call the extracting fct
  344. if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "complete", $p_path, $p_mode, $p_remove_path)) != 1)
  345. {
  346. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  347. return(0);
  348. }
  349. // ----- Return
  350. TrFctEnd(__FILE__, __LINE__, $p_list);
  351. return $p_list;
  352. }
  353. // --------------------------------------------------------------------------------
  354. // --------------------------------------------------------------------------------
  355. // Function : PclTarExtractList()
  356. // Description :
  357. // Extract the files present in the archive $p_tarname and specified in
  358. // $p_filelist, in the directory
  359. // $p_path. The relative path of the archived files are keep and become
  360. // relative to $p_path.
  361. // If a directory is sp�cified in the list, all the files from this directory
  362. // will be extracted.
  363. // If a file with the same name already exists it will be replaced.
  364. // If the path to the file does not exist, it will be created.
  365. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  366. // function will determine the type of the archive.
  367. // Parameters :
  368. // $p_tarname : Name of an existing tar file
  369. // $p_filelist : An array containing file or directory names, or
  370. // a string containing one filename or directory name, or
  371. // a string containing a list of filenames and/or directory
  372. // names separated by spaces.
  373. // $p_path : Path where the files will be extracted. The files will use
  374. // their memorized path from $p_path.
  375. // If $p_path is "", files will be extracted in "./".
  376. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  377. // extracted files. If the path does not match the file path,
  378. // the file is extracted with its memorized path.
  379. // $p_path and $p_remove_path are commulative.
  380. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  381. // Return Values :
  382. // Same as PclTarList()
  383. // --------------------------------------------------------------------------------
  384. function PclTarExtractList($p_tarname, $p_filelist, $p_path="./", $p_remove_path="", $p_mode="")
  385. {
  386. TrFctStart(__FILE__, __LINE__, "PclTarExtractList", "tar=$p_tarname, list, path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
  387. $v_result=1;
  388. // ----- Extract the tar format from the extension
  389. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  390. {
  391. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  392. {
  393. // ----- Return
  394. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  395. return 0;
  396. }
  397. }
  398. // ----- Look if the $p_filelist is really an array
  399. if (is_array($p_filelist))
  400. {
  401. // ----- Call the extracting fct
  402. if (($v_result = PclTarHandleExtract($p_tarname, $p_filelist, $p_list, "partial", $p_path, $v_tar_mode, $p_remove_path)) != 1)
  403. {
  404. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  405. return(0);
  406. }
  407. }
  408. // ----- Look if the $p_filelist is a string
  409. else if (is_string($p_filelist))
  410. {
  411. // ----- Create a list with the elements from the string
  412. $v_list = explode(" ", $p_filelist);
  413. // ----- Call the extracting fct
  414. if (($v_result = PclTarHandleExtract($p_tarname, $v_list, $p_list, "partial", $p_path, $v_tar_mode, $p_remove_path)) != 1)
  415. {
  416. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  417. return(0);
  418. }
  419. }
  420. // ----- Invalid variable
  421. else
  422. {
  423. // ----- Error log
  424. PclErrorLog(-3, "Invalid variable type p_filelist");
  425. // ----- Return
  426. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  427. return 0;
  428. }
  429. // ----- Return
  430. TrFctEnd(__FILE__, __LINE__, $p_list);
  431. return $p_list;
  432. }
  433. // --------------------------------------------------------------------------------
  434. // --------------------------------------------------------------------------------
  435. // Function : PclTarExtractIndex()
  436. // Description :
  437. // Extract the files present in the archive $p_tarname and specified at
  438. // the indexes in $p_index, in the directory
  439. // $p_path. The relative path of the archived files are keep and become
  440. // relative to $p_path.
  441. // If a directory is specified in the list, the directory only is created. All
  442. // the file stored in this archive for this directory
  443. // are not extracted.
  444. // If a file with the same name already exists it will be replaced.
  445. // If the path to the file does not exist, it will be created.
  446. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  447. // function will determine the type of the archive.
  448. // Parameters :
  449. // $p_tarname : Name of an existing tar file
  450. // $p_index : A single index (integer) or a string of indexes of files to
  451. // extract. The form of the string is "0,4-6,8-12" with only numbers
  452. // and '-' for range or ',' to separate ranges. No spaces or ';'
  453. // are allowed.
  454. // $p_path : Path where the files will be extracted. The files will use
  455. // their memorized path from $p_path.
  456. // If $p_path is "", files will be extracted in "./".
  457. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  458. // extracted files. If the path does not match the file path,
  459. // the file is extracted with its memorized path.
  460. // $p_path and $p_remove_path are commulative.
  461. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  462. // Return Values :
  463. // Same as PclTarList()
  464. // --------------------------------------------------------------------------------
  465. function PclTarExtractIndex($p_tarname, $p_index, $p_path="./", $p_remove_path="", $p_mode="")
  466. {
  467. TrFctStart(__FILE__, __LINE__, "PclTarExtractIndex", "tar=$p_tarname, index='$p_index', path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
  468. $v_result=1;
  469. // ----- Extract the tar format from the extension
  470. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  471. {
  472. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  473. {
  474. // ----- Return
  475. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  476. return 0;
  477. }
  478. }
  479. // ----- Look if the $p_index is really an integer
  480. if (is_integer($p_index))
  481. {
  482. // ----- Call the extracting fct
  483. if (($v_result = PclTarHandleExtractByIndexList($p_tarname, "$p_index", $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1)
  484. {
  485. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  486. return(0);
  487. }
  488. }
  489. // ----- Look if the $p_filelist is a string
  490. else if (is_string($p_index))
  491. {
  492. // ----- Call the extracting fct
  493. if (($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1)
  494. {
  495. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  496. return(0);
  497. }
  498. }
  499. // ----- Invalid variable
  500. else
  501. {
  502. // ----- Error log
  503. PclErrorLog(-3, "Invalid variable type $p_index");
  504. // ----- Return
  505. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  506. return 0;
  507. }
  508. // ----- Return
  509. TrFctEnd(__FILE__, __LINE__, $p_list);
  510. return $p_list;
  511. }
  512. // --------------------------------------------------------------------------------
  513. // --------------------------------------------------------------------------------
  514. // Function : PclTarDelete()
  515. // Description :
  516. // This function deletes from the archive $p_tarname the files which are listed
  517. // in $p_filelist. $p_filelist can be a string with file names separated by
  518. // spaces, or an array containing the file names.
  519. // Parameters :
  520. // $p_tarname : Name of an existing tar file
  521. // $p_filelist : An array or a string containing file names to remove from the
  522. // archive.
  523. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  524. // Return Values :
  525. // List of the files which are kept in the archive (same format as PclTarList())
  526. // --------------------------------------------------------------------------------
  527. function PclTarDelete($p_tarname, $p_filelist, $p_mode="")
  528. {
  529. TrFctStart(__FILE__, __LINE__, "PclTarDelete", "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
  530. $v_result=1;
  531. // ----- Extract the tar format from the extension
  532. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  533. {
  534. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  535. {
  536. // ----- Return
  537. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  538. return 0;
  539. }
  540. }
  541. // ----- Look if the $p_filelist is really an array
  542. if (is_array($p_filelist))
  543. {
  544. // ----- Call the extracting fct
  545. if (($v_result = PclTarHandleDelete($p_tarname, $p_filelist, $p_list, $p_mode)) != 1)
  546. {
  547. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  548. return(0);
  549. }
  550. }
  551. // ----- Look if the $p_filelist is a string
  552. else if (is_string($p_filelist))
  553. {
  554. // ----- Create a list with the elements from the string
  555. $v_list = explode(" ", $p_filelist);
  556. // ----- Call the extracting fct
  557. if (($v_result = PclTarHandleDelete($p_tarname, $v_list, $p_list, $p_mode)) != 1)
  558. {
  559. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  560. return(0);
  561. }
  562. }
  563. // ----- Invalid variable
  564. else
  565. {
  566. // ----- Error log
  567. PclErrorLog(-3, "Invalid variable type p_filelist");
  568. // ----- Return
  569. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  570. return 0;
  571. }
  572. // ----- Return
  573. TrFctEnd(__FILE__, __LINE__, $p_list);
  574. return $p_list;
  575. }
  576. // --------------------------------------------------------------------------------
  577. // --------------------------------------------------------------------------------
  578. // Function : PclTarUpdate()
  579. // Description :
  580. // This function updates the files in $p_filelist which are already in the
  581. // $p_tarname archive with an older last modified date. If the file does not
  582. // exist, it is added at the end of the archive.
  583. // Parameters :
  584. // $p_tarname : Name of an existing tar file
  585. // $p_filelist : An array or a string containing file names to update from the
  586. // archive.
  587. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  588. // Return Values :
  589. // List of the files contained in the archive. The field status contains
  590. // "updated", "not_updated", "added" or "ok" for the files not concerned.
  591. // --------------------------------------------------------------------------------
  592. function PclTarUpdate($p_tarname, $p_filelist, $p_mode="", $p_add_dir="", $p_remove_dir="")
  593. {
  594. TrFctStart(__FILE__, __LINE__, "PclTarUpdate", "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
  595. $v_result=1;
  596. // ----- Extract the tar format from the extension
  597. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  598. {
  599. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  600. {
  601. // ----- Return
  602. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  603. return 0;
  604. }
  605. }
  606. // ----- Look if the $p_filelist is really an array
  607. if (is_array($p_filelist))
  608. {
  609. // ----- Call the extracting fct
  610. if (($v_result = PclTarHandleUpdate($p_tarname, $p_filelist, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1)
  611. {
  612. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  613. return(0);
  614. }
  615. }
  616. // ----- Look if the $p_filelist is a string
  617. else if (is_string($p_filelist))
  618. {
  619. // ----- Create a list with the elements from the string
  620. $v_list = explode(" ", $p_filelist);
  621. // ----- Call the extracting fct
  622. if (($v_result = PclTarHandleUpdate($p_tarname, $v_list, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1)
  623. {
  624. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  625. return(0);
  626. }
  627. }
  628. // ----- Invalid variable
  629. else
  630. {
  631. // ----- Error log
  632. PclErrorLog(-3, "Invalid variable type p_filelist");
  633. // ----- Return
  634. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  635. return 0;
  636. }
  637. // ----- Return
  638. TrFctEnd(__FILE__, __LINE__, $p_list);
  639. return $p_list;
  640. }
  641. // --------------------------------------------------------------------------------
  642. // --------------------------------------------------------------------------------
  643. // Function : PclTarMerge()
  644. // Description :
  645. // This function add the content of $p_tarname_add at the end of $p_tarname.
  646. // Parameters :
  647. // $p_tarname : Name of an existing tar file
  648. // $p_tarname_add : Name of an existing tar file taht will be added at the end
  649. // of $p_tarname.
  650. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  651. // $p_mode_add : 'tar' or 'tgz', if not set, will be determined by $p_tarname_add
  652. // extension
  653. // Return Values :
  654. // List of the files contained in the archive. The field status contains
  655. // "updated", "not_updated", "added" or "ok" for the files not concerned.
  656. // --------------------------------------------------------------------------------
  657. function PclTarMerge($p_tarname, $p_tarname_add, $p_mode="", $p_mode_add="")
  658. {
  659. TrFctStart(__FILE__, __LINE__, "PclTarMerge", "tar='$p_tarname', tar_add='$p_tarname_add', mode='$p_mode', mode_add='$p_mode_add'");
  660. $v_result=1;
  661. // ----- Check the parameters
  662. if (($p_tarname == "") || ($p_tarname_add == ""))
  663. {
  664. // ----- Error log
  665. PclErrorLog(-3, "Invalid empty archive name");
  666. // ----- Return
  667. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  668. return PclErrorCode();
  669. }
  670. // ----- Extract the tar format from the extension
  671. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  672. {
  673. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  674. {
  675. // ----- Return
  676. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  677. return 0;
  678. }
  679. }
  680. if (($p_mode_add == "") || (($p_mode_add!="tar") && ($p_mode_add!="tgz")))
  681. {
  682. if (($p_mode_add = PclTarHandleExtension($p_tarname_add)) == "")
  683. {
  684. // ----- Return
  685. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  686. return 0;
  687. }
  688. }
  689. // ----- Clear filecache
  690. clearstatcache();
  691. // ----- Check the file size
  692. if ((!is_file($p_tarname)) ||
  693. (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar")))
  694. {
  695. // ----- Error log
  696. if (!is_file($p_tarname))
  697. PclErrorLog(-4, "Archive '$p_tarname' does not exist");
  698. else
  699. PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)");
  700. // ----- Return
  701. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  702. return PclErrorCode();
  703. }
  704. if ((!is_file($p_tarname_add)) ||
  705. (((($v_size_add = filesize($p_tarname_add)) % 512) != 0) && ($p_mode_add=="tar")))
  706. {
  707. // ----- Error log
  708. if (!is_file($p_tarname_add))
  709. PclErrorLog(-4, "Archive '$p_tarname_add' does not exist");
  710. else
  711. PclErrorLog(-6, "Archive '$p_tarname_add' has invalid size ".filesize($p_tarname_add)."(not a 512 block multiple)");
  712. // ----- Return
  713. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  714. return PclErrorCode();
  715. }
  716. // ----- Look for compressed archive
  717. if ($p_mode == "tgz")
  718. {
  719. // ----- Open the file in read mode
  720. if (($p_tar = @gzopen($p_tarname, "rb")) == 0)
  721. {
  722. // ----- Error log
  723. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  724. // ----- Return
  725. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  726. return PclErrorCode();
  727. }
  728. // ----- Open a temporary file in write mode
  729. $v_temp_tarname = uniqid("pcltar-").".tmp";
  730. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  731. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  732. {
  733. // ----- Close tar file
  734. gzclose($p_tar);
  735. // ----- Error log
  736. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  737. // ----- Return
  738. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  739. return PclErrorCode();
  740. }
  741. // ----- Read the first 512 bytes block
  742. $v_buffer = gzread($p_tar, 512);
  743. // ----- Read the following blocks but not the last one
  744. if (!gzeof($p_tar))
  745. {
  746. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  747. $i=1;
  748. // ----- Read new 512 block and write the already read
  749. do{
  750. // ----- Write the already read block
  751. $v_binary_data = pack("a512", "$v_buffer");
  752. gzputs($v_temp_tar, $v_binary_data);
  753. $i++;
  754. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  755. // ----- Read next block
  756. $v_buffer = gzread($p_tar, 512);
  757. } while (!gzeof($p_tar));
  758. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  759. }
  760. }
  761. // ----- Look for uncompressed tar file
  762. else if ($p_mode=="tar")
  763. {
  764. // ----- Open the tar file
  765. if (($p_tar = fopen($p_tarname, "r+b")) == 0)
  766. {
  767. // ----- Error log
  768. PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
  769. // ----- Return
  770. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  771. return PclErrorCode();
  772. }
  773. // ----- Go to the beginning of last block
  774. TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  775. fseek($p_tar, $v_size-512);
  776. TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  777. }
  778. // ----- Look for unknown type
  779. else
  780. {
  781. // ----- Error log
  782. PclErrorLog(-3, "Invalid tar mode $p_mode");
  783. // ----- Return
  784. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  785. return PclErrorCode();
  786. }
  787. // ----- Look for type of archive to add
  788. if ($p_mode_add == "tgz")
  789. {
  790. TrFctMessage(__FILE__, __LINE__, 4, "Opening file $p_tarname_add");
  791. // ----- Open the file in read mode
  792. if (($p_tar_add = @gzopen($p_tarname_add, "rb")) == 0)
  793. {
  794. // ----- Error log
  795. PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
  796. // ----- Return
  797. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  798. return PclErrorCode();
  799. }
  800. // ----- Read the first 512 bytes block
  801. $v_buffer = gzread($p_tar_add, 512);
  802. // ----- Read the following blocks but not the last one
  803. if (!gzeof($p_tar_add))
  804. {
  805. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  806. $i=1;
  807. // ----- Read new 512 block and write the already read
  808. do{
  809. // ----- Write the already read block
  810. $v_binary_data = pack("a512", "$v_buffer");
  811. if ($p_mode=="tar")
  812. fputs($p_tar, $v_binary_data);
  813. else
  814. gzputs($v_temp_tar, $v_binary_data);
  815. $i++;
  816. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  817. // ----- Read next block
  818. $v_buffer = gzread($p_tar_add, 512);
  819. } while (!gzeof($p_tar_add));
  820. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  821. }
  822. // ----- Close the files
  823. gzclose($p_tar_add);
  824. }
  825. // ----- Look for uncompressed tar file
  826. else if ($p_mode=="tar")
  827. {
  828. // ----- Open the file in read mode
  829. if (($p_tar_add = @fopen($p_tarname_add, "rb")) == 0)
  830. {
  831. // ----- Error log
  832. PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
  833. // ----- Return
  834. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  835. return PclErrorCode();
  836. }
  837. // ----- Read the first 512 bytes block
  838. $v_buffer = fread($p_tar_add, 512);
  839. // ----- Read the following blocks but not the last one
  840. if (!feof($p_tar_add))
  841. {
  842. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  843. $i=1;
  844. // ----- Read new 512 block and write the already read
  845. do{
  846. // ----- Write the already read block
  847. $v_binary_data = pack("a512", "$v_buffer");
  848. if ($p_mode=="tar")
  849. fputs($p_tar, $v_binary_data);
  850. else
  851. gzputs($v_temp_tar, $v_binary_data);
  852. $i++;
  853. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  854. // ----- Read next block
  855. $v_buffer = fread($p_tar_add, 512);
  856. } while (!feof($p_tar_add));
  857. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  858. }
  859. // ----- Close the files
  860. fclose($p_tar_add);
  861. }
  862. // ----- Call the footer of the tar archive
  863. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  864. // ----- Look for closing compressed archive
  865. if ($p_mode == "tgz")
  866. {
  867. // ----- Close the files
  868. gzclose($p_tar);
  869. gzclose($v_temp_tar);
  870. // ----- Unlink tar file
  871. if (!@unlink($p_tarname))
  872. {
  873. // ----- Error log
  874. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  875. }
  876. // ----- Rename tar file
  877. if (!@rename($v_temp_tarname, $p_tarname))
  878. {
  879. // ----- Error log
  880. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  881. // ----- Return
  882. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  883. return PclErrorCode();
  884. }
  885. // ----- Return
  886. TrFctEnd(__FILE__, __LINE__, $v_result);
  887. return $v_result;
  888. }
  889. // ----- Look for closing uncompressed tar file
  890. else if ($p_mode=="tar")
  891. {
  892. // ----- Close the tarfile
  893. fclose($p_tar);
  894. }
  895. // ----- Return
  896. TrFctEnd(__FILE__, __LINE__, $v_result);
  897. return $v_result;
  898. }
  899. // --------------------------------------------------------------------------------
  900. // --------------------------------------------------------------------------------
  901. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  902. // ***** *****
  903. // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
  904. // --------------------------------------------------------------------------------
  905. // --------------------------------------------------------------------------------
  906. // Function : PclTarHandleCreate()
  907. // Description :
  908. // Parameters :
  909. // $p_tarname : Name of the tar file
  910. // $p_list : An array containing the file or directory names to add in the tar
  911. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  912. // Return Values :
  913. // --------------------------------------------------------------------------------
  914. function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir="", $p_remove_dir="")
  915. {
  916. TrFctStart(__FILE__, __LINE__, "PclTarHandleCreate", "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  917. $v_result=1;
  918. $v_list_detail = array();
  919. // ----- Check the parameters
  920. if (($p_tarname == "") || (($p_mode != "tar") && ($p_mode != "tgz")))
  921. {
  922. // ----- Error log
  923. if ($p_tarname == "")
  924. PclErrorLog(-3, "Invalid empty archive name");
  925. else
  926. PclErrorLog(-3, "Unknown mode '$p_mode'");
  927. // ----- Return
  928. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  929. return PclErrorCode();
  930. }
  931. // ----- Look for tar file
  932. if ($p_mode == "tar")
  933. {
  934. // ----- Open the tar file
  935. if (($p_tar = fopen($p_tarname, "wb")) == 0)
  936. {
  937. // ----- Error log
  938. PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
  939. // ----- Return
  940. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  941. return PclErrorCode();
  942. }
  943. // ----- Call the adding fct inside the tar
  944. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  945. {
  946. // ----- Call the footer of the tar archive
  947. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  948. }
  949. // ----- Close the tarfile
  950. fclose($p_tar);
  951. }
  952. // ----- Look for tgz file
  953. else
  954. {
  955. // ----- Open the tar file
  956. if (($p_tar = @gzopen($p_tarname, "wb")) == 0)
  957. {
  958. // ----- Error log
  959. PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
  960. // ----- Return
  961. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  962. return PclErrorCode();
  963. }
  964. // ----- Call the adding fct inside the tar
  965. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  966. {
  967. // ----- Call the footer of the tar archive
  968. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  969. }
  970. // ----- Close the tarfile
  971. gzclose($p_tar);
  972. }
  973. // ----- Return
  974. TrFctEnd(__FILE__, __LINE__, $v_result);
  975. return $v_result;
  976. }
  977. // --------------------------------------------------------------------------------
  978. // --------------------------------------------------------------------------------
  979. // Function : PclTarHandleAppend()
  980. // Description :
  981. // Parameters :
  982. // $p_tarname : Name of the tar file
  983. // $p_list : An array containing the file or directory names to add in the tar
  984. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  985. // Return Values :
  986. // --------------------------------------------------------------------------------
  987. function PclTarHandleAppend($p_tarname, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
  988. {
  989. TrFctStart(__FILE__, __LINE__, "PclTarHandleAppend", "tar=$p_tarname, list, mode=$p_mode");
  990. $v_result=1;
  991. // ----- Check the parameters
  992. if ($p_tarname == "")
  993. {
  994. // ----- Error log
  995. PclErrorLog(-3, "Invalid empty archive name");
  996. // ----- Return
  997. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  998. return PclErrorCode();
  999. }
  1000. clearstatcache();
  1001. // ----- Check the file size
  1002. if ((!is_file($p_tarname)) ||
  1003. (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar")))
  1004. {
  1005. // ----- Error log
  1006. if (!is_file($p_tarname))
  1007. PclErrorLog(-4, "Archive '$p_tarname' does not exist");
  1008. else
  1009. PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)");
  1010. // ----- Return
  1011. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1012. return PclErrorCode();
  1013. }
  1014. // ----- Look for compressed archive
  1015. if ($p_mode == "tgz")
  1016. {
  1017. // ----- Open the file in read mode
  1018. if (($p_tar = @gzopen($p_tarname, "rb")) == 0)
  1019. {
  1020. // ----- Error log
  1021. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  1022. // ----- Return
  1023. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1024. return PclErrorCode();
  1025. }
  1026. // ----- Open a temporary file in write mode
  1027. $v_temp_tarname = uniqid("pcltar-").".tmp";
  1028. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  1029. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  1030. {
  1031. // ----- Close tar file
  1032. gzclose($p_tar);
  1033. // ----- Error log
  1034. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  1035. // ----- Return
  1036. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1037. return PclErrorCode();
  1038. }
  1039. // ----- Read the first 512 bytes block
  1040. $v_buffer = gzread($p_tar, 512);
  1041. // ----- Read the following blocks but not the last one
  1042. if (!gzeof($p_tar))
  1043. {
  1044. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  1045. $i=1;
  1046. // ----- Read new 512 block and write the already read
  1047. do{
  1048. // ----- Write the already read block
  1049. $v_binary_data = pack("a512", "$v_buffer");
  1050. gzputs($v_temp_tar, $v_binary_data);
  1051. $i++;
  1052. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  1053. // ----- Read next block
  1054. $v_buffer = gzread($p_tar, 512);
  1055. } while (!gzeof($p_tar));
  1056. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  1057. }
  1058. // ----- Call the adding fct inside the tar
  1059. if (($v_result = PclTarHandleAddList($v_temp_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  1060. {
  1061. // ----- Call the footer of the tar archive
  1062. $v_result = PclTarHandleFooter($v_temp_tar, $p_mode);
  1063. }
  1064. // ----- Close the files
  1065. gzclose($p_tar);
  1066. gzclose($v_temp_tar);
  1067. // ----- Unlink tar file
  1068. if (!@unlink($p_tarname))
  1069. {
  1070. // ----- Error log
  1071. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  1072. }
  1073. // ----- Rename tar file
  1074. if (!@rename($v_temp_tarname, $p_tarname))
  1075. {
  1076. // ----- Error log
  1077. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  1078. // ----- Return
  1079. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1080. return PclErrorCode();
  1081. }
  1082. // ----- Return
  1083. TrFctEnd(__FILE__, __LINE__, $v_result);
  1084. return $v_result;
  1085. }
  1086. // ----- Look for uncompressed tar file
  1087. else if ($p_mode=="tar")
  1088. {
  1089. // ----- Open the tar file
  1090. if (($p_tar = fopen($p_tarname, "r+b")) == 0)
  1091. {
  1092. // ----- Error log
  1093. PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
  1094. // ----- Return
  1095. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1096. return PclErrorCode();
  1097. }
  1098. // ----- Go to the beginning of last block
  1099. TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1100. fseek($p_tar, $v_size-512);
  1101. TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1102. // ----- Call the adding fct inside the tar
  1103. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  1104. {
  1105. // ----- Call the footer of the tar archive
  1106. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  1107. }
  1108. // ----- Close the tarfile
  1109. fclose($p_tar);
  1110. }
  1111. // ----- Look for unknown type
  1112. else
  1113. {
  1114. // ----- Error log
  1115. PclErrorLog(-3, "Invalid tar mode $p_mode");
  1116. // ----- Return
  1117. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1118. return PclErrorCode();
  1119. }
  1120. // ----- Return
  1121. TrFctEnd(__FILE__, __LINE__, $v_result);
  1122. return $v_result;
  1123. }
  1124. // --------------------------------------------------------------------------------
  1125. // --------------------------------------------------------------------------------
  1126. // Function : PclTarHandleAddList()
  1127. // Description :
  1128. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  1129. // different from the real path of the file. This is usefull if you want to have PclTar
  1130. // running in any directory, and memorize relative path from an other directory.
  1131. // Parameters :
  1132. // $p_tar : File descriptor of the tar archive
  1133. // $p_list : An array containing the file or directory names to add in the tar
  1134. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  1135. // $p_list_detail : list of added files with their properties (specially the status field)
  1136. // $p_add_dir : Path to add in the filename path archived
  1137. // $p_remove_dir : Path to remove in the filename path archived
  1138. // Return Values :
  1139. // --------------------------------------------------------------------------------
  1140. function PclTarHandleAddList($p_tar, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
  1141. {
  1142. TrFctStart(__FILE__, __LINE__, "PclTarHandleAddList", "tar='$p_tar', list, mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  1143. $v_result=1;
  1144. $v_header = array();
  1145. // ----- Recuperate the current number of elt in list
  1146. $v_nb = sizeof($p_list_detail);
  1147. // ----- Check the parameters
  1148. if ($p_tar == 0)
  1149. {
  1150. // ----- Error log
  1151. PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
  1152. // ----- Return
  1153. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1154. return PclErrorCode();
  1155. }
  1156. // ----- Check the arguments
  1157. if (sizeof($p_list) == 0)
  1158. {
  1159. // ----- Error log
  1160. PclErrorLog(-3, "Invalid file list parameter (invalid or empty list)");
  1161. // ----- Return
  1162. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1163. return PclErrorCode();
  1164. }
  1165. // ----- Loop on the files
  1166. for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++)
  1167. {
  1168. // ----- Recuperate the filename
  1169. $p_filename = $p_list[$j];
  1170. TrFctMessage(__FILE__, __LINE__, 2, "Looking for file [$p_filename]");
  1171. // ----- Skip empty file names
  1172. if ($p_filename == "")
  1173. {
  1174. TrFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
  1175. continue;
  1176. }
  1177. // ----- Check the filename
  1178. if (!file_exists($p_filename))
  1179. {
  1180. // ----- Error log
  1181. TrFctMessage(__FILE__, __LINE__, 2, "File '$p_filename' does not exists");
  1182. PclErrorLog(-4, "File '$p_filename' does not exists");
  1183. // ----- Return
  1184. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1185. return PclErrorCode();
  1186. }
  1187. // ----- Check the path length
  1188. if (strlen($p_filename) > 99)
  1189. {
  1190. // ----- Error log
  1191. PclErrorLog(-5, "File name is too long (max. 99) : '$p_filename'");
  1192. // ----- Return
  1193. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1194. return PclErrorCode();
  1195. }
  1196. TrFctMessage(__FILE__, __LINE__, 4, "File position before header =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1197. // ----- Add the file
  1198. if (($v_result = PclTarHandleAddFile($p_tar, $p_filename, $p_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
  1199. {
  1200. // ----- Return status
  1201. TrFctEnd(__FILE__, __LINE__, $v_result);
  1202. return $v_result;
  1203. }
  1204. // ----- Store the file infos
  1205. $p_list_detail[$v_nb++] = $v_header;
  1206. // ----- Look for directory
  1207. if (is_dir($p_filename))
  1208. {
  1209. TrFctMessage(__FILE__, __LINE__, 2, "$p_filename is a directory");
  1210. // ----- Look for path
  1211. if ($p_filename != ".")
  1212. $v_path = $p_filename."/";
  1213. else
  1214. $v_path = "";
  1215. // ----- Read the directory for files and sub-directories
  1216. $p_hdir = opendir($p_filename);
  1217. $p_hitem = readdir($p_hdir); // '.' directory
  1218. $p_hitem = readdir($p_hdir); // '..' directory
  1219. while ($p_hitem = readdir($p_hdir))
  1220. {
  1221. // ----- Look for a file
  1222. if (is_file($v_path.$p_hitem))
  1223. {
  1224. TrFctMessage(__FILE__, __LINE__, 4, "Add the file '".$v_path.$p_hitem."'");
  1225. // ----- Add the file
  1226. if (($v_result = PclTarHandleAddFile($p_tar, $v_path.$p_hitem, $p_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
  1227. {
  1228. // ----- Return status
  1229. TrFctEnd(__FILE__, __LINE__, $v_result);
  1230. return $v_result;
  1231. }
  1232. // ----- Store the file infos
  1233. $p_list_detail[$v_nb++] = $v_header;
  1234. }
  1235. // ----- Recursive call to PclTarHandleAddFile()
  1236. else
  1237. {
  1238. TrFctMessage(__FILE__, __LINE__, 4, "'".$v_path.$p_hitem."' is a directory");
  1239. // ----- Need an array as parameter
  1240. $p_temp_list[0] = $v_path.$p_hitem;
  1241. $v_result = PclTarHandleAddList($p_tar, $p_temp_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
  1242. }
  1243. }
  1244. // ----- Free memory for the recursive loop
  1245. unset($p_temp_list);
  1246. unset($p_hdir);
  1247. unset($p_hitem);
  1248. }
  1249. else
  1250. {
  1251. TrFctMessage(__FILE__, __LINE__, 4, "File position after blocks =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1252. }
  1253. }
  1254. // ----- Return
  1255. TrFctEnd(__FILE__, __LINE__, $v_result);
  1256. return $v_result;
  1257. }
  1258. // --------------------------------------------------------------------------------
  1259. // --------------------------------------------------------------------------------
  1260. // Function : PclTarHandleAddFile()
  1261. // Description :
  1262. // Parameters :
  1263. // Return Values :
  1264. // --------------------------------------------------------------------------------
  1265. function PclTarHandleAddFile($p_tar, $p_filename, $p_mode, &$p_header, $p_add_dir, $p_remove_dir)
  1266. {
  1267. TrFctStart(__FILE__, __LINE__, "PclTarHandleAddFile", "tar='$p_tar', filename='$p_filename', p_mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  1268. $v_result=1;
  1269. // ----- Check the parameters
  1270. if ($p_tar == 0)
  1271. {
  1272. // ----- Error log
  1273. PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
  1274. // ----- Return
  1275. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1276. return PclErrorCode();
  1277. }
  1278. // ----- Skip empty file names
  1279. if ($p_filename == "")
  1280. {
  1281. // ----- Error log
  1282. PclErrorLog(-3, "Invalid file list parameter (invalid or empty list)");
  1283. // ----- Return
  1284. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1285. return PclErrorCode();
  1286. }
  1287. // ----- Calculate the stored filename
  1288. $v_stored_filename = $p_filename;
  1289. if ($p_remove_dir != "")
  1290. {
  1291. if (substr($p_remove_dir, -1) != '/')
  1292. $p_remove_dir .= "/";
  1293. if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./"))
  1294. {
  1295. if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./"))
  1296. $p_remove_dir = "./".$p_remove_dir;
  1297. if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./"))
  1298. $p_remove_dir = substr($p_remove_dir, 2);
  1299. }
  1300. if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
  1301. {
  1302. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  1303. TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_filename' = '$v_stored_filename'");
  1304. }
  1305. }
  1306. if ($p_add_dir != "")
  1307. {
  1308. if (substr($p_add_dir, -1) == "/")
  1309. $v_stored_filename = $p_add_dir.$v_stored_filename;
  1310. else
  1311. $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  1312. TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
  1313. }
  1314. // ----- Check the path length
  1315. if (strlen($v_stored_filename) > 99)
  1316. {
  1317. // ----- Error log
  1318. PclErrorLog(-5, "Stored file name is too long (max. 99) : '$v_stored_filename'");
  1319. // ----- Return
  1320. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1321. return PclErrorCode();
  1322. }
  1323. // ----- Look for a file
  1324. if (is_file($p_filename))
  1325. {
  1326. // ----- Open the source file
  1327. if (($v_file = fopen($p_filename, "rb")) == 0)
  1328. {
  1329. // ----- Error log
  1330. PclErrorLog(-2, "Unable to open file '$p_filename' in binary read mode");
  1331. // ----- Return
  1332. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1333. return PclErrorCode();
  1334. }
  1335. // ----- Call the header generation
  1336. if (($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename)) != 1)
  1337. {
  1338. // ----- Return status
  1339. TrFctEnd(__FILE__, __LINE__, $v_result);
  1340. return $v_result;
  1341. }
  1342. TrFctMessage(__FILE__, __LINE__, 4, "File position after header =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1343. // ----- Read the file by 512 octets blocks
  1344. $i=0;
  1345. while (($v_buffer = fread($v_file, 512)) != "")
  1346. {
  1347. $v_binary_data = pack("a512", "$v_buffer");
  1348. if ($p_mode == "tar")
  1349. fputs($p_tar, $v_binary_data);
  1350. else
  1351. gzputs($p_tar, $v_binary_data);
  1352. $i++;
  1353. }
  1354. TrFctMessage(__FILE__, __LINE__, 2, "$i 512 bytes blocks");
  1355. // ----- Close the file
  1356. fclose($v_file);
  1357. TrFctMessage(__FILE__, __LINE__, 4, "File position after blocks =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1358. }
  1359. // ----- Look for a directory
  1360. else
  1361. {
  1362. // ----- Call the header generation
  1363. if (($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename)) != 1)
  1364. {
  1365. // ----- Return status
  1366. TrFctEnd(__FILE__, __LINE__, $v_result);
  1367. return $v_result;
  1368. }
  1369. TrFctMessage(__FILE__, __LINE__, 4, "File position after header =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1370. }
  1371. // ----- Return
  1372. TrFctEnd(__FILE__, __LINE__, $v_result);
  1373. return $v_result;
  1374. }
  1375. // --------------------------------------------------------------------------------
  1376. // --------------------------------------------------------------------------------
  1377. // Function : PclTarHandleHeader()
  1378. // Description :
  1379. // This function creates in the TAR $p_tar, the TAR header for the file
  1380. // $p_filename.
  1381. //
  1382. // 1. The informations needed to compose the header are recuperated and formatted
  1383. // 2. Two binary strings are composed for the first part of the header, before
  1384. // and after checksum field.
  1385. // 3. The checksum is calculated from the two binary strings
  1386. // 4. The header is write in the tar file (first binary string, binary string
  1387. // for checksum and last binary string).
  1388. // Parameters :
  1389. // $p_tar : a valid file descriptor, opened in write mode,
  1390. // $p_filename : The name of the file the header is for,
  1391. // $p_mode : The mode of the archive ("tar" or "tgz").
  1392. // $p_header : A pointer to a array where will be set the file properties
  1393. // Return Values :
  1394. // --------------------------------------------------------------------------------
  1395. function PclTarHandleHeader($p_tar, $p_filename, $p_mode, &$p_header, $p_stored_filename)
  1396. {
  1397. TrFctStart(__FILE__, __LINE__, "PclTarHandleHeader", "tar=$p_tar, file='$p_filename', mode='$p_mode', stored_filename='$p_stored_filename'");
  1398. $v_result=1;
  1399. // ----- Check the parameters
  1400. if (($p_tar == 0) || ($p_filename == ""))
  1401. {
  1402. // ----- Error log
  1403. PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
  1404. // ----- Return
  1405. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1406. return PclErrorCode();
  1407. }
  1408. // ----- Filename (reduce the path of stored name)
  1409. if ($p_stored_filename == "")
  1410. $p_stored_filename = $p_filename;
  1411. $v_reduce_filename = PclTarHandlePathReduction($p_stored_filename);
  1412. TrFctMessage(__FILE__, __LINE__, 2, "Filename (reduced) '$v_reduce_filename', strlen ".strlen($v_reduce_filename));
  1413. // ----- Get file info
  1414. $v_info = stat($p_filename);
  1415. $v_uid = sprintf("%6s ", DecOct($v_info[4]));
  1416. $v_gid = sprintf("%6s ", DecOct($v_info[5]));
  1417. TrFctMessage(__FILE__, __LINE__, 3, "uid=$v_uid, gid=$v_gid");
  1418. $v_perms = sprintf("%6s ", DecOct(fileperms($p_filename)));
  1419. TrFctMessage(__FILE__, __LINE__, 3, "file permissions $v_perms");
  1420. // ----- File mtime
  1421. $v_mtime_data = filemtime($p_filename);
  1422. TrFctMessage(__FILE__, __LINE__, 2, "File mtime : $v_mtime_data");
  1423. $v_mtime = sprintf("%11s", DecOct($v_mtime_data));
  1424. // ----- File typeflag
  1425. // '0' or '\0' is the code for regular file
  1426. // '5' is directory
  1427. if (is_dir($p_filename))
  1428. {
  1429. $v_typeflag = "5";
  1430. $v_size = 0;
  1431. }
  1432. else
  1433. {
  1434. $v_typeflag = "";
  1435. // ----- Get the file size
  1436. clearstatcache();
  1437. $v_size = filesize($p_filename);
  1438. }
  1439. TrFctMessage(__FILE__, __LINE__, 2, "File size : $v_size");
  1440. $v_size = sprintf("%11s ", DecOct($v_size));
  1441. TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : $v_typeflag");
  1442. // ----- Linkname
  1443. $v_linkname = "";
  1444. // ----- Magic
  1445. $v_magic = "";
  1446. // ----- Version
  1447. $v_version = "";
  1448. // ----- uname
  1449. $v_uname = "";
  1450. // ----- gname
  1451. $v_gname = "";
  1452. // ----- devmajor
  1453. $v_devmajor = "";
  1454. // ----- devminor
  1455. $v_devminor = "";
  1456. // ----- prefix
  1457. $v_prefix = "";
  1458. // ----- Compose the binary string of the header in two parts arround the checksum position
  1459. $v_binary_data_first = pack("a100a8a8a8a12A12", $v_reduce_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
  1460. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, "");
  1461. // ----- Calculate the checksum
  1462. $v_checksum = 0;
  1463. // ..... First part of the header
  1464. for ($i=0; $i<148; $i++)
  1465. {
  1466. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1467. }
  1468. // ..... Ignore the checksum value and replace it by ' ' (space)
  1469. for ($i=148; $i<156; $i++)
  1470. {
  1471. $v_checksum += ord(' ');
  1472. }
  1473. // ..... Last part of the header
  1474. for ($i=156, $j=0; $i<512; $i++, $j++)
  1475. {
  1476. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1477. }
  1478. TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
  1479. // ----- Write the first 148 bytes of the header in the archive
  1480. if ($p_mode == "tar")
  1481. fputs($p_tar, $v_binary_data_first, 148);
  1482. else
  1483. gzputs($p_tar, $v_binary_data_first, 148);
  1484. // ----- Write the calculated checksum
  1485. $v_checksum = sprintf("%6s ", DecOct($v_checksum));
  1486. $v_binary_data = pack("a8", $v_checksum);
  1487. if ($p_mode == "tar")
  1488. fputs($p_tar, $v_binary_data, 8);
  1489. else
  1490. gzputs($p_tar, $v_binary_data, 8);
  1491. // ----- Write the last 356 bytes of the header in the archive
  1492. if ($p_mode == "tar")
  1493. fputs($p_tar, $v_binary_data_last, 356);
  1494. else
  1495. gzputs($p_tar, $v_binary_data_last, 356);
  1496. // ----- Set the properties in the header "structure"
  1497. $p_header[filename] = $v_reduce_filename;
  1498. $p_header[mode] = $v_perms;
  1499. $p_header[uid] = $v_uid;
  1500. $p_header[gid] = $v_gid;
  1501. $p_header[size] = $v_size;
  1502. $p_header[mtime] = $v_mtime;
  1503. $p_header[typeflag] = $v_typeflag;
  1504. $p_header[status] = "added";
  1505. // ----- Return
  1506. TrFctEnd(__FILE__, __LINE__, $v_result);
  1507. return $v_result;
  1508. }
  1509. // --------------------------------------------------------------------------------
  1510. // --------------------------------------------------------------------------------
  1511. // Function : PclTarHandleFooter()
  1512. // Description :
  1513. // Parameters :
  1514. // Return Values :
  1515. // --------------------------------------------------------------------------------
  1516. function PclTarHandleFooter($p_tar, $p_mode)
  1517. {
  1518. TrFctStart(__FILE__, __LINE__, "PclTarHandleFooter", "tar='$p_tar', p_mode=$p_mode");
  1519. $v_result=1;
  1520. // ----- Write the last 0 filled block for end of archive
  1521. $v_binary_data = pack("a512", "");
  1522. if ($p_mode == "tar")
  1523. fputs($p_tar, $v_binary_data);
  1524. else
  1525. gzputs($p_tar, $v_binary_data);
  1526. // ----- Return
  1527. TrFctEnd(__FILE__, __LINE__, $v_result);
  1528. return $v_result;
  1529. }
  1530. // --------------------------------------------------------------------------------
  1531. // --------------------------------------------------------------------------------
  1532. // Function : PclTarHandleExtract()
  1533. // Description :
  1534. // Parameters :
  1535. // $p_tarname : Filename of the tar (or tgz) archive
  1536. // $p_file_list : An array which contains the list of files to extract, this
  1537. // array may be empty when $p_mode is 'complete'
  1538. // $p_list_detail : An array where will be placed the properties of each extracted/listed file
  1539. // $p_mode : 'complete' will extract all files from the archive,
  1540. // 'partial' will look for files in $p_file_list
  1541. // 'list' will only list the files from the archive without any extract
  1542. // $p_path : Path to add while writing the extracted files
  1543. // $p_tar_mode : 'tar' for GNU TAR archive, 'tgz' for compressed archive
  1544. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  1545. // extracted files. If the path does not match the file path,
  1546. // the file is extracted with its memorized path.
  1547. // $p_remove_path does not apply to 'list' mode.
  1548. // $p_path and $p_remove_path are commulative.
  1549. // Return Values :
  1550. // --------------------------------------------------------------------------------
  1551. function PclTarHandleExtract($p_tarname, $p_file_list, &$p_list_detail, $p_mode, $p_path, $p_tar_mode, $p_remove_path)
  1552. {
  1553. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtract", "archive='$p_tarname', list, mode=$p_mode, path=$p_path, tar_mode=$p_tar_mode, remove_path='$p_remove_path'");
  1554. $v_result=1;
  1555. $v_nb = 0;
  1556. $v_extract_all = TRUE;
  1557. $v_listing = FALSE;
  1558. // ----- Check the path
  1559. /*
  1560. if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../")))
  1561. $p_path = "./".$p_path;
  1562. */
  1563. $isWin = (substr(PHP_OS, 0, 3) == 'WIN');
  1564. if(!$isWin)
  1565. {
  1566. if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../")))
  1567. $p_path = "./".$p_path;
  1568. }
  1569. // ----- Look for path to remove format (should end by /)
  1570. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  1571. {
  1572. $p_remove_path .= '/';
  1573. }
  1574. $p_remove_path_size = strlen($p_remove_path);
  1575. // ----- Study the mode
  1576. switch ($p_mode) {
  1577. case "complete" :
  1578. // ----- Flag extract of all files
  1579. $v_extract_all = TRUE;
  1580. $v_listing = FALSE;
  1581. break;
  1582. case "partial" :
  1583. // ----- Flag extract of specific files
  1584. $v_extract_all = FALSE;
  1585. $v_listing = FALSE;
  1586. break;
  1587. case "list" :
  1588. // ----- Flag list of all files
  1589. $v_extract_all = FALSE;
  1590. $v_listing = TRUE;
  1591. break;
  1592. default :
  1593. // ----- Error log
  1594. PclErrorLog(-3, "Invalid extract mode ($p_mode)");
  1595. // ----- Return
  1596. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1597. return PclErrorCode();
  1598. }
  1599. // ----- Open the tar file
  1600. if ($p_tar_mode == "tar")
  1601. {
  1602. TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  1603. $v_tar = fopen($p_tarname, "rb");
  1604. }
  1605. else
  1606. {
  1607. TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode");
  1608. $v_tar = @gzopen($p_tarname, "rb");
  1609. }
  1610. // ----- Check that the archive is open
  1611. if ($v_tar == 0)
  1612. {
  1613. // ----- Error log
  1614. PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
  1615. // ----- Return
  1616. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1617. return PclErrorCode();
  1618. }
  1619. // ----- Read the blocks
  1620. While (!($v_end_of_file = ($p_tar_mode == "tar"?feof($v_tar):gzeof($v_tar))))
  1621. {
  1622. TrFctMessage(__FILE__, __LINE__, 3, "Looking for next header ...");
  1623. // ----- Clear cache of file infos
  1624. clearstatcache();
  1625. // ----- Reset extract tag
  1626. $v_extract_file = FALSE;
  1627. $v_extraction_stopped = 0;
  1628. // ----- Read the 512 bytes header
  1629. if ($p_tar_mode == "tar")
  1630. $v_binary_data = fread($v_tar, 512);
  1631. else
  1632. $v_binary_data = gzread($v_tar, 512);
  1633. // ----- Read the header properties
  1634. if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1)
  1635. {
  1636. // ----- Close the archive file
  1637. if ($p_tar_mode == "tar")
  1638. fclose($v_tar);
  1639. else
  1640. gzclose($v_tar);
  1641. // ----- Return
  1642. TrFctEnd(__FILE__, __LINE__, $v_result);
  1643. return $v_result;
  1644. }
  1645. // ----- Look for empty blocks to skip
  1646. if ($v_header["filename"] == "")
  1647. {
  1648. TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?");
  1649. continue;
  1650. }
  1651. TrFctMessage(__FILE__, __LINE__, 2, "Found file '" . $v_header["filename"] . "', size '$v_header[size]'");
  1652. // ----- Look for partial extract
  1653. if ((!$v_extract_all) && (is_array($p_file_list)))
  1654. {
  1655. TrFctMessage(__FILE__, __LINE__, 2, "Look if the file '$v_header[filename]' need to be extracted");
  1656. // ----- By default no unzip if the file is not found
  1657. $v_extract_file = FALSE;
  1658. // ----- Look into the file list
  1659. for ($i=0; $i<sizeof($p_file_list); $i++)
  1660. {
  1661. TrFctMessage(__FILE__, __LINE__, 2, "Compare archived file '$v_header[filename]' from asked list file '".$p_file_list[$i]."'");
  1662. // ----- Look if it is a directory
  1663. if (substr($p_file_list[$i], -1) == "/")
  1664. {
  1665. TrFctMessage(__FILE__, __LINE__, 3, "Compare file '$v_header[filename]' with directory '$p_file_list[$i]'");
  1666. // ----- Look if the directory is in the filename path
  1667. if ((strlen($v_header["filename"]) > strlen($p_file_list[$i])) && (substr($v_header["filename"], 0, strlen($p_file_list[$i])) == $p_file_list[$i]))
  1668. {
  1669. // ----- The file is in the directory, so extract it
  1670. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' is in directory '$p_file_list[$i]' : extract it");
  1671. $v_extract_file = TRUE;
  1672. // ----- End of loop
  1673. break;
  1674. }
  1675. }
  1676. // ----- It is a file, so compare the file names
  1677. else if ($p_file_list[$i] == $v_header["filename"])
  1678. {
  1679. // ----- File found
  1680. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' should be extracted");
  1681. $v_extract_file = TRUE;
  1682. // ----- End of loop
  1683. break;
  1684. }
  1685. }
  1686. // ----- Trace
  1687. if (!$v_extract_file)
  1688. {
  1689. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' should not be extracted");
  1690. }
  1691. }
  1692. else
  1693. {
  1694. // ----- All files need to be extracted
  1695. $v_extract_file = TRUE;
  1696. }
  1697. // ----- Look if this file need to be extracted
  1698. if (($v_extract_file) && (!$v_listing))
  1699. {
  1700. // ----- Look for path to remove
  1701. if (($p_remove_path != "")
  1702. && (substr($v_header["filename"], 0, $p_remove_path_size) == $p_remove_path))
  1703. {
  1704. TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '$v_header[filename]'");
  1705. // ----- Remove the path
  1706. $v_header["filename"] = substr($v_header["filename"], $p_remove_path_size);
  1707. TrFctMessage(__FILE__, __LINE__, 3, "Reslting file is '$v_header[filename]'");
  1708. }
  1709. // ----- Add the path to the file
  1710. if (($p_path != "./") && ($p_path != "/"))
  1711. {
  1712. // ----- Look for the path end '/'
  1713. while (substr($p_path, -1) == "/")
  1714. {
  1715. TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
  1716. $p_path = substr($p_path, 0, strlen($p_path)-1);
  1717. TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
  1718. }
  1719. // ----- Add the path
  1720. if (substr($v_header["filename"], 0, 1) == "/")
  1721. $v_header["filename"] = $p_path.$v_header["filename"];
  1722. else
  1723. $v_header["filename"] = $p_path."/".$v_header["filename"];
  1724. }
  1725. // ----- Trace
  1726. TrFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '$v_header[filename]', size '$v_header[size]'");
  1727. // ----- Check that the file does not exists
  1728. if (file_exists($v_header["filename"]))
  1729. {
  1730. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' already exists");
  1731. // ----- Look if file is a directory
  1732. if (is_dir($v_header["filename"]))
  1733. {
  1734. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is a directory");
  1735. // ----- Change the file status
  1736. $v_header["status"] = "already_a_directory";
  1737. // ----- Skip the extract
  1738. $v_extraction_stopped = 1;
  1739. $v_extract_file = 0;
  1740. }
  1741. // ----- Look if file is write protected
  1742. else if (!is_writeable($v_header["filename"]))
  1743. {
  1744. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is write protected");
  1745. // ----- Change the file status
  1746. $v_header["status"] = "write_protected";
  1747. // ----- Skip the extract
  1748. $v_extraction_stopped = 1;
  1749. $v_extract_file = 0;
  1750. }
  1751. // ----- Look if the extracted file is older
  1752. else if (filemtime($v_header["filename"]) > $v_header["mtime"])
  1753. {
  1754. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is newer (".date("l dS of F Y h:i:s A", filemtime($v_header[filename])).") than the extracted file (".date("l dS of F Y h:i:s A", $v_header[mtime]).")");
  1755. // ----- Change the file status
  1756. $v_header["status"] = "newer_exist";
  1757. // ----- Skip the extract
  1758. $v_extraction_stopped = 1;
  1759. $v_extract_file = 0;
  1760. }
  1761. }
  1762. // ----- Check the directory availability and create it if necessary
  1763. else
  1764. {
  1765. if ($v_header["typeflag"]=="5")
  1766. $v_dir_to_check = $v_header["filename"];
  1767. else if (!strstr($v_header["filename"], "/"))
  1768. $v_dir_to_check = "";
  1769. else
  1770. $v_dir_to_check = dirname($v_header["filename"]);
  1771. if (($v_result = PclTarHandlerDirCheck($v_dir_to_check)) != 1)
  1772. {
  1773. TrFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '$v_header[filename]'");
  1774. // ----- Change the file status
  1775. $v_header["status"] = "path_creation_fail";
  1776. // ----- Skip the extract
  1777. $v_extraction_stopped = 1;
  1778. $v_extract_file = 0;
  1779. }
  1780. }
  1781. // ----- Do the extraction
  1782. if (($v_extract_file) && ($v_header["typeflag"]!="5"))
  1783. {
  1784. // ----- Open the destination file in write mode
  1785. if (($v_dest_file = @fopen($v_header["filename"], "wb")) == 0)
  1786. {
  1787. TrFctMessage(__FILE__, __LINE__, 2, "Error while opening '$v_header[filename]' in write binary mode");
  1788. // ----- Change the file status
  1789. $v_header["status"] = "write_error";
  1790. // ----- Jump to next file
  1791. TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file");
  1792. if ($p_tar_mode == "tar")
  1793. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  1794. else
  1795. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  1796. }
  1797. else
  1798. {
  1799. TrFctMessage(__FILE__, __LINE__, 2, "Start extraction of '$v_header[filename]'");
  1800. // ----- Read data
  1801. $n = floor($v_header["size"]/512);
  1802. for ($i=0; $i<$n; $i++)
  1803. {
  1804. TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number ".($i+1));
  1805. if ($p_tar_mode == "tar")
  1806. $v_content = fread($v_tar, 512);
  1807. else
  1808. $v_content = gzread($v_tar, 512);
  1809. fwrite($v_dest_file, $v_content, 512);
  1810. }
  1811. if (($v_header["size"] % 512) != 0)
  1812. {
  1813. TrFctMessage(__FILE__, __LINE__, 3, "Read last ".($v_header["size"] % 512)." bytes in a 512 block");
  1814. if ($p_tar_mode == "tar")
  1815. $v_content = fread($v_tar, 512);
  1816. else
  1817. $v_content = gzread($v_tar, 512);
  1818. fwrite($v_dest_file, $v_content, ($v_header["size"] % 512));
  1819. }
  1820. // ----- Close the destination file
  1821. fclose($v_dest_file);
  1822. // ----- Change the file mode, mtime
  1823. touch($v_header["filename"], $v_header["mtime"]);
  1824. //chmod($v_header[filename], DecOct($v_header[mode]));
  1825. }
  1826. // ----- Check the file size
  1827. clearstatcache();
  1828. if (filesize($v_header["filename"]) != $v_header["size"])
  1829. {
  1830. // ----- Close the archive file
  1831. if ($p_tar_mode == "tar")
  1832. fclose($v_tar);
  1833. else
  1834. gzclose($v_tar);
  1835. // ----- Error log
  1836. PclErrorLog(-7, "Extracted file '$v_header[filename]' does not have the correct file size '".filesize($v_filename)."' ('$v_header[size]' expected). Archive may be corrupted.");
  1837. // ----- Return
  1838. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1839. return PclErrorCode();
  1840. }
  1841. // ----- Trace
  1842. TrFctMessage(__FILE__, __LINE__, 2, "Extraction done");
  1843. }
  1844. else
  1845. {
  1846. TrFctMessage(__FILE__, __LINE__, 2, "Extraction of file '$v_header[filename]' skipped.");
  1847. // ----- Jump to next file
  1848. TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file");
  1849. if ($p_tar_mode == "tar")
  1850. fseek($v_tar, ftell($v_tar)+(ceil(($v_header["size"]/512))*512));
  1851. else
  1852. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header["size"]/512))*512));
  1853. }
  1854. }
  1855. // ----- Look for file that is not to be unzipped
  1856. else
  1857. {
  1858. // ----- Trace
  1859. TrFctMessage(__FILE__, __LINE__, 2, "Jump file '$v_header[filename]'");
  1860. TrFctMessage(__FILE__, __LINE__, 4, "Position avant jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  1861. // ----- Jump to next file
  1862. if ($p_tar_mode == "tar")
  1863. fseek($v_tar, ($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))+(ceil(($v_header[size]/512))*512));
  1864. else
  1865. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  1866. TrFctMessage(__FILE__, __LINE__, 4, "Position apr�s jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  1867. }
  1868. if ($p_tar_mode == "tar")
  1869. $v_end_of_file = feof($v_tar);
  1870. else
  1871. $v_end_of_file = gzeof($v_tar);
  1872. // ----- File name and properties are logged if listing mode or file is extracted
  1873. if ($v_listing || $v_extract_file || $v_extraction_stopped)
  1874. {
  1875. TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '$v_header[filename]'");
  1876. // ----- Log extracted files
  1877. if (($v_file_dir = dirname($v_header["filename"])) == $v_header["filename"])
  1878. $v_file_dir = "";
  1879. if ((substr($v_header["filename"], 0, 1) == "/") && ($v_file_dir == ""))
  1880. $v_file_dir = "/";
  1881. // ----- Add the array describing the file into the list
  1882. $p_list_detail[$v_nb] = $v_header;
  1883. // ----- Increment
  1884. $v_nb++;
  1885. }
  1886. }
  1887. // ----- Close the tarfile
  1888. if ($p_tar_mode == "tar")
  1889. fclose($v_tar);
  1890. else
  1891. gzclose($v_tar);
  1892. // ----- Return
  1893. TrFctEnd(__FILE__, __LINE__, $v_result);
  1894. return $v_result;
  1895. }
  1896. // --------------------------------------------------------------------------------
  1897. // --------------------------------------------------------------------------------
  1898. // Function : PclTarHandleExtractByIndexList()
  1899. // Description :
  1900. // Extract the files which are at the indexes specified. If the 'file' at the
  1901. // index is a directory, the directory only is created, not all the files stored
  1902. // for that directory.
  1903. // Parameters :
  1904. // $p_index_string : String of indexes of files to extract. The form of the
  1905. // string is "0,4-6,8-12" with only numbers and '-' for
  1906. // for range, and ',' to separate ranges. No spaces or ';'
  1907. // are allowed.
  1908. // Return Values :
  1909. // --------------------------------------------------------------------------------
  1910. function PclTarHandleExtractByIndexList($p_tarname, $p_index_string, &$p_list_detail, $p_path, $p_remove_path, $p_tar_mode)
  1911. {
  1912. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtractByIndexList", "archive='$p_tarname', index_string='$p_index_string', list, path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
  1913. $v_result=1;
  1914. $v_nb = 0;
  1915. // ----- TBC : I should check the string by a regexp
  1916. // ----- Check the path
  1917. if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path, 0, 2) != "./")))
  1918. $p_path = "./".$p_path;
  1919. // ----- Look for path to remove format (should end by /)
  1920. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  1921. {
  1922. $p_remove_path .= '/';
  1923. }
  1924. $p_remove_path_size = strlen($p_remove_path);
  1925. // ----- Open the tar file
  1926. if ($p_tar_mode == "tar")
  1927. {
  1928. TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  1929. $v_tar = @fopen($p_tarname, "rb");
  1930. }
  1931. else
  1932. {
  1933. TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode");
  1934. $v_tar = @gzopen($p_tarname, "rb");
  1935. }
  1936. // ----- Check that the archive is open
  1937. if ($v_tar == 0)
  1938. {
  1939. // ----- Error log
  1940. PclErrorLog(-2, "Unable to open archive '$p_tarname' in binary read mode");
  1941. // ----- Return
  1942. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1943. return PclErrorCode();
  1944. }
  1945. // ----- Manipulate the index list
  1946. $v_list = explode(",", $p_index_string);
  1947. sort($v_list);
  1948. // ----- Loop on the index list
  1949. $v_index=0;
  1950. for ($i=0; ($i<sizeof($v_list)) && ($v_result); $i++)
  1951. {
  1952. TrFctMessage(__FILE__, __LINE__, 3, "Looking for index part '$v_list[$i]'");
  1953. // ----- Extract range
  1954. $v_index_list = explode("-", $v_list[$i]);
  1955. $v_size_index_list = sizeof($v_index_list);
  1956. if ($v_size_index_list == 1)
  1957. {
  1958. TrFctMessage(__FILE__, __LINE__, 3, "Only one index '$v_index_list[0]'");
  1959. // ----- Do the extraction
  1960. $v_result = PclTarHandleExtractByIndex($v_tar, $v_index, $v_index_list[0], $v_index_list[0], $p_list_detail, $p_path, $p_remove_path, $p_tar_mode);
  1961. }
  1962. else if ($v_size_index_list == 2)
  1963. {
  1964. TrFctMessage(__FILE__, __LINE__, 3, "Two indexes '$v_index_list[0]' and '$v_index_list[1]'");
  1965. // ----- Do the extraction
  1966. $v_result = PclTarHandleExtractByIndex($v_tar, $v_index, $v_index_list[0], $v_index_list[1], $p_list_detail, $p_path, $p_remove_path, $p_tar_mode);
  1967. }
  1968. }
  1969. // ----- Close the tarfile
  1970. if ($p_tar_mode == "tar")
  1971. fclose($v_tar);
  1972. else
  1973. gzclose($v_tar);
  1974. // ----- Return
  1975. TrFctEnd(__FILE__, __LINE__, $v_result);
  1976. return $v_result;
  1977. }
  1978. // --------------------------------------------------------------------------------
  1979. // --------------------------------------------------------------------------------
  1980. // Function : PclTarHandleExtractByIndex()
  1981. // Description :
  1982. // Parameters :
  1983. // Return Values :
  1984. // --------------------------------------------------------------------------------
  1985. function PclTarHandleExtractByIndex($p_tar, &$p_index_current, $p_index_start, $p_index_stop, &$p_list_detail, $p_path, $p_remove_path, $p_tar_mode)
  1986. {
  1987. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtractByIndex", "archive_descr='$p_tar', index_current=$p_index_current, index_start='$p_index_start', index_stop='$p_index_stop', list, path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
  1988. $v_result=1;
  1989. $v_nb = 0;
  1990. // TBC : I should replace all $v_tar by $p_tar in this function ....
  1991. $v_tar = $p_tar;
  1992. // ----- Look the number of elements already in $p_list_detail
  1993. $v_nb = sizeof($p_list_detail);
  1994. // ----- Read the blocks
  1995. While (!($v_end_of_file = ($p_tar_mode == "tar"?feof($v_tar):gzeof($v_tar))))
  1996. {
  1997. TrFctMessage(__FILE__, __LINE__, 3, "Looking for next file ...");
  1998. TrFctMessage(__FILE__, __LINE__, 3, "Index current=$p_index_current, range=[$p_index_start, $p_index_stop])");
  1999. if ($p_index_current > $p_index_stop)
  2000. {
  2001. TrFctMessage(__FILE__, __LINE__, 2, "Stop extraction, past stop index");
  2002. break;
  2003. }
  2004. // ----- Clear cache of file infos
  2005. clearstatcache();
  2006. // ----- Reset extract tag
  2007. $v_extract_file = FALSE;
  2008. $v_extraction_stopped = 0;
  2009. // ----- Read the 512 bytes header
  2010. if ($p_tar_mode == "tar")
  2011. $v_binary_data = fread($v_tar, 512);
  2012. else
  2013. $v_binary_data = gzread($v_tar, 512);
  2014. // ----- Read the header properties
  2015. if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1)
  2016. {
  2017. // ----- Return
  2018. TrFctEnd(__FILE__, __LINE__, $v_result);
  2019. return $v_result;
  2020. }
  2021. // ----- Look for empty blocks to skip
  2022. if ($v_header[filename] == "")
  2023. {
  2024. TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?");
  2025. continue;
  2026. }
  2027. TrFctMessage(__FILE__, __LINE__, 2, "Found file '$v_header[filename]', size '$v_header[size]'");
  2028. // ----- Look if file is in the range to be extracted
  2029. if (($p_index_current >= $p_index_start) && ($p_index_current <= $p_index_stop))
  2030. {
  2031. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' is in the range to be extracted");
  2032. $v_extract_file = TRUE;
  2033. }
  2034. else
  2035. {
  2036. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' is out of the range");
  2037. $v_extract_file = FALSE;
  2038. }
  2039. // ----- Look if this file need to be extracted
  2040. if ($v_extract_file)
  2041. {
  2042. if (($v_result = PclTarHandleExtractFile($v_tar, $v_header, $p_path, $p_remove_path, $p_tar_mode)) != 1)
  2043. {
  2044. // ----- Return
  2045. TrFctEnd(__FILE__, __LINE__, $v_result);
  2046. return $v_result;
  2047. }
  2048. }
  2049. // ----- Look for file that is not to be extracted
  2050. else
  2051. {
  2052. // ----- Trace
  2053. TrFctMessage(__FILE__, __LINE__, 2, "Jump file '$v_header[filename]'");
  2054. TrFctMessage(__FILE__, __LINE__, 4, "Position avant jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  2055. // ----- Jump to next file
  2056. if ($p_tar_mode == "tar")
  2057. fseek($v_tar, ($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))+(ceil(($v_header[size]/512))*512));
  2058. else
  2059. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  2060. TrFctMessage(__FILE__, __LINE__, 4, "Position apr�s jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  2061. }
  2062. if ($p_tar_mode == "tar")
  2063. $v_end_of_file = feof($v_tar);
  2064. else
  2065. $v_end_of_file = gzeof($v_tar);
  2066. // ----- File name and properties are logged if listing mode or file is extracted
  2067. if ($v_extract_file)
  2068. {
  2069. TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '$v_header[filename]'");
  2070. // ----- Log extracted files
  2071. if (($v_file_dir = dirname($v_header[filename])) == $v_header[filename])
  2072. $v_file_dir = "";
  2073. if ((substr($v_header[filename], 0, 1) == "/") && ($v_file_dir == ""))
  2074. $v_file_dir = "/";
  2075. // ----- Add the array describing the file into the list
  2076. $p_list_detail[$v_nb] = $v_header;
  2077. // ----- Increment
  2078. $v_nb++;
  2079. }
  2080. // ----- Increment the current file index
  2081. $p_index_current++;
  2082. }
  2083. // ----- Return
  2084. TrFctEnd(__FILE__, __LINE__, $v_result);
  2085. return $v_result;
  2086. }
  2087. // --------------------------------------------------------------------------------
  2088. // --------------------------------------------------------------------------------
  2089. // Function : PclTarHandleExtractFile()
  2090. // Description :
  2091. // Parameters :
  2092. // Return Values :
  2093. // --------------------------------------------------------------------------------
  2094. function PclTarHandleExtractFile($p_tar, &$v_header, $p_path, $p_remove_path, $p_tar_mode)
  2095. {
  2096. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtractFile", "archive_descr='$p_tar', path=$p_path, remove_path='$p_remove_path', tar_mode=$p_tar_mode");
  2097. $v_result=1;
  2098. // TBC : I should replace all $v_tar by $p_tar in this function ....
  2099. $v_tar = $p_tar;
  2100. $v_extract_file = 1;
  2101. $p_remove_path_size = strlen($p_remove_path);
  2102. // ----- Look for path to remove
  2103. if (($p_remove_path != "")
  2104. && (substr($v_header[filename], 0, $p_remove_path_size) == $p_remove_path))
  2105. {
  2106. TrFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '$v_header[filename]'");
  2107. // ----- Remove the path
  2108. $v_header[filename] = substr($v_header[filename], $p_remove_path_size);
  2109. TrFctMessage(__FILE__, __LINE__, 3, "Resulting file is '$v_header[filename]'");
  2110. }
  2111. // ----- Add the path to the file
  2112. if (($p_path != "./") && ($p_path != "/"))
  2113. {
  2114. // ----- Look for the path end '/'
  2115. while (substr($p_path, -1) == "/")
  2116. {
  2117. TrFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
  2118. $p_path = substr($p_path, 0, strlen($p_path)-1);
  2119. TrFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
  2120. }
  2121. // ----- Add the path
  2122. if (substr($v_header[filename], 0, 1) == "/")
  2123. $v_header[filename] = $p_path.$v_header[filename];
  2124. else
  2125. $v_header[filename] = $p_path."/".$v_header[filename];
  2126. }
  2127. // ----- Trace
  2128. TrFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '$v_header[filename]', size '$v_header[size]'");
  2129. // ----- Check that the file does not exists
  2130. if (file_exists($v_header[filename]))
  2131. {
  2132. TrFctMessage(__FILE__, __LINE__, 2, "File '$v_header[filename]' already exists");
  2133. // ----- Look if file is a directory
  2134. if (is_dir($v_header[filename]))
  2135. {
  2136. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is a directory");
  2137. // ----- Change the file status
  2138. $v_header[status] = "already_a_directory";
  2139. // ----- Skip the extract
  2140. $v_extraction_stopped = 1;
  2141. $v_extract_file = 0;
  2142. }
  2143. // ----- Look if file is write protected
  2144. else if (!is_writeable($v_header[filename]))
  2145. {
  2146. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is write protected");
  2147. // ----- Change the file status
  2148. $v_header[status] = "write_protected";
  2149. // ----- Skip the extract
  2150. $v_extraction_stopped = 1;
  2151. $v_extract_file = 0;
  2152. }
  2153. // ----- Look if the extracted file is older
  2154. else if (filemtime($v_header[filename]) > $v_header[mtime])
  2155. {
  2156. TrFctMessage(__FILE__, __LINE__, 2, "Existing file '$v_header[filename]' is newer (".date("l dS of F Y h:i:s A", filemtime($v_header[filename])).") than the extracted file (".date("l dS of F Y h:i:s A", $v_header[mtime]).")");
  2157. // ----- Change the file status
  2158. $v_header[status] = "newer_exist";
  2159. // ----- Skip the extract
  2160. $v_extraction_stopped = 1;
  2161. $v_extract_file = 0;
  2162. }
  2163. }
  2164. // ----- Check the directory availability and create it if necessary
  2165. else
  2166. {
  2167. if ($v_header[typeflag]=="5")
  2168. $v_dir_to_check = $v_header[filename];
  2169. else if (!strstr($v_header[filename], "/"))
  2170. $v_dir_to_check = "";
  2171. else
  2172. $v_dir_to_check = dirname($v_header[filename]);
  2173. if (($v_result = PclTarHandlerDirCheck($v_dir_to_check)) != 1)
  2174. {
  2175. TrFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '$v_header[filename]'");
  2176. // ----- Change the file status
  2177. $v_header[status] = "path_creation_fail";
  2178. // ----- Skip the extract
  2179. $v_extraction_stopped = 1;
  2180. $v_extract_file = 0;
  2181. }
  2182. }
  2183. // ----- Do the real bytes extraction (if not a directory)
  2184. if (($v_extract_file) && ($v_header[typeflag]!="5"))
  2185. {
  2186. // ----- Open the destination file in write mode
  2187. if (($v_dest_file = @fopen($v_header[filename], "wb")) == 0)
  2188. {
  2189. TrFctMessage(__FILE__, __LINE__, 2, "Error while opening '$v_header[filename]' in write binary mode");
  2190. // ----- Change the file status
  2191. $v_header[status] = "write_error";
  2192. // ----- Jump to next file
  2193. TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file");
  2194. if ($p_tar_mode == "tar")
  2195. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  2196. else
  2197. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  2198. }
  2199. else
  2200. {
  2201. TrFctMessage(__FILE__, __LINE__, 2, "Start extraction of '$v_header[filename]'");
  2202. // ----- Read data
  2203. $n = floor($v_header[size]/512);
  2204. for ($i=0; $i<$n; $i++)
  2205. {
  2206. TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number ".($i+1));
  2207. if ($p_tar_mode == "tar")
  2208. $v_content = fread($v_tar, 512);
  2209. else
  2210. $v_content = gzread($v_tar, 512);
  2211. fwrite($v_dest_file, $v_content, 512);
  2212. }
  2213. if (($v_header[size] % 512) != 0)
  2214. {
  2215. TrFctMessage(__FILE__, __LINE__, 3, "Read last ".($v_header[size] % 512)." bytes in a 512 block");
  2216. if ($p_tar_mode == "tar")
  2217. $v_content = fread($v_tar, 512);
  2218. else
  2219. $v_content = gzread($v_tar, 512);
  2220. fwrite($v_dest_file, $v_content, ($v_header[size] % 512));
  2221. }
  2222. // ----- Close the destination file
  2223. fclose($v_dest_file);
  2224. // ----- Change the file mode, mtime
  2225. touch($v_header[filename], $v_header[mtime]);
  2226. //chmod($v_header[filename], DecOct($v_header[mode]));
  2227. }
  2228. // ----- Check the file size
  2229. clearstatcache();
  2230. if (filesize($v_header[filename]) != $v_header[size])
  2231. {
  2232. // ----- Error log
  2233. PclErrorLog(-7, "Extracted file '$v_header[filename]' does not have the correct file size '".filesize($v_filename)."' ('$v_header[size]' expected). Archive may be corrupted.");
  2234. // ----- Return
  2235. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2236. return PclErrorCode();
  2237. }
  2238. // ----- Trace
  2239. TrFctMessage(__FILE__, __LINE__, 2, "Extraction done");
  2240. }
  2241. else
  2242. {
  2243. TrFctMessage(__FILE__, __LINE__, 2, "Extraction of file '$v_header[filename]' skipped.");
  2244. // ----- Jump to next file
  2245. TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file");
  2246. if ($p_tar_mode == "tar")
  2247. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  2248. else
  2249. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  2250. }
  2251. // ----- Return
  2252. TrFctEnd(__FILE__, __LINE__, $v_result);
  2253. return $v_result;
  2254. }
  2255. // --------------------------------------------------------------------------------
  2256. // --------------------------------------------------------------------------------
  2257. // Function : PclTarHandleDelete()
  2258. // Description :
  2259. // Parameters :
  2260. // Return Values :
  2261. // --------------------------------------------------------------------------------
  2262. function PclTarHandleDelete($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode)
  2263. {
  2264. TrFctStart(__FILE__, __LINE__, "PclTarHandleDelete", "archive='$p_tarname', list, tar_mode=$p_tar_mode");
  2265. $v_result=1;
  2266. $v_nb=0;
  2267. // ----- Look for regular tar file
  2268. if ($p_tar_mode == "tar")
  2269. {
  2270. // ----- Open file
  2271. TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  2272. if (($v_tar = @fopen($p_tarname, "rb")) == 0)
  2273. {
  2274. // ----- Error log
  2275. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  2276. // ----- Return
  2277. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2278. return PclErrorCode();
  2279. }
  2280. // ----- Open a temporary file in write mode
  2281. $v_temp_tarname = uniqid("pcltar-").".tmp";
  2282. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  2283. if (($v_temp_tar = @fopen($v_temp_tarname, "wb")) == 0)
  2284. {
  2285. // ----- Close tar file
  2286. fclose($v_tar);
  2287. // ----- Error log
  2288. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  2289. // ----- Return
  2290. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2291. return PclErrorCode();
  2292. }
  2293. }
  2294. // ----- Look for compressed tar file
  2295. else
  2296. {
  2297. // ----- Open the file in read mode
  2298. TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode");
  2299. if (($v_tar = @gzopen($p_tarname, "rb")) == 0)
  2300. {
  2301. // ----- Error log
  2302. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  2303. // ----- Return
  2304. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2305. return PclErrorCode();
  2306. }
  2307. // ----- Open a temporary file in write mode
  2308. $v_temp_tarname = uniqid("pcltar-").".tmp";
  2309. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  2310. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  2311. {
  2312. // ----- Close tar file
  2313. gzclose($v_tar);
  2314. // ----- Error log
  2315. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  2316. // ----- Return
  2317. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2318. return PclErrorCode();
  2319. }
  2320. }
  2321. // ----- Read the blocks
  2322. While (!($v_end_of_file = ($p_tar_mode == "tar"?feof($v_tar):gzeof($v_tar))))
  2323. {
  2324. TrFctMessage(__FILE__, __LINE__, 3, "Looking for next header ...");
  2325. // ----- Clear cache of file infos
  2326. clearstatcache();
  2327. // ----- Reset delete tag
  2328. $v_delete_file = FALSE;
  2329. // ----- Read the first 512 block header
  2330. if ($p_tar_mode == "tar")
  2331. $v_binary_data = fread($v_tar, 512);
  2332. else
  2333. $v_binary_data = gzread($v_tar, 512);
  2334. // ----- Read the header properties
  2335. if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1)
  2336. {
  2337. // ----- Close the archive file
  2338. if ($p_tar_mode == "tar")
  2339. {
  2340. fclose($v_tar);
  2341. fclose($v_temp_tar);
  2342. }
  2343. else
  2344. {
  2345. gzclose($v_tar);
  2346. gzclose($v_temp_tar);
  2347. }
  2348. @unlink($v_temp_tarname);
  2349. // ----- Return
  2350. TrFctEnd(__FILE__, __LINE__, $v_result);
  2351. return $v_result;
  2352. }
  2353. // ----- Look for empty blocks to skip
  2354. if ($v_header[filename] == "")
  2355. {
  2356. TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?");
  2357. continue;
  2358. }
  2359. TrFctMessage(__FILE__, __LINE__, 2, "Found file '$v_header[filename]', size '$v_header[size]'");
  2360. // ----- Look for filenames to delete
  2361. for ($i=0, $v_delete_file=FALSE; ($i<sizeof($p_file_list)) && (!$v_delete_file); $i++)
  2362. {
  2363. // ----- Compare the file names
  2364. // if ($p_file_list[$i] == $v_header[filename])
  2365. if (($v_len = strcmp($p_file_list[$i], $v_header[filename])) <= 0)
  2366. {
  2367. if ($v_len==0)
  2368. {
  2369. TrFctMessage(__FILE__, __LINE__, 3, "Found that '$v_header[filename]' need to be deleted");
  2370. $v_delete_file = TRUE;
  2371. }
  2372. else
  2373. {
  2374. TrFctMessage(__FILE__, __LINE__, 3, "Look if '$v_header[filename]' is a file in $p_file_list[$i]");
  2375. if (substr($v_header[filename], strlen($p_file_list[$i]), 1) == "/")
  2376. {
  2377. TrFctMessage(__FILE__, __LINE__, 3, "'$v_header[filename]' is a file in $p_file_list[$i]");
  2378. $v_delete_file = TRUE;
  2379. }
  2380. }
  2381. }
  2382. }
  2383. // ----- Copy files that do not need to be deleted
  2384. if (!$v_delete_file)
  2385. {
  2386. TrFctMessage(__FILE__, __LINE__, 2, "Keep file '$v_header[filename]'");
  2387. // ----- Write the file header
  2388. if ($p_tar_mode == "tar")
  2389. {
  2390. fputs($v_temp_tar, $v_binary_data, 512);
  2391. }
  2392. else
  2393. {
  2394. gzputs($v_temp_tar, $v_binary_data, 512);
  2395. }
  2396. // ----- Write the file data
  2397. $n = ceil($v_header[size]/512);
  2398. for ($i=0; $i<$n; $i++)
  2399. {
  2400. TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number ".($i+1));
  2401. if ($p_tar_mode == "tar")
  2402. {
  2403. $v_content = fread($v_tar, 512);
  2404. fwrite($v_temp_tar, $v_content, 512);
  2405. }
  2406. else
  2407. {
  2408. $v_content = gzread($v_tar, 512);
  2409. gzwrite($v_temp_tar, $v_content, 512);
  2410. }
  2411. }
  2412. // ----- File name and properties are logged if listing mode or file is extracted
  2413. TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '$v_header[filename]'");
  2414. // ----- Add the array describing the file into the list
  2415. $p_list_detail[$v_nb] = $v_header;
  2416. $p_list_detail[$v_nb][status] = "ok";
  2417. // ----- Increment
  2418. $v_nb++;
  2419. }
  2420. // ----- Look for file that is to be deleted
  2421. else
  2422. {
  2423. // ----- Trace
  2424. TrFctMessage(__FILE__, __LINE__, 2, "Start deletion of '$v_header[filename]'");
  2425. TrFctMessage(__FILE__, __LINE__, 4, "Position avant jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  2426. // ----- Jump to next file
  2427. if ($p_tar_mode == "tar")
  2428. fseek($v_tar, ftell($v_tar)+(ceil(($v_header[size]/512))*512));
  2429. else
  2430. gzseek($v_tar, gztell($v_tar)+(ceil(($v_header[size]/512))*512));
  2431. TrFctMessage(__FILE__, __LINE__, 4, "Position apr�s jump [".($p_tar_mode=="tar"?ftell($v_tar):gztell($v_tar))."]");
  2432. }
  2433. // ----- Look for end of file
  2434. if ($p_tar_mode == "tar")
  2435. $v_end_of_file = feof($v_tar);
  2436. else
  2437. $v_end_of_file = gzeof($v_tar);
  2438. }
  2439. // ----- Write the last empty buffer
  2440. PclTarHandleFooter($v_temp_tar, $p_tar_mode);
  2441. // ----- Close the tarfile
  2442. if ($p_tar_mode == "tar")
  2443. {
  2444. fclose($v_tar);
  2445. fclose($v_temp_tar);
  2446. }
  2447. else
  2448. {
  2449. gzclose($v_tar);
  2450. gzclose($v_temp_tar);
  2451. }
  2452. // ----- Unlink tar file
  2453. if (!@unlink($p_tarname))
  2454. {
  2455. // ----- Error log
  2456. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  2457. }
  2458. // ----- Rename tar file
  2459. if (!@rename($v_temp_tarname, $p_tarname))
  2460. {
  2461. // ----- Error log
  2462. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  2463. // ----- Return
  2464. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2465. return PclErrorCode();
  2466. }
  2467. // ----- Return
  2468. TrFctEnd(__FILE__, __LINE__, $v_result);
  2469. return $v_result;
  2470. }
  2471. // --------------------------------------------------------------------------------
  2472. // --------------------------------------------------------------------------------
  2473. // Function : PclTarHandleUpdate()
  2474. // Description :
  2475. // Parameters :
  2476. // Return Values :
  2477. // --------------------------------------------------------------------------------
  2478. function PclTarHandleUpdate($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode, $p_add_dir, $p_remove_dir)
  2479. {
  2480. TrFctStart(__FILE__, __LINE__, "PclTarHandleUpdate", "archive='$p_tarname', list, tar_mode=$p_tar_mode");
  2481. $v_result=1;
  2482. $v_nb=0;
  2483. $v_found_list = array();
  2484. // ----- Look for regular tar file
  2485. if ($p_tar_mode == "tar")
  2486. {
  2487. // ----- Open file
  2488. TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  2489. if (($v_tar = @fopen($p_tarname, "rb")) == 0)
  2490. {
  2491. // ----- Error log
  2492. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  2493. // ----- Return
  2494. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2495. return PclErrorCode();
  2496. }
  2497. // ----- Open a temporary file in write mode
  2498. $v_temp_tarname = uniqid("pcltar-").".tmp";
  2499. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  2500. if (($v_temp_tar = @fopen($v_temp_tarname, "wb")) == 0)
  2501. {
  2502. // ----- Close tar file
  2503. fclose($v_tar);
  2504. // ----- Error log
  2505. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  2506. // ----- Return
  2507. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2508. return PclErrorCode();
  2509. }
  2510. }
  2511. // ----- Look for compressed tar file
  2512. else
  2513. {
  2514. // ----- Open the file in read mode
  2515. TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode");
  2516. if (($v_tar = @gzopen($p_tarname, "rb")) == 0)
  2517. {
  2518. // ----- Error log
  2519. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  2520. // ----- Return
  2521. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2522. return PclErrorCode();
  2523. }
  2524. // ----- Open a temporary file in write mode
  2525. $v_temp_tarname = uniqid("pcltar-").".tmp";
  2526. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  2527. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  2528. {
  2529. // ----- Close tar file
  2530. gzclose($v_tar);
  2531. // ----- Error log
  2532. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  2533. // ----- Return
  2534. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2535. return PclErrorCode();
  2536. }
  2537. }
  2538. // ----- Prepare the list of files
  2539. for ($i=0; $i<sizeof($p_file_list); $i++)
  2540. {
  2541. // ----- Reset the found list
  2542. $v_found_list[$i] = 0;
  2543. // ----- Calculate the stored filename
  2544. $v_stored_list[$i] = $p_file_list[$i];
  2545. if ($p_remove_dir != "")
  2546. {
  2547. if (substr($p_file_list[$i], -1) != '/')
  2548. $p_remove_dir .= "/";
  2549. if (substr($p_file_list[$i], 0, strlen($p_remove_dir)) == $p_remove_dir)
  2550. {
  2551. $v_stored_list[$i] = substr($p_file_list[$i], strlen($p_remove_dir));
  2552. TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
  2553. }
  2554. }
  2555. if ($p_add_dir != "")
  2556. {
  2557. if (substr($p_add_dir, -1) == "/")
  2558. $v_stored_list[$i] = $p_add_dir.$v_stored_list[$i];
  2559. else
  2560. $v_stored_list[$i] = $p_add_dir."/".$v_stored_list[$i];
  2561. TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_file_list[$i]' = '$v_stored_list[$i]'");
  2562. }
  2563. $v_stored_list[$i] = PclTarHandlePathReduction($v_stored_list[$i]);
  2564. TrFctMessage(__FILE__, __LINE__, 3, "After reduction '$v_stored_list[$i]'");
  2565. }
  2566. // ----- Update file cache
  2567. clearstatcache();
  2568. // ----- Read the blocks
  2569. While (!($v_end_of_file = ($p_tar_mode == "tar"?feof($v_tar):gzeof($v_tar))))
  2570. {
  2571. TrFctMessage(__FILE__, __LINE__, 3, "Looking for next header ...");
  2572. // ----- Clear cache of file infos
  2573. clearstatcache();
  2574. // ----- Reset current found filename
  2575. $v_current_filename = "";
  2576. // ----- Reset delete tag
  2577. $v_delete_file = FALSE;
  2578. // ----- Read the first 512 block header
  2579. if ($p_tar_mode == "tar")
  2580. $v_binary_data = fread($v_tar, 512);
  2581. else
  2582. $v_binary_data = gzread($v_tar, 512);
  2583. // ----- Read the header properties
  2584. if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1)
  2585. {
  2586. // ----- Close the archive file
  2587. if ($p_tar_mode == "tar")
  2588. {
  2589. fclose($v_tar);
  2590. fclose($v_temp_tar);
  2591. }
  2592. else
  2593. {
  2594. gzclose($v_tar);
  2595. gzclose($v_temp_tar);
  2596. }
  2597. @unlink($v_temp_tarname);
  2598. // ----- Return
  2599. TrFctEnd(__FILE__, __LINE__, $v_result);
  2600. return $v_result;
  2601. }
  2602. // ----- Look for empty blocks to skip
  2603. if ($v_header[filename] == "")
  2604. {
  2605. TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?");
  2606. continue;
  2607. }
  2608. TrFctMessage(__FILE__, __LINE__, 2, "Found file '$v_header[filename]', size '$v_header[size]'");
  2609. // ----- Look for filenames to update
  2610. for ($i=0, $v_update_file=FALSE, $v_found_file=FALSE; ($i<sizeof($v_stored_list)) && (!$v_update_file); $i++)
  2611. {
  2612. TrFctMessage(__FILE__, __LINE__, 4, "Compare with file '$v_stored_list[$i]'");
  2613. // ----- Compare the file names
  2614. if ($v_stored_list[$i] == $v_header[filename])
  2615. {
  2616. TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' is present in archive");
  2617. TrFctMessage(__FILE__, __LINE__, 3, "File '$v_stored_list[$i]' mtime=".filemtime($p_file_list[$i])." ".date("l dS of F Y h:i:s A", filemtime($p_file_list[$i])));
  2618. TrFctMessage(__FILE__, __LINE__, 3, "Archived mtime=".$v_header[mtime]." ".date("l dS of F Y h:i:s A", $v_header[mtime]));
  2619. // ----- Store found informations
  2620. $v_found_file = TRUE;
  2621. $v_current_filename = $p_file_list[$i];
  2622. // ----- Look if the file need to be updated
  2623. if (filemtime($p_file_list[$i]) > $v_header[mtime])
  2624. {
  2625. TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be updated");
  2626. $v_update_file = TRUE;
  2627. }
  2628. else
  2629. {
  2630. TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' does not need to be updated");
  2631. $v_update_file = FALSE;
  2632. }
  2633. // ----- Flag the name in order not to add the file at the end
  2634. $v_found_list[$i] = 1;
  2635. }
  2636. else
  2637. {
  2638. TrFctMessage(__FILE__, __LINE__, 4, "File '$p_file_list[$i]' is not '$v_header[filename]'");
  2639. }
  2640. }
  2641. // ----- Copy files that do not need to be updated
  2642. if (!$v_update_file)
  2643. {
  2644. TrFctMessage(__FILE__, __LINE__, 2, "Keep file '$v_header[filename]'");
  2645. // ----- Write the file header
  2646. if ($p_tar_mode == "tar")
  2647. {
  2648. fputs($v_temp_tar, $v_binary_data, 512);
  2649. }
  2650. else
  2651. {
  2652. gzputs($v_temp_tar, $v_binary_data, 512);
  2653. }
  2654. // ----- Write the file data
  2655. $n = ceil($v_header[size]/512);
  2656. for ($j=0; $j<$n; $j++)
  2657. {
  2658. TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number ".($j+1));
  2659. if ($p_tar_mode == "tar")
  2660. {
  2661. $v_content = fread($v_tar, 512);
  2662. fwrite($v_temp_tar, $v_content, 512);
  2663. }
  2664. else
  2665. {
  2666. $v_content = gzread($v_tar, 512);
  2667. gzwrite($v_temp_tar, $v_content, 512);
  2668. }
  2669. }
  2670. // ----- File name and properties are logged if listing mode or file is extracted
  2671. TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '$v_header[filename]'");
  2672. // ----- Add the array describing the file into the list
  2673. $p_list_detail[$v_nb] = $v_header;
  2674. $p_list_detail[$v_nb][status] = ($v_found_file?"not_updated":"ok");
  2675. // ----- Increment
  2676. $v_nb++;
  2677. }
  2678. // ----- Look for file that need to be updated
  2679. else
  2680. {
  2681. // ----- Trace
  2682. TrFctMessage(__FILE__, __LINE__, 2, "Start update of file '$v_current_filename'");
  2683. // ----- Store the old file size
  2684. $v_old_size = $v_header[size];
  2685. // ----- Add the file
  2686. if (($v_result = PclTarHandleAddFile($v_temp_tar, $v_current_filename, $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
  2687. {
  2688. // ----- Close the tarfile
  2689. if ($p_tar_mode == "tar")
  2690. {
  2691. fclose($v_tar);
  2692. fclose($v_temp_tar);
  2693. }
  2694. else
  2695. {
  2696. gzclose($v_tar);
  2697. gzclose($v_temp_tar);
  2698. }
  2699. @unlink($p_temp_tarname);
  2700. // ----- Return status
  2701. TrFctEnd(__FILE__, __LINE__, $v_result);
  2702. return $v_result;
  2703. }
  2704. // ----- Trace
  2705. TrFctMessage(__FILE__, __LINE__, 2, "Skip old file '$v_header[filename]'");
  2706. // ----- Jump to next file
  2707. if ($p_tar_mode == "tar")
  2708. fseek($v_tar, ftell($v_tar)+(ceil(($v_old_size/512))*512));
  2709. else
  2710. gzseek($v_tar, gztell($v_tar)+(ceil(($v_old_size/512))*512));
  2711. // ----- Add the array describing the file into the list
  2712. $p_list_detail[$v_nb] = $v_header;
  2713. $p_list_detail[$v_nb][status] = "updated";
  2714. // ----- Increment
  2715. $v_nb++;
  2716. }
  2717. // ----- Look for end of file
  2718. if ($p_tar_mode == "tar")
  2719. $v_end_of_file = feof($v_tar);
  2720. else
  2721. $v_end_of_file = gzeof($v_tar);
  2722. }
  2723. // ----- Look for files that does not exists in the archive and need to be added
  2724. for ($i=0; $i<sizeof($p_file_list); $i++)
  2725. {
  2726. // ----- Look if file not found in the archive
  2727. if (!$v_found_list[$i])
  2728. {
  2729. TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' need to be added");
  2730. // ----- Add the file
  2731. if (($v_result = PclTarHandleAddFile($v_temp_tar, $p_file_list[$i], $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
  2732. {
  2733. // ----- Close the tarfile
  2734. if ($p_tar_mode == "tar")
  2735. {
  2736. fclose($v_tar);
  2737. fclose($v_temp_tar);
  2738. }
  2739. else
  2740. {
  2741. gzclose($v_tar);
  2742. gzclose($v_temp_tar);
  2743. }
  2744. @unlink($p_temp_tarname);
  2745. // ----- Return status
  2746. TrFctEnd(__FILE__, __LINE__, $v_result);
  2747. return $v_result;
  2748. }
  2749. // ----- Add the array describing the file into the list
  2750. $p_list_detail[$v_nb] = $v_header;
  2751. $p_list_detail[$v_nb][status] = "added";
  2752. // ----- Increment
  2753. $v_nb++;
  2754. }
  2755. else
  2756. {
  2757. TrFctMessage(__FILE__, __LINE__, 3, "File '$p_file_list[$i]' was already updated if needed");
  2758. }
  2759. }
  2760. // ----- Write the last empty buffer
  2761. PclTarHandleFooter($v_temp_tar, $p_tar_mode);
  2762. // ----- Close the tarfile
  2763. if ($p_tar_mode == "tar")
  2764. {
  2765. fclose($v_tar);
  2766. fclose($v_temp_tar);
  2767. }
  2768. else
  2769. {
  2770. gzclose($v_tar);
  2771. gzclose($v_temp_tar);
  2772. }
  2773. // ----- Unlink tar file
  2774. if (!@unlink($p_tarname))
  2775. {
  2776. // ----- Error log
  2777. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  2778. }
  2779. // ----- Rename tar file
  2780. if (!@rename($v_temp_tarname, $p_tarname))
  2781. {
  2782. // ----- Error log
  2783. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  2784. // ----- Return
  2785. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2786. return PclErrorCode();
  2787. }
  2788. // ----- Return
  2789. TrFctEnd(__FILE__, __LINE__, $v_result);
  2790. return $v_result;
  2791. }
  2792. // --------------------------------------------------------------------------------
  2793. // --------------------------------------------------------------------------------
  2794. // Function : PclTarHandleReadHeader()
  2795. // Description :
  2796. // Parameters :
  2797. // Return Values :
  2798. // --------------------------------------------------------------------------------
  2799. function PclTarHandleReadHeader($v_binary_data, &$v_header)
  2800. {
  2801. TrFctStart(__FILE__, __LINE__, "PclTarHandleReadHeader", "");
  2802. $v_result=1;
  2803. // ----- Read the 512 bytes header
  2804. /*
  2805. if ($p_tar_mode == "tar")
  2806. $v_binary_data = fread($p_tar, 512);
  2807. else
  2808. $v_binary_data = gzread($p_tar, 512);
  2809. */
  2810. // ----- Look for no more block
  2811. if (strlen($v_binary_data)==0)
  2812. {
  2813. $v_header[filename] = "";
  2814. $v_header[status] = "empty";
  2815. // ----- Return
  2816. TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found");
  2817. return $v_result;
  2818. }
  2819. // ----- Look for invalid block size
  2820. if (strlen($v_binary_data) != 512)
  2821. {
  2822. $v_header[filename] = "";
  2823. $v_header[status] = "invalid_header";
  2824. TrFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
  2825. // ----- Error log
  2826. PclErrorLog(-10, "Invalid block size : ".strlen($v_binary_data));
  2827. // ----- Return
  2828. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2829. return PclErrorCode();
  2830. }
  2831. // ----- Calculate the checksum
  2832. $v_checksum = 0;
  2833. // ..... First part of the header
  2834. for ($i=0; $i<148; $i++)
  2835. {
  2836. $v_checksum+=ord(substr($v_binary_data,$i,1));
  2837. }
  2838. // ..... Ignore the checksum value and replace it by ' ' (space)
  2839. for ($i=148; $i<156; $i++)
  2840. {
  2841. $v_checksum += ord(' ');
  2842. }
  2843. // ..... Last part of the header
  2844. for ($i=156; $i<512; $i++)
  2845. {
  2846. $v_checksum+=ord(substr($v_binary_data,$i,1));
  2847. }
  2848. TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum");
  2849. // ----- Extract the values
  2850. TrFctMessage(__FILE__, __LINE__, 2, "Header : '$v_binary_data'");
  2851. $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data);
  2852. // ----- Extract the checksum for check
  2853. $v_header["checksum"] = OctDec(trim($v_data["checksum"]));
  2854. TrFctMessage(__FILE__, __LINE__, 3, "File checksum : $v_header[checksum]");
  2855. if ($v_header["checksum"] != $v_checksum)
  2856. {
  2857. TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : $v_checksum calculated, $v_header[checksum] expected");
  2858. $v_header["filename"] = "";
  2859. $v_header["status"] = "invalid_header";
  2860. // ----- Look for last block (empty block)
  2861. if (($v_checksum == 256) && ($v_header["checksum"] == 0))
  2862. {
  2863. $v_header["status"] = "empty";
  2864. // ----- Return
  2865. TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found");
  2866. return $v_result;
  2867. }
  2868. // ----- Error log
  2869. PclErrorLog(-13, "Invalid checksum : $v_checksum calculated, " . $v_header["checksum"] . " expected");
  2870. // ----- Return
  2871. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2872. return PclErrorCode();
  2873. }
  2874. TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ($v_checksum)");
  2875. // ----- Extract the properties
  2876. $v_header["filename"] = trim($v_data["filename"]);
  2877. TrFctMessage(__FILE__, __LINE__, 2, "Name : '$v_header[filename]'");
  2878. $v_header["mode"] = OctDec(trim($v_data["mode"]));
  2879. TrFctMessage(__FILE__, __LINE__, 2, "Mode : '".DecOct($v_header["mode"])."'");
  2880. $v_header["uid"] = OctDec(trim($v_data["uid"]));
  2881. TrFctMessage(__FILE__, __LINE__, 2, "Uid : '$v_header[uid]'");
  2882. $v_header["gid"] = OctDec(trim($v_data["gid"]));
  2883. TrFctMessage(__FILE__, __LINE__, 2, "Gid : '$v_header[gid]'");
  2884. $v_header["size"] = OctDec(trim($v_data["size"]));
  2885. TrFctMessage(__FILE__, __LINE__, 2, "Size : '$v_header[size]'");
  2886. $v_header["mtime"] = OctDec(trim($v_data["mtime"]));
  2887. TrFctMessage(__FILE__, __LINE__, 2, "Date : ".date("l dS of F Y h:i:s A", $v_header["mtime"]));
  2888. if (($v_header["typeflag"] = $v_data["typeflag"]) == "5")
  2889. {
  2890. $v_header["size"] = 0;
  2891. TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '$v_header[size]'");
  2892. }
  2893. TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : $v_header[typeflag]");
  2894. /* ----- All these fields are removed form the header because they do not carry interesting info
  2895. $v_header[link] = trim($v_data[link]);
  2896. TrFctMessage(__FILE__, __LINE__, 2, "Linkname : $v_header[linkname]");
  2897. $v_header[magic] = trim($v_data[magic]);
  2898. TrFctMessage(__FILE__, __LINE__, 2, "Magic : $v_header[magic]");
  2899. $v_header[version] = trim($v_data[version]);
  2900. TrFctMessage(__FILE__, __LINE__, 2, "Version : $v_header[version]");
  2901. $v_header[uname] = trim($v_data[uname]);
  2902. TrFctMessage(__FILE__, __LINE__, 2, "Uname : $v_header[uname]");
  2903. $v_header[gname] = trim($v_data[gname]);
  2904. TrFctMessage(__FILE__, __LINE__, 2, "Gname : $v_header[gname]");
  2905. $v_header[devmajor] = trim($v_data[devmajor]);
  2906. TrFctMessage(__FILE__, __LINE__, 2, "Devmajor : $v_header[devmajor]");
  2907. $v_header[devminor] = trim($v_data[devminor]);
  2908. TrFctMessage(__FILE__, __LINE__, 2, "Devminor : $v_header[devminor]");
  2909. */
  2910. // ----- Set the status field
  2911. $v_header["status"] = "ok";
  2912. // ----- Return
  2913. TrFctEnd(__FILE__, __LINE__, $v_result);
  2914. return $v_result;
  2915. }
  2916. // --------------------------------------------------------------------------------
  2917. // --------------------------------------------------------------------------------
  2918. // Function : PclTarHandlerDirCheck()
  2919. // Description :
  2920. // Check if a directory exists, if not it creates it and all the parents directory
  2921. // which may be useful.
  2922. // Parameters :
  2923. // $p_dir : Directory path to check (without / at the end).
  2924. // Return Values :
  2925. // 1 : OK
  2926. // -1 : Unable to create directory
  2927. // --------------------------------------------------------------------------------
  2928. function PclTarHandlerDirCheck($p_dir)
  2929. {
  2930. $v_result = 1;
  2931. TrFctStart(__FILE__, __LINE__, "PclTarHandlerDirCheck", "$p_dir");
  2932. // ----- Check the directory availability
  2933. if ((is_dir($p_dir)) || ($p_dir == ""))
  2934. {
  2935. TrFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
  2936. return 1;
  2937. }
  2938. // ----- Look for file alone
  2939. /*
  2940. if (!strstr("$p_dir", "/"))
  2941. {
  2942. TrFctEnd(__FILE__, __LINE__, "'$p_dir' is a file with no directory");
  2943. return 1;
  2944. }
  2945. */
  2946. // ----- Extract parent directory
  2947. $p_parent_dir = dirname($p_dir);
  2948. TrFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
  2949. // ----- Just a check
  2950. if ($p_parent_dir != $p_dir)
  2951. {
  2952. // ----- Look for parent directory
  2953. if ($p_parent_dir != "")
  2954. {
  2955. if (($v_result = PclTarHandlerDirCheck($p_parent_dir)) != 1)
  2956. {
  2957. TrFctEnd(__FILE__, __LINE__, $v_result);
  2958. return $v_result;
  2959. }
  2960. }
  2961. }
  2962. // ----- Create the directory
  2963. TrFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
  2964. /*
  2965. * MODIFIED FOR JOOMLA
  2966. * @since 1.5 December 12, 2005
  2967. */
  2968. jimport('joomla.filesystem.folder');
  2969. if (!JFolder::create($p_dir, 0777))
  2970. {
  2971. // ----- Error log
  2972. PclErrorLog(-8, "Unable to create directory '$p_dir'");
  2973. // ----- Return
  2974. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  2975. return PclErrorCode();
  2976. }
  2977. // ----- Return
  2978. TrFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
  2979. return $v_result;
  2980. }
  2981. // --------------------------------------------------------------------------------
  2982. // --------------------------------------------------------------------------------
  2983. // Function : PclTarHandleExtension()
  2984. // Description :
  2985. // Parameters :
  2986. // Return Values :
  2987. // --------------------------------------------------------------------------------
  2988. function PclTarHandleExtension($p_tarname)
  2989. {
  2990. TrFctStart(__FILE__, __LINE__, "PclTarHandleExtension", "tar=$p_tarname");
  2991. // ----- Look for file extension
  2992. if ((substr($p_tarname, -7) == ".tar.gz") || (substr($p_tarname, -4) == ".tgz"))
  2993. {
  2994. TrFctMessage(__FILE__, __LINE__, 2, "Archive is a gzip tar");
  2995. $v_tar_mode = "tgz";
  2996. }
  2997. else if (substr($p_tarname, -4) == ".tar")
  2998. {
  2999. TrFctMessage(__FILE__, __LINE__, 2, "Archive is a tar");
  3000. $v_tar_mode = "tar";
  3001. }
  3002. else
  3003. {
  3004. // ----- Error log
  3005. PclErrorLog(-9, "Invalid archive extension");
  3006. TrFctMessage(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  3007. $v_tar_mode = "";
  3008. }
  3009. // ----- Return
  3010. TrFctEnd(__FILE__, __LINE__, $v_tar_mode);
  3011. return $v_tar_mode;
  3012. }
  3013. // --------------------------------------------------------------------------------
  3014. // --------------------------------------------------------------------------------
  3015. // Function : PclTarHandlePathReduction()
  3016. // Description :
  3017. // Parameters :
  3018. // Return Values :
  3019. // --------------------------------------------------------------------------------
  3020. function PclTarHandlePathReduction($p_dir)
  3021. {
  3022. TrFctStart(__FILE__, __LINE__, "PclTarHandlePathReduction", "dir='$p_dir'");
  3023. $v_result = "";
  3024. // ----- Look for not empty path
  3025. if ($p_dir != "")
  3026. {
  3027. // ----- Explode path by directory names
  3028. $v_list = explode("/", $p_dir);
  3029. // ----- Study directories from last to first
  3030. for ($i=sizeof($v_list)-1; $i>=0; $i--)
  3031. {
  3032. // ----- Look for current path
  3033. if ($v_list[$i] == ".")
  3034. {
  3035. // ----- Ignore this directory
  3036. // Should be the first $i=0, but no check is done
  3037. }
  3038. else if ($v_list[$i] == "..")
  3039. {
  3040. // ----- Ignore it and ignore the $i-1
  3041. $i--;
  3042. }
  3043. else if (($v_list[$i] == "") && ($i!=(sizeof($v_list)-1)) && ($i!=0))
  3044. {
  3045. // ----- Ignore only the double '//' in path,
  3046. // but not the first and last '/'
  3047. }
  3048. else
  3049. {
  3050. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  3051. }
  3052. }
  3053. }
  3054. // ----- Return
  3055. TrFctEnd(__FILE__, __LINE__, $v_result);
  3056. return $v_result;
  3057. }
  3058. // --------------------------------------------------------------------------------
  3059. // ----- End of double include look
  3060. }
  3061. ?>