Stats::showPage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace PragmaRX\Tracker\Vendor\Laravel\Controllers;
4
5
use Bllim\Datatables\Facade\Datatables;
6
use Illuminate\Routing\Controller;
7
use Illuminate\Support\Facades\View;
8
use Illuminate\Support\Str;
9
use PragmaRX\Tracker\Vendor\Laravel\Facade as Tracker;
10
use PragmaRX\Tracker\Vendor\Laravel\Support\Session;
11
12
class Stats extends Controller
13
{
14
    private $adminProperties = [
15
        'admin',
16
        'root',
17
        'is_admin',
18
        'is_root',
19
    ];
20
21
    public function __construct()
22
    {
23
        $this->authentication = app()->make('tracker.authentication');
0 ignored issues
show
Bug introduced by
The property authentication does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
    }
25
26
    public function index(Session $session)
27
    {
28
        if (!$this->isAuthenticated()) {
29
            return View::make('pragmarx/tracker::message')->with('message', trans('tracker::tracker.auth_required'));
30
        }
31
32
        if (!$this->hasAdminProperty()) {
33
            return View::make('pragmarx/tracker::message')->with('message', trans('tracker::tracker.miss_admin_prop'));
34
        }
35
36
        if (!$this->isAdmin()) {
37
            return View::make('pragmarx/tracker::message')->with('message', trans('tracker::tracker.not_admin'));
38
        }
39
40
        return $this->showPage($session, $session->getValue('page'));
41
    }
42
43
    /**
44
     * @param Session $session
45
     */
46
    public function showPage($session, $page)
47
    {
48
        $me = $this;
49
50
        if (method_exists($me, $page)) {
51
            return $this->$page($session);
52
        }
53
    }
54
55
    public function visits(Session $session)
56
    {
57
        $datatables_data =
58
        [
59
            'datatables_ajax_route' => route('tracker.stats.api.visits'),
60
            'datatables_columns'    => '
61
                { "data" : "id",          "title" : "'.trans('tracker::tracker.id').'", "orderable": true, "searchable": true },
62
                { "data" : "client_ip",   "title" : "'.trans('tracker::tracker.ip_address').'", "orderable": true, "searchable": true },
63
                { "data" : "country",     "title" : "'.trans('tracker::tracker.country_city').'", "orderable": true, "searchable": true },
64
                { "data" : "user",        "title" : "'.trans('tracker::tracker.user').'", "orderable": true, "searchable": true },
65
                { "data" : "device",      "title" : "'.trans('tracker::tracker.device').'", "orderable": true, "searchable": true },
66
                { "data" : "browser",     "title" : "'.trans('tracker::tracker.browser').'", "orderable": true, "searchable": true },
67
                { "data" : "language",    "title" : "'.trans('tracker::tracker.language').'", "orderable": true, "searchable": true },
68
                { "data" : "referer",     "title" : "'.trans('tracker::tracker.referer').'", "orderable": true, "searchable": true },
69
                { "data" : "pageViews",   "title" : "'.trans('tracker::tracker.page_views').'", "orderable": true, "searchable": true },
70
                { "data" : "lastActivity","title" : "'.trans('tracker::tracker.last_activity').'", "orderable": true, "searchable": true },
71
            ',
72
        ];
73
74
        return View::make('pragmarx/tracker::index')
75
            ->with('sessions', Tracker::sessions($session->getMinutes()))
76
            ->with('title', ''.trans('tracker::tracker.visits').'')
77
            ->with('username_column', Tracker::getConfig('authenticated_user_username_column'))
78
            ->with('datatables_data', $datatables_data);
79
    }
80
81
    public function log($uuid)
82
    {
83
        return View::make('pragmarx/tracker::log')
84
                ->with('uuid', $uuid)
85
                ->with('title', 'log');
86
    }
87
88
    public function summary()
89
    {
90
        return View::make('pragmarx/tracker::summary')
91
                ->with('title', ''.trans('tracker::tracker.page_views_summary').'');
92
    }
93
94
    public function apiPageviews(Session $session)
95
    {
96
        return Tracker::pageViews($session->getMinutes())->toJson();
97
    }
98
99
    public function apiPageviewsByCountry(Session $session)
100
    {
101
        return Tracker::pageViewsByCountry($session->getMinutes())->toJson();
102
    }
103
104
    public function apiLog($uuid)
105
    {
106
        $query = Tracker::sessionLog($uuid, false);
107
108
        $query->select([
109
            'id',
110
            'session_id',
111
            'method',
112
            'path_id',
113
            'query_id',
114
            'route_path_id',
115
            'is_ajax',
116
            'is_secure',
117
            'is_json',
118
            'wants_json',
119
            'error_id',
120
            'created_at',
121
        ]);
122
123
        return Datatables::of($query)
124
            ->edit_column('route_name', function ($row) {
125
                $path = $row->routePath;
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
126
127
                return    $row->routePath
128
                            ? $row->routePath->route->name.'<br>'.$row->routePath->route->action
129
                            : ($row->path ? $row->path->path : '');
130
            })
131
132
            ->edit_column('route', function ($row) {
133
                $route = null;
134
135
                if ($row->routePath) {
136
                    foreach ($row->routePath->parameters as $parameter) {
137
                        $route .= ($route ? '<br>' : '').$parameter->parameter.'='.$parameter->value;
138
                    }
139
                }
140
141
                return $route;
142
            })
143
144
            ->edit_column('query', function ($row) {
145
                $query = null;
146
147
                if ($row->logQuery) {
148
                    foreach ($row->logQuery->arguments as $argument) {
149
                        $query .= ($query ? '<br>' : '').$argument->argument.'='.$argument->value;
150
                    }
151
                }
152
153
                return $query;
154
            })
155
156
            ->edit_column('is_ajax', function ($row) {
157
                return    $row->is_ajax ? 'yes' : 'no';
158
            })
159
160
            ->edit_column('is_secure', function ($row) {
161
                return    $row->is_secure ? 'yes' : 'no';
162
            })
163
164
            ->edit_column('is_json', function ($row) {
165
                return    $row->is_json ? 'yes' : 'no';
166
            })
167
168
            ->edit_column('wants_json', function ($row) {
169
                return    $row->wants_json ? 'yes' : 'no';
170
            })
171
172
            ->edit_column('error', function ($row) {
173
                return    $row->error ? 'yes' : 'no';
174
            })
175
176
            ->make(true);
177
    }
178
179
    public function users(Session $session)
180
    {
181
        return View::make('pragmarx/tracker::users')
182
            ->with('users', Tracker::users($session->getMinutes()))
183
            ->with('title', ''.trans('tracker::tracker.users').'')
184
            ->with('username_column', Tracker::getConfig('authenticated_user_username_column'));
185
    }
186
187
    private function events(Session $session)
188
    {
189
        return View::make('pragmarx/tracker::events')
190
            ->with('events', Tracker::events($session->getMinutes()))
191
            ->with('title', ''.trans('tracker::tracker.events').'');
192
    }
193
194
    public function errors(Session $session)
195
    {
196
        return View::make('pragmarx/tracker::errors')
197
            ->with('error_log', Tracker::errors($session->getMinutes()))
198
            ->with('title', ''.trans('tracker::tracker.errors').'');
199
    }
200
201
    public function apiErrors(Session $session)
202
    {
203
        $query = Tracker::errors($session->getMinutes(), false);
204
205
        $query->select([
206
            'id',
207
            'error_id',
208
            'session_id',
209
            'path_id',
210
            'updated_at',
211
        ]);
212
213
        return Datatables::of($query)
214
                ->edit_column('updated_at', function ($row) {
215
                    return "{$row->updated_at->diffForHumans()}";
216
                })
217
                ->make(true);
218
    }
219
220
    public function apiEvents(Session $session)
221
    {
222
        $query = Tracker::events($session->getMinutes(), false);
223
224
        return Datatables::of($query)->make(true);
225
    }
226
227
    public function apiUsers(Session $session)
228
    {
229
        $username_column = Tracker::getConfig('authenticated_user_username_column');
230
231
        return Datatables::of(Tracker::users($session->getMinutes(), false))
232
                ->edit_column('user_id', function ($row) use ($username_column) {
233
                    return "{$row->user->$username_column}";
234
                })
235
                ->edit_column('updated_at', function ($row) {
236
                    return "{$row->updated_at->diffForHumans()}";
237
                })
238
                ->make(true);
239
    }
240
241
    public function apiVisits(Session $session)
242
    {
243
        $username_column = Tracker::getConfig('authenticated_user_username_column');
244
245
        $query = Tracker::sessions($session->getMinutes(), false);
246
247
        $query->select([
248
            'id',
249
            'uuid',
250
            'user_id',
251
            'device_id',
252
            'agent_id',
253
            'client_ip',
254
            'referer_id',
255
            'cookie_id',
256
            'geoip_id',
257
            'language_id',
258
            'is_robot',
259
            'updated_at',
260
        ]);
261
262
        return Datatables::of($query)
263
                ->edit_column('id', function ($row) {
264
                    $uri = route('tracker.stats.log', $row->uuid);
265
266
                    return '<a href="'.$uri.'">'.$row->id.'</a>';
267
                })
268
269
                ->add_column('country', function ($row) {
270
                    $cityName = $row->geoip && $row->geoip->city ? ' - '.$row->geoip->city : '';
271
272
                    $countryName = ($row->geoip ? $row->geoip->country_name : '').$cityName;
273
274
                    $countryCode = strtolower($row->geoip ? $row->geoip->country_code : '');
275
276
                    $flag = $countryCode
277
                            ? "<span class=\"f16\"><span class=\"flag $countryCode\" alt=\"$countryName\" /></span></span>"
278
                            : '';
279
280
                    return "$flag $countryName";
281
                })
282
283
                ->add_column('user', function ($row) use ($username_column) {
284
                    return $row->user ? $row->user->$username_column : 'guest';
285
                })
286
287
                ->add_column('device', function ($row) {
288
                    $model = ($row->device && $row->device->model && $row->device->model !== 'unavailable' ? '['.$row->device->model.']' : '');
289
290
                    $platform = ($row->device && $row->device->platform ? ' ['.trim($row->device->platform.' '.$row->device->platform_version).']' : '');
291
292
                    $mobile = ($row->device && $row->device->is_mobile ? ' [mobile device]' : '');
293
294
                    return $model || $platform || $mobile
295
                            ? $row->device->kind.' '.$model.' '.$platform.' '.$mobile
296
                            : '';
297
                })
298
299
                ->add_column('browser', function ($row) {
300
                    return $row->agent && $row->agent
301
                            ? $row->agent->browser.' ('.$row->agent->browser_version.')'
302
                            : '';
303
                })
304
305
                ->add_column('language', function ($row) {
306
                    return $row->language && $row->language
307
                        ? $row->language->preference
308
                        : '';
309
                })
310
311
                ->add_column('referer', function ($row) {
312
                    return $row->referer ? $row->referer->domain->name : '';
313
                })
314
315
                ->add_column('pageViews', function ($row) {
316
                    return $row->page_views;
317
                })
318
319
                ->add_column('lastActivity', function ($row) {
320
                    return $row->updated_at->diffForHumans();
321
                })
322
323
                ->make(true);
324
    }
325
326
    private function isAuthenticated()
327
    {
328
        return $this->authentication->check();
329
    }
330
331
    private function hasAdminProperty()
332
    {
333
        $user = $this->authentication->user();
334
335
        foreach ($this->adminProperties as $property) {
336
            $propertyCamel = Str::camel($property);
337
338
            if (
339
                    isset($user->$property) ||
340
                    isset($user->$propertyCamel) ||
341
                    method_exists($user, $property) ||
342
                    method_exists($user, $propertyCamel)
343
            ) {
344
                return true;
345
            }
346
        }
347
348
        return false;
349
    }
350
351
    private function isAdmin()
352
    {
353
        $user = $this->authentication->user();
354
355
        foreach ($this->adminProperties as $property) {
356
            $propertyCamel = Str::camel($property);
357
358
            if (
359
                (isset($user->$property) && $user->$property) ||
360
                (isset($user->$propertyCamel) && $user->$propertyCamel) ||
361
                (method_exists($user, $property) && $user->$property()) ||
362
                (method_exists($user, $propertyCamel) && $user->$propertyCamel())
363
            ) {
364
                return true;
365
            }
366
        }
367
368
        return false;
369
    }
370
}
371