CurlSoap::zDebug()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 29
cp 0
rs 9.408
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 12
1
<?php
2
3
namespace NFePHP\Esfinge\Soap;
4
5
use RuntimeException;
6
use NFePHP\Esfinge\Files\FilesFolders;
7
8
class CurlSoap
9
{
10
    /**
11
     * soapDebug
12
     * @var string
13
     */
14
    public $soapDebug = '';
15
    /**
16
     * soapTimeout
17
     * @var integer
18
     */
19
    public $soapTimeout = 10;
20
    /**
21
     * lastMsg
22
     * @var string
23
     */
24
    public $lastMsg = '';
25
26
    /**
27
     * errorCurl
28
     * @var string
29
     */
30
    public $errorCurl = '';
31
    /**
32
     * infoCurl
33
     * @var array
34
     */
35
    public $infoCurl = array();
36
    /**
37
     * proxyIP
38
     * @var string
39
     */
40
    private $proxyIP = '';
41
    /**
42
     * proxyPORT
43
     * @var string
44
     */
45
    private $proxyPORT = '';
46
    /**
47
     * proxyUSER
48
     * @var string
49
     */
50
    private $proxyUSER = '';
51
    /**
52
     * proxyPASS
53
     * @var string
54
     */
55
    private $proxyPASS = '';
56
    private $pathlog;
57
    private $debug = false;
58
    private $msgHeader = '';
59
    private $msgBody = '';
60
    
61
    public function __construct($pathlog, $timeout, $aproxy, $debug = false)
62
    {
63
        $this->pathlog = $pathlog;
64
        $this->soapTimeout = $timeout;
65
        $ipNumber = $aproxy['proxyIp'];
66
        $port = $aproxy['proxyPort'];
67
        $user = $aproxy['proxyUser'];
68
        $pass = $aproxy['proxyPass'];
69
        $this->setProxy($ipNumber, $port, $user, $pass);
70
        $this->debug = $debug;
71
    }
72
    
73
    /**
74
     * setProxy
75
     * Seta o uso do proxy
76
     * @param string $ipNumber numero IP do proxy server
77
     * @param string $port numero da porta usada pelo proxy
78
     * @param string $user nome do usuário do proxy
79
     * @param string $pass senha de acesso ao proxy
80
     * @return boolean
81
     */
82
    public function setProxy($ipNumber, $port, $user = '', $pass = '')
83
    {
84
        $this->proxyIP = $ipNumber;
85
        $this->proxyPORT = $port;
86
        $this->proxyUSER = $user;
87
        $this->proxyPASS = $pass;
88
    }
89
    
90
    /**
91
     * getProxy
92
     * Retorna os dados de configuração do Proxy em um array
93
     * @return array
94
     */
95
    public function getProxy()
96
    {
97
        $aProxy['ip'] = $this->proxyIP;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aProxy was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aProxy = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
98
        $aProxy['port'] = $this->proxyPORT;
99
        $aProxy['username'] = $this->proxyUSER;
100
        $aProxy['password'] = $this->proxyPASS;
101
        return $aProxy;
102
    }
103
    
104
    /**
105
     * Envia mensagem ao webservice
106
     * @param string $urlsevice
0 ignored issues
show
Documentation introduced by
There is no parameter named $urlsevice. Did you maybe mean $urlservice?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
107
     * @param string $namespace
108
     * @param string $header
109
     * @param string $body
110
     * @param string $method
111
     * @return boolean|string
112
     */
113
    public function send($urlservice, $namespace, $header, $body, $method)
114
    {
115
        //monta a mensagem ao webservice
116
        $data = '<?xml version="1.0" encoding="utf-8"?>'.'<soap:Envelope ';
117
        $data .= 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ';
118
        $data .= 'xmlns:svc="'.$namespace.'">';
119
        $data .= '<soap:Header>'.$header.'</soap:Header>';
120
        $data .= '<soap:Body>'.$body.'</soap:Body>';
121
        $data .= '</soap:Envelope>';
122
        $data = $this->clearMsg($data);
123
        //grava em propriedade da classe o envelope não compactado
124
        $this->lastMsg = $data;
125
        //salva o envelope em arquivo para efeito de LOG
126
        $mark = date('Ym').DIRECTORY_SEPARATOR.date('d_His_').$method;
127
        $filepath = $mark.'.xml';
128
        FilesFolders::save($this->pathlog, $filepath, $data);
129
        //compacta mensagem com GZip
130
        $data = gzencode($data);
131
        //tamanho da mensagem
132
        $tamanho = strlen($data);
133
        //estabelecimento dos parametros da mensagem
134
        $parametros = [
135
            "Accept-Encoding: gzip, deflate",
136
            "Content-Type: text/xml;charset=UTF-8",
137
            "Content-encoding: gzip",
138
            "Content-length: $tamanho"
139
        ];
140
        //solicita comunicação via cURL
141
        $resposta = $this->zCommCurl($urlservice, $data, $parametros, $mark);
142
        if (empty($resposta)) {
143
            $msg = "Não houve retorno do Curl.\n $this->errorCurl";
144
            throw new RuntimeException($msg);
145
        }
146
        //verifica o retorno
147
        if ($this->infoCurl["http_code"] != '200') {
148
            //se não é igual a 200 houve erro
149
            $msg = $this->msgHeader ."\r\n\r\n". $this->msgBody;
150
            $filepath = $mark.'_ERROR.log';
151
            FilesFolders::save($this->pathlog, $filepath, $msg);
152
            throw new RuntimeException($msg);
153
        }
154
        //localiza a primeira marca de tag
155
        $xPos = stripos($this->msgBody, "<");
156
        $lenresp = strlen($this->msgBody);
157
        //se não existir não é um xml nem um html
158
        if ($xPos !== false) {
159
            $xml = substr($this->msgBody, $xPos, $lenresp-$xPos);
160
        } else {
161
            $xml = '';
162
        }
163
        //testa para saber se é um xml mesmo ou é um html
164
        $result = simplexml_load_string($xml, 'SimpleXmlElement', LIBXML_NOERROR+LIBXML_ERR_FATAL+LIBXML_ERR_NONE);
165
        if ($result === false) {
166
            //não é um xml então pode limpar
167
            $xml = '';
168
        }
169
        if ($xml == '') {
170
            $msg = "Não houve retorno de um xml verifique soapDebug!!";
171
            throw new RuntimeException($msg);
172
        }
173
        if ($xml != '' && substr($xml, 0, 5) != '<?xml') {
174
            $xml = '<?xml version="1.0" encoding="utf-8"?>'.$xml;
175
        }
176
        //grava a resposta do TCE/SC
177
        $filepath = $mark.'_response.xml';
178
        FilesFolders::save($this->pathlog, $filepath, $xml);
179
        return $xml;
180
    }
181
    
182
    /**
183
     * Envio via cURL
184
     * @param string $url
185
     * @param string $data
186
     * @param array $parametros
187
     * @return string
188
     */
189
    protected function zCommCurl($url, $data = '', $parametros = array(), $mark = 'dd')
190
    {
191
        $this->msgHeader = '';
192
        $this->msgBody = '';
193
        //incializa cURL
194
        $oCurl = curl_init();
195
        //setting da seção soap
196
        if ($this->proxyIP != '') {
197
            curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1);
198
            curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
199
            curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP.':'.$this->proxyPORT);
200
            if ($this->proxyPASS != '') {
201
                curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUSER.':'.$this->proxyPASS);
202
                curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
203
            }
