Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        backend
5
 * @package         framework
6
 * @author          Ryan Djurovich (2004-2009), WebsiteBaker Project
7
 * @copyright       2009-2012, WebsiteBaker 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 1805 2012-11-04 00:53:51Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2012-11-04 01:53:51 +0100 (Sun, 04 Nov 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

    
25
// Load the other required class files if they are not already loaded
26
if(!class_exists('wb', false)){ include(WB_PATH.'/framework/class.wb.php'); }
27

    
28
// Get WB version
29
require_once(ADMIN_PATH.'/interface/version.php');
30

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

    
35

    
36
/**
37
 * admin
38
 *
39
 * @package
40
 * @copyright
41
 * @version 2012
42
 * @access public
43
 */
44
class admin extends wb {
45
	// Authenticate user then auto print the header
46
	/**
47
	 * admin::__construct()
48
	 *
49
	 * @param string $section_name
50
	 * @param string $section_permission
51
	 * @param bool $auto_header
52
	 * @param bool $auto_auth
53
	 * @return void
54
	 */
55
	public function __construct($section_name= '##skip##', $section_permission = 'start', $auto_header = true, $auto_auth = true)
56
	{
57
		parent::__construct(SecureForm::BACKEND);
58
    	if( $section_name != '##skip##' )
59
    	{
60
    		global $database, $MESSAGE;
61
    		// Specify the current applications name
62
    		$this->section_name = $section_name;
63
    		$this->section_permission = $section_permission;
64
    		$maintance = ( defined('SYSTEM_LOCKED') && (SYSTEM_LOCKED==true) ? true : false );
65
    		// Authenticate the user for this application
66
    		if( ($auto_auth == true) )
67
    		{
68
    			// First check if the user is logged-in
69
    			if($this->is_authenticated() == false)
70
    			{
71
    				header('Location: '.ADMIN_URL.'/login/index.php');
72
    				exit(0);
73
    			}
74
    			// Now check if they are allowed in this section
75
    			if($this->get_permission($section_permission) == false) {
76
    				die($MESSAGE['ADMIN_INSUFFICIENT_PRIVELLIGES']);
77
    			}
78
    		}
79

    
80
			if( ($maintance==true) || $this->get_session('USER_ID')!= 1 )
81
			{
82
           	//  check for show maintenance screen and terminate if needed
83
        		$this->ShowMaintainScreen('locked');
84
            }
85

    
86
    		// Check if the backend language is also the selected language. If not, send headers again.
87
    		$sql  = 'SELECT `language` FROM `'.TABLE_PREFIX.'users` ';
88
    		$sql .= 'WHERE `user_id`='.(int)$this->get_user_id();
89
    		$get_user_language = @$database->query($sql);
90
    		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
91
    		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
92
    		$user_language = substr($user_language[0],0,2);
93
    		// obtain the admin folder (e.g. /admin)
94
    		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
95

    
96
    		if( (LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
97
    			&& strpos($_SERVER['SCRIPT_NAME'],$admin_folder.'/') !== false) {
98
    			// check if page_id is set
99
    			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
100
    			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
101
    			 //  check if there is an query-string
102
    			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
103
    				header('Location: '.$_SERVER['SCRIPT_NAME'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
104
    			} else {
105
    				header('Location: '.$_SERVER['SCRIPT_NAME'] .'?lang='.$user_language .$page_id_url .$section_id_url);
106
    			}
107
    			exit();
108
    		}
109

    
110
    		// Auto header code
111
    		if($auto_header == true) {
112
    			$this->print_header();
113
    		}
114
    	}
115
	}
116

    
117
	// Print the admin header
118
	/**
119
	 *
120
	 * @param string $body_tags
121
	 * @return void
122
	 */
123
	function print_header($body_tags = '')
124
	{
125
		// Get vars from the language file
126
		global $MENU, $MESSAGE, $TEXT;
127
		// Connect to database and get website title
128
		global $database;
129
		// $GLOBALS['FTAN'] = $this->getFTAN();
130
		$this->createFTAN();
131
		$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name`=\'website_title\'';
132
		$get_title = $database->query($sql);
133
		$title = $get_title->fetchRow();
134
		// Setup template object, parse vars to it, then parse it
135
		$header_template = new Template(dirname($this->correct_theme_source('header.htt')) );
136
		$header_template->set_file('page', 'header.htt');
137
		$header_template->set_block('page', 'header_block', 'header');
138
		if(defined('DEFAULT_CHARSET')) {
139
			$charset=DEFAULT_CHARSET;
140
		} else {
141
			$charset='utf-8';
142
		}
143

    
144
		// work out the URL for the 'View menu' link in the WB backend
145
		// if the page_id is set, show this page otherwise show the root directory of WB
146
		$view_url = WB_URL;
147
		if(isset($_GET['page_id'])) {
148
			// extract page link from the database
149
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
150
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
151
			$result = @$database->query($sql);
152
			$row = @$result->fetchRow();
153
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
154
		}
155

    
156
        $HelpUrl = ((strtolower(LANGUAGE)!='de') ? '/en/help.php' : '/de/hilfe.php');
157
		$sServerAdress = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '127.0.0.1';
158
		$header_template->set_var(	array(
159
							'SECTION_FORGOT' => $MENU['FORGOT'],
160
							'SECTION_NAME' => $MENU['LOGIN'],
161
							'BODY_TAGS' => $body_tags,
162
							'WEBSITE_TITLE' => ($title['value']),
163
							'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
164
							'CURRENT_USER' => $MESSAGE['START_CURRENT_USER'],
165
							'DISPLAY_NAME' => $this->get_display_name(),
166
							'CHARSET' => $charset,
167
							//'LANGUAGE' => strtolower(LANGUAGE),
168
							'LANGUAGE' => LANGUAGE,
169
							'VERSION' => VERSION,
170
							'SP' => (defined('SP') ? SP : ''),
171
							'REVISION' => REVISION,
172
							'SERVER_ADDR' => ((int)$this->get_user_id()==1 ? $sServerAdress : ''),
173
							'WB_URL' => WB_URL,
174
							'ADMIN_URL' => ADMIN_URL,
175
							'THEME_URL' => THEME_URL,
176
							'START_URL' => ADMIN_URL.'/index.php',
177
							'START_CLASS' => 'start',
178
							'TITLE_START' => $TEXT['READ_MORE'],
179
							'TITLE_VIEW' => $TEXT['WEBSITE'],
180
							'TITLE_HELP' => 'WebsiteBaker '.$MENU['HELP'],
181
							'URL_VIEW' => $view_url,
182
							'TITLE_LOGOUT' => $MENU['LOGIN'],
183
							'LOGIN_DISPLAY_HIDDEN' => !$this->is_authenticated() ? 'hidden' : '',
184
							'LOGIN_DISPLAY_NONE' => !$this->is_authenticated() ? 'none' : '',
185
							'LOGIN_LINK' => $_SERVER['SCRIPT_NAME'],
186
							'LOGIN_ICON' => 'login',
187
							'START_ICON' => 'blank',
188
							'URL_HELP' => 'http://www.websitebaker2.org'.$HelpUrl,
189
							'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
190
							'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
191
						)
192
					);
193
		$header_template->set_block('header_block', 'maintenance_block', 'maintenance');
194
		if($this->get_user_id() == 1)
195
		{
196
			$sys_locked = (((int)(defined('SYSTEM_LOCKED') ? SYSTEM_LOCKED : 0)) == 1);
197
			$header_template->set_var('MAINTENANCE_MODE', ($sys_locked ? $TEXT['MAINTENANCE_OFF'] : $TEXT['MAINTENANCE_ON']));
198
			$header_template->set_var('MAINTENANCE_ICON', THEME_URL.'/images/'.($sys_locked ? 'lock' : 'unlock').'.png');
199
			$header_template->set_var('MAINTAINANCE_URL', ADMIN_URL.'/settings/locking.php');
200
			$header_template->parse('maintenance', 'maintenance_block', true);
201
		}else
202
		{
203
			$header_template->set_block('maintenance_block', '');
204
		}
205

    
206
		// Create the menu
207
        $UrlLang = ((strtolower(LANGUAGE)!='de') ? 'en' : strtolower(LANGUAGE));
208
		if(!$this->is_authenticated())
209
		{
210
    		$header_template->set_var('STYLE', 'login');
211
    		$menu = array(
212
//						array('http://www.websitebaker.org/', '_blank', 'WebsiteBaker Home', 'help', 0),
213
//						array($view_url, '_blank', $TEXT['FRONTEND'], '', 0),
214
//						array(ADMIN_URL.'/login/index.php', '', $MENU['LOGIN'], '', 0)
215
						);
216
		} else {
217
			$header_template->set_var('STYLE', 'start');
218
			$header_template->set_var(	array(
219
						'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
220
						'TITLE_LOGOUT' => $MENU['LOGOUT'],
221
						'LOGIN_DISPLAY_NONE' => '',
222
						'START_ICON' => 'home',
223
						'LOGIN_ICON' => 'logout',
224
						'LOGIN_LINK' => ADMIN_URL.'/logout/index.php',
225
						'TITLE_START' => $MENU['START']
226
						)
227
					);
228

    
229
			// @array ( $url, $target, $title, $page_permission, $permission_required )
230
			$menu = array(
231
//					array(ADMIN_URL.'/index.php', '', $MENU['START'], 'start', 1 ),
232
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
233
// 					array($view_url, '_blank', $MENU['FRONTEND'], 'pages', 1),
234
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
235
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
236
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 1),
237
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
238
					array(ADMIN_URL.'/admintools/index.php', '', $MENU['ADMINTOOLS'], 'admintools', 1),
239
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1),
240
//					array('http://addons.websitebaker2.org/', '', 'WB-Addons', 'preferences', 1),
241
//					array('http://template.websitebaker2.org/', '', 'WB-Template', 'preferences', 1),
242
//					array('http://www.websitebaker.org/', '_blank', 'WebsiteBaker Home', '', 0),
243
//					array(ADMIN_URL.'/logout/index.php', '', $MENU['LOGOUT'], '', 0)
244
					);
245
		}
246

    
247
		$header_template->set_block('header_block', 'linkBlock', 'link');
248
		foreach($menu AS $menu_item)
249
		{
250
			$link = $menu_item[0];
251
			$target = ($menu_item[1] == '') ? '_self' : $menu_item[1];
252
			$title = $menu_item[2];
253
			$permission_title = $menu_item[3];
254
			$required = $menu_item[4];
255
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
256
			if($required == false || ($this->is_authenticated() && $this->get_link_permission($permission_title)) )
257
			{
258
				$header_template->set_var('LINK', $link);
259
				$header_template->set_var('TARGET', $target);
260
				// If link is the current section apply a class name
261
				if($permission_title == strtolower($this->section_name)) {
262
					$header_template->set_var('CLASS', $menu_item[3] . ' current');
263
					$header_template->set_var('STYLE', $menu_item[3] );
264
				} else {
265
					$header_template->set_var('CLASS', $menu_item[3] );
266
				}
267
				$header_template->set_var('TITLE', $title);
268
				// Print link
269
				$header_template->parse('link', 'linkBlock', true);
270
			}
271
		}
272
		$header_template->parse('header', 'header_block', false);
273
		$header_template->pparse('output', 'page');
274
		unset($header_template);
275
	}
276

    
277
	// Print the admin footer
278
		function print_footer($activateJsAdmin = false) {
279
		global $database,$starttime,$iPhpDeclaredClasses;
280
		// include the required file for Javascript admin
281
		if($activateJsAdmin != false) {
282
			if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
283
				@include_once(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
284
			}
285
		}
286

    
287
		// Setup template object, parse vars to it, then parse it
288
		$footer_template = new Template(dirname($this->correct_theme_source('footer.htt')));
289
		$footer_template->set_file('page', 'footer.htt');
290
		$footer_template->set_block('page', 'footer_block', 'header');
291
		$footer_template->set_var(array(
292
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
293
						'WB_URL' => WB_URL,
294
						'ADMIN_URL' => ADMIN_URL,
295
						'THEME_URL' => THEME_URL,
296
			 ) );
297

    
298
		$footer_template->set_block('footer_block', 'show_debug_block', 'show_debug');
299

    
300
		$bDebug = (defined('DEBUG') ? DEBUG : false);
301
		$bDevInfo = (defined('DEV_INFOS') && (DEV_INFOS == true) && (1 == $this->get_user_id()) ? true : false);
302
//         if( $debug && (1 == $this->get_user_id()))
303
        if( $bDevInfo )
304
		{
305

    
306
			$footer_template->set_var('MEMORY', number_format(memory_get_peak_usage(true), 0, ',', '.').'&nbsp;Byte' );
307
//			$footer_template->set_var('MEMORY', number_format(memory_get_usage(true), 0, ',', '.').'&nbsp;Byte' );
308
			$footer_template->set_var('QUERIES', $database->getQueryCount );
309
			// $footer_template->set_var('QUERIES', 'disabled' );
310
	        $included_files =  get_included_files();
311
			$footer_template->set_var('INCLUDES', sizeof($included_files) );
312
	        $included_classes =  get_declared_classes();
313
			$footer_template->set_var('CLASSES', sizeof($included_classes)-$iPhpDeclaredClasses );
314

    
315
			$sum_classes = 0;
316
			$sum_filesize = 0;
317
			$footer_template->set_block('show_debug_block', 'show_block_list', 'show_list');
318
			$footer_template->set_block('show_block_list', 'include_block_list', 'include_list');
319
			// $bDebug = true;  for testing
320
			foreach($included_files as $filename)
321
			{
322
				if(!is_readable($filename)) { continue; }
323
				if($bDebug)
324
				{
325
					$footer_template->set_var('INCLUDES_ARRAY', str_replace(WB_PATH, '',$filename) );
326
					$footer_template->set_var('FILESIZE', number_format(filesize($filename), 0, ',', '.').'&nbsp;Byte');
327
					$footer_template->parse('include_list', 'include_block_list', true);
328
				}
329
				$sum_filesize += filesize($filename);
330
			}
331
			$footer_template->parse('show_list', 'show_block_list', true);
332

    
333
			$endtime = array_sum(explode(" ",microtime()));
334
			$iEndTime = $endtime;
335
			$iStartTime = $starttime;
336
			if(!$bDebug)
337
			{
338
				$footer_template->parse('show_list', '');
339
				$footer_template->parse('include_list', '');
340
			}
341

    
342
			$footer_template->set_var('FILESIZE', ini_get('memory_limit'));
343
			$footer_template->set_var('TXT_SUM_FILESIZE', 'Summary size of included files:&nbsp;');
344
			$footer_template->set_var('SUM_FILESIZE', number_format($sum_filesize, 0, ',', '.').'&nbsp;Byte');
345
			$footer_template->set_var('SUM_CLASSES', number_format($sum_classes, 0, ',', '.').'&nbsp;Byte');
346
			$footer_template->set_var('PAGE_LOAD_TIME', round($iEndTime-$iStartTime,3 ));
347
			$footer_template->set_var('DUMP_CLASSES', '<pre>'.var_export($included_classes,true).'</pre>');
348

    
349
			$footer_template->parse('show_debug', 'show_debug_block', true);
350
        } else {
351
			$footer_template->parse('show_debug', '');
352
			$footer_template->parse('show_list', '');
353

    
354
        }
355
		$footer_template->parse('header', 'footer_block', false);
356
		$footer_template->pparse('output', 'page');
357
		unset($footer_template);
358
	}
359

    
360
	// Return a system permission
361
	function get_permission($name, $type = 'system') {
362

    
363
		// Append to permission type
364
		$type .= '_permissions';
365
		// Check if we have a section to check for
366
		if($name == 'start') {
367
			return true;
368
		} else {
369
			// Set system permissions var
370
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
371
			// Set module permissions var
372
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
373
			// Set template permissions var
374
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
375
			// Return true if system perm = 1
376
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
377
				if($type == 'system_permissions') {
378
					return true;
379
				} else {
380
					return false;
381
				}
382
			} else {
383
				if($type == 'system_permissions') {
384
					return false;
385
				} else {
386
					return true;
387
				}
388
			}
389
		}
390

    
391
	}
392

    
393
 function get_user_details($user_id) {
394
  global $database;
395
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
396
  $sql  = 'SELECT `username`,`display_name`,`email` ';
397
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
398
  $sql .= 'WHERE `user_id`='.(int)$user_id;
399
  if( ($resUsers = $database->query($sql)) ) {
400
   if( ($recUser = $resUsers->fetchRow()) ) {
401
    $retval = $recUser;
402
   }
403
  }
404
  return $retval;
405
 }
406

    
407
    //
408
	function get_section_details( $section_id, $backLink = 'index.php' ) {
409
	global $database, $TEXT;
410
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'sections` ';
411
		$sql .= 'WHERE `section_id`='.intval($section_id);
412
		if(($resSection = $database->query($sql))){
413
			if(!($recSection = $resSection->fetchRow())) {
414
				$this->print_header();
415
				$this->print_error($TEXT['SECTION'].' '.$TEXT['NOT_FOUND'], $backLink, true);
416
			}
417
			} else {
418
				$this->print_header();
419
				$this->print_error($database->get_error(), $backLink, true);
420
			}
421
		return $recSection;
422
	}
423

    
424
	function get_page_details( $page_id, $backLink = 'index.php' ) {
425
		global $database, $TEXT;
426
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'pages` ';
427
		$sql .= 'WHERE `page_id`='.intval($page_id);
428
		if(($resPages = $database->query($sql))){
429
			if(!($recPage = $resPages->fetchRow())) {
430
			$this->print_header();
431
			$this->print_error($TEXT['PAGE'].' '.$TEXT['NOT_FOUND'], $backLink, true);
432
			}
433
		} else {
434
			$this->print_header();
435
			$this->print_error($database->get_error(), $backLink, true);
436
		}
437
		return $recPage;
438
	}
439

    
440
	function get_page_permission($page,$action='admin') {
441
		if($action != 'viewing') { $action = 'admin'; }
442
		$action_groups = $action.'_groups';
443
		$action_users  = $action.'_users';
444
		$groups = $users = '0';
445
		if(is_array($page)) {
446
			$groups = $page[$action_groups];
447
			$users  = $page[$action_users];
448
		} else {
449
			global $database;
450
			$sql  = 'SELECT `'.$action_groups.'`,`'.$action_users.'` ';
451
			$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
452
			$sql .= 'WHERE `page_id`='.(int)$page;
453
			if( ($res = $database->query($sql)) ) {
454
				if( ($rec = $res->fetchRow()) ) {
455
					$groups = $rec[$action_groups];
456
					$users  = $rec[$action_users];
457
				}
458
			}
459
		}
460
		return ($this->ami_group_member($groups) || $this->is_group_match($this->get_user_id(), $users));
461
	}
462

    
463
	// Returns a system permission for a menu link
464
	function get_link_permission($title) {
465
		$title = str_replace('_blank', '', $title);
466
		$title = strtolower($title);
467
		// Set system permissions var
468
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
469
		// Set module permissions var
470
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
471
		if($title == 'start') {
472
			return true;
473
		} else {
474
			// Return true if system perm = 1
475
			if(is_numeric(array_search($title, $system_permissions))) {
476
				return true;
477
			} else {
478
				return false;
479
			}
480
		}
481
	}
482

    
483
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
484
	function register_backend_modfiles_body($file_id="js")
485
		{
486
		// sanity check of parameter passed to the function
487
		$file_id = strtolower($file_id);
488
		if($file_id !== "javascript" && $file_id !== "js")
489
		{
490
			return;
491
		}
492
		global $database;
493
        $body_links = "";
494
		// define default baselink and filename for optional module javascript and stylesheet files
495
		if($file_id == "js") {
496
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
497
			$base_file = "backend_body.js";
498
		}
499
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
500
		if(isset($_GET['tool']))
501
			{
502
			// check if displayed page contains a installed admin tool
503
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
504
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
505
			$result = $database->query($sql);
506
			if($result->numRows())
507
				{
508
				// check if admin tool directory contains a backend_body.js file to include
509
				$tool = $result->fetchRow();
510
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
511
				{
512
					// return link to the backend_body.js file
513
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
514
				}
515
			}
516
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
517
		{
518
			// check if displayed page in the backend contains a page module
519
			if (isset($_GET['page_id']))
520
			{
521
				$page_id = (int) addslashes($_GET['page_id']);
522
			} else {
523
				$page_id = (int) addslashes($_POST['page_id']);
524
			}
525
			// gather information for all models embedded on actual page
526
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
527
			$query_modules = $database->query($sql);
528
			while($row = $query_modules->fetchRow()) {
529
				// check if page module directory contains a backend_body.js file
530
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
531
					// create link with backend_body.js source for the current module
532
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
533
					// ensure that backend_body.js is only added once per module type
534
					if(strpos($body_links, $tmp_link) === false) {
535
						$body_links .= $tmp_link ."\n";
536
					}
537
				}
538
			}
539
			// write out links with all external module javascript/CSS files, remove last line feed
540
			return rtrim($body_links);
541
		}
542
	}
543

    
544

    
545
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
546
	function register_backend_modfiles($file_id="css") {
547
		// sanity check of parameter passed to the function
548
		$file_id = strtolower($file_id);
549
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
550
			return;
551
		}
552

    
553
		global $database;
554
		// define default baselink and filename for optional module javascript and stylesheet files
555
		$head_links = "";
556
		if($file_id == "css") {
557
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
558
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
559
			$base_file = "backend.css";
560
		} else {
561
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
562
			$base_file = "backend.js";
563
		}
564

    
565
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
566
		if(isset($_GET['tool'])) {
567
			// check if displayed page contains a installed admin tool
568
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
569
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
570
			$result = $database->query($sql);
571
			if($result->numRows()) {
572
				// check if admin tool directory contains a backend.js or backend.css file to include
573
				$tool = $result->fetchRow();
574
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
575
        			// return link to the backend.js or backend.css file
576
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
577
				}
578
			}
579
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
580
			// check if displayed page in the backend contains a page module
581
			if (isset($_GET['page_id'])) {
582
				$page_id = (int)$_GET['page_id'];
583
			} else {
584
				$page_id = (int)$_POST['page_id'];
585
			}
586

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

    
591
    		while($row = $query_modules->fetchRow()) {
592
				// check if page module directory contains a backend.js or backend.css file
593
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
594
					// create link with backend.js or backend.css source for the current module
595
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
596
        			// ensure that backend.js or backend.css is only added once per module type
597
        			if(strpos($head_links, $tmp_link) === false) {
598
						$head_links .= $tmp_link ."\n";
599
					}
600
				}
601
    		}
602
    		// write out links with all external module javascript/CSS files, remove last line feed
603
			return rtrim($head_links);
604
		}
605
	}
606
}
(11-11/25)