Project

General

Profile

1
<?php
2

    
3
// $Id: class.admin.php 530 2008-01-12 16:39:59Z doc $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, 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
if(!defined('WB_URL')) {
36
	header('Location: ../index.php');
37
	exit(0);
38
}
39

    
40
require_once(WB_PATH.'/framework/class.wb.php');
41

    
42
// Include PHPLIB template class
43
require_once(WB_PATH."/include/phplib/template.inc");
44

    
45

    
46
// Get WB version
47
require_once(ADMIN_PATH.'/interface/version.php');
48

    
49
/*
50
Begin user changeable settings
51
*/
52

    
53

    
54
class admin extends wb {
55
	// Authenticate user then auto print the header
56
	function admin($section_name, $section_permission = 'start', $auto_header = true, $auto_auth = true) {
57
		$this->wb();
58
		global $MESSAGE;
59
		// Specify the current applications name
60
		$this->section_name = $section_name;
61
		$this->section_permission = $section_permission;
62
		// Authenticate the user for this application
63
		if($auto_auth == true) {
64
			// First check if the user is logged-in
65
			if($this->is_authenticated() == false) {
66
				header('Location: '.ADMIN_URL.'/login/index.php');
67
				exit(0);
68
			}
69
			// Now check if they are allowed in this section
70
			if($this->get_permission($section_permission) == false) {
71
				die($MESSAGE['ADMIN']['INSUFFICIENT_PRIVELLIGES']);
72
			}
73
		}
74
		// Auto header code
75
		if($auto_header == true) {
76
			$this->print_header();
77
		}
78
	}
79
	
80
	// Print the admin header
81
	function print_header($body_tags = '') {
82
		// Get vars from the language file
83
		global $MENU;
84
		global $MESSAGE;
85
		global $TEXT;
86
		// Connect to database and get website title
87
		global $database;
88
		$get_title = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'website_title'");
89
		$title = $get_title->fetchRow();
90
		$header_template = new Template(ADMIN_PATH."/interface");
91
		$header_template->set_file('page', 'header.html');
92
		$header_template->set_block('page', 'header_block', 'header');
93
		if(defined('DEFAULT_CHARSET')) {
94
			$charset=DEFAULT_CHARSET;
95
		} else {
96
			$charset='utf-8';
97
		}
98
		$header_template->set_var(	array(
99
													'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
100
													'INTERFACE_DIR' => ADMIN_URL.'/interface',
101
													'BODY_TAGS' => $body_tags,
102
													'WEBSITE_TITLE' => ($title['value']),
103
													'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
104
													'CHARSET' => $charset,
105
													'VERSION' => VERSION,
106
													'WB_URL' => WB_URL,
107
													'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
108
													'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
109
													)
110
											);
111

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

    
122
		// Create the menu
123
		$menu = array(
124
					array(ADMIN_URL.'/start/index.php', '', $MENU['START'], 'start', 0),
125
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
126
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
127
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
128
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 0),
129
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
130
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1),
131
					array('http://www.websitebaker.org/help/'.WB_VERSION, '_blank', $MENU['HELP'], 'help', 0),
132
					array($view_url, '_blank', $MENU['VIEW'], 'view', 0),
133
					array(ADMIN_URL.'/logout/index.php', '', $MENU['LOGOUT'], 'logout', 0)
134
					);
135
		$header_template->set_block('header_block', 'linkBlock', 'link');
136
		foreach($menu AS $menu_item) {
137
			$link = $menu_item[0];
138
			$target = $menu_item[1];
139
			$title = $menu_item[2];
140
			$permission_title = $menu_item[3];
141
			$required = $menu_item[4];
142
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
143
			if($required == false OR $this->get_link_permission($permission_title)) {
144
				$header_template->set_var('LINK', $link);
145
				$header_template->set_var('TARGET', $target);
146
				// If link is the current section apply a class name
147
				if($permission_title == strtolower($this->section_name)) {
148
					$header_template->set_var('CLASS', 'current');
149
				} else {
150
					$header_template->set_var('CLASS', '');
151
				}
152
				$header_template->set_var('TITLE', $title);
153
				// Print link
154
				$header_template->parse('link', 'linkBlock', true);
155
			}
156
		}
157
		$header_template->parse('header', 'header_block', false);
158
		$header_template->pparse('output', 'page');
159
	}
160
	
161
	// Print the admin footer
162
	function print_footer() {
163
		$footer_template = new Template(ADMIN_PATH."/interface");
164
		$footer_template->set_file('page', 'footer.html');
165
		$footer_template->set_block('page', 'footer_block', 'header');
166
		$footer_template->parse('header', 'footer_block', false);
167
		$footer_template->pparse('output', 'page');
168
	}
169
	
170
	// Return a system permission
