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