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 1486 2011-08-08 12:03:20Z DarkViper $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
15
 * @lastmodified    $Date: 2011-08-08 14:03:20 +0200 (Mon, 08 Aug 2011) $
16
 *
17
 */
18

    
19
// Must include code to stop this file being access directly
20
if(defined('WB_PATH') == false) { die("Cannot access this file directly"); }
21

    
22
require_once(WB_PATH.'/framework/class.wb.php');
23

    
24
// Get WB version
25
require_once(ADMIN_PATH.'/interface/version.php');
26

    
27
// Include EditArea wrapper functions
28
// require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
29
//require_once(WB_PATH . '/framework/SecureForm.php');
30

    
31

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

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

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

    
81
		// Auto header code
82
		if($auto_header == true) {
83
			$this->print_header();
84
		}
85
	}
86
	}
87

    
88
	// Print the admin header
89
	function print_header($body_tags = '') {
90
		// Get vars from the language file
91
		global $MENU;
92
		global $MESSAGE;
93
		global $TEXT;
94
		// Connect to database and get website title
95
		global $database;
96
		// $GLOBALS['FTAN'] = $this->getFTAN();
97
		$this->createFTAN();
98
		$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name`=\'website_title\'';
99
		$get_title = $database->query($sql);
100
		$title = $get_title->fetchRow();
101
		$header_template = new Template(THEME_PATH.'/templates');
102
		$header_template->set_file('page', 'header.htt');
103
		$header_template->set_block('page', 'header_block', 'header');
104
		if(defined('DEFAULT_CHARSET')) {
105
			$charset=DEFAULT_CHARSET;
106
		} else {
107
			$charset='utf-8';
108
		}
109

    
110
		// work out the URL for the 'View menu' link in the WB backend
111
		// if the page_id is set, show this page otherwise show the root directory of WB
112
		$view_url = WB_URL;
113
		if(isset($_GET['page_id'])) {
114
			// extract page link from the database
115
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
116
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
117
			$result = @$database->query($sql);
118
			$row = @$result->fetchRow();
119
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
120
		}
121

    
122
		$header_template->set_var(	array(
123
							'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
124
							'BODY_TAGS' => $body_tags,
125
							'WEBSITE_TITLE' => ($title['value']),
126
							'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
127
							'CURRENT_USER' => $MESSAGE['START']['CURRENT_USER'],
128
							'DISPLAY_NAME' => $this->get_display_name(),
129
							'CHARSET' => $charset,
130
							'LANGUAGE' => strtolower(LANGUAGE),
131
							'VERSION' => VERSION,
132
							'REVISION' => REVISION,
133
							'WB_URL' => WB_URL,
134
							'ADMIN_URL' => ADMIN_URL,
135
							'THEME_URL' => THEME_URL,
136
							'TITLE_START' => $MENU['START'],
137
							'TITLE_VIEW' => $MENU['VIEW'],
138
							'TITLE_HELP' => $MENU['HELP'],
139
							'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
140
							'URL_VIEW' => $view_url,
141
							'URL_HELP' => 'http://www.websitebaker2.org/',
142
							'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
143
							'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
144
						)
145
					);
146

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

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

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

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

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

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

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

    
400

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

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

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

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

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

    
464
?>
(6-6/18)