Project

General

Profile

1 1379 Luisehahne
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         account
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id$
14
 * @filesource		$HeadURL$
15
 * @lastmodified    $Date$
16
 *
17
 */
18
19 1496 DarkViper
/* -------------------------------------------------------- */
20
// Must include code to stop this file being accessed directly
21
require_once('globalExceptionHandler.php');
22
if(!defined('WB_PATH')) { throw new IllegalFileException(); }
23
/* -------------------------------------------------------- */
24 1379 Luisehahne
require_once(WB_PATH.'/framework/class.wb.php');
25
26
// Get WB version
27
require_once(ADMIN_PATH.'/interface/version.php');
28
29
// Include EditArea wrapper functions
30 1428 Luisehahne
// require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
31 1462 DarkViper
//require_once(WB_PATH . '/framework/SecureForm.php');
32 1379 Luisehahne
33
34
class admin extends wb {
35
	// Authenticate user then auto print the header
36
	public function __construct($section_name= '##skip##', $section_permission = 'start', $auto_header = true, $auto_auth = true)
37
	{
38
		parent::__construct(SecureForm::BACKEND);
39
	if( $section_name != '##skip##' )
40
	{
41
		global $database, $MESSAGE;
42
		// Specify the current applications name
43
		$this->section_name = $section_name;
44
		$this->section_permission = $section_permission;
45
		// Authenticate the user for this application
46
		if($auto_auth == true)
47
		{
48
			// First check if the user is logged-in
49
			if($this->is_authenticated() == false)
50
			{
51
				header('Location: '.ADMIN_URL.'/login/index.php');
52
				exit(0);
53
			}
54
55
			// Now check if they are allowed in this section
56
			if($this->get_permission($section_permission) == false) {
57
				die($MESSAGE['ADMIN']['INSUFFICIENT_PRIVELLIGES']);
58
			}
59
		}
60
61
		// Check if the backend language is also the selected language. If not, send headers again.
62 1486 DarkViper
		$sql  = 'SELECT `language` FROM `'.TABLE_PREFIX.'users` ';
63
		$sql .= 'WHERE `user_id`='.(int)$this->get_user_id();
64
		$get_user_language = @$database->query($sql);
65 1379 Luisehahne
		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
66
		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
67
		$user_language = substr($user_language[0],0,2);
68
		// obtain the admin folder (e.g. /admin)
69
		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
70
		if((LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
71
			&& strpos($_SERVER['PHP_SELF'],$admin_folder.'/') !== false) {
72
			// check if page_id is set
73
			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
74
			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
75
			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
76
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
77
			} else {
78
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url);
79
			}
80
			exit();
81
		}
82
83
		// Auto header code
84
		if($auto_header == true) {
85
			$this->print_header();
86
		}
87
	}
88
	}
89
90
	// Print the admin header
