text changes to registration mail content
[namibia] / module / Utility / src / Utility / Remote / Sftp.php
1 <?php
2 /**
3  * Simple FTP Class
4  *
5  * @package Sftp
6  * @name Sftp
7  * @version 1.0
8  * @author Shay Anderson 05.11
9  * @link shayanderson.com
10  * @license http://www.gnu.org/licenses/gpl.html GPL License
11  * SFTP is free software and is distributed WITHOUT ANY WARRANTY
12  */
13 namespace Utility\Remote;
14
15
16
17 class Sftp
18 {
19     /**
20      * FTP host
21      *
22      * @var string $_host
23      */
24     private $_host;
25
26     /**
27      * FTP port
28      *
29      * @var int $_port
30      */
31     private $_port = 21;
32
33     /**
34      * FTP password
35      *
36      * @var string $_pwd
37      */
38     private $_pwd;
39
40     /**
41      * FTP stream
42      *
43      * @var resource $_id
44      */
45     private $_stream;
46
47     /**
48      * FTP timeout
49      *
50      * @var int $_timeout
51      */
52     private $_timeout = 90;
53
54     /**
55      * FTP user
56      *
57      * @var string $_user
58      */
59     private $_user;
60
61     /**
62      * Last error
63      *
64      * @var string $error
65      */
66     public $error;
67
68     /**
69      * FTP passive mode flag
70      *
71      * @var bool $passive
72      */
73     public $passive = false;
74
75     /**
76      * SSL-FTP connection flag
77      *
78      * @var bool $ssl
79      */
80     public $ssl = false;
81
82     /**
83      * System type of FTP server
84      *
85      * @var string $system_type
86      */
87     public $system_type;
88
89     /**
90      * Initialize connection params
91      *
92      * @param string $host
93      * @param string $user
94      * @param string $password
95      * @param int $port
96      * @param int $timeout (seconds)
97      */
98     public function  __construct($host = null, $user = null, $password = null, $port = 21, $timeout = 90) {
99         $this->_host = $host;
100         $this->_user = $user;
101         $this->_pwd = $password;
102         $this->_port = (int)$port;
103         $this->_timeout = (int)$timeout;
104     }
105
106     /**
107      * Auto close connection
108      */
109     public function  __destruct() {
110         $this->close();
111     }
112
113     /**
114      * Change currect directory on FTP server
115      *
116      * @param string $directory
117      * @return bool
118      */
119     public function cd($directory = null) {
120         // attempt to change directory
121         if(ftp_chdir($this->_stream, $directory)) {
122             // success
123             return true;
124             // fail
125         } else {
126             $this->error = "Failed to change directory to \"{$directory}\"";
127             return false;
128         }
129     }
130
131     /**
132      * Set file permissions
133      *
134      * @param int $permissions (ex: 0644)
135      * @param string $remote_file
136      * @return false
137      */
138     public function chmod($permissions = 0, $remote_file = null) {
139         // attempt chmod
140         if(ftp_chmod($this->_stream, $permissions, $remote_file)) {
141             // success
142             return true;
143             // failed
144         } else {
145             $this->error = "Failed to set file permissions for \"{$remote_file}\"";
146             return false;
147         }
148     }
149
150     /**
151      * Close FTP connection
152      */
153     public function close() {
154         // check for valid FTP stream
155         if($this->_stream) {
156             // close FTP connection
157             ftp_close($this->_stream);
158
159             // reset stream
160             $this->_stream = false;
161         }
162     }
163
164     /**
165      * Connect to FTP server
166      *
167      * @return bool
168      */
169     public function connect() {
170         // check if non-SSL connection
171         if(!$this->ssl) {
172             // attempt connection
173             if(!$this->_stream = ftp_connect($this->_host, $this->_port, $this->_timeout)) {
174                 // set last error
175                 $this->error = "Failed to connect to {$this->_host}";
176                 return false;
177             }
178             // SSL connection
179         } elseif(function_exists("ftp_ssl_connect")) {
180             // attempt SSL connection
181             if(!$this->_stream = ftp_ssl_connect($this->_host, $this->_port, $this->_timeout)) {
182                 // set last error
183                 $this->error = "Failed to connect to {$this->_host} (SSL connection)";
184                 return false;
185             }
186             // invalid connection type
187         } else {
188             $this->error = "Failed to connect to {$this->_host} (invalid connection type)";
189             return false;
190         }
191
192         // attempt login
193         if(ftp_login($this->_stream, $this->_user, $this->_pwd)) {
194             // set passive mode
195             ftp_pasv($this->_stream, (bool)$this->passive);
196
197             // set system type
198             $this->system_type = ftp_systype($this->_stream);
199
200             // connection successful
201             return true;
202             // login failed
203         } else {
204             $this->error = "Failed to connect to {$this->_host} (login failed)";
205             return false;
206         }
207     }
208
209     /**
210      * Delete file on FTP server
211      *
212      * @param string $remote_file
213      * @return bool
214      */
215     public function delete($remote_file = null) {
216         // attempt to delete file
217         if(ftp_delete($this->_stream, $remote_file)) {
218             // success
219             return true;
220             // fail
221         } else {
222             $this->error = "Failed to delete file \"{$remote_file}\"";
223             return false;
224         }
225     }
226
227     /**
228      * Download file from server
229      *
230      * @param string $remote_file
231      * @param string $local_file
232      * @param int $mode
233      * @return bool
234      */
235     public function get($remote_file = null, $local_file = null, $mode = FTP_ASCII) {
236         // attempt download
237         if(ftp_get($this->_stream, $local_file, $remote_file, $mode)) {
238             // success
239             return true;
240             // download failed
241         } else {
242             $this->error = "Failed to download file \"{$remote_file}\"";
243             return false;
244         }
245     }
246
247     /**
248      * Get list of files/directories in directory
249      *
250      * @param string $directory
251      * @return array
252      */
253     public function ls($directory = null) {
254         $list = array();
255
256         // attempt to get list
257         if($list = ftp_nlist($this->_stream, $directory)) {
258             // success
259             return $list;
260             // fail
261         } else {
262             $this->error = "Failed to get directory list";
263             return array();
264         }
265     }
266
267     /**
268      * Create directory on FTP server
269      *
270      * @param string $directory
271      * @return bool
272      */
273     public function mkdir($directory = null) {
274         // attempt to create dir
275         if(ftp_mkdir($this->_stream, $directory)) {
276             // success
277             return true;
278             // fail
279         } else {
280             $this->error = "Failed to create directory \"{$directory}\"";
281             return false;
282         }
283     }
284
285     /**
286      * Upload file to server
287      *
288      * @param string $local_path
289      * @param string $remote_file_path
290      * @param int $mode
291      * @return bool
292      */
293     public function put($local_file = null, $remote_file = null, $mode = FTP_ASCII) {
294         // attempt to upload file
295         if(ftp_put($this->_stream, $remote_file, $local_file, $mode)) {
296             // success
297             return true;
298             // upload failed
299         } else {
300             $this->error = "Failed to upload file \"{$local_file}\"";
301             return false;
302         }
303     }
304
305     /**
306      * Get current directory
307      *
308      * @return string
309      */
310     public function pwd() {
311         return ftp_pwd($this->_stream);
312     }
313
314     /**
315      * Rename file on FTP server
316      *
317      * @param string $old_name
318      * @param string $new_name
319      * @return bool
320      */
321     public function rename($old_name = null, $new_name = null) {
322         // attempt rename
323         if(ftp_rename($this->_stream, $old_name, $new_name)) {
324             // success
325             return true;
326             // fail
327         } else {
328             $this->error = "Failed to rename file \"{$old_name}\"";
329             return false;
330         }
331     }
332
333     /**
334      * Remove directory on FTP server
335      *
336      * @param string $directory
337      * @return bool
338      */
339     public function rmdir($directory = null) {
340         // attempt remove dir
341         if(ftp_rmdir($this->_stream, $directory)) {
342             // success
343             return true;
344             // fail
345         } else {
346             $this->error = "Failed to remove directory \"{$directory}\"";
347             return false;
348         }
349     }
350 }