forked from Cotonti/Cotonti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
151 lines (140 loc) · 3.89 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
<?php
/**
* Index loader
*
* @package Cotonti
* @copyright (c) Cotonti Team
* @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
*/
if (php_sapi_name() == 'cli-server') {
// Embedded PHP webserver routing
$tmp = explode('?', $_SERVER['REQUEST_URI']);
$REQUEST_FILENAME = mb_substr($tmp[0], 1);
unset($tmp);
if (file_exists($REQUEST_FILENAME) && !preg_match('#\.php$#', $REQUEST_FILENAME)) {
// Transfer static file if exists
return false;
}
// Language selector
$langs = array_map(
function ($dir) { return str_replace("lang/", "", $dir); },
glob('lang/??', GLOB_ONLYDIR)
);
if (preg_match('#^(' . join('|', $langs) . ')/(.*)$#', $REQUEST_FILENAME, $mt)) {
$REQUEST_FILENAME = $mt[2];
$_GET['l'] = $mt[1];
}
// Sitemap shortcut
if ($REQUEST_FILENAME === 'sitemap.xml') {
$_GET['r'] = 'sitemap';
}
// Admin area and message are special scripts
if (preg_match('#^admin/([a-z0-9]+)#', $REQUEST_FILENAME, $mt)) {
$_GET['m'] = $mt[1];
include 'admin.php';
exit;
}
if (preg_match('#^(admin|login|message)(/|$)#', $REQUEST_FILENAME, $mt)) {
include $mt[1].'.php';
exit;
}
// PHP files have priority
if (preg_match('#\.php$#', $REQUEST_FILENAME) && $REQUEST_FILENAME !== 'index.php') {
include $REQUEST_FILENAME;
exit;
}
// All the rest goes through standard rewrite gateway
if ($REQUEST_FILENAME !== 'index.php') {
$_GET['rwr'] = $REQUEST_FILENAME;
}
unset($REQUEST_FILENAME, $langs, $mt);
}
// Redirect to install if config is missing
if (!file_exists('./datas/config.php')) {
if (file_exists('install.php')) {
header('Location: install.php');
exit;
}
$message_body = '<p><em>'.@date('Y-m-d H:i').'</em></p>';
$message_body .= "<p>File 'install.php' not found</p>";
echo $message_body;
http_response_code(500);
exit (1);
}
// Let the include files know that we are Cotonti
const COT_CODE = true;
// Load vital core configuration from file
require_once './datas/config.php';
// If it is a new install, redirect
if (isset($cfg['new_install']) && $cfg['new_install']) {
header('Location: install.php');
exit;
}
// Load the Core API
require_once $cfg['system_dir'] . '/functions.php';
// Bootstrap
require_once $cfg['system_dir'] . '/common.php';
$ext = isset($_GET['e']) ? cot_import('e', 'G', 'ALP') : false;
$ajax = cot_import('r', 'G', 'ALP');
$popup = cot_import('o', 'G', 'ALP');
if (!$ext) {
// Support for ajax and popup hooked plugins
$ext = $ajax ? $ajax : ($popup ? $popup : $ext);
}
unset ($ajax, $popup);
// Detect selected extension
if ($ext === false) {
// Default environment for index module
define('COT_MODULE', true);
$env['type'] = 'module';
$env['ext'] = 'index';
} else {
$found = false;
if (preg_match('`^\w+$`', $ext)) {
$module_found = false;
$plugin_found = false;
if (file_exists($cfg['modules_dir'] . '/' . $ext) && isset($cot_modules[$ext])) {
$module_found = true;
$found = true;
}
if (file_exists($cfg['plugins_dir'] . '/' . $ext)) {
$plugin_found = true;
$found = true;
}
if ($module_found && $plugin_found) {
// Need to query the db to check which one is installed
$res = $db->query("SELECT ct_plug FROM $db_core WHERE ct_code = ? LIMIT 1", $ext);
if ($res->rowCount() == 1) {
if ((int) $res->fetchColumn()) {
$module_found = false;
} else {
$plugin_found = false;
}
} else {
$found = false;
}
}
if ($module_found) {
$env['type'] = 'module';
define('COT_MODULE', true);
} elseif ($plugin_found) {
$env['type'] = 'plug';
$env['location'] = 'plugins';
define('COT_PLUG', true);
}
}
if ($found) {
$env['ext'] = $ext;
} else {
// Error page
cot_die_message(404);
exit;
}
}
unset($ext);
// Load the requested extension
if (Cot::$env['type'] == 'plug') {
require_once Cot::$cfg['system_dir'] . '/plugin.php';
} else {
require_once Cot::$cfg['modules_dir'] . '/' . Cot::$env['ext'] . '/' . Cot::$env['ext'] . '.php';
}