-
Notifications
You must be signed in to change notification settings - Fork 7
/
dbtoolkit_import.php
368 lines (314 loc) · 19 KB
/
dbtoolkit_import.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
if(!empty($_SESSION['appInstall'])){
if(!file_exists($_SESSION['appInstall'])){
unset($_SESSION['appInstall']);
}
}
if(!empty($_FILES['itfInstaller']['size'])){
$overrides = array('action'=>'itf_install', 'mimes'=> array('itf' => 'application/itf', 'dbt' => 'application/dbt'));
$_POST['action'] = 'itf_install';
$upload = wp_handle_upload($_FILES['itfInstaller'], $overrides);
$_SESSION['appInstall'] = $upload['file'];
}
?>
<div class="wrap ">
<img src="<?php echo WP_PLUGIN_URL . '/db-toolkit/images/dbtoolkit-logo.png'; ?>" name="DB-Toolkit" title="DB-Toolkit" align="absmiddle" />Application Installer
<br class="clear" /><br />
<div id="poststuff">
<?php
if(!empty($_SESSION['appInstall'])){
global $user_ID;
$pth = pathinfo($_SESSION['appInstall']);
//die;
$data = file_get_contents($_SESSION['appInstall']);
if($predata = @gzinflate($data)){
$data = $predata;
}
$data = unserialize(base64_decode($data));
if($pth['extension'] == 'itf'){
$exisits = get_option('_'.sanitize_title($data['appInfo']['name']).'_app');
if(!empty($ewxisits)){
echo '<p><strong>ERROR: App is already installed.</strong></p>';
echo '<p id="returnLink" style="display:block;"><a href="'.$_SERVER['REQUEST_URI'].'">Back to installer</p>';
unset($_SESSION['appInstall']);
return;
}
if(!empty($data['appInfo'])){
$Ext = pathinfo(basename($data['appInfo']['imageFile']));
if(!empty($data['logo'])){
$filename = sanitize_file_name($data['appInfo']['name'].'.'.$Ext['extension']);
$src = wp_upload_bits($filename, null, base64_decode($data['logo']));
$data['appInfo']['imageURL'] = $src['url'];
$data['appInfo']['imageFile'] = $src['file'];
}
update_option('_'.sanitize_title($data['appInfo']['name']).'_app', $data['appInfo']);
}
if(!empty($data['application'])){
echo '<p>Installing Application: <strong>'.$data['application'].'</strong></p>';
echo '<p id="createingInterfaces">Creating Interfaces...</p>';
if(!empty($data['tables'])){
echo '<p id="buildingTables">Building Tables...</p>';
}
echo '<p id="populatingApp">Populating App...</p>';
echo '<p id="installStatus"></p>';
echo '<p id="returnLink" style="display:none;"><a href="'.$_SERVER['REQUEST_URI'].'">Back to installer</p>';
$_SESSION['dataform']['OutScripts'] .= "
ajaxCall('core_createInterfaces', '".$_SESSION['appInstall']."', function(P){
if(P){
jQuery('#createingInterfaces').html('Creating Interfaces...<strong>COMPLETE</strong>');
ajaxCall('core_createTables', '".$_SESSION['appInstall']."', function(T){
if(T){
jQuery('#buildingTables').html('Building Tables...<strong>COMPLETE</strong>');
ajaxCall('core_populateApp', '".$_SESSION['appInstall']."', function(E){
if(E){
jQuery('#populatingApp').html('Populating App...<strong>COMPLETE</strong>');
jQuery('#installStatus').html('<strong>INSTALLATION COMPLETE</strong>');
jQuery('#returnLink').show('slow');
}
});
}
});
}else{
jQuery('#createingInterfaces').html('Creating Interfaces...<strong>ERROR: Could not create interfaces.</strong>');
}
});
";
}else{
unset($_SESSION['appInstall']);
echo '<p><strong>Invalid File.</strong></p>';
echo '<p id="returnLink" style="display:none;"><a href="'.$_SERVER['REQUEST_URI'].'">Back to installer</p>';
$_SESSION['dataform']['OutScripts'] .= "
jQuery('#returnLink').show('slow');
";
}
}
// New DBT format installation
if($pth['extension'] == 'dbt'){
echo '<h2>Installation Results</h2>';
// Extract Main App Definition and Settings
$data['MainApp'] = unserialize($data['MainApp']);
$data['AppSettings'] = unserialize($data['AppSettings']);
if(empty($data['MainApp'])){
$data['MainApp']['state'] = $data['AppSettings']['state'];
$data['MainApp']['name'] = $data['AppSettings']['name'];
$data['MainApp']['description'] = $data['AppSettings']['description'];
}
//vardump($data);
//die;
// Get App Key
$appKey = sanitize_title($data['MainApp']['name']);
// Check if its installed
if(get_option('_'.$appKey.'_app')){
unset($_SESSION['appInstall']);
echo '<strong>'.$data['MainApp']['name'].'</strong> is already Installed';
echo '<p id="returnLink" style="display:block;"><a href="'.$_SERVER['REQUEST_URI'].'">Back to installer</p>';
echo '</div></div>';
exit;
}
// Register App
$allApps = get_option('dt_int_Apps');
$allApps[$appKey] = $data['MainApp'];
update_option('dt_int_Apps', $allApps);
unset($data['MainApp']);
// Create Interfaces
foreach($data['Interfaces'] as $InterfaceID=>$cfg){
$cfg = unserialize($cfg);
$cfg['Content'] = unserialize(base64_decode($cfg['Content']));
// Apply System Prefix
array_walk_recursive($cfg['Content'], 'core_applySystemTables');
//Save config
$cfg['Content'] = base64_encode(serialize($cfg['Content']));
update_option($InterfaceID, $cfg);
}
unset($data['Interfaces']);
// Create Filter Locks if any
if(!empty($data['FilterLocks'])){
foreach($data['FilterLocks'] as $InterfaceLock=>$cfg){
$cfg = unserialize($cfg);
update_option($InterfaceLock, $cfg);
}
unset($data['FilterLocks']);
}
// Create Clusters
foreach($data['Clusters'] as $ClusterID=>$cfg){
$cfg = unserialize($cfg);
update_option($ClusterID, $cfg);
}
unset($data['Clusters']);
// Upload Logo
$newFileName = uniqid('dbtlgo').'.png';
if (isset($data['Logo'])) {
$logoFile = wp_upload_bits($newFileName, null, base64_decode($data['Logo']));
if(!empty($logoFile)){
$data['AppSettings']['imageURL'] = $logoFile['url'];
$data['AppSettings']['imageFile'] = $logoFile['file'];
}
unset($data['Logo']);
}
// Create Tables
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
foreach($data['Tables'] as $tableKey=>$tableStruct){
// decode table settings
$query = str_replace('{{wp_prefix}}', $wpdb->prefix, base64_decode($tableStruct));
dbDelta($query);
}
unset($data['Tables']);
// Create Default Data
foreach($data['Data'] as $Row){
$query = str_replace('{{wp_prefix}}', $wpdb->prefix, base64_decode($Row));
dbDelta($query);
}
unset($data['Data']);
$complete = true;
if(!empty($data['Bindings'])){
if(!empty($_POST['pageSettings'])){
$complete = false;
foreach($data['Bindings'] as $interface=>$pagename){
if($_POST['page_'.$interface] == 'newpage'){
$new_post = array(
'post_title' => $pagename,
'post_content' => '',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page'
);
$post_id = wp_insert_post($new_post);
}else{
$post_id = $_POST['binding'][$interface];
}
// update binding with new page ID
// check Page Bindings
//check previous binding
$OldBinding = get_option('_dbtbinding_'.$post_id);
if(!empty($OldBinding)){
//unbind old interface
$prvBindingCFG = get_option($OldBinding);
$prvBindingCFG['Content'] = unserialize(base64_decode($prvBindingCFG['Content']));
if($prvBindingCFG['Content']['_ItemBoundPage'] == $post_id){
$prvBindingCFG['Content']['_ItemBoundPage'] = 0;
$prvBindingCFG['Content'] = base64_encode(serialize($prvBindingCFG['Content']));
update_option($OldBinding, $prvBindingCFG);
}
}
//bind interface to new page//
$oldOptions = get_option($interface);
if(!empty($oldOptions)){
// remove bindings if there are any
$oldOptions['Content'] = unserialize(base64_decode($oldOptions['Content']));
$oldOptions['Content']['_ItemBoundPage'] = $post_id;
$oldOptions['Content'] = base64_encode(serialize($oldOptions['Content']));
$oldOptions['_ItemBound'] = $post_id;
update_option($interface, $oldOptions);
}
update_option('_dbtbinding_'.$post_id, $interface, false);
// Check Dependicies and change redirects
if(is_array($data['Dependencies'][$pagename])){
foreach($data['Dependencies'][$pagename] as $redirectInterface){
$redirectCFG = get_option($redirectInterface);
$redirectCFG['Content'] = unserialize(base64_decode($redirectCFG['Content']));
$redirectCFG['Content']['_ItemViewPage'] = $post_id;
$redirectCFG['Content'] = base64_encode(serialize($redirectCFG['Content']));
update_option($redirectInterface, $redirectCFG);
}
}
}
$complete = true;
}else{
$pageList = array();
$complete = false;
echo '<h2 style="margin-bottom:0;">Page Setup</h2>';
echo '<span class="description">These are pages that the application use to display content. You can assign them to new pages, or let the installer create them for you.</span>';
echo '<br /><br /><form name="importApplication" enctype="multipart/form-data" method="POST" action="'.$_SERVER['REQUEST_URI'].'">';
foreach($data['Bindings'] as $interface=>$pagename){
$icfg = get_option($interface);
//vardump($icfg);
//titles
echo '<div style="width:200px; float:left; padding-right:20px; border-right:1px dashed #ccc;">';
echo '<div><strong>'.$icfg['_ReportDescription'].'</strong></div>';
echo '<span class="description">'.$icfg['_ReportExtendedDescription'].' </span>';
echo '</div>';
// settings
echo '<div style="width:400px; float:left; padding-left:20px;">';
echo '<div style="padding:3px;"><input type="radio" id="newpage_'.$interface.'" name="page_'.$interface.'" value="newpage" checked="checked" /><label for="newpage_'.$interface.'"> Create a new page called: <strong>'.$pagename.'</strong></label></div>';
echo '<div style="padding:3px;"><input type="radio" id="existingpage_'.$interface.'" name="page_'.$interface.'" value="existing" /><label for="existingpage_'.$interface.'"> Use existing Page: '.wp_dropdown_pages( "name=binding[".$interface."]&echo=0&show_option_none=-select-").'</label></div>';
echo '</div>';
echo '<div style="clear:both;padding:15px 0;"></div>';
$pageList[$pagename] = $interface;
}
//if(empty($data['Dependencies'])){
echo '<input type="submit" value="Save Setting" name="pageSettings" class="button">';
echo '</form>';
//}
}
}
// check redirect pages (dependencies)
/*
if(!empty($data['Dependencies'])){
$complete = false;
if(!empty($_POST['pageSettings'])){
}else{
$complete = false;
echo '<h2>Link Pages</h2>';
echo '<span class="description">These are pages that the application pages link to.<br />You can assign them to new pages, or let the installer create them for you.</span>';
echo '<br /><br />';
foreach($data['Dependencies'] as $interface=>$pagename){
$icfg = get_option($interface);
//vardump($icfg);
//titles
echo '<div style="width:200px; float:left; padding-right:20px; border-right:1px dashed #ccc;">';
echo '<div><strong>'.$icfg['_ReportDescription'].'</strong></div>';
echo '<span class="description">'.$icfg['_ReportExtendedDescription'].' </span>';
echo '</div>';
// settings
echo '<div style="width:400px; float:left; padding-left:20px;">';
echo '<div style="padding:3px;"><input type="radio" id="newpage_'.$interface.'" name="page_'.$interface.'" value="newpage" checked="checked" /><label for="newpage_'.$interface.'"> Create a new page called: <strong>'.$pagename.'</strong></label></div>';
echo '<div style="padding:3px;"><input type="radio" id="existingpage_'.$interface.'" name="page_'.$interface.'" value="existing" /><label for="existingpage_'.$interface.'"> Use existing Page: '.wp_dropdown_pages( "name=binding[".$interface."]&echo=0&show_option_none=-select-").'</label></div>';
echo '</div>';
echo '<div style="clear:both;padding:15px 0;"></div>';
}
echo '<input type="submit" value="Save Setting" name="pageSettings" class="button">';
echo '</form>';
vardump($data);
die;
}
}
*/
if(!empty($complete)){
// Create Main App Settings
update_option('_'.$appKey.'_app', $data['AppSettings']);
unset($data['AppSettings']);
// Clear Session and end off.
unset($_SESSION['appInstall']);
echo '<p><strong>Installation Complete.</strong></p>';
echo '<p id="returnLink" style="display:block;"><a href="'.$_SERVER['REQUEST_URI'].'">Back to installer</p>';
}
}
}else{
?>
<form name="importApplication" enctype="multipart/form-data" method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<h4>Install a DB-Toolkit Application in .itf or .dbt format</h4>
<p class="install-help">If you have an application in .itf or .dbt format, you may install it by uploading it here.</p>
<input type="file" name="itfInstaller">
<input type="submit" value="Install Now" class="button">
</form>
<?php
}
?>
</div>
</div>
<?php
/*
global $user_ID;
$new_post = array(
'post_title' => $pagename,
'post_content' => '',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page'
);
$post_id = wp_insert_post($new_post);
*/
?>