This repository has been archived by the owner on Jan 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
258 lines (216 loc) · 7.01 KB
/
index.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
## -------------------------------------------------------
# Klasemate
## -------------------------------------------------------
# Created by Laurensius Jeffrey Chandra
# http://klasemate.arcestia.id
#
# File: index.php
# License: GPLv2
# Copyright: (c) 2016 - Klasemate
## -------------------------------------------------------
// Run initialization file before loading Main()
require("init.php");
class Main
{
// Controller, Action and ID
protected $controller = "";
protected $action = "";
protected $id = "";
// Current theme, template and language
private $theme;
private $template;
private $language;
// Instances of non-static Kernel classes
private $Core;
private $Db;
private $Session;
// Configurations
private $Config = array();
// Controller instance
private $instance;
// Page content
private $content;
private $head;
// Defined variables inside controller
private $view_data;
/**
* --------------------------------------------------------------------
* MAIN CLASS CONSTRUCTOR
* --------------------------------------------------------------------
*/
public function __construct()
{
// Load kernel classes
$this->_LoadKernel();
// Load configuration file
if(is_file("config.php")) {
require_once("config.php");
}
else {
Html::Error("Configuration file is missing.");
}
// If config.php is empty, go to Klasemate installer
if(filesize("config.php") == 0 || empty($config)) {
header("Location: install/");
exit;
}
// Instance of Database() class
$this->Db = new Database();
$this->Db->Connect($config);
// Get query strings from URL
$this->controller = strtolower(Http::Request("c"));
$this->action = strtolower(Http::Request("act"));
$this->id = strtolower(Http::Request("id"));
// If there isn't any controller defined
if(!$this->controller) {
$this->controller = "community";
}
// Initialize Session() class
$this->Session = new Session($this->Db, $this->controller);
$this->Session->UpdateSession();
// Get settings from database
$this->_GetConfig();
// Get current template and language
$this->_GetTemplate();
$this->_GetLanguage();
// Instantiate class Core()
$this->Core = new Core($this->Db, $this->Config, $this->Session->member_info);
// OK, let's go...
$this->_LoadController($this->controller, $this->action);
$this->_LoadView($this->controller, $this->action);
}
/**
* --------------------------------------------------------------------
* LOAD MAIN KERNEL/CORE MODULES
* --------------------------------------------------------------------
*/
private function _LoadKernel()
{
foreach(scandir("kernel") as $filename) {
if(substr_count($filename, ".php") < 1) {
continue; // Ignore non-PHP files
}
require("kernel/" . $filename);
}
}
/**
* --------------------------------------------------------------------
* LOAD CONTROLLER AND FIRST METHOD
* --------------------------------------------------------------------
*/
private function _LoadController($controller, $action = "")
{
// Controllers names are in UpperCamelCase, but URLs in lowercase
$controller = $this->controller = ucwords($controller);
$action = ($action != "") ? Text::FormatActionName($this->action) : $this->action = "Main";
// Load Application controller
require("controllers/Application.php");
require("controllers/" . $controller . ".php");
$this->instance = new $controller();
// Create an instance of non-static Kernel classes in Application controller
$this->instance->Db = $this->Db;
$this->instance->Core = $this->Core;
$this->instance->Session = $this->Session;
// Execute Controller::_BeforeAction() method
if(method_exists($this->instance, "_BeforeAction")) {
$this->instance->_BeforeAction($this->id);
}
// Execute Controller with the provided action method
$this->instance->Run();
$this->instance->$action($this->id);
// Execute Controller::_AfterAction() method
if(method_exists($this->instance, "_AfterAction")) {
$this->instance->_AfterAction($this->id);
}
// Get defined variables
$this->view_data = $this->instance->Get();
}
/**
* --------------------------------------------------------------------
* LOAD MASTER PAGE AND VIEW
* --------------------------------------------------------------------
*/
private function _LoadView($controller, $action)
{
if($this->instance->HasLayout()) {
$page_info = array();
// Get defined variables in controller
foreach($this->view_data as $k => $v) {
$$k = $v;
}
$breadcrumb = $this->Core->Breadcrumb($page_info);
$page_title = $this->Core->PageTitle($page_info);
// Load page content
ob_start();
require("templates/" . $this->template . "/" . $this->controller . "." . Text::FormatActionName($this->action) . ".phtml");
$this->content = ob_get_clean();
// Load master page
require("templates/" . $this->template . "/" . $this->instance->master . ".phtml");
}
}
/**
* --------------------------------------------------------------------
* GET CONFIGURATIONS FROM DATABASE
* --------------------------------------------------------------------
*/
public function _GetConfig()
{
$this->Db->Query("SELECT * FROM c_config;");
$this->Config = $this->Db->FetchConfig();
}
/**
* --------------------------------------------------------------------
* GET DEFAULT TEMPLATE OR, IF LOGGED IN, THE ONE DEFINED BY MEMBER
* --------------------------------------------------------------------
*/
private function _GetTemplate()
{
if($this->Session->session_info['member_id']) {
// Get member-defined theme
$this->theme = $this->Session->member_info['theme'];
$this->Config['theme'] = $this->theme;
$this->template = $this->Session->member_info['template'];
$this->Config['template'] = $this->template;
}
else {
// Get default theme set
$this->theme = $this->Config['theme_default_set'];
$this->Config['theme'] = $this->theme;
$this->template = $this->Config['template_default_set'];
$this->Config['template'] = $this->template;
}
}
/**
* --------------------------------------------------------------------
* GET DEFUALT LANGUAGE OR, IF LOGGED IN, THE ONE DEFINED BY MEMBER
* --------------------------------------------------------------------
*/
private function _GetLanguage()
{
// Get default or member set language
if($this->Session->session_info['member_id']) {
$this->language = $this->Session->member_info['language'];
}
else {
$this->language = $this->Config['language_default_set'];
}
// Store selected language in $this->Config
$this->Config['language'] = $this->language;
// Load language files
@include("languages/" . $this->language . "/default.php");
@include("languages/" . $this->language . "/" . $this->controller . ".php");
// Populate dictionary array
if(@is_array($t)) {
foreach($t as $k => $v) {
i18n::$dictionary[$k] = $v;
}
}
else {
echo Html::Notification(
"Language files or keywords are missing for <b>" . $this->language . "</b>.", "failure", true
);
}
}
}
new Main();