91
	function print_header($body_tags = '') {
92
		// Get vars from the language file
93
		global $MENU;
94
		global $MESSAGE;
95
		global $TEXT;
96
		// Connect to database and get website title
97
		global $database;
98 1457 Luisehahne
		// $GLOBALS['FTAN'] = $this->getFTAN();
99
		$this->createFTAN();
100 1486 DarkViper
		$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name`=\'website_title\'';
101
		$get_title = $database->query($sql);
102 1379 Luisehahne
		$title = $get_title->fetchRow();
103
		$header_template = new Template(THEME_PATH.'/templates');
104
		$header_template->set_file('page', 'header.htt');
105
		$header_template->set_block('page', 'header_block', 'header');
106
		if(defined('DEFAULT_CHARSET')) {
107
			$charset=DEFAULT_CHARSET;
108
		} else {
109
			$charset='utf-8';
110
		}
111
112
		// work out the URL for the 'View menu' link in the WB backend
113
		// if the page_id is set, show this page otherwise show the root directory of WB
114
		$view_url = WB_URL;
115
		if(isset($_GET['page_id'])) {
116
			// extract page link from the database
117 1486 DarkViper
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
118
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
119
			$result = @$database->query($sql);
120 1379 Luisehahne
			$row = @$result->fetchRow();
121
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
122
		}
123
124
		$header_template->set_var(	array(
125 1402 Luisehahne
							'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
126
							'BODY_TAGS' => $body_tags,
127
							'WEBSITE_TITLE' => ($title['value']),
128
							'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
129
							'CURRENT_USER' => $MESSAGE['START']['CURRENT_USER'],
130
							'DISPLAY_NAME' => $this->get_display_name(),
131
							'CHARSET' => $charset,
132
							'LANGUAGE' => strtolower(LANGUAGE),
133
							'VERSION' => VERSION,
134
							'REVISION' => REVISION,
135
							'WB_URL' => WB_URL,
136
							'ADMIN_URL' => ADMIN_URL,
137
							'THEME_URL' => THEME_URL,
138
							'TITLE_START' => $MENU['START'],
139
							'TITLE_VIEW' => $MENU['VIEW'],
140
							'TITLE_HELP' => $MENU['HELP'],
141
							'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
142
							'URL_VIEW' => $view_url,
143
							'URL_HELP' => 'http://www.websitebaker2.org/',
144
							'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
145
							'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
146
						)
147
					);
148 1379 Luisehahne
149
		// Create the menu
150
		$menu = array(
151
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
152
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
153
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
154
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 0),
155
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
156
					array(ADMIN_URL.'/admintools/index.php', '', $MENU['ADMINTOOLS'], 'admintools', 1),
157
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1)
158
					);
159
		$header_template->set_block('header_block', 'linkBlock', 'link');
160
		foreach($menu AS $menu_item) {
161
			$link = $menu_item[0];
162
			$target = ($menu_item[1] == '') ? '_self' : $menu_item[1];
163
			$title = $menu_item[2];
164
			$permission_title = $menu_item[3];
165
			$required = $menu_item[4];
166
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
167
			if($required == false OR $this->get_link_permission($permission_title)) {
168
				$header_template->set_var('LINK', $link);
169
				$header_template->set_var('TARGET', $target);
170
				// If link is the current section apply a class name
171
				if($permission_title == strtolower($this->section_name)) {
172
					$header_template->set_var('CLASS', $menu_item[3] . ' current');
173
				} else {
174
					$header_template->set_var('CLASS', $menu_item[3]);
175
				}
176
				$header_template->set_var('TITLE', $title);
177
				// Print link
178
				$header_template->parse('link', 'linkBlock', true);
179
			}
180
		}
181
		$header_template->parse('header', 'header_block', false);
182
		$header_template->pparse('output', 'page');
183
	}
184
185
	// Print the admin footer
186 1443 Luisehahne
		function print_footer($activateJsAdmin = false) {
187 1379 Luisehahne
		// include the required file for Javascript admin
188 1443 Luisehahne
		if($activateJsAdmin != false) {
189
			if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
190
				@include_once(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
191
			}
192 1379 Luisehahne
		}
193 1443 Luisehahne
194 1379 Luisehahne
		$footer_template = new Template(THEME_PATH.'/templates');
195
		$footer_template->set_file('page', 'footer.htt');
196
		$footer_template->set_block('page', 'footer_block', 'header');
197
		$footer_template->set_var(array(
198
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
199
						'WB_URL' => WB_URL,
200
						'ADMIN_URL' => ADMIN_URL,
201
						'THEME_URL' => THEME_URL,
202
			 ) );
203
		$footer_template->parse('header', 'footer_block', false);
204
		$footer_template->pparse('output', 'page');
205
	}
206
207
	// Return a system permission
208
	function get_permission($name, $type = 'system') {
209
		// Append to permission type
210
		$type .= '_permissions';
211
		// Check if we have a section to check for
212
		if($name == 'start') {
213
			return true;
214
		} else {
215
			// Set system permissions var
216
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
217
			// Set module permissions var
218
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
219
			// Set template permissions var
220
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
221
			// Return true if system perm = 1
222
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
223
				if($type == 'system_permissions') {
224
					return true;
225
				} else {
226
					return false;
227
				}
228
			} else {
229
				if($type == 'system_permissions') {
230
					return false;
231
				} else {
232
					return true;
233
				}
234
			}
235
		}
236
	}
237 1441 Luisehahne
/*
238 1379 Luisehahne
	function get_user_details($user_id) {
239
		global $database;
240 1441 Luisehahne
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'users` ';
241
		$sql .= 'WHERE `user_id`='.(int)$user_id.' LIMIT 1';
242
		if(($resUser = $database->query($sql))){
243
			if(!($recUser = $resUser->fetchRow())) {
244
				$recUser['display_name'] = 'Unknown';
245
				$recUser['username'] = 'unknown';
246
			}
247 1379 Luisehahne
		}
248 1441 Luisehahne
		return $recUser;
249
	}
