Issues (32)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Http/HttpClient.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Mechpave\ImgurClient\Http;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ServerException;
8
use Mechpave\ImgurClient\Exception\AuthenticationException;
9
use Mechpave\ImgurClient\Exception\InvalidParameterException;
10
use Mechpave\ImgurClient\Exception\NotFoundException;
11
use Mechpave\ImgurClient\Exception\RateLimitException;
12
use Mechpave\ImgurClient\Exception\ServerErrorException;
13
use Mechpave\ImgurClient\ImgurClient;
14
use Mechpave\ImgurClient\Model\EndpointModel;
15
use Mechpave\ImgurClient\Model\StatusCodeModel;
16
17
class HttpClient implements HttpClientInterface
18
{
19
    /**
20
     * Imgur client
21
     *
22
     * @var ImgurClient
23
     */
24
    protected $client;
25
26
    /**
27
     * HTTP client object
28
     *
29
     * @var GuzzleClient
30
     */
31
    protected $httpClient;
32
33
    /**
34
     * Client constructor.
35
     * @param ImgurClient $client
36
     */
37 8
    public function __construct(ImgurClient $client)
38
    {
39 8
        $this->client = $client;
40 8
        $this->initHttpClient();
41 8
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 View Code Duplication
    public function get($uri, array $queryParams = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $options = [];
49
50
        if (!empty($queryParams)) {
51
            $options['query'] = $queryParams;
52
        }
53
54
        $response = $this->makeRequest('GET', $uri, $options);
55
56
        return $response;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 View Code Duplication
    public function post($uri, array $postData = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        $options = [];
65
66
        if (!empty($postData)) {
67
            $options['form_params'] = $postData;
68
        }
69
70
        $response = $this->makeRequest('POST', $uri, $options);
71
72
        return $response;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function delete($uri)
79
    {
80
        $response = $this->makeRequest('DELETE', $uri);
81
82
        return $response;
83
    }
84
85
    /**
86
     * Makes http request and handles Imgur errors
87
     *
88
     * @param string $method
89
     * @param string $uri
90
     * @param array $options
91
     * @return ImgurResponseInterface
92
     * @throws AuthenticationException
93
     * @throws InvalidParameterException
94
     * @throws NotFoundException
95
     * @throws RateLimitException
96
     * @throws ServerErrorException
97
     * @throws \Exception
98
     */
99 6
    protected function makeRequest($method, $uri = null, $options = [])
100
    {
101
        try {
102 6
            $response = $this->httpClient->request($method, $uri, $options);
103 5
        } catch (ClientException $e) {
104
            //4** status codes
105 4
            $statusCode = $e->getResponse()->getStatusCode();
106 4
            $body = json_decode($e->getResponse()->getBody()->getContents(), true);
107
108
            switch ($statusCode) {
109 4
                case StatusCodeModel::PARAMETER_MISSING:
110 1
                    throw new InvalidParameterException($body['data']['error'], $statusCode);
111
                    break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
112 3
                case StatusCodeModel::AUTHENTICATION_REQUIRED:
113 2
                case StatusCodeModel::FORBIDDEN:
114 1
                    throw new AuthenticationException($body['data']['error'], $statusCode);
115
                    break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
116 2
                case StatusCodeModel::RESOURCE_NOT_FOUND:
117 1
                    throw new NotFoundException($body['data']['error'], $statusCode);
118
                    break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
119 1
                case StatusCodeModel::RATE_LIMITING:
120 1
                    throw new RateLimitException($body['data']['error'], $statusCode);
121
                    break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
122
                default:
123
                    throw $e;
124
                    break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
125
            }
126 1
        } catch (ServerException $e) {
127
            //5** status codes
128 1
            $statusCode = $e->getResponse()->getStatusCode();
129 1
            throw new ServerErrorException('Internal Imgur server error', $statusCode);
130
        } catch (\Exception $e) {
131
            //unknown error
132
            throw $e;
133
        }
134
135 1
        $imgurResponse = new ImgurResponse($response);
136
137 1
        return $imgurResponse;
138
    }
139
140
    /**
141
     * Initializes HTTP client and sets base URI
142
     */
143 8
    protected function initHttpClient()
144
    {
145 8
        $baseUri = EndpointModel::STANDARD;
146
147 8
        if ($this->client->getMashapeKey()) {
148 1
            $baseUri = EndpointModel::COMMERCIAL;
149
        }
150
151 8
        $headers = $this->prepareAuthenticationHeaders();
152
153 8
        $this->httpClient = new GuzzleClient([
154 8
            'base_uri' => $baseUri,
155 8
            'headers' => $headers,
156
        ]);
157 8
    }
158
159
    /**
160
     * Prepare Authentication headers required by Imgur and/or Mashape
161
     *
162
     * @return array
163
     */
164 8
    protected function prepareAuthenticationHeaders()
165
    {
166 8
        $headers = [];
167
168 8
        $headers['Authorization'] = 'Client-ID ' . $this->client->getClientId();
169 8
        if($this->client->getToken() && $this->client->getToken()->getAccessToken()) {
170 1
            $headers['Authorization'] = 'Bearer ' . $this->client->getToken()->getAccessToken();
171
        }
172
173 8
        if($this->client->getMashapeKey()) {
174 1
            $headers['X-Mashape-Key'] = $this->client->getMashapeKey();
175
        }
176
177 8
        return $headers;
178
    }
179
}