Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framewotk
5
 * @package         backend admin
6
 * @author          Ryan Djurovich, 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 1700 2012-08-27 14:40:56Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2012-08-27 16:40:56 +0200 (Mon, 27 Aug 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
		// Authenticate the user for this application
63
		if($auto_auth == true)
64
		{
65
			// First check if the user is logged-in
66
			if($this->is_authenticated() == false)
67
			{
68
				header('Location: '.ADMIN_URL.'/login/index.php');
69
				exit(0);
70
			}
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
		// Check if the backend language is also the selected language. If not, send headers again.
79
		$sql  = 'SELECT `language` FROM `'.TABLE_PREFIX.'users` ';
80
		$sql .= 'WHERE `user_id`='.(int)$this->get_user_id();
81
		$get_user_language = @$database->query($sql);
82
		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
83
		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
84
		$user_language = substr($user_language[0],0,2);
85
		// obtain the admin folder (e.g. /admin)
86
		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
87
		if((LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
88
			&& strpos($_SERVER['PHP_SELF'],$admin_folder.'/') !== false) {
89
			// check if page_id is set
90
			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
91
			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
92
			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
93
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
94
			} else {
95
				header('Location: '.$_SERVER['PHP_SELF'] .'?lang='.$user_language .$page_id_url .$section_id_url);
96
			}
97
			exit();
98
		}
99

    
100
		// Auto header code
101
		if($auto_header == true) {
102
			$this->print_header();
103
		}
104
	}
105
	}
106

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

    
135
		// work out the URL for the 'View menu' link in the WB backend
136
		// if the page_id is set, show this page otherwise show the root directory of WB
137
		$view_url = WB_URL;
138
		if(isset($_GET['page_id'])) {
139
			// extract page link from the database
140
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
141
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
142
			$result = @$database->query($sql);
143
			$row = @$result->fetchRow();
144
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
145
		}
146
		$sServerAdress = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '127.0.0.1';
147
		$header_template->set_var(	array(
148
							'SECTION_FORGOT' => $MENU['FORGOT'],
149
							'SECTION_NAME' => $MENU['LOGIN'],
150
							'BODY_TAGS' => $body_tags,
151
							'WEBSITE_TITLE' => ($title['value']),
152
							'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
153
							'CURRENT_USER' => $MESSAGE['START_CURRENT_USER'],
154
							'DISPLAY_NAME' => $this->get_display_name(),
155
							'CHARSET' => $charset,
156
							'LANGUAGE' => strtolower(LANGUAGE),
157
							'VERSION' => VERSION,
158
							'SP' => (defined('SP') ? SP : ''),
159
							'REVISION' => REVISION,
160
							'SERVER_ADDR' => ((int)$this->get_user_id()==1 ? $sServerAdress : ''),
161
							'WB_URL' => WB_URL,
162
							'ADMIN_URL' => ADMIN_URL,
163
							'THEME_URL' => THEME_URL,
164
							'START_URL' => ADMIN_URL.'/index.php',
165
							'START_CLASS' => 'start',
166
							'TITLE_START' => $TEXT['READ_MORE'],
167
							'TITLE_VIEW' => $TEXT['WEBSITE'],
168
							'TITLE_HELP' => $MENU['HELP'],
169
							'URL_VIEW' => $view_url,
170
							'TITLE_LOGOUT' => $MENU['LOGIN'],
171
							'LOGIN_DISPLAY_HIDDEN' => !$this->is_authenticated() ? 'hidden' : '',
172
							'LOGIN_DISPLAY_NONE' => !$this->is_authenticated() ? 'none' : '',
173
							'LOGIN_LINK' => $_SERVER['SCRIPT_NAME'],
174
							'LOGIN_ICON' => 'login',
175
							'START_ICON' => 'blank',
176
							'URL_HELP' => 'http://www.websitebaker.org/',
177
							'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
178
							'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
179
						)
180
					);
181

    
182
		// Create the menu
183
		if(!$this->is_authenticated())
