Project

General

Profile

1 4 ryan
<?php
2
3 18 stefan
// $Id$
4 4 ryan
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8 519 Ruebenwurz
 Copyright (C) 2004-2008, Ryan Djurovich
9 4 ryan
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 18 stefan
if(!defined('WB_URL')) {
36
	header('Location: ../index.php');
37 286 stefan
	exit(0);
38 18 stefan
}
39 4 ryan
40 5 stefan
require_once(WB_PATH.'/framework/class.wb.php');
41
42 4 ryan
// 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 55 stefan
54 5 stefan
class admin extends wb {
55 4 ryan
	// Authenticate user then auto print the header
56
	function admin($section_name, $section_permission = 'start', $auto_header = true, $auto_auth = true) {
57 55 stefan
		$this->wb();
58 4 ryan
		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 286 stefan
				exit(0);
68 4 ryan
			}
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 702 doc
75
		// Check if the backend language is also the selected language. If not, send headers again.
76
		global $database;
77
		$get_user_language = @$database->query("SELECT language FROM ".TABLE_PREFIX.
78
			"users WHERE user_id = '" .(int) $this->get_user_id() ."'");
79
		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
80
		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
81
		$user_language = substr($user_language[0],0,2);
82 749 doc
		// obtain the admin folder (e.g. /admin)
83
		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
84 745 doc
		if((LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
85 749 doc
			&& strpos($_SERVER['PHP_SELF'],$admin_folder.'/') !== false) {
86 745 doc
			// check if page_id is set
87
			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
88
			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
89 840 thorn
			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
90
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
91
			} else {
92
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url);
93
			}
94 702 doc
			exit();
95
		}
96
97 4 ryan
		// Auto header code
98
		if($auto_header == true) {
99
			$this->print_header();
100
		}
101
	}
102
103
	// Print the admin header
104
	function print_header($body_tags = '') {
105
		// Get vars from the language file
106
		global $MENU;
107
		global $MESSAGE;
108
		global $TEXT;
109
		// Connect to database and get website title
110 95 stefan
		global $database;
111 387 Ruebenwurz
		$get_title = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'website_title'");
112 4 ryan
		$title = $get_title->fetchRow();
113
		$header_template = new Template(ADMIN_PATH."/interface");
114
		$header_template->set_file('page', 'header.html');
115
		$header_template->set_block('page', 'header_block', 'header');
116 291 stefan
		if(defined('DEFAULT_CHARSET')) {
117
			$charset=DEFAULT_CHARSET;
118
		} else {
119
			$charset='utf-8';
120
		}
121 621 doc
122
		// work out the URL for the 'View menu' link in the WB backend
123
		// if the page_id is set, show this page otherwise show the root directory of WB
124
		$view_url = WB_URL;
125
		if(isset($_GET['page_id'])) {
126
			// extract page link from the database
127 656 thorn
			$result = @$database->query("SELECT link FROM " .TABLE_PREFIX ."pages WHERE page_id = '" .(int) addslashes($_GET['page_id']) ."'");
128 621 doc
			$row = @$result->fetchRow();
129
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
130
		}
131
132 4 ryan
		$header_template->set_var(	array(
133
													'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
134
													'BODY_TAGS' => $body_tags,
135 106 stefan
													'WEBSITE_TITLE' => ($title['value']),
136 4 ryan
													'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
137 291 stefan
													'CHARSET' => $charset,
138 458 doc
													'VERSION' => VERSION,
139
													'WB_URL' => WB_URL,
140 742 doc
													'ADMIN_URL' => ADMIN_URL,
141 621 doc
													'TITLE_START' => $MENU['START'],
142
													'TITLE_VIEW' => $MENU['VIEW'],
143
													'TITLE_HELP' => $MENU['HELP'],
144
													'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
145
													'URL_VIEW' => $view_url,
146
													'URL_HELP' => 'http://www.websitebaker.org/help/'.WB_VERSION,
147 458 doc
													'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
148
													'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
149 4 ryan
													)
150
											);
151 530 doc
152 4 ryan
		// Create the menu
153
		$menu = array(
154
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
155
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
156
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
157
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 0),
158
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
159 536 doc
					array(ADMIN_URL.'/admintools/index.php', '', $MENU['ADMINTOOLS'], 'admintools', 1),
160 621 doc
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1)
161 4 ryan
					);
162
		$header_template->set_block('header_block', 'linkBlock', 'link');
163
		foreach($menu AS $menu_item) {
164
			$link = $menu_item[0];
165
			$target = $menu_item[1];
166
			$title = $menu_item[2];
167
			$permission_title = $menu_item[3];
168
			$required = $menu_item[4];
169
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
170
			if($required == false OR $this->get_link_permission($permission_title)) {
171
				$header_template->set_var('LINK', $link);
172
				$header_template->set_var('TARGET', $target);
173
				// If link is the current section apply a class name
174
				if($permission_title == strtolower($this->section_name)) {
175
					$header_template->set_var('CLASS', 'current');
176
				} else {
177
					$header_template->set_var('CLASS', '');
178
				}
179
				$header_template->set_var('TITLE', $title);
180
				// Print link
181
				$header_template->parse('link', 'linkBlock', true);
182
			}
183
		}
184
		$header_template->parse('header', 'header_block', false);
185
		$header_template->pparse('output', 'page');
186
	}
187
188
	// Print the admin footer
