Skip to content

Commit 6aa9ee1

Browse files
author
Mark S.
committed
Merge pull request #33 from fotografde/develop
Develop
2 parents e837cd8 + 184e850 commit 6aa9ee1

5 files changed

Lines changed: 90 additions & 28 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ matrix:
2323
- DB=mysql CAKE_VERSION=master COVERALLS=1
2424

2525
before_script:
26+
- composer selfupdate
2627
- git clone -b master https://github.com/FriendsOfCake/travis.git --depth 1 ../travis
2728
- ../travis/before_script.sh
2829
- cd ../cakephp/app
30+
- ln -s ~/.composer/vendor/phpseclib ../vendors/phpseclib
2931
- echo "Configure::write('Security.salt', 'AycG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');" >> Config/bootstrap.php
3032
- echo "Configure::write('Security.cipherSeed', '16659201697453542496749683615');" >> Config/bootstrap.php
3133
- cd ..

Controller/FtpAppController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class FtpAppController extends AppController {
1616
*/
1717
public function __construct($request = null, $response = null) {
1818
parent::__construct($request, $response);
19-
if (Configure::read('Cakeftp.enabled') !== true) {
20-
throw new InternalErrorException(__d('cakeftp', 'CakeFTP client/console are disabled by default for security. To enable put Configure::write(\'Cakeftp.enabled\', true); in your Config/bootstrap.php or AppController.php.'));
19+
// The latter is deprecated
20+
if (!Configure::read('Ftp.enabled') && !Configure::read('Cakeftp.enabled')) {
21+
throw new InternalErrorException(__d('cakeftp', 'CakePHP FTP client/console are disabled by default for security. To enable put Configure::write(\'Ftp.enabled\', true); in your Config/bootstrap.php or config file.'));
2122
}
2223
}
2324
}

Model/Datasource/FtpSource.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function init($config = array()) {
8888
if (!empty($config['type'])) {
8989
$config['type'] = strtolower($config['type']);
9090
}
91-
$this->config = array_merge($this->config, (array)$config);
91+
$this->config = $config + $this->config;
9292
if ($this->config['cache'] === true) {
9393
Cache::config('cakeftp', array('engine' => 'File', 'prefix' => 'cakeftp_'));
9494
$this->config['cache'] = 'cakeftp';
@@ -213,7 +213,7 @@ public function create(Model $model, $fields = null, $values = null) {
213213
}
214214
$data['direction'] = (!empty($data['direction'])) ? strtolower($data['direction']) : 'up';
215215
$model->id = dirname($data['remote']);
216-
if ($this->config['type'] == "ftp") {
216+
if ($this->config['type'] === "ftp") {
217217
if (!$this->_ftp('ftp_chdir', array($this->config['connection'], $model->id))) {
218218
throw new Exception(__d('cakeftp', 'Could not change directory'));
219219
}
@@ -235,7 +235,7 @@ public function create(Model $model, $fields = null, $values = null) {
235235
$this->_ftp('unlink', array($data['local']));
236236
throw new Exception(__d('cakeftp', 'Failed to download'));
237237
}
238-
} elseif ($this->config['type'] == "ssh") {
238+
} elseif ($this->config['type'] === "ssh") {
239239
$this->config['connection']->chdir($model->id);
240240
switch ($data['direction']) {
241241
case 'up':
@@ -291,11 +291,11 @@ public function delete(Model $model, $file = null) {
291291
if (!$this->connect()) {
292292
throw new Exception(__d('cakeftp', 'Failed to connect'));
293293
}
294-
if ($this->config['type'] == "ftp") {
294+
if ($this->config['type'] === "ftp") {
295295
if ($this->_ftp('ftp_delete', array($this->config['connection'], $file))) {
296296
return true;
297297
}
298-
} elseif ($this->config['type'] == "ssh") {
298+
} elseif ($this->config['type'] === "ssh") {
299299
if ($this->config['connection']->delete($file)) {
300300
return true;
301301
}
@@ -313,13 +313,13 @@ public function delete(Model $model, $file = null) {
313313
* @throws Exception
314314
*/
315315
public function query($query = null, $data = null) {
316-
if (strtolower($query) == 'connect') {
316+
if (strtolower($query) === 'connect') {
317317
return $this->connect(current($data));
318318
}
319-
if (strtolower($query) == 'connection') {
319+
if (strtolower($query) === 'connection') {
320320
return $this->config;
321321
}
322-
if (strtolower($query) == 'console') {
322+
if (strtolower($query) === 'console') {
323323
return $this->console(current($data));
324324
}
325325
throw new Exception(__d('cakeftp', 'That method is not supported.'));
@@ -392,14 +392,17 @@ public function connect($config = array()) {
392392
return true;
393393

394394
case 'ssh':
395-
if (strpos(get_include_path(), 'phpseclib') === false) {
396-
set_include_path(App::pluginPath('Ftp') . DS . 'Vendor' . DS . 'phpseclib' . DS . 'phpseclib' . DS . PATH_SEPARATOR . get_include_path());
397-
if (!App::import('Vendor', 'Ftp.Net_SFTP', array('file' => 'phpseclib' . DS . 'phpseclib' . DS . 'Net' . DS . 'SFTP.php'))) {
398-
throw new Exception(__d('cakeftp', 'Please upload the contents of the phpseclib (https://github.com/phpseclib/phpseclib) to the app/Plugin/Ftp/Vendor/phpseclib/ folder'));
395+
// Deprecated way of not using composer, but a ROOT/vendors/phpseclib/ one (downloaded version).
396+
if (!class_exists('Net_SFTP') && strpos(get_include_path(), 'phpseclib') === false) {
397+
if (!App::import('Vendor', 'Net_SFTP', array('file' => 'phpseclib' . DS . 'phpseclib' . DS . 'Net' . DS . 'SFTP.php'))) {
398+
throw new Exception(__d('cakeftp', 'Please upload the contents of the phpseclib (https://github.com/phpseclib/phpseclib) to the ROOT/vendors/phpseclib/ folder'));
399399
}
400400
}
401+
if (!class_exists('Net_SFTP')) {
402+
throw new Exception(__d('cakeftp', 'Please make sure phpseclib (https://github.com/phpseclib/phpseclib) is a loaded composer dependency.'));
403+
}
401404
$port = !empty($this->config['port']) ? $this->config['port'] : 22;
402-
$this->config['connection'] = new Net_SFTP($this->config['host'], $port);
405+
$this->config['connection'] = $this->_getFtp($this->config['host'], $port);
403406
if (!$this->config['connection']->login($this->config['username'], $this->config['password'])) {
404407
unset($this->config['connection']);
405408
throw new Exception(__d('cakeftp', 'Login failed'));
@@ -410,6 +413,19 @@ public function connect($config = array()) {
410413
return false;
411414
}
412415

416+
/**
417+
* FtpSource::_getFtp()
418+
*
419+
* Allows mocking of the ssh ftp class.
420+
*
421+
* @param string $host
422+
* @param int $port
423+
* @return Net_SFTP Instance
424+
*/
425+
protected function _getFtp($host, $port = 22) {
426+
return new Net_SFTP();
427+
}
428+
413429
/**
414430
* console
415431
* @param string $cmd
@@ -439,7 +455,7 @@ public function console($cmd = null) {
439455
*/
440456
public function quit() {
441457
if (isset($this->config['connection']) && $this->config['connection']) {
442-
if ($this->config['type'] == "ftp") {
458+
if ($this->config['type'] === "ftp") {
443459
$this->_ftp('ftp_close', array($this->config['connection']));
444460
}
445461
$this->config['connection'] = null;
@@ -499,7 +515,7 @@ protected function _parsels($ls = null, $path = '') {
499515
if (empty($line)) {
500516
continue;
501517
}
502-
if (substr($line, -1) == ':') {
518+
if (substr($line, -1) === ':') {
503519
$thisPath = substr($line, strlen($path), -1);
504520
continue;
505521
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![License](https://poser.pugx.org/fotografde/cakephp-ftp/license.svg)](https://packagist.org/packages/fotografde/cakephp-ftp)
44
[![Total Downloads](https://poser.pugx.org/fotografde/cakephp-ftp/d/total.svg)](https://packagist.org/packages/fotografde/cakephp-ftp)
55

6-
Requires: CakePHP 2.0+ (tested with CakePHP 2.5+), PHP 5.2+ (tested with PHP5.3+)
6+
Requires: CakePHP 2.0+ (tested with CakePHP 2.5+), PHP 5.3+, phpseclib for ssh
77

88
## Features
99

Test/Case/Model/Datasource/FtpSourceTest.php

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
App::uses('DataSource', 'Model/Datasource');
55
App::uses('FtpSource', 'Ftp.Model/Datasource');
66

7+
/*
8+
if (!class_exists('Net_SFTP') && strpos(get_include_path(), 'phpseclib') === false) {
9+
if (!App::import('Vendor', 'Net_SFTP', array('file' => 'phpseclib' . DS . 'phpseclib' . DS . 'Net' . DS . 'SFTP.php'))) {
10+
throw new Exception(__d('cakeftp', 'Please upload the contents of the phpseclib (https://github.com/phpseclib/phpseclib) to the APP/Vendor/phpseclib/ folder'));
11+
}
12+
}
13+
if (!class_exists('Net_SFTP')) {
14+
throw new Exception(__d('cakeftp', 'Please make sure phpseclib (https://github.com/phpseclib/phpseclib) is a loaded composer dependency.'));
15+
}
16+
*/
17+
718
/**
819
* Ftp Source Test
920
*
@@ -69,10 +80,10 @@ public function tearDown() {
6980
}
7081

7182
/**
72-
* testInit
83+
* testInit and uppercase type
7384
*/
7485
public function testInit() {
75-
$this->FtpSource = new FtpSource(array($this->defaultConfig));
86+
$this->FtpSource = new FtpSource($this->defaultConfig);
7687
$data = array('type' => 'FTP', 'cache' => true);
7788
$this->assertTrue($this->FtpSource->init($data));
7889
$this->assertEqual($this->FtpSource->config['type'], 'ftp');
@@ -91,7 +102,7 @@ public function testConnect() {
91102
// FTP FAILED CONNECT
92103
$this->FtpSource = $this->getMock('FtpSource', array('_ftp'), array($this->defaultConfig));
93104
$callback = create_function('$method,$params', <<<END
94-
if (\$method == "ftp_connect") {
105+
if (\$method === "ftp_connect") {
95106
return false;
96107
}
97108
return true;
@@ -109,7 +120,7 @@ public function testConnect() {
109120
// FTP FAILED LOGIN
110121
$this->FtpSource = $this->getMock('FtpSource', array('_ftp'), array($this->defaultConfig));
111122
$callback = create_function('$method,$params', <<<END
112-
if (\$method == "ftp_login") {
123+
if (\$method === "ftp_login") {
113124
return false;
114125
}
115126
return true;
@@ -135,17 +146,49 @@ public function testConnect() {
135146
// TODO: ADD SFTP
136147
}
137148

149+
/**
150+
* testConnectSsh
151+
*/
152+
public function testConnectSsh() {
153+
$this->skipIf(true, 'FIXME: Non-composered vendor of phpseclib not found in travis, locally it\'s fine.');
154+
155+
$config = array('type' => 'ssh') + $this->defaultConfig;
156+
$this->FtpSource = $this->getMock('FtpSource', array('_getFtp'), array($config));
157+
$data = array('cache' => true);
158+
$this->assertTrue($this->FtpSource->init($data));
159+
$this->assertEqual($this->FtpSource->config['type'], 'ssh');
160+
$this->assertEqual($this->FtpSource->config['cache'], 'cakeftp');
161+
162+
$this->assertTrue($this->FtpSource->init(array(
163+
'host' => 'https://localhost',
164+
)));
165+
$this->assertEquals('localhost', $this->FtpSource->config['host']);
166+
167+
$Ftp = $this->getMock('Net_SFTP', array(), array('https://localhost', 21));
168+
$this->FtpSource->expects($this->once())
169+
->method('_getFtp')
170+
->with('localhost', 21)
171+
->will($this->returnValue($Ftp));
172+
$Ftp->expects($this->once())
173+
->method('login')
174+
->with('testuser', 1234)
175+
->will($this->returnValue(true));
176+
177+
$result = $this->FtpSource->connect();
178+
$this->assertTrue($result);
179+
}
180+
138181
/**
139182
* testRead
140183
*/
141184
public function testRead() {
142185
$Model = new FtpSourceTestModel();
143186
$this->FtpSource = $this->getMock('FtpSource', array('_ftp'), array($this->defaultConfig));
144187
$callback = create_function('$method,$params', <<<END
145-
if (\$method == 'ftp_pwd') {
188+
if (\$method === 'ftp_pwd') {
146189
return '/path/to/remote/folder/';
147190
}
148-
if (\$method == 'ftp_rawlist') {
191+
if (\$method === 'ftp_rawlist') {
149192
return array(
150193
"drwxr-x--- 3 kyle group 4096 Jul 12 12:16 public_ftp",
151194
"drwxr-x--- 15 kyle group 4096 Nov 3 21:31 public_html",
@@ -192,10 +235,10 @@ public function testCreate() {
192235
$Model = new FtpSourceTestModel();
193236
$this->FtpSource = $this->getMock('FtpSource', array('_ftp'), array($this->defaultConfig));
194237
$callback = create_function('$method,$params', <<<END
195-
if (\$method == 'ftp_put') {
238+
if (\$method === 'ftp_put') {
196239
return false;
197240
}
198-
if (\$method == 'ftp_get') {
241+
if (\$method === 'ftp_get') {
199242
return false;
200243
}
201244
return true;
@@ -235,10 +278,10 @@ public function testDelete() {
235278
$Model = new FtpSourceTestModel();
236279
$this->FtpSource = $this->getMock('FtpSource', array('_ftp'), array($this->defaultConfig));
237280
$callback = create_function('$method,$params', <<<END
238-
if (\$method == 'ftp_delete') {
281+
if (\$method === 'ftp_delete') {
239282
return true;
240283
}
241-
if (\$method == 'ftp_connect' || \$method == 'ftp_login') {
284+
if (\$method === 'ftp_connect' || \$method === 'ftp_login') {
242285
return true;
243286
}
244287
return false;

0 commit comments

Comments
 (0)