Project

General

Profile

1
<?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: class.admin.php 1499 2011-08-12 11:21:25Z DarkViper $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
15
 * @lastmodified    $Date: 2011-08-12 13:21:25 +0200 (Fri, 12 Aug 2011) $
16
 *
17
 */
18
/* -------------------------------------------------------- */
19
// Must include code to stop this file being accessed directly
20
if(!defined('WB_PATH')) {
21
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
22
	throw new IllegalFileException();
23
}
24
/* -------------------------------------------------------- */
25
require_once(WB_PATH.'/framework/class.wb.php');
26

    
27
// Get WB version
28
require_once(ADMIN_PATH.'/interface/version.php');
29

    
30
// Include EditArea wrapper functions
31
// require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
32
//require_once(WB_PATH . '/framework/SecureForm.php');
33

    
34

    
35
class admin extends wb {
36
	// Authenticate user then auto print the header
37
	public function __construct($section_name= '##skip##', $section_permission = 'start', $auto_header = true, $auto_auth = true)
38
	{
39
		parent::__construct(SecureForm::BACKEND);
40
	if( $section_name != '##skip##' )
41
	{
42
		global $database, $MESSAGE;
43
		// Specify the current applications name
44
		$this->section_name = $section_name;
45
		$this->section_permission = $section_permission;
46
		// Authenticate the user for this application
47
		if($auto_auth == true)
48
		{
49
			// First check if the user is logged-in
50
			if($this->is_authenticated() == false)
51
			{
52
				header('Location: '.ADMIN_URL.'/login/index.php');
53
				exit(0);
54
			}
55

    
56
			// Now check if they are allowed in this section
57
			if($this->get_permission($section_permission) == false) {
58
				die($MESSAGE['ADMIN']['INSUFFICIENT_PRIVELLIGES']);
59
			}
60
		}
61

    
62
		// Check if the backend language is also the selected language. If not, send headers again.
63
		$sql  = 'SELECT `language` FROM `'.TABLE_PREFIX.'users` ';
64
		$sql .= 'WHERE `user_id`='.(int)$this->get_user_id();
65
		$get_user_language = @$database->query($sql);
66
		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
67
		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
68
		$user_language = substr($user_language[0],0,2);
69
		// obtain the admin folder (e.g. /admin)
70
		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
71
		if((LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
72
			&& strpos($_SERVER['PHP_SELF'],$admin_folder.'/') !== false) {
73
			// check if page_id is set
74
			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
75
			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
76
			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
77
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
78
			} else {
79
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url);
80
			}
81
			exit();
82
		}
83

    
84
		// Auto header code
85
		if($auto_header == true) {
86
			$this->print_header();
87
		}
88
	}
89
	}
90

    
91
	// Print the admin header
