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 2098 2014-02-11 01:37:03Z darkviper $
13
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.admin.php $
14
 * @lastmodified    $Date: 2014-02-11 02:37:03 +0100 (Tue, 11 Feb 2014) $
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
        $this->_oTrans->enableAddon('templates\\'.$this->_oReg->DefaultTheme);
59
    	if( $section_name != '##skip##' )
60
    	{
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', $this->_oTrans->MESSAGE_MEDIA_DIR_ACCESS_DENIED);
88
            	 	    $oTpl->set_var( 'PAGE_TITLE', $this->_oTrans->MESSAGE_ADMIN_INSUFFICIENT_PRIVELLIGES);
89
            	 	    $oTpl->set_var( 'BACK_LINK', $sBackLink );
90
            	 	    $oTpl->set_var( 'TEXT_BACK', $this->_oTrans->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 `'.$this->_oDb->TablePrefix.'users` '
105
    		     . 'WHERE `user_id`='.(int)$this->get_user_id();
106
            $user_language = preg_replace('/([a-z]{2}).*/i', '\1', strtoupper((string)$this->_oDb->getOne($sql)));
107
    		// obtain the admin folder (e.g. /admin)
108
    		$admin_folder = str_replace(WB_PATH, '', ADMIN_PATH);
109

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

    
124
    		// Auto header code
125
    		if($auto_header == true) {
126
    			$this->print_header();
127
    		}
128
    	}
129
	}
130

    
131
	// Print the admin header
132
	/**
133
	 *
134
	 * @param string $body_tags
135
	 * @return void
136
	 */
137
	function print_header($body_tags = '')
138
	{
139
		// $GLOBALS['FTAN'] = $this->getFTAN();
140
		$this->createFTAN();
141
		$sql = 'SELECT `value` FROM `'.$this->_oDb->TablePrefix.'settings` '
142
             . 'WHERE `name`=\'website_title\'';
143
        $title = (string)$this->_oDb->getOne($sql);
144
		// Setup template object, parse vars to it, then parse it
145
		$header_template = new Template(dirname($this->correct_theme_source('header.htt')) );
146
		$header_template->set_file('page', 'header.htt');
147
		$header_template->set_block('page', 'header_block', 'header');
148
        $header_template->set_var($this->_oTrans->getLangArray());
149
		if(defined('DEFAULT_CHARSET')) {
150
			$charset=DEFAULT_CHARSET;
151
		} else {
152
			$charset='utf-8';
153
		}
154

    
155
		// work out the URL for the 'View menu' link in the WB backend
156
		// if the page_id is set, show this page otherwise show the root directory of WB
157
		$view_url = WB_URL;
158
		if(isset($_GET['page_id'])) {
159
			// extract page link from the database
160
			$sql = 'SELECT `link` FROM `'.$this->_oDb->TablePrefix.'pages` '
161
			     . 'WHERE `page_id`='.intval($_GET['page_id']);
162
            $row = (string)$this->_oDb->getOne($sql);
163
            if ($row) { $view_url .= PAGES_DIRECTORY .$row. PAGE_EXTENSION; }
164
		}
165

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

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

    
239
			// @array ( $url, $target, $title, $page_permission, $permission_required )
240
			$menu = array(
241
//					array(ADMIN_URL.'/index.php', '', $MENU['START'], 'start', 1 ),
242
					array(ADMIN_URL.'/pages/index.php', '', $this->_oTrans->MENU_PAGES, 'pages', 1),
243
// 					array($view_url, '_blank', $MENU['FRONTEND'], 'pages', 1),
244
					array(ADMIN_URL.'/media/index.php', '', $this->_oTrans->MENU_MEDIA, 'media', 1),
245
					array(ADMIN_URL.'/addons/index.php', '', $this->_oTrans->MENU_ADDONS, 'addons', 1),
246
					array(ADMIN_URL.'/preferences/index.php', '', $this->_oTrans->MENU_PREFERENCES, 'preferences', 1),
247
					array(ADMIN_URL.'/settings/index.php', '', $this->_oTrans->MENU_SETTINGS, 'settings', 1),
248
					array(ADMIN_URL.'/admintools/index.php', '', $this->_oTrans->MENU_ADMINTOOLS, 'admintools', 1),
249
					array(ADMIN_URL.'/access/index.php', '', $this->_oTrans->MENU_ACCESS, 'access', 1),
250
//					array('http://addons.websitebaker2.org/', '', 'WB-Addons', 'preferences', 1),
251
//					array('http://template.websitebaker2.org/', '', 'WB-Template', 'preferences', 1),
252
//					array('http://www.websitebaker.org/', '_blank', 'WebsiteBaker Home', '', 0),
253
//					array(ADMIN_URL.'/logout/index.php', '', $MENU['LOGOUT'], '', 0)
254
					);
255
		}
256

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

    
288
	// Print the admin footer
