Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         start
6
 * @author          Ryan Djurovich, 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: upgradePermissions.php 2070 2014-01-03 01:21:42Z darkviper $
13
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/groups/upgradePermissions.php $
14
 * @lastmodified    $Date: 2014-01-03 02:21:42 +0100 (Fri, 03 Jan 2014) $
15
 *
16
 */
17

    
18
/* -------------------------------------------------------- */
19
// Must include code to stop this file being accessed directly
20
if(!defined('WB_URL')) {
21
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
22
	throw new IllegalFileException();
23
}
24
/* -------------------------------------------------------- */
25

    
26
/**
27
 *
28
 *
29
 * @access public getSystemDefaultPermissions()
30
 * @param none
31
 * @return array System Default Permissions
32
 *
33
 */
34
function getSystemDefaultPermissions ()
35
{
36
	$retVal = array(
37
		'access' => 0,
38
		'addons' => 0,
39
		'admintools' => 0,
40
		'admintools_view' => 0,
41
		'groups' => 0,
42
		'groups_add' => 0,
43
		'groups_delete' => 0,
44
		'groups_modify' => 0,
45
		'groups_view' => 0,
46
		'languages' => 0,
47
		'languages_install' => 0,
48
		'languages_uninstall' => 0,
49
		'languages_view' => 0,
50
		'media' => 0,
51
		'media_create' => 0,
52
		'media_delete' => 0,
53
		'media_rename' => 0,
54
		'media_upload' => 0,
55
		'media_view' => 0,
56
		'modules' => 0,
57
		'modules_install' => 0,
58
		'modules_uninstall' => 0,
59
		'modules_view' => 0,
60
        'modules_advanced' => 0,
61
		'pages' => 0,
62
		'pages_add' => 0,
63
		'pages_add_l0' => 0,
64
		'pages_delete' => 0,
65
		'pages_intro' => 0,
66
		'pages_modify' => 0,
67
		'pages_settings' => 0,
68
		'pages_view' => 0,
69
		'preferences' => 1,
70
		'preferences_view' => 1,
71
		'settings' => 0,
72
		'settings_basic' => 0,
73
		'settings_advanced' => 0,
74
		'settings_view' => 0,
75
		'templates' => 0,
76
		'templates_install' => 0,
77
		'templates_uninstall' => 0,
78
		'templates_view' => 0,
79
		'users' => 0,
80
		'users_add' => 0,
81
		'users_delete' => 0,
82
		'users_modify' => 0,
83
		'users_view' => 0
84
	);
85
	return $retVal;
86
}
87

    
88
/* *****************************************************************************
89
 * Prepare $module_permissions for workout
90
 * @access public
91
 * @param object $admin: admin-object
92
 * @return array:
93
 */
94
	function set_module_permissions ($admin)
95
	{
96
		// Get template permissions
97
		$modules = array('');
98
		$dirs = scan_current_dir(WB_PATH.'/modules');
99

    
100
		if(is_array($admin->get_post('module_permissions')))
101
		{
102
			$modules = $admin->get_post('module_permissions');
103
		}
104
// foldername validation
105
 		array_walk($dirs['path'],'check_dir' );
106
// delete empty items
107
		foreach($dirs['path'] AS $key=>$val) {
108
			if(empty($dirs['path'][$key])) { unset($dirs['path'][$key]); }
109
		}
110
// list of unckecked modules directories
111
		$modules = array_diff ( $dirs['path'], $modules );
112
// reindex
113
		$modules = array_merge($modules);
114
		return $modules;
115
	}
116

    
117
/* *****************************************************************************
118
 * Prepare $template_permissions for workout
119
 * @access public
120
 * @param object $admin: admin-object
121
 * @return array:
122
 */
123
	function set_template_permissions ($admin)
124
	{
125
		// Get template permissions
126
		$templates = array();
127
		$dirs = scan_current_dir(WB_PATH.'/templates');
128
		if(is_array($admin->get_post('template_permissions')))
129
		{
130
			foreach($admin->get_post('template_permissions') AS $selected_name)
131
			{
132
				if( file_exists( WB_PATH.'/templates/'.$selected_name.'/info.php') && in_array ($selected_name, $dirs['path']) )
133
				{
134
					$templates[] = $selected_name;
135
				}
136
			}
137
		}
138
		$templates = (sizeof($templates) > 0) ? array_diff($dirs['path'], $templates) : $dirs['path'];
139
		// return $template_permissions = implode(',', $templates);
140
		return $templates;
141
	}
