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 1782 2012-10-11 12:29:11Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2012-10-11 14:29:11 +0200 (Thu, 11 Oct 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
/**
35
 * admin
36
 *
37
 * @package
38
 * @copyright
39
 * @version 2012
40
 * @access public
41
 */
42
class admin extends wb {
43
	// Authenticate user then auto print the header
44
	/**
45
	 * admin::__construct()
46
	 *
47
	 * @param string $section_name
48
	 * @param string $section_permission
49
	 * @param bool $auto_header
50
	 * @param bool $auto_auth
51
	 * @return void
52
	 */
53
	public function __construct($section_name= '##skip##', $section_permission = 'start', $auto_header = true, $auto_auth = true)
54
	{
55
		parent::__construct(SecureForm::BACKEND);
56
    	if( $section_name != '##skip##' )
57
    	{
58
    		global $database, $MESSAGE;
59
    		// Specify the current applications name
60
    		$this->section_name = $section_name;
61
    		$this->section_permission = $section_permission;
62
    		$maintance = ( defined('SYSTEM_LOCKED') && (SYSTEM_LOCKED==true) ? true : false );
63
    		// Authenticate the user for this application
64
    		if( ($auto_auth == true) )
65
    		{
66
    			// First check if the user is logged-in
67
    			if($this->is_authenticated() == false)
68
    			{
69
    				header('Location: '.ADMIN_URL.'/login/index.php');
70
    				exit(0);
71
    			}
72
    			// Now check if they are allowed in this section
73
    			if($this->get_permission($section_permission) == false) {
74
    				die($MESSAGE['ADMIN_INSUFFICIENT_PRIVELLIGES']);
75
    			}
76
    		}
77

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

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

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

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

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

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

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

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

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

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

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

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

    
296
		$footer_template->set_block('footer_block', 'show_debug_block', 'show_debug');
297

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

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

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

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

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

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

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

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

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

    
389
	}
390

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

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

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

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

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

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

    
542

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

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

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

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

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