204
        }
205
        //força a resolução de nomes com IPV4 e não com IPV6, isso
206
        //pode acelerar temporáriamente as falhas ou demoras decorrentes de
207
        //ambiente mal preparados como os da SEFAZ GO, porém pode causar
208
        //problemas no futuro quando os endereços IPV4 deixarem de ser usados
209
        curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
210
        curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soapTimeout);
211
        curl_setopt($oCurl, CURLOPT_URL, $url);
212
        curl_setopt($oCurl, CURLOPT_VERBOSE, 1);
213
        curl_setopt($oCurl, CURLOPT_HEADER, 1);
214
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
215
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
216
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
217
        curl_setopt($oCurl, CURLOPT_POST, 1);
218
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $data);
219
        if (!empty($parametros)) {
220
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parametros);
221
        }
222
        //inicia a conexão
223
        $resposta = curl_exec($oCurl);
224
        if ($this->debug) {
225
            $filepath = $mark.'_DEBUG.log';
226
            FilesFolders::save($this->pathlog, $filepath, $resposta);
227
        }
228
        //obtem as informações da conexão
229
        $info = curl_getinfo($oCurl);
230
        //split http header e body
231
        $header_size = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
232
        $this->msgHeader = substr($resposta, 0, $header_size);
233
        $this->msgBody = $this->ungzip(substr($resposta, $header_size));
