1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\File\Local; |
5
|
|
|
use phpbu\App\Exception; |
6
|
|
|
use phpbu\App\Util; |
7
|
|
|
use SplFileInfo; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Backup Target class. |
11
|
|
|
* |
12
|
|
|
* @package phpbu |
13
|
|
|
* @subpackage Backup |
14
|
|
|
* @author Sebastian Feldmann <[email protected]> |
15
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
16
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
17
|
|
|
* @link https://phpbu.de/ |
18
|
|
|
* @since Class available since Release 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class Target |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Path object. |
24
|
|
|
* |
25
|
|
|
* @var Path |
26
|
|
|
*/ |
27
|
|
|
private $path; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Backup filename. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $filename; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Filename with potential date placeholders like %d. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $filenameRaw; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* List of custom file suffixes f.e. 'tar' |
45
|
|
|
* |
46
|
|
|
* @var string[] |
47
|
|
|
*/ |
48
|
|
|
private $fileSuffixes = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Indicates if the filename changes over time. |
52
|
|
|
* |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
private $filenameIsChanging = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Target MIME type |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $mimeType = 'text/plain'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Size in bytes |
66
|
|
|
* |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
private $size; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Should the file be compressed. |
73
|
|
|
* |
74
|
|
|
* @var bool |
75
|
|
|
*/ |
76
|
|
|
private $compress = false; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* File compression. |
80
|
|
|
* |
81
|
|
|
* @var \phpbu\App\Backup\Target\Compression |
82
|
|
|
*/ |
83
|
|
|
private $compression; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Should the file be encrypted. |
87
|
|
|
* |
88
|
|
|
* @var bool |
89
|
|
|
*/ |
90
|
|
|
private $crypt = false; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* File crypter. |
94
|
|
|
* |
95
|
|
|
* @var \phpbu\App\Backup\Crypter |
96
|
|
|
*/ |
97
|
|
|
private $crypter; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Constructor. |
101
|
|
|
* |
102
|
|
|
* @param string $path |
103
|
|
|
* @param string $filename |
104
|
|
|
* @param integer $time |
105
|
|
|
*/ |
106
|
56 |
|
public function __construct($path, $filename, $time = null) |
107
|
|
|
{ |
108
|
56 |
|
$this->path = new Path($path, $time); |
109
|
56 |
|
$this->setFile($filename, $time); |
110
|
56 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Filename setter. |
114
|
|
|
* |
115
|
|
|
* @param string $file |
116
|
|
|
* @param int $time |
117
|
|
|
*/ |
118
|
56 |
|
public function setFile($file, $time = null) |
119
|
|
|
{ |
120
|
56 |
|
$this->filenameRaw = $file; |
121
|
56 |
|
if (Util\Path::isContainingPlaceholder($file)) { |
122
|
42 |
|
$this->filenameIsChanging = true; |
123
|
42 |
|
$file = Util\Path::replaceDatePlaceholders($file, $time); |
124
|
|
|
} |
125
|
56 |
|
$this->filename = $file; |
126
|
56 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Append another suffix to the filename. |
130
|
|
|
* |
131
|
|
|
* @param string $suffix |
132
|
|
|
*/ |
133
|
4 |
|
public function appendFileSuffix(string $suffix): void |
134
|
|
|
{ |
135
|
4 |
|
$this->fileSuffixes[] = $suffix; |
136
|
4 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Remove last file suffix if it matches |
140
|
|
|
* |
141
|
|
|
* @param string $suffix |
142
|
|
|
*/ |
143
|
1 |
|
public function removeFileSuffix(string $suffix): void |
144
|
|
|
{ |
145
|
1 |
|
$lastIndex = count($this->fileSuffixes) - 1; |
146
|
1 |
|
if ($this->fileSuffixes[$lastIndex] === $suffix) { |
147
|
1 |
|
unset($this->fileSuffixes[$lastIndex]); |
148
|
|
|
} |
149
|
1 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Checks if the backup target directory is writable. |
153
|
|
|
* Creates the Directory if it doesn't exist. |
154
|
|
|
* |
155
|
|
|
* @throws \phpbu\App\Exception |
156
|
|
|
*/ |
157
|
4 |
|
public function setupPath() |
158
|
|
|
{ |
159
|
|
|
// if directory doesn't exist, create it |
160
|
4 |
|
if (!is_dir($this->path->getPath())) { |
161
|
3 |
|
$reporting = error_reporting(); |
162
|
3 |
|
error_reporting(0); |
163
|
3 |
|
$created = mkdir($this->path->getPath(), 0755, true); |
164
|
3 |
|
error_reporting($reporting); |
165
|
3 |
|
if (!$created) { |
166
|
1 |
|
throw new Exception(sprintf('cant\'t create directory: %s', $this->path->getPath())); |
167
|
|
|
} |
168
|
|
|
} |
169
|
3 |
|
if (!is_writable($this->path->getPath())) { |
170
|
1 |
|
throw new Exception(sprintf('no write permission for directory: %s', $this->path->getPath())); |
171
|
|
|
} |
172
|
2 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Target file MIME type setter. |
176
|
|
|
* |
177
|
|
|
* @param string $mime |
178
|
|
|
*/ |
179
|
1 |
|
public function setMimeType(string $mime) |
180
|
|
|
{ |
181
|
1 |
|
$this->mimeType = $mime; |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Return the path to the backup file. |
186
|
|
|
* |
187
|
|
|
* @return Path |
188
|
|
|
*/ |
189
|
16 |
|
public function getPath() : Path |
190
|
|
|
{ |
191
|
16 |
|
return $this->path; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Return the name to the backup file. |
196
|
|
|
* |
197
|
|
|
* @param bool $plain |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
24 |
|
public function getFilename(bool $plain = false) : string |
201
|
|
|
{ |
202
|
24 |
|
return $this->filename . $this->getFilenameSuffix($plain); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Return the name of the backup file without compressor or encryption suffix. |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
1 |
|
public function getFilenamePlain() : string |
211
|
|
|
{ |
212
|
1 |
|
return $this->getFilename(true); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Return the raw name of the backup file incl. date placeholder. |
217
|
|
|
* |
218
|
|
|
* @param bool $plain |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
23 |
|
public function getFilenameRaw($plain = false) : string |
222
|
|
|
{ |
223
|
23 |
|
return $this->filenameRaw . $this->getFilenameSuffix($plain); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Return custom file suffix like '.tar'. |
228
|
|
|
* |
229
|
|
|
* @param bool $plain |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
36 |
|
public function getFilenameSuffix($plain = false) : string |
233
|
|
|
{ |
234
|
36 |
|
return $this->getSuffixToAppend() . ($plain ? '' : $this->getCompressionSuffix() . $this->getCrypterSuffix()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Return added suffixes. |
239
|
|
|
* |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
36 |
|
public function getSuffixToAppend() : string |
243
|
|
|
{ |
244
|
36 |
|
return count($this->fileSuffixes) ? '.' . implode('.', $this->fileSuffixes) : ''; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Return the compressor suffix. |
249
|
|
|
* |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
34 |
|
public function getCompressionSuffix() : string |
253
|
|
|
{ |
254
|
34 |
|
return $this->shouldBeCompressed() ? '.' . $this->compression->getSuffix() : ''; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Return the crypter suffix. |
259
|
|
|
* |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
34 |
|
public function getCrypterSuffix() : string |
263
|
|
|
{ |
264
|
34 |
|
return $this->shouldBeEncrypted() ? '.' . $this->crypter->getSuffix() : ''; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Return file MIME type. |
269
|
|
|
* |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
3 |
|
public function getMimeType() : string |
273
|
|
|
{ |
274
|
3 |
|
$mimeType = $this->mimeType; |
275
|
3 |
|
if ($this->shouldBeCompressed()) { |
276
|
1 |
|
$mimeType = $this->compression->getMimeType(); |
277
|
|
|
} |
278
|
3 |
|
return $mimeType; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Size setter. |
283
|
|
|
* |
284
|
|
|
* @param int $size |
285
|
|
|
*/ |
286
|
1 |
|
public function setSize(int $size) |
287
|
|
|
{ |
288
|
1 |
|
$this->size = $size; |
289
|
1 |
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Return the actual file size in bytes. |
293
|
|
|
* |
294
|
|
|
* @return int |
295
|
|
|
* @throws \phpbu\App\Exception |
296
|
|
|
*/ |
297
|
3 |
|
public function getSize() : int |
298
|
|
|
{ |
299
|
3 |
|
if (null === $this->size) { |
300
|
2 |
|
if (!file_exists($this)) { |
301
|
1 |
|
throw new Exception(sprintf('target file \'%s\' doesn\'t exist', $this->getFilename())); |
302
|
|
|
} |
303
|
1 |
|
$this->size = filesize($this); |
304
|
|
|
} |
305
|
2 |
|
return $this->size; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Target file exists already. |
310
|
|
|
* |
311
|
|
|
* @param bool $plain |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
1 |
|
public function fileExists(bool $plain = false) : bool |
315
|
|
|
{ |
316
|
1 |
|
return file_exists($this->getPathname($plain)); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Return as backup file object. |
321
|
|
|
* |
322
|
|
|
* @return \phpbu\App\Backup\File\Local |
323
|
|
|
*/ |
324
|
1 |
|
public function toFile() : Local |
325
|
|
|
{ |
326
|
1 |
|
return new Local(new SplFileInfo($this->getPathname())); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Return path and filename of the backup file. |
331
|
|
|
* |
332
|
|
|
* @param bool $plain |
333
|
|
|
* @return string |
334
|
|
|
*/ |
335
|
7 |
|
public function getPathname(bool $plain = false) : string |
336
|
|
|
{ |
337
|
7 |
|
return $this->path->getPath() . DIRECTORY_SEPARATOR . $this->getFilename($plain); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Return path and plain filename of the backup file. |
342
|
|
|
* |
343
|
|
|
* @return string |
344
|
|
|
*/ |
345
|
1 |
|
public function getPathnamePlain() : string |
346
|
|
|
{ |
347
|
1 |
|
return $this->getPathname(true); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Filename configured with any date placeholders. |
352
|
|
|
* |
353
|
|
|
* @return bool |
354
|
|
|
*/ |
355
|
2 |
|
public function hasChangingFilename() : bool |
356
|
|
|
{ |
357
|
2 |
|
return $this->filenameIsChanging; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Disable file compression. |
362
|
|
|
*/ |
363
|
2 |
|
public function disableCompression() |
364
|
|
|
{ |
365
|
2 |
|
$this->compress = false; |
366
|
2 |
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Enable file compression. |
370
|
|
|
* |
371
|
|
|
* @throws \phpbu\App\Exception |
372
|
|
|
*/ |
373
|
2 |
|
public function enableCompression() |
374
|
|
|
{ |
375
|
2 |
|
if (null == $this->compression) { |
376
|
1 |
|
throw new Exception('can\'t enable compression without a compressor'); |
377
|
|
|
} |
378
|
1 |
|
$this->compress = true; |
379
|
1 |
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Compression setter. |
383
|
|
|
* |
384
|
|
|
* @param \phpbu\App\Backup\Target\Compression $compression |
385
|
|
|
*/ |
386
|
12 |
|
public function setCompression(Target\Compression $compression) |
387
|
|
|
{ |
388
|
12 |
|
$this->compression = $compression; |
389
|
12 |
|
$this->compress = true; |
390
|
12 |
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Compressor getter. |
394
|
|
|
* |
395
|
|
|
* @return \phpbu\App\Backup\Target\Compression |
396
|
|
|
*/ |
397
|
1 |
|
public function getCompression() : Target\Compression |
398
|
|
|
{ |
399
|
1 |
|
return $this->compression; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Is a compressor set? |
404
|
|
|
* |
405
|
|
|
* @return bool |
406
|
|
|
*/ |
407
|
39 |
|
public function shouldBeCompressed() : bool |
408
|
|
|
{ |
409
|
39 |
|
return $this->compress !== false; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Crypter setter. |
414
|
|
|
* |
415
|
|
|
* @param \phpbu\App\Backup\Crypter $crypter |
416
|
|
|
*/ |
417
|
3 |
|
public function setCrypter(Crypter $crypter) |
418
|
|
|
{ |
419
|
3 |
|
$this->crypter = $crypter; |
420
|
3 |
|
$this->crypt = true; |
421
|
3 |
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Crypter getter. |
425
|
|
|
* |
426
|
|
|
* @return \phpbu\App\Backup\Crypter |
427
|
|
|
*/ |
428
|
1 |
|
public function getCrypter() : Crypter |
429
|
|
|
{ |
430
|
1 |
|
return $this->crypter; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Disable file encryption. |
435
|
|
|
*/ |
436
|
1 |
|
public function disableEncryption() |
437
|
|
|
{ |
438
|
1 |
|
$this->crypt = false; |
439
|
1 |
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Is a crypter set? |
443
|
|
|
* |
444
|
|
|
* @return bool |
445
|
|
|
*/ |
446
|
35 |
|
public function shouldBeEncrypted() : bool |
447
|
|
|
{ |
448
|
35 |
|
return $this->crypt !== false; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Magic to string method. |
453
|
|
|
* |
454
|
|
|
* @return string |
455
|
|
|
*/ |
456
|
2 |
|
public function __toString() : string |
457
|
|
|
{ |
458
|
2 |
|
return $this->getPathname(); |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|