This repository was archived by the owner on May 18, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBaseController.php
More file actions
150 lines (130 loc) · 3.56 KB
/
Copy pathBaseController.php
File metadata and controls
150 lines (130 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace App\Controllers;
use App\Libraries\GitHubHelper;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\Response;
use Config\App;
use Config\Services;
use Psr\Log\LoggerInterface;
/**
* Class BaseController
*
* BaseController provides a convenient place for loading components
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
* class Home extends BaseController
*
* For security be sure to declare any new methods as protected or private.
*/
class BaseController extends Controller
{
/**
* @var App
*/
protected $config;
/**
* @var array
*/
protected $errors;
/**
* Parameters for view components
*
* @var array
*/
protected $data = [];
/**
* Identifier for our content
*
* @var mixed
*/
protected $id;
/**
* @var string|null
*/
protected $realUrl;
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = [];
/**
* @var GitHubHelper
*/
protected $gitter;
/**
* Initialization.
*
*/
public function __construct()
{
$this->config = config('App');
$this->data['mybb_forum_url'] = $this->config->mybbForumURL;
$this->errors = [];
$this->gitter = new GitHubHelper();
}
/**
* Renders this page
*
* @param string $view The view file to render
*/
protected function render(string $view)
{
// URL without the locale
try
{
$this->realUrl = trim('/' . $this->request->uri->getSegment(2) . '/' . $this->request->uri->getSegment(3), '/ ');
}
catch (HTTPException $e)
{
$this->realUrl = null;
}
if (empty($this->realUrl))
$this->realUrl = 'home';
$this->buildNavbars();
$data = $this->data;
if ( ! isset($data['pagetitle']))
{
$data['pagetitle'] = $data['title'];
}
$this->response->noCache();
// Prevent some security threats, per Kevin
// Turn on IE8-IE9 XSS prevention tools
$this->response->setHeader('X-XSS-Protection', '1; mode=block');
// Don't allow any pages to be framed - Defends against CSRF
$this->response->setHeader('X-Frame-Options', 'DENY');
// prevent mime based attacks
$this->response->setHeader('X-Content-Type-Options', 'nosniff');
// finally, assemble the browser page!
echo view($view, $data);
}
/**
* Builds the localized top & bottom navbars
*/
private function buildNavbars(): void
{
// Massage the menubar
$choices = $this->config->menuChoices;
foreach ($choices['menudata'] as &$menuitem)
{
$menuitem['active'] = (ltrim($menuitem['link'], '/ ') == $this->realUrl) ? 'active' : '';
$menuitem['link'] = $menuitem['link'];
$menuitem['name'] = lang('Site.' . $menuitem['name']); // localize
}
$this->data['menubar'] = view('theme/menubar', $choices);
// Massage the footer menu
$choices = $this->config->footerChoices;
foreach ($choices['menudata'] as &$menuitem)
{
$menuitem['active'] = (ltrim($menuitem['link'], '/ ') == $this->realUrl) ? 'active' : '';
$menuitem['link'] = $menuitem['link'];
$menuitem['name'] = lang('Site.' . $menuitem['name']); // localize
}
$this->data['footerbar'] = view('theme/footerbar', $choices);
}
}