234
        //carrega os dados para debug
235
        $this->zDebug($info, $data, "$this->msgHeader\n\n$this->msgBody");
236
        $this->errorCurl = curl_error($oCurl);
237
        //fecha a conexão
238
        curl_close($oCurl);
239
        //retorna resposta
240
        return $resposta;
241
    }
242
    
243
    /**
244
     * Uncompress gzip msg usando gzdecode
245
     * @param string $body
246
     * @return string
247
     */
248
    private function ungzip($body)
249
    {
250
        if (substr($body, 0, 2) == "\x1f\x8b") {
251
            return trim(gzdecode($body));
252
        }
253
        return $body;
254
    }
255
    
256
    /**
257
     * zDebug
258
     * @param array $info
259
     * @param string $data
260
     * @param string $resposta
261
     */
262
    private function zDebug($info = array(), $data = '', $resposta = '')
263
    {
264
        $this->infoCurl["url"] = $info["url"];
265
        $this->infoCurl["content_type"] = $info["content_type"];
266
        $this->infoCurl["http_code"] = $info["http_code"];
267
        $this->infoCurl["header_size"] = $info["header_size"];
268
        $this->infoCurl["request_size"] = $info["request_size"];
269
        $this->infoCurl["filetime"] = $info["filetime"];
270
        $this->infoCurl["ssl_verify_result"] = $info["ssl_verify_result"];
271
        $this->infoCurl["redirect_count"] = $info["redirect_count"];
272
        $this->infoCurl["total_time"] = $info["total_time"];
273
        $this->infoCurl["namelookup_time"] = $info["namelookup_time"];
274
        $this->infoCurl["connect_time"] = $info["connect_time"];
275
        $this->infoCurl["pretransfer_time"] = $info["pretransfer_time"];
276
        $this->infoCurl["size_upload"] = $info["size_upload"];
277
        $this->infoCurl["size_download"] = $info["size_download"];
278
        $this->infoCurl["speed_download"] = $info["speed_download"];
279
        $this->infoCurl["speed_upload"] = $info["speed_upload"];
280
        $this->infoCurl["download_content_length"] = $info["download_content_length"];
281
        $this->infoCurl["upload_content_length"] = $info["upload_content_length"];
282
        $this->infoCurl["starttransfer_time"] = $info["starttransfer_time"];
283
        $this->infoCurl["redirect_time"] = $info["redirect_time"];
284
        //coloca as informações em uma variável
285
        $txtInfo ="";
286
        foreach ($info as $key => $content) {
287
            if (is_string($content)) {
288
                $txtInfo .= strtoupper($key).'='.$content."\n";
289
            }
290
        }
291
        //carrega a variavel debug
292
        $this->soapDebug = $data."\n\n".$txtInfo."\n".$resposta;
293
    }
294
    
295
    /**
296
     * clearMsg
297
     * @param string $msg
298
     * @return string
299
     */
300
    protected function clearMsg($msg)
301
    {
302
        $nmsg = str_replace(array(' standalone="no"','default:',':default',"\n","\r","\t"), '', $msg);
303
        $nnmsg = str_replace('> ', '>', $nmsg);
304
        if (strpos($nnmsg, '> ')) {
305
            $nnmsg = $this->clearMsg((string) $nnmsg);
306
        }
307
        return $nnmsg;
308
    }
309
}
310