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 1443 2011-04-19 19:38:37Z Luisehahne $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
15
 * @lastmodified    $Date: 2011-04-19 21:38:37 +0200 (Tue, 19 Apr 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
		$get_title = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'website_title'");
96
		$title = $get_title->fetchRow();
97
		$header_template = new Template(THEME_PATH.'/templates');
98
		$header_template->set_file('page', 'header.htt');
99
		$header_template->set_block('page', 'header_block', 'header');
100
		if(defined('DEFAULT_CHARSET')) {
101
			$charset=DEFAULT_CHARSET;
102
		} else {
103
			$charset='utf-8';
104
		}
105

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

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

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

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

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

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

    
291
	/** Function get_page_permission takes either a numerical page_id,
292
	 * upon which it looks up the permissions in the database,
293
	 * or an array with keys admin_groups and admin_users
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
		if (is_array($page)) {
301
				$groups=$page[$action_groups];
302
				$users=$page[$action_users];
303
		} else {
304
			global $database;
305
			$results = $database->query("SELECT $action_groups,$action_users FROM ".TABLE_PREFIX."pages WHERE page_id = '$page'");
306
			$result = $results->fetchRow();
307
			$groups = explode(',', str_replace('_', '', $result[$action_groups]));
308
			$users = explode(',', str_replace('_', '', $result[$action_users]));
309
		}
310

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

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

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

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

    
427

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

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

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

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

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

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

    
491
?>
(4-4/16)