189
	function print_footer() {
190 601 doc
		// include the required file for Javascript admin
191
    	if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
192
      	@include(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
193
    	}
194 4 ryan
		$footer_template = new Template(ADMIN_PATH."/interface");
195
		$footer_template->set_file('page', 'footer.html');
196
		$footer_template->set_block('page', 'footer_block', 'header');
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 906 doc
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
217 4 ryan
				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 318 stefan
232
	function get_user_details($user_id) {
233
		global $database;
234
		$query_user = "SELECT username,display_name FROM ".TABLE_PREFIX."users WHERE user_id = '$user_id'";
235
		$get_user = $database->query($query_user);
236
		if($get_user->numRows() != 0) {
237
			$user = $get_user->fetchRow();
238
		} else {
239
			$user['display_name'] = 'Unknown';
240
			$user['username'] = 'unknown';
241
		}
242
		return $user;
243
	}
244
245
	function get_page_details($page_id) {
246 321 stefan
		global $database;
247 318 stefan
		$query = "SELECT page_id,page_title,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
248
		$results = $database->query($query);
249
		if($database->is_error()) {
250
			$this->print_header();
251
			$this->print_error($database->get_error());
252
		}
253
		if($results->numRows() == 0) {
254
			$this->print_header();
255
			$this->print_error($MESSAGE['PAGES']['NOT_FOUND']);
256
		}
257
		$results_array = $results->fetchRow();
258
		return $results_array;
259
	}
260
261
	/** Function get_page_permission takes either a numerical page_id,
262
	 * upon which it looks up the permissions in the database,
263
	 * or an array with keys admin_groups and admin_users
264
	 */
265
	function get_page_permission($page,$action='admin') {
266
		if ($action!='viewing') $action='admin';
267
		$action_groups=$action.'_groups';
268
		$action_users=$action.'_users';
269
		if (is_array($page)) {
270
				$groups=$page[$action_groups];
271
				$users=$page[$action_users];
272
		} else {
273
			global $database;
274 321 stefan
			$results = $database->query("SELECT $action_groups,$action_users FROM ".TABLE_PREFIX."pages WHERE page_id = '$page'");
275 318 stefan
			$result = $results->fetchRow();
276
			$groups = explode(',', str_replace('_', '', $result[$action_groups]));
277
			$users = explode(',', str_replace('_', '', $result[$action_users]));
278
		}
279 546 doc
280
		$in_group = FALSE;
281
		foreach($this->get_groups_id() as $cur_gid){
282
		    if (in_array($cur_gid, $groups)) {
283
		        $in_group = TRUE;
284
		    }
285
		}
286
		if((!$in_group) AND !is_numeric(array_search($this->get_user_id(), $users))) {
287 318 stefan
			return false;
288
		}
289
		return true;
290
	}
291
292 5 stefan
293 4 ryan
	// Returns a system permission for a menu link
294
	function get_link_permission($title) {
295
		$title = str_replace('_blank', '', $title);
296
		$title = strtolower($title);
297
		// Set system permissions var
298
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
299
		// Set module permissions var
300
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
301
		if($title == 'start') {
302
			return true;
303
		} else {
304
			// Return true if system perm = 1
305
			if(is_numeric(array_search($title, $system_permissions))) {
306
				return true;
307
			} else {
308
				return false;
309
			}
310
		}
311
	}
312 458 doc
313
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
314
	function register_backend_modfiles($file_id="css") {
315
		// sanity check of parameter passed to the function
316
		$file_id = strtolower($file_id);
317
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
318
			return;
319
		}
320
321
		global $database;
322
		// define default baselink and filename for optional module javascript and stylesheet files
323
		$head_links = "";
324
		if($file_id == "css") {
325
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
326 794 doc
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
327 458 doc
			$base_file = "backend.css";
328
		} else {
329
			$base_link = '<script type="text/javascript" src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js"></script>';
330
			$base_file = "backend.js";
331
		}
332
333
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
334
		if(isset($_GET['tool'])) {
335
			// check if displayed page contains a installed admin tool
336
			$result = $database->query("SELECT * FROM " .TABLE_PREFIX ."addons
337 656 thorn
				WHERE type = 'module' AND function = 'tool' AND directory = '".addslashes($_GET['tool'])."'");
338 458 doc
339
			if($result->numRows()) {
340
				// check if admin tool directory contains a backend.js or backend.css file to include
341
				$tool = $result->fetchRow();
342
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
343
        			// return link to the backend.js or backend.css file
344
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
345
				}
346
			}
347
		} elseif(isset($_GET['page_id'])) {
348
			// check if displayed page in the backend contains a page module
349 656 thorn
			$page_id = (int) addslashes($_GET['page_id']);
350 458 doc
351
    		// gather information for all models embedded on actual page
352
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections
353 842 thorn
				WHERE page_id=$page_id");
354 458 doc
355
    		while($row = $query_modules->fetchRow()) {
356
				// check if page module directory contains a backend.js or backend.css file
357
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
358
					// create link with backend.js or backend.css source for the current module
359
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
360
        			// ensure that backend.js or backend.css is only added once per module type
361
        			if(strpos($head_links, $tmp_link) === false) {
362
						$head_links .= $tmp_link ."\n";
363
					}
364
				}
365
    		}
366
    		// write out links with all external module javascript/CSS files, remove last line feed
367
			return rtrim($head_links);
368
		}
369
	}
370 4 ryan
}
371
372 840 thorn
?>