250
*/
251
 function get_user_details($user_id) {
252
  global $database;
253
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
254
  $sql  = 'SELECT `username`,`display_name`,`email` ';
255
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
256 1486 DarkViper
  $sql .= 'WHERE `user_id`='.(int)$user_id;
257 1441 Luisehahne
  if( ($resUsers = $database->query($sql)) ) {
258
   if( ($recUser = $resUsers->fetchRow()) ) {
259
    $retval = $recUser;
260
   }
261
  }
262
  return $retval;
263
 }
264
265
    //
266
	function get_section_details( $section_id, $backLink = 'index.php' ) {
267
	global $database, $TEXT;
268
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'sections` ';
269 1486 DarkViper
		$sql .= 'WHERE `section_id`='.intval($section_id);
270 1441 Luisehahne
		if(($resSection = $database->query($sql))){
271
			if(!($recSection = $resSection->fetchRow())) {
272
				$this->print_header();
273
				$this->print_error($TEXT['SECTION'].' '.$TEXT['NOT_FOUND'], $backLink, true);
274
			}
275
			} else {
276
				$this->print_header();
277
				$this->print_error($database->get_error(), $backLink, true);
278
			}
279
		return $recSection;
280
	}
281
282
	function get_page_details( $page_id, $backLink = 'index.php' ) {
283 1486 DarkViper
		global $database, $TEXT;
284
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'pages` ';
285
		$sql .= 'WHERE `page_id`='.intval($page_id);
286
		if(($resPages = $database->query($sql))){
287
			if(!($recPage = $resPages->fetchRow())) {
288
			$this->print_header();
289
			$this->print_error($TEXT['PAGE'].' '.$TEXT['NOT_FOUND'], $backLink, true);
290
			}
291 1441 Luisehahne
		} else {
292 1486 DarkViper
			$this->print_header();
293
			$this->print_error($database->get_error(), $backLink, true);
294 1379 Luisehahne
		}
295 1486 DarkViper
		return $recPage;
296 1379 Luisehahne
	}
297
298 1441 Luisehahne
	function get_page_permission($page,$action='admin') {
299
		if($action != 'viewing') { $action = 'admin'; }
300
		$action_groups = $action.'_groups';
301
		$action_users  = $action.'_users';
302
		$groups = $users = '0';
303
		if(is_array($page)) {
304
			$groups = $page[$action_groups];
305
			$users  = $page[$action_users];
306
		} else {
307
			global $database;
308
			$sql  = 'SELECT `'.$action_groups.'`,`'.$action_users.'` ';
309
			$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
310
			$sql .= 'WHERE `page_id`='.(int)$page;
311
			if( ($res = $database->query($sql)) ) {
312
				if( ($rec = $res->fetchRow()) ) {
313
					$groups = $rec[$action_groups];
314
					$users  = $rec[$action_users];
315
				}
316
			}
317
		}
318
		return ($this->ami_group_member($groups) || $this->is_group_match($this->get_user_id(), $users));
319
	}
320
321 1379 Luisehahne
	// Returns a system permission for a menu link
322
	function get_link_permission($title) {
323
		$title = str_replace('_blank', '', $title);
324
		$title = strtolower($title);
325
		// Set system permissions var
326
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
327
		// Set module permissions var
328
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
329
		if($title == 'start') {
330
			return true;
331
		} else {
332
			// Return true if system perm = 1
333
			if(is_numeric(array_search($title, $system_permissions))) {
334
				return true;
335
			} else {
336
				return false;
337
			}
338
		}
339
	}
