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 1625 Luisehahne
		$header_template = new Template(dirname($this->correct_theme_source('header.htt')));
105 1379 Luisehahne
		$header_template->set_file('page', 'header.htt');
106
		$header_template->set_block('page', 'header_block', 'header');
107
		if(defined('DEFAULT_CHARSET')) {
108
			$charset=DEFAULT_CHARSET;
109
		} else {
110
			$charset='utf-8';
111
		}
112
113
		// work out the URL for the 'View menu' link in the WB backend
114
		// if the page_id is set, show this page otherwise show the root directory of WB
115
		$view_url = WB_URL;
116
		if(isset($_GET['page_id'])) {
117
			// extract page link from the database
118 1486 DarkViper
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
119
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
120
			$result = @$database->query($sql);
121 1379 Luisehahne
			$row = @$result->fetchRow();
122
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
123
		}
124
125
		$header_template->set_var(	array(
126 1402 Luisehahne
							'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
127
							'BODY_TAGS' => $body_tags,
128
							'WEBSITE_TITLE' => ($title['value']),
129
							'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
130
							'CURRENT_USER' => $MESSAGE['START']['CURRENT_USER'],
131
							'DISPLAY_NAME' => $this->get_display_name(),
132
							'CHARSET' => $charset,
133
							'LANGUAGE' => strtolower(LANGUAGE),
134
							'VERSION' => VERSION,
135 1532 Luisehahne
							'SP' => (defined('SP') ? SP : ''),
136 1402 Luisehahne
							'REVISION' => REVISION,
137 1563 Luisehahne
							'SERVER_ADDR' => ((int)$this->get_user_id()==1 ? $_SERVER['SERVER_ADDR'] : ''),
138 1402 Luisehahne
							'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 1625 Luisehahne
		$footer_template = new Template(dirname($this->correct_theme_source('footer.htt')));
199 1379 Luisehahne
		$footer_template->set_file('page', 'footer.htt');
200
		$footer_template->set_block('page', 'footer_block', 'header');
201
		$footer_template->set_var(array(
202
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
203
						'WB_URL' => WB_URL,
204
						'ADMIN_URL' => ADMIN_URL,
205
						'THEME_URL' => THEME_URL,
206
			 ) );
207
		$footer_template->parse('header', 'footer_block', false);
208
		$footer_template->pparse('output', 'page');
209
	}
210
211
	// Return a system permission
212
	function get_permission($name, $type = 'system') {
213
		// Append to permission type
214
		$type .= '_permissions';
215
		// Check if we have a section to check for
216
		if($name == 'start') {
217
			return true;
218
		} else {
219
			// Set system permissions var
220
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
221
			// Set module permissions var
222
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
223
			// Set template permissions var
224
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
225
			// Return true if system perm = 1
226
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
227
				if($type == 'system_permissions') {
228
					return true;
229
				} else {
230
					return false;
231
				}
232
			} else {
233
				if($type == 'system_permissions') {
234
					return false;
235
				} else {
236
					return true;
237
				}
238
			}
239
		}
240
	}
241 1441 Luisehahne
/*
242 1379 Luisehahne
	function get_user_details($user_id) {
243
		global $database;
244 1441 Luisehahne
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'users` ';
245
		$sql .= 'WHERE `user_id`='.(int)$user_id.' LIMIT 1';
246
		if(($resUser = $database->query($sql))){
247
			if(!($recUser = $resUser->fetchRow())) {
248
				$recUser['display_name'] = 'Unknown';
249
				$recUser['username'] = 'unknown';
250
			}
251 1379 Luisehahne
		}
252 1441 Luisehahne
		return $recUser;
253
	}
254
*/
255
 function get_user_details($user_id) {
256
  global $database;
257
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
258
  $sql  = 'SELECT `username`,`display_name`,`email` ';
259
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
260 1486 DarkViper
  $sql .= 'WHERE `user_id`='.(int)$user_id;
261 1441 Luisehahne
  if( ($resUsers = $database->query($sql)) ) {
262
   if( ($recUser = $resUsers->fetchRow()) ) {
263
    $retval = $recUser;
264
   }
265
  }
266
  return $retval;
267
 }
268
269
    //
270
	function get_section_details( $section_id, $backLink = 'index.php' ) {
271
	global $database, $TEXT;
272
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'sections` ';
273 1486 DarkViper
		$sql .= 'WHERE `section_id`='.intval($section_id);
274 1441 Luisehahne
		if(($resSection = $database->query($sql))){
275
			if(!($recSection = $resSection->fetchRow())) {
276
				$this->print_header();
277
				$this->print_error($TEXT['SECTION'].' '.$TEXT['NOT_FOUND'], $backLink, true);
278
			}
279
			} else {
280
				$this->print_header();
281
				$this->print_error($database->get_error(), $backLink, true);
282
			}
283
		return $recSection;
284
	}
285
286
	function get_page_details( $page_id, $backLink = 'index.php' ) {
287 1486 DarkViper
		global $database, $TEXT;
288
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'pages` ';
289
		$sql .= 'WHERE `page_id`='.intval($page_id);
290
		if(($resPages = $database->query($sql))){
291
			if(!($recPage = $resPages->fetchRow())) {
292
			$this->print_header();
293
			$this->print_error($TEXT['PAGE'].' '.$TEXT['NOT_FOUND'], $backLink, true);
294
			}
295 1441 Luisehahne
		} else {
296 1486 DarkViper
			$this->print_header();
297
			$this->print_error($database->get_error(), $backLink, true);
298 1379 Luisehahne
		}
299 1486 DarkViper
		return $recPage;
300 1379 Luisehahne
	}
