/
YamlConfig.php
266 lines (219 loc) · 7.72 KB
/
YamlConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* $Header$
* $Id$
*/
/*
* a simple class of static methods to return some basic configuration data or import it
*
* forward looking it would be preferable if packages exposed methods to get data and
* this class could get those methods and call on them, rather than having to have sepecific functions
* in here
* date created 2009/9/1
* @author Will James <will@tekimaki.com>
* @version $Revision$
* @class YamlConfig
*/
class YamlConfig {
private $mInstance;
private function YamlConfig(){
}
public function getInstance(){
if( empty( $this->mInstance ) ){
$this->mInstance = new YamlConfig();
}
return $this->mInstance;
}
public static function processUploadFile( &$pParamHash ){
$pParamHash['config_log'] = array();
if( YamlConfig::verifyUpload( $pParamHash ) ){
foreach( $pParamHash['upload_process'] as $file ){
if( $hash = Horde_Yaml::loadFile( $file['tmp_name'] ) ){
// deal with anything that might be in this hash
// @Todo probably want to move this out of here eventually
// kernel_config settings
if( !empty( $hash['kernel_config'] ) ){
// parser is a little annoying when it comes to n and y - it reinterprets them as FALSE and TRUE
// we're lazy and dont want to regex the dump so lets try just flipping them back
foreach( $hash['kernel_config'] as $pkg=>$data ){
foreach( $hash['kernel_config'][$pkg] as $config=>$value ){
if( $value === TRUE || $value === FALSE ){
$hash['kernel_config'][$pkg][$config] = $value?'y':'n';
}
}
}
$pParamHash['config_data']['kernel_config'] = $hash['kernel_config'];
// store the configurations
YamlConfig::setKernelConfig( $pParamHash );
}
// themes_layouts settings
if( !empty( $hash['themes_layouts'] ) ){
$pParamHash['config_data']['themes_layouts'] = $hash['themes_layouts'];
// store the configurations
YamlConfig::setThemesLayouts( $pParamHash );
}
// users_permissions settings
if( !empty( $hash['users_permissions'] ) ){
$pParamHash['config_data']['users_permissions'] = $hash['users_permissions'];
// store the configurations
YamlConfig::setUsersPermissions( $pParamHash );
}
}
}
}
else{
$pParamHash['config_log']['ERRORS'] = "Upload verification failed. ".$pParamHash['errors']['files'];
}
return (
empty( $pParamHash['errors'] ) ||
count( $pParamHash['errors'] ) == 0
);
}
private function verifyUpload( &$pParamHash ){
if( !empty( $_FILES )) {
foreach( $_FILES as $key => $file ) {
if( !empty( $file['name'] ) && !empty( $file['tmp_name'] ) && is_uploaded_file( $file['tmp_name'] ) && empty( $file['error'] ) ) {
$pParamHash['upload_process'][$key] = $file;
}
}
}else{
$pParamHash['errors']['files'] = tra( '$_FILES is empty' );
}
return (
empty( $pParamHash['errors'] ) ||
count( $pParamHash['errors'] ) == 0
);
}
/* Package Specific handlers - these could become service handlers eventually */
// data from kernel_config table by package
public static function getKernelConfig( $pPackage ){
global $gBitSystem;
$data = array( 'kernel_config'=>array() );
$pkgs = array();
if( !empty( $pPackage ) && strtoupper( $pPackage ) != 'ALL' ){
if( in_array( $pPackage, array_keys($gBitSystem->mPackages) ) ){
$pkgs[$pPackage] = "y";
}
else{
$pParamHash['errors']['package'] = tra( 'Package not in system' );
}
}else{
$pkgs = &$gBitSystem->mPackages;
}
foreach( $pkgs as $pkg=>$hash ){
// hideous - but gBitSystem currently has no other way to return config by package name
$gBitSystem->mConfig = NULL;
$gBitSystem->loadConfig( $pkg );
ksort( $gBitSystem->mConfig );
$data['kernel_config'][$pkg] = $gBitSystem->mConfig;
}
ksort( $data );
// restore normal settings
$gBitSystem->mConfig = NULL;
$gBitSystem->loadConfig();
$ret = Horde_Yaml::dump( $data );
return $ret;
}
// data from themes layouts
public static function getThemesLayout( $pPackage ){
global $gBitThemes;
$layouts = $gBitThemes->getAllLayouts();
$data = array( 'themes_layouts'=>array() );
if( !empty( $pPackage ) && strtoupper( $pPackage ) != 'ALL' ){
if( in_array( $pPackage, array_keys($layouts) ) ){
$hash = array( $pPackage => $layouts[ $pPackage ] );
}
else{
$pParamHash['errors']['package'] = tra( 'No layout for this package exists.' );
}
}else{
$hash = $layouts;
}
foreach( $hash as $pkg=>$area ){
// layout modules are needlessly nested in areas - remove them
foreach( $area as $modules ){
foreach( $modules as $module ){
$data['themes_layouts'][$pkg][] = $module;;
}
}
}
$ret = Horde_Yaml::dump( $data );
return $ret;
}
// data from users
public static function getUsersPermissions( $pPackage ){
global $gBitSystem, $gBitUser;
// get a list of all groups and their users_permissions
$data = array( 'users_permissions'=>array() );
$listHash = array(
'only_root_groups' => TRUE,
'sort_mode' => !empty( $_REQUEST['sort_mode'] ) ? $_REQUEST['sort_mode'] : 'group_name_asc'
);
if( !empty( $pPackage ) && strtoupper( $pPackage ) != 'ALL' ){
$packages = array( $pPackage );
}else{
$packages = array_keys( $gBitSystem->mPackages );
}
$allPerms = $gBitUser->getGroupPermissions();
$allGroups = $gBitUser->getAllGroups( $listHash );
foreach( $allPerms as $perm=>$params ){
if( in_array( $params['package'], $packages ) ){
$data['users_permissions'][$perm] = array( 'description' => $params['perm_desc'], 'groups' => array() );
foreach( $allGroups as $group ){
if( in_array( $perm, array_keys( $group['perms'] ) ) ){
$data['users_permissions'][$perm]['groups'][] = $group['group_id'];
}
}
}
}
$ret = Horde_Yaml::dump( $data );
return $ret;
}
public static function setKernelConfig( &$pParamHash ){
global $gBitSystem;
if( !empty( $pParamHash['config_data'] ) && !empty( $pParamHash['config_data']['kernel_config'] ) ){
$hash = $pParamHash['config_data'];
foreach( $hash['kernel_config'] as $pkg=>$data ){
foreach( $hash['kernel_config'][$pkg] as $config=>$value ){
$gBitSystem->storeConfig( $config, $value, $pkg );
$pParamHash['config_log']['KERNEL::storeConfig'][$pkg][$config] = $value;
}
}
}
}
public static function setThemesLayouts( &$pParamHash ){
global $gBitThemes;
if( !empty( $pParamHash['config_data'] ) && !empty( $pParamHash['config_data']['themes_layouts'] ) ){
$hash = $pParamHash['config_data'];
foreach( $hash['themes_layouts'] as $pkg=>$modules ){
foreach( $modules as $module ){
$gBitThemes->storeModule( $module );
//storeModule modifies the module hash, so pick up the results from 'store' param
$pParamHash['config_log']['THEMES::storeModule'][$pkg][] = $module['store'];
}
}
}
}
public static function setUsersPermissions( &$pParamHash ){
global $gBitUser;
$listHash = array(
'only_root_groups' => TRUE,
'sort_mode' => !empty( $_REQUEST['sort_mode'] ) ? $_REQUEST['sort_mode'] : 'group_name_asc'
);
$allGroups = $gBitUser->getAllGroups( $listHash );
if( !empty( $pParamHash['config_data'] ) && !empty( $pParamHash['config_data']['users_permissions'] ) ){
$hash = $pParamHash['config_data'];
foreach( $hash['users_permissions'] as $perm=>$data ){
foreach( array_keys( $allGroups ) as $groupId ) {
if( in_array( $groupId, $data['groups'] ) ){
$gBitUser->assignPermissionToGroup( $perm, $groupId );
$pParamHash['config_log']['USERS::setPermissions'][$perm]['assign_to_group'][] = $groupId;
} else {
$gBitUser->removePermissionFromGroup( $perm, $groupId );
$pParamHash['config_log']['USERS::setPermissions'][$perm]['remove_from_group'][] = $groupId;
}
}
}
}
}
}