Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        backend
5
 * @package         framework
6
 * @author          Ryan Djurovich (2004-2009), WebsiteBaker Project
7
 * @copyright       2009-2013, WebsiteBaker Org. e.V.
8
 * @link            http://www.websitebaker.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 1908 2013-06-07 02:58:25Z Luisehahne $
13
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2013-06-07 04:58:25 +0200 (Fri, 07 Jun 2013) $
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, $TEXT;
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
                    $sErrorMsgFile = $this->correct_theme_source('ErrorMsgFile.htt');
78
            		if(file_exists($sErrorMsgFile))
79
            		{
80
                        $this->print_header();
81
                        $oTpl = new Template(dirname( $sErrorMsgFile ));
82
//                        $oTpl->debug = true;
83
                        $sBackLink = (isset($_SERVER['QUERY_STRING'])&& ($_SERVER['QUERY_STRING']!='')) ? $_SERVER['HTTP_REFERER'].'?'.$_SERVER['QUERY_STRING'] :  $_SERVER['HTTP_REFERER'];
84
            		    $oTpl->set_file( 'page', 'ErrorMsgFile.htt' );
85
            	 	    $oTpl->set_var( 'THEME_URL', THEME_URL );
86
            			$oTpl->set_var( 'PAGE_ICON', 'negative');
87
            			$oTpl->set_var( 'ERROR_TITLE', $MESSAGE['MEDIA_DIR_ACCESS_DENIED']);
88
            	 	    $oTpl->set_var( 'PAGE_TITLE', $MESSAGE['ADMIN_INSUFFICIENT_PRIVELLIGES'] );
89
            	 	    $oTpl->set_var( 'BACK_LINK', $sBackLink );
90
            	 	    $oTpl->set_var( 'TEXT_BACK', $TEXT['BACK'] );
91
                		$output = $oTpl->finish($oTpl->parse('output', 'page'));
92
        			}
93
        			throw new ErrorMsgException($output);
94
                }
95
    		}
96

    
97
			if( ($maintance==true) || $this->get_session('USER_ID')!= 1 )
98
			{
99
           	//  check for show maintenance screen and terminate if needed
100
        		$this->ShowMaintainScreen('locked');
101
            }
102

    
103
    		// Check if the backend language is also the selected language. If not, send headers again.
104
    		$sql  = 'SELECT `language` FROM `'.TABLE_PREFIX.'users` ';
105
    		$sql .= 'WHERE `user_id`='.(int)$this->get_user_id();
106
    		$get_user_language = @$database->query($sql);
107
    		$user_language = ($get_user_language) ? $get_user_language->fetchRow() : '';
108
    		// prevent infinite loop if language file is not XX.php (e.g. DE_du.php)
109
    		$user_language = substr($user_language[0],0,2);
110
    		// obtain the admin folder (e.g. /admin)
111
    		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
112

    
113
    		if( (LANGUAGE != $user_language) && file_exists(WB_PATH .'/languages/' .$user_language .'.php')
114
    			&& strpos($_SERVER['SCRIPT_NAME'],$admin_folder.'/') !== false) {
115
    			// check if page_id is set
116
    			$page_id_url = (isset($_GET['page_id'])) ? '&page_id=' .(int) $_GET['page_id'] : '';
117
    			$section_id_url = (isset($_GET['section_id'])) ? '&section_id=' .(int) $_GET['section_id'] : '';
118
    			 //  check if there is an query-string
119
    			if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
120
    				header('Location: '.$_SERVER['SCRIPT_NAME'] .'?lang='.$user_language .$page_id_url .$section_id_url.'&'.$_SERVER['QUERY_STRING']);
121
    			} else {
122
    				header('Location: '.$_SERVER['SCRIPT_NAME'] .'?lang='.$user_language .$page_id_url .$section_id_url);
123
    			}
124
    			exit();
125
    		}
126

    
127
    		// Auto header code
128
    		if($auto_header == true) {
129
    			$this->print_header();
130
    		}
131
    	}
132
	}
133

    
134
	// Print the admin header
135
	/**
136
	 *
137
	 * @param string $body_tags
138
	 * @return void
139
	 */
140
	function print_header($body_tags = '')
141
	{
142
		// Get vars from the language file
143
		global $MENU, $MESSAGE, $TEXT;
144
		// Connect to database and get website title
145
		global $database;
146
		// $GLOBALS['FTAN'] = $this->getFTAN();
147
		$this->createFTAN();
148
		$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name`=\'website_title\'';
149
		$get_title = $database->query($sql);
150
		$title = $get_title->fetchRow();
151
		// Setup template object, parse vars to it, then parse it
152
		$header_template = new Template(dirname($this->correct_theme_source('header.htt')) );
153
		$header_template->set_file('page', 'header.htt');
154
		$header_template->set_block('page', 'header_block', 'header');
155
		if(defined('DEFAULT_CHARSET')) {
156
			$charset=DEFAULT_CHARSET;
157
		} else {
158
			$charset='utf-8';
159
		}
160

    
161
		// work out the URL for the 'View menu' link in the WB backend
162
		// if the page_id is set, show this page otherwise show the root directory of WB
163
		$view_url = WB_URL;
164
		if(isset($_GET['page_id'])) {
165
			// extract page link from the database
166
			$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
167
			$sql .= 'WHERE `page_id`='.intval($_GET['page_id']);
168
			$result = @$database->query($sql);
169
			$row = @$result->fetchRow();
170
			if($row) $view_url .= PAGES_DIRECTORY .$row['link']. PAGE_EXTENSION;
171
		}
172

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

    
223
		// Create the menu
224
        $UrlLang = ((strtolower(LANGUAGE)!='de') ? 'en' : strtolower(LANGUAGE));
225
		if(!$this->is_authenticated())
226
		{
227
    		$header_template->set_var('STYLE', 'login');
228
    		$menu = array(
229
//						array('http://www.websitebaker.org/', '_blank', 'WebsiteBaker Home', 'help', 0),
230
//						array($view_url, '_blank', $TEXT['FRONTEND'], '', 0),
231
//						array(ADMIN_URL.'/login/index.php', '', $MENU['LOGIN'], '', 0)
232
						);
233
		} else {
234
			$header_template->set_var('STYLE', 'start');
235
			$header_template->set_var(	array(
236
						'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
237
						'TITLE_LOGOUT' => $MENU['LOGOUT'],
238
						'LOGIN_DISPLAY_NONE' => '',
239
						'START_ICON' => 'home',
240
						'LOGIN_ICON' => 'logout',
241
						'LOGIN_LINK' => ADMIN_URL.'/logout/index.php',
242
						'TITLE_START' => $MENU['START']
243
						)
244
					);
245

    
246
			// @array ( $url, $target, $title, $page_permission, $permission_required )
247
			$menu = array(
248
//					array(ADMIN_URL.'/index.php', '', $MENU['START'], 'start', 1 ),
249
					array(ADMIN_URL.'/pages/index.php', '', $MENU['PAGES'], 'pages', 1),
250
// 					array($view_url, '_blank', $MENU['FRONTEND'], 'pages', 1),
251
					array(ADMIN_URL.'/media/index.php', '', $MENU['MEDIA'], 'media', 1),
252
					array(ADMIN_URL.'/addons/index.php', '', $MENU['ADDONS'], 'addons', 1),
253
					array(ADMIN_URL.'/preferences/index.php', '', $MENU['PREFERENCES'], 'preferences', 1),
254
					array(ADMIN_URL.'/settings/index.php', '', $MENU['SETTINGS'], 'settings', 1),
255
					array(ADMIN_URL.'/admintools/index.php', '', $MENU['ADMINTOOLS'], 'admintools', 1),
256
					array(ADMIN_URL.'/access/index.php', '', $MENU['ACCESS'], 'access', 1),
257
//					array('http://addons.websitebaker2.org/', '', 'WB-Addons', 'preferences', 1),
258
//					array('http://template.websitebaker2.org/', '', 'WB-Template', 'preferences', 1),
259
//					array('http://www.websitebaker.org/', '_blank', 'WebsiteBaker Home', '', 0),
260
//					array(ADMIN_URL.'/logout/index.php', '', $MENU['LOGOUT'], '', 0)
261
					);
262
		}
263

    
264
		$header_template->set_block('header_block', 'linkBlock', 'link');
265
		foreach($menu AS $menu_item)
266
		{
267
			$link = $menu_item[0];
268
			$target = ($menu_item[1] == '') ? '_self' : $menu_item[1];
269
			$title = $menu_item[2];
270
			$permission_title = $menu_item[3];
271
			$required = $menu_item[4];
272
			$replace_old = array(ADMIN_URL, WB_URL, '/', 'index.php');
273
			if($required == false || ($this->is_authenticated() && $this->get_link_permission($permission_title)) )
274
			{
275
				$header_template->set_var('LINK', $link);
276
				$header_template->set_var('TARGET', $target);
277
				// If link is the current section apply a class name
278
				if($permission_title == strtolower($this->section_name)) {
279
					$header_template->set_var('CLASS', $menu_item[3] . ' current');
280
					$header_template->set_var('STYLE', $menu_item[3] );
281
				} else {
282
					$header_template->set_var('CLASS', $menu_item[3] );
283
				}
284
				$header_template->set_var('TITLE', $title);
285
				// Print link
286
				$header_template->parse('link', 'linkBlock', true);
287
			}
288
		}
289
		$header_template->parse('header', 'header_block', false);
290
		$header_template->pparse('output', 'page');
291
		unset($header_template);
292
	}
293

    
294
	// Print the admin footer
295
		function print_footer($activateJsAdmin = false) {
296
		global $database,$starttime,$iPhpDeclaredClasses;
297
		$oTrans = Translate::getInstance();
298
		$oTrans->disableAddon();
299
		// include the required file for Javascript admin
300
		if($activateJsAdmin == true) {
301
			if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
302
				@include_once(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
303
			}
304
		}
305

    
306
		// Setup template object, parse vars to it, then parse it
307
		$footer_template = new Template(dirname($this->correct_theme_source('footer.htt')));
308
		$footer_template->set_file('page', 'footer.htt');
309
		$footer_template->set_block('page', 'footer_block', 'header');
310
		$footer_template->set_var(array(
311
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
312
						'WB_URL' => WB_URL,
313
						'ADMIN_URL' => ADMIN_URL,
314
						'THEME_URL' => THEME_URL,
315
			 ) );
316

    
317
		$footer_template->set_block('footer_block', 'show_debug_block', 'show_debug');
318

    
319
		$bDebug = (defined('DEBUG') ? DEBUG : false);
320
		$bDevInfo = (defined('DEV_INFOS') && (DEV_INFOS == true) && (1 == $this->get_user_id()) ? true : false);
321
//         if( $debug && (1 == $this->get_user_id()))
322
        if( $bDevInfo )
323
		{
324

    
325
			$footer_template->set_var('MEMORY', number_format(memory_get_peak_usage(true), 0, ',', '.').'&nbsp;Byte' );
326
//			$footer_template->set_var('MEMORY', number_format(memory_get_usage(true), 0, ',', '.').'&nbsp;Byte' );
327
			$footer_template->set_var('QUERIES', $database->getQueryCount );
328
			// $footer_template->set_var('QUERIES', 'disabled' );
329
	        $included_files =  get_included_files();
330
			$footer_template->set_var('INCLUDES', sizeof($included_files) );
331
	        $included_classes =  get_declared_classes();
332
			$footer_template->set_var('CLASSES', sizeof($included_classes)-$iPhpDeclaredClasses );
333

    
334
			$sum_classes = 0;
335
			$sum_filesize = 0;
336
			$footer_template->set_block('show_debug_block', 'show_block_list', 'show_list');
337
			$footer_template->set_block('show_block_list', 'include_block_list', 'include_list');
338
			// $bDebug = true;  for testing
339
			foreach($included_files as $filename)
340
			{
341
				if(!is_readable($filename)) { continue; }
342
				if($bDebug)
343
				{
344
					$footer_template->set_var('INCLUDES_ARRAY', str_replace(WB_PATH, '',$filename) );
345
					$footer_template->set_var('FILESIZE', number_format(filesize($filename), 0, ',', '.').'&nbsp;Byte');
346
					$footer_template->parse('include_list', 'include_block_list', true);
347
				}
348
				$sum_filesize += filesize($filename);
349
			}
350
			$footer_template->parse('show_list', 'show_block_list', true);
351

    
352
			$endtime = array_sum(explode(" ",microtime()));
353
			$iEndTime = $endtime;
354
			$iStartTime = $starttime;
355
			if(!$bDebug)
356
			{
357
				$footer_template->parse('show_list', '');
358
				$footer_template->parse('include_list', '');
359
			}
360

    
361
			$footer_template->set_var('FILESIZE', ini_get('memory_limit'));
362
			$footer_template->set_var('TXT_SUM_FILESIZE', 'Summary size of included files:&nbsp;');
363
			$footer_template->set_var('SUM_FILESIZE', number_format($sum_filesize, 0, ',', '.').'&nbsp;Byte');
364
			$footer_template->set_var('SUM_CLASSES', number_format($sum_classes, 0, ',', '.').'&nbsp;Byte');
365
			$footer_template->set_var('PAGE_LOAD_TIME', round($iEndTime-$iStartTime,3 ));
366
			$footer_template->set_var('DUMP_CLASSES', '<pre>'.var_export($included_classes,true).'</pre>');
367

    
368
			$footer_template->parse('show_debug', 'show_debug_block', true);
369
        } else {
370
			$footer_template->parse('show_debug', '');
371
			$footer_template->parse('show_list', '');
372

    
373
        }
374
		$footer_template->parse('header', 'footer_block', false);
375
		$footer_template->pparse('output', 'page');
376
		unset($footer_template);
377
	}
378

    
379
	// Return a system permission
380
	function get_permission($name, $type = 'system') {
381

    
382
		// Append to permission type
383
		$type .= '_permissions';
384
		// Check if we have a section to check for
385
		if($name == 'start') {
386
			return true;
387
		} else {
388
			// Set system permissions var
389
			$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
390
			// Set module permissions var
391
			$module_permissions = $this->get_session('MODULE_PERMISSIONS');
392
			// Set template permissions var
393
			$template_permissions = $this->get_session('TEMPLATE_PERMISSIONS');
394
			// Return true if system perm = 1
395
			if (isset($$type) && is_array($$type) && is_numeric(array_search($name, $$type))) {
396
				if($type == 'system_permissions') {
397
					return true;
398
				} else {
399
					return false;
400
				}
401
			} else {
402
				if($type == 'system_permissions') {
403
					return false;
404
				} else {
405
					return true;
406
				}
407
			}
408
		}
409

    
410
	}
411

    
412
 function get_user_details($user_id) {
413
  global $database;
414
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
415
  $sql  = 'SELECT `username`,`display_name`,`email` ';
416
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
417
  $sql .= 'WHERE `user_id`='.(int)$user_id;
418
  if( ($resUsers = $database->query($sql)) ) {
419
   if( ($recUser = $resUsers->fetchRow()) ) {
420
    $retval = $recUser;
421
   }
422
  }
423
  return $retval;
424
 }
425

    
426
    //
427
	function get_section_details( $section_id, $backLink = 'index.php' ) {
428
	global $database, $TEXT;
429
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'sections` ';
430
		$sql .= 'WHERE `section_id`='.intval($section_id);
431
		if(($resSection = $database->query($sql))){
432
			if(!($recSection = $resSection->fetchRow())) {
433
				$this->print_header();
434
				$this->print_error($TEXT['SECTION'].' '.$TEXT['NOT_FOUND'], $backLink, true);
435
			}
436
			} else {
437
				$this->print_header();
438
				$this->print_error($database->get_error(), $backLink, true);
439
			}
440
		return $recSection;
441
	}
442

    
443
	function get_page_details( $page_id, $backLink = 'index.php' ) {
444
		global $database, $TEXT;
445
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'pages` ';
446
		$sql .= 'WHERE `page_id`='.intval($page_id);
447
		if(($resPages = $database->query($sql))){
448
			if(!($recPage = $resPages->fetchRow())) {
449
			$this->print_header();
450
			$this->print_error($TEXT['PAGE'].' '.$TEXT['NOT_FOUND'], $backLink, true);
451
			}
452
		} else {
453
			$this->print_header();
454
			$this->print_error($database->get_error(), $backLink, true);
455
		}
456
		return $recPage;
457
	}
458

    
459
	function get_page_permission($page,$action='admin') {
460
		if($action != 'viewing') { $action = 'admin'; }
461
		$action_groups = $action.'_groups';
462
		$action_users  = $action.'_users';
463
		$groups = $users = '0';
464
		if(is_array($page)) {
465
			$groups = $page[$action_groups];
466
			$users  = $page[$action_users];
467
		} else {
468
			global $database;
469
			$sql  = 'SELECT `'.$action_groups.'`,`'.$action_users.'` ';
470
			$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
471
			$sql .= 'WHERE `page_id`='.(int)$page;
472
			if( ($res = $database->query($sql)) ) {
473
				if( ($rec = $res->fetchRow()) ) {
474
					$groups = $rec[$action_groups];
475
					$users  = $rec[$action_users];
476
				}
477
			}
478
		}
479
		return ($this->ami_group_member($groups) || $this->is_group_match($this->get_user_id(), $users));
480
	}
481

    
482
	// Returns a system permission for a menu link
483
	function get_link_permission($title) {
484
		$title = str_replace('_blank', '', $title);
485
		$title = strtolower($title);
486
		// Set system permissions var
487
		$system_permissions = $this->get_session('SYSTEM_PERMISSIONS');
488
		// Set module permissions var
489
		$module_permissions = $this->get_session('MODULE_PERMISSIONS');
490
		if($title == 'start') {
491
			return true;
492
		} else {
493
			// Return true if system perm = 1
494
			if(is_numeric(array_search($title, $system_permissions))) {
495
				return true;
496
			} else {
497
				return false;
498
			}
499
		}
500
	}
501

    
502
	// Function to add optional module Javascript or CSS stylesheets into the <body> section of the backend
503
	function register_backend_modfiles_body($file_id="js")
504
		{
505
		// sanity check of parameter passed to the function
506
		$file_id = strtolower($file_id);
507
		if($file_id !== "javascript" && $file_id !== "js")
508
		{
509
			return;
510
		}
511
		global $database;
512
        $body_links = "";
513
		// define default baselink and filename for optional module javascript and stylesheet files
514
		if($file_id == "js") {
515
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend_body.js" type="text/javascript"></script>';
516
			$base_file = "backend_body.js";
517
		}
518

    
519
        $sActionRequest = isset($_POST['tool']) ? $_POST['tool'] : null;
520
        $sActionRequest = isset($_GET['tool'])  ? $_GET['tool']  : $sActionRequest;
521

    
522
		// check if backend_body.js files needs to be included to the <body></body> section of the backend
523
		if(isset($sActionRequest))
524
			{
525
			// check if displayed page contains a installed admin tool
526
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
527
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($sActionRequest).'\'';
528
			$result = $database->query($sql);
529
			if($result->numRows())
530
				{
531
				// check if admin tool directory contains a backend_body.js file to include
532
				$tool = $result->fetchRow();
533
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file"))
534
				{
535
					// return link to the backend_body.js file
536
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
537
				}
538
			}
539
		} elseif(isset($_GET['page_id']) or isset($_POST['page_id']))
540
		{
541
			// check if displayed page in the backend contains a page module
542
			if (isset($_GET['page_id']))
543
			{
544
				$page_id = (int) addslashes($_GET['page_id']);
545
			} else {
546
				$page_id = (int) addslashes($_POST['page_id']);
547
			}
548
			// gather information for all models embedded on actual page
549
			$sql = 'SELECT `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
550
			$query_modules = $database->query($sql);
551
			while($row = $query_modules->fetchRow()) {
552
				// check if page module directory contains a backend_body.js file
553
				if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
554
					// create link with backend_body.js source for the current module
555
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
556
					// ensure that backend_body.js is only added once per module type
557
					if(strpos($body_links, $tmp_link) === false) {
558
						$body_links .= $tmp_link ."\n";
559
					}
560
				}
561
			}
562
			// write out links with all external module javascript/CSS files, remove last line feed
563
			return rtrim($body_links);
564
		}
565
	}
566

    
567

    
568
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
569
	function register_backend_modfiles($file_id="css") {
570
		// sanity check of parameter passed to the function
571
		$file_id = strtolower($file_id);
572
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
573
			return;
574
		}
575

    
576
		global $database;
577
		// define default baselink and filename for optional module javascript and stylesheet files
578
		$head_links = "";
579
		if($file_id == "css") {
580
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
581
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
582
			$base_file = "backend.css";
583
		} else {
584
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
585
			$base_file = "backend.js";
586
		}
587

    
588
        $sActionRequest = isset($_POST['tool']) ? $_POST['tool'] : null;
589
        $sActionRequest = isset($_GET['tool'])  ? $_GET['tool']  : $sActionRequest;
590

    
591
		// check if backend.js or backend.css files needs to be included to the <head></head> section of the backend
592
		if(isset($sActionRequest)) {
593
			// check if displayed page contains a installed admin tool
594
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'addons` ';
595
			$sql .= 'WHERE `type`=\'module\' AND `function`=\'tool\' AND `directory`=\''.addslashes($sActionRequest).'\'';
596
			$result = $database->query($sql);
597
			if($result->numRows()) {
598
				// check if admin tool directory contains a backend.js or backend.css file to include
599
				$tool = $result->fetchRow();
600
				if(file_exists(WB_PATH ."/modules/" .$tool['directory'] ."/$base_file")) {
601
        			// return link to the backend.js or backend.css file
602
					return str_replace("{MODULE_DIRECTORY}", $tool['directory'], $base_link);
603
				}
604
			}
605
		} elseif(isset($_GET['page_id']) || isset($_POST['page_id'])) {
606
			// check if displayed page in the backend contains a page module
607
			if (isset($_GET['page_id'])) {
608
				$page_id = (int)$_GET['page_id'];
609
			} else {
610
				$page_id = (int)$_POST['page_id'];
611
			}
612

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

    
617
    		while($row = $query_modules->fetchRow()) {
618
				// check if page module directory contains a backend.js or backend.css file
619
      		if(file_exists(WB_PATH ."/modules/" .$row['module'] ."/$base_file")) {
620
					// create link with backend.js or backend.css source for the current module
621
					$tmp_link = str_replace("{MODULE_DIRECTORY}", $row['module'], $base_link);
622
        			// ensure that backend.js or backend.css is only added once per module type
623
        			if(strpos($head_links, $tmp_link) === false) {
624
						$head_links .= $tmp_link ."\n";
625
					}
626
				}
627
    		}
628
    		// write out links with all external module javascript/CSS files, remove last line feed
629
			return rtrim($head_links);
630
		}
631
	}
632
}
(23-23/36)