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 1462 2011-06-30 17:21:40Z DarkViper $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
15
 * @lastmodified    $Date: 2011-06-30 19:21:40 +0200 (Thu, 30 Jun 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
		$get_user_language = @$database->query("SELECT language FROM ".TABLE_PREFIX.
61
			"users WHERE user_id = '" .(int) $this->get_user_id() ."'");
62
		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
63
		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
64
		$user_language = substr($user_language[0],0,2);
65
		// obtain the admin folder (e.g. /admin)
66
		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
67
		if((LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
68
			&& strpos($_SERVER['PHP_SELF'],$admin_folder.'/') !== false) {
69
			// check if page_id is set
70
			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
71
			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
72
			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
73
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
74
			} else {
75
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url);
76
			}
77
			exit();
78
		}
79

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

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

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

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

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

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

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

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

    
293
	/** Function get_page_permission takes either a numerical page_id,
294
	 * upon which it looks up the permissions in the database,
295
	 * or an array with keys admin_groups and admin_users
296
	 */
297
/*
298
	function get_page_permission($page,$action='admin') {
299
		if ($action!='viewing') $action='admin';
300
		$action_groups=$action.'_groups';
301
		$action_users=$action.'_users';
302
		if (is_array($page)) {
303
				$groups=$page[$action_groups];
304
				$users=$page[$action_users];
305
		} else {
306
			global $database;
307
			$results = $database->query("SELECT $action_groups,$action_users FROM ".TABLE_PREFIX."pages WHERE page_id = '$page'");
308
			$result = $results->fetchRow();
309
			$groups = explode(',', str_replace('_', '', $result[$action_groups]));
310
			$users = explode(',', str_replace('_', '', $result[$action_users]));
311
		}
312

    
313
		$in_group = FALSE;
314
		foreach($this->get_groups_id() as $cur_gid){
315
		    if (in_array($cur_gid, $groups)) {
316
		        $in_group = TRUE;
317
		    }
318
		}
319
		if((!$in_group) AND !is_numeric(array_search($this->get_user_id(), $users))) {
320
			return false;
321
		}
322
		return true;
323
	}
324
*/
325

    
326
	function get_page_permission($page,$action='admin') {
327
		if($action != 'viewing') { $action = 'admin'; }
328
		$action_groups = $action.'_groups';
329
		$action_users  = $action.'_users';
330
		$groups = $users = '0';
331
		if(is_array($page)) {
332
			$groups = $page[$action_groups];
333
			$users  = $page[$action_users];
334
		} else {
335
			global $database;
336
			$sql  = 'SELECT `'.$action_groups.'`,`'.$action_users.'` ';
337
			$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
338
			$sql .= 'WHERE `page_id`='.(int)$page;
339
			if( ($res = $database->query($sql)) ) {
340
				if( ($rec = $res->fetchRow()) ) {
341
					$groups = $rec[$action_groups];
342
					$users  = $rec[$action_users];
343
				}
344
			}
345
		}
346
		return ($this->ami_group_member($groups) || $this->is_group_match($this->get_user_id(), $users));
347
	}
348

    
349
	// Returns a system permission for a menu link
350
	function get_link_permission($title) {
351
		$title = str_replace('_blank', '', $title);
352
		$title = strtolower($title);
353
		// Set system permissions var
354
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
355
		// Set module permissions var
356
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
357
		if($title == 'start') {
358
			return true;
359
		} else {
360
			// Return true if system perm = 1
361
			if(is_numeric(array_search($title, $system_permissions))) {
362
				return true;
363
			} else {
364
				return false;
365
			}
366
		}
367
	}
368

    
369
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
370
	function register_backend_modfiles_body($file_id="js")
371
		{
372
		// sanity check of parameter passed to the function
373
		$file_id = strtolower($file_id);
374
		if($file_id !== "javascript" && $file_id !== "js")
375
		{
376
			return;
377
		}
378
		global $database;
379
        $body_links = "";
380
		// define default baselink and filename for optional module javascript and stylesheet files
381
		if($file_id == "js") {
382
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
383
			$base_file = "backend_body.js";
384
		}
385
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
386
		if(isset($_GET['tool']))
387
			{
388
			// check if displayed page contains a installed admin tool
389
			$result = $database->query("SELECT * FROM " .TABLE_PREFIX ."addons
390
				WHERE type = 'module' AND function = 'tool' AND directory = '".addslashes($_GET['tool'])."'");
391
			if($result->numRows())
392
				{
393
				// check if admin tool directory contains a backend_body.js file to include
394
				$tool = $result->fetchRow();
395
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
396
				{
397
					// return link to the backend_body.js file
398
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
399
				}
400
			}
401
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
402
		{
403
			// check if displayed page in the backend contains a page module
404
			if (isset($_GET['page_id']))
405
			{
406
				$page_id = (int) addslashes($_GET['page_id']);
407
			} else {
408
				$page_id = (int) addslashes($_POST['page_id']);
409
			}
410
			// gather information for all models embedded on actual page
411
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections
412
				WHERE page_id=$page_id");
413
			while($row = $query_modules->fetchRow()) {
414
				// check if page module directory contains a backend_body.js file
415
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
416
					// create link with backend_body.js source for the current module
417
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
418
					// ensure that backend_body.js is only added once per module type
419
					if(strpos($body_links, $tmp_link) === false) {
420
						$body_links .= $tmp_link ."\n";
421
					}
422
				}
423
			}
424
			// write out links with all external module javascript/CSS files, remove last line feed
425
			return rtrim($body_links);
426
		}
427
	}
428

    
429

    
430
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
431
	function register_backend_modfiles($file_id="css") {
432
		// sanity check of parameter passed to the function
433
		$file_id = strtolower($file_id);
434
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
435
			return;
436
		}
437

    
438
		global $database;
439
		// define default baselink and filename for optional module javascript and stylesheet files
440
		$head_links = "";
441
		if($file_id == "css") {
442
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
443
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
444
			$base_file = "backend.css";
445
		} else {
446
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
447
			$base_file = "backend.js";
448
		}
449

    
450
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
451
		if(isset($_GET['tool'])) {
452
			// check if displayed page contains a installed admin tool
453
			$result = $database->query("SELECT * FROM " .TABLE_PREFIX ."addons
454
				WHERE type = 'module' AND function = 'tool' AND directory = '".addslashes($_GET['tool'])."'");
455

    
456
			if($result->numRows()) {
457
				// check if admin tool directory contains a backend.js or backend.css file to include
458
				$tool = $result->fetchRow();
459
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
460
        			// return link to the backend.js or backend.css file
461
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
462
				}
463
			}
464
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
465
			// check if displayed page in the backend contains a page module
466
			if (isset($_GET['page_id'])) {
467
				$page_id = (int)$_GET['page_id'];
468
			} else {
469
				$page_id = (int)$_POST['page_id'];
470
			}
471

    
472
    		// gather information for all models embedded on actual page
473
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections
474
				WHERE page_id=$page_id");
475

    
476
    		while($row = $query_modules->fetchRow()) {
477
				// check if page module directory contains a backend.js or backend.css file
478
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
479
					// create link with backend.js or backend.css source for the current module
480
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
481
        			// ensure that backend.js or backend.css is only added once per module type
482
        			if(strpos($head_links, $tmp_link) === false) {
483
						$head_links .= $tmp_link ."\n";
484
					}
485
				}
486
    		}
487
    		// write out links with all external module javascript/CSS files, remove last line feed
488
			return rtrim($head_links);
489
		}
490
	}
491
}
492

    
493
?>
(5-5/18)