92
	function print_header($body_tags = '') {
93
		// Get vars from the language file
94
		global $MENU;
95
		global $MESSAGE;
96
		global $TEXT;
97
		// Connect to database and get website title
98
		global $database;
99
		// $GLOBALS['FTAN'] = $this->getFTAN();
100
		$this->createFTAN();
101
		$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name`=\'website_title\'';
102
		$get_title = $database->query($sql);
103
		$title = $get_title->fetchRow();
104
		$header_template = new Template(THEME_PATH.'/templates');
105
		$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
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
119
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
120
			$result = @$database->query($sql);
121
			$row = @$result->fetchRow();
122
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
123
		}
124

    
125
		$header_template->set_var(	array(
126
							'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
							'REVISION' => REVISION,
136
							'WB_URL' => WB_URL,
137
							'ADMIN_URL' => ADMIN_URL,
138
							'THEME_URL' => THEME_URL,
139
							'TITLE_START' => $MENU['START'],
140
							'TITLE_VIEW' => $MENU['VIEW'],
141
							'TITLE_HELP' => $MENU['HELP'],
142
							'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
143
							'URL_VIEW' => $view_url,
144
							'URL_HELP' => 'http://www.websitebaker2.org/',
145
							'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
146
							'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
147
						)
148
					);
149

    
150
		// Create the menu
151
		$menu = array(
152
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
153
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
154
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
155
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 0),
156
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
157
					array(ADMIN_URL.'/admintools/index.php', '', $MENU['ADMINTOOLS'], 'admintools', 1),
158
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1)
159
					);
160
		$header_template->set_block('header_block', 'linkBlock', 'link');
161
		foreach($menu AS $menu_item) {
162
			$link = $menu_item[0];
163
			$target = ($menu_item[1] == '') ? '_self' : $menu_item[1];
164
			$title = $menu_item[2];
165
			$permission_title = $menu_item[3];
166
			$required = $menu_item[4];
167
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
168
			if($required == false OR $this->get_link_permission($permission_title)) {
169
				$header_template->set_var('LINK', $link);
170
				$header_template->set_var('TARGET', $target);
171
				// If link is the current section apply a class name
172
				if($permission_title == strtolower($this->section_name)) {
173
					$header_template->set_var('CLASS', $menu_item[3] . ' current');
174
				} else {
175
					$header_template->set_var('CLASS', $menu_item[3]);
176
				}
177
				$header_template->set_var('TITLE', $title);
178
				// Print link
179
				$header_template->parse('link', 'linkBlock', true);
180
			}
181
		}
182
		$header_template->parse('header', 'header_block', false);
183
		$header_template->pparse('output', 'page');
184
	}
185
	
186
	// Print the admin footer
187
		function print_footer($activateJsAdmin = false) {
188
		// include the required file for Javascript admin
189
		if($activateJsAdmin != false) {
190
			if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
191
				@include_once(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
192
			}
193
		}
194

    
195
		$footer_template = new Template(THEME_PATH.'/templates');
196
		$footer_template->set_file('page', 'footer.htt');
197
		$footer_template->set_block('page', 'footer_block', 'header');
198
		$footer_template->set_var(array(
199
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
200
						'WB_URL' => WB_URL,
201
						'ADMIN_URL' => ADMIN_URL,
202
						'THEME_URL' => THEME_URL,
203
			 ) );
204
		$footer_template->parse('header', 'footer_block', false);
205
		$footer_template->pparse('output', 'page');
206
	}
207
	
208
	// Return a system permission
209
	function get_permission($name, $type = 'system') {
210
		// Append to permission type
211
		$type .= '_permissions';
212
		// Check if we have a section to check for
213
		if($name == 'start') {
214
			return true;
215
		} else {
216
			// Set system permissions var
217
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
218
			// Set module permissions var
219
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
220
			// Set template permissions var
221
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
222
			// Return true if system perm = 1
223
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
224
				if($type == 'system_permissions') {
225
					return true;
226
				} else {
227
					return false;
228
				}
229
			} else {
230
				if($type == 'system_permissions') {
231
					return false;
232
				} else {
233
					return true;
234
				}
235
			}
236
		}
237
	}
238
/*
239
	function get_user_details($user_id) {
240
		global $database;
241
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'users` ';
242
		$sql .= 'WHERE `user_id`='.(int)$user_id.' LIMIT 1';
243
		if(($resUser = $database->query($sql))){
244
			if(!($recUser = $resUser->fetchRow())) {
245
				$recUser['display_name'] = 'Unknown';
246
				$recUser['username'] = 'unknown';
247
			}
248
		}
249
		return $recUser;
250
	}
251
*/
252
 function get_user_details($user_id) {
253
  global $database;
254
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
255
  $sql  = 'SELECT `username`,`display_name`,`email` ';
256
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
257
  $sql .= 'WHERE `user_id`='.(int)$user_id;
258
  if( ($resUsers = $database->query($sql)) ) {
259
   if( ($recUser = $resUsers->fetchRow()) ) {
260
    $retval = $recUser;
261
   }
262
  }
263
  return $retval;
264
 }
265

    
266
    //
267
	function get_section_details( $section_id, $backLink = 'index.php' ) {
268
	global $database, $TEXT;
269
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'sections` ';
270
		$sql .= 'WHERE `section_id`='.intval($section_id);
271
		if(($resSection = $database->query($sql))){
272
			if(!($recSection = $resSection->fetchRow())) {
273
				$this->print_header();
274
				$this->print_error($TEXT['SECTION'].' '.$TEXT['NOT_FOUND'], $backLink, true);
275
			}
276
			} else {
277
				$this->print_header();
278
				$this->print_error($database->get_error(), $backLink, true);
279
			}
280
		return $recSection;
281
	}
282

    
283
	function get_page_details( $page_id, $backLink = 'index.php' ) {
284
		global $database, $TEXT;
285
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'pages` ';
286
		$sql .= 'WHERE `page_id`='.intval($page_id);
287
		if(($resPages = $database->query($sql))){
288
			if(!($recPage = $resPages->fetchRow())) {
289
			$this->print_header();
290
			$this->print_error($TEXT['PAGE'].' '.$TEXT['NOT_FOUND'], $backLink, true);
291
			}
292
		} else {
293
			$this->print_header();
294
			$this->print_error($database->get_error(), $backLink, true);
295
		}
296
		return $recPage;
297
	}