142

    
143
/* *****************************************************************************
144
 * Prepare $system_permissions for save
145
 * @access public
146
 * @param
147
 * @return string: parsed HTML-content
148
 */
149
	function set_system_permissions ($aSystemPermissions = array())
150
	{
151
		// Implode system permissions
152
		$aSystemPermissions = !is_array($aSystemPermissions) ? array() : $aSystemPermissions;
153
		$aSystemPermissionsSet = array();
154
		foreach($aSystemPermissions AS $key => $value)
155
		{
156
			if($value == true)
157
			{
158
				$aSystemPermissionsSet[] = $key;
159
			}
160
		}
161
		return implode(',', $aSystemPermissionsSet);
162
	}
163

    
164
/* *****************************************************************************
165
 * array_walk callback functions
166
 */
167

    
168
	if(!function_exists('check_dir'))
169
	{
170
		function check_dir(&$val, $key ) {
171
			$RetVal = null;
172
			$aArray[$key] = $val;
173
			$RetVal = array_slice ($aArray,!preg_match('/^[a-z]{1}[a-z][a-z_\-0-9]{2,}$/i', $val ));
174
			$RetVal = each ($RetVal);
175
			$val = $RetVal['value'];
176
		}
177
	}
178

    
179

    
180
	if(!function_exists('remove_underline')){
181
		function remove_underline(& $val, $key, $vars = array())
182
		{
183
			$val = rtrim($val, ',');
184
			$vars = explode ( '_', $val);
185
			$val = $vars[0];
186
		}
187
	}
188

    
189
	if(!function_exists('addons')){
190
		function addons(& $val, $key, $vars = '')
191
		{
192
			$val = rtrim($val, '_');
193
            $val = ($val == 'modules_view') || ($val == 'templates_view') || ($val == 'languages_view') ? 'addons' : $val;
194
		}
195
	}
196

    
197
	if(!function_exists('settings')){
198
		function settings(& $val, $key, $vars = '')
199
		{
200
            $val = ($val == 'settings_view') ? 'settings_basic' : $val;
201
//            $val  = ($val == 'settings_basic') || ($val == 'settings_advanced') ? 'settings_view' : $val1;
202
		}
203
	}
204

    
205
	if(!function_exists('access')){
206
		function access(& $val, $key, $vars = '')
207
		{
208
            $val = ($val == 'groups_view') || ($val == 'users_view') ? 'access' : $val;
209
		}
210
	}
211
	function convertArrayToString ($val=null)
212
	{
213
		$settings = '';
214
		if(is_array($val))
215
		{
216
			foreach( $val as $key => $value )
217
			{
218
				$settings .= trim($value.',','\'');
219
			}
220
		} else {
221
			$settings = $val;
222
		}
223
		return trim($settings,',');
224
	}
225

    
226
	function convertKeyArrayToString ($val=null)
227
	{
228
		$settings = '';
229
		if(is_array($val))
230
		{
231
			foreach( $val as $key => $value )
232
			{
233
				$settings .= trim($key.',','\'');
234
			}
235
		} else {
236
			$settings = $val;
237
		}
238
		return trim($settings,',');
239
	}
240

    
241
// ---------------------------------------
242
	function convertStringToArray ($val=null)
243
	{
244
		$array = array();
245
		$settings = '';
246
		if(!is_array($val)){
247
			$settings = explode(',', $val);
248
			foreach( $settings as $value )
249
			{
250
				$array[] = $value;
251
			}
252
			} else {
253
				$array = $val;
254
			}
255
		return $array;
256
	}
257
// ---------------------------------------
258
	function convertStringToKeyArray ($val=null)
259
	{
260
		$array = array();
261
		if(!is_array($val)){
262
			$settings = explode(',', $val);
263
			foreach( $settings as $value )
264
			{
265
				$array[$value] = 1;
266
			}
267
			} else {
268
				$array = $val;
269
			}
270
		return $array;
271
	}
272

    
273
// ---------------------------------------
274
// workout to upgrade the groups system_permissions
275
/**
276
 * get_system_permissions()
277
 *
278
 * @return
279
 */
280
 function get_system_permissions ($admin, $SystemPermissions = null )