184
		{
185
		$header_template->set_var('STYLE', 'login');
186
		$menu = array(
187
//						array('http://www.websitebaker.org/', '_blank', 'WebsiteBaker Home', 'help', 0),
188
//						array($view_url, '_blank', $TEXT['FRONTEND'], '', 0),
189
//						array(ADMIN_URL.'/login/index.php', '', $MENU['LOGIN'], '', 0)
190
						);
191
		} else {
192
			$header_template->set_var('STYLE', 'start');
193
			$header_template->set_var(	array(
194
						'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
195
						'TITLE_LOGOUT' => $MENU['LOGOUT'],
196
						'LOGIN_DISPLAY_NONE' => '',
197
						'START_ICON' => 'home',
198
						'LOGIN_ICON' => 'logout',
199
						'LOGIN_LINK' => ADMIN_URL.'/logout/index.php',
200
						'TITLE_START' => $MENU['START']
201
						)
202
					);
203
			// @array ( $url, $target, $title, $page_permission, $ppermission_required )
204
			$menu = array(
205
//					array(ADMIN_URL.'/index.php', '', $MENU['START'], 'start', 1 ),
206
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
207
// 					array($view_url, '_blank', $MENU['FRONTEND'], 'pages', 1),
208
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
209
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
210
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 1),
211
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
212
					array(ADMIN_URL.'/admintools/index.php', '', $MENU['ADMINTOOLS'], 'admintools', 1),
213
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1),
214
//					array('http://www.websitebaker.org/', '_blank', 'WebsiteBaker Home', '', 0),
215
//					array(ADMIN_URL.'/logout/index.php', '', $MENU['LOGOUT'], '', 0)
216

    
217
					);
218
		}
219

    
220
		$header_template->set_block('header_block', 'linkBlock', 'link');
221
		foreach($menu AS $menu_item)
222
		{
223
			$link = $menu_item[0];
224
			$target = ($menu_item[1] == '') ? '_self' : $menu_item[1];
225
			$title = $menu_item[2];
226
			$permission_title = $menu_item[3];
227
			$required = $menu_item[4];
228
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
229
			if($required == false || ($this->is_authenticated() && $this->get_link_permission($permission_title)) )
230
			{
231
				$header_template->set_var('LINK', $link);
232
				$header_template->set_var('TARGET', $target);
233
				// If link is the current section apply a class name
234
				if($permission_title == strtolower($this->section_name)) {
235
					$header_template->set_var('CLASS', $menu_item[3] . ' current');
236
					$header_template->set_var('STYLE', $menu_item[3] );
237
				} else {
238
					$header_template->set_var('CLASS', $menu_item[3] );
239
				}
240
				$header_template->set_var('TITLE', $title);
241
				// Print link
242
				$header_template->parse('link', 'linkBlock', true);
243
			}
244
		}
245
		$header_template->parse('header', 'header_block', false);
246
		$header_template->pparse('output', 'page');
247
		unset($header_template);
248
	}
249

    
250
	// Print the admin footer
251
		function print_footer($activateJsAdmin = false) {
252
		global $database,$starttime,$iPhpDeclaredClasses;
253
		// include the required file for Javascript admin
254
		if($activateJsAdmin != false) {
255
			if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
256
				@include_once(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
257
			}
258
		}
259

    
260
		// Setup template object, parse vars to it, then parse it
261
		$footer_template = new Template(dirname($this->correct_theme_source('footer.htt')));
262
		$footer_template->set_file('page', 'footer.htt');
263
		$footer_template->set_block('page', 'footer_block', 'header');
264
		$footer_template->set_var(array(
265
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
266
						'WB_URL' => WB_URL,
267
						'ADMIN_URL' => ADMIN_URL,
268
						'THEME_URL' => THEME_URL,
269
			 ) );
270

    
271
		$footer_template->set_block('footer_block', 'show_debug_block', 'show_debug');
272

    
273
		$bDebug = (defined('DEBUG') ? DEBUG : false);
274
		$bDevInfo = (defined('DEV_INFOS') && (DEV_INFOS == true) && (1 == $this->get_user_id()) ? true : false);
275
//         if( $debug && (1 == $this->get_user_id()))
276
        if( $bDevInfo )
277
		{
278

    
279
			$footer_template->set_var('MEMORY', number_format(memory_get_peak_usage(true), 0, ',', '.').'&nbsp;Byte' );
280
//			$footer_template->set_var('MEMORY', number_format(memory_get_usage(true), 0, ',', '.').'&nbsp;Byte' );
281
			$footer_template->set_var('QUERIES', $database->getQueryCount );
282
			// $footer_template->set_var('QUERIES', 'disabled' );
283
	        $included_files =  get_included_files();
284
			$footer_template->set_var('INCLUDES', sizeof($included_files) );
285
	        $included_classes =  get_declared_classes();
286
			$footer_template->set_var('CLASSES', sizeof($included_classes)-$iPhpDeclaredClasses );
287

    
288
			$sum_classes = 0;
289
			$sum_filesize = 0;
290
			$footer_template->set_block('show_debug_block', 'show_block_list', 'show_list');
291
			$footer_template->set_block('show_block_list', 'include_block_list', 'include_list');
292
			// $bDebug = true;  for testing
293
			foreach($included_files as $filename)
294
			{
295
				if(!is_readable($filename)) { continue; }
296
				if($bDebug)
297
				{
298
					$footer_template->set_var('INCLUDES_ARRAY', str_replace(WB_PATH, '',$filename) );
299
					$footer_template->set_var('FILESIZE', number_format(filesize($filename), 0, ',', '.').'&nbsp;Byte');
300
					$footer_template->parse('include_list', 'include_block_list', true);
301
				}
302
				$sum_filesize += filesize($filename);
303
			}
304
			$footer_template->parse('show_list', 'show_block_list', true);
305

    
306
			$endtime = array_sum(explode(" ",microtime()));
307
			$iEndTime = $endtime;
308
			$iStartTime = $starttime;
309
			if(!$bDebug)
310
			{
311
				$footer_template->parse('show_list', '');
312
				$footer_template->parse('include_list', '');
313
			}
314

    
315
			$footer_template->set_var('FILESIZE', ini_get('memory_limit'));
316
			$footer_template->set_var('TXT_SUM_FILESIZE', 'Summary size of included files:&nbsp;');
317
			$footer_template->set_var('SUM_FILESIZE', number_format($sum_filesize, 0, ',', '.').'&nbsp;Byte');
318
			$footer_template->set_var('SUM_CLASSES', number_format($sum_classes, 0, ',', '.').'&nbsp;Byte');
319
			$footer_template->set_var('PAGE_LOAD_TIME', round($iEndTime-$iStartTime,3 ));
320
			$footer_template->set_var('DUMP_CLASSES', '<pre>'.var_export($included_classes,true).'</pre>');
321

    
322
			$footer_template->parse('show_debug', 'show_debug_block', true);
323
        } else {
324
			$footer_template->parse('show_debug', '');
325
			$footer_template->parse('show_list', '');
326

    
327
        }
328
		$footer_template->parse('header', 'footer_block', false);
329
		$footer_template->pparse('output', 'page');
330
		unset($footer_template);
331
	}
