Project

General

Profile

1
<?php
2

    
3
// $Id: class.admin.php 909 2009-01-13 19:08:52Z doc $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, 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
// Get WB version
46
require_once(ADMIN_PATH.'/interface/version.php');
47

    
48
// Include EditArea wrapper functions
49
require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
50

    
51
/*
52
Begin user changeable settings
53
*/
54

    
55

    
56
class admin extends wb {
57
	// Authenticate user then auto print the header
58
	function admin($section_name, $section_permission = 'start', $auto_header = true, $auto_auth = true) {
59
		$this->wb();
60
		global $MESSAGE;
61
		// Specify the current applications name
62
		$this->section_name = $section_name;
63
		$this->section_permission = $section_permission;
64
		// Authenticate the user for this application
65
		if($auto_auth == true) {
66
			// First check if the user is logged-in
67
			if($this->is_authenticated() == false) {
68
				header('Location: '.ADMIN_URL.'/login/index.php');
69
				exit(0);
70
			}
71
			// Now check if they are allowed in this section
72
			if($this->get_permission($section_permission) == false) {
73
				die($MESSAGE['ADMIN']['INSUFFICIENT_PRIVELLIGES']);
74
			}
75
		}
76
		
77
		// Check if the backend language is also the selected language. If not, send headers again.
78
		global $database;
79
		$get_user_language = @$database->query("SELECT language FROM ".TABLE_PREFIX.
80
			"users WHERE user_id = '" .(int) $this->get_user_id() ."'");
81
		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
82
		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
83
		$user_language = substr($user_language[0],0,2);
84
		// obtain the admin folder (e.g. /admin)
85
		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
86
		if((LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
87
			&& strpos($_SERVER['PHP_SELF'],$admin_folder.'/') !== false) {
88
			// check if page_id is set
89
			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
90
			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
91
			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
92
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
93
			} else {
94
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url);
95
			}
96
			exit();
97
		}
98

    
99
		// Auto header code
100
		if($auto_header == true) {
101
			$this->print_header();
102
		}
103
	}
104
	
105
	// Print the admin header
106
	function print_header($body_tags = '') {
107
		// Get vars from the language file
108
		global $MENU;
109
		global $MESSAGE;
110
		global $TEXT;
111
		// Connect to database and get website title
112
		global $database;
113
		$get_title = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'website_title'");
114
		$title = $get_title->fetchRow();
115
		$header_template = new Template(ADMIN_PATH."/interface");
116
		$header_template->set_file('page', 'header.html');
117
		$header_template->set_block('page', 'header_block', 'header');
118
		if(defined('DEFAULT_CHARSET')) {
119
			$charset=DEFAULT_CHARSET;
120
		} else {
121
			$charset='utf-8';
122
		}
123

    
124
		// work out the URL for the 'View menu' link in the WB backend
125
		// if the page_id is set, show this page otherwise show the root directory of WB
126
		$view_url = WB_URL;
127
		if(isset($_GET['page_id'])) {
128
			// extract page link from the database
129
			$result = @$database->query("SELECT link FROM " .TABLE_PREFIX ."pages WHERE page_id = '" .(int) addslashes($_GET['page_id']) ."'");
130
			$row = @$result->fetchRow();
131
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
132
		}
133

    
134
		$header_template->set_var(	array(
135
													'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
136
													'BODY_TAGS' => $body_tags,
137
													'WEBSITE_TITLE' => ($title['value']),
138
													'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
139
													'CHARSET' => $charset,
140
													'VERSION' => VERSION,
141
													'WB_URL' => WB_URL,
142
													'ADMIN_URL' => ADMIN_URL,
143
													'TITLE_START' => $MENU['START'],
144
													'TITLE_VIEW' => $MENU['VIEW'],
145
													'TITLE_HELP' => $MENU['HELP'],
146
													'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
147
													'URL_VIEW' => $view_url,
148
													'URL_HELP' => 'http://www.websitebaker.org/help/'.WB_VERSION,
149
													'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
150
													'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
151
													)
152
											);
153

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

    
282
		$in_group = FALSE;
283
		foreach($this->get_groups_id() as $cur_gid){
284
		    if (in_array($cur_gid, $groups)) {
285
		        $in_group = TRUE;
286
		    }
287
		}
288
		if((!$in_group) AND !is_numeric(array_search($this->get_user_id(), $users))) {
289
			return false;
290
		}
291
		return true;
292
	}
293
		
294

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

    
315
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
316
	function register_backend_modfiles($file_id="css") {
317
		// sanity check of parameter passed to the function
318
		$file_id = strtolower($file_id);
319
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") { 
320
			return;
321
		}
322

    
323
		global $database;
324
		// define default baselink and filename for optional module javascript and stylesheet files
325
		$head_links = "";
326
		if($file_id == "css") {
327
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"'; 
328
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
329
			$base_file = "backend.css";
330
		} else {
331
			$base_link = '<script type="text/javascript" src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js"></script>';
332
			$base_file = "backend.js";
333
		}
334

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

    
341
			if($result->numRows()) {
342
				// check if admin tool directory contains a backend.js or backend.css file to include
343
				$tool = $result->fetchRow();
344
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
345
        			// return link to the backend.js or backend.css file
346
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
347
				}
348
			}
349
		} elseif(isset($_GET['page_id'])) {
350
			// check if displayed page in the backend contains a page module
351
			$page_id = (int) addslashes($_GET['page_id']);
352

    
353
    		// gather information for all models embedded on actual page
354
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections 
355
				WHERE page_id=$page_id");
356

    
357
    		while($row = $query_modules->fetchRow()) {
358
				// check if page module directory contains a backend.js or backend.css file
359
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
360
					// create link with backend.js or backend.css source for the current module
361
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
362
        			// ensure that backend.js or backend.css is only added once per module type
363
        			if(strpos($head_links, $tmp_link) === false) {
364
						$head_links .= $tmp_link ."\n";
365
					}
366
				}
367
    		}
368
    		// write out links with all external module javascript/CSS files, remove last line feed
369
			return rtrim($head_links);
370
		}
371
	}
372
}
373

    
374
?>
(2-2/14)