Issues (3099)

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.

Helper/Google/Analytics/ConfigHelper.php (15 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 Kunstmaan\DashboardBundle\Helper\Google\Analytics;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig;
7
8
class ConfigHelper
9
{
10
    /** @var ServiceHelper */
11
    private $serviceHelper;
12
13
    /** @var string */
14
    private $token = false;
15
16
    /** @var string */
17
    private $propertyId = false;
18
19
    /** @var string */
20
    private $accountId = false;
21
22
    /** @var string */
23
    private $profileId = false;
24
25
    /** @var EntityManager */
26
    private $em;
27
28
    /**
29
     * constructor
30
     *
31
     * @param ServiceHelper $serviceHelper
32
     * @param EntityManager $em
33
     */
34
    public function __construct(ServiceHelper $serviceHelper, EntityManager $em)
0 ignored issues
show
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
35
    {
36
        $this->serviceHelper = $serviceHelper;
37
        $this->em = $em;
38
        $this->init();
39
    }
40
41
    /**
42
     * Tries to initialise the Client object
43
     *
44
     * @param int $configId
0 ignored issues
show
Should the type for parameter $configId not be false|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
45
     */
46
    public function init($configId = false)
47
    {
48
        // if token is already saved in the database
49
        if ($this->getToken($configId) && '' !== $this->getToken($configId)) {
0 ignored issues
show
It seems like $configId defined by parameter $configId on line 46 can also be of type integer; however, Kunstmaan\DashboardBundl...onfigHelper::getToken() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
            $this
51
                ->serviceHelper
52
                ->getClientHelper()
53
                ->getClient()
54
                ->setAccessToken($this->getToken($configId));
0 ignored issues
show
It seems like $configId defined by parameter $configId on line 46 can also be of type integer; however, Kunstmaan\DashboardBundl...onfigHelper::getToken() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55
        }
56
57
        if ($configId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $configId of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
58
            $this->getAccountId($configId);
0 ignored issues
show
$configId 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...
59
            $this->getPropertyId($configId);
0 ignored issues
show
$configId 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...
60
            $this->getProfileId($configId);
0 ignored issues
show
$configId 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...
61
        }
62
    }
63
64
    /* =============================== TOKEN =============================== */
65
66
    /**
67
     * Get the token from the database
68
     *
69
     * @return string $token
70
     */
71 View Code Duplication
    private function getToken($configId = false)
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...
72
    {
73
        if (!$this->token || $configId) {
74
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
75
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
76
            if ($configId) {
77
                $this->token = $analyticsConfigRepository->find($configId)->getToken();
78
            } else {
79
                $this->token = $analyticsConfigRepository->findFirst()->getToken();
80
            }
81
        }
82
83
        return $this->token;
84
    }
85
86
    /**
87
     * Save the token to the database
88
     */
89
    public function saveToken($token, $configId = false)
90
    {
91
        $this->token = $token;
92
        $this->em->getRepository(AnalyticsConfig::class)->saveToken($token, $configId);
93
    }
94
95
    /**
96
     * Check if token is set
97
     *
98
     * @return bool $result
99
     */
100
    public function tokenIsSet()
101
    {
102
        return $this->getToken() && '' !== $this->getToken();
103
    }
104
105
    /* =============================== ACCOUNT =============================== */
106
107
    /**
108
     * Get a list of all available accounts
109
     *
110
     * @return array $data A list of all properties
111
     */
112
    public function getAccounts()
113
    {
114
        $accounts = $this->serviceHelper->getService()->management_accounts->listManagementAccounts()->getItems();
115
        $data = array();
116
117
        foreach ($accounts as $account) {
118
            $data[$account->getName()] = array(
119
                    'accountId' => $account->getId(),
120
                    'accountName' => $account->getName(),
121
                );
122
        }
123
        ksort($data);
124
125
        return $data;
126
    }
127
128
    /**
129
     * Get the accountId from the database
130
     *
131
     * @return string $accountId
132
     */
133 View Code Duplication
    public function getAccountId($configId = false)
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...
134
    {
135
        if (!$this->accountId || $configId) {
136
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
137
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
138
            if ($configId) {
139
                $this->accountId = $analyticsConfigRepository->find($configId)->getAccountId();
140
            } else {
141
                $this->accountId = $analyticsConfigRepository->findFirst()->getAccountId();
142
            }
143
        }
144
145
        return $this->accountId;
146
    }
147
148
    /**
149
     * Save the accountId to the database
150
     */
151
    public function saveAccountId($accountId, $configId = false)
152
    {
153
        $this->accountId = $accountId;
154
        $this->em->getRepository(AnalyticsConfig::class)->saveAccountId($accountId, $configId);
155
    }
156
157
    /**
158
     * Check if token is set
159
     *
160
     * @return bool $result
161
     */
162
    public function accountIsSet()
163
    {
164
        return $this->getAccountId() && '' !== $this->getAccountId();
165
    }
166
167
    /* =============================== PROPERTY =============================== */
168
169
    /**
170
     * Get a list of all available properties
171
     *
172
     * @return array $data A list of all properties
0 ignored issues
show
Should the return type not be false|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
173
     */
174
    public function getProperties($accountId = false)
175
    {
176
        if (!$this->getAccountId() && !$accountId) {
177
            return false;
178
        }
179
180
        if ($accountId) {
181
            $webproperties = $this->serviceHelper->getService()->management_webproperties->listManagementWebproperties($accountId);
182
        } else {
183
            $webproperties = $this->serviceHelper->getService()->management_webproperties->listManagementWebproperties($this->getAccountId());
184
        }
185
        $data = array();
186
187
        foreach ($webproperties->getItems() as $property) {
188
            $profiles = $this->getProfiles($accountId, $property->getId());
189
            if (\count($profiles) > 0) {
190
                $data[$property->getName()] = array(
191
                        'propertyId' => $property->getId(),
192
                        'propertyName' => $property->getName() . ' (' . $property->getWebsiteUrl() . ')',
193
                    );
194
            }
195
        }
196
        ksort($data);
197
198
        return $data;
199
    }
200
201
    /**
202
     * Get the propertyId from the database
203
     *
204
     * @return string $propertyId
205
     */
206 View Code Duplication
    public function getPropertyId($configId = false)
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...
207
    {
208
        if (!$this->propertyId || $configId) {
209
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
210
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
211
            if ($configId) {
212
                $this->propertyId = $analyticsConfigRepository->find($configId)->getPropertyId();
213
            } else {
214
                $this->propertyId = $analyticsConfigRepository->findFirst()->getPropertyId();
215
            }
216
        }
217
218
        return $this->propertyId;
219
    }
220
221
    /**
222
     * Save the propertyId to the database
223
     */
224
    public function savePropertyId($propertyId, $configId = false)
225
    {
226
        $this->propertyId = $propertyId;
227
        $this->em->getRepository(AnalyticsConfig::class)->savePropertyId($propertyId, $configId);
228
    }
229
230
    /**
231
     * Check if propertyId is set
232
     *
233
     * @return bool $result
234
     */
235
    public function propertyIsSet()
236
    {
237
        return null !== $this->getPropertyId() && '' !== $this->getPropertyId();
238
    }
239
240
    /* =============================== PROFILE =============================== */
241
242
    /**
243
     * Get a list of all available profiles
244
     *
245
     * @return array $data A list of all properties
0 ignored issues
show
Should the return type not be false|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
246
     */
247
    public function getProfiles($accountId = false, $propertyId = false)
248
    {
249
        if ((!$this->getAccountId() && !$accountId) || (!$this->getPropertyId() && !$propertyId)) {
250
            return false;
251
        }
252
253
        // get views
254
        if ($accountId && $propertyId) {
255
            $profiles = $this->serviceHelper->getService()->management_profiles->listManagementProfiles(
256
                    $accountId,
257
                    $propertyId
258
                );
259
        } else {
260
            $profiles = $this->serviceHelper->getService()->management_profiles->listManagementProfiles(
261
                    $this->getAccountId(),
262
                    $this->getPropertyId()
263
                );
264
        }
265
266
        $data = array();
267
        if (\is_array($profiles->getItems())) {
268
            foreach ($profiles->getItems() as $profile) {
269
                $data[$profile->name] = array(
270
                            'profileId' => $profile->id,
271
                            'profileName' => $profile->name,
272
                            'created' => $profile->created,
273
                        );
274
            }
275
        }
276
        ksort($data);
277
278
        return $data;
279
    }
280
281
    /**
282
     * Get the propertyId from the database
283
     *
284
     * @return string $propertyId
285
     */
286 View Code Duplication
    public function getProfileId($configId = false)
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...
287
    {
288
        if (!$this->profileId || $configId) {
289
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
290
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
291
            if ($configId) {
292
                $this->profileId = $analyticsConfigRepository->find($configId)->getProfileId();
293
            } else {
294
                $this->profileId = $analyticsConfigRepository->findFirst()->getProfileId();
295
            }
296
        }
297
298
        return $this->profileId;
299
    }
300
301
    /**
302
     * Save the profileId to the database
303
     */
304
    public function saveProfileId($profileId, $configId = false)
305
    {
306
        $this->profileId = $profileId;
307
        $this->em->getRepository(AnalyticsConfig::class)->saveProfileId($profileId, $configId);
308
    }
309
310
    /**
311
     * Check if token is set
312
     *
313
     * @return bool $result
314
     */
315
    public function profileIsSet()
316
    {
317
        return null !== $this->getProfileId() && '' !== $this->getProfileId();
318
    }
319
320
    /**
321
     * Get the active profile
322
     *
323
     * @return the profile
324
     */
325
    public function getActiveProfile()
326
    {
327
        $profiles = $this->getProfiles();
328
        $profileId = $this->getProfileId();
329
330
        if (!\is_array($profiles)) {
331
            throw new \Exception('<fg=red>The config is invalid</fg=red>');
332
        }
333
334
        foreach ($profiles as $profile) {
335
            if ($profile['profileId'] == $profileId) {
336
                return $profile;
337
            }
338
        }
339
    }
340
341
    /* =============================== PROFILE SEGMENTS =============================== */
342
343
    /**
344
     * get all segments for the saved profile
345
     *
346
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
347
     */
348
    public function getProfileSegments()
349
    {
350
        $profileSegments = $this
351
                    ->serviceHelper
352
                    ->getService()
353
                    ->management_segments
354
                    ->listManagementSegments()
355
                    ->items;
356
357
        $builtin = array();
358
        $own = array();
359
        foreach ($profileSegments as $segment) {
360
            if ($segment->type == 'BUILT_IN') {
361
                $builtin[] = array(
362
                        'name' => $segment->name,
363
                        'query' => $segment->segmentId,
364
                    );
365
            } else {
366
                $own[] = array(
367
                        'name' => $segment->name,
368
                        'query' => $segment->segmentId,
369
                    );
370
            }
371
        }
372
373
        return array('builtin' => $builtin, 'own' => $own);
374
    }
375
376
    /* =============================== CONFIG =============================== */
377
378
    /**
379
     * Save the config to the database
380
     */
381
    public function saveConfigName($configName, $configId = false)
382
    {
383
        $this->em->getRepository(AnalyticsConfig::class)->saveConfigName($configName, $configId);
384
    }
385
386
    /* =============================== AUTH URL =============================== */
387
388
    /**
389
     * get the authUrl
390
     *
391
     * @return string $authUrl
392
     */
393
    public function getAuthUrl()
394
    {
395
        return $this
396
                ->serviceHelper
397
                ->getClientHelper()
398
                ->getClient()
399
                ->createAuthUrl();
400
    }
401
}
402