301
302 1441 Luisehahne
	function get_page_permission($page,$action='admin') {
303
		if($action != 'viewing') { $action = 'admin'; }
304
		$action_groups = $action.'_groups';
305
		$action_users  = $action.'_users';
306
		$groups = $users = '0';
307
		if(is_array($page)) {
308
			$groups = $page[$action_groups];
309
			$users  = $page[$action_users];
310
		} else {
311
			global $database;
312
			$sql  = 'SELECT `'.$action_groups.'`,`'.$action_users.'` ';
313
			$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
314
			$sql .= 'WHERE `page_id`='.(int)$page;
315
			if( ($res = $database->query($sql)) ) {
316
				if( ($rec = $res->fetchRow()) ) {
317
					$groups = $rec[$action_groups];
318
					$users  = $rec[$action_users];
319
				}
320
			}
321
		}
322
		return ($this->ami_group_member($groups) || $this->is_group_match($this->get_user_id(), $users));
323
	}
324
325 1379 Luisehahne
	// Returns a system permission for a menu link
326
	function get_link_permission($title) {
327
		$title = str_replace('_blank', '', $title);
328
		$title = strtolower($title);
329
		// Set system permissions var
330
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
331
		// Set module permissions var
332
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
333
		if($title == 'start') {
334
			return true;
335
		} else {
336
			// Return true if system perm = 1
337
			if(is_numeric(array_search($title, $system_permissions))) {
338
				return true;
339
			} else {
340
				return false;
341
			}
342
		}
343
	}
344
345
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
346
	function register_backend_modfiles_body($file_id="js")
347
		{
348
		// sanity check of parameter passed to the function
349
		$file_id = strtolower($file_id);
350
		if($file_id !== "javascript" && $file_id !== "js")
351
		{
352
			return;
353
		}
354
		global $database;
355
        $body_links = "";
356
		// define default baselink and filename for optional module javascript and stylesheet files
357
		if($file_id == "js") {
358 1441 Luisehahne
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
359 1379 Luisehahne
			$base_file = "backend_body.js";
360
		}
361
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
362
		if(isset($_GET['tool']))
363
			{
364
			// check if displayed page contains a installed admin tool
365 1486 DarkViper
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
366
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
367
			$result = $database->query($sql);
368 1379 Luisehahne
			if($result->numRows())
369
				{
370
				// check if admin tool directory contains a backend_body.js file to include
371
				$tool = $result->fetchRow();
372
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
373
				{
374
					// return link to the backend_body.js file
375
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
376
				}
377
			}
378
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
379
		{
380
			// check if displayed page in the backend contains a page module
381
			if (isset($_GET['page_id']))
382
			{
383
				$page_id = (int) addslashes($_GET['page_id']);
384
			} else {
385
				$page_id = (int) addslashes($_POST['page_id']);
386
			}
387
			// gather information for all models embedded on actual page
388 1486 DarkViper
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
389
			$query_modules = $database->query($sql);
390 1379 Luisehahne
			while($row = $query_modules->fetchRow()) {
391
				// check if page module directory contains a backend_body.js file
392
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
393
					// create link with backend_body.js source for the current module
394
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
395
					// ensure that backend_body.js is only added once per module type
396
					if(strpos($body_links, $tmp_link) === false) {
397
						$body_links .= $tmp_link ."\n";
398
					}
399
				}
400
			}
401
			// write out links with all external module javascript/CSS files, remove last line feed
402
			return rtrim($body_links);
403
		}
404
	}
405
406
407
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
408
	function register_backend_modfiles($file_id="css") {
409
		// sanity check of parameter passed to the function
410
		$file_id = strtolower($file_id);
411
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
412
			return;
413
		}
414
415
		global $database;
416
		// define default baselink and filename for optional module javascript and stylesheet files
417
		$head_links = "";
418
		if($file_id == "css") {
419
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
420
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
421
			$base_file = "backend.css";
422
		} else {
423 1441 Luisehahne
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
424 1379 Luisehahne
			$base_file = "backend.js";
425
		}
426
427
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
428
		if(isset($_GET['tool'])) {
429
			// check if displayed page contains a installed admin tool
430 1486 DarkViper
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
431
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
432
			$result = $database->query($sql);
433 1379 Luisehahne
			if($result->numRows()) {
434
				// check if admin tool directory contains a backend.js or backend.css file to include
435
				$tool = $result->fetchRow();
436
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
437
        			// return link to the backend.js or backend.css file
438
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
439
				}
440
			}
441 1441 Luisehahne
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
442 1379 Luisehahne
			// check if displayed page in the backend contains a page module
443
			if (isset($_GET['page_id'])) {
444
				$page_id = (int)$_GET['page_id'];
445
			} else {
446
				$page_id = (int)$_POST['page_id'];
447
			}
448
449
    		// gather information for all models embedded on actual page
450 1486 DarkViper
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
451
			$query_modules = $database->query($sql);
452 1379 Luisehahne
453
    		while($row = $query_modules->fetchRow()) {
454
				// check if page module directory contains a backend.js or backend.css file
455
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
456
					// create link with backend.js or backend.css source for the current module
457
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
458
        			// ensure that backend.js or backend.css is only added once per module type
459
        			if(strpos($head_links, $tmp_link) === false) {
460
						$head_links .= $tmp_link ."\n";
461
					}
462
				}
463
    		}
464
    		// write out links with all external module javascript/CSS files, remove last line feed
465
			return rtrim($head_links);
466
		}
467
	}
468
}
469
470 1412 DarkViper
?>