Project

General

Profile

1
<?php
2
/*
3
*
4
*                       About WebsiteBaker
5
*
6
* Website Baker is a PHP-based Content Management System (CMS)
7
* designed with one goal in mind: to enable its users to produce websites
8
* with ease.
9
*
10
*                       LICENSE INFORMATION
11
*
12
* WebsiteBaker is free software; you can redistribute it and/or
13
* modify it under the terms of the GNU General Public License
14
* as published by the Free Software Foundation; either version 2
15
* of the License, or (at your option) any later version.
16
*
17
* WebsiteBaker is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20
* See the GNU General Public License for more details.
21
*
22
* You should have received a copy of the GNU General Public License
23
* along with this program; if not, write to the Free Software
24
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
*
26
*                   WebsiteBaker Extra Information
27
*
28
*
29
*/
30
/**
31
 *
32
 * @category        frontend
33
 * @package         account
34
 * @author          WebsiteBaker Project
35
 * @copyright       2004-2009, Ryan Djurovich
36
 * @copyright       2009-2010, Website Baker Org. e.V.
37
 * @link			http://www.websitebaker2.org/
38
 * @license         http://www.gnu.org/licenses/gpl.html
39
 * @platform        WebsiteBaker 2.8.x
40
 * @requirements    PHP 4.3.4 and higher
41
 * @version         $Id: class.admin.php 1269 2010-01-22 22:51:34Z Luisehahne $
42
 * @filesource		$HeadURL:  $
43
 * @lastmodified    $Date: $
44
 *
45
 */
46

    
47
if(!defined('WB_URL')) {
48
	header('Location: ../index.php');
49
	exit(0);
50
}
51

    
52
require_once(WB_PATH.'/framework/class.wb.php');
53

    
54
// Include PHPLIB template class
55
require_once(WB_PATH."/include/phplib/template.inc");
56

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

    
60
// Include EditArea wrapper functions
61
require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
62

    
63
/*
64
Begin user changeable settings
65
*/
66

    
67

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

    
111
		// Auto header code
112
		if($auto_header == true) {
113
			$this->print_header();
114
		}
115
	}
116
	
117
	// Print the admin header
118
	function print_header($body_tags = '') {
119
		// Get vars from the language file
120
		global $MENU;
121
		global $MESSAGE;
122
		global $TEXT;
123
		// Connect to database and get website title
124
		global $database;
125
		$get_title = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'website_title'");
126
		$title = $get_title->fetchRow();
127
		$header_template = new Template(THEME_PATH.'/templates');
128
		$header_template->set_file('page', 'header.htt');
129
		$header_template->set_block('page', 'header_block', 'header');
130
		if(defined('DEFAULT_CHARSET')) {
131
			$charset=DEFAULT_CHARSET;
132
		} else {
133
			$charset='utf-8';
134
		}
135

    
136
		// work out the URL for the 'View menu' link in the WB backend
137
		// if the page_id is set, show this page otherwise show the root directory of WB
138
		$view_url = WB_URL;
139
		if(isset($_GET['page_id'])) {
140
			// extract page link from the database
141
			$result = @$database->query("SELECT link FROM " .TABLE_PREFIX ."pages WHERE page_id = '" .(int) addslashes($_GET['page_id']) ."'");
142
			$row = @$result->fetchRow();
143
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
144
		}
145

    
146
		$header_template->set_var(	array(
147
													'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
148
													'BODY_TAGS' => $body_tags,
149
													'WEBSITE_TITLE' => ($title['value']),
150
													'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
151
													'CURRENT_USER' => $MESSAGE['START']['CURRENT_USER'],
152
													'DISPLAY_NAME' => $this->get_display_name(),
153
													'CHARSET' => $charset,
154
													'LANGUAGE' => strtolower(LANGUAGE),
155
													'VERSION' => VERSION,
156
													'REVISION' => REVISION,
157
													'WB_URL' => WB_URL,
158
													'ADMIN_URL' => ADMIN_URL,
159
													'THEME_URL' => THEME_URL,
160
													'TITLE_START' => $MENU['START'],
161
													'TITLE_VIEW' => $MENU['VIEW'],
162
													'TITLE_HELP' => $MENU['HELP'],
163
													'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
164
													'URL_VIEW' => $view_url,
165
													'URL_HELP' => 'http://help.websitebaker.org/',
166
													'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
167
													'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
168
													)
169
											);
170

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

    
306
		$in_group = FALSE;
307
		foreach($this->get_groups_id() as $cur_gid){
308
		    if (in_array($cur_gid, $groups)) {
309
		        $in_group = TRUE;
310
		    }
311
		}
312
		if((!$in_group) AND !is_numeric(array_search($this->get_user_id(), $users))) {
313
			return false;
314
		}
315
		return true;
316
	}
