Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framewotk
5
 * @package         backend admin
6
 * @author          Ryan Djurovich, WebsiteBaker Project
7
 * @copyright       2009-2011, Website Baker Org. e.V.
8
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id: class.admin.php 1678 2012-04-27 21:32:56Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2012-04-27 23:32:56 +0200 (Fri, 27 Apr 2012) $
15
 *
16
 */
17
/* -------------------------------------------------------- */
18
// Must include code to stop this file being accessed directly
19
if(!defined('WB_PATH')) {
20
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
21
	throw new IllegalFileException();
22
}
23
/* -------------------------------------------------------- */
24
require_once(WB_PATH.'/framework/class.wb.php');
25

    
26
// Get WB version
27
require_once(ADMIN_PATH.'/interface/version.php');
28

    
29
// Include EditArea wrapper functions
30
// require_once(WB_PATH . '/include/editarea/wb_wrapper_edit_area.php');
31
//require_once(WB_PATH . '/framework/SecureForm.php');
32

    
33

    
34
class admin extends wb {
35
	// Authenticate user then auto print the header
36
	public function __construct($section_name= '##skip##', $section_permission = 'start', $auto_header = true, $auto_auth = true)
37
	{
38
		parent::__construct(SecureForm::BACKEND);
39
	if( $section_name != '##skip##' )
40
	{
41
		global $database, $MESSAGE;
42
		// Specify the current applications name
43
		$this->section_name = $section_name;
44
		$this->section_permission = $section_permission;
45
		// Authenticate the user for this application
46
		if($auto_auth == true)
47
		{
48
			// First check if the user is logged-in
49
			if($this->is_authenticated() == false)
50
			{
51
				header('Location: '.ADMIN_URL.'/login/index.php');
52
				exit(0);
53
			}
54

    
55
			// Now check if they are allowed in this section
56
			if($this->get_permission($section_permission) == false) {
57
				die($MESSAGE['ADMIN']['INSUFFICIENT_PRIVELLIGES']);
58
			}
59
		}
60

    
61
		// Check if the backend language is also the selected language. If not, send headers again.
62
		$sql  = 'SELECT `language` FROM `'.TABLE_PREFIX.'users` ';
63
		$sql .= 'WHERE `user_id`='.(int)$this->get_user_id();
64
		$get_user_language = @$database->query($sql);
65
		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
66
		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
67
		$user_language = substr($user_language[0],0,2);
68
		// obtain the admin folder (e.g. /admin)
69
		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
70
		if((LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
71
			&& strpos($_SERVER['PHP_SELF'],$admin_folder.'/') !== false) {
72
			// check if page_id is set
73
			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
74
			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
75
			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
76
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
77
			} else {
78
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url);
79
			}
80
			exit();
81
		}
82

    
83
		// Auto header code
84
		if($auto_header == true) {
85
			$this->print_header();
86
		}
87
	}
88
	}
89

    
90
	// Print the admin header
