1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* An entity on the filesystem, used as a base class for Webroot and SSPakFile |
5
|
|
|
*/ |
6
|
|
|
class FilesystemEntity { |
7
|
|
|
protected $server; |
8
|
|
|
protected $path; |
9
|
|
|
protected $executor; |
10
|
|
|
protected $identity = null; |
11
|
|
|
|
12
|
|
|
function __construct($path, $executor) { |
|
|
|
|
13
|
|
|
$this->executor = $executor; |
14
|
|
|
|
15
|
|
|
if(strpos($path,':') !== false) { |
16
|
|
|
list($this->server,$this->path) = explode(':', $path, 2); |
17
|
|
|
} else { |
18
|
|
|
$this->server = null; |
19
|
|
|
$this->path = $path; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
function isLocal() { |
|
|
|
|
24
|
|
|
return $this->server == null; |
25
|
|
|
} |
26
|
|
|
function getPath() { |
|
|
|
|
27
|
|
|
return $this->path; |
28
|
|
|
} |
29
|
|
|
function getServer() { |
|
|
|
|
30
|
|
|
return $this->server; |
31
|
|
|
} |
32
|
|
|
function setSSHItentityFile($filename) { |
|
|
|
|
33
|
|
|
$this->identity = $filename; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Execute a command on the relevant server |
38
|
|
|
* @param string $command Shell command, either a fully escaped string or an array |
39
|
|
|
*/ |
40
|
|
|
function exec($command, $options = array()) { |
|
|
|
|
41
|
|
|
return $this->createProcess($command, $options)->exec(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a process for later exection |
46
|
|
|
* @param string $command Shell command, either a fully escaped string or an array |
47
|
|
|
* @return Process |
48
|
|
|
*/ |
49
|
|
|
function createProcess($command, $options = array()) { |
|
|
|
|
50
|
|
|
if($this->server) { |
51
|
|
|
if ($this->identity && !isset($options['identity'])) { |
52
|
|
|
$options['identity'] = $this->identity; |
53
|
|
|
} |
54
|
|
|
return $this->executor->createRemote($this->server, $command, $options); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->executor->createLocal($command, $options); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Upload a file to the given destination on the server |
62
|
|
|
* @param string $file The file to upload |
|
|
|
|
63
|
|
|
* @param string $dest The remote filename/dir to upload to |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
function upload($source, $dest) { |
|
|
|
|
66
|
|
|
if($this->server) { |
67
|
|
|
$this->executor->execLocal(array("scp", $source, "$this->server:$dest")); |
68
|
|
|
} else { |
69
|
|
|
$this->executor->execLocal(array("cp", $source, $dest)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create a file with the given content at the given destination on the server |
75
|
|
|
* @param string $content The content of the file |
76
|
|
|
* @param string $dest The remote filename/dir to upload to |
77
|
|
|
*/ |
78
|
|
|
function uploadContent($content, $dest) { |
|
|
|
|
79
|
|
|
$this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($dest)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Download a file from the given source on the server to the given file |
84
|
|
|
* @param string $source The remote filename to download |
85
|
|
|
* @param string $dest The local filename/dir to download to |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
function download($source, $dest) { |
|
|
|
|
88
|
|
|
if($this->server) { |
89
|
|
|
$this->executor->execLocal(array("scp", "$this->server:$source", $dest)); |
90
|
|
|
} else { |
91
|
|
|
$this->executor->execLocal(array("cp", $file, $dest)); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns true if the given file or directory exists |
97
|
|
|
* @param string $file The file/dir to look for |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
|
|
function exists($file = null) { |
|
|
|
|
101
|
|
|
if(!$file) $file = $this->path; |
|
|
|
|
102
|
|
|
if($file == '@self') return true; |
103
|
|
|
|
104
|
|
|
if($this->server) { |
105
|
|
|
$result = $this->exec("if [ -e " . escapeshellarg($file) . " ]; then echo yes; fi"); |
106
|
|
|
return (trim($result['output']) == 'yes'); |
107
|
|
|
|
108
|
|
|
} else { |
109
|
|
|
return file_exists($file); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create the given file with the given content |
115
|
|
|
*/ |
116
|
|
|
function writeFile($file, $content) { |
|
|
|
|
117
|
|
|
if($this->server) { |
118
|
|
|
$this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($file)); |
119
|
|
|
|
120
|
|
|
} else { |
121
|
|
|
file_put_contents($file, $content); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Remove a file or folder from the webroot's server |
127
|
|
|
* |
128
|
|
|
* @param string $file The file to remove |
129
|
|
|
*/ |
130
|
|
|
function unlink($file) { |
|
|
|
|
131
|
|
|
if(!$file || $file == '/' || $file == '.') throw new Exception("Can't unlink file '$file'"); |
132
|
|
|
$this->exec(array('rm', '-rf', $file)); |
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.