317
		
318

    
319
	// Returns a system permission for a menu link
320
	function get_link_permission($title) {
321
		$title = str_replace('_blank', '', $title);
322
		$title = strtolower($title);
323
		// Set system permissions var
324
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
325
		// Set module permissions var
326
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
327
		if($title == 'start') {
328
			return true;
329
		} else {
330
			// Return true if system perm = 1
331
			if(is_numeric(array_search($title, $system_permissions))) {
332
				return true;
333
			} else {
334
				return false;
335
			}
336
		}
337
	}
338

    
339
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
340
	function register_backend_modfiles_body($file_id="js")
341
		{
342
		// sanity check of parameter passed to the function
343
		$file_id = strtolower($file_id);
344
		if($file_id !== "javascript" && $file_id !== "js")
345
		{
346
			return;
347
		}
348
		global $database;
349
        $body_links = "";
350
		// define default baselink and filename for optional module javascript and stylesheet files
351
		if($file_id == "js") {
352
			$base_link = '<script type="text/javascript" src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js"></script>';
353
			$base_file = "backend_body.js";
354
		}
355
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
356
		if(isset($_GET['tool']))
357
			{
358
			// check if displayed page contains a installed admin tool
359
			$result = $database->query("SELECT * FROM " .TABLE_PREFIX ."addons
360
				WHERE type = 'module' AND function = 'tool' AND directory = '".addslashes($_GET['tool'])."'");
361
			if($result->numRows())
362
				{
363
				// check if admin tool directory contains a backend_body.js file to include
364
				$tool = $result->fetchRow();
365
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
366
				{
367
					// return link to the backend_body.js file
368
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
369
				}
370
			}
371
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
372
		{
373
			// check if displayed page in the backend contains a page module
374
			if (isset($_GET['page_id']))
375
			{
376
				$page_id = (int) addslashes($_GET['page_id']);
377
			} else {
378
				$page_id = (int) addslashes($_POST['page_id']);
379
			}
380
			// gather information for all models embedded on actual page
381
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections
382
				WHERE page_id=$page_id");
383
			while($row = $query_modules->fetchRow()) {
384
				// check if page module directory contains a backend_body.js file
385
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
386
					// create link with backend_body.js source for the current module
387
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
388
					// ensure that backend_body.js is only added once per module type
389
					if(strpos($body_links, $tmp_link) === false) {
390
						$body_links .= $tmp_link ."\n";
391
					}
392
				}
393
			}
394
			// write out links with all external module javascript/CSS files, remove last line feed
395
			return rtrim($body_links);
396
		}
397
	}
398

    
399

    
400
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
401
	function register_backend_modfiles($file_id="css") {
402
		// sanity check of parameter passed to the function
403
		$file_id = strtolower($file_id);
404
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
405
			return;
406
		}
407

    
408
		global $database;
409
		// define default baselink and filename for optional module javascript and stylesheet files
410
		$head_links = "";
411
		if($file_id == "css") {
412
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
413
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
414
			$base_file = "backend.css";
415
		} else {
416
			$base_link = '<script type="text/javascript" src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js"></script>';
417
			$base_file = "backend.js";
418
		}
419

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

    
426
			if($result->numRows()) {
427
				// check if admin tool directory contains a backend.js or backend.css file to include
428
				$tool = $result->fetchRow();
429
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
430
        			// return link to the backend.js or backend.css file
431
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
432
				}
433
			}
434
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id'])) {
435
			// check if displayed page in the backend contains a page module
436
			if (isset($_GET['page_id'])) {
437
				$page_id = (int) addslashes($_GET['page_id']);
438
			} else {
439
				$page_id = (int) addslashes($_POST['page_id']);
440
			}
441

    
442
    		// gather information for all models embedded on actual page
443
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections
444
				WHERE page_id=$page_id");
445

    
446
    		while($row = $query_modules->fetchRow()) {
447
				// check if page module directory contains a backend.js or backend.css file
448
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
449
					// create link with backend.js or backend.css source for the current module
450
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
451
        			// ensure that backend.js or backend.css is only added once per module type
452
        			if(strpos($head_links, $tmp_link) === false) {
453
						$head_links .= $tmp_link ."\n";
454
					}
455
				}
456
    		}
457
    		// write out links with all external module javascript/CSS files, remove last line feed
458
			return rtrim($head_links);
459
		}
460
	}
461
}
462

    
463
?>
(3-3/15)