Issues (302)

Security Analysis    not enabled

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.

examples/Metrica/api.php (27 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
 * User: naxel
4
 * Date: 14.02.14 15:15
5
 */
6
7
use Yandex\Metrica\Management\ManagementClient;
8
use Yandex\Metrica\Analytics\AnalyticsClient;
9
10
$errorMessage = null;
11
$status = 'ok';
12
$result = null;
13
14
//Is auth
15
if (isset($_COOKIE['yaAccessToken']) && isset($_COOKIE['yaClientId'])) {
16
    $settings = require_once '../settings.php';
17
18
    try {
19
20
        $managementClient = new ManagementClient($_COOKIE['yaAccessToken']);
21
22
        if (isset($_GET['method'])) {
23
24
            switch ($_GET['method']) {
25
26
                case 'getCounter':
27
                    if (isset($_GET['counterId']) && $_GET['counterId']) {
28
                        //GET /management/v1/counter/{counterId}
29
                        $paramsObj = new \Yandex\Metrica\Management\Models\CounterParams();
30
                        $paramsObj->setField('goals,mirrors,grants,filters,operations');
31
                        /**
32
                         * @see http://api.yandex.ru/metrika/doc/beta/management/counters/counter.xml
33
                         */
34
                        $result = $managementClient
35
                            ->counters()
36
                            ->getCounter($_GET['counterId'], $paramsObj)
37
                            ->toArray();
38
                    }
39
                    break;
40
41 View Code Duplication
                case 'getFilter':
0 ignored issues
show
This code seems to be duplicated across 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...
42
                    if (isset($_GET['counterId'], $_GET['filterId']) && $_GET['counterId'] && $_GET['filterId']) {
43
                        //GET /management/v1/counter/{counterId}/filter/{filterId}
44
                        /**
45
                         * @see http://api.yandex.ru/metrika/doc/beta/management/filters/filter.xml
46
                         */
47
                        $result = $managementClient
48
                            ->filters()
49
                            ->getFilter($_GET['filterId'], $_GET['counterId'])
50
                            ->toArray();
51
                    }
52
                    break;
53
54 View Code Duplication
                case 'getGrant':
0 ignored issues
show
This code seems to be duplicated across 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
                    if (isset($_GET['userLogin'], $_GET['counterId']) && $_GET['userLogin'] && $_GET['counterId']) {
56
                        //GET /management/v1/counter/{counterId}/grant/{userLogin}
57
                        /**
58
                         * @see http://api.yandex.ru/metrika/doc/beta/management/grants/grantold.xml
59
                         */
60
                        $result = $managementClient
61
                            ->grants()
62
                            ->getGrant($_GET['counterId'], $_GET['userLogin'])
63
                            ->toArray();
64
                    }
65
                    break;
66
67 View Code Duplication
                case 'getOperation':
0 ignored issues
show
This code seems to be duplicated across 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...
68
                    //GET /management/v1/counter/{counterId}/operation/{operationId}
69
                    /**
70
                     * @see http://api.yandex.ru/metrika/doc/beta/management/operations/operation.xml
71
                     */
72
                    if (isset($_GET['operationId'], $_GET['counterId'])
73
                        && $_GET['operationId']
74
                        && $_GET['counterId']
75
                    ) {
76
                        $result = $managementClient
77
                            ->operations()
78
                            ->getOperation($_GET['operationId'], $_GET['counterId'])
79
                            ->toArray();
80
                    }
81
                    break;
82
83
                case 'getPageViewsCount':
84
                    if (isset($_GET['counterId']) && $_GET['counterId']) {
85
                        $paramsObj = new \Yandex\Metrica\Analytics\Models\Params();
86
                        $paramsObj
87
                            ->setMetrics('ga:pageviews')
88
                            ->setStartDate('6daysAgo')
89
                            ->setEndDate('today')
90
                            ->setIds('ga:' . $_GET['counterId']);
91
92
                        $analyticsClient = new AnalyticsClient($_COOKIE['yaAccessToken']);
93
                        $response = $analyticsClient
94
                            ->ga()
95
                            ->getGaData($paramsObj);
96
                        $result = $response->getRows();
97
                        if (empty($result)) {
98
                            $result = 0;
99
                        } else {
100
                            $result = current(current($result));
101
                        }
102
                    }
103
104
                    break;
105
            }
106
        }
107
108
        if (isset($_POST['method'])) {
109
110
            switch ($_POST['method']) {
111
                case 'addCounter':
112
                    if (isset($_POST['counterSite']) && isset($_POST['counterName'])) {
113
                        //POST /counters
114
                        /**
115
                         * @see http://api.yandex.ru/metrika/doc/ref/reference/add-counter.xml
116
                         */
117
                        $counterPostRequest = new Yandex\Metrica\Management\Models\Counter();
118
                        $counterPostRequest
119
                            ->setName($_POST['counterName'])
120
                            ->setSite($_POST['counterSite']);
121
122
                        $codeOptions = new \Yandex\Metrica\Management\Models\CodeOptions();
123
                        $codeOptions
124
                            ->setAsync(1)
0 ignored issues
show
1 is of type integer, but the function expects a boolean.

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...
125
                            ->setTrackHash(1)
0 ignored issues
show
1 is of type integer, but the function expects a boolean.

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...
126
                            ->setVisor(1)
0 ignored issues
show
1 is of type integer, but the function expects a boolean.

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...
127
                            ->setUt(1)
0 ignored issues
show
1 is of type integer, but the function expects a boolean.

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...
128
                            ->setXmlSite(1)
0 ignored issues
show
1 is of type integer, but the function expects a boolean.

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...
129
                            ->setInOneLine(1);
0 ignored issues
show
1 is of type integer, but the function expects a boolean.

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...
130
131
                        $counterPostRequest->setCodeOptions($codeOptions);
132
133
                        $result = $managementClient
134
                            ->counters()
135
                            ->addCounter($counterPostRequest)
136
                            ->toArray();
137
                    }
138
                    break;
139
140
                case 'updateCounter':
141
                    if (isset($_POST['counterSite']) && isset($_POST['counterName'])
142
                        && isset($_POST['counterId']) && $_POST['counterId']
143
                    ) {
144
                        //PUT /counter/{id}
145
                        /**
146
                         * @see http://api.yandex.ru/metrika/doc/ref/reference/edit-counter.xml
147
                         */
148
                        $params = new Yandex\Metrica\Management\Models\ExtendCounter();
149
150
                        $codeOptions = new \Yandex\Metrica\Management\Models\CodeOptions();
151
                        $codeOptions
152
                            ->setAsync(1)
0 ignored issues
show
1 is of type integer, but the function expects a boolean.

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...
153
                            ->setTrackHash(0)
0 ignored issues
show
0 is of type integer, but the function expects a boolean.

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...
154
                            ->setVisor(0)
0 ignored issues
show
0 is of type integer, but the function expects a boolean.

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...
155
                            ->setUt(0)
0 ignored issues
show
0 is of type integer, but the function expects a boolean.

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...
156
                            ->setXmlSite(0)
0 ignored issues
show
0 is of type integer, but the function expects a boolean.

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...
157
                            ->setInOneLine(0);
0 ignored issues
show
0 is of type integer, but the function expects a boolean.

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...
158
159
                        $params->setCodeOptions($codeOptions);
160
161
                        $webvisor = new \Yandex\Metrica\Management\Models\Webvisor();
162
                        $webvisor
163
                            ->setArchEnabled(1)
0 ignored issues
show
1 is of type integer, but the function expects a boolean.

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...
164
                            ->setUrls('regexp:.*')
165
                            ->setArchType('load')
166
                            ->setLoadPlayerType('on_your_behalf');
167
168
                        $params->setWebvisor($webvisor);
169
170
                        if ($_POST['counterName']) {
171
                            $params->setName($_POST['counterName']);
172
                        }
173
                        if ($_POST['counterSite']) {
174
                            $params->setSite($_POST['counterSite']);
175
                        }
176
177
                        $result = $managementClient
178
                            ->counters()
179
                            ->updateCounter($_POST['counterId'], $params)
180
                            ->toArray();
181
                    }
182
                    break;
183
184 View Code Duplication
                case 'deleteCounter':
0 ignored issues
show
This code seems to be duplicated across 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...
185
                    if (isset($_POST['counterId']) && $_POST['counterId']) {
186
                        //DELETE /counter/{id}
187
                        /**
188
                         * @see http://api.yandex.ru/metrika/doc/ref/reference/delete-counter.xml
189
                         */
190
                        $response = $managementClient
191
                            ->counters()
192
                            ->deleteCounter($_POST['counterId']);
193
194
195
                        if (isset($response['errors']) && $response['errors']) {
196
                            $status = 'error';
197
                            $errorMessage = $response['errors'][0]['text'];
198
199
                        } else {
200
                            $result = array(
201
                                'id' => $_POST['counterId']
202
                            );
203
                        }
204
                    }
205
                    break;
206
207 View Code Duplication
                case 'addDelegate':
0 ignored issues
show
This code seems to be duplicated across 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...
208
                    if (isset($_POST['userLogin'], $_POST['createAt'], $_POST['comment']) && $_POST['userLogin']) {
209
                        //POST /management/v1/delegates
210
                        /**
211
                         * @see http://api.yandex.ru/metrika/doc/beta/management/delegates/adddelegate.xml
212
                         */
213
                        $delegateModel = new Yandex\Metrica\Management\Models\Delegate();
214
                        $delegateModel
215
                            ->setUserLogin($_POST['userLogin'])
216
                            ->setCreatedAt($_POST['createAt'])
217
                            ->setComment($_POST['comment']);
218
                        $result = $managementClient
219
                            ->delegates()
220
                            ->addDelegates($delegateModel)
0 ignored issues
show
$delegateModel is of type object<Yandex\Metrica\Management\Models\Delegate>, but the function expects a string.

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...
221
                            ->toArray();
222
                    }
223
                    break;
224
225 View Code Duplication
                case 'updateDelegate':
0 ignored issues
show
This code seems to be duplicated across 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...
226
                    if (isset($_POST['userLogin'], $_POST['createAt'], $_POST['comment']) && $_POST['userLogin']) {
227
                        //PUT /management/v1/delegates
228
                        /**
229
                         * @see http://api.yandex.ru/metrika/doc/beta/management/delegates/updatedelegates.xml
230
                         */
231
                        $delegateModel = new Yandex\Metrica\Management\Models\Delegate();
232
                        $delegateModel
233
                            ->setUserLogin($_POST['userLogin'])
234
                            ->setCreatedAt($_POST['createAt'])
235
                            ->setComment($_POST['comment']);
236
                        $result = $managementClient
237
                            ->delegates()
238
                            ->updateDelegates($delegateModel)
0 ignored issues
show
$delegateModel is of type object<Yandex\Metrica\Management\Models\Delegate>, but the function expects a object<Yandex\Metrica\Ma...ement\Models\Delegates>.

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...
239
                            ->toArray();
240
                    }
241
                    break;
242
243 View Code Duplication
                case 'deleteDelegate':
0 ignored issues
show
This code seems to be duplicated across 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...
244
                    if (isset($_POST['userLogin']) && $_POST['userLogin']) {
245
                        //DELETE /management/v1/delegate/{userLogin}
246
                        /**
247
                         * @see http://api.yandex.ru/metrika/doc/beta/management/delegates/deletedelegateold.xml
248
                         */
249
                        $response = $managementClient
250
                            ->delegates()
251
                            ->deleteDelegate($_POST['userLogin']);
252
253
                        if (isset($response['errors']) && $response['errors']) {
254
                            $status = 'error';
255
                            $errorMessage = $response['errors'][0]['text'];
256
257
                        } else {
258
                            $result = array(
259
                                'id' => $_POST['userLogin']
260
                            );
261
                        }
262
                    }
263
                    break;
264
265 View Code Duplication
                case 'deleteAccount':
0 ignored issues
show
This code seems to be duplicated across 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...
266
                    if (isset($_POST['userLogin']) && $_POST['userLogin']) {
267
                        //DELETE /management/v1/account/{userLogin}
268
                        /**
269
                         * @see http://api.yandex.ru/metrika/doc/beta/management/accounts/deleteaccountold.xml
270
                         */
271
                        $response = $managementClient
272
                            ->accounts()
273
                            ->deleteAccount($_POST['userLogin']);
274
275
                        if (isset($response['errors']) && $response['errors']) {
276
                            $status = 'error';
277
                            $errorMessage = $response['errors'][0]['text'];
278
279
                        } else {
280
                            $result = array(
281
                                'id' => $_POST['userLogin']
282
                            );
283
                        }
284
                    }
285
                    break;
286
287
                case 'addFilter':
288
                    if (isset($_POST['params'], $_POST['counterId']) && $_POST['params'] && $_POST['counterId']) {
289
                        parse_str($_POST['params'], $params);
290
                        $filterModel = new Yandex\Metrica\Management\Models\Filter((array)$params);
291
292
                        //POST /management/v1/counter/{counterId}/filters
293
                        /**
294
                         * @see http://api.yandex.ru/metrika/doc/beta/management/filters/addfilter.xml
295
                         */
296
                        $result = $managementClient
297
                            ->filters()
298
                            ->addFilter($_POST['counterId'], $filterModel)
299
                            ->toArray();
300
                    }
301
                    break;
302
303
                case 'updateFilter':
304
                    if (isset($_POST['params'], $_POST['counterId'], $_POST['filterId'])
305
                        && $_POST['params']
306
                        && $_POST['counterId']
307
                        && $_POST['filterId']
308
                    ) {
309
                        parse_str($_POST['params'], $params);
310
                        $filterModel = new Yandex\Metrica\Management\Models\Filter((array)$params);
311
312
                        //PUT /management/v1/counter/{counterId}/filter/{filterId}
313
                        /**
314
                         * @see http://api.yandex.ru/metrika/doc/beta/management/filters/editfilter.xml
315
                         */
316
                        $result = $managementClient
317
                            ->filters()
318
                            ->updateFilter($_POST['filterId'],$_POST['counterId'], $filterModel)
319
                            ->toArray();
320
                    }
321
                    break;
322
323 View Code Duplication
                case 'deleteFilter':
0 ignored issues
show
This code seems to be duplicated across 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...
324
                    if (isset($_POST['counterId'], $_POST['filterId']) && $_POST['counterId'] && $_POST['filterId']) {
325
326
                        //DELETE /management/v1/counter/{counterId}/filter/{filterId}
327
                        /**
328
                         * @see http://api.yandex.ru/metrika/doc/beta/management/filters/deletefilter.xml
329
                         */
330
                        $response = $managementClient
331
                            ->filters()
332
                            ->deleteFilter($_POST['filterId'], $_POST['counterId']);
333
334
                        if (isset($response['errors']) && $response['errors']) {
335
                            $status = 'error';
336
                            $errorMessage = $response['errors'][0]['text'];
337
338
                        } else {
339
                            $result = array(
340
                                'id' => $_POST['filterId']
341
                            );
342
                        }
343
                    }
344
                    break;
345
346 View Code Duplication
                case 'addGrant':
0 ignored issues
show
This code seems to be duplicated across 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...
347
                    if (isset($_POST['counterId'], $_POST['userLogin'], $_POST['perm'], $_POST['comment']
348
                        , $_POST['createdAt'])
349
                        && $_POST['counterId']
350
                        && $_POST['userLogin']
351
                        && $_POST['perm']
352
                    ) {
353
354
                        $grantModel = new Yandex\Metrica\Management\Models\Grant();
355
                        $grantModel
356
                            ->setUserLogin($_POST['userLogin'])
357
                            ->setPerm($_POST['perm'])
358
                            ->setComment($_POST['comment'])
359
                            ->setCreatedAt($_POST['createdAt']);
360
361
                        //POST /management/v1/counter/{counterId}/grants
362
                        /**
363
                         * @see http://api.yandex.ru/metrika/doc/beta/management/grants/addgrant.xml
364
                         */
365
                        $result = $managementClient
366
                            ->grants()
367
                            ->addGrant($_POST['counterId'], $grantModel)
368
                            ->toArray();
369
                    }
370
                    break;
371
372 View Code Duplication
                case 'updateGrant':
0 ignored issues
show
This code seems to be duplicated across 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...
373
                    if (isset($_POST['counterId'], $_POST['userLogin'], $_POST['perm'], $_POST['comment']
374
                        , $_POST['createdAt'])
375
                        && $_POST['counterId']
376
                        && $_POST['userLogin']
377
                        && $_POST['perm']
378
                    ) {
379
380
                        $grantModel = new Yandex\Metrica\Management\Models\Grant();
381
                        $grantModel
382
                            ->setUserLogin($_POST['userLogin'])
383
                            ->setPerm($_POST['perm'])
384
                            ->setComment($_POST['comment'])
385
                            ->setCreatedAt($_POST['createdAt']);
386
387
                        //PUT /management/v1/counter/{counterId}/grant/{userLogin}
388
                        /**
389
                         * @see http://api.yandex.ru/metrika/doc/beta/management/grants/editgrantold.xml
390
                         */
391
                        $result = $managementClient
392
                            ->grants()
393
                            ->updateGrant($_POST['counterId'], $_POST['userLogin'], $grantModel)
394
                            ->toArray();
395
                    }
396
                    break;
397
398 View Code Duplication
                case 'deleteGrant':
0 ignored issues
show
This code seems to be duplicated across 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...
399
                    if (isset($_POST['counterId'], $_POST['userLogin'])
400
                        && $_POST['counterId']
401
                        && $_POST['userLogin']
402
                    ) {
403
                        //DELETE /management/v1/counter/{counterId}/grant/{userLogin}
404
                        /**
405
                         * @see http://api.yandex.ru/metrika/doc/beta/management/grants/deletegrantold.xml
406
                         */
407
                        $response = $managementClient
408
                            ->grants()
409
                            ->deleteGrant( $_POST['counterId'], $_POST['userLogin']);
410
411
                        if (isset($response['errors']) && $response['errors']) {
412
                            $status = 'error';
413
                            $errorMessage = $response['errors'][0]['text'];
414
415
                        } else {
416
                            $result = array(
417
                                'id' => $_POST['userLogin']
418
                            );
419
                        }
420
                    }
421
                    break;
422
423
                case 'addOperation':
424
                    if (isset($_POST['counterId'], $_POST['action'], $_POST['attr'], $_POST['value'], $_POST['status'])
425
                        && $_POST['counterId']
426
                    ) {
427
428
                        $operationModel = new Yandex\Metrica\Management\Models\Operation();
429
                        $operationModel->setAction($_POST['action'])
430
                            ->setAttr($_POST['attr'])
431
                            ->setValue($_POST['value'])
432
                            ->setStatus($_POST['status']);
433
434
                        //POST /management/v1/counter/{counterId}/operations
435
                        /**
436
                         * @see http://api.yandex.ru/metrika/doc/beta/management/operations/addoperation.xml
437
                         */
438
                        $result = $managementClient
439
                            ->operations()
440
                            ->addOperation($_POST['counterId'], $operationModel)
441
                            ->toArray();
442
                    }
443
                    break;
444
445
                case 'updateOperation':
446
                    if (isset($_POST['counterId'], $_POST['operationId'], $_POST['action'], $_POST['attr']
447
                        , $_POST['value'], $_POST['status'])
448
                        && $_POST['counterId']
449
                        && $_POST['operationId']
450
                    ) {
451
452
                        $operationModel = new Yandex\Metrica\Management\Models\Operation();
453
                        $operationModel
454
                            ->setAction($_POST['action'])
455
                            ->setAttr($_POST['attr'])
456
                            ->setValue($_POST['value'])
457
                            ->setStatus($_POST['status'])
458
                            ->setId($_POST['operationId']);
459
460
                        //PUT /management/v1/counter/{counterId}/operation/{operationId}
461
                        /**
462
                         * @see http://api.yandex.ru/metrika/doc/beta/management/operations/editoperation.xml
463
                         */
464
                        $result = $managementClient
465
                            ->operations()
466
                            ->updateOperation($_POST['operationId'], $_POST['counterId'], $operationModel)
467
                            ->toArray();
468
                    }
469
                    break;
470
            }
471
        }
472
473
    } catch (\Exception $ex) {
474
        $errorMessage = $ex->getMessage();
475
        if ($errorMessage === 'PlatformNotAllowed') {
476
            $errorMessage .= '<p>Возможно, у приложения нет прав на доступ к ресурсу. Попробуйте '
477
                . '<a href="' . rtrim(str_replace($_SERVER['DOCUMENT_ROOT'], '', __DIR__), "/") . '/../OAuth/' . '">авторизироваться</a> и повторить.</p>';
478
        }
479
    }
480
}
481
482
483
echo json_encode(
484
    array(
485
        'status' => $status,
486
        'message' => $errorMessage,
487
        'result' => $result,
488
    )
489
);
490