-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsftp.php
147 lines (125 loc) · 4.33 KB
/
sftp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
class Sftp {
private $host;
private $port;
private $session;
private $sftp;
private $privateKey;
private $publicKey;
public function __construct(string $host, int $port=22) {
$this->host = $host;
$this->port = $port;
}
/**
* Sets, and activates, the path to public and private SSH key
* @param string $public Path to public-key (~/.ssh/id_rsa.pub)
* @param string $private Path to private-key (~/.ssh/id_rsa)
*/
public function keys(string $public, string $private) {
$this->privateKey = $private;
$this->publicKey = $public;
}
/**
* Connect to a server
* @param string $username The username to connect with
* @param string|null $password The password to use. Can be left
* blank, ie. if using keys
*/
public function connect(string $username, string $password=null) {
$failed = false;
if (!$this->session = ssh2_connect($this->host, $this->port)){
$failed = true;
throw new Exception("Cannot connect to server");
}
# Password
if (!isset($this->privateKey)){
if (!ssh2_auth_password($this->session, $username, $password)){
$failed = true;
throw new Exception("Invalid username or password");
}
}
# Priv/pub keys
if (isset($this->privateKey)){
if (!ssh2_auth_pubkey_file($this->session,
$username,
$this->publicKey,
$this->privateKey,
$password )){
$failed = true;
throw new Exception("Authentication rejected by server");
}
}
if(!$failed)
$this->sftp = ssh2_sftp($this->session);
}
/**
* Scans a given directory on the connected host
* @param string $dir The directory to scan
*/
public function ls(string $dir){
if(isset($this->sftp))
return scandir('ssh2.sftp://'.(int)$this->sftp . $dir);
}
/**
* Send a file to the host
* @param string $src Source destination (path/local)
* @param string $dst Remote destination (path/remote)
* @param int|integer $chmod Permissions. Default rw, r, r
*/
public function sendFile(string $src, string $dst, int $chmod=644) {
$chmod = "0"+$chmod;
if(isset($this->sftp)){
$stream = @fopen("ssh2.sftp://".$this->session . $dst, 'w');
$data = @file_get_contents($src);
if (!$stream)
throw new Exception("Could not open remote file: ".$dst);
if ($data === false)
throw new Exception("Could not open local file: ".$src);
if (@fwrite($stream, $data) === false)
throw new Exception("Could not send data from file: ".$src);
@fclose($stream);
}
}
/**
* Request a file from the host
* @param string $src Source destination (path/remote)
* @param string $dst Local destination (path/local)
*/
public function receiveFile(string $src, string $dst) {
if(isset($this->sftp))
return ssh2_scp_recv($this->session, $src, $dst);
}
/**
* Open a file on the host
* @param string $dst The destination of the file (remote)
*/
public function openFile(string $dst) {
if(isset($this->sftp))
return fopen("ssh2.sftp://".(int)$sftp . $dst, 'r');
}
/**
* Deletes a file from the host
* @param string $dst The destination file (path/remote)
*/
public function removeFile(string $dst) {
if ($this->sftp)
return ssh2_sftp_unlink($this->sftp, $dst);
}
/**
* Creates a new folder on host
* @param string $dst Destination for the foler (path/remote)
*/
public function createFolder(string $dst) {
if ($this->sftp)
return ssh2_sftp_mkdir($this->sftp, $dst);
}
/**
* Deletes an EMPTY folder
* @param string $dst The folder to remove (Needs to be empty)
*/
public function removeFolder(string $dst) {
if ($this->sftp)
return ssh2_sftp_rmdir($this->sftp, $dst);
}
}
?>