281
{
282
	$retVal = null;
283
	$aValidAll = array();
284
	$aValidView = array();
285
	$aValidBlock = array();
286
	$aValidAddons = array();
287
	$aValidAccess = array();
288
	$aValidSettings = array();
289
	$aPermissions = array();
290
	$sValueType = '';
291
	$sTempPermissions = '';
292
	if($SystemPermissions==null) { return false; }
293

    
294
// be sure is the right string for working inside
295
	if(is_string($SystemPermissions)) {
296
		$SystemPermissions = convertStringToKeyArray($SystemPermissions);
297
	}
298
	if(is_array($SystemPermissions)&& sizeof($SystemPermissions)>0) {
299
		$aPermissions = convertStringToKeyArray($SystemPermissions);
300
//		$sTempPermissions = convertKeyArrayToString($aPermissions).',';
301
		$sTempPermissions = implode(",", $aPermissions).',';
302
	}
303
// workout setting preferences
304
	if($admin->is_group_match('preferences_view',$sTempPermissions))
305
	{
306
		$aPermissions[]    = 'preferences';
307
		$sTempPermissions .= 'preferences,';
308
	}
309
// workout setting admintools
310
	if($admin->is_group_match('admintools_view',$sTempPermissions))
311
	{
312
		$aPermissions[]    = 'admintools';
313
		$sTempPermissions .= 'admintools,';
314
	}
315
// search all data with *_view, if not found delete the permission block
316
	$patternView = '/[a-z]+_view/i';
317
	if(preg_match_all($patternView, $sTempPermissions, $array ))
318
	{
319
// build new Permissions kist, remove invaild entries, needed to disable checknoxes
320
		array_walk($array[0], 'remove_underline');
321
		$sValueType = array_unique($array[0]);
322
		foreach($sValueType as $key => $view )
323
		{
324
//build new permission string
325
		$regex = "/(($view)[a-z_0-9]*)\,/i";
326
		preg_match_all ($regex, $sTempPermissions, $aMatch);
327
		$aValidBlock = $aMatch[1];
328
		$aValidAll = array_merge($aValidAll,$aValidBlock);
329
		}
330
// set all missing/needed entries
331
		$aValidAddons = $aValidAll;
332
		$aValidAccess = $aValidAll;
333
		$aValidSettings = $aValidAll;
334
		array_walk($aValidAddons,   'addons');
335
		array_walk($aValidAccess,   'access');
336
		array_walk($aValidSettings, 'settings');
337
// merge all arays and set to POST ready for save and change to advanced modus
338
		$aSystem = array_merge_recursive( $sValueType, $aValidAll, $aValidBlock, $aValidSettings, $aValidAddons, $aValidAccess);
339
		$retVal = array_unique($aSystem);
340
		natsort($retVal);
341
// set correct index key
342
		$retVal = array_merge($retVal);
343
// convert to right format
344
		$retVal = array_fill_keys($retVal, 1);
345
	}
346

    
347
	$_POST['system_permissions'] = $retVal;
348
	return $retVal;
349
}
350
// ---------------------------------------
351
//print '<pre style="text-align: left;"><strong>function '.__FUNCTION__.'( '.''.' );</strong>  basename: '.basename(__FILE__).'  line: '.__LINE__.' -> <br />';
352
//print_r( $_POST ); print '</pre>';
353

    
354
/**
355
 * setSystemCheckboxes()
356
 *
357
 * @param mixed $tpl
358
 * @param mixed $permissions
359
 * @return
360
 */
361
function setSystemCheckboxes( &$tpl, $admin, $permissions = null )
362
{
363
	$array = array();
364
	$aSytemArray = getSystemDefaultPermissions();
365
	if(!is_array($permissions))
366
	{
367
		$array = convertStringToKeyArray($permissions);
368
	} else {
369
		$array = $permissions;
370
	}
371

    
372
	if ( true == (isset( $_POST['advanced_action']) && (( $_POST['advanced_action'] == 'no') || strpos( $_POST['advanced_action'], ">>") > 0 ) ) )
373
	{
374
	// set adbanced modus
375
		$tpl->parse('hidden_advanced_permission_list', '', true);
376
		$array = !is_array($array) ? array() : $array;
377
		$aPermissions = isset($_POST['system_permissions']) ? $_POST['system_permissions'] : array();
378
 		foreach($array AS $key => $value)
379
		{
380
	//		if(strpos($key,'_view')) { continue; }
381
		$checked='';
382
			if(array_key_exists($key, $aPermissions)) {
383
			 continue;
384
             }
385
			$tpl->set_var('SYS_NAME', "system_permissions[$key]" );
386
			$tpl->set_var('SYS_VALUE', 1 );
387
			$tpl->parse('hidden_advanced_permission_list', 'show_cmd_hidden_advanced_permission_list_block', true);
388
		}
389

    
390
	} else {
391
	// set basic modus
392
		$tpl->set_var('SYS_NAME', "none" );
393
		$tpl->set_var('SYS_VALUE', '' );
394
		$tpl->parse('hidden_permission_list', 'show_cmd_hidden_permission_list_block', true);
395
		$array = !is_array($array) ? array() : $array;
396
		foreach($array AS $key => $value)
397
		{
398
		$checked='';
399
			if(strpos($key,'_view')) {
400
			 continue;
401
             }
402
			$tpl->set_var('SYS_NAME', "system_permissions[$key]" );
403
			$tpl->set_var('SYS_VALUE', 1 );
404
			$tpl->parse('hidden_permission_list', 'show_cmd_hidden_permission_list_block', true);
405
		}
406
	}
407
	reset($array);
408
	// set checked
409
	foreach($array AS $key => $value)
410
	{
411
		$checked='';
412
        if( $key != '' )
413
		{
414
            $checked = 'checked="checked"';
415
		}
416
		$tpl->set_var('VALUE', 1);
417
		$tpl->set_var(($key.'_checked'), $checked);
418
	}
419

    
420
// clean html
421
    $result = array_diff_key($aSytemArray, $array);
422
	foreach($result AS $key => $value)
423
	{
424
		$tpl->set_var('VALUE', 1);
425
		$tpl->set_var(($key.'_checked'), '');
426
	}
427

    
428
	return $array;
429
}
430

    
431
/**
432
 * upgrade_group_system_permissions()
433
 *
434
 * @return void
435
 */
