Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
 * FTP wrapper class
 * @author Tjaart Viljoen
 * @copyright 2013 Nirph Online
 */
namespace Utility\Remote;

class Ftp
{
	protected $_serverAddress;
	protected $_connection;
	protected $_username;
	protected $_password;
	protected $_verbose;
	protected $_timeout = 60;

	/**
	 * Class constructor.
	 * Set default parameters.
	 * @param string $serverAddress
	 * @param string $username
	 * @param string $password
	 */
	function __construct($serverAddress, $username, $password, $timeout = 60, $verbose = false)
	{
		$this->_serverAddress = $serverAddress;
		$this->_username	  = $username;
		$this->_password	  = $password;
		$this->_verbose		  = $verbose;
		$this->_timeout		  = $timeout;
	}

	/**
	 * Change remote FTP directory.
	 * @param unknown $directory
	 * @throws \ErrorException
	 * @return boolean
	 */
	public function changeDirectory($directory)
	{
		$this->_secureConnection();
		$this->_output('Changing directory to "' . $directory . '"');
		$result = ftp_chdir($this->_connection, $directory);

		if(false == $result)
		{
			error_log('Couldn\'t change to FTP directory "' . $directory . '"');
			throw new \Exception('Couldn\'t change to FTP directory "' . $directory . '"');
			return false;
		}
		return $result;
	}

	/**
	 * Create a new remote directory.
	 * @param string $directory
	 * @throws \Exception
	 * @return boolean
	 */
	public function createDirectory($directory)
	{
		$this->_secureConnection();
		$this->_output('Creating directory "' . $directory . '"');
		$result = ftp_mkdir($this->_connection, $directory);
		if(false == $result)
		{
			$error = 'Couldn\'t create remote FTP directory "' . $directory . '"' ;
			error_log($error);
			throw new \Exception($error);
			return false;
		}
		return true;
	}

	/**
	 * Connect to the remote server.
	 * @throws \Exception
	 * @return \Utility\Remote\Ftp
	 */
	public function connect()
	{
		$this->_connection = ftp_connect($this->_serverAddress);

		if(false == $this->_connection)
		{
			throw new \Exception("Couldn't connect to FTP server: " . $this->_serverAddress );
		}
		$this->_output('Connected to "' . $this->_serverAddress . '"');
		return $this;
	}

	/**
	 * Close the remote FTP server connection.
	 */
	public function closeConnection()
	{
		$this->_output('Closing FTP connection');
		ftp_close($this->_connection);
		$this->_connection = null;
	}

	/**
	 * Login to the remote FTP server.
	 * @throws \Exception
	 * @return \Utility\Remote\Ftp
	 */
	protected function _login()
	{
		$this->_output('Logging in to "' . $this->_serverAddress . '"');
		$logingResult = ftp_login($this->_connection, $this->_username, $this->_password);

		if(false == $logingResult)
		{
			$this->_output('Login failure');
			$this->closeConnection();
			throw new \Exception("Couldn't log into remote server, please check your credentials and try again");
		}
		$this->_output('Logged in to "' . $this->_serverAddress . '"');
		return $this;
	}

	/**
	 * $return the content of a directory.
	 * Returns an empty array on error.
	 * @param string $directory
	 * @return array
	 */
	public function getDirectoryContents($directory = '.')
	{
		$this->_secureConnection();
		$this->_output('Retrieving direcory contents for "' . $directory . '"');
		$contents = ftp_nlist($this->_connection, '.');

		if(false == $contents)
		{
			error_log('Couldn\'t retrieve directory contents for: ' . $this->_serverAddress . ', directory: "' . $directory . '"');
			return array();
		}
		return $contents;
	}

	/**
	 * Delete a remote Directory
	 * @example deleteRemoteDirectory('./relative-path-directory');
	 * @example deleteRemoteDirectory('/temp/absolute-path-directory');
	 * @param string $directory
	 * @return boolean
	 */
	public function deleteRemoteDirectory($directory)
	{
		$this->_secureConnection();
		$this->_output('Deleting direcory "' . $directory . '"');
		$result = ftp_rmdir($this->_connection, $directory);

		if(false == $result)
		{
			error_log('Couldn\'t delete remote FTP directory "' . $directory . '"');
			return false;
		}

		return true;
	}

	/**
	 * Delete a remote file
	 * @param string $filename
	 * @return boolean
	 */
	public function deleteRemoteFile($filename)
	{
		$this->_secureConnection();
		$this->_output('Deleting file "' . $filename . '"');
		$result = ftp_delete($this->_connection, $filename);

		if(false == $result)
		{
			error_log('Couldn\'t delete remote FTP file "' . $filename . '"');
			return false;
		}

		return true;
	}

	/**
	 * Download a remote file to a specified location.
	 * @param string $localFilename
	 * @param string $remoteFilename
	 * @throws \Exception
	 * @return void|boolean
	 */
	public function downloadFile($localFilename, $remoteFilename)
	{
		set_time_limit(0);
		$this->_secureConnection();
		$this->_output('Downloading file"' . $remoteFilename . '" to "' . $localFilename . '"');
		ftp_pasv($this->_connection, true);
		if(ftp_get($this->_connection, $localFilename, $remoteFilename, FTP_BINARY))
		{
			ftp_pasv($this->_connection, false);
			return true;
		}
		ftp_pasv($this->_connection, false);
		return false;
	}

	/**
	 * Upload a specified file.
	 * @param string $localFilename
	 * @param string $remoteFilename
	 * @throws \Exception
	 * @return void|boolean
	 */
	public function uploadFile($localFilename, $remoteFilename, $mode = FTP_BINARY)
	{
		set_time_limit(0);
		$this->_secureConnection();
		$this->_output('Uploading file"' . $localFilename . '" to "' . $remoteFilename . '"');
		ftp_pasv($this->_connection, true);
		if(ftp_put($this->_connection, $remoteFilename, $localFilename, $mode))
		{
			ftp_pasv($this->_connection, false);
			return true;
		}

		ftp_pasv($this->_connection, false);

		$error = 'Couldn\'t upload "' . $localFilename . '" to remote location "' . $remoteFilename . '"';
		error_log($error);
		throw new \Exception($error);
		return false;
	}

	/**
	 * Checks the connection and reconnects to the remote FTP server if necessary
	 */
	protected function _secureConnection()
	{
		if(null == $this->_connection)
		{
			$this->connect();
			$this->_login();
		}
	}

	/**
	 * Returns the present working directory
	 * @return string
	 */
	public function whereAmI()
	{
		$this->_secureConnection();
		return (string) ftp_pwd($this->_connection);
	}

	protected function _output($string)
	{
		if(false == $this->_verbose)
		{
			return;
		}
		echo $string . PHP_EOL;
	}
}

Commits for namibiamodule/Utility/src/Utility/Remote/Ftp.php

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit