Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framewotk
5
 * @package         backend admin
6
 * @author          Ryan Djurovich, WebsiteBaker Project
7
 * @copyright       2009-2012, 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 1684 2012-05-05 07:17:09Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2012-05-05 09:17:09 +0200 (Sat, 05 May 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

    
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 ? $_SERVER['SERVER_ADDR'] : ''),
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_NONE' => ' display: none; ',
172
							'LOGIN_LINK' => $_SERVER['SCRIPT_NAME'],
173
							'LOGIN_ICON' => 'login',
174
							'START_ICON' => 'blank',
175
							'URL_HELP' => 'http://www.websitebaker.org/',
176
							'BACKEND_MODULE_CSS' => $this->register_backend_modfiles('css'),	// adds backend.css
177
							'BACKEND_MODULE_JS'  => $this->register_backend_modfiles('js')		// adds backend.js
178
						)
179
					);
180

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

    
214
					);
215
		}
216

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

    
244
	// Print the admin footer
245
		function print_footer($activateJsAdmin = false) {
246
		global $database,$starttime;
247
		// include the required file for Javascript admin
248
		if($activateJsAdmin != false) {
249
			if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
250
				@include_once(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
251
			}
252
		}
253

    
254
		// Setup template object, parse vars to it, then parse it
255
		$footer_template = new Template(dirname($this->correct_theme_source('footer.htt')));
256
		$footer_template->set_file('page', 'footer.htt');
257
		$footer_template->set_block('page', 'footer_block', 'header');
258
		$footer_template->set_var(array(
259
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
260
						'WB_URL' => WB_URL,
261
						'ADMIN_URL' => ADMIN_URL,
262
						'THEME_URL' => THEME_URL,
263
			 ) );
264

    
265
		$footer_template->set_block('footer_block', 'show_debug_block', 'show_debug');
266

    
267
		$bDebug = (defined('DEBUG') ? DEBUG : false);
268
		$bDevInfo = (defined('DEV_INFOS') && (DEV_INFOS == true) && (1 == $this->get_user_id()) ? true : false);
269
//         if( $debug && (1 == $this->get_user_id()))
270
        if( $bDevInfo )
271
		{
272

    
273
			$footer_template->set_var('MEMORY', number_format(memory_get_peak_usage(), 0, ',', '.').'&nbsp;Byte' );
274
			$footer_template->set_var('QUERIES', $database->getQueryCount );
275
			// $footer_template->set_var('QUERIES', 'disabled' );
276
	        $included_files =  get_included_files();
277
			$footer_template->set_var('INCLUDES', sizeof($included_files) );
278

    
279
			$sum_filesize = 0;
280
			$footer_template->set_block('show_debug_block', 'show_block_list', 'show_list');
281
			$footer_template->set_block('show_block_list', 'include_block_list', 'include_list');
282
			// $bDebug = true;  for testing
283
			foreach($included_files as $filename)
284
			{
285
				if(!is_readable($filename)) { continue; }
286
				if($bDebug)
287
				{
288
					$footer_template->set_var('INCLUDES_ARRAY', str_replace(WB_PATH, '',$filename) );
289
					$footer_template->set_var('FILESIZE', number_format(filesize($filename), 0, ',', '.').'&nbsp;Byte');
290
					$footer_template->parse('include_list', 'include_block_list', true);
291
				}
292
				$sum_filesize += filesize($filename);
293
			}
294
			$footer_template->parse('show_list', 'show_block_list', true);
295

    
296
			$endtime = array_sum(explode(" ",microtime()));
297
			$iEndTime = $endtime;
298
			$iStartTime = $starttime;
299
			if(!$bDebug)
300
			{
301
				$footer_template->parse('show_list', '');
302
				$footer_template->parse('include_list', '');
303
			}
304

    
305
			$footer_template->set_var('FILESIZE', ini_get('memory_limit'));
306
			$footer_template->set_var('TXT_SUM_FILESIZE', 'Summary size of included files:&nbsp;');
307
			$footer_template->set_var('SUM_FILESIZE', number_format($sum_filesize, 0, ',', '.').'&nbsp;Byte');
308
			$footer_template->set_var('PAGE_LOAD_TIME', round($iEndTime-$iStartTime,3 ));
309

    
310
			$footer_template->parse('show_debug', 'show_debug_block', true);
311
        } else {
312
			$footer_template->parse('show_debug', '');
313
			$footer_template->parse('show_list', '');
314

    
315
        }
316
		$footer_template->parse('header', 'footer_block', false);
317
		$footer_template->pparse('output', 'page');
318
	}
319

    
320
	// Return a system permission
321
	function get_permission($name, $type = 'system') {
322
		// Append to permission type
323
		$type .= '_permissions';
324
		// Check if we have a section to check for
325
		if($name == 'start') {
326
			return true;
327
		} else {
328
			// Set system permissions var
329
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
330
			// Set module permissions var
331
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
332
			// Set template permissions var
333
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
334
			// Return true if system perm = 1
335
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
336
				if($type == 'system_permissions') {
337
					return true;
338
				} else {
339
					return false;
340
				}
341
			} else {
342
				if($type == 'system_permissions') {
343
					return false;
344
				} else {
345
					return true;
346
				}
347
			}
348
		}
349
	}
350
/*
351
	function get_user_details($user_id) {
352
		global $database;
353
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'users` ';
354
		$sql .= 'WHERE `user_id`='.(int)$user_id.' LIMIT 1';
355
		if(($resUser = $database->query($sql))){
356
			if(!($recUser = $resUser->fetchRow())) {
357
				$recUser['display_name'] = 'Unknown';
358
				$recUser['username'] = 'unknown';
359
			}
360
		}
361
		return $recUser;
362
	}
363
*/
364
 function get_user_details($user_id) {
365
  global $database;
366
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
367
  $sql  = 'SELECT `username`,`display_name`,`email` ';
368
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
369
  $sql .= 'WHERE `user_id`='.(int)$user_id;
370
  if( ($resUsers = $database->query($sql)) ) {
371
   if( ($recUser = $resUsers->fetchRow()) ) {
372
    $retval = $recUser;
373
   }
374
  }
375
  return $retval;
376
 }
377

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

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

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

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

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

    
515

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

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

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

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

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