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 1378 2011-01-13 01:21:42Z Luisehahne $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
15
 * @lastmodified    $Date: 2011-01-13 02:21:42 +0100 (Thu, 13 Jan 2011) $
16
 *
17
 */
18

    
19
if(!defined('WB_URL')) {
20
	header('Location: ../index.php');
21
	exit(0);
22
}
23

    
24
require_once(WB_PATH.'/framework/class.wb.php');
25

    
26
// Include PHPLIB template class
27
require_once(WB_PATH."/include/phplib/template.inc");
28

    
29
// Get WB version
30
require_once(ADMIN_PATH.'/interface/version.php');
31

    
32
// Include EditArea wrapper functions
33
require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
34
require_once(WB_PATH . '/framework/SecureForm.php');
35

    
36

    
37
class admin extends wb {
38
	// Authenticate user then auto print the header
39
	public function __construct($section_name, $section_permission = 'start', $auto_header = true, $auto_auth = true) {
40
		parent::__construct(SecureForm::BACKEND);
41
		global $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
			// First check if the user is logged-in
48
			if($this->is_authenticated() == false) {
49
				header('Location: '.ADMIN_URL.'/login/index.php');
50
				exit(0);
51
			}
52
			// Now check if they are allowed in this section
53
			if($this->get_permission($section_permission) == false) {
54
				die($MESSAGE['ADMIN']['INSUFFICIENT_PRIVELLIGES']);
55
			}
56
		}
57
		
58
		// Check if the backend language is also the selected language. If not, send headers again.
59
		global $database;
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
	// Print the admin header
87
	function print_header($body_tags = '') {
88
		// Get vars from the language file
89
		global $MENU;
90
		global $MESSAGE;
91
		global $TEXT;
92
		// Connect to database and get website title
93
		global $database;
94
		$get_title = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'website_title'");
95
		$title = $get_title->fetchRow();
96
		$header_template = new Template(THEME_PATH.'/templates');
97
		$header_template->set_file('page', 'header.htt');
98
		$header_template->set_block('page', 'header_block', 'header');
99
		if(defined('DEFAULT_CHARSET')) {
100
			$charset=DEFAULT_CHARSET;
101
		} else {
102
			$charset='utf-8';
103
		}
104

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

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

    
140
		// Create the menu
141
		$menu = array(
142
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
143
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
144
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
145
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 0),
146
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
147
					array(ADMIN_URL.'/admintools/index.php', '', $MENU['ADMINTOOLS'], 'admintools', 1),
148
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1)
149
					);
150
		$header_template->set_block('header_block', 'linkBlock', 'link');
151
		foreach($menu AS $menu_item) {
152
			$link = $menu_item[0];
153
			$target = ($menu_item[1] == '') ? '_self' : $menu_item[1];
154
			$title = $menu_item[2];
155
			$permission_title = $menu_item[3];
156
			$required = $menu_item[4];
157
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
158
			if($required == false OR $this->get_link_permission($permission_title)) {
159
				$header_template->set_var('LINK', $link);
160
				$header_template->set_var('TARGET', $target);
161
				// If link is the current section apply a class name
162
				if($permission_title == strtolower($this->section_name)) {
163
					$header_template->set_var('CLASS', $menu_item[3] . ' current');
164
				} else {
165
					$header_template->set_var('CLASS', $menu_item[3]);
166
				}
167
				$header_template->set_var('TITLE', $title);
168
				// Print link
169
				$header_template->parse('link', 'linkBlock', true);
170
			}
171
		}
172
		$header_template->parse('header', 'header_block', false);
173
		$header_template->pparse('output', 'page');
174
	}
175
	
176
	// Print the admin footer
