Issues (1092)

Helper/Connection/Fsockopen.php (5 issues)

1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Helper\Connection;
28
29
/**
30
 * Helper class for connections with fsockopen
31
 */
32
class Fsockopen
33
{
34
    /**
35
     * Determine if this connection type can be used on the given server
36
     *
37
     * @return bool
38
     */
39
    public function isApplicable()
40
    {
41
        if (function_exists("fsockopen")) {
42
            return true;
43
        }
44
        return false;
45
    }
46
47
    /**
48
     * Get request header for fsockopen request
49
     *
50
     * @param  array $aParsedRequestUrl
51
     * @return string
52
     */
53
    protected function getSocketRequestHeader($aParsedRequestUrl)
54
    {
55
        $sRequestHeader  = "POST ".$aParsedRequestUrl['path']." HTTP/1.1\r\n";
56
        $sRequestHeader .= "Host: ".$aParsedRequestUrl['host']."\r\n";
57
        $sRequestHeader .= "Content-Type: application/x-www-form-urlencoded\r\n";
58
        $sRequestHeader .= "Content-Length: ".strlen($aParsedRequestUrl['query'])."\r\n";
59
        $sRequestHeader .= "Connection: close\r\n\r\n";
60
        $sRequestHeader .= $aParsedRequestUrl['query'];
61
        return $sRequestHeader;
62
    }
63
64
    /**
65
     * Read the response from fsockopen request
66
     *
67
     * @param  object $oFsockOpen
68
     * @return array
69
     */
70
    protected function getSocketResponse($oFsockOpen)
71
    {
72
        $aResponse = [];
73
74
        $sResponseHeader = "";
75
        do {
76
            $sResponseHeader .= fread($oFsockOpen, 1);
0 ignored issues
show
$oFsockOpen of type object is incompatible with the type resource expected by parameter $stream of fread(). ( Ignorable by Annotation )

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

76
            $sResponseHeader .= fread(/** @scrutinizer ignore-type */ $oFsockOpen, 1);
Loading history...
77
        } while (!preg_match("/\\r\\n\\r\\n$/", $sResponseHeader) && !feof($oFsockOpen));
0 ignored issues
show
$oFsockOpen of type object is incompatible with the type resource expected by parameter $stream of feof(). ( Ignorable by Annotation )

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

77
        } while (!preg_match("/\\r\\n\\r\\n$/", $sResponseHeader) && !feof(/** @scrutinizer ignore-type */ $oFsockOpen));
Loading history...
78
79
        while (!feof($oFsockOpen)) {
80
            $aResponse[] = fgets($oFsockOpen, 1024);
0 ignored issues
show
$oFsockOpen of type object is incompatible with the type resource expected by parameter $stream of fgets(). ( Ignorable by Annotation )

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

80
            $aResponse[] = fgets(/** @scrutinizer ignore-type */ $oFsockOpen, 1024);
Loading history...
81
        }
82
        if (count($aResponse) == 0) {
83
            $aResponse[] = 'connection-type: 3 - '.$sResponseHeader;
84
        }
85
        return $aResponse;
86
    }
87
88
    /**
89
     * Send fsockopen request
90
     *
91
     * @param  array $aParsedRequestUrl
92
     * @return array
93
     */
94
    public function sendSocketRequest($aParsedRequestUrl)
95
    {
96
        if (!$this->isApplicable()) {
97
            return ["errormessage" => "Cli-Curl is not applicable on this server."];
98
        }
99
100
        $iErrorNumber = '';
101
        $sErrorString = '';
102
103
        $sScheme = '';
104
        $iPort = 80;
105
        if ($aParsedRequestUrl['scheme'] == 'https') {
106
            $sScheme = 'ssl://';
107
            $iPort = 443;
108
        }
109
110
        $oFsockOpen = fsockopen($sScheme.$aParsedRequestUrl['host'], $iPort, $iErrorNumber, $sErrorString, 45);
0 ignored issues
show
$iErrorNumber of type string is incompatible with the type integer expected by parameter $error_code of fsockopen(). ( Ignorable by Annotation )

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

110
        $oFsockOpen = fsockopen($sScheme.$aParsedRequestUrl['host'], $iPort, /** @scrutinizer ignore-type */ $iErrorNumber, $sErrorString, 45);
Loading history...
111
        if ($oFsockOpen) {
0 ignored issues
show
$oFsockOpen is of type resource, thus it always evaluated to false.
Loading history...
112
            fwrite($oFsockOpen, $this->getSocketRequestHeader($aParsedRequestUrl));
113
            return $this->getSocketResponse($oFsockOpen);
114
        }
115
        return ["errormessage=fsockopen:Failed opening http socket connection: ".$sErrorString." (".$iErrorNumber.")"];
116
    }
117
}
118