1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Mage Scan |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category MageScan |
8
|
|
|
* @package MageScan |
9
|
|
|
* @author Steve Robbins <[email protected]> |
10
|
|
|
* @copyright 2015 Steve Robbins |
11
|
|
|
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0 |
12
|
|
|
* @link https://github.com/steverobbins/magescan |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace MageScan; |
16
|
|
|
|
17
|
|
|
use GuzzleHttp\Client; |
18
|
|
|
use GuzzleHttp\Promise; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Make a cURL request to a url |
22
|
|
|
* |
23
|
|
|
* @category MageScan |
24
|
|
|
* @package MageScan |
25
|
|
|
* @author Steve Robbins <[email protected]> |
26
|
|
|
* @copyright 2015 Steve Robbins |
27
|
|
|
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0 |
28
|
|
|
* @link https://github.com/steverobbins/magescan |
29
|
|
|
*/ |
30
|
|
|
class Request |
31
|
|
|
{ |
32
|
|
|
const REQUEST_TIMEOUT = 15.0; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* If true, SSL does not have to be verified |
36
|
|
|
* |
37
|
|
|
* @var boolean |
38
|
|
|
*/ |
39
|
|
|
protected $insecure = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The base URL of the Magento application |
43
|
|
|
* |
44
|
|
|
* @var string|boolean |
45
|
|
|
*/ |
46
|
|
|
protected $url; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Client cache |
50
|
|
|
* |
51
|
|
|
* @var Client |
52
|
|
|
*/ |
53
|
|
|
protected $client; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initialize request object |
57
|
|
|
* |
58
|
|
|
* @param string $baseUri |
59
|
|
|
* @param boolean $verify |
60
|
|
|
*/ |
61
|
|
|
public function __construct($baseUri = false, $verify = true) |
62
|
|
|
{ |
63
|
|
|
$this->url = $baseUri; |
64
|
|
|
$params = [ |
65
|
|
|
'verify' => $verify, |
66
|
|
|
'http_errors' => false |
67
|
|
|
]; |
68
|
|
|
if ($this->url !== false) { |
69
|
|
|
$params['base_uri'] = $this->url; |
70
|
|
|
} |
71
|
|
|
$this->client = new Client($params); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Pass undefined requests to client |
76
|
|
|
* |
77
|
|
|
* @param string $method |
78
|
|
|
* @param arary $args |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function __call($method, $args) |
83
|
|
|
{ |
84
|
|
|
$paths = isset($args[0]) ? $args[0] : false; |
85
|
|
|
$params = isset($args[1]) ? $args[1] : []; |
86
|
|
|
if (substr($method, -4) === 'Many') { |
87
|
|
|
$promises = []; |
88
|
|
|
foreach ($paths as $path) { |
89
|
|
|
$promises[$path] = $this->client->requestAsync(substr($method, 0, -4), '/' . $path, $params); |
90
|
|
|
} |
91
|
|
|
return Promise\unwrap($promises); |
92
|
|
|
} |
93
|
|
|
return $this->client->request($method, $paths ? '/' . $paths : null, $params); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the base url of this request |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getUrl() |
102
|
|
|
{ |
103
|
|
|
return $this->url; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Manipulate header data into a parsable format |
108
|
|
|
* |
109
|
|
|
* @param string $rawData |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function parseHeader($rawData) |
114
|
|
|
{ |
115
|
|
|
$data = []; |
116
|
|
|
foreach (explode("\n", trim($rawData)) as $line) { |
117
|
|
|
$bits = explode(': ', $line); |
118
|
|
|
if (count($bits) > 1) { |
119
|
|
|
$key = $bits[0]; |
120
|
|
|
unset($bits[0]); |
121
|
|
|
$data[$key] = trim(implode(': ', $bits)); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
return $data; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Parse out the count from the response |
129
|
|
|
* |
130
|
|
|
* @param string $response |
131
|
|
|
* @param string $pattern |
132
|
|
|
* @param boolean $returnAll |
133
|
|
|
* |
134
|
|
|
* @return string|array|boolean |
135
|
|
|
*/ |
136
|
|
|
public function findMatchInResponse($response, $pattern, $returnAll = false) |
137
|
|
|
{ |
138
|
|
|
if (empty($response)) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
if (preg_match($pattern, $response, $match) |
142
|
|
|
&& (isset($match[1]) || $returnAll) |
143
|
|
|
) { |
144
|
|
|
return $returnAll ? $match : $match[1]; |
145
|
|
|
} |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|