332

    
333
	// Return a system permission
334
	function get_permission($name, $type = 'system') {
335

    
336
		// Append to permission type
337
		$type .= '_permissions';
338
		// Check if we have a section to check for
339
		if($name == 'start') {
340
			return true;
341
		} else {
342
			// Set system permissions var
343
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
344
			// Set module permissions var
345
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
346
			// Set template permissions var
347
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
348
			// Return true if system perm = 1
349
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
350
				if($type == 'system_permissions') {
351
					return true;
352
				} else {
353
					return false;
354
				}
355
			} else {
356
				if($type == 'system_permissions') {
357
					return false;
358
				} else {
359
					return true;
360
				}
361
			}
362
		}
363

    
364
	}
365

    
366
 function get_user_details($user_id) {
367
  global $database;
368
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
369
  $sql  = 'SELECT `username`,`display_name`,`email` ';
370
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
371
  $sql .= 'WHERE `user_id`='.(int)$user_id;
372
  if( ($resUsers = $database->query($sql)) ) {
373
   if( ($recUser = $resUsers->fetchRow()) ) {
374
    $retval = $recUser;
375
   }
376
  }
377
  return $retval;
378
 }
379

    
380
    //
381
	function get_section_details( $section_id, $backLink = 'index.php' ) {
382
	global $database, $TEXT;
383
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'sections` ';
384
		$sql .= 'WHERE `section_id`='.intval($section_id);
385
		if(($resSection = $database->query($sql))){
386
			if(!($recSection = $resSection->fetchRow())) {
387
				$this->print_header();
388
				$this->print_error($TEXT['SECTION'].' '.$TEXT['NOT_FOUND'], $backLink, true);
389
			}
390
			} else {
391
				$this->print_header();
392
				$this->print_error($database->get_error(), $backLink, true);
393
			}
394
		return $recSection;
395
	}
396

    
397
	function get_page_details( $page_id, $backLink = 'index.php' ) {
398
		global $database, $TEXT;
399
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'pages` ';
400
		$sql .= 'WHERE `page_id`='.intval($page_id);
401
		if(($resPages = $database->query($sql))){
402
			if(!($recPage = $resPages->fetchRow())) {
403
			$this->print_header();
404
			$this->print_error($TEXT['PAGE'].' '.$TEXT['NOT_FOUND'], $backLink, true);
405
			}
406
		} else {
407
			$this->print_header();
408
			$this->print_error($database->get_error(), $backLink, true);
409
		}
410
		return $recPage;
411
	}
