PageRenderTime 56ms CodeModel.GetById 25ms 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

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

  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 …

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