Project

General

Profile

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

    
66
if(!defined('WB_URL')) {
67
	header('Location: ../index.php');
68
	exit(0);
69
}
70

    
71
require_once(WB_PATH.'/framework/class.wb.php');
72

    
73
// Include PHPLIB template class
74
require_once(WB_PATH."/include/phplib/template.inc");
75

    
76
// Get WB version
77
require_once(ADMIN_PATH.'/interface/version.php');
78

    
79
// Include EditArea wrapper functions
80
require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
81

    
82
/*
83
Begin user changeable settings
84
*/
85

    
86

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

    
130
		// Auto header code
131
		if($auto_header == true) {
132
			$this->print_header();
133
		}
134
	}
135
	
136
	// Print the admin header
137
	function print_header($body_tags = '') {
138
		// Get vars from the language file
139
		global $MENU;
140
		global $MESSAGE;
141
		global $TEXT;
142
		// Connect to database and get website title
143
		global $database;
144
		$get_title = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'website_title'");
145
		$title = $get_title->fetchRow();
146
		$header_template = new Template(THEME_PATH.'/templates');
147
		$header_template->set_file('page', 'header.htt');
148
		$header_template->set_block('page', 'header_block', 'header');
149
		if(defined('DEFAULT_CHARSET')) {
150
			$charset=DEFAULT_CHARSET;
151
		} else {
152
			$charset='utf-8';
153
		}
154

    
155
		// work out the URL for the 'View menu' link in the WB backend
156
		// if the page_id is set, show this page otherwise show the root directory of WB
157
		$view_url = WB_URL;
158
		if(isset($_GET['page_id'])) {
159
			// extract page link from the database
160
			$result = @$database->query("SELECT link FROM " .TABLE_PREFIX ."pages WHERE page_id = '" .(int) addslashes($_GET['page_id']) ."'");
161
			$row = @$result->fetchRow();
162
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
163
		}
164

    
165
		$header_template->set_var(	array(
166
													'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
167
													'BODY_TAGS' => $body_tags,
168
													'WEBSITE_TITLE' => ($title['value']),
169
													'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
170
													'CURRENT_USER' => $MESSAGE['START']['CURRENT_USER'],
171
													'DISPLAY_NAME' => $this->get_display_name(),
172
													'CHARSET' => $charset,
173
													'LANGUAGE' => strtolower(LANGUAGE),
174
													'VERSION' => VERSION,
175
													'REVISION' => REVISION,
176
													'WB_URL' => WB_URL,
177
													'ADMIN_URL' => ADMIN_URL,
178
													'THEME_URL' => THEME_URL,
179
													'TITLE_START' => $MENU['START'],
180
													'TITLE_VIEW' => $MENU['VIEW'],
181
													'TITLE_HELP' => $MENU['HELP'],
182
													'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
183
													'URL_VIEW' => $view_url,
184
													'URL_HELP' => 'http://help.websitebaker.org/',
185
													'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
186
													'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
187
													)
188
											);
189

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

    
325
		$in_group = FALSE;
326
		foreach($this->get_groups_id() as $cur_gid){
327
		    if (in_array($cur_gid, $groups)) {
328
		        $in_group = TRUE;
329
		    }
330
		}
331
		if((!$in_group) AND !is_numeric(array_search($this->get_user_id(), $users))) {
332
			return false;
333
		}
334
		return true;
335
	}
336
		
337

    
338
	// Returns a system permission for a menu link
339
	function get_link_permission($title) {
340
		$title = str_replace('_blank', '', $title);
341
		$title = strtolower($title);
342
		// Set system permissions var
343
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
344
		// Set module permissions var
345
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
346
		if($title == 'start') {
347
			return true;
348
		} else {
349
			// Return true if system perm = 1
350
			if(is_numeric(array_search($title, $system_permissions))) {
351
				return true;
352
			} else {
353
				return false;
354
			}
355
		}
356
	}
357

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

    
418

    
419
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
420
	function register_backend_modfiles($file_id="css") {
421
		// sanity check of parameter passed to the function
422
		$file_id = strtolower($file_id);
423
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
424
			return;
425
		}
426

    
427
		global $database;
428
		// define default baselink and filename for optional module javascript and stylesheet files
429
		$head_links = "";
430
		if($file_id == "css") {
431
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
432
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
433
			$base_file = "backend.css";
434
		} else {
435
			$base_link = '<script type="text/javascript" src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js"></script>';
436
			$base_file = "backend.js";
437
		}
438

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

    
445
			if($result->numRows()) {
446
				// check if admin tool directory contains a backend.js or backend.css file to include
447
				$tool = $result->fetchRow();
448
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
449
        			// return link to the backend.js or backend.css file
450
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
451
				}
452
			}
453
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id'])) {
454
			// check if displayed page in the backend contains a page module
455
			if (isset($_GET['page_id'])) {
456
				$page_id = (int) addslashes($_GET['page_id']);
457
			} else {
458
				$page_id = (int) addslashes($_POST['page_id']);
459
			}
460

    
461
    		// gather information for all models embedded on actual page
462
			$query_modules = $database->query("SELECT module FROM " .TABLE_PREFIX ."sections
463
				WHERE page_id=$page_id");
464

    
465
    		while($row = $query_modules->fetchRow()) {
466
				// check if page module directory contains a backend.js or backend.css file
467
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
468
					// create link with backend.js or backend.css source for the current module
469
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
470
        			// ensure that backend.js or backend.css is only added once per module type
471
        			if(strpos($head_links, $tmp_link) === false) {
472
						$head_links .= $tmp_link ."\n";
473
					}
474
				}
475
    		}
476
    		// write out links with all external module javascript/CSS files, remove last line feed
477
			return rtrim($head_links);
478
		}
479
	}
480
}
481

    
482
?>
(3-3/15)