This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of Caspeco. |
||
5 | * |
||
6 | (c) HOY Multimedia AB <[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 Hoy\Caspeco\Http; |
||
13 | |||
14 | use GuzzleHttp\Client; |
||
15 | use GuzzleHttp\Exception\RequestException; |
||
16 | use GuzzleHttp\Psr7\Request; |
||
17 | use Hoy\Caspeco\Exceptions\AuthenticationException; |
||
18 | use Hoy\Caspeco\Exceptions\HtmlException; |
||
19 | use Hoy\Caspeco\Exceptions\HttpException; |
||
20 | use Hoy\Caspeco\Exceptions\ValidationException; |
||
21 | use Psr\Http\Message\RequestInterface; |
||
22 | use Stringy\Stringy; |
||
23 | |||
24 | /** |
||
25 | * This is the abstract client class. |
||
26 | * |
||
27 | * @author Vincent Klaiber <[email protected]> |
||
28 | */ |
||
29 | abstract class AbstractClient |
||
30 | { |
||
31 | /** |
||
32 | * Create a new abstract api instance. |
||
33 | * |
||
34 | * @param array $config |
||
35 | */ |
||
36 | public function __construct(array $config) |
||
37 | { |
||
38 | $this->client = new Client(['base_uri' => $config['url']]); |
||
0 ignored issues
–
show
|
|||
39 | $this->signature = new Signature($config['id'], $config['secret'], $config['url']); |
||
0 ignored issues
–
show
The property
signature does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Make a get request. |
||
44 | * |
||
45 | * @param string $uri |
||
46 | * @param array $options |
||
47 | * |
||
48 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
49 | */ |
||
50 | public function get($uri, array $options = []) |
||
51 | { |
||
52 | return $this->request(__FUNCTION__, $uri, $options); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Make a post request. |
||
57 | * |
||
58 | * @param string $uri |
||
59 | * @param array $options |
||
60 | * |
||
61 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
62 | */ |
||
63 | public function post($uri, array $options = []) |
||
64 | { |
||
65 | return $this->request(__FUNCTION__, $uri, $options); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Make a put request. |
||
70 | * |
||
71 | * @param string $uri |
||
72 | * @param array $options |
||
73 | * |
||
74 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
75 | */ |
||
76 | public function put($uri, array $options = []) |
||
77 | { |
||
78 | return $this->request(__FUNCTION__, $uri, $options); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Make a delete request. |
||
83 | * |
||
84 | * @param string $uri |
||
85 | * @param array $options |
||
86 | * |
||
87 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
88 | */ |
||
89 | public function delete($uri, array $options = []) |
||
90 | { |
||
91 | return $this->request(__FUNCTION__, $uri, $options); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Sign and send request. |
||
96 | * |
||
97 | * @param string $method |
||
98 | * @param string $uri |
||
99 | * @param array $options |
||
100 | * |
||
101 | * @return mixed |
||
102 | */ |
||
103 | protected function request($method, $uri, array $options = []) |
||
104 | { |
||
105 | $uri = $this->buildUriFromString($uri); |
||
106 | |||
107 | $body = isset($options['json']) ? json_encode($options['json']) : null; |
||
108 | |||
109 | $request = new Request($method, $uri, [], $body); |
||
110 | $request = $this->signature->sign($request); |
||
111 | |||
112 | $options['headers'] = $request->getHeaders(); |
||
113 | |||
114 | return $this->send($request, $options); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Send the given request. |
||
119 | * |
||
120 | * @param \Psr\Http\Message\RequestInterface $request |
||
121 | * @param array $options |
||
122 | * |
||
123 | * @throws \GuzzleHttp\Exception\RequestException |
||
124 | * @throws \Hoy\Caspeco\Exceptions\HttpException |
||
125 | * |
||
126 | * @return mixed |
||
127 | */ |
||
128 | protected function send(RequestInterface $request, array $options = []) |
||
129 | { |
||
130 | try { |
||
131 | $response = $this->client->send($request, $options); |
||
132 | |||
133 | $content = $response->getBody()->getContents(); |
||
134 | $body = json_decode($content); |
||
135 | |||
136 | if (property_exists($body, 'Message')) { |
||
137 | throw new ValidationException(400, $this->formatJsonError($content)); |
||
138 | } |
||
139 | |||
140 | return $body; |
||
141 | } catch (RequestException $exception) { |
||
142 | $this->handleException($exception); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Handle Guzzle exception. |
||
148 | * |
||
149 | * @param \GuzzleHttp\Exception\RequestException $exception |
||
150 | * |
||
151 | * @throws \Hoy\Caspeco\Exceptions\HttpException |
||
152 | */ |
||
153 | protected function handleException(RequestException $exception) |
||
154 | { |
||
155 | $code = $exception->getResponse()->getStatusCode(); |
||
156 | $message = $exception->getResponse()->getBody()->getContents(); |
||
157 | |||
158 | if (Stringy::create($message)->isJson()) { |
||
159 | throw new HttpException($exception->getResponse()->getStatusCode(), $this->formatJsonError($message)); |
||
160 | } |
||
161 | |||
162 | if (Stringy::create($message)->contains('html')) { |
||
163 | throw new HtmlException($exception->getResponse()->getStatusCode(), $message); |
||
164 | } |
||
165 | |||
166 | throw new AuthenticationException($code, $message); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Build API uri from string. |
||
171 | * |
||
172 | * @param string $uri |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | protected function buildUriFromString($uri) |
||
177 | { |
||
178 | return $uri = '/v1/'.$uri.'/'; |
||
0 ignored issues
–
show
$uri is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Format JSON error message. |
||
183 | * |
||
184 | * @param string $json |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | protected function formatJsonError($json) |
||
189 | { |
||
190 | $error = json_decode($json); |
||
191 | |||
192 | return sprintf('%s (%s)', $error->Message, $error->Code); |
||
193 | } |
||
194 | } |
||
195 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: