Project

General

Profile

1
<?php
2

    
3
// $Id: class.admin.php 794 2008-04-03 17:15:11Z 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
		
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
		// obtain the admin folder (e.g. /admin)
83
		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
84
		if((LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
85
			&& strpos($_SERVER['PHP_SELF'],$admin_folder.'/') !== false) {
86
			// 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
			header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url);
90
			exit();
91
		}
92

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

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

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

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

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

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

    
309
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
310
	function register_backend_modfiles($file_id="css") {
311
		// sanity check of parameter passed to the function
312
		$file_id = strtolower($file_id);
313
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") { 
314
			return;
315
		}
316

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

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

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

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

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

    
368
?>
(2-2/14)