289
	function print_footer($activateJsAdmin = false) {
290
		global $starttime, $iPhpDeclaredClasses;
291
		// include the required file for Javascript admin
292
		if($activateJsAdmin == true) {
293
			if(file_exists(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php')){
294
				@include_once(WB_PATH.'/modules/jsadmin/jsadmin_backend_include.php');
295
			}
296
		}
297
		// Setup template object, parse vars to it, then parse it
298
		$footer_template = new Template(dirname($this->correct_theme_source('footer.htt')));
299
		$footer_template->set_file('page', 'footer.htt');
300
		$footer_template->set_block('page', 'footer_block', 'header');
301
        $footer_template->set_var($this->_oTrans->getLangArray());
302
		$footer_template->set_var(array(
303
						'BACKEND_BODY_MODULE_JS' => $this->register_backend_modfiles_body('js'),
304
						'WB_URL' => WB_URL,
305
						'ADMIN_URL' => ADMIN_URL,
306
						'THEME_URL' => THEME_URL,
307
			 ) );
308

    
309
		$footer_template->set_block('footer_block', 'show_debug_block', 'show_debug');
310

    
311
		$bDebug = (defined('DEBUG') ? DEBUG : false);
312
		$bDevInfo = (defined('DEV_INFOS') && (DEV_INFOS == true) && (1 == $this->get_user_id()) ? true : false);
313
//         if( $debug && (1 == $this->get_user_id()))
314
        if( $bDevInfo )
315
		{
316

    
317
			$footer_template->set_var('MEMORY', number_format(memory_get_peak_usage(true), 0, ',', '.').'&nbsp;Byte' );
318
//			$footer_template->set_var('MEMORY', number_format(memory_get_usage(true), 0, ',', '.').'&nbsp;Byte' );
319
			$footer_template->set_var('QUERIES', $this->_oDb->getQueryCount );
320
			// $footer_template->set_var('QUERIES', 'disabled' );
321
	        $included_files =  get_included_files();
322
			$footer_template->set_var('INCLUDES', sizeof($included_files) );
323
	        $included_classes =  get_declared_classes();
324
			$footer_template->set_var('CLASSES', sizeof($included_classes)-$iPhpDeclaredClasses );
325

    
326
			$sum_classes = 0;
327
			$sum_filesize = 0;
328
			$footer_template->set_block('show_debug_block', 'show_block_list', 'show_list');
329
			$footer_template->set_block('show_block_list', 'include_block_list', 'include_list');
330
			// $bDebug = true;  for testing
331
			foreach($included_files as $filename)
332
			{
333
				if(!is_readable($filename)) { continue; }
334
				if($bDebug)
335
				{
336
					$footer_template->set_var('INCLUDES_ARRAY', str_replace(WB_PATH, '',$filename) );
337
					$footer_template->set_var('FILESIZE', number_format(filesize($filename), 0, ',', '.').'&nbsp;Byte');
338
					$footer_template->parse('include_list', 'include_block_list', true);
339
				}
340
				$sum_filesize += filesize($filename);
341
			}
342
			$footer_template->parse('show_list', 'show_block_list', true);
343

    
344
			$endtime = array_sum(explode(" ",microtime()));
345
			$iEndTime = $endtime;
346
			$iStartTime = $starttime;
347
			if(!$bDebug)
348
			{
349
				$footer_template->parse('show_list', '');
350
				$footer_template->parse('include_list', '');
351
			}
352

    
353
			$footer_template->set_var('FILESIZE', ini_get('memory_limit'));
354
			$footer_template->set_var('TXT_SUM_FILESIZE', 'Summary size of included files:&nbsp;');
355
			$footer_template->set_var('SUM_FILESIZE', number_format($sum_filesize, 0, ',', '.').'&nbsp;Byte');
356
			$footer_template->set_var('SUM_CLASSES', number_format($sum_classes, 0, ',', '.').'&nbsp;Byte');
357
			$footer_template->set_var('PAGE_LOAD_TIME', round($iEndTime-$iStartTime,3 ));
358
			$footer_template->set_var('DUMP_CLASSES', '<pre>'.var_export($included_classes,true).'</pre>');
359

    
360
			$footer_template->parse('show_debug', 'show_debug_block', true);
361
        } else {
362
			$footer_template->parse('show_debug', '');
363
			$footer_template->parse('show_list', '');
364

    
365
        }
366
		$footer_template->parse('header', 'footer_block', false);
367
		$footer_template->pparse('output', 'page');
368
		unset($footer_template);
369

    
370
	}
371

    
372
	// Return a system permission
373
	function get_permission($name, $type = 'system') {
374

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

    
403
	}
404

    
405
 function get_user_details($user_id) {
406
  global $database;
407
  $retval = array('username'=>'unknown','display_name'=>'Unknown','email'=>'');
408
  $sql  = 'SELECT `username`,`display_name`,`email` ';
409
  $sql .= 'FROM `'.TABLE_PREFIX.'users` ';
410
  $sql .= 'WHERE `user_id`='.(int)$user_id;
411
  if( ($resUsers = $database->query($sql)) ) {
412
   if( ($recUser = $resUsers->fetchRow()) ) {
413
    $retval = $recUser;
414
   }
415
  }
416
  return $retval;
417
 }
418

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

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

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

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

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

    
512
        $sActionRequest = isset($_POST['tool']) ? $_POST['tool'] : null;
513
        $sActionRequest = isset($_GET['tool'])  ? $_GET['tool']  : $sActionRequest;
514

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

    
560

    
561
	// Function to add optional module Javascript or CSS stylesheets into the <head> section of the backend
562
	function register_backend_modfiles($file_id="css") {
563
		// sanity check of parameter passed to the function
564
		$file_id = strtolower($file_id);
565
		if($file_id !== "css" && $file_id !== "javascript" && $file_id !== "js") {
566
			return;
567
		}
568

    
569
		global $database;
570
		// define default baselink and filename for optional module javascript and stylesheet files
571
		$head_links = "";
572
		if($file_id == "css") {
573
      	$base_link = '<link href="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.css"';
574
			$base_link.= ' rel="stylesheet" type="text/css" media="screen" />';
575
			$base_file = "backend.css";
576
		} else {
577
			$base_link = '<script src="'.WB_URL.'/modules/{MODULE_DIRECTORY}/backend.js" type="text/javascript"></script>';
578
			$base_file = "backend.js";
579
		}
580

    
581
        $sActionRequest = isset($_POST['tool']) ? $_POST['tool'] : null;
582
        $sActionRequest = isset($_GET['tool'])  ? $_GET['tool']  : $sActionRequest;
583

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

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

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