298

    
299
	function get_page_permission($page,$action='admin') {
300
		if($action != 'viewing') { $action = 'admin'; }
301
		$action_groups = $action.'_groups';
302
		$action_users  = $action.'_users';
303
		$groups = $users = '0';
304
		if(is_array($page)) {
305
			$groups = $page[$action_groups];
306
			$users  = $page[$action_users];
307
		} else {
308
			global $database;
309
			$sql  = 'SELECT `'.$action_groups.'`,`'.$action_users.'` ';
310
			$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
311
			$sql .= 'WHERE `page_id`='.(int)$page;
312
			if( ($res = $database->query($sql)) ) {
313
				if( ($rec = $res->fetchRow()) ) {
314
					$groups = $rec[$action_groups];
315
					$users  = $rec[$action_users];
316
				}
317
			}
318
		}
319
		return ($this->ami_group_member($groups) || $this->is_group_match($this->get_user_id(), $users));
320
	}
321

    
322
	// Returns a system permission for a menu link
323
	function get_link_permission($title) {
324
		$title = str_replace('_blank', '', $title);
325
		$title = strtolower($title);
326
		// Set system permissions var
327
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
328
		// Set module permissions var
329
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
330
		if($title == 'start') {
331
			return true;
332
		} else {
333
			// Return true if system perm = 1
334
			if(is_numeric(array_search($title, $system_permissions))) {
335
				return true;
336
			} else {
337
				return false;
338
			}
339
		}
340
	}
341

    
342
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
343
	function register_backend_modfiles_body($file_id="js")
344
		{
345
		// sanity check of parameter passed to the function
346
		$file_id = strtolower($file_id);
347
		if($file_id !== "javascript" && $file_id !== "js")
348
		{
349
			return;
350
		}
351
		global $database;
352
        $body_links = "";
353
		// define default baselink and filename for optional module javascript and stylesheet files
354
		if($file_id == "js") {
355
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
356
			$base_file = "backend_body.js";
357
		}
358
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
359
		if(isset($_GET['tool']))
360
			{
361
			// check if displayed page contains a installed admin tool
362
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
363
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
364
			$result = $database->query($sql);
365
			if($result->numRows())
366
				{
367
				// check if admin tool directory contains a backend_body.js file to include
368
				$tool = $result->fetchRow();
369
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
370
				{
371
					// return link to the backend_body.js file
372
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
373
				}
374
			}
375
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
376
		{
377
			// check if displayed page in the backend contains a page module
378
			if (isset($_GET['page_id']))
379
			{
380
				$page_id = (int) addslashes($_GET['page_id']);
381
			} else {
382
				$page_id = (int) addslashes($_POST['page_id']);
383
			}
384
			// gather information for all models embedded on actual page
385
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
386
			$query_modules = $database->query($sql);
387
			while($row = $query_modules->fetchRow()) {
388
				// check if page module directory contains a backend_body.js file
389
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
390
					// create link with backend_body.js source for the current module
391
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
392
					// ensure that backend_body.js is only added once per module type
393
					if(strpos($body_links, $tmp_link) === false) {
394
						$body_links .= $tmp_link ."\n";
395
					}
396
				}
397
			}
398
			// write out links with all external module javascript/CSS files, remove last line feed
399
			return rtrim($body_links);
400
		}
401
	}
402

    
403

    
404
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
405
	function register_backend_modfiles($file_id="css") {
406
		// sanity check of parameter passed to the function
407
		$file_id = strtolower($file_id);
408
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
409
			return;
410
		}
411

    
412
		global $database;
413
		// define default baselink and filename for optional module javascript and stylesheet files
414
		$head_links = "";
415
		if($file_id == "css") {
416
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
417
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
418
			$base_file = "backend.css";
419
		} else {
420
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
421
			$base_file = "backend.js";
422
		}
423

    
424
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
425
		if(isset($_GET['tool'])) {
426
			// check if displayed page contains a installed admin tool
427
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
428
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
429
			$result = $database->query($sql);
430
			if($result->numRows()) {
431
				// check if admin tool directory contains a backend.js or backend.css file to include
432
				$tool = $result->fetchRow();
433
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
434
        			// return link to the backend.js or backend.css file
435
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
436
				}
437
			}
438
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
439
			// check if displayed page in the backend contains a page module
440
			if (isset($_GET['page_id'])) {
441
				$page_id = (int)$_GET['page_id'];
442
			} else {
443
				$page_id = (int)$_POST['page_id'];
444
			}
445

    
446
    		// gather information for all models embedded on actual page
447
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
448
			$query_modules = $database->query($sql);
449

    
450
    		while($row = $query_modules->fetchRow()) {
451
				// check if page module directory contains a backend.js or backend.css file
452
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
453
					// create link with backend.js or backend.css source for the current module
454
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
455
        			// ensure that backend.js or backend.css is only added once per module type
456
        			if(strpos($head_links, $tmp_link) === false) {
457
						$head_links .= $tmp_link ."\n";
458
					}
459
				}
460
    		}
461
    		// write out links with all external module javascript/CSS files, remove last line feed
462
			return rtrim($head_links);
463
		}
464
	}
465
}
466

    
467
?>
(7-7/20)