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 1674 2012-04-20 18:50:29Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2012-04-20 20:50:29 +0200 (Fri, 20 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;
94
		global $MESSAGE;
95
		global $TEXT;
96
		// Connect to database and get website title
97
		global $database;
98
		// $GLOBALS['FTAN'] = $this->getFTAN();
99
		$this->createFTAN();
100
		$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name`=\'website_title\'';
101
		$get_title = $database->query($sql);
102
		$title = $get_title->fetchRow();
103
		// Setup template object, parse vars to it, then parse it
104
		$header_template = new Template(dirname($this->correct_theme_source('header.htt')));
105
		$header_template->set_file('page', 'header.htt');
106
		$header_template->set_block('page', 'header_block', 'header');
107
		if(defined('DEFAULT_CHARSET')) {
108
			$charset=DEFAULT_CHARSET;
109
		} else {
110
			$charset='utf-8';
111
		}
112

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

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

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

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

    
209
		$footer_template->set_block('footer_block', 'show_debug_block', 'show_debug');
210

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

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

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

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

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

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

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

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

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

    
451

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

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

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

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

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

    
515
?>
(7-7/22)