Issues (15)

src/Stream/ActiveDataStream.php (2 issues)

Labels
1
<?php
2
3
/**
4
 * This file is part of the Lazzard/ftp-bridge package.
5
 *
6
 * (c) El Amrani Chakir <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lazzard\FtpBridge\Stream;
13
14
use Lazzard\FtpBridge\Response\Response;
15
use Lazzard\FtpBridge\Error\ErrorTrigger;
16
17
/**
18
 * @since  1.0
19
 * @author El Amrani Chakir <[email protected]>
20
 * 
21
 * @internal
22
 */
23
class ActiveDataStream extends DataStream
24
{
25
    /** @var string */
26
    public $activeIpAddress;
27
28
    /**
29
     * Opens a data stream socket.
30
     *
31
     * @param LoggerInterface $logger
0 ignored issues
show
The type Lazzard\FtpBridge\Stream\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
     * @param StreamInterface $commandStream
33
     * @param string          $activeIpAddress
34
     */
35
    public function __construct($logger, $commandStream, $activeIpAddress = null)
36
    {
37
        parent::__construct($logger, $commandStream);
38
        $this->activeIpAddress = $activeIpAddress;
39
    }
40
41
    /**
42
     * Opens a stream socket connection that listening to the local random port sent with 
43
     * the PORT command.
44
     * 
45
     * {@inheritDoc}
46
     */
47
    public function open()
48
    {
49
        $ip = str_replace(".", ",", $this->activeIpAddress ?: $_SERVER['SERVER_ADDR']);
50
51
        $low  = rand(32, 255);
52
        $high = rand(32, 255);
53
        // $port = ($low * 256) + $high
54
        $port = ($low<<8) + $high;
55
56
        // 1- create a stream socket.
57
        // 2- bind the socket to a local host address.
58
        // 3- listen to the socket on the local port that will
59
        // be send along with PORT comamnd.
60
        // 4- send the PORT command.
61
        if (is_resource($stream = stream_socket_server('tcp://0.0.0.0:'.$port, $errnon, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN))) {
62
            $this->commandStream->write("PORT $ip,$low,$high");
63
            $response = new Response($this->commandStream->read());
0 ignored issues
show
It seems like $this->commandStream->read() can also be of type false; however, parameter $reply of Lazzard\FtpBridge\Response\Response::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            $response = new Response(/** @scrutinizer ignore-type */ $this->commandStream->read());
Loading history...
64
            if ($response->getCode() === 200) {
65
                $this->stream = $stream;
66
                return true;
67
            }
68
69
            return !ErrorTrigger::raise($response->getMessage());
70
        }
71
72
        return !ErrorTrigger::raise("Unable to open the data stream socket connection.");
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function read()
79
    {
80
        $conn = stream_socket_accept($this->stream);
81
        $data = fread($conn, 8192);
82
        fclose($conn);
83
84
        $this->log($data);
85
86
        return $data;
87
    }
88
}