Completed
Push — master ( 0cff70...dba08f )
by Morris
09:32
created

FTP::unlink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 1
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
/**
3
 * @author Bart Visscher <[email protected]>
4
 * @author Felix Moeller <[email protected]>
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Michael Gapczynski <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Philipp Kapfer <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Robin McCorkell <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @copyright Copyright (c) 2016, ownCloud, Inc.
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OCA\Files_External\Lib\Storage;
32
33
use Icewind\Streams\RetryWrapper;
34
35
class FTP extends StreamWrapper{
36
	private $password;
37
	private $user;
38
	private $host;
39
	private $secure;
40
	private $root;
41
42
	private static $tempFiles=array();
43
44
	public function __construct($params) {
45
		if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
46
			$this->host=$params['host'];
47
			$this->user=$params['user'];
48
			$this->password=$params['password'];
49
			if (isset($params['secure'])) {
50
				$this->secure = $params['secure'];
51
			} else {
52
				$this->secure = false;
53
			}
54
			$this->root=isset($params['root'])?$params['root']:'/';
55 View Code Duplication
			if ( ! $this->root || $this->root[0]!='/') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
				$this->root='/'.$this->root;
57
			}
58
			if (substr($this->root, -1) !== '/') {
59
				$this->root .= '/';
60
			}
61
		} else {
62
			throw new \Exception('Creating FTP storage failed');
63
		}
64
		
65
	}
66
67
	public function getId(){
68
		return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root;
69
	}
70
71
	/**
72
	 * construct the ftp url
73
	 * @param string $path
74
	 * @return string
75
	 */
76
	public function constructUrl($path) {
77
		$url='ftp';
78
		if ($this->secure) {
79
			$url.='s';
80
		}
81
		$url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path;
82
		return $url;
83
	}
84
85
	/**
86
	 * Unlinks file or directory
87
	 * @param string $path
88
	 */
89
	public function unlink($path) {
90
		if ($this->is_dir($path)) {
91
			return $this->rmdir($path);
92
		}
93
		else {
94
			$url = $this->constructUrl($path);
95
			$result = unlink($url);
96
			clearstatcache(true, $url);
97
			return $result;
98
		}
99
	}
100
	public function fopen($path,$mode) {
101
		switch($mode) {
102
			case 'r':
103
			case 'rb':
104
			case 'w':
105
			case 'wb':
106
			case 'a':
107 View Code Duplication
			case 'ab':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
				//these are supported by the wrapper
109
				$context = stream_context_create(array('ftp' => array('overwrite' => true)));
110
				$handle = fopen($this->constructUrl($path), $mode, false, $context);
111
				return RetryWrapper::wrap($handle);
112
			case 'r+':
113
			case 'w+':
114
			case 'wb+':
115
			case 'a+':
116
			case 'x':
117
			case 'x+':
118
			case 'c':
119 View Code Duplication
			case 'c+':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
				//emulate these
121
				if (strrpos($path, '.')!==false) {
122
					$ext=substr($path, strrpos($path, '.'));
123
				} else {
124
					$ext='';
125
				}
126
				$tmpFile=\OCP\Files::tmpFile($ext);
127
				\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
128
				if ($this->file_exists($path)) {
129
					$this->getFile($path, $tmpFile);
130
				}
131
				self::$tempFiles[$tmpFile]=$path;
132
				return fopen('close://'.$tmpFile, $mode);
133
		}
134
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method OCA\Files_External\Lib\S...ge\StreamWrapper::fopen of type resource.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
135
	}
136
137
	public function writeBack($tmpFile) {
138 View Code Duplication
		if (isset(self::$tempFiles[$tmpFile])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
			$this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
140
			unlink($tmpFile);
141
		}
142
	}
143
144
	/**
145
	 * check if php-ftp is installed
146
	 */
147
	public static function checkDependencies() {
148
		if (function_exists('ftp_login')) {
149
			return(true);
150
		} else {
151
			return array('ftp');
152
		}
153
	}
154
155
}
156