91
	function print_header($body_tags = '') {
92
		// Get vars from the language file
93
		global $MENU, $MESSAGE, $TEXT;
94
		// Connect to database and get website title
95
		global $database;
96
		// $GLOBALS['FTAN'] = $this->getFTAN();
97
		$this->createFTAN();
98
		$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name`=\'website_title\'';
99
		$get_title = $database->query($sql);
100
		$title = $get_title->fetchRow();
101
		// Setup template object, parse vars to it, then parse it
102
		$header_template = new Template(dirname($this->correct_theme_source('header.htt')),'keep');
103
		$header_template->set_file('page', 'header.htt');
104
		$header_template->set_block('page', 'header_block', 'header');
105
		if(defined('DEFAULT_CHARSET')) {
106
			$charset=DEFAULT_CHARSET;
107
		} else {
108
			$charset='utf-8';
109
		}
110

    
111
		// work out the URL for the 'View menu' link in the WB backend
112
		// if the page_id is set, show this page otherwise show the root directory of WB
113
		$view_url = WB_URL;
114
		if(isset($_GET['page_id'])) {
115
			// extract page link from the database
116
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
117
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
118
			$result = @$database->query($sql);
119
			$row = @$result->fetchRow();
120
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
121
		}
122

    
123
		$header_template->set_var(	array(
124
							'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
125
							'BODY_TAGS' => $body_tags,
126
							'WEBSITE_TITLE' => ($title['value']),
127
							'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
128
							'CURRENT_USER' => $MESSAGE['START']['CURRENT_USER'],
129
							'DISPLAY_NAME' => $this->get_display_name(),
130
							'CHARSET' => $charset,
131
							'LANGUAGE' => strtolower(LANGUAGE),
132
							'VERSION' => VERSION,
133
							'SP' => (defined('SP') ? SP : ''),
134
							'REVISION' => REVISION,
135
							'SERVER_ADDR' => ((int)$this->get_user_id()==1 ? $_SERVER['SERVER_ADDR'] : ''),
136
							'WB_URL' => WB_URL,
137
							'ADMIN_URL' => ADMIN_URL,
138
							'THEME_URL' => THEME_URL,
139
							'TITLE_START' => $MENU['START'],
140
							'TITLE_VIEW' => $MENU['VIEW'],
141
							'TITLE_HELP' => $MENU['HELP'],
142
							'TITLE_LOGOUT' =>  $MENU['LOGOUT'],
143
							'URL_VIEW' => $view_url,
144
							'URL_HELP' => 'http://www.websitebaker2.org/',
145
							'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
146
							'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
147
						)
148
					);
149

    
150
		// Create the menu
151
		$menu = array(
152
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
153
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
154
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
155
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 0),
156
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
157
					array(ADMIN_URL.'/admintools/index.php', '', $MENU['ADMINTOOLS'], 'admintools', 1),
158
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1)
159
					);
160
		$header_template->set_block('header_block', 'linkBlock', 'link');
161
		foreach($menu AS $menu_item) {
162
			$link = $menu_item[0];
163
			$target = ($menu_item[1] == '') ? '_self' : $menu_item[1];
164
			$title = $menu_item[2];
165
			$permission_title = $menu_item[3];
166
			$required = $menu_item[4];
167
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
168
			if($required == false OR $this->get_link_permission($permission_title)) {
169
				$header_template->set_var('LINK', $link);
170
				$header_template->set_var('TARGET', $target);
171
				// If link is the current section apply a class name
172
				if($permission_title == strtolower($this->section_name)) {
173
					$header_template->set_var('CLASS', $menu_item[3] . ' current');
174
				} else {
175
					$header_template->set_var('CLASS', $menu_item[3]);
176
				}
177
				$header_template->set_var('TITLE', $title);
178
				// Print link
179
				$header_template->parse('link', 'linkBlock', true);
180
			}
181
		}
182
		$header_template->parse('header', 'header_block', false);
183
		$header_template->pparse('output', 'page');
184
	}
185
	
186
	// Print the admin footer
187
		function print_footer($activateJsAdmin = false) {
188
		global $database;
189
		// include the required file for Javascript admin
190
		if($activateJsAdmin != false) {
191
			if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
192
				@include_once(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
193
			}
194
		}
195

    
196
		// Setup template object, parse vars to it, then parse it
197
		$footer_template = new Template(dirname($this->correct_theme_source('footer.htt')));
198
		$footer_template->set_file('page', 'footer.htt');
199
		$footer_template->set_block('page', 'footer_block', 'header');
200
		$footer_template->set_var(array(
201
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
202
						'WB_URL' => WB_URL,
203
						'ADMIN_URL' => ADMIN_URL,
204
						'THEME_URL' => THEME_URL,
205
			 ) );
206

    
207
		$footer_template->set_block('footer_block', 'show_debug_block', 'show_debug');
208

    
209
		$bDebug = (defined('DEBUG') ? DEBUG : false);
210
		$bDevInfo = (defined('DEV_INFOS') && (DEV_INFOS == true) && (1 == $this->get_user_id()) ? true : false);
211
//         if( $debug && (1 == $this->get_user_id()))
212
        if( $bDevInfo )
213
		{
214
			$footer_template->set_var('MEMORY', number_format(memory_get_peak_usage(), 0, ',', '.').'&nbsp;Byte' );
215
			$footer_template->set_var('QUERIES', $database->getQueryCount );
216
			// $footer_template->set_var('QUERIES', 'disabled' );
217
	        $included_files =  get_included_files();
218
			$footer_template->set_var('INCLUDES', sizeof($included_files) );
219

    
220
			$sum_filesize = 0;
221
			$footer_template->set_block('show_debug_block', 'show_block_list', 'show_list');
222
			$footer_template->set_block('show_block_list', 'include_block_list', 'include_list');
223
			// $debug = true;
224
			foreach($included_files as $filename)
225
			{
226
				if(!is_readable($filename)) { continue; }
227
				if($bDebug)
228
				{
229
					$footer_template->set_var('INCLUDES_ARRAY', str_replace(WB_PATH, '',$filename) );
230
					$footer_template->set_var('FILESIZE', number_format(filesize($filename), 0, ',', '.').'&nbsp;Byte');
231
					$footer_template->parse('include_list', 'include_block_list', true);
232
				}
233
				$sum_filesize += filesize($filename);
234
			}
235
			$footer_template->parse('show_list', 'show_block_list', true);
236

    
237
			if(!$bDebug)
238
			{
239
				$footer_template->parse('include_list', '');
240
				$footer_template->parse('show_list', '');
241
			}
242

    
243
			$footer_template->set_var('FILESIZE', ini_get('memory_limit'));
244
			$footer_template->set_var('TXT_SUM_FILESIZE', 'Summary size of included files:&nbsp;');
245
			$footer_template->set_var('SUM_FILESIZE', number_format($sum_filesize, 0, ',', '.').'&nbsp;Byte');
246
			$footer_template->parse('show_debug', 'show_debug_block', true);
247
        } else {
248
			$footer_template->parse('show_debug', '');
249
        }
250
		$footer_template->parse('header', 'footer_block', false);
251
		$footer_template->pparse('output', 'page');
252
	}
253
	
254
	// Return a system permission
255
	function get_permission($name, $type = 'system') {
256
		// Append to permission type
257
		$type .= '_permissions';
258
		// Check if we have a section to check for
259
		if($name == 'start') {
260
			return true;
261
		} else {
262
			// Set system permissions var
263
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
264
			// Set module permissions var
265
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
266
			// Set template permissions var
267
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
268
			// Return true if system perm = 1
269
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
270
				if($type == 'system_permissions') {
271
					return true;
272
				} else {
273
					return false;
274
				}
275
			} else {
276
				if($type == 'system_permissions') {
277
					return false;
278
				} else {
279
					return true;
280
				}
281
			}
282
		}
283
	}
284
/*
285
	function get_user_details($user_id) {
286
		global $database;
287
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'users` ';
288
		$sql .= 'WHERE `user_id`='.(int)$user_id.' LIMIT 1';
289
		if(($resUser = $database->query($sql))){
290
			if(!($recUser = $resUser->fetchRow())) {
291
				$recUser['display_name'] = 'Unknown';
292
				$recUser['username'] = 'unknown';
293
			}
294
		}
295
		return $recUser;
296
	}
