GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Issues (3)

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/Analytics.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
namespace Spatie\Analytics;
4
5
use Carbon\Carbon;
6
use DateTime;
7
use Exception;
8
use Google_Auth_AssertionCredentials;
9
use Google_Client;
10
11
class Analytics
12
{
13
    /**
14
     * @var \Spatie\Analytics\GoogleClient
15
     */
16
    protected $client;
17
18
    /**
19
     * @var string
20
     */
21
    protected $siteId;
22
23
    /**
24
     * @param  \Spatie\Analytics\GoogleClient $client
25
     * @param  string $siteId
26
     */
27
    public function __construct(GoogleClient $client, $siteId = '')
28
    {
29
        $this->client = $client;
30
        $this->siteId = $siteId;
31
    }
32
33
    /**
34
     * Get the amount of visitors and pageViews.
35
     *
36
     * @param  int $numberOfDays
37
     * @param  string $groupBy  Possible values: date, yearMonth
38
     * @return array
39
     */
40
    public function getVisitorsAndPageViews($numberOfDays = 365, $groupBy = 'date')
41
    {
42
        list($startDate, $endDate) = $this->calculateNumberOfDays($numberOfDays);
43
44
        return $this->getVisitorsAndPageViewsForPeriod($startDate, $endDate, $groupBy);
45
    }
46
47
    /**
48
     * Get the amount of visitors and pageviews for the given period.
49
     *
50
     * @param  \DateTime $startDate
51
     * @param  \DateTime $endDate
52
     * @param  string $groupBy  Possible values: date, yearMonth
53
     * @return array
54
     */
55
    public function getVisitorsAndPageViewsForPeriod(DateTime $startDate, DateTime $endDate, $groupBy = 'date')
56
    {
57
        $answer = $this->performQuery(
58
            $startDate,
59
            $endDate,
60
            'ga:visits,ga:pageviews',
61
            array('dimensions' => 'ga:'.$groupBy)
62
        );
63
64
        if (is_null($answer->rows)) {
65
            return array();
66
        }
67
68
        $visitorData = array();
69
70
        foreach ($answer->rows as $dateRow) {
71
            $visitorData[] = array(
72
                $groupBy    => Carbon::createFromFormat(($groupBy == 'yearMonth' ? 'Ym' : 'Ymd'), $dateRow[0]),
73
                'visitors'  => $dateRow[1],
74
                'pageViews' => $dateRow[2]
75
            );
76
        }
77
78
        return $visitorData;
79
    }
80
81
    /**
82
     * Get the top keywords.
83
     *
84
     * @param  int $numberOfDays
85
     * @param  int $maxResults
86
     * @return array
87
     */
88
    public function getTopKeywords($numberOfDays = 365, $maxResults = 30)
89
    {
90
        list($startDate, $endDate) = $this->calculateNumberOfDays($numberOfDays);
91
92
        return $this->getTopKeyWordsForPeriod($startDate, $endDate, $maxResults);
93
    }
94
95
    /**
96
     * Get the top keywords for the given period.
97
     *
98
     * @param  \DateTime $startDate
99
     * @param  \DateTime $endDate
100
     * @param  int $maxResults
101
     * @return array
102
     */
103
    public function getTopKeyWordsForPeriod(DateTime $startDate, DateTime $endDate, $maxResults = 30)
104
    {
105
        $answer = $this->performQuery(
106
            $startDate,
107
            $endDate,
108
            'ga:sessions',
109
            array(
110
                'dimensions' => 'ga:keyword',
111
                'sort' => '-ga:sessions',
112
                'max-results' => $maxResults,
113
                'filters' => 'ga:keyword!=(not set);ga:keyword!=(not provided)'
114
            )
115
        );
116
117
        if (is_null($answer->rows)) {
118
            return array();
119
        }
120
121
        $keywordData = array();
122
123
        foreach ($answer->rows as $pageRow) {
124
            $keywordData[] = array(
125
                'keyword' => $pageRow[0],
126
                'sessions' => $pageRow[1]
127
            );
128
        }
129
130
        return $keywordData;
131
    }
132
133
    /**
134
     * Get the top referrers.
135
     *
136
     * @param  int $numberOfDays
137
     * @param  int $maxResults
138
     * @return array
139
     */
140
    public function getTopReferrers($numberOfDays = 365, $maxResults = 20)
141
    {
142
        list($startDate, $endDate) = $this->calculateNumberOfDays($numberOfDays);
143
144
        return $this->getTopReferrersForPeriod($startDate, $endDate, $maxResults);
145
    }
146
147
    /**
148
     * Get the top referrers for the given period.
149
     *
150
     * @param  \DateTime $startDate
151
     * @param  \DateTime $endDate
152
     * @param  int $maxResults
153
     * @return array
154
     */
155 View Code Duplication
    public function getTopReferrersForPeriod(DateTime $startDate, DateTime $endDate, $maxResults)
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...
156
    {
157
        $answer = $this->performQuery(
158
            $startDate,
159
            $endDate,
160
            'ga:pageviews',
161
            array(
162
                'dimensions' => 'ga:fullReferrer',
163
                'sort' => '-ga:pageviews',
164
                'max-results' => $maxResults
165
            )
166
        );
167
168
        if (is_null($answer->rows)) {
169
            return array();
170
        }
171
172
        $referrerData = array();
173
174
        foreach ($answer->rows as $pageRow) {
175
            $referrerData[] = array(
176
                'url' => $pageRow[0],
177
                'pageViews' => $pageRow[1]
178
            );
179
        }
180
181
        return $referrerData;
182
    }
183
184
    /**
185
     * Get the top browsers.
186
     *
187
     * @param  int $numberOfDays
188
     * @param  int $maxResults
189
     * @return array
190
     */
191
    public function getTopBrowsers($numberOfDays = 365, $maxResults = 6)
192
    {
193
        list($startDate, $endDate) = $this->calculateNumberOfDays($numberOfDays);
194
195
        return $this->getTopBrowsersForPeriod($startDate, $endDate, $maxResults);
196
    }
197
198
    /**
199
     * Get the top browsers for the given period.
200
     *
201
     * @param  \DateTime $startDate
202
     * @param  \DateTime $endDate
203
     * @param  int $maxResults
204
     * @return array
205
     */
206
    public function getTopBrowsersForPeriod(DateTime $startDate, DateTime $endDate, $maxResults)
207
    {
208
        $answer = $this->performQuery(
209
            $startDate,
210
            $endDate,
211
            'ga:sessions',
212
            array(
213
                'dimensions' => 'ga:browser',
214
                'sort' => '-ga:sessions'
215
            )
216
        );
217
218
        if (is_null($answer->rows)) {
219
            return array();
220
        }
221
222
        $browserData = array();
223
224
        foreach ($answer->rows as $browserRow) {
225
            $browserData[] = array(
226
                'browser' => $browserRow[0],
227
                'sessions' => $browserRow[1]
228
            );
229
        }
230
231
        $browserCollection = array_slice($browserData, 0, $maxResults - 1);
232
233
        if (count($browserData) > $maxResults) {
234
            $otherBrowsers = array_slice($browserData, $maxResults - 1);
235
236
            $sessions = array_map(function ($browser) {
237
                return $browser['sessions'];
238
            }, $otherBrowsers);
239
240
            $browserCollection[] = array(
241
                'browser' => 'other',
242
                'sessions' => array_sum($sessions)
243
            );
244
        }
245
246
        return $browserCollection;
247
    }
248
249
    /**
250
     * Get the most visited pages.
251
     *
252
     * @param  int $numberOfDays
253
     * @param  int $maxResults
254
     * @return array
255
     */
256
    public function getMostVisitedPages($numberOfDays = 365, $maxResults = 20)
257
    {
258
        list($startDate, $endDate) = $this->calculateNumberOfDays($numberOfDays);
259
260
        return $this->getMostVisitedPagesForPeriod($startDate, $endDate, $maxResults);
261
    }
262
263
    /**
264
     * Get the number of active users currently on the site
265
     *
266
     * @param  array $others
267
     * @return int
268
     */
269
    public function getActiveUsers($others = array())
270
    {
271
        $answer = $this->performRealTimeQuery('rt:activeUsers', $others);
272
    
273
        if (is_null($answer->rows)) {
274
            return 0;
275
        }
276
        
277
        return $answer->rows[0][0];
278
    }
279
    
280
    /**
281
     * Get the most visited pages for the given period.
282
     *
283
     * @param  \DateTime $startDate
284
     * @param  \DateTime $endDate
285
     * @param  int $maxResults
286
     * @return array
287
     */
288 View Code Duplication
    public function getMostVisitedPagesForPeriod(DateTime $startDate, DateTime $endDate, $maxResults = 20)
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...
289
    {
290
        $answer = $this->performQuery(
291
            $startDate,
292
            $endDate,
293
            'ga:pageviews',
294
            array(
295
                'dimensions' => 'ga:pagePath',
296
                'sort' => '-ga:pageviews',
297
                'max-results' => $maxResults
298
            )
299
        );
300
301
        if (is_null($answer->rows)) {
302
            return array();
303
        }
304
305
        $pagesData = array();
306
307
        foreach ($answer->rows as $pageRow) {
308
            $pagesData[] = array(
309
                'url' => $pageRow[0],
310
                'pageViews' => $pageRow[1]
311
            );
312
        }
313
314
        return $pagesData;
315
    }
316
317
    /**
318
     * Returns the site id (ga:xxxxxxx) for the given url.
319
     *
320
     * @param  string $url
321
     * @return string
322
     */
323
    public function getSiteIdByUrl($url)
324
    {
325
        return $this->client->getSiteIdByUrl($url);
326
    }
327
328
    /**
329
     * Call the query method on the authenticated client.
330
     *
331
     * @param  \DateTime $startDate
332
     * @param  \DateTime $endDate
333
     * @param  string $metrics
334
     * @param  array $others
335
     * @return mixed
336
     */
337
    public function performQuery(DateTime $startDate, DateTime $endDate, $metrics, $others = array())
338
    {
339
        return $this->client->performQuery(
340
            $this->siteId,
341
            $startDate->format('Y-m-d'),
342
            $endDate->format('Y-m-d'),
343
            $metrics,
344
            $others
345
        );
346
    }
347
348
    /**
349
     * Call the real time query method on the authenticated client.
350
     *
351
     * @param  string $metrics
352
     * @param  array $others
353
     * @return mixed
354
     */
355
    public function performRealTimeQuery($metrics, $others = array())
356
    {
357
        return $this->client->performRealTimeQuery(
358
            $this->siteId,
359
            $metrics,
360
            $others
361
        );
362
    }
363
    
364
    /**
365
     * Return true if this site is configured to use Google Analytics.
366
     *
367
     * @return bool
368
     */
369
    public function isEnabled()
370
    {
371
        return $this->siteId != '';
372
    }
373
374
    /**
375
     * Returns an array with the current date and the date minus the number of days specified.
376
     *
377
     * @param  int $numberOfDays
378
     * @return array
379
     */
380
    protected function calculateNumberOfDays($numberOfDays)
381
    {
382
        $endDate = Carbon::today();
383
        $startDate = Carbon::today()->subDays($numberOfDays);
384
385
        return array(
386
            $startDate,
387
            $endDate
388
        );
389
    }
390
391
    /**
392
     * Create a new instance via a set of parameters
393
     * 
394
     * @param  string $siteId
395
     *         Ex. ga:xxxxxxxx
396
     * @param  string $clientId
397
     *         Ex. xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com
398
     * @param  string $serviceEmail
399
     *         Ex. xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com
400
     * @param  string $certificatePath
401
     *         Ex. /../keys/analytics/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12
402
     * 
403
     * @param  \Spatie\Analytics\Cache|null $cache
404
     * @param  int $cacheLifetimeInMinutes
405
     * @param  int $realTimeCacheLifetime
406
     * 
407
     * @return \Spatie\Analytics\Analytics
408
     * 
409
     * @throws \Exception
410
     */
411
    public static function create($siteId, $clientId, $serviceEmail, $certificatePath, Cache $cache = null,
412
        $cacheLifetimeInMinutes = 0, $realTimeCacheLifetime = 0
413
    ) {
414
        if (! file_exists($certificatePath)) {
415
            throw new Exception("Can't find the .p12 certificate in: $certificatePath");
416
        }
417
418
        $client = new Google_Client(
419
            array(
420
                'oauth2_client_id' => $clientId,
421
                'use_objects'      => true,
422
            )
423
        );
424
425
        $client->setAccessType('offline');
426
427
        $client->setAssertionCredentials(
428
            new Google_Auth_AssertionCredentials(
429
                $serviceEmail,
430
                array('https://www.googleapis.com/auth/analytics.readonly'),
431
                file_get_contents($certificatePath)
432
            )
433
        );
434
435
        $googleApi = new GoogleClient($client, $cache);
436
437
        $googleApi        
438
            ->setCacheLifeTimeInMinutes($cacheLifetimeInMinutes)
439
            ->setRealTimeCacheLifeTimeInMinutes($realTimeCacheLifetime);
440
441
        return new static($googleApi, $siteId);
442
    }
443
}
444