171
	function get_permission($name, $type = 'system') {
172
		// Append to permission type
173
		$type .= '_permissions';
174
		// Check if we have a section to check for
175
		if($name == 'start') {
176
			return true;
177
		} else {
178
			// Set system permissions var
179
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
180
			// Set module permissions var
181
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
182
			// Set template permissions var
183
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
184
			// Return true if system perm = 1
185
			if(is_numeric(array_search($name, $$type))) {
186
				if($type == 'system_permissions') {
187
					return true;
188
				} else {
189
					return false;
190
				}
191
			} else {
192
				if($type == 'system_permissions') {
193
					return false;
194
				} else {
195
					return true;
196
				}
197
			}
198
		}
199
	}
200
		
201
	function get_user_details($user_id) {
202
		global $database;
203
		$query_user = "SELECT username,display_name FROM ".TABLE_PREFIX."users WHERE user_id = '$user_id'";
204
		$get_user = $database->query($query_user);
205
		if($get_user->numRows() != 0) {
206
			$user = $get_user->fetchRow();
207
		} else {
208
			$user['display_name'] = 'Unknown';
209
			$user['username'] = 'unknown';
210
		}
211
		return $user;
212
	}	
213
	
214
	function get_page_details($page_id) {
215
		global $database;
216
		$query = "SELECT page_id,page_title,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
217
		$results = $database->query($query);
218
		if($database->is_error()) {
219
			$this->print_header();
220
			$this->print_error($database->get_error());
221
		}
222
		if($results->numRows() == 0) {
223
			$this->print_header();
224
			$this->print_error($MESSAGE['PAGES']['NOT_FOUND']);
225
		}
226
		$results_array = $results->fetchRow();
227
		return $results_array;
228
	}	
229
	
230
	/** Function get_page_permission takes either a numerical page_id,
231
	 * upon which it looks up the permissions in the database,
232
	 * or an array with keys admin_groups and admin_users  
233
	 */
234
	function get_page_permission($page,$action='admin') {
235
		if ($action!='viewing') $action='admin';
236
		$action_groups=$action.'_groups';
237
		$action_users=$action.'_users';
238
		if (is_array($page)) {
239
				$groups=$page[$action_groups];
240
				$users=$page[$action_users];
241
		} else {				
242
			global $database;
243
			$results = $database->query("SELECT $action_groups,$action_users FROM ".TABLE_PREFIX."pages WHERE page_id = '$page'");
244
			$result = $results->fetchRow();
245
			$groups = explode(',', str_replace('_', '', $result[$action_groups]));
246
			$users = explode(',', str_replace('_', '', $result[$action_users]));
247
		}
248
		if(!is_numeric(array_search($this->get_group_id(), $groups)) AND !is_numeric(array_search($this->get_user_id(), $users))) {
249
			return false;
250
		}
251
		return true;
252
	}
253
		
254

    
255
	// Returns a system permission for a menu link
256
	function get_link_permission($title) {
257
		$title = str_replace('_blank', '', $title);
258
		$title = strtolower($title);
259
		// Set system permissions var
260
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
261
		// Set module permissions var
262
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
263
		if($title == 'start') {
264
			return true;
265
		} else {
266
			// Return true if system perm = 1
267
			if(is_numeric(array_search($title, $system_permissions))) {
268
				return true;
269
			} else {
270
				return false;
271
			}
272
		}
273
	}
274

    
275
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
276
	function register_backend_modfiles($file_id="css") {
277
		// sanity check of parameter passed to the function
278
		$file_id = strtolower($file_id);
279
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") { 
280
			return;
281
		}
282

    
283
		global $database;
284
		// define default baselink and filename for optional module javascript and stylesheet files
285
		$head_links = "";
286
		if($file_id == "css") {
287
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"'; 
288
			$base_link.= 'rel="stylesheet" type="text/css" media="screen" />';
289
			$base_file = "backend.css";
290
		} else {
291
			$base_link = '<script type="text/javascript" src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js"></script>';
292
			$base_file = "backend.js";
293
		}
294

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

    
301
			if($result->numRows()) {
302
				// check if admin tool directory contains a backend.js or backend.css file to include
303
				$tool = $result->fetchRow();
304
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
305
        			// return link to the backend.js or backend.css file
306
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
307
				}
308
			}
309
		} elseif(isset($_GET['page_id'])) {
310
			// check if displayed page in the backend contains a page module
311
			$page_id = (int) $_GET['page_id'];
312

    
313
    		// gather information for all models embedded on actual page
314
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections 
315
				WHERE page_id=$page_id AND module<>'wysiwyg'");
316

    
317
    		while($row = $query_modules->fetchRow()) {
318
				// check if page module directory contains a backend.js or backend.css file
319
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
320
					// create link with backend.js or backend.css source for the current module
321
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
322
        			// ensure that backend.js or backend.css is only added once per module type
323
        			if(strpos($head_links, $tmp_link) === false) {
324
						$head_links .= $tmp_link ."\n";
325
					}
326
				}
327
    		}
328
    		// write out links with all external module javascript/CSS files, remove last line feed
329
			return rtrim($head_links);
330
		}
331
	}
332
}
333

    
334
?>
(2-2/13)