-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.php
384 lines (290 loc) · 11.2 KB
/
core.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?
header('Content-type: text/html; charset=utf-8');
define("CORE_LOADED",true);
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
require "default.conf.php";
require "conf.inc.php";
require "inc/auth.php";
require "inc/functions.php";
$lang = Array();
require "lang/{$cfg['interfacelanguage']}.php";
require "inc/html.php";
/**** Basic Checks ****/
$cfg['path'] = strlen($cfg['path']) ? $cfg['path'] : $cfg['default_path'];
$cfg['domain'] = strlen($cfg['domain']) ? $cfg['domain'] : $cfg['default_domain'];
$cfg['domain'] .= ($cfg['path'][0] == '/') ? substr($cfg['path'],1) : $cfg['path'];
if (! file_exists('styles/' . STYLE . '/templates/index.tpl')) {
exit("File 'styles/" . STYLE . "/templates/index.tpl' not found. Missing or mistyped style name?");
}
/* Do not allow tampering with Session/Cookie variables */
if (isset($_REQUEST['_SESSION']) || isset($_REQUEST['_COOKIE'])) {
die("Get lost Muppet!");
}
/**** Language specific initialisations ****/
require "inc/lng_init.php";
// Main HTML output handler
$anego = new Anego(STYLE);
$anego->error_reporting = E_ALL & ~E_NOTICE;
$anego->assign('language', $cfg['interfacelanguage']);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
$anego->assign('browser', 'ie');
} else {
$anego->assign('browser', 'non-ie');
}
// Todo: Some of these icons are not needed anymore
$defIcons = array(
'add' => $cfg['path'] . 'styles/default/img/add.png',
'del' => $cfg['path'] . 'styles/default/img/delBig.png',
'drag' => $cfg['path'] . 'styles/default/img/drag.png',
'linkpic' => $cfg['path'] . 'styles/default/img/linkPic.png',
'folder' => $cfg['path'] . 'styles/default/img/folder.png',
'file' => $cfg['path'] . 'styles/default/img/file.png',
'loading' => $cfg['path'] . 'styles/default/img/progress_active.gif',
'edit' => $cfg['path'] . 'styles/default/img/pencil.png',
'editB' => $cfg['path'] . 'styles/default/img/pencilB.png',
'delB' => $cfg['path'] . 'styles/default/img/del.png'
);
// Tells the loader to load all default javascript files
$anego->AddJsModule('de');
// Loads language related js files
$anego->AddJsModule('ad'.$cfg['interfacelanguage']);
// Settings required by js files
$anego->AddJsPreload("\tanego=new Object();");
$anego->AddJsPreload("\tanego.language='{$cfg['interfacelanguage']}';");
$anego->AddJsPreload("\tanego.style='".STYLE."';");
$anego->AddJsPreload("\tanego.fancyURLs=".($cfg['fancyURLs']?'1':'0').";");
$anego->AddJsPreload("\tanego.submenuStyle='".$cfg['submenuStyle']."';");
$anego->AddJsPreload("\tanego.animatePageLoad=".$cfg['ajaxloadFadeTimer'].";");
$anego->AddJsPreload("\tanego.pageLoad='".$cfg['pageLoad']."';");
$anego->AddJsPreload("\tanego.path='" . $cfg['path'] . "';");
$anego->assign('anegopath', $cfg['path']);
$anego->assign('submenustyle', $cfg['submenuStyle']);
$anego->assign("cfg", $cfg['fancyURLs']);
/***** Init database, etc. *****/
$customheader="";
define("VIS_USER",1);
define("VIS_INMENU",2);
define('MENU_MAIN','MAIN');
define('MENU_MINOR','MINOR');
/**** Init MySQL ****/
require "inc/db_init.php";
/***** Settings *****/
$s = array();
// Todo: Reduce to the needed settings (dont retrieve all)
$q = "SELECT * FROM ".SETTINGS;
$res = mysqli_query($sql_link, $q) or
BailSQLn(__('A database query failed.'),$q);
while($row = mysqli_fetch_array($res)) {
$settings[$row['name']] = $row;
}
if (!isset($settings['pagetitle'])) {
$settings['pagetitle'] = array('value' => 'Anego CMS');
}
function getSetting($name, $full = false) {
global $settings;
if ($full) {
return isset($settings[$name]) ? $settings[$name] : null;
}
return isset($settings[$name]) ? $settings[$name]['value'] : null;
}
function setSetting($name, $value) {
global $sql_link;
$q = 'REPLACE INTO '.SETTINGS.' (name,value) VALUES (\'' . $name . '\', \'' . $value . '\')';
mysqli_query($sql_link, $q) or
BailErr('Failed applying setings', $q);
}
/*** More Frontend Setup code ***/
$anego->AddJsPreload("\tanego.homepage=" . HomePage() . ';');
if (getSetting('autoeditmode') && LOGINOK) {
$anego->AddJsPreload("\tanego.editmode=1;");
}
if (getSetting('keywords')) {
$anego->AddHeadHeader("\t" . '<meta name="keywords" content="' . htmlentities(utf8_decode(getSetting('keywords'))).'">');
}
if (getSetting('description')) {
$anego->AddHeadHeader("\t" . '<meta name="description" content="' . htmlentities(utf8_decode(str_replace("\n",' ',getSetting('description')))).'">');
}
$anego->assign('pagetitle', str_replace(array('<','>'),array('<','>'), getSetting('pagetitle')));
$anego->assign('loginok', LOGINOK);
$anego->assign('editablePage', LOGINOK && basename($_SERVER['SCRIPT_NAME']) == 'index.php');
$anego->assign('basepath', $cfg['path']);
/******* Page display *******/
/* Determine which page to show - returns the current page to be shown */
function CurrentPage() {
$p = 0;
// Integer page?
if (intval(@$_GET['p'])) {
$p = intval($_GET['p']);
}
// Named page?
if ( preg_match("/^[\w\d\-]{2,}$/", @$_GET['p'])) {
$p = $_GET['p'];
}
// No particular page? Show startpage
if (! $p) $p = HomePage();
return $p;
}
/* Returns and defines constant HOMEPAGE, which is the first page to be shown when a visitor comes to the site */
function HomePage() {
global $sql_link;
if(defined('HOMEPAGE')) {
return HOMEPAGE;
}
$q = "SELECT value FROM ".SETTINGS." WHERE name='firstpage'";
$res = mysqli_query($sql_link, $q) or
BailSQLn(__('Failed getting settings data'),$q);
list($p) = mysqli_fetch_array($res);
/* No home page set up => lets just take the first page we can find */
if(mysqli_affected_rows($sql_link)==0) {
$q = "SELECT idx FROM ".PAGES." LIMIT 1";
$res = mysqli_query($sql_link, $q) or
BailSQLn(__('Failed getting settings data'),$q);
list($p) = mysqli_fetch_array($res);
// Not even a page available? Damn.
if (mysqli_affected_rows($sql_link) == 0) $p = -1;
}
define('HOMEPAGE',$p);
return intval($p);
}
function getURLFromPage($pageidx) {
global $sql_link;
$q = "SELECT url FROM ".PAGES." WHERE idx=" . intval($pageidx);
$res = mysqli_query($sql_link, $q) or
BailSQLn(__('Failed getting settings data'),$q);
list($url) = mysqli_fetch_array($res);
return $url;
}
/* Admin links */
function AdminBar($p) {
global $cfg,$anego;
$userRole = UserRole();
if(LOGINOK && $userRole>=Role::ProMod) {
if($p!=-1) {
$anego->AddLink("<a href=\"javascript:Core.editPage()\" id=\"pageEditLink\">" . __('Edit page') . "</a>");
}
$pgadurl = $cfg['path'] . ($cfg['fancyURLs'] ? 'admin/pgad' : 'admin.php?a=pgad');
$setgurl = $cfg['path'] . ($cfg['fancyURLs'] ? 'admin/setg' : 'admin.php?a=setg');
$anego->AddLink('<a href="' . $pgadurl . '">' . __('Edit Menu') . '</a>');
if ($userRole>=Role::Admin) {
$anego->AddLink('<a href="' . $setgurl . '">' . __('Settings') . '</a>');
}
}
}
/* Print the page */
function PrintPage($p) {
global $anego, $cfg, $sql_link;
if($p == -1) {
$anego->AddContent(__('<i>No start page set up yet. Please check your settings.</i>'));
AdminBar(-1);
$anego->display('index.tpl');
exit();
}
/********* Get page content ********/
$selection = '';
if (is_numeric($p)) {
$selection = "idx='$p'";
} else {
$selection = "(url='" . mysqli_real_escape_string($sql_link, $p) . "' AND nolink=0 AND file='')";
}
$q = "SELECT idx, name, file, content, content_prepared, content_validuntil FROM ".PAGES." WHERE " . $selection . ' ' . (!LOGINOK?"AND (visibility&1)=1":"");
$res = mysqli_query($sql_link, $q) or
BailSQL("Failed getting page data for page $p<br>",$q);
$page = mysqli_fetch_array($res);
AdminBar($page['idx']);
if(!mysqli_affected_rows($sql_link)) {
$anego->AddContent(__('Page nonexistant or no permission to see it'));
$anego->display('index.tpl');
exit();
}
$anego->curPg = $page['idx'];
$anego->AddJsPreload("\tanego.curPg = 'pages/" . $page['idx'] . "';");
$anego->assign('pagetitle', $page['name'] . " - " . $anego->get_template_vars('pagetitle'));
$anego->assign('pagename', $page['name']);
$pageloadfiles = pageLoadFiles($page['idx']);
if (count($pageloadfiles['js'])) {
$anego->AddJsPreload("\tanego.pageJS=new Array('" . implode("','",$pageloadfiles['js']) . "');");
}
if (count($pageloadfiles['css'])) {
foreach ($pageloadfiles['css'] as $css) {
$anego->AddCSSFile($css);
}
}
if (strlen($page['file']) && file_exists($page['file'])) {
/***** Page is file: include file *****/
include($page['file']);
} else {
/***** Otherwise set up and display page *****/
if (!strlen($page['content'])) {
$page['content']="<i id=\"hasnoContent\">". __('This page has not been filled with content yet. Please use the \'Edit this page\' Link to enter your text') . "</i>";
}
if ((!strlen($page['content_prepared']) && strlen($page['content'])) || $page["content_validuntil"] < time()) {
$page['content_prepared'] = refreshPageCache($page["idx"]);
}
$anego->AddContent($page['content_prepared']);
$anego->assign('currentpage', $p);
$anego->assign('currentpageid', $page['idx']);
if (!$anego->get_template_vars('pageTitle')) {
$anego->assign('pageTitle', $page['name']);
}
$anego->display('index.tpl');
}
}
function refreshPageCache($p) {
include_once('inc/modules.php');
$pmg = new PageManager();
$pmg->loadModules();
// Also updates the DB
return $pmg->generatePage($p);
}
// Returns required module-js and module-css files per page
function pageLoadFiles($p) {
global $cfg, $sql_link;
if (file_exists('var/installed_modules')) {
$modules = unserialize(file_get_contents('var/installed_modules'));
} else {
$modules = array();
}
// Optimize: Save this information in PAGES so we can eliminate this query
$q = 'SELECT module_id FROM '.PAGE_ELEMENT.' WHERE page_id='.$p.' GROUP BY module_id';
$res = mysqli_query($sql_link, $q) or
BailErr("Failed getting page data for page $p<br>",$q);
$js = array();
$modjs = array();
$modcss = array();
$css = array();
while (list($mid) = mysqli_fetch_row($res)) {
// typecast to array to allow non-array values in the module config
if (LOGINOK) {
$modjs = array_merge(@(array)$modules[$mid]['config']['js']['load'],@(array)$modules[$mid]['config']['js']['pageMod']);
} else {
$modjs = array_merge(@(array)$modules[$mid]['config']['js']['load'],@(array)$modules[$mid]['config']['js']['pageView']);
}
$modcss = array_merge(@(array)$modules[$mid]['config']['css']['load'],@(array)$modules[$mid]['config']['css']['pageView']);
foreach($modjs as $idx => $file) {
$js[] = 'modules/'.$mid.'/'.str_replace('%lng', $cfg['interfacelanguage'], $file);
}
foreach($modcss as $idx => $file) {
$css[] = 'modules/'.$mid.'/'.str_replace('%lng', $cfg['interfacelanguage'], $file);
}
}
return array(
"js" => $js,
"css" => $css
);
}
// Returns required module-js files per page when editing
function pageEditJs() {
if(file_exists('var/installed_modules'))
$modules = unserialize(file_get_contents('var/installed_modules'));
else $modules = Array();
$js = Array();
foreach($modules as $mid=>$mod) {
if(!isset($mod['config']['js'])) echo "$mod has no js";
if(is_array($mod['config']['js']['pageEdit'])) {
foreach($mod['config']['js']['pageEdit'] as $file)
$js[]='modules/'.$mid.'/'.$file;
} else
$js[]='modules/'.$mid.'/'.$mod['config']['js']['pageEdit'];
}
return $js;
}