177
	function print_footer() {
178
		// include the required file for Javascript admin
179
		if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
180
		@include(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
181
		}
182
		$footer_template = new Template(THEME_PATH.'/templates');
183
		$footer_template->set_file('page', 'footer.htt');
184
		$footer_template->set_block('page', 'footer_block', 'header');
185
		$footer_template->set_var(array(
186
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
187
						'WB_URL' => WB_URL,
188
						'WB_PATH' => WB_PATH,
189
						'ADMIN_URL' => ADMIN_URL,
190
						'THEME_URL' => THEME_URL,
191
			 ) );
192
		$footer_template->parse('header', 'footer_block', false);
193
		$footer_template->pparse('output', 'page');
194
	}
195
	
196
	// Return a system permission
197
	function get_permission($name, $type = 'system') {
198
		// Append to permission type
199
		$type .= '_permissions';
200
		// Check if we have a section to check for
201
		if($name == 'start') {
202
			return true;
203
		} else {
204
			// Set system permissions var
205
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
206
			// Set module permissions var
207
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
208
			// Set template permissions var
209
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
210
			// Return true if system perm = 1
211
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
212
				if($type == 'system_permissions') {
213
					return true;
214
				} else {
215
					return false;
216
				}
217
			} else {
218
				if($type == 'system_permissions') {
219
					return false;
220
				} else {
221
					return true;
222
				}
223
			}
224
		}
225
	}
226
		
227
	function get_user_details($user_id) {
228
		global $database;
229
		$query_user = "SELECT username,display_name FROM ".TABLE_PREFIX."users WHERE user_id = '$user_id'";
230
		$get_user = $database->query($query_user);
231
		if($get_user->numRows() != 0) {
232
			$user = $get_user->fetchRow();
233
		} else {
234
			$user['display_name'] = 'Unknown';
235
			$user['username'] = 'unknown';
236
		}
237
		return $user;
238
	}	
239
	
240
	function get_page_details($page_id) {
241
		global $database;
242
		$query = "SELECT page_id,page_title,menu_title,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
243
		$results = $database->query($query);
244
		if($database->is_error()) {
245
			$this->print_header();
246
			$this->print_error($database->get_error());
247
		}
248
		if($results->numRows() == 0) {
249
			$this->print_header();
250
			$this->print_error($MESSAGE['PAGES']['NOT_FOUND']);
251
		}
252
		$results_array = $results->fetchRow();
253
		return $results_array;
254
	}	
255
	
256
	/** Function get_page_permission takes either a numerical page_id,
257
	 * upon which it looks up the permissions in the database,
258
	 * or an array with keys admin_groups and admin_users  
259
	 */
260
	function get_page_permission($page,$action='admin') {
261
		if ($action!='viewing') $action='admin';
262
		$action_groups=$action.'_groups';
263
		$action_users=$action.'_users';
264
		if (is_array($page)) {
265
				$groups=$page[$action_groups];
266
				$users=$page[$action_users];
267
		} else {				
268
			global $database;
269
			$results = $database->query("SELECT $action_groups,$action_users FROM ".TABLE_PREFIX."pages WHERE page_id = '$page'");
270
			$result = $results->fetchRow();
271
			$groups = explode(',', str_replace('_', '', $result[$action_groups]));
272
			$users = explode(',', str_replace('_', '', $result[$action_users]));
273
		}
274

    
275
		$in_group = FALSE;
276
		foreach($this->get_groups_id() as $cur_gid){
277
		    if (in_array($cur_gid, $groups)) {
278
		        $in_group = TRUE;
279
		    }
280
		}
281
		if((!$in_group) AND !is_numeric(array_search($this->get_user_id(), $users))) {
282
			return false;
283
		}
284
		return true;
285
	}
286
		
287

    
288
	// Returns a system permission for a menu link
289
	function get_link_permission($title) {
290
		$title = str_replace('_blank', '', $title);
291
		$title = strtolower($title);
292
		// Set system permissions var
293
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
294
		// Set module permissions var
295
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
296
		if($title == 'start') {
297
			return true;
298
		} else {
299
			// Return true if system perm = 1
300
			if(is_numeric(array_search($title, $system_permissions))) {
301
				return true;
302
			} else {
303
				return false;
304
			}
305
		}
306
	}
