1
|
|
|
<?php namespace Cornford\Backup; |
2
|
|
|
|
3
|
|
|
use Cornford\Backup\Contracts\BackupFilesystemInterface; |
4
|
|
|
|
5
|
|
|
class BackupFilesystem implements BackupFilesystemInterface { |
6
|
|
|
|
7
|
|
|
const OS_UNKNOWN = 1; |
8
|
|
|
const OS_WIN = 2; |
9
|
|
|
const OS_LINUX = 3; |
10
|
|
|
const OS_OSX = 4; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Check path exists. |
14
|
|
|
* |
15
|
|
|
* @param string $path |
16
|
|
|
* |
17
|
|
|
* @return boolean |
18
|
|
|
*/ |
19
|
|
|
public function checkPathExists($path) |
20
|
|
|
{ |
21
|
|
|
if (!is_dir($path)) { |
22
|
|
|
return false; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Check file exists. |
30
|
|
|
* |
31
|
|
|
* @param string $filepath |
32
|
|
|
* |
33
|
|
|
* @return boolean |
34
|
|
|
*/ |
35
|
|
|
public function checkFileExists($filepath) |
36
|
|
|
{ |
37
|
|
|
if (!$this->checkPathExists(dirname($filepath))) { |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!is_file($filepath)) { |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check if file is empty. |
50
|
|
|
* |
51
|
|
|
* @param string $filepath |
52
|
|
|
* |
53
|
|
|
* @return boolean |
54
|
|
|
*/ |
55
|
|
|
public function checkFileEmpty($filepath) |
56
|
|
|
{ |
57
|
|
|
return filesize($filepath) == 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Remove file. |
62
|
|
|
* |
63
|
|
|
* @param string $filepath |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function removeFile($filepath) |
68
|
|
|
{ |
69
|
|
|
unlink($filepath); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Write a compressed file. |
74
|
|
|
* |
75
|
|
|
* @param string $compressedFilepath |
76
|
|
|
* @param string $filepath |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function writeCompressedFile($compressedFilepath, $filepath) |
81
|
|
|
{ |
82
|
|
|
$filePointer = fopen($compressedFilepath, 'w'); |
83
|
|
|
fwrite($filePointer, gzencode(file_get_contents($filepath), 9)); |
84
|
|
|
fclose($filePointer); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Write an uncompressed file. |
89
|
|
|
* |
90
|
|
|
* @param string $uncompressedFilepath |
91
|
|
|
* @param string $filepath |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function writeUncompressedFile($uncompressedFilepath, $filepath) |
96
|
|
|
{ |
97
|
|
|
$filePointer = fopen($uncompressedFilepath, 'w'); |
98
|
|
|
fwrite($filePointer, gzdecode(file_get_contents($filepath, 9))); |
99
|
|
|
fclose($filePointer); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the operating system. |
104
|
|
|
* |
105
|
|
|
* @return integer |
106
|
|
|
*/ |
107
|
|
|
public function getOperatingSystem() |
108
|
|
|
{ |
109
|
|
|
switch (true) { |
|
|
|
|
110
|
|
|
case stristr(PHP_OS, 'DAR'): |
111
|
|
|
return self::OS_OSX; |
112
|
|
|
case stristr(PHP_OS, 'WIN'): |
113
|
|
|
return self::OS_WIN; |
114
|
|
|
case stristr(PHP_OS, 'LINUX'): |
115
|
|
|
return self::OS_LINUX; |
116
|
|
|
default: |
117
|
|
|
return self::OS_UNKNOWN; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Locate command location. |
123
|
|
|
* |
124
|
|
|
* @param string $command |
125
|
|
|
* |
126
|
|
|
* @return string|false |
127
|
|
|
*/ |
128
|
|
|
public function locateCommand($command) |
129
|
|
|
{ |
130
|
|
|
switch ($this->getOperatingSystem()) { |
131
|
|
|
case self::OS_OSX: |
132
|
|
|
case self::OS_LINUX: |
133
|
|
|
exec(sprintf('which %s', $command), $result, $returnCode); |
134
|
|
|
if (isset($result[0])) { |
135
|
|
|
$result = substr($result[0], 0, strrpos($result[0], '/') + 1); |
136
|
|
|
} |
137
|
|
|
break; |
138
|
|
|
case self::OS_WIN: |
139
|
|
|
exec(sprintf('where %s', $command), $result, $returnCode); |
140
|
|
|
if (isset($result[0])) { |
141
|
|
|
$result = $result[0]; |
142
|
|
|
} |
143
|
|
|
break; |
144
|
|
|
default: |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (empty($result)) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $result; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Check a function exists. |
157
|
|
|
* |
158
|
|
|
* @param string $function |
159
|
|
|
* |
160
|
|
|
* @return boolean |
161
|
|
|
*/ |
162
|
|
|
public function checkFunctionExists($function) |
163
|
|
|
{ |
164
|
|
|
return function_exists($function); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |