PageRenderTime 63ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

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

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