Completed
Push — 14.2 ( 26c8f8...a8a1c7 )
by Ralf
40:44 queued 14:52
created

hook_config_validate.inc.php ➔ backup_dir()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 1
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Setup
4
 *
5
 * @link http://www.egroupware.org
6
 * @package setup
7
 * @author Miles Lott <[email protected]>
8
 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
9
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
10
 * @version $Id$
11
 */
12
13
/*
14
  Set global flag to indicate for which config settings we have equally named validation methods
15
*/
16
$GLOBALS['egw_info']['server']['found_validation_hook'] = array(
17
	'vfs_storage_mode',
18
	'temp_dir',
19
	'files_dir',
20
	'backup_dir',
21
	'mcrypt_algo',
22
	'ldap_search_filter',
23
	'auth_type',
24
);
25
26
/**
27
 * Validate different auth-types
28
 *
29
 * @param array $settings
30
 */
31
function auth_type($settings)
32
{
33
	switch($settings['auth_type'])
34
	{
35
		case 'mail':
36
			if (class_exists('Horde_Imap_Client_Socket') && !in_array($settings['mail_server_type'], array('pop', 'pops')))
37
			{
38
				return;	// we use Horde code instead of imap extension
39
			}
40
			try {
41
				check_load_extension('imap', true);
42
			}
43
			catch (Exception $ex)
44
			{
45
				$GLOBALS['config_error'] = $ex->getMessage();
46
			}
47
			break;
48
	}
49
}
50
51
/**
52
 * Set vfs_fstab depending from what the user selected for vfs_storage_mode
53
 *
54
 * @param array $settings
55
 */
56
function vfs_storage_mode($settings)
57
{
58
	switch($settings['vfs_storage_mode'])
59
	{
60
		case 'fs':
61
			config::save_value('vfs_fstab','','phpgwapi');
62
			break;
63
		case 'db':
64
			config::save_value('vfs_fstab', array(
65
				'/' => 'sqlfs://$host/?storage=db',
66
				'/apps' => 'links://$host/apps?storage=db',
67
			),'phpgwapi');
68
			break;
69
	}
70
}
71
72 View Code Duplication
function temp_dir($settings)
1 ignored issue
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
{
74
	if (!setup_detection::check_dir($settings['temp_dir'],$error_msg))
75
	{
76
		$GLOBALS['config_error'] = lang("Your temporary directory '%1' %2",$settings['temp_dir'],$error_msg);
77
	}
78
}
79
80 View Code Duplication
function files_dir($settings)
1 ignored issue
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
{
82
	if (!setup_detection::check_dir($settings['files_dir'],$error_msg,true))
83
	{
84
		$GLOBALS['config_error'] = lang("Your files directory '%1' %2",$settings['files_dir'],$error_msg);
85
	}
86
}
87
88
function backup_dir(&$settings)
89
{
90
	if (@is_writeable($settings['files_dir']) && empty($settings['backup_dir']))
91
	{
92
		$settings['backup_dir'] = $settings['files_dir'].'/db_backup';
93
	}
94
	if (!setup_detection::check_dir($settings['backup_dir'],$error_msg,true))
95
	{
96
		$GLOBALS['config_error'] = lang("Your backup directory '%1' %2",$settings['backup_dir'],$error_msg);
97
	}
98
}
99
100
function _mcrypt_test_module_mode($module,$mode)
101
{
102
	/* Data */
103
	$key = 'this is a very long key, even too long for the cipher';
104
	$plain_text = 'very important data';
105
106
	/* Open module, and create IV */
107
	if(!$GLOBALS['td'] = @mcrypt_module_open($module, '',$mode, ''))
108
	{
109
		@mcrypt_module_close($GLOBALS['td']);
110
		return False;
111
	}
112
	$key = substr($key, 0, mcrypt_enc_get_key_size($GLOBALS['td']));
113
	$iv_size = mcrypt_enc_get_iv_size($GLOBALS['td']);
114
	$iv = @mcrypt_create_iv($iv_size, MCRYPT_RAND);
115
116
	/* Initialize encryption handle */
117
	if(mcrypt_generic_init($GLOBALS['td'], $key, $iv) != -1)
118
	{
119
		/* Encrypt data */
120
		$c_t = mcrypt_generic($GLOBALS['td'], $plain_text);
121
		mcrypt_generic_deinit($GLOBALS['td']);
122
123
		// close the module
124
		mcrypt_module_close($GLOBALS['td']);
125
126
		/* Reinitialize buffers for decryption */
127
		/* Open module */
128
		$GLOBALS['td'] = mcrypt_module_open($module, '', $mode, '');
129
		$key = substr($key, 0, mcrypt_enc_get_key_size($GLOBALS['td']));
130
131
		mcrypt_generic_init($GLOBALS['td'], $key, $iv);
132
		$p_t = mdecrypt_generic($GLOBALS['td'], $c_t);
133
134
		/* Clean up */
135
		mcrypt_generic_deinit($GLOBALS['td']);
136
		mcrypt_module_close($GLOBALS['td']);
137
	}
138
	else
139
	{
140
		@mcrypt_module_close($GLOBALS['td']);
141
	}
142
	return rtrim($p_t) === $plain_text;
143
}
144
145
/* run a self-test through every listed cipher and mode - from robert at peakepro dot com (php.net manual) */
146
function mcrypt_check_sanity()
147
{
148
	$modes = mcrypt_list_modes();
149
	$algorithms = mcrypt_list_algorithms();
150
151
	foreach($algorithms as $cipher)
152
	{
153
		if(mcrypt_module_self_test($cipher))
154
		{
155
			$GLOBALS['ciphers'][$cipher] = array();
156
			foreach($modes as $mode)
157
			{
158
				if(_mcrypt_test_module_mode($cipher,$mode))
159
				{
160
					$result = True;
161
				}
162
				else
163
				{
164
					$result = False;
165
				}
166
				@$GLOBALS['ciphers'][$cipher][$mode] = $result;
167
			}
168
		}
169
	}
170
	ksort($GLOBALS['ciphers']);
171
	/* _debug_array($GLOBALS['ciphers']); */
172
}
173
174
function mcrypt_algo($settings)
175
{
176
	/* _debug_array($settings);exit; */
177
	if(@function_exists('mcrypt_list_modes'))
178
	{
179
		mcrypt_check_sanity();
180
		if(!@$GLOBALS['ciphers'][$settings['mcrypt_algo']][$settings['mcrypt_mode']])
181
		{
182
			$GLOBALS['config_error'] = lang('Invalid Mcrypt Algorithm/Mode combination');
183
		}
184
	}
185
}
186
187
/**
188
 * Add bracket around the ldap_search_filter, if admin forgot it
189
 *
190
 * @param array $settings
191
 */
192
function ldap_search_filter(&$settings)
193
{
194
	$filter =& $settings['ldap_search_filter'];
195
196
	if ($filter && $filter{0} != '(') $filter = '('.$filter.')';
197
}
198