Project

General

Profile

1
<?php
2

    
3
// $Id: class.admin.php,v 1.13 2005/06/23 01:10:46 rdjurovich Exp $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
/*
27

    
28
Admin class
29

    
30
This class will be used for every program that will be included
31
in the administration section of Website Baker.
32

    
33
*/
34

    
35
// Stop this file from being accessed directly
36
if(!defined('WB_PATH')) { exit('Direct access to this file is not allowed'); }
37

    
38
// Say that this file has been loaded
39
define('ADMIN_CLASS_LOADED', true);
40
	
41
// Load the other required class files if they are not already loaded
42
require_once(WB_PATH.'/framework/class.database.php');
43
if(!isset($database)) {
44
	$database = new database();
45
}
46

    
47
// Include PHPLIB template class
48
require_once(WB_PATH."/include/phplib/template.inc");
49

    
50
// Start a session
51
if(!defined('SESSION_STARTED')) {
52
	session_name(APP_NAME.'_session_id');
53
	session_start();
54
	define('SESSION_STARTED', true);
55
}
56

    
57
// Get WB version
58
require_once(ADMIN_PATH.'/interface/version.php');
59

    
60
/*
61
Begin user changeable settings
62
*/
63
if(!defined('FRONTEND_LOADED')) {
64
	// Get users language
65
	if(!defined('LANGUAGE')) {
66
		if(isset($_SESSION['LANGUAGE']) AND $_SESSION['LANGUAGE'] != '') {
67
			define('LANGUAGE', $_SESSION['LANGUAGE']);
68
		} else {
69
			define('LANGUAGE', DEFAULT_LANGUAGE);
70
		}
71
	}
72
	// Get users timezone
73
	if(!defined('TIMEZONE')) {
74
		if(isset($_SESSION['TIMEZONE'])) {
75
			define('TIMEZONE', $_SESSION['TIMEZONE']);
76
		} else {
77
			define('TIMEZONE', DEFAULT_TIMEZONE);
78
		}
79
	}
80
	// Get users date format
81
	if(!defined('DATE_FORMAT')) {
82
		if(isset($_SESSION['DATE_FORMAT'])) {
83
			define('DATE_FORMAT', $_SESSION['DATE_FORMAT']);
84
		} else {
85
			define('DATE_FORMAT', DEFAULT_DATE_FORMAT);
86
		}
87
	}
88
	// Get users time format
89
	if(!defined('TIME_FORMAT')) {
90
		if(isset($_SESSION['TIME_FORMAT'])) {
91
			define('TIME_FORMAT', $_SESSION['TIME_FORMAT']);
92
		} else {
93
			define('TIME_FORMAT', DEFAULT_TIME_FORMAT);
94
		}
95
	}
96
	// Load the language file
97
	if(!defined('LANGUAGE_LOADED')) {
98
		if(!file_exists(WB_PATH.'/languages/'.LANGUAGE.'.php')) {
99
			exit('Error loading language file '.LANGUAGE.', please check configuration');
100
		} else {
101
			require(WB_PATH.'/languages/'.LANGUAGE.'.php');
102
		}
103
	}
104
}
105
/*
106
End user changeable settings
107
*/
108

    
109
class admin {
110
	// Authenticate user then auto print the header
111
	function admin($section_name, $section_permission = 'start', $auto_header = true, $auto_auth = true) {
112
		global $MESSAGE;
113
		// Specify the current applications name
114
		$this->section_name = $section_name;
115
		$this->section_permission = $section_permission;
116
		// Authenticate the user for this application
117
		if($auto_auth == true) {
118
			// First check if the user is logged-in
119
			if($this->is_authenticated() == false) {
120
				header('Location: '.ADMIN_URL.'/login/index.php');
121
			}
122
			// Now check if they are allowed in this section
123
			if($this->get_permission($section_permission) == false) {
124
				die($MESSAGE['ADMIN']['INSUFFICIENT_PRIVELLIGES']);
125
			}
126
		}
127
		// Auto header code
128
		if($auto_header == true) {
129
			$this->print_header();
130
		}
131
	}
132
	
133
	// Print the admin header
134
	function print_header($body_tags = '') {
135
		// Get vars from the language file
136
		global $MENU;
137
		global $MESSAGE;
138
		global $TEXT;
139
		// Connect to database and get website title
140
		$database = new database();
141
		$get_title = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'title'");
142
		$title = $get_title->fetchRow();
143
		$header_template = new Template(ADMIN_PATH."/interface");
144
		$header_template->set_file('page', 'header.html');
145
		$header_template->set_block('page', 'header_block', 'header');
146
		$header_template->set_var(	array(
147
													'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
148
													'INTERFACE_DIR' => ADMIN_URL.'/interface',
149
													'BODY_TAGS' => $body_tags,
150
													'WEBSITE_TITLE' => stripslashes($title['value']),
151
													'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
152
													'VERSION' => VERSION
153
													)
154
											);
155
		// Create the menu
156
		$menu = array(
157
					array(ADMIN_URL.'/start/index.php', '', $MENU['START'], 'start', 0),
158
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
159
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
160
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
161
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 0),
162
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
163
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1),
164
					array('http://www.websitebaker.org/docs/', '_blank', $MENU['HELP'], 'help', 0),
165
					array(WB_URL.'/', '_blank', $MENU['VIEW'], 'view', 0),
166
					array(ADMIN_URL.'/logout/index.php', '', $MENU['LOGOUT'], 'logout', 0)
167
					);