436
function upgrade_group_system_permissions ( )
437
{
438
	global $admin;
439
	$database = WbDatabase::getInstance();
440
	$aGroups = array();
441
	$sTempPermissions = '';
442
	$aTempPermissions = array();
443
	$aAllowedPermissions = array(
444
	'admintools','groups','languages','media','modules','pages','preferences','settings','templates','users'
445
	);
446
	$aPermissions = $aMatches = array();
447
	$sql  = 'SELECT `group_id`,`name`,`system_permissions` FROM `'.TABLE_PREFIX.'groups` ';
448
//	$sql .= 'WHERE `group_id` != 1 ';
449
	$sql .= 'ORDER BY `group_id` ';
450
	if($oRes = $database->query($sql) )
451
	{
452
		while( $aPage = $oRes->fetchRow(MYSQL_ASSOC) )
453
		{
454
			$sTempPermissions = convertKeyArrayToString (getSystemDefaultPermissions()).',';
455
			$sPermissions = $aPage['group_id']!= 1 ? $aPage['system_permissions'].',' : $sTempPermissions;
456
// check if old groups system_permissions format, there was no prferences
457
			if( !preg_match_all( '/(preferences[a-z_0-9]*)\,/iU', $sPermissions, $aMatches) )
458
			{
459
// fetch all known permission entries to set the permission_view
460
				foreach($aAllowedPermissions as $PermissionFound)
461
				{
462
					$aMatches = array();
463
					if( preg_match_all( "/(($PermissionFound)[a-z_0-9]*)\,/i", $sPermissions, $aMatches) )
464
					{
465
						$val1 = $admin->is_group_match("$PermissionFound".'_view',$sPermissions);
466
						$val2 = $admin->is_group_match("$PermissionFound",$sPermissions);
467
						if(!$val1 && $val2)
468
						{
469
							$sPermissions .= $PermissionFound.'_view,';
470
						}
471
					}
472
					$aTempPermissions = explode(',',$sPermissions);
473
					if(!$admin->is_group_match('preferences_view',$sPermissions))
474
					{
475
						$sPermissions .= 'preferences,preferences_view,';
476
					}
477
				}
478
			}
479
// upgrade all groups system permission
480
			$aTempPermissions = convertStringToArray(trim($sPermissions,','));
481
			natsort($aTempPermissions);
482
// reindex
483
			$aTempPermissions = array_merge(($aTempPermissions));
484
			$retVal = array_fill_keys($aTempPermissions, 1);
485
			$aPermissions[$aPage['name']] = get_system_permissions($admin, $retVal);
486
			$aGroups[$aPage['name']] = convertKeyArrayToString($aPermissions[$aPage['name']]);
487
// and update DB
488
			$sql  = 'UPDATE `'.TABLE_PREFIX.'groups` SET ';
489
			$sql .= '`system_permissions` =\''.$aGroups[$aPage['name']].'\' ';
490
			$sql .= 'WHERE `name` = \''.$aPage['name'].'\' ';
491
			if(!$database->query($sql) )
492
			{
493
			}
494
		}
495
	}
496
	return !$database->is_error();
497
}
(6-6/6)