Issues (4)

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/Client.php (3 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
/**
4
 * This file is part of the Telegraph package.
5
 *
6
 * (c) Arthur Edamov <[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
declare(strict_types = 1);
13
14
namespace Telegraph;
15
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Class Client
20
 * @package Telegraph
21
 */
22
class Client
23
{
24
    const BASE_URI = 'https://api.telegra.ph/';
25
26
    /**
27
     * @var \GuzzleHttp\Client
28
     */
29
    private $httpClient;
30
31
    /**
32
     * Client constructor.
33
     */
34
    public function __construct()
35
    {
36
        $this->httpClient = new \GuzzleHttp\Client(['base_uri' => self::BASE_URI]);
37
    }
38
39
    /**
40
     * @param string $shortName
41
     * @param string $authorName
42
     * @param string $authorUrl
43
     * @return Account
44
     */
45 View Code Duplication
    public function createAccount(string $shortName, string $authorName = '', string $authorUrl = ''): Account
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...
46
    {
47
        $response = $this->httpClient->post('/createAccount', [
48
            'json' => [
49
                'short_name' => $shortName,
50
                'author_name' => $authorName,
51
                'author_url' => $authorUrl
52
            ]
53
        ]);
54
55
        return new Account(self::getDecodedResponseResult($response));
56
    }
57
58
    /**
59
     * @param string $accessToken
60
     * @param string $shortName
61
     * @param string $authorName
62
     * @param string $authorUrl
63
     * @return Account
64
     */
65 View Code Duplication
    public function editAccountInfo(string $accessToken, string $shortName, string $authorName = '', string $authorUrl = ''): Account
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...
66
    {
67
        $response = $this->httpClient->post('/editAccountInfo', [
68
            'json' => [
69
                'access_token' => $accessToken,
70
                'short_name' => $shortName,
71
                'author_name' => $authorName,
72
                'author_url' => $authorUrl
73
            ]
74
        ]);
75
76
        return new Account(self::getDecodedResponseResult($response));
77
    }
78
79
    /**
80
     * @param string $accessToken
81
     * @param array $fields
82
     * @return Account
83
     */
84 View Code Duplication
    public function getAccountInfo(string $accessToken, array $fields = ['short_name', 'author_name', 'author_url']): Account
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...
85
    {
86
        $response = $this->httpClient->post('/getAccountInfo', [
87
            'json' => [
88
                'access_token' => $accessToken,
89
                'fields' => $fields
90
            ]
91
        ]);
92
93
        return new Account(self::getDecodedResponseResult($response));
94
    }
95
96
    /**
97
     * @param string $accessToken
98
     * @return Account
99
     */
100
    public function revokeAccessToken(string $accessToken): Account
101
    {
102
        $response = $this->httpClient->post('/revokeAccessToken', [
103
            'json' => [
104
                'access_token' => $accessToken
105
            ]
106
        ]);
107
108
        return new Account(self::getDecodedResponseResult($response));
109
    }
110
111
112
    /**
113
     * @param string $accessToken
114
     * @param string $title
115
     * @param string|NodeElement[] $content
116
     * @param string $authorName
117
     * @param string $authorUrl
118
     * @param bool $returnContent
119
     * @return Page
120
     */
121
    public function createPage(string $accessToken, string $title, $content, string $authorName = '', string $authorUrl = '', bool $returnContent = false): Page
122
    {
123
        $response = $this->httpClient->post('/createPage', [
124
            'json' => [
125
                'access_token' => $accessToken,
126
                'title' => $title,
127
                'content' => self::decoratePageContent($content),
128
                'author_name' => $authorName,
129
                'author_url' => $authorUrl,
130
                'return_content' => $returnContent
131
            ]
132
        ]);
133
134
        return new Page(self::getDecodedResponseResult($response));
135
    }
136
137
    public function editPage(string $accessToken, string $path, string $title, $content, string $authorName = null, string $authorUrl = null, bool $returnContent = false)
138
    {
139
        $response = $this->httpClient->post('/editPage/' . $path, [
140
            'json' => array_filter([
141
                'access_token' => $accessToken,
142
                'title' => $title,
143
                'content' => self::decoratePageContent($content),
144
                'author_name' => $authorName,
145
                'author_url' => $authorUrl,
146
                'return_content' => $returnContent,
147
            ])
148
        ]);
149
150
        return new Page(self::getDecodedResponseResult($response));
151
    }
152
153
    public function getPage(string $path, bool $returnContent = false)
154
    {
155
        $response = $this->httpClient->post('/getPage/' . $path, [
156
            'json' => [
157
                'return_content' => $returnContent
158
            ]
159
        ]);
160
161
        return new Page(self::getDecodedResponseResult($response));
162
    }
163
164
    public function getPageList(string $accessToken, int $offset = 0, int $limit = 50)
165
    {
166
        $response = $this->httpClient->post('/getPageList', [
167
            'json' => [
168
                'access_token' => $accessToken,
169
                'offset' => $offset,
170
                'limit' => $limit
171
            ]
172
        ]);
173
174
        return new PageList(self::getDecodedResponseResult($response));
175
    }
176
177
    public function getViews(string $path, int $year = null, int $month = null, int $day = null, int $hour = null)
178
    {
179
        $response = $this->httpClient->post('/getPage', [
180
            'json' => [
181
                'path' => $path,
182
                'year' => $year,
183
                'month' => $month,
184
                'day' => $day,
185
                'hour' => $hour,
186
            ]
187
        ]);
188
189
        return new PageViews(self::getDecodedResponseResult($response));
190
    }
191
192
    /**
193
     * @param ResponseInterface $response
194
     * @return array
195
     * @throws RequestException
196
     */
197
    private static function getDecodedResponseResult(ResponseInterface $response): array
198
    {
199
        $decodedResponse = self::decodeResponse($response);
200
201
        if ($decodedResponse['ok'] === false) {
202
            throw new RequestException($decodedResponse['error']);
203
        }
204
205
        return $decodedResponse['result'];
206
    }
207
208
    /**
209
     * @param ResponseInterface $response
210
     * @return mixed
211
     */
212
    private static function decodeResponse(ResponseInterface $response)
213
    {
214
        return json_decode($response->getBody()->getContents(), true);
215
    }
216
217
    /**
218
     * @param NodeElement[]|string $content
219
     * @return array
220
     * @throws InvalidContentTypeException
221
     */
222
    private static function decoratePageContent($content): array
223
    {
224
        if (is_string($content)) {
225
            return [$content];
226
        } elseif (is_array($content)) {
227
            $result = [];
228
            foreach ($content as $item) {
229
                if (!$item instanceof NodeElement) {
230
                    throw new InvalidContentTypeException();
231
                }
232
                $result[] = $item->toArray();
233
            }
234
235
            return $result;
236
        }
237
238
        throw new InvalidContentTypeException();
239
    }
240
}
241