297
*/
298
 function get_user_details($user_id) {
299
  global $database;
300
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
301
  $sql  = 'SELECT `username`,`display_name`,`email` ';
302
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
303
  $sql .= 'WHERE `user_id`='.(int)$user_id;
304
  if( ($resUsers = $database->query($sql)) ) {
305
   if( ($recUser = $resUsers->fetchRow()) ) {
306
    $retval = $recUser;
307
   }
308
  }
309
  return $retval;
310
 }
311

    
312
    //
313
	function get_section_details( $section_id, $backLink = 'index.php' ) {
314
	global $database, $TEXT;
315
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'sections` ';
316
		$sql .= 'WHERE `section_id`='.intval($section_id);
317
		if(($resSection = $database->query($sql))){
318
			if(!($recSection = $resSection->fetchRow())) {
319
				$this->print_header();
320
				$this->print_error($TEXT['SECTION'].' '.$TEXT['NOT_FOUND'], $backLink, true);
321
			}
322
			} else {
323
				$this->print_header();
324
				$this->print_error($database->get_error(), $backLink, true);
325
			}
326
		return $recSection;
327
	}
328

    
329
	function get_page_details( $page_id, $backLink = 'index.php' ) {
330
		global $database, $TEXT;
331
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'pages` ';
332
		$sql .= 'WHERE `page_id`='.intval($page_id);
333
		if(($resPages = $database->query($sql))){
334
			if(!($recPage = $resPages->fetchRow())) {
335
			$this->print_header();
336
			$this->print_error($TEXT['PAGE'].' '.$TEXT['NOT_FOUND'], $backLink, true);
337
			}
338
		} else {
339
			$this->print_header();
340
			$this->print_error($database->get_error(), $backLink, true);
341
		}
342
		return $recPage;
343
	}
344

    
345
	function get_page_permission($page,$action='admin') {
346
		if($action != 'viewing') { $action = 'admin'; }
347
		$action_groups = $action.'_groups';
348
		$action_users  = $action.'_users';
349
		$groups = $users = '0';
350
		if(is_array($page)) {
351
			$groups = $page[$action_groups];
352
			$users  = $page[$action_users];
353
		} else {
354
			global $database;
355
			$sql  = 'SELECT `'.$action_groups.'`,`'.$action_users.'` ';
356
			$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
357
			$sql .= 'WHERE `page_id`='.(int)$page;
358
			if( ($res = $database->query($sql)) ) {
359
				if( ($rec = $res->fetchRow()) ) {
360
					$groups = $rec[$action_groups];
361
					$users  = $rec[$action_users];
362
				}
363
			}
364
		}
365
		return ($this->ami_group_member($groups) || $this->is_group_match($this->get_user_id(), $users));
366
	}
