Completed
Pull Request — master (#2)
by Stephen
13:19
created

ftp_pure::_data_write_block()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * PemFTP - A Ftp implementation in pure PHP
4
 *
5
 * @package PemFTP
6
 * @since 2.5.0
7
 *
8
 * @version 1.0
9
 * @copyright Alexey Dotsenko
10
 * @author Alexey Dotsenko
11
 * @link http://www.phpclasses.org/browse/package/1743.html Site
12
 * @license LGPL http://www.opensource.org/licenses/lgpl-license.html
13
 */
14
15
/**
16
 * FTP implementation using fsockopen to connect.
17
 *
18
 * @package PemFTP
19
 * @subpackage Pure
20
 * @since 2.5.0
21
 *
22
 * @version 1.0
23
 * @copyright Alexey Dotsenko
24
 * @author Alexey Dotsenko
25
 * @link http://www.phpclasses.org/browse/package/1743.html Site
26
 * @license LGPL http://www.opensource.org/licenses/lgpl-license.html
27
 */
28
class ftp_pure extends ftp_base {
29
30
	function __construct($verb=FALSE, $le=FALSE) {
31
		parent::__construct(false, $verb, $le);
32
	}
33
34
// <!-- --------------------------------------------------------------------------------------- -->
35
// <!--       Private functions                                                                 -->
36
// <!-- --------------------------------------------------------------------------------------- -->
37
38
	function _settimeout($sock) {
39
		if(!@stream_set_timeout($sock, $this->_timeout)) {
40
			$this->PushError('_settimeout','socket set send timeout');
41
			$this->_quit();
42
			return FALSE;
43
		}
44
		return TRUE;
45
	}
46
47
	function _connect($host, $port) {
48
		$this->SendMSG("Creating socket");
49
		$sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
50
		if (!$sock) {
51
			$this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
0 ignored issues
show
Documentation introduced by
$errstr . ' (' . $errno . ')' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
			return FALSE;
53
		}
54
		$this->_connected=true;
55
		return $sock;
56
	}
57
58
	function _readmsg($fnction="_readmsg"){
59
		if(!$this->_connected) {
60
			$this->PushError($fnction, 'Connect first');
61
			return FALSE;
62
		}
63
		$result=true;
64
		$this->_message="";
65
		$this->_code=0;
66
		$go=true;
67
		do {
68
			$tmp=@fgets($this->_ftp_control_sock, 512);
69
			if($tmp===false) {
70
				$go=$result=false;
71
				$this->PushError($fnction,'Read failed');
72
			} else {
73
				$this->_message.=$tmp;
74
				if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
75
			}
76
		} while($go);
77
		if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
78
		$this->_code=(int)$regs[1];
0 ignored issues
show
Bug introduced by
The variable $regs does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
79
		return $result;
80
	}
81
82
	function _exec($cmd, $fnction="_exec") {
83
		if(!$this->_ready) {
84
			$this->PushError($fnction,'Connect first');
85
			return FALSE;
86
		}
87
		if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
88
		$status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
89
		if($status===false) {
90
			$this->PushError($fnction,'socket write failed');
91
			return FALSE;
92
		}
93
		$this->_lastaction=time();
94
		if(!$this->_readmsg($fnction)) return FALSE;
95
		return TRUE;
96
	}
97
98
	function _data_prepare($mode=FTP_ASCII) {
99
		if(!$this->_settype($mode)) return FALSE;
100
		if($this->_passive) {
101
			if(!$this->_exec("PASV", "pasv")) {
102
				$this->_data_close();
103
				return FALSE;
104
			}
105
			if(!$this->_checkCode()) {
106
				$this->_data_close();
107
				return FALSE;
108
			}
109
			$ip_port = explode(",", preg_replace("/^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*$/s", "\\1", $this->_message));
110
			$this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
111
            $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
112
			$this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
113
			$this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout);
114
			if(!$this->_ftp_data_sock) {
115
				$this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")");
0 ignored issues
show
Documentation introduced by
$errstr . ' (' . $errno . ')' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
				$this->_data_close();
117
				return FALSE;
118
			}
119
			else $this->_ftp_data_sock;
120
		} else {
121
			$this->SendMSG("Only passive connections available!");
122
			return FALSE;
123
		}
124
		return TRUE;
125
	}
126
127
	function _data_read($mode=FTP_ASCII, $fp=NULL) {
128
		if(is_resource($fp)) $out=0;
129
		else $out="";
130
		if(!$this->_passive) {
131
			$this->SendMSG("Only passive connections available!");
132
			return FALSE;
133
		}
134
		while (!feof($this->_ftp_data_sock)) {
135
			$block=fread($this->_ftp_data_sock, $this->_ftp_buff_size);
136
			if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
137
			if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
138
			else $out.=$block;
139
		}
140
		return $out;
141
	}
142
143
	function _data_write($mode=FTP_ASCII, $fp=NULL) {
144
		if(is_resource($fp)) $out=0;
0 ignored issues
show
Unused Code introduced by
$out is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
		else $out="";
0 ignored issues
show
Unused Code introduced by
$out is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
146
		if(!$this->_passive) {
147
			$this->SendMSG("Only passive connections available!");
148
			return FALSE;
149
		}
150
		if(is_resource($fp)) {
151
			while(!feof($fp)) {
152
				$block=fread($fp, $this->_ftp_buff_size);
153
				if(!$this->_data_write_block($mode, $block)) return false;
154
			}
155
		} elseif(!$this->_data_write_block($mode, $fp)) return false;
156
		return TRUE;
157
	}
158
159
	function _data_write_block($mode, $block) {
160
		if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
161
		do {
162
			if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
163
				$this->PushError("_data_write","Can't write to socket");
164
				return FALSE;
165
			}
166
			$block=substr($block, $t);
167
		} while(!empty($block));
168
		return true;
169
	}
170
171
	function _data_close() {
172
		@fclose($this->_ftp_data_sock);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
173
		$this->SendMSG("Disconnected data from remote host");
174
		return TRUE;
175
	}
176
177
	function _quit($force=FALSE) {
178
		if($this->_connected or $force) {
179
			@fclose($this->_ftp_control_sock);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
180
			$this->_connected=false;
181
			$this->SendMSG("Socket closed");
182
		}
183
	}
184
}
185
186
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
187