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 1529 2011-11-25 05:03:32Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2011-11-25 06:03:32 +0100 (Fri, 25 Nov 2011) $
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
							'REVISION' => REVISION,
137
							'WB_URL' => WB_URL,
138
							'ADMIN_URL' => ADMIN_URL,
139
							'THEME_URL' => THEME_URL,
140
							'TITLE_START' => $MENU['START'],
141
							'TITLE_VIEW' => $MENU['VIEW'],
142
							'TITLE_HELP' => $MENU['HELP'],
143
							'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
144
							'URL_VIEW' => $view_url,
145
							'URL_HELP' => 'http://www.websitebaker2.org/',
146
							'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
147
							'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
148
						)
149
					);
150

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

    
196
		// Setup template object, parse vars to it, then parse it
197
		$ThemePath = realpath(WB_PATH.$this->correct_theme_source('footer.htt'));
198
		$footer_template = new Template($ThemePath);
199
		$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
/*
242
	function get_user_details($user_id) {
243
		global $database;
244
		$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
		}
252
		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
  $sql .= 'WHERE `user_id`='.(int)$user_id;
261
  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
		$sql .= 'WHERE `section_id`='.intval($section_id);
274
		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
		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
		} else {
296
			$this->print_header();
297
			$this->print_error($database->get_error(), $backLink, true);
298
		}
299
		return $recPage;
300
	}
301

    
302
	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
	// 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
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
359
			$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
			$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
			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
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
389
			$query_modules = $database->query($sql);
390
			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
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
424
			$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
			$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
			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
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
442
			// 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
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
451
			$query_modules = $database->query($sql);
452

    
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
?>
(7-7/20)