307

    
308
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
309
	function register_backend_modfiles_body($file_id="js")
310
		{
311
		// sanity check of parameter passed to the function
312
		$file_id = strtolower($file_id);
313
		if($file_id !== "javascript" && $file_id !== "js")
314
		{
315
			return;
316
		}
317
		global $database;
318
        $body_links = "";
319
		// define default baselink and filename for optional module javascript and stylesheet files
320
		if($file_id == "js") {
321
			$base_link = '<script type="text/javascript" src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js"></script>';
322
			$base_file = "backend_body.js";
323
		}
324
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
325
		if(isset($_GET['tool']))
326
			{
327
			// check if displayed page contains a installed admin tool
328
			$result = $database->query("SELECT * FROM " .TABLE_PREFIX ."addons
329
				WHERE type = 'module' AND function = 'tool' AND directory = '".addslashes($_GET['tool'])."'");
330
			if($result->numRows())
331
				{
332
				// check if admin tool directory contains a backend_body.js file to include
333
				$tool = $result->fetchRow();
334
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
335
				{
336
					// return link to the backend_body.js file
337
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
338
				}
339
			}
340
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
341
		{
342
			// check if displayed page in the backend contains a page module
343
			if (isset($_GET['page_id']))
344
			{
345
				$page_id = (int) addslashes($_GET['page_id']);
346
			} else {
347
				$page_id = (int) addslashes($_POST['page_id']);
348
			}
349
			// gather information for all models embedded on actual page
350
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections
351
				WHERE page_id=$page_id");
352
			while($row = $query_modules->fetchRow()) {
353
				// check if page module directory contains a backend_body.js file
354
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
355
					// create link with backend_body.js source for the current module
356
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
357
					// ensure that backend_body.js is only added once per module type
358
					if(strpos($body_links, $tmp_link) === false) {
359
						$body_links .= $tmp_link ."\n";
360
					}
361
				}
362
			}
363
			// write out links with all external module javascript/CSS files, remove last line feed
364
			return rtrim($body_links);
365
		}
366
	}
367

    
368

    
369
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
370
	function register_backend_modfiles($file_id="css") {
371
		// sanity check of parameter passed to the function
372
		$file_id = strtolower($file_id);
373
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
374
			return;
375
		}
376

    
377
		global $database;
378
		// define default baselink and filename for optional module javascript and stylesheet files
379
		$head_links = "";
380
		if($file_id == "css") {
381
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
382
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
383
			$base_file = "backend.css";
384
		} else {
385
			$base_link = '<script type="text/javascript" src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js"></script>';
386
			$base_file = "backend.js";
387
		}
388

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

    
395
			if($result->numRows()) {
396
				// check if admin tool directory contains a backend.js or backend.css file to include
397
				$tool = $result->fetchRow();
398
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
399
        			// return link to the backend.js or backend.css file
400
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
401
				}
402
			}
403
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id'])) {
404
			// check if displayed page in the backend contains a page module
405
			if (isset($_GET['page_id'])) {
406
				$page_id = (int)$_GET['page_id'];
407
			} else {
408
				$page_id = (int)$_POST['page_id'];
409
			}
410

    
411
    		// gather information for all models embedded on actual page
412
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections
413
				WHERE page_id=$page_id");
414

    
415
    		while($row = $query_modules->fetchRow()) {
416
				// check if page module directory contains a backend.js or backend.css file
417
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
418
					// create link with backend.js or backend.css source for the current module
419
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
420
        			// ensure that backend.js or backend.css is only added once per module type
421
        			if(strpos($head_links, $tmp_link) === false) {
422
						$head_links .= $tmp_link ."\n";
423
					}
424
				}
425
    		}
426
    		// write out links with all external module javascript/CSS files, remove last line feed
427
			return rtrim($head_links);
428
		}
429
	}
430
}
431

    
432
?>
(4-4/16)