Completed
Push — dev ( e34862...928524 )
by Darko
08:39
created

BasePageController::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace App\Http\Controllers;
4
5
require_once NN_LIB.'utility/SmartyUtils.php';
6
7
use App\Models\User;
8
use Blacklight\SABnzbd;
9
use App\Models\Category;
10
use App\Models\Settings;
11
use Blacklight\Contents;
12
use App\Models\Forumpost;
13
use Illuminate\Support\Arr;
14
use App\Events\UserLoggedIn;
15
use Illuminate\Support\Facades\Auth;
16
use Illuminate\Pagination\LengthAwarePaginator;
17
18
class BasePageController extends Controller
19
{
20
    /**
21
     * @var \App\Models\Settings
22
     */
23
    public $settings;
24
25
    /**
26
     * @var string
27
     */
28
    public $title = '';
29
30
    /**
31
     * @var string
32
     */
33
    public $content = '';
34
35
    /**
36
     * @var string
37
     */
38
    public $meta_keywords = '';
39
40
    /**
41
     * @var string
42
     */
43
    public $meta_title = '';
44
45
    /**
46
     * @var string
47
     */
48
    public $meta_description = '';
49
50
    /**
51
     * Current page the user is browsing. ie browse.
52
     *
53
     * @var string
54
     */
55
    public $page = '';
56
57
    /**
58
     * @var string
59
     */
60
    public $page_template = '';
61
62
    /**
63
     * @var User
64
     */
65
    public $userdata;
66
67
    /**
68
     * User's theme.
69
     *
70
     * @var string
71
     */
72
    protected $theme = 'Gentele';
73
74
    /**
75
     * @var \Illuminate\Foundation\Application|mixed
76
     */
77
    public $smarty;
78
79
    /**
80
     * BasePageController constructor.
81
     *
82
     * @throws \Exception
83
     */
84
    public function __construct()
85
    {
86
        $this->middleware(['auth', 'web'])->except('api', 'contact', 'showContactForm', 'callback', 'getNzb', 'terms', 'capabilities', 'movie', 'apiSearch', 'tv', 'details', 'failed', 'showRssDesc', 'fullFeedRss', 'categoryFeedRss', 'cartRss', 'myMoviesRss', 'myShowsRss');
87
        // Buffer settings/DB connection.
88
        $this->settings = new Settings();
89
        $this->smarty = app('smarty.view');
90
91
        foreach (Arr::get(config('ytake-laravel-smarty'), 'plugins_paths', []) as $plugins) {
92
            $this->smarty->addPluginsDir($plugins);
93
        }
94
        $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
0 ignored issues
show
Bug Best Practice introduced by
The property error_reporting does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
95
96
        $this->smarty->assign('serverroot', url('/'));
97
    }
98
99
    /**
100
     * @param $query
101
     * @param $totalCount
102
     * @param $items
103
     * @param $page
104
     * @param $path
105
     * @param $reqQuery
106
     *
107
     * @return \Illuminate\Pagination\LengthAwarePaginator
108
     */
109
    public function paginate($query, $totalCount, $items, $page, $path, $reqQuery): LengthAwarePaginator
110
    {
111
        return new LengthAwarePaginator($query, $totalCount, $items, $page, ['path' => $path, 'query' => $reqQuery]);
112
    }
113
114
    /**
115
     * @throws \Exception
116
     */
117
    protected function setPrefs(): void
118
    {
119
        if (Auth::check()) {
120
            $this->userdata = User::find(Auth::id());
121
            $this->setUserPreferences();
122
            if ($this->theme === 'None') {
123
                $this->theme = Settings::settingValue('site.main.style');
124
            }
125
        } else {
126
            $this->theme = Settings::settingValue('site.main.style');
127
            // Tell Smarty which directories to use for templates
128
            $this->smarty->setTemplateDir([
0 ignored issues
show
Bug introduced by
The method setTemplateDir() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
            $this->smarty->/** @scrutinizer ignore-call */ 
129
                           setTemplateDir([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
                'user' => config('ytake-laravel-smarty.template_path').DIRECTORY_SEPARATOR.$this->theme,
130
                'shared' => config('ytake-laravel-smarty.template_path').'/shared',
131
                'default' => config('ytake-laravel-smarty.template_path').'/Gentele',
132
            ]);
133
134
            $this->smarty->assign(
0 ignored issues
show
Bug introduced by
The method assign() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
            $this->smarty->/** @scrutinizer ignore-call */ 
135
                           assign(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
                [
136
                    'isadmin' => false,
137
                    'ismod' => false,
138
                    'loggedin' => false,
139
                ]
140
            );
141
        }
142
143
        $this->smarty->assign(
144
            [
145
                'theme'=> $this->theme,
146
                'site' => $this->settings,
147
            ]
148
        );
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function isPostBack(): bool
155
    {
156
        return \request()->isMethod('POST');
157
    }
158
159
    /**
160
     * Show 404 page.
161
     *
162
     * @param $message
163
     *
164
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
165
     */
166
    public function show404($message = null)
167
    {
168
        if ($message !== null) {
169
            return view('errors.404')->with('Message', $message);
170
        }
171
172
        return view('errors.404');
173
    }
174
175
    /**
176
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
177
     */
178
    public function show403()
179
    {
180
        return view('errors.403');
181
    }
182
183
    /**
184
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
185
     */
186
    public function show503()
187
    {
188
        return view('errors.503')->with('Error', 'Service temporarily unavailable');
189
    }
190
191
    /**
192
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
193
     */
194
    public function showBadBoy()
195
    {
196
        return view('errors.badboy')->with('Message', 'This is not you account.');
197
    }
198
199
    /**
200
     * Show maintenance page.
201
     */
202
    public function showMaintenance()
203
    {
204
        return view('errors.maintenance')->with('Message', 'We are performing an site maintenance.');
205
    }
206
207
    /**
208
     * Show Security token mismatch page.
209
     */
210
    public function showTokenError()
211
    {
212
        return view('errors.tokenError')->with('Error', 'Token mismatch');
213
    }
214
215
    /**
216
     * @param string $retry
217
     */
218
    public function show429($retry = '')
219
    {
220
        abort(429, $retry);
221
    }
222
223
    public function render()
224
    {
225
        $this->smarty->display($this->page_template);
0 ignored issues
show
Bug introduced by
The method display() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
        $this->smarty->/** @scrutinizer ignore-call */ 
226
                       display($this->page_template);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
226
    }
227
228
    /**
229
     * @throws \Exception
230
     */
231
    protected function setUserPreferences(): void
232
    {
233
        $this->userdata->categoryexclusions = User::getCategoryExclusionById(Auth::id());
0 ignored issues
show
Bug introduced by
The property categoryexclusions does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
234
235
        // Change the theme to user's selected theme if they selected one, else use the admin one.
236
        if ((int) Settings::settingValue('site.main.userselstyle') === 1) {
237
            $this->theme = $this->userdata->style ?? 'None';
238
            if ($this->theme === 'None') {
239
                $this->theme = Settings::settingValue('site.main.style');
240
            }
241
        } else {
242
            $this->theme = Settings::settingValue('site.main.style');
243
        }
244
245
        // Update last login every 15 mins.
246
        if (now()->subHours(3) > $this->userdata->lastlogin) {
247
            event(new UserLoggedIn($this->userdata));
248
        }
249
250
        $this->smarty->assign('userdata', $this->userdata);
251
        $this->smarty->assign('loggedin', 'true');
252
253
        if ($this->userdata->nzbvortex_api_key !== '' && $this->userdata->nzbvortex_server_url !== '') {
254
            $this->smarty->assign('weHasVortex', true);
255
        } else {
256
            $this->smarty->assign('weHasVortex', false);
257
        }
258
259
        $sab = new SABnzbd($this);
260
        $this->smarty->assign('sabintegrated', $sab->integratedBool);
261
        if ($sab->integratedBool && $sab->url !== '' && $sab->apikey !== '') {
262
            $this->smarty->assign('sabapikeytype', $sab->apikeytype);
263
        }
264
        if ($this->userdata->hasRole('Admin')) {
265
            $this->smarty->assign('isadmin', 'true');
266
        }
267
268
        if ($this->userdata->hasRole('Moderator')) {
269
            $this->smarty->assign('ismod', 'true');
270
        }
271
272
        // Tell Smarty which directories to use for templates
273
        $this->smarty->setTemplateDir([
274
            'user' => config('ytake-laravel-smarty.template_path').DIRECTORY_SEPARATOR.$this->theme,
275
            'shared' => config('ytake-laravel-smarty.template_path').'/shared',
276
            'default' => config('ytake-laravel-smarty.template_path').'/Gentele',
277
        ]);
278
279
        $role = User::ROLE_USER;
280
        if (! empty($this->userdata)) {
281
            $role = $this->userdata->roles_id;
282
        }
283
284
        $content = new Contents();
285
        $this->smarty->assign('usefulcontentlist', $content->getForMenuByTypeAndRole(Contents::TYPEUSEFUL, $role));
286
        $this->smarty->assign('articlecontentlist', $content->getForMenuByTypeAndRole(Contents::TYPEARTICLE, $role));
287
        if ($this->userdata !== null) {
288
            $this->smarty->assign('recentforumpostslist', Forumpost::getPosts(Settings::settingValue('..showrecentforumposts')));
289
        }
290
291
        $parentcatlist = Category::getForMenu($this->userdata->categoryexclusions);
292
293
        $this->smarty->assign('parentcatlist', $parentcatlist);
294
        $this->smarty->assign('catClass', Category::class);
295
296
        if (\request()->has('t')) {
297
            $this->smarty->assign('header_menu_cat', \request()->input('t'));
298
        } else {
299
            $this->smarty->assign('header_menu_cat', '');
300
        }
301
        $header_menu = $this->smarty->fetch('headermenu.tpl');
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

301
        /** @scrutinizer ignore-call */ 
302
        $header_menu = $this->smarty->fetch('headermenu.tpl');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
302
        $this->smarty->assign('header_menu', $header_menu);
303
    }
304
305
    /**
306
     *  Set admin preferences.
307
     */
308
    public function setAdminPrefs()
309
    {
310
        // Tell Smarty which directories to use for templates
311
        $this->smarty->setTemplateDir(
312
            [
313
                'admin'    => config('ytake-laravel-smarty.template_path').'/admin',
314
                'shared'    => config('ytake-laravel-smarty.template_path').'/shared',
315
                'default'    => config('ytake-laravel-smarty.template_path').'/admin',
316
            ]
317
        );
318
319
        $this->smarty->assign('catClass', Category::class);
320
    }
321
322
    /**
323
     * Output the page.
324
     */
325
    public function pagerender(): void
326
    {
327
        $this->page_template = 'basepage.tpl';
328
329
        $this->render();
330
    }
331
332
    /**
333
     * Output a page using the admin template.
334
     *
335
     * @throws \Exception
336
     */
337
    public function adminrender(): void
338
    {
339
        $admin_menu = $this->smarty->fetch('adminmenu.tpl');
340
        $this->smarty->assign('admin_menu', $admin_menu);
341
342
        $this->page_template = 'baseadminpage.tpl';
343
344
        $this->render();
345
    }
346
347
    /**
348
     * @throws \Exception
349
     */
350
    public function adminBasePage(): void
351
    {
352
        $this->setAdminPrefs();
353
        $this->smarty->assign([
354
            'meta_title' => 'Admin Home',
355
            'meta_description' => 'Admin home page',
356
        ]);
357
        $this->adminrender();
358
    }
359
}
360