367

    
368
	// Returns a system permission for a menu link
369
	function get_link_permission($title) {
370
		$title = str_replace('_blank', '', $title);
371
		$title = strtolower($title);
372
		// Set system permissions var
373
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
374
		// Set module permissions var
375
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
376
		if($title == 'start') {
377
			return true;
378
		} else {
379
			// Return true if system perm = 1
380
			if(is_numeric(array_search($title, $system_permissions))) {
381
				return true;
382
			} else {
383
				return false;
384
			}
385
		}
386
	}
387

    
388
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
389
	function register_backend_modfiles_body($file_id="js")
390
		{
391
		// sanity check of parameter passed to the function
392
		$file_id = strtolower($file_id);
393
		if($file_id !== "javascript" && $file_id !== "js")
394
		{
395
			return;
396
		}
397
		global $database;
398
        $body_links = "";
399
		// define default baselink and filename for optional module javascript and stylesheet files
400
		if($file_id == "js") {
401
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
402
			$base_file = "backend_body.js";
403
		}
404
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
405
		if(isset($_GET['tool']))
406
			{
407
			// check if displayed page contains a installed admin tool
408
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
409
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
410
			$result = $database->query($sql);
411
			if($result->numRows())
412
				{
413
				// check if admin tool directory contains a backend_body.js file to include
414
				$tool = $result->fetchRow();
415
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
416
				{
417
					// return link to the backend_body.js file
418
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
419
				}
420
			}
421
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
422
		{
423
			// check if displayed page in the backend contains a page module
424
			if (isset($_GET['page_id']))
425
			{
426
				$page_id = (int) addslashes($_GET['page_id']);
427
			} else {
428
				$page_id = (int) addslashes($_POST['page_id']);
429
			}
430
			// gather information for all models embedded on actual page
431
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
432
			$query_modules = $database->query($sql);
433
			while($row = $query_modules->fetchRow()) {
434
				// check if page module directory contains a backend_body.js file
435
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
436
					// create link with backend_body.js source for the current module
437
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
438
					// ensure that backend_body.js is only added once per module type
439
					if(strpos($body_links, $tmp_link) === false) {
440
						$body_links .= $tmp_link ."\n";
441
					}
442
				}
443
			}
444
			// write out links with all external module javascript/CSS files, remove last line feed
445
			return rtrim($body_links);
446
		}
447
	}
448

    
449

    
450
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
451
	function register_backend_modfiles($file_id="css") {
452
		// sanity check of parameter passed to the function
453
		$file_id = strtolower($file_id);
454
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
455
			return;
456
		}
457

    
458
		global $database;
459
		// define default baselink and filename for optional module javascript and stylesheet files
460
		$head_links = "";
461
		if($file_id == "css") {
462
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
463
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
464
			$base_file = "backend.css";
465
		} else {
466
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
467
			$base_file = "backend.js";
468
		}
469

    
470
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
471
		if(isset($_GET['tool'])) {
472
			// check if displayed page contains a installed admin tool
473
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
474
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
475
			$result = $database->query($sql);
476
			if($result->numRows()) {
477
				// check if admin tool directory contains a backend.js or backend.css file to include
478
				$tool = $result->fetchRow();
479
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
480
        			// return link to the backend.js or backend.css file
481
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
482
				}
483
			}
484
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
485
			// check if displayed page in the backend contains a page module
486
			if (isset($_GET['page_id'])) {
487
				$page_id = (int)$_GET['page_id'];
488
			} else {
489
				$page_id = (int)$_POST['page_id'];
490
			}
491

    
492
    		// gather information for all models embedded on actual page
493
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
494
			$query_modules = $database->query($sql);
495

    
496
    		while($row = $query_modules->fetchRow()) {
497
				// check if page module directory contains a backend.js or backend.css file
498
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
499
					// create link with backend.js or backend.css source for the current module
500
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
501
        			// ensure that backend.js or backend.css is only added once per module type
502
        			if(strpos($head_links, $tmp_link) === false) {
503
						$head_links .= $tmp_link ."\n";
504
					}
505
				}
506
    		}
507
    		// write out links with all external module javascript/CSS files, remove last line feed
508
			return rtrim($head_links);
509
		}
510
	}
511
}
512

    
513
?>
(7-7/22)