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