PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://miacms.googlecode.com/
PHP | 3575 lines | 1996 code | 511 blank | 1068 comment | 619 complexity | 7642ed2fb1c2f2ec1eb28c483f1b24c7 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @package MiaCMS
  4. */
  5. /** ensure this file is being included by a parent file */
  6. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  7. // --------------------------------------------------------------------------------
  8. // PhpConcept Library - Tar Module 1.3
  9. // --------------------------------------------------------------------------------
  10. // License GNU/GPL - Vincent Blavet - August 2001
  11. // http://www.phpconcept.net
  12. // --------------------------------------------------------------------------------
  13. //
  14. // Presentation :
  15. // PclTar is a library that allow you to create a GNU TAR + GNU ZIP archive,
  16. // to add files or directories, to extract all the archive or a part of it.
  17. // So far tests show that the files generated by PclTar are readable by
  18. // gzip tools and WinZip application.
  19. //
  20. // Description :
  21. // See readme.txt (English & Français) and http://www.phpconcept.net
  22. //
  23. // Warning :
  24. // This library and the associated files are non commercial, non professional
  25. // work.
  26. // It should not have unexpected results. However if any damage is caused by
  27. // this software the author can not be responsible.
  28. // The use of this software is at the risk of the user.
  29. //
  30. // --------------------------------------------------------------------------------
  31. // ----- Look for double include
  32. if (!defined("PCL_TAR"))
  33. {
  34. define( "PCL_TAR", 1 );
  35. // ----- Configuration variable
  36. // Theses values may be changed by the user of PclTar library
  37. if (!isset($g_pcltar_lib_dir))
  38. $g_pcltar_lib_dir = "lib";
  39. // ----- Error codes
  40. // -1 : Unable to open file in binary write mode
  41. // -2 : Unable to open file in binary read mode
  42. // -3 : Invalid parameters
  43. // -4 : File does not exist
  44. // -5 : Filename is too long (max. 99)
  45. // -6 : Not a valid tar file
  46. // -7 : Invalid extracted file size
  47. // -8 : Unable to create directory
  48. // -9 : Invalid archive extension
  49. // -10 : Invalid archive format
  50. // -11 : Unable to delete file (unlink)
  51. // -12 : Unable to rename file (rename)
  52. // -13 : Invalid header checksum
  53. // --------------------------------------------------------------------------------
  54. // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
  55. // --------------------------------------------------------------------------------
  56. // ----- Global variables
  57. $g_pcltar_version = "1.3";
  58. // ----- Extract extension type (.php3/.php/...)
  59. // $g_pcltar_extension = substr(strrchr(basename($PATH_TRANSLATED), '.'), 1);
  60. $g_pcltar_extension = substr(strrchr(basename(@$_SERVER["PATH_TRANSLATED"]), '.'), 1);
  61. // ----- Include other libraries
  62. // This library should be called by each script before the include of PhpZip
  63. // Library in order to limit the potential 'lib' directory path problem.
  64. if (!defined("PCLERROR_LIB"))
  65. {
  66. include($g_pcltar_lib_dir."/pclerror.lib.".$g_pcltar_extension);
  67. }
  68. if (!defined("PCLTRACE_LIB"))
  69. {
  70. include($g_pcltar_lib_dir."/pcltrace.lib.".$g_pcltar_extension);
  71. }
  72. // --------------------------------------------------------------------------------
  73. // Function : PclTarCreate()
  74. // Description :
  75. // Creates a new archive with name $p_tarname containing the files and/or
  76. // directories indicated in $p_list. If the tar filename extension is
  77. // ".tar", the file will not be compressed. If it is ".tar.gz" or ".tgz"
  78. // it will be a gzip compressed tar archive.
  79. // If you want to use an other extension, you must indicate the mode in
  80. // $p_mode ("tar" or "tgz").
  81. // $p_add_dir and $p_remove_dir give you the ability to store a path
  82. // which is not the real path of the files.
  83. // Parameters :
  84. // $p_tarname : Name of an existing tar file
  85. // $p_filelist : An array containing file or directory names, or
  86. // a string containing one filename or directory name, or
  87. // a string containing a list of filenames and/or directory
  88. // names separated by spaces.
  89. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive,
  90. // if $p_mode is not specified, it will be determined by the extension.
  91. // $p_add_dir : Path to add in the filename path archived
  92. // $p_remove_dir : Path to remove in the filename path archived
  93. // Return Values :
  94. // 1 on success, or an error code (see table at the beginning).
  95. // --------------------------------------------------------------------------------
  96. function PclTarCreate($p_tarname, $p_filelist="", $p_mode="", $p_add_dir="", $p_remove_dir="")
  97. {
  98. TrFctStart(__FILE__, __LINE__, "PclTarCreate", "tar=$p_tarname, file='$p_filelist', mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  99. $v_result=1;
  100. // ----- Look for default mode
  101. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  102. {
  103. // ----- Extract the tar format from the extension
  104. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  105. {
  106. // ----- Return
  107. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  108. return PclErrorCode();
  109. }
  110. // ----- Trace
  111. TrFctMessage(__FILE__, __LINE__, 1, "Auto mode selected : found $p_mode");
  112. }
  113. // ----- Look if the $p_filelist is really an array
  114. if (is_array($p_filelist))
  115. {
  116. // ----- Call the create fct
  117. $v_result = PclTarHandleCreate($p_tarname, $p_filelist, $p_mode, $p_add_dir, $p_remove_dir);
  118. }
  119. // ----- Look if the $p_filelist is a string
  120. else if (is_string($p_filelist))
  121. {
  122. // ----- Create a list with the elements from the string
  123. $v_list = explode(" ", $p_filelist);
  124. // ----- Call the create fct
  125. $v_result = PclTarHandleCreate($p_tarname, $v_list, $p_mode, $p_add_dir, $p_remove_dir);
  126. }
  127. // ----- Invalid variable
  128. else
  129. {
  130. // ----- Error log
  131. PclErrorLog(-3, "Invalid variable type p_filelist");
  132. $v_result = -3;
  133. }
  134. // ----- Return
  135. TrFctEnd(__FILE__, __LINE__, $v_result);
  136. return $v_result;
  137. }
  138. // --------------------------------------------------------------------------------
  139. // --------------------------------------------------------------------------------
  140. // Function : PclTarAdd()
  141. // Description :
  142. // PLEASE DO NOT USE ANY MORE THIS FUNCTION. Use PclTarAddList().
  143. //
  144. // This function is maintained only for compatibility reason
  145. //
  146. // Parameters :
  147. // $p_tarname : Name of an existing tar file
  148. // $p_filelist : An array containing file or directory names, or
  149. // a string containing one filename or directory name, or
  150. // a string containing a list of filenames and/or directory
  151. // names separated by spaces.
  152. // Return Values :
  153. // 1 on success,
  154. // Or an error code (see list on top).
  155. // --------------------------------------------------------------------------------
  156. function PclTarAdd($p_tarname, $p_filelist)
  157. {
  158. TrFctStart(__FILE__, __LINE__, "PclTarAdd", "tar=$p_tarname, file=$p_filelist");
  159. $v_result=1;
  160. $v_list_detail = array();
  161. // ----- Extract the tar format from the extension
  162. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  163. {
  164. // ----- Return
  165. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  166. return PclErrorCode();
  167. }
  168. // ----- Look if the $p_filelist is really an array
  169. if (is_array($p_filelist))
  170. {
  171. // ----- Call the add fct
  172. $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $v_list_detail, "", "");
  173. }
  174. // ----- Look if the $p_filelist is a string
  175. else if (is_string($p_filelist))
  176. {
  177. // ----- Create a list with the elements from the string
  178. $v_list = explode(" ", $p_filelist);
  179. // ----- Call the add fct
  180. $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $v_list_detail, "", "");
  181. }
  182. // ----- Invalid variable
  183. else
  184. {
  185. // ----- Error log
  186. PclErrorLog(-3, "Invalid variable type p_filelist");
  187. $v_result = -3;
  188. }
  189. // ----- Cleaning
  190. unset($v_list_detail);
  191. // ----- Return
  192. TrFctEnd(__FILE__, __LINE__, $v_result);
  193. return $v_result;
  194. }
  195. // --------------------------------------------------------------------------------
  196. // --------------------------------------------------------------------------------
  197. // Function : PclTarAddList()
  198. // Description :
  199. // Add a list of files or directories ($p_filelist) in the tar archive $p_tarname.
  200. // The list can be an array of file/directory names or a string with names
  201. // separated by one space.
  202. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  203. // different from the real path of the file. This is usefull if you want to have PclTar
  204. // running in any directory, and memorize relative path from an other directory.
  205. // If $p_mode is not set it will be automatically computed from the $p_tarname
  206. // extension (.tar, .tar.gz or .tgz).
  207. // Parameters :
  208. // $p_tarname : Name of an existing tar file
  209. // $p_filelist : An array containing file or directory names, or
  210. // a string containing one filename or directory name, or
  211. // a string containing a list of filenames and/or directory
  212. // names separated by spaces.
  213. // $p_add_dir : Path to add in the filename path archived
  214. // $p_remove_dir : Path to remove in the filename path archived
  215. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  216. // Return Values :
  217. // 1 on success,
  218. // Or an error code (see list on top).
  219. // --------------------------------------------------------------------------------
  220. function PclTarAddList($p_tarname, $p_filelist, $p_add_dir="", $p_remove_dir="", $p_mode="")
  221. {
  222. 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");
  223. $v_result=1;
  224. $p_list_detail = array();
  225. // ----- Extract the tar format from the extension
  226. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  227. {
  228. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  229. {
  230. // ----- Return
  231. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  232. return PclErrorCode();
  233. }
  234. }
  235. // ----- Look if the $p_filelist is really an array
  236. if (is_array($p_filelist))
  237. {
  238. // ----- Call the add fct
  239. $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
  240. }
  241. // ----- Look if the $p_filelist is a string
  242. else if (is_string($p_filelist))
  243. {
  244. // ----- Create a list with the elements from the string
  245. $v_list = explode(" ", $p_filelist);
  246. // ----- Call the add fct
  247. $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
  248. }
  249. // ----- Invalid variable
  250. else
  251. {
  252. // ----- Error log
  253. PclErrorLog(-3, "Invalid variable type p_filelist");
  254. $v_result = -3;
  255. }
  256. // ----- Return
  257. if ($v_result != 1)
  258. {
  259. TrFctEnd(__FILE__, __LINE__, 0);
  260. return 0;
  261. }
  262. TrFctEnd(__FILE__, __LINE__, $p_list_detail);
  263. return $p_list_detail;
  264. }
  265. // --------------------------------------------------------------------------------
  266. // --------------------------------------------------------------------------------
  267. // Function : PclTarList()
  268. // Description :
  269. // Gives the list of all the files present in the tar archive $p_tarname.
  270. // The list is the function result, it will be 0 on error.
  271. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  272. // function will determine the type of the archive.
  273. // Parameters :
  274. // $p_tarname : Name of an existing tar file
  275. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  276. // Return Values :
  277. // 0 on error (Use PclErrorCode() and PclErrorString() for more info)
  278. // or
  279. // An array containing file properties. Each file properties is an array of
  280. // properties.
  281. // The properties (array field names) are :
  282. // filename, size, mode, uid, gid, mtime, typeflag, status
  283. // Exemple : $v_list = PclTarList("my.tar");
  284. // for ($i=0; $i<sizeof($v_list); $i++)
  285. // echo "Filename :'".$v_list[$i][filename]."'<br>";
  286. // --------------------------------------------------------------------------------
  287. function PclTarList($p_tarname, $p_mode="")
  288. {
  289. TrFctStart(__FILE__, __LINE__, "PclTarList", "tar=$p_tarname, mode='$p_mode'");
  290. $v_result=1;
  291. // ----- Extract the tar format from the extension
  292. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  293. {
  294. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  295. {
  296. // ----- Return
  297. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  298. return 0;
  299. }
  300. }
  301. // ----- Call the extracting fct
  302. $p_list = array();
  303. if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "list", "", $p_mode, "")) != 1)
  304. {
  305. unset($p_list);
  306. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  307. return(0);
  308. }
  309. // ----- Return
  310. TrFctEnd(__FILE__, __LINE__, $p_list);
  311. return $p_list;
  312. }
  313. // --------------------------------------------------------------------------------
  314. // --------------------------------------------------------------------------------
  315. // Function : PclTarExtract()
  316. // Description :
  317. // Extract all the files present in the archive $p_tarname, in the directory
  318. // $p_path. The relative path of the archived files are keep and become
  319. // relative to $p_path.
  320. // If a file with the same name already exists it will be replaced.
  321. // If the path to the file does not exist, it will be created.
  322. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  323. // function will determine the type of the archive.
  324. // Parameters :
  325. // $p_tarname : Name of an existing tar file.
  326. // $p_path : Path where the files will be extracted. The files will use
  327. // their memorized path from $p_path.
  328. // If $p_path is "", files will be extracted in "./".
  329. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  330. // extracted files. If the path does not match the file path,
  331. // the file is extracted with its memorized path.
  332. // $p_path and $p_remove_path are commulative.
  333. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  334. // Return Values :
  335. // Same as PclTarList()
  336. // --------------------------------------------------------------------------------
  337. function PclTarExtract($p_tarname, $p_path="./", $p_remove_path="", $p_mode="")
  338. {
  339. TrFctStart(__FILE__, __LINE__, "PclTarExtract", "tar='$p_tarname', path='$p_path', remove_path='$p_remove_path', mode='$p_mode'");
  340. $v_result=1;
  341. // ----- Extract the tar format from the extension
  342. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  343. {
  344. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  345. {
  346. // ----- Return
  347. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  348. return 0;
  349. }
  350. }
  351. // ----- Call the extracting fct
  352. if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "complete", $p_path, $p_mode, $p_remove_path)) != 1)
  353. {
  354. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  355. return(0);
  356. }
  357. // ----- Return
  358. TrFctEnd(__FILE__, __LINE__, $p_list);
  359. return $p_list;
  360. }
  361. // --------------------------------------------------------------------------------
  362. // --------------------------------------------------------------------------------
  363. // Function : PclTarExtractList()
  364. // Description :
  365. // Extract the files present in the archive $p_tarname and specified in
  366. // $p_filelist, in the directory
  367. // $p_path. The relative path of the archived files are keep and become
  368. // relative to $p_path.
  369. // If a directory is spécified in the list, all the files from this directory
  370. // will be extracted.
  371. // If a file with the same name already exists it will be replaced.
  372. // If the path to the file does not exist, it will be created.
  373. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  374. // function will determine the type of the archive.
  375. // Parameters :
  376. // $p_tarname : Name of an existing tar file
  377. // $p_filelist : An array containing file or directory names, or
  378. // a string containing one filename or directory name, or
  379. // a string containing a list of filenames and/or directory
  380. // names separated by spaces.
  381. // $p_path : Path where the files will be extracted. The files will use
  382. // their memorized path from $p_path.
  383. // If $p_path is "", files will be extracted in "./".
  384. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  385. // extracted files. If the path does not match the file path,
  386. // the file is extracted with its memorized path.
  387. // $p_path and $p_remove_path are commulative.
  388. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  389. // Return Values :
  390. // Same as PclTarList()
  391. // --------------------------------------------------------------------------------
  392. function PclTarExtractList($p_tarname, $p_filelist, $p_path="./", $p_remove_path="", $p_mode="")
  393. {
  394. TrFctStart(__FILE__, __LINE__, "PclTarExtractList", "tar=$p_tarname, list, path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
  395. $v_result=1;
  396. // ----- Extract the tar format from the extension
  397. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  398. {
  399. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  400. {
  401. // ----- Return
  402. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  403. return 0;
  404. }
  405. }
  406. // ----- Look if the $p_filelist is really an array
  407. if (is_array($p_filelist))
  408. {
  409. // ----- Call the extracting fct
  410. if (($v_result = PclTarHandleExtract($p_tarname, $p_filelist, $p_list, "partial", $p_path, $v_tar_mode, $p_remove_path)) != 1)
  411. {
  412. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  413. return(0);
  414. }
  415. }
  416. // ----- Look if the $p_filelist is a string
  417. else if (is_string($p_filelist))
  418. {
  419. // ----- Create a list with the elements from the string
  420. $v_list = explode(" ", $p_filelist);
  421. // ----- Call the extracting fct
  422. if (($v_result = PclTarHandleExtract($p_tarname, $v_list, $p_list, "partial", $p_path, $v_tar_mode, $p_remove_path)) != 1)
  423. {
  424. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  425. return(0);
  426. }
  427. }
  428. // ----- Invalid variable
  429. else
  430. {
  431. // ----- Error log
  432. PclErrorLog(-3, "Invalid variable type p_filelist");
  433. // ----- Return
  434. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  435. return 0;
  436. }
  437. // ----- Return
  438. TrFctEnd(__FILE__, __LINE__, $p_list);
  439. return $p_list;
  440. }
  441. // --------------------------------------------------------------------------------
  442. // --------------------------------------------------------------------------------
  443. // Function : PclTarExtractIndex()
  444. // Description :
  445. // Extract the files present in the archive $p_tarname and specified at
  446. // the indexes in $p_index, in the directory
  447. // $p_path. The relative path of the archived files are keep and become
  448. // relative to $p_path.
  449. // If a directory is specified in the list, the directory only is created. All
  450. // the file stored in this archive for this directory
  451. // are not extracted.
  452. // If a file with the same name already exists it will be replaced.
  453. // If the path to the file does not exist, it will be created.
  454. // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  455. // function will determine the type of the archive.
  456. // Parameters :
  457. // $p_tarname : Name of an existing tar file
  458. // $p_index : A single index (integer) or a string of indexes of files to
  459. // extract. The form of the string is "0,4-6,8-12" with only numbers
  460. // and '-' for range or ',' to separate ranges. No spaces or ';'
  461. // are allowed.
  462. // $p_path : Path where the files will be extracted. The files will use
  463. // their memorized path from $p_path.
  464. // If $p_path is "", files will be extracted in "./".
  465. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  466. // extracted files. If the path does not match the file path,
  467. // the file is extracted with its memorized path.
  468. // $p_path and $p_remove_path are commulative.
  469. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  470. // Return Values :
  471. // Same as PclTarList()
  472. // --------------------------------------------------------------------------------
  473. function PclTarExtractIndex($p_tarname, $p_index, $p_path="./", $p_remove_path="", $p_mode="")
  474. {
  475. TrFctStart(__FILE__, __LINE__, "PclTarExtractIndex", "tar=$p_tarname, index='$p_index', path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
  476. $v_result=1;
  477. // ----- Extract the tar format from the extension
  478. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  479. {
  480. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  481. {
  482. // ----- Return
  483. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  484. return 0;
  485. }
  486. }
  487. // ----- Look if the $p_index is really an integer
  488. if (is_integer($p_index))
  489. {
  490. // ----- Call the extracting fct
  491. if (($v_result = PclTarHandleExtractByIndexList($p_tarname, "$p_index", $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1)
  492. {
  493. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  494. return(0);
  495. }
  496. }
  497. // ----- Look if the $p_filelist is a string
  498. else if (is_string($p_index))
  499. {
  500. // ----- Call the extracting fct
  501. if (($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1)
  502. {
  503. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  504. return(0);
  505. }
  506. }
  507. // ----- Invalid variable
  508. else
  509. {
  510. // ----- Error log
  511. PclErrorLog(-3, "Invalid variable type $p_index");
  512. // ----- Return
  513. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  514. return 0;
  515. }
  516. // ----- Return
  517. TrFctEnd(__FILE__, __LINE__, $p_list);
  518. return $p_list;
  519. }
  520. // --------------------------------------------------------------------------------
  521. // --------------------------------------------------------------------------------
  522. // Function : PclTarDelete()
  523. // Description :
  524. // This function deletes from the archive $p_tarname the files which are listed
  525. // in $p_filelist. $p_filelist can be a string with file names separated by
  526. // spaces, or an array containing the file names.
  527. // Parameters :
  528. // $p_tarname : Name of an existing tar file
  529. // $p_filelist : An array or a string containing file names to remove from the
  530. // archive.
  531. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  532. // Return Values :
  533. // List of the files which are kept in the archive (same format as PclTarList())
  534. // --------------------------------------------------------------------------------
  535. function PclTarDelete($p_tarname, $p_filelist, $p_mode="")
  536. {
  537. TrFctStart(__FILE__, __LINE__, "PclTarDelete", "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
  538. $v_result=1;
  539. // ----- Extract the tar format from the extension
  540. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  541. {
  542. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  543. {
  544. // ----- Return
  545. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  546. return 0;
  547. }
  548. }
  549. // ----- Look if the $p_filelist is really an array
  550. if (is_array($p_filelist))
  551. {
  552. // ----- Call the extracting fct
  553. if (($v_result = PclTarHandleDelete($p_tarname, $p_filelist, $p_list, $p_mode)) != 1)
  554. {
  555. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  556. return(0);
  557. }
  558. }
  559. // ----- Look if the $p_filelist is a string
  560. else if (is_string($p_filelist))
  561. {
  562. // ----- Create a list with the elements from the string
  563. $v_list = explode(" ", $p_filelist);
  564. // ----- Call the extracting fct
  565. if (($v_result = PclTarHandleDelete($p_tarname, $v_list, $p_list, $p_mode)) != 1)
  566. {
  567. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  568. return(0);
  569. }
  570. }
  571. // ----- Invalid variable
  572. else
  573. {
  574. // ----- Error log
  575. PclErrorLog(-3, "Invalid variable type p_filelist");
  576. // ----- Return
  577. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  578. return 0;
  579. }
  580. // ----- Return
  581. TrFctEnd(__FILE__, __LINE__, $p_list);
  582. return $p_list;
  583. }
  584. // --------------------------------------------------------------------------------
  585. // --------------------------------------------------------------------------------
  586. // Function : PclTarUpdate()
  587. // Description :
  588. // This function updates the files in $p_filelist which are already in the
  589. // $p_tarname archive with an older last modified date. If the file does not
  590. // exist, it is added at the end of the archive.
  591. // Parameters :
  592. // $p_tarname : Name of an existing tar file
  593. // $p_filelist : An array or a string containing file names to update from the
  594. // archive.
  595. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  596. // Return Values :
  597. // List of the files contained in the archive. The field status contains
  598. // "updated", "not_updated", "added" or "ok" for the files not concerned.
  599. // --------------------------------------------------------------------------------
  600. function PclTarUpdate($p_tarname, $p_filelist, $p_mode="", $p_add_dir="", $p_remove_dir="")
  601. {
  602. TrFctStart(__FILE__, __LINE__, "PclTarUpdate", "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
  603. $v_result=1;
  604. // ----- Extract the tar format from the extension
  605. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  606. {
  607. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  608. {
  609. // ----- Return
  610. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  611. return 0;
  612. }
  613. }
  614. // ----- Look if the $p_filelist is really an array
  615. if (is_array($p_filelist))
  616. {
  617. // ----- Call the extracting fct
  618. if (($v_result = PclTarHandleUpdate($p_tarname, $p_filelist, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1)
  619. {
  620. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  621. return(0);
  622. }
  623. }
  624. // ----- Look if the $p_filelist is a string
  625. else if (is_string($p_filelist))
  626. {
  627. // ----- Create a list with the elements from the string
  628. $v_list = explode(" ", $p_filelist);
  629. // ----- Call the extracting fct
  630. if (($v_result = PclTarHandleUpdate($p_tarname, $v_list, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1)
  631. {
  632. TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
  633. return(0);
  634. }
  635. }
  636. // ----- Invalid variable
  637. else
  638. {
  639. // ----- Error log
  640. PclErrorLog(-3, "Invalid variable type p_filelist");
  641. // ----- Return
  642. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  643. return 0;
  644. }
  645. // ----- Return
  646. TrFctEnd(__FILE__, __LINE__, $p_list);
  647. return $p_list;
  648. }
  649. // --------------------------------------------------------------------------------
  650. // --------------------------------------------------------------------------------
  651. // Function : PclTarMerge()
  652. // Description :
  653. // This function add the content of $p_tarname_add at the end of $p_tarname.
  654. // Parameters :
  655. // $p_tarname : Name of an existing tar file
  656. // $p_tarname_add : Name of an existing tar file taht will be added at the end
  657. // of $p_tarname.
  658. // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  659. // $p_mode_add : 'tar' or 'tgz', if not set, will be determined by $p_tarname_add
  660. // extension
  661. // Return Values :
  662. // List of the files contained in the archive. The field status contains
  663. // "updated", "not_updated", "added" or "ok" for the files not concerned.
  664. // --------------------------------------------------------------------------------
  665. function PclTarMerge($p_tarname, $p_tarname_add, $p_mode="", $p_mode_add="")
  666. {
  667. TrFctStart(__FILE__, __LINE__, "PclTarMerge", "tar='$p_tarname', tar_add='$p_tarname_add', mode='$p_mode', mode_add='$p_mode_add'");
  668. $v_result=1;
  669. // ----- Check the parameters
  670. if (($p_tarname == "") || ($p_tarname_add == ""))
  671. {
  672. // ----- Error log
  673. PclErrorLog(-3, "Invalid empty archive name");
  674. // ----- Return
  675. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  676. return PclErrorCode();
  677. }
  678. // ----- Extract the tar format from the extension
  679. if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
  680. {
  681. if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
  682. {
  683. // ----- Return
  684. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  685. return 0;
  686. }
  687. }
  688. if (($p_mode_add == "") || (($p_mode_add!="tar") && ($p_mode_add!="tgz")))
  689. {
  690. if (($p_mode_add = PclTarHandleExtension($p_tarname_add)) == "")
  691. {
  692. // ----- Return
  693. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  694. return 0;
  695. }
  696. }
  697. // ----- Clear filecache
  698. clearstatcache();
  699. // ----- Check the file size
  700. if ((!is_file($p_tarname)) ||
  701. (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar")))
  702. {
  703. // ----- Error log
  704. if (!is_file($p_tarname))
  705. PclErrorLog(-4, "Archive '$p_tarname' does not exist");
  706. else
  707. PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)");
  708. // ----- Return
  709. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  710. return PclErrorCode();
  711. }
  712. if ((!is_file($p_tarname_add)) ||
  713. (((($v_size_add = filesize($p_tarname_add)) % 512) != 0) && ($p_mode_add=="tar")))
  714. {
  715. // ----- Error log
  716. if (!is_file($p_tarname_add))
  717. PclErrorLog(-4, "Archive '$p_tarname_add' does not exist");
  718. else
  719. PclErrorLog(-6, "Archive '$p_tarname_add' has invalid size ".filesize($p_tarname_add)."(not a 512 block multiple)");
  720. // ----- Return
  721. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  722. return PclErrorCode();
  723. }
  724. // ----- Look for compressed archive
  725. if ($p_mode == "tgz")
  726. {
  727. // ----- Open the file in read mode
  728. if (($p_tar = @gzopen($p_tarname, "rb")) == 0)
  729. {
  730. // ----- Error log
  731. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  732. // ----- Return
  733. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  734. return PclErrorCode();
  735. }
  736. // ----- Open a temporary file in write mode
  737. $v_temp_tarname = uniqid("pcltar-").".tmp";
  738. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  739. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  740. {
  741. // ----- Close tar file
  742. gzclose($p_tar);
  743. // ----- Error log
  744. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  745. // ----- Return
  746. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  747. return PclErrorCode();
  748. }
  749. // ----- Read the first 512 bytes block
  750. $v_buffer = gzread($p_tar, 512);
  751. // ----- Read the following blocks but not the last one
  752. if (!gzeof($p_tar))
  753. {
  754. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  755. $i=1;
  756. // ----- Read new 512 block and write the already read
  757. do{
  758. // ----- Write the already read block
  759. $v_binary_data = pack("a512", "$v_buffer");
  760. gzputs($v_temp_tar, $v_binary_data);
  761. $i++;
  762. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  763. // ----- Read next block
  764. $v_buffer = gzread($p_tar, 512);
  765. } while (!gzeof($p_tar));
  766. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  767. }
  768. }
  769. // ----- Look for uncompressed tar file
  770. else if ($p_mode=="tar")
  771. {
  772. // ----- Open the tar file
  773. if (($p_tar = fopen($p_tarname, "r+b")) == 0)
  774. {
  775. // ----- Error log
  776. PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
  777. // ----- Return
  778. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  779. return PclErrorCode();
  780. }
  781. // ----- Go to the beginning of last block
  782. TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  783. fseek($p_tar, $v_size-512);
  784. TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  785. }
  786. // ----- Look for unknown type
  787. else
  788. {
  789. // ----- Error log
  790. PclErrorLog(-3, "Invalid tar mode $p_mode");
  791. // ----- Return
  792. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  793. return PclErrorCode();
  794. }
  795. // ----- Look for type of archive to add
  796. if ($p_mode_add == "tgz")
  797. {
  798. TrFctMessage(__FILE__, __LINE__, 4, "Opening file $p_tarname_add");
  799. // ----- Open the file in read mode
  800. if (($p_tar_add = @gzopen($p_tarname_add, "rb")) == 0)
  801. {
  802. // ----- Error log
  803. PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
  804. // ----- Return
  805. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  806. return PclErrorCode();
  807. }
  808. // ----- Read the first 512 bytes block
  809. $v_buffer = gzread($p_tar_add, 512);
  810. // ----- Read the following blocks but not the last one
  811. if (!gzeof($p_tar_add))
  812. {
  813. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  814. $i=1;
  815. // ----- Read new 512 block and write the already read
  816. do{
  817. // ----- Write the already read block
  818. $v_binary_data = pack("a512", "$v_buffer");
  819. if ($p_mode=="tar")
  820. fputs($p_tar, $v_binary_data);
  821. else
  822. gzputs($v_temp_tar, $v_binary_data);
  823. $i++;
  824. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  825. // ----- Read next block
  826. $v_buffer = gzread($p_tar_add, 512);
  827. } while (!gzeof($p_tar_add));
  828. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  829. }
  830. // ----- Close the files
  831. gzclose($p_tar_add);
  832. }
  833. // ----- Look for uncompressed tar file
  834. else if ($p_mode=="tar")
  835. {
  836. // ----- Open the file in read mode
  837. if (($p_tar_add = @fopen($p_tarname_add, "rb")) == 0)
  838. {
  839. // ----- Error log
  840. PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
  841. // ----- Return
  842. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  843. return PclErrorCode();
  844. }
  845. // ----- Read the first 512 bytes block
  846. $v_buffer = fread($p_tar_add, 512);
  847. // ----- Read the following blocks but not the last one
  848. if (!feof($p_tar_add))
  849. {
  850. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  851. $i=1;
  852. // ----- Read new 512 block and write the already read
  853. do{
  854. // ----- Write the already read block
  855. $v_binary_data = pack("a512", "$v_buffer");
  856. if ($p_mode=="tar")
  857. fputs($p_tar, $v_binary_data);
  858. else
  859. gzputs($v_temp_tar, $v_binary_data);
  860. $i++;
  861. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  862. // ----- Read next block
  863. $v_buffer = fread($p_tar_add, 512);
  864. } while (!feof($p_tar_add));
  865. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  866. }
  867. // ----- Close the files
  868. fclose($p_tar_add);
  869. }
  870. // ----- Call the footer of the tar archive
  871. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  872. // ----- Look for closing compressed archive
  873. if ($p_mode == "tgz")
  874. {
  875. // ----- Close the files
  876. gzclose($p_tar);
  877. gzclose($v_temp_tar);
  878. // ----- Unlink tar file
  879. if (!@unlink($p_tarname))
  880. {
  881. // ----- Error log
  882. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  883. }
  884. // ----- Rename tar file
  885. if (!@rename($v_temp_tarname, $p_tarname))
  886. {
  887. // ----- Error log
  888. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  889. // ----- Return
  890. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  891. return PclErrorCode();
  892. }
  893. // ----- Return
  894. TrFctEnd(__FILE__, __LINE__, $v_result);
  895. return $v_result;
  896. }
  897. // ----- Look for closing uncompressed tar file
  898. else if ($p_mode=="tar")
  899. {
  900. // ----- Close the tarfile
  901. fclose($p_tar);
  902. }
  903. // ----- Return
  904. TrFctEnd(__FILE__, __LINE__, $v_result);
  905. return $v_result;
  906. }
  907. // --------------------------------------------------------------------------------
  908. // --------------------------------------------------------------------------------
  909. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  910. // ***** *****
  911. // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
  912. // --------------------------------------------------------------------------------
  913. // --------------------------------------------------------------------------------
  914. // Function : PclTarHandleCreate()
  915. // Description :
  916. // Parameters :
  917. // $p_tarname : Name of the tar file
  918. // $p_list : An array containing the file or directory names to add in the tar
  919. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  920. // Return Values :
  921. // --------------------------------------------------------------------------------
  922. function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir="", $p_remove_dir="")
  923. {
  924. TrFctStart(__FILE__, __LINE__, "PclTarHandleCreate", "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  925. $v_result=1;
  926. $v_list_detail = array();
  927. // ----- Check the parameters
  928. if (($p_tarname == "") || (($p_mode != "tar") && ($p_mode != "tgz")))
  929. {
  930. // ----- Error log
  931. if ($p_tarname == "")
  932. PclErrorLog(-3, "Invalid empty archive name");
  933. else
  934. PclErrorLog(-3, "Unknown mode '$p_mode'");
  935. // ----- Return
  936. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  937. return PclErrorCode();
  938. }
  939. // ----- Look for tar file
  940. if ($p_mode == "tar")
  941. {
  942. // ----- Open the tar file
  943. if (($p_tar = fopen($p_tarname, "wb")) == 0)
  944. {
  945. // ----- Error log
  946. PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
  947. // ----- Return
  948. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  949. return PclErrorCode();
  950. }
  951. // ----- Call the adding fct inside the tar
  952. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  953. {
  954. // ----- Call the footer of the tar archive
  955. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  956. }
  957. // ----- Close the tarfile
  958. fclose($p_tar);
  959. }
  960. // ----- Look for tgz file
  961. else
  962. {
  963. // ----- Open the tar file
  964. if (($p_tar = @gzopen($p_tarname, "wb")) == 0)
  965. {
  966. // ----- Error log
  967. PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
  968. // ----- Return
  969. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  970. return PclErrorCode();
  971. }
  972. // ----- Call the adding fct inside the tar
  973. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  974. {
  975. // ----- Call the footer of the tar archive
  976. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  977. }
  978. // ----- Close the tarfile
  979. gzclose($p_tar);
  980. }
  981. // ----- Return
  982. TrFctEnd(__FILE__, __LINE__, $v_result);
  983. return $v_result;
  984. }
  985. // --------------------------------------------------------------------------------
  986. // --------------------------------------------------------------------------------
  987. // Function : PclTarHandleAppend()
  988. // Description :
  989. // Parameters :
  990. // $p_tarname : Name of the tar file
  991. // $p_list : An array containing the file or directory names to add in the tar
  992. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  993. // Return Values :
  994. // --------------------------------------------------------------------------------
  995. function PclTarHandleAppend($p_tarname, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
  996. {
  997. TrFctStart(__FILE__, __LINE__, "PclTarHandleAppend", "tar=$p_tarname, list, mode=$p_mode");
  998. $v_result=1;
  999. // ----- Check the parameters
  1000. if ($p_tarname == "")
  1001. {
  1002. // ----- Error log
  1003. PclErrorLog(-3, "Invalid empty archive name");
  1004. // ----- Return
  1005. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1006. return PclErrorCode();
  1007. }
  1008. clearstatcache();
  1009. // ----- Check the file size
  1010. if ((!is_file($p_tarname)) ||
  1011. (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar")))
  1012. {
  1013. // ----- Error log
  1014. if (!is_file($p_tarname))
  1015. PclErrorLog(-4, "Archive '$p_tarname' does not exist");
  1016. else
  1017. PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)");
  1018. // ----- Return
  1019. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1020. return PclErrorCode();
  1021. }
  1022. // ----- Look for compressed archive
  1023. if ($p_mode == "tgz")
  1024. {
  1025. // ----- Open the file in read mode
  1026. if (($p_tar = @gzopen($p_tarname, "rb")) == 0)
  1027. {
  1028. // ----- Error log
  1029. PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
  1030. // ----- Return
  1031. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1032. return PclErrorCode();
  1033. }
  1034. // ----- Open a temporary file in write mode
  1035. $v_temp_tarname = uniqid("pcltar-").".tmp";
  1036. TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
  1037. if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
  1038. {
  1039. // ----- Close tar file
  1040. gzclose($p_tar);
  1041. // ----- Error log
  1042. PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
  1043. // ----- Return
  1044. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1045. return PclErrorCode();
  1046. }
  1047. // ----- Read the first 512 bytes block
  1048. $v_buffer = gzread($p_tar, 512);
  1049. // ----- Read the following blocks but not the last one
  1050. if (!gzeof($p_tar))
  1051. {
  1052. TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
  1053. $i=1;
  1054. // ----- Read new 512 block and write the already read
  1055. do{
  1056. // ----- Write the already read block
  1057. $v_binary_data = pack("a512", "$v_buffer");
  1058. gzputs($v_temp_tar, $v_binary_data);
  1059. $i++;
  1060. TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
  1061. // ----- Read next block
  1062. $v_buffer = gzread($p_tar, 512);
  1063. } while (!gzeof($p_tar));
  1064. TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
  1065. }
  1066. // ----- Call the adding fct inside the tar
  1067. if (($v_result = PclTarHandleAddList($v_temp_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  1068. {
  1069. // ----- Call the footer of the tar archive
  1070. $v_result = PclTarHandleFooter($v_temp_tar, $p_mode);
  1071. }
  1072. // ----- Close the files
  1073. gzclose($p_tar);
  1074. gzclose($v_temp_tar);
  1075. // ----- Unlink tar file
  1076. if (!@unlink($p_tarname))
  1077. {
  1078. // ----- Error log
  1079. PclErrorLog(-11, "Error while deleting archive name $p_tarname");
  1080. }
  1081. // ----- Rename tar file
  1082. if (!@rename($v_temp_tarname, $p_tarname))
  1083. {
  1084. // ----- Error log
  1085. PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
  1086. // ----- Return
  1087. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1088. return PclErrorCode();
  1089. }
  1090. // ----- Return
  1091. TrFctEnd(__FILE__, __LINE__, $v_result);
  1092. return $v_result;
  1093. }
  1094. // ----- Look for uncompressed tar file
  1095. else if ($p_mode=="tar")
  1096. {
  1097. // ----- Open the tar file
  1098. if (($p_tar = fopen($p_tarname, "r+b")) == 0)
  1099. {
  1100. // ----- Error log
  1101. PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
  1102. // ----- Return
  1103. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1104. return PclErrorCode();
  1105. }
  1106. // ----- Go to the beginning of last block
  1107. TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1108. fseek($p_tar, $v_size-512);
  1109. TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
  1110. // ----- Call the adding fct inside the tar
  1111. if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1)
  1112. {
  1113. // ----- Call the footer of the tar archive
  1114. $v_result = PclTarHandleFooter($p_tar, $p_mode);
  1115. }
  1116. // ----- Close the tarfile
  1117. fclose($p_tar);
  1118. }
  1119. // ----- Look for unknown type
  1120. else
  1121. {
  1122. // ----- Error log
  1123. PclErrorLog(-3, "Invalid tar mode $p_mode");
  1124. // ----- Return
  1125. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1126. return PclErrorCode();
  1127. }
  1128. // ----- Return
  1129. TrFctEnd(__FILE__, __LINE__, $v_result);
  1130. return $v_result;
  1131. }
  1132. // --------------------------------------------------------------------------------
  1133. // --------------------------------------------------------------------------------
  1134. // Function : PclTarHandleAddList()
  1135. // Description :
  1136. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  1137. // different from the real path of the file. This is usefull if you want to have PclTar
  1138. // running in any directory, and memorize relative path from an other directory.
  1139. // Parameters :
  1140. // $p_tar : File descriptor of the tar archive
  1141. // $p_list : An array containing the file or directory names to add in the tar
  1142. // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
  1143. // $p_list_detail : list of added files with their properties (specially the status field)
  1144. // $p_add_dir : Path to add in the filename path archived
  1145. // $p_remove_dir : Path to remove in the filename path archived
  1146. // Return Values :
  1147. // --------------------------------------------------------------------------------
  1148. function PclTarHandleAddList($p_tar, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
  1149. {
  1150. TrFctStart(__FILE__, __LINE__, "PclTarHandleAddList", "tar='$p_tar', list, mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
  1151. $v_result=1;
  1152. $v_header = array();
  1153. // ----- Recuperate the current number of elt in list
  1154. $v_nb = sizeof($p_list_detail);
  1155. // ----- Check the parameters
  1156. if ($p_tar == 0)
  1157. {
  1158. // ----- Error log
  1159. PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
  1160. // ----- Return
  1161. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1162. return PclErrorCode();
  1163. }
  1164. // ----- Check the arguments
  1165. if (sizeof($p_list) == 0)
  1166. {
  1167. // ----- Error log
  1168. PclErrorLog(-3, "Invalid file list parameter (invalid or empty list)");
  1169. // ----- Return
  1170. TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
  1171. return PclErrorCode();
  1172. }
  1173. // ----- Loop on the files
  1174. for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++)
  1175. {
  1176. // ----- Recuperate the filename
  1177. $p_filename = $p_list[$j];
  1178. TrFctMessage(__FILE__, __LINE__, 2, "Looking for file [$p_filename]");
  1179. // ----- Skip empty file names
  1180. if ($p_filename == "")
  1181. {
  1182. TrFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
  1183. continue;
  1184. }
  1185. // ----- Check the filename
  1186. if (!file_exists($p_filename))
  1187. {
  1188. // ----- Error log
  1189. TrFctMessage(__FILE__, __LINE__, 2, "File '$p_filename' does not exists");
  1190. PclErrorLog(-4, "File '$p_filename' does not exists");
  1191. // ----- Return
  1192. TrFctEnd(__FILE__, __LINE__, PclErrorCode(),

Large files files are truncated, but you can click here to view the full file