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