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