340
341
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
342
	function register_backend_modfiles_body($file_id="js")
343
		{
344
		// sanity check of parameter passed to the function
345
		$file_id = strtolower($file_id);
346
		if($file_id !== "javascript" && $file_id !== "js")
347
		{
348
			return;
349
		}
350
		global $database;
351
        $body_links = "";
352
		// define default baselink and filename for optional module javascript and stylesheet files
353
		if($file_id == "js") {
354 1441 Luisehahne
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
355 1379 Luisehahne
			$base_file = "backend_body.js";
356
		}
357
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
358
		if(isset($_GET['tool']))
359
			{
360
			// check if displayed page contains a installed admin tool
361 1486 DarkViper
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
362
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
363
			$result = $database->query($sql);
364 1379 Luisehahne
			if($result->numRows())
365
				{
366
				// check if admin tool directory contains a backend_body.js file to include
367
				$tool = $result->fetchRow();
368
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
369
				{
370
					// return link to the backend_body.js file
371
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
372
				}
373
			}
374
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
375
		{
376
			// check if displayed page in the backend contains a page module
377
			if (isset($_GET['page_id']))
378
			{
379
				$page_id = (int) addslashes($_GET['page_id']);
380
			} else {
381
				$page_id = (int) addslashes($_POST['page_id']);
382
			}
383
			// gather information for all models embedded on actual page
384 1486 DarkViper
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
385
			$query_modules = $database->query($sql);
386 1379 Luisehahne
			while($row = $query_modules->fetchRow()) {
387
				// check if page module directory contains a backend_body.js file
388
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
389
					// create link with backend_body.js source for the current module
390
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
391
					// ensure that backend_body.js is only added once per module type
392
					if(strpos($body_links, $tmp_link) === false) {
393
						$body_links .= $tmp_link ."\n";
394
					}
395
				}
396
			}
397
			// write out links with all external module javascript/CSS files, remove last line feed
398
			return rtrim($body_links);
399
		}
400
	}
401
402
403
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
404
	function register_backend_modfiles($file_id="css") {
405
		// sanity check of parameter passed to the function
406
		$file_id = strtolower($file_id);
407
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
408
			return;
409
		}
410
411
		global $database;
412
		// define default baselink and filename for optional module javascript and stylesheet files
413
		$head_links = "";
414
		if($file_id == "css") {
415
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
416
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
417
			$base_file = "backend.css";
418
		} else {
419 1441 Luisehahne
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
420 1379 Luisehahne
			$base_file = "backend.js";
421
		}
422
423
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
424
		if(isset($_GET['tool'])) {
425
			// check if displayed page contains a installed admin tool
426 1486 DarkViper
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
427
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
428
			$result = $database->query($sql);
429 1379 Luisehahne
			if($result->numRows()) {
430
				// check if admin tool directory contains a backend.js or backend.css file to include
431
				$tool = $result->fetchRow();
432
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
433
        			// return link to the backend.js or backend.css file
434
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
435
				}
436
			}
437 1441 Luisehahne
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
438 1379 Luisehahne
			// check if displayed page in the backend contains a page module
439
			if (isset($_GET['page_id'])) {
440
				$page_id = (int)$_GET['page_id'];
441
			} else {
442
				$page_id = (int)$_POST['page_id'];
443
			}
444
445
    		// gather information for all models embedded on actual page
446 1486 DarkViper
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
447
			$query_modules = $database->query($sql);
448 1379 Luisehahne
449
    		while($row = $query_modules->fetchRow()) {
450
				// check if page module directory contains a backend.js or backend.css file
451
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
452
					// create link with backend.js or backend.css source for the current module
453
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
454
        			// ensure that backend.js or backend.css is only added once per module type
455
        			if(strpos($head_links, $tmp_link) === false) {
456
						$head_links .= $tmp_link ."\n";
457
					}
458
				}
459
    		}
460
    		// write out links with all external module javascript/CSS files, remove last line feed
461
			return rtrim($head_links);
462
		}
463
	}
464
}
465
466 1412 DarkViper
?>