Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framewotk
5
 * @package         backend admin
6
 * @author          Ryan Djurovich, WebsiteBaker Project
7
 * @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: class.admin.php 1563 2012-01-06 01:22:01Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2012-01-06 02:22:01 +0100 (Fri, 06 Jan 2012) $
15
 *
16
 */
17
/* -------------------------------------------------------- */
18
// Must include code to stop this file being accessed directly
19
if(!defined('WB_PATH')) {
20
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
21
	throw new IllegalFileException();
22
}
23
/* -------------------------------------------------------- */
24
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
// require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
31
//require_once(WB_PATH . '/framework/SecureForm.php');
32

    
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
		$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
		$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
		// $GLOBALS['FTAN'] = $this->getFTAN();
99
		$this->createFTAN();
100
		$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name`=\'website_title\'';
101
		$get_title = $database->query($sql);
102
		$title = $get_title->fetchRow();
103
		// 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
		$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
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
120
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
121
			$result = @$database->query($sql);
122
			$row = @$result->fetchRow();
123
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
124
		}
125

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

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

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

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

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

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

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

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

    
408

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

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

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

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

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

    
472
?>
(7-7/22)