Fsockopen   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 86
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isApplicable() 0 7 2
A getSocketRequestHeader() 0 10 1
B getSocketResponse() 0 17 5
B sendSocketRequest() 0 23 4
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);
77
        } while (!preg_match("/\\r\\n\\r\\n$/", $sResponseHeader) && !feof($oFsockOpen));
78
79
        while (!feof($oFsockOpen)) {
80
            $aResponse[] = fgets($oFsockOpen, 1024);
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);
111
        if ($oFsockOpen) {
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