168
		$header_template->set_block('header_block', 'linkBlock', 'link');
169
		foreach($menu AS $menu_item) {
170
			$link = $menu_item[0];
171
			$target = $menu_item[1];
172
			$title = $menu_item[2];
173
			$permission_title = $menu_item[3];
174
			$required = $menu_item[4];
175
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
176
			if($required == false OR $this->get_link_permission($permission_title)) {
177
				$header_template->set_var('LINK', $link);
178
				$header_template->set_var('TARGET', $target);
179
				// If link is the current section apply a class name
180
				if($permission_title == strtolower($this->section_name)) {
181
					$header_template->set_var('CLASS', 'current');
182
				} else {
183
					$header_template->set_var('CLASS', '');
184
				}
185
				$header_template->set_var('TITLE', $title);
186
				// Print link
187
				$header_template->parse('link', 'linkBlock', true);
188
			}
189
		}
190
		$header_template->parse('header', 'header_block', false);
191
		$header_template->pparse('output', 'page');
192
	}
193
	
194
	// Print the admin footer
195
	function print_footer() {
196
		$footer_template = new Template(ADMIN_PATH."/interface");
197
		$footer_template->set_file('page', 'footer.html');
198
		$footer_template->set_block('page', 'footer_block', 'header');
199
		$footer_template->parse('header', 'footer_block', false);
200
		$footer_template->pparse('output', 'page');
201
	}
202
	
203
	// Print a success message which then automatically redirects the user to another page
204
	function print_success($message, $redirect = 'index.php') {
205
		global $TEXT;
206
		$success_template = new Template(ADMIN_PATH.'/interface');
207
		$success_template->set_file('page', 'success.html');
208
		$success_template->set_block('page', 'main_block', 'main');
209
		$success_template->set_var('MESSAGE', $message);
210
		$success_template->set_var('REDIRECT', $redirect);
211
		$success_template->set_var('NEXT', $TEXT['NEXT']);
212
		$success_template->parse('main', 'main_block', false);
213
		$success_template->pparse('output', 'page');
214
	}
215
	
216
	// Print a error message
217
	function print_error($message, $link = 'index.php', $auto_footer = true) {
218
		global $TEXT;
219
		$success_template = new Template(ADMIN_PATH.'/interface');
220
		$success_template->set_file('page', 'error.html');
221
		$success_template->set_block('page', 'main_block', 'main');
222
		$success_template->set_var('MESSAGE', $message);
223
		$success_template->set_var('LINK', $link);
224
		$success_template->set_var('BACK', $TEXT['BACK']);
225
		$success_template->parse('main', 'main_block', false);
226
		$success_template->pparse('output', 'page');
227
		if($auto_footer == true) {
228
			$this->print_footer();
229
		}
230
		exit();
231
	}
232
	
233
	// Return a system permission
234
	function get_permission($name, $type = 'system') {
235
		// Append to permission type
236
		$type .= '_permissions';
237
		// Check if we have a section to check for
238
		if($name == 'start') {
239
			return true;
240
		} else {
241
			// Set system permissions var
242
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
243
			// Set module permissions var
244
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
245
			// Set template permissions var
246
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
247
			// Return true if system perm = 1
248
			if(is_numeric(array_search($name, $$type))) {
249
				if($type == 'system_permissions') {
250
					return true;
251
				} else {
252
					return false;
253
				}
254
			} else {
255
				if($type == 'system_permissions') {
256
					return false;
257
				} else {
258
					return true;
259
				}
260
			}
261
		}
262
	}
