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 1668 2012-04-17 12:53:12Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2012-04-17 14:53:12 +0200 (Tue, 17 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') ? true : 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($bDebug)
229
				{
230
					$footer_template->set_var('INCLUDES_ARRAY', str_replace(WB_PATH, '',$filename) );
231
					$footer_template->set_var('FILESIZE', number_format(filesize($filename), 0, ',', '.').'&nbsp;Byte');
232
					$footer_template->parse('include_list', 'include_block_list', true);
233
				}
234
				$sum_filesize += filesize($filename);
235
			}
236
			$footer_template->parse('show_list', 'show_block_list', true);
237

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

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

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

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

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

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

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

    
450

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

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

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

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

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

    
514
?>
(7-7/22)