Issues (10)

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/api/BaseAPI.php (2 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
 * Sunlight-php
4
 *
5
 * PHP Version 7.0.1
6
 *
7
 * @category Sunlight_Foundation_API
8
 * @package  ContactMyReps
9
 * @author   edfialk <[email protected]>
10
 * @license  http://opensource.org/licenses/MIT MIT License
11
 * @link     https://github.com/contactmyreps/sunlight-php
12
 */
13
14
namespace ContactMyReps\Sunlight\api;
15
16
use GuzzleHttp\Client;
17
use GuzzleHttp\Exception\RequestException;
18
use Psr\Http\Message\ResponseInterface;
19
20
/**
21
* Sunlight Foundation Base API
22
 *
23
 * @category Sunlight_Foundation_API
24
 * @package  ContactMyReps
25
 * @author   edfialk <[email protected]>
26
 * @license  http://opensource.org/licenses/MIT MIT License
27
 * @link     https://github.com/contactmyreps/sunlight-php
28
*/
29
abstract class BaseAPI
30
{
31
32
    public $options = [];
33
34
    private static $instance;
35
36
    /**
37
     * Create api
38
     *
39
     * @param array $options local api options
40
     */
41
    public function __construct($options = [])
42
    {
43
        $this->options = array_merge($this->options, $options);
44
    }
45
46
    /**
47
     * Set or retrieve option value
48
     *
49
     * @param string or array $key   the option key to set/retrieve, or an array of options to set
50
     * @param object          $value value to store
51
     *
52
     * @return object          if key is string and value is null, return options[key] value
53
     */
54 View Code Duplication
    public function option($key, $value = 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...
55
    {
56
        if (is_array($key)) {
57
            $this->options = array_merge($this->options, $key);
58
            return;
59
        }
60
61
        if (null === $value) {
62
            return $this->options[$key] ?? null;
63
        }
64
65
        $this->options[$key] = $value;
66
    }
67
68
    /**
69
     * Submit asynchronous get request
70
     *
71
     * @param string $url    request url
72
     * @param array  $fields response fields
73
     *
74
     * @return GuzzleHttp\Promise      Request promise
75
     */
76
    public function getAsync($url, $fields = [])
77
    {
78
        $url = $this->addFieldsToUrl($fields, $url);
79
80
        return $this->getClient()->getAsync($url)->then(
81
            function (ResponseInterface $response) {
82
                return $this->format($response);
0 ignored issues
show
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<ContactMyReps\Sunlight\api\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
            },
84
            function (RequestException $e) {
85
                echo "INSIDE BASE ASYNC EXCEPTION\n";
86
                return $e;
87
            }
88
        );
89
    }
90
91
    /**
92
     * Submit get request
93
     *
94
     * @param string $url    request url
95
     * @param array  $fields response fields
96
     *
97
     * @return GuzzleHttp\Promise or json or array         response
98
     */
99
    public function get($url, $fields = [])
100
    {
101
        $url = $this->addFieldsToUrl($fields, $url);
102
103
        if ($this->option('async') === true) {
104
            return $this->getAsync($url);
105
        }
106
107
        $response = $this->getClient()->get($url);
108
        return $this->format($response);
109
    }
110
111
    /**
112
     * Convert guzzle response to data
113
     *
114
     * @param Response $response GuzzleHttp\Response
115
     *
116
     * @return string or array           data
117
     */
118
    public function format($response)
119
    {
120
        $data = (string) $response->getBody();
121
122
        if ($this->option('json') === true) {
123
            return $data;
124
        }
125
126
        return json_decode($data);
127
    }
128
129
    /**
130
     * Get API instance
131
     *
132
     * @param string $key     API class name
133
     * @param array  $options API options
134
     *
135
     * @return object          API instance
136
     */
137
    public static function getInstance($key, $options)
138
    {
139
        $class = get_called_class();
140
        if (!isset(self::$instance[$class])) {
141
            self::$instance[$class] = new static($options);
142
            self::$instance[$class]->setClient($key);
143
        }
144
        return self::$instance[$class];
145
    }
146
147
    /**
148
     * Copy display fields to url string
149
     *
150
     * @param array  $fields response fields
151
     * @param string $url    request url
152
     *
153
     * @return string url with ?fields=field1,field2
154
     */
155
    public function addFieldsToUrl($fields, $url)
156
    {
157
        if (count($fields) == 0) {
158
            return $url;
159
        }
160
161
        $url .= strpos($url, "?") !== false ? "&" : "?";
162
        $url .= "fields=".implode(",", $fields);
163
        return $url;
164
    }
165
166
    /**
167
     * Create API Guzzle Client
168
     *
169
     * @param string $key Sunlight Foundation API key
170
     *
171
     * @return null
172
     */
173
    abstract protected function setClient($key);
174
175
    /**
176
     * Get API Guzzle Client
177
     *
178
     * @return GuzzleHttp\Client API Client
179
     */
180
    abstract protected function getClient();
181
}
182