263
	
264
	// Returns a system permission for a menu link
265
	function get_link_permission($title) {
266
		$title = str_replace('_blank', '', $title);
267
		$title = strtolower($title);
268
		// Set system permissions var
269
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
270
		// Set module permissions var
271
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
272
		if($title == 'start') {
273
			return true;
274
		} else {
275
			// Return true if system perm = 1
276
			if(is_numeric(array_search($title, $system_permissions))) {
277
				return true;
278
			} else {
279
				return false;
280
			}
281
		}
282
	}
283
	
284
	// Check whether we should show a page or not (for front-end)
285
	function show_page($page) {
286
		// First check if the page is set to private
287
		if($page['visibility'] == 'private' OR $page['visibility'] == 'registered') {
288
			// Check if the user is logged in
289
			if($this->is_authenticated() == true) {
290
				// Now check if the user has perms to view it
291
				$viewing_groups = explode(',', $page['viewing_groups']);
292
				$viewing_users = explode(',', $page['viewing_users']);
293
				if(is_numeric(array_search($this->get_group_id(), $viewing_groups)) OR is_numeric(array_search($this->get_user_id(), $viewing_users))) {
294
					return true;
295
				} else {
296
					return false;
297
				}
298
			} else {
299
				return false;
300
			}
301
		} elseif($page['visibility'] == 'public') {
302
			return true;
303
		} else {
304
			return false;
305
		}
306
	}
307
	
308
	// Check if the user is already authenticated or not
309
	function is_authenticated() {
310
		if(isset($_SESSION['USER_ID']) AND $_SESSION['USER_ID'] != "" AND is_numeric($_SESSION['USER_ID'])) {
311
			return true;
312
		} else {
313
			return false;
314
		}
315
	}
316
	
317
	// Get POST data
318
	function get_post($field) {
319
		if(isset($_POST[$field])) {
320
			return $_POST[$field];
321
		} else {
322
			return null;
323
		}
324
	}
325
	
326
	// Get GET data
327
	function get_get($field) {
328
		if(isset($_GET[$field])) {
329
			return $_GET[$field];
330
		} else {
331
			return null;
332
		}
333
	}
334
	
335
	// Get SESSION data
336
	function get_session($field) {
337
		if(isset($_SESSION[$field])) {
338
			return $_SESSION[$field];
339
		} else {
340
			return null;
341
		}
342
	}
343
	
344
	// Get SERVER data
345
	function get_server($field) {
346
		if(isset($_SERVER[$field])) {
347
			return $_SERVER[$field];
348
		} else {
349
			return null;
350
		}
351
	}
352
	
353
	// Get the current users id
354
	function get_user_id() {
355
		return $_SESSION['USER_ID'];
356
	}
357
	
358
	// Get the current users group id
359
	function get_group_id() {
360
		return $_SESSION['GROUP_ID'];
361
	}
362
	
363
	// Get the current users group name
364
	function get_group_name() {
365
		return $_SESSION['GROUP_NAME'];
366
	}
367
	
368
	// Get the current users username
369
	function get_username() {
370
		return $_SESSION['USERNAME'];
371
	}
372
	
373
	// Get the current users display name
374
	function get_display_name() {
375
		return stripslashes($_SESSION['DISPLAY_NAME']);
376
	}
377
	
378
	// Get the current users email address
379
	function get_email() {
380
		return $_SESSION['EMAIL'];
381
	}
382
	
383
	// Get the current users home folder
384
	function get_home_folder() {
385
		return $_SESSION['HOME_FOLDER'];
386
	}
387
	
388
	// Get the current users timezone
389
	function get_timezone() {
390
		if(!isset($_SESSION['USE_DEFAULT_TIMEZONE'])) {
391
			return $_SESSION['TIMEZONE'];
392
		} else {
393
			return '-72000';
394
		}
395
	}
396
	
397
	// Validate supplied email address
398
	function validate_email($email) {
399
		if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
400
			return true;
401
		} else {
402
			return false;
403
		}
404
	}
405
	
406
}
407

    
408
?>
(1-1/7)