412

    
413
	function get_page_permission($page,$action='admin') {
414
		if($action != 'viewing') { $action = 'admin'; }
415
		$action_groups = $action.'_groups';
416
		$action_users  = $action.'_users';
417
		$groups = $users = '0';
418
		if(is_array($page)) {
419
			$groups = $page[$action_groups];
420
			$users  = $page[$action_users];
421
		} else {
422
			global $database;
423
			$sql  = 'SELECT `'.$action_groups.'`,`'.$action_users.'` ';
424
			$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
425
			$sql .= 'WHERE `page_id`='.(int)$page;
426
			if( ($res = $database->query($sql)) ) {
427
				if( ($rec = $res->fetchRow()) ) {
428
					$groups = $rec[$action_groups];
429
					$users  = $rec[$action_users];
430
				}
431
			}
432
		}
433
		return ($this->ami_group_member($groups) || $this->is_group_match($this->get_user_id(), $users));
434
	}
435

    
436
	// Returns a system permission for a menu link
437
	function get_link_permission($title) {
438
		$title = str_replace('_blank', '', $title);
439
		$title = strtolower($title);
440
		// Set system permissions var
441
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
442
		// Set module permissions var
443
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
444
		if($title == 'start') {
445
			return true;
446
		} else {
447
			// Return true if system perm = 1
448
			if(is_numeric(array_search($title, $system_permissions))) {
449
				return true;
450
			} else {
451
				return false;
452
			}
453
		}
454
	}
455

    
456
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
457
	function register_backend_modfiles_body($file_id="js")
458
		{
459
		// sanity check of parameter passed to the function
460
		$file_id = strtolower($file_id);
461
		if($file_id !== "javascript" && $file_id !== "js")
462
		{
463
			return;
464
		}
465
		global $database;
466
        $body_links = "";
467
		// define default baselink and filename for optional module javascript and stylesheet files
468
		if($file_id == "js") {
469
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
470
			$base_file = "backend_body.js";
471
		}
472
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
473
		if(isset($_GET['tool']))
474
			{
475
			// check if displayed page contains a installed admin tool
476
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
477
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
478
			$result = $database->query($sql);
479
			if($result->numRows())
480
				{
481
				// check if admin tool directory contains a backend_body.js file to include
482
				$tool = $result->fetchRow();
483
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
484
				{
485
					// return link to the backend_body.js file
486
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
487
				}
488
			}
489
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
490
		{
491
			// check if displayed page in the backend contains a page module
492
			if (isset($_GET['page_id']))
493
			{
494
				$page_id = (int) addslashes($_GET['page_id']);
495
			} else {
496
				$page_id = (int) addslashes($_POST['page_id']);
497
			}
498
			// gather information for all models embedded on actual page
499
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
500
			$query_modules = $database->query($sql);
501
			while($row = $query_modules->fetchRow()) {
502
				// check if page module directory contains a backend_body.js file
503
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
504
					// create link with backend_body.js source for the current module
505
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
506
					// ensure that backend_body.js is only added once per module type
507
					if(strpos($body_links, $tmp_link) === false) {
508
						$body_links .= $tmp_link ."\n";
509
					}
510
				}
511
			}
512
			// write out links with all external module javascript/CSS files, remove last line feed
513
			return rtrim($body_links);
514
		}
515
	}
516

    
517

    
518
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
519
	function register_backend_modfiles($file_id="css") {
520
		// sanity check of parameter passed to the function
521
		$file_id = strtolower($file_id);
522
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
523
			return;
524
		}
525

    
526
		global $database;
527
		// define default baselink and filename for optional module javascript and stylesheet files
528
		$head_links = "";
529
		if($file_id == "css") {
530
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
531
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
532
			$base_file = "backend.css";
533
		} else {
534
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
535
			$base_file = "backend.js";
536
		}
537

    
538
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
539
		if(isset($_GET['tool'])) {
540
			// check if displayed page contains a installed admin tool
541
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
542
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($_GET['tool']).'\'';
543
			$result = $database->query($sql);
544
			if($result->numRows()) {
545
				// check if admin tool directory contains a backend.js or backend.css file to include
546
				$tool = $result->fetchRow();
547
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
548
        			// return link to the backend.js or backend.css file
549
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
550
				}
551
			}
552
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
553
			// check if displayed page in the backend contains a page module
554
			if (isset($_GET['page_id'])) {
555
				$page_id = (int)$_GET['page_id'];
556
			} else {
557
				$page_id = (int)$_POST['page_id'];
558
			}
559

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

    
564
    		while($row = $query_modules->fetchRow()) {
565
				// check if page module directory contains a backend.js or backend.css file
566
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
567
					// create link with backend.js or backend.css source for the current module
568
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
569
        			// ensure that backend.js or backend.css is only added once per module type
570
        			if(strpos($head_links, $tmp_link) === false) {
571
						$head_links .= $tmp_link ."\n";
572
					}
573
				}
574
    		}
575
    		// write out links with all external module javascript/CSS files, remove last line feed
576
			return rtrim($head_links);
577
		}
578
	}
579
}
(11-11/25)