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
|
<?php
/**
* @version $Header$
* @package install
* @subpackage functions
*/
// assign next step in installation process
$gBitSmarty->assign( 'next_step', $step );
$schema = $gBitInstaller->mPackages;
ksort( $schema );
$gBitSmarty->assign( 'schema', $schema );
// ===================== Post install table check =====================
// $dbTables is the output of BitSystem::verifyInstalledPackages() in
// install_inc.php and contains all tables that are not present in the database
// - even tables of packages that are not installed
$dbIntegrity = install_check_database_integrity( $dbTables );
// ===================== Permissions =====================
// check all permissions, compare them to each other and see if there are old
// permissions and ones that need to be inserted
$query = "SELECT * FROM `".BIT_DB_PREFIX."users_permissions` ORDER BY `package` ASC";
$result = $gBitInstaller->mDb->query( $query );
while( !$result->EOF ) {
foreach( $result->fields as $r ) {
$bitPerms[$result->fields['perm_name']][] = $r;
}
$bitPerms[$result->fields['perm_name']]['sql'][] = ( defined( 'ROLE_MODEL' ) ) ? "DELETE FROM `" . BIT_DB_PREFIX . "users_role_permissions` WHERE `perm_name`='" . $result->fields['perm_name'] . "'" : "DELETE FROM `" . BIT_DB_PREFIX . "users_group_permissions` WHERE `perm_name`='" . $result->fields['perm_name'] . "'";
$bitPerms[$result->fields['perm_name']]['sql'][] = "DELETE FROM `" . BIT_DB_PREFIX . "users_permissions` WHERE `perm_name`='" . $result->fields['perm_name'] . "'";
$result->MoveNext();
}
// we will make sure all the permission levels are what they should be. we will
// update these without consulting the user. this is purely backend stuff, has
// no outcome on the site itself but determines what the default permission
// level is. the user can never modify these settings.
foreach ( array_keys( $gBitInstaller->mPermHash ) as $perm ) {
// permission level is stored in [2]
$bindVars = [];
if (!empty( $bitPerms[$perm] ) && $gBitInstaller->mPermHash[$perm][2] != $bitPerms[$perm][2]) {
$query = "UPDATE `" . BIT_DB_PREFIX . "users_permissions` SET `perm_level` = ? WHERE `perm_name` = ?";
$bindVars[] = $gBitInstaller->mPermHash[$perm][2];
$bindVars[] = $perm;
$gBitInstaller->mDb->query( $query, $bindVars );
}
}
// compare both perm arrays with each other and work out what permissions need
// to be added and which ones removed
$insPerms = $delPerms = [];
foreach ( array_keys( $gBitInstaller->mPermHash ) as $perm ) {
if (!in_array( $perm, array_keys( $bitPerms ) )) {
if ($gBitInstaller->isInstalled( $gBitInstaller->mPermHash[$perm][3] )) {
$insPerms[$perm] = $gBitInstaller->mPermHash[$perm];
}
}
}
foreach ( array_keys( $bitPerms ) as $perm ) {
if (!in_array( $perm, array_keys( $gBitInstaller->mPermHash ) )) {
$delPerms[$perm] = $bitPerms[$perm];
}
}
$gBitSmarty->assign( 'delPerms', $delPerms );
$gBitSmarty->assign( 'insPerms', $insPerms );
// ===================== Services =====================
// check if we have installed more than one service of any given type
// DEPRECATED - this check never really made sense. The original purpose
// of the services was to have multiple under the same identifier to
// be able to invoke them all in one shot. Recent updates to the services
// flattens the array so that there is one and only one service set per
// service type. This could revert back with time. For now this is commented
// out.
/*
$serviceList = [];
if( !empty( $gLibertySystem->mServices ) ) {
foreach( $gLibertySystem->mServices as $service_name => $service ) {
if( count( $service ) > 1 ) {
$serviceList[$service_name] = $service;
}
}
}
*/
// ===================== Process Form =====================
// create missing tables if possible
if (!empty( $_REQUEST['create_tables'] ) && !empty( $dbIntegrity )) {
$gBitInstallDb = ADONewConnection( $gBitDbType );
if ($gBitInstallDb->Connect( $gBitDbHost, $gBitDbUser, $gBitDbPassword, $gBitDbType != 'pdo' ? $gBitDbName : NULL )) {
$dict = NewDataDictionary( $gBitInstallDb, 'firebird' );
if (!$gBitInstaller->mDb->getCaseSensitivity()) {
$dict->connection->nameQuote = '';
}
if (!empty( $gDebug ) || !empty( $_REQUEST['debug'] )) {
$gBitInstallDb->debug = 99;
}
// If we use MySql check which storage engine to use
$build = isset( $_SESSION['use_innodb'] )
? ( ( $_SESSION['use_innodb'] == true ) ? [ 'NEW', 'MYSQL' => 'ENGINE=INNODB' ] : [ 'NEW', 'MYSQL' => 'ENGINE=MYISAM' ] ) : 'NEW';
$tablePrefix = $gBitInstaller->getTablePrefix();
foreach( $dbIntegrity as $package => $info ) {
foreach( $info['tables'] as $table ) {
$completeTableName = $tablePrefix.$table['name'];
$sql = $dict->CreateTableSQL( $completeTableName, $gBitInstaller->mPackages[$package]['tables'][$table['name']], $build );
// Uncomment this line to see the create sql
//vd( $sql );
if( $sql ) {
$dict->ExecuteSQLArray( $sql );
}
}
}
}
}
// if any of the serviceList items have been unchecked, disable the appropriate packages
if( !empty( $_REQUEST['resolve_conflicts'] ) ) {
if( !empty( $gDebug ) || !empty( $_REQUEST['debug'] ) ) {
$gBitInstallDb->debug = 99;
}
// === Permissions
$fixedPermissions = [];
$permMap = [];
$permMap['basic'] = ANONYMOUS_TEAM_ID;
$permMap['registered'] = 3;
$permMap['editors'] = 2;
$permMap['admin'] = 1;
if( !empty( $_REQUEST['perms'] ) ) {
foreach( $_REQUEST['perms'] as $perm ) {
if( !empty( $delPerms[$perm] )) {
foreach( $delPerms[$perm]['sql'] as $sql ) {
$gBitInstaller->mDb->query( $sql );
}
$fixedPermissions[] = $delPerms[$perm];
}
if( !empty( $insPerms[$perm] )) {
$gBitInstaller->mDb->query( $insPerms[$perm]['sql'] );
$fixedPermissions[] = $insPerms[$perm];
if( !empty( $roleMap[$insPerms[$perm][2]] )) {
if ( defined( 'ROLE_MODEL' ) ) {
$gBitUser->assignPermissionToRole( $perm, $permMap[$insPerms[$perm][2]] );
} else {
$gBitUser->assignPermissionToRole( $perm, $permMap[$insPerms[$perm][2]] );
}
}
}
}
}
$gBitSmarty->assign( 'fixedPermissions', $fixedPermissions );
// === Services
$deActivated = [];
foreach( $serviceList as $service ) {
foreach( array_keys( $service ) as $package ) {
$packages = !empty( $_REQUEST['packages'] ) ? $_REQUEST['packages'] : [];
if( !in_array( $package, $packages )) {
$gBitSystem->storeConfig( 'package_'.$package, 'n', KERNEL_PKG_NAME );
$deActivated[] = $package;
}
}
}
$gBitSmarty->assign( 'next_step', $step + 1 );
$gBitSmarty->assign( 'deActivated', $deActivated );
// display the confirmation page
$app = '_done';
} elseif( !empty( $_REQUEST['skip'] ) ) {
// if there were no conflicts, we move on to the next page
header( "Location: ".$_SERVER['SCRIPT_NAME']."?step=".++$step );
die;
}
// make sure everything is up to date after the above changes
$dbTables = $gBitInstaller->verifyInstalledPackages( 'all' );
$dbIntegrity = install_check_database_integrity( $dbTables );
$gBitSmarty->assign( 'dbIntegrity', $dbIntegrity );
// $gBitSmarty->assign( 'serviceList', $serviceList );
/**
* function - install_check_database_integrity
*/
function install_check_database_integrity( $pDbTables ) {
global $gBitInstaller;
$ret = [];
if( !empty( $pDbTables['missing'] ) && is_array( $pDbTables['missing'] )) {
foreach( array_keys( $pDbTables['missing'] ) as $package ) {
// we can't use the 'installed' flag in $gBitInstaller->mPackages[$package] because that is set to 'not installed' as soon as a table is missing
if( count( $gBitInstaller->mPackages[$package]['tables'] ) > count( $pDbTables['missing'][$package] )) {
// at least one table is missing
$ret[$package] = [
'name' => ucfirst( $gBitInstaller->mPackages[$package]['name'] ),
'required' => $gBitInstaller->mPackages[$package]['required'],
];
foreach( $pDbTables['missing'][$package] as $table ) {
$ret[$package]['tables'][$table] = [
'name' => $table,
'sql' => $gBitInstaller->mPackages[$package]['tables'][$table],
];
}
}
}
}
return $ret;
}
|