Project

General

Profile

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