forked from CaMer0n/multisite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_config.php
446 lines (285 loc) · 9.58 KB
/
admin_config.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
// Generated e107 Plugin Admin Area
require_once('../../class2.php');
if (!getperms('P'))
{
e107::redirect('admin');
exit;
}
// e107::lan('multisite',true);
define('e_MULTISITE_CONFIG', e_SYSTEM_BASE."multisite.json");
define('e_MULTISITE_SQL', e_SYSTEM_BASE.'multisite.sql');
class multisite_adminArea extends e_admin_dispatcher
{
protected static $multisiteData = array();
protected $modes = array(
'main' => array(
'controller' => 'multisite_ui',
'path' => null,
'ui' => 'multisite_form_ui',
'uipath' => null
),
);
protected $adminMenu = array(
// 'main/prefs' => array('caption'=> LAN_PREFS, 'perm' => 'P'),
'main/configure' => array('caption'=> LAN_CONFIGURE, 'perm' => 'P'),
'main/create' => array('caption'=> "Create New Site", 'perm' => 'P')
);
protected $adminMenuAliases = array(
'main/edit' => 'main/list'
);
protected $menuTitle = 'Multi Site';
function init()
{
if(file_exists(e_MULTISITE_CONFIG) && $tmp = file_get_contents(e_MULTISITE_CONFIG))
{
self::$multisiteData = e107::unserialize($tmp);
$c = 0;
$links = array();
foreach(self::$multisiteData as $val)
{
if($val['haystack'] !== 'host')
{
continue;
}
$url = str_replace($_SERVER['HTTP_HOST'],$val['match'],e_REQUEST_SELF);
$links[$c] = array('caption'=> $val['name'], 'perm' => 'P', 'url'=>$url, 'query'=>'');
$c++;
}
if(!empty($links))
{
$this->adminMenu['div2'] = array( 'divider'=> true);
$this->adminMenu['div1'] = array('header'=>'Jump To:');
}
foreach($links as $k=>$l)
{
$this->adminMenu['other/'.$k] = $l;
}
}
}
public static function getMultisiteData()
{
return self::$multisiteData;
}
}
class multisite_ui extends e_admin_ui
{
protected $pluginTitle = 'Multiple Site';
protected $pluginName = 'multisite';
// protected $eventName = 'multisite-'; // remove comment to enable event triggers in admin.
protected $table = '';
protected $pid = '';
protected $perPage = 10;
protected $batchDelete = true;
protected $batchExport = true;
protected $batchCopy = true;
// protected $sortField = 'somefield_order';
// protected $orderStep = 10;
// protected $tabs = array('Tabl 1','Tab 2'); // Use 'tab'=>0 OR 'tab'=>1 in the $fields below to enable.
// protected $listQry = "SELECT * FROM `#tableName` WHERE field != '' "; // Example Custom Query. LEFT JOINS allowed. Should be without any Order or Limit.
protected $listOrder = ' DESC';
protected $fields = array(
// 'database' => array('title' => LAN_LZ_THEMEPREF_03, 'type'=>'text', 'writeParms'=>array('size'=>'xxlarge'),'help'=>''),
'newdb' => array('title' => "New Site Database", 'type'=>'dropdown', 'data'=>'str', 'writeParms'=>array()),
// 'cdn' => array('title' => 'CDN', 'type'=>'dropdown', 'writeParms'=>array('optArray'=>array( 'cdnjs' => 'CDNJS (Cloudflare)', 'jsdelivr' => 'jsDelivr')))
);
protected $fieldpref = array();
// protected $preftabs = array('General', 'Other' );
protected $prefs = array(
// 'active' => array('title'=> 'Active', 'tab'=>0, 'type'=>'boolean', 'data' => 'str', 'help'=>''),
);
public function init()
{
// Set drop-down values (if any).
$emptyDBs = $this->getDatabases('empty');
if(!empty($emptyDBs))
{
$this->fields['newdb']['writeParms']['optArray'] = $this->getDatabases('empty');
$this->fields['newdb']['writeParms']['default'] = 'blank';
}
else
{
$this->fields['newdb']['help'] = 'No empty databases found.';
}
if(!empty($_POST['etrigger_submit']) && !empty($_POST['newdb']))
{
if($error = $this->createSite($_POST['newdb'])) // no errors.
{
e107::getMessage()->addError($error);
}
else
{
e107::getMessage()->addSuccess('New Site Created!');
}
}
if(!empty($_POST['multisite_submit']) && !empty($_POST['ms']))
{
$this->saveResults($_POST['ms']);
$this->redirectAction('configure');
}
e107::getDebug()->log(defset('e_MULTISITE_IN_USE'));
$data = file_get_contents(e_BASE."e107_config.php");
if(strpos($data,'multisite/multisite.php') === false)
{
$text = '<p>Please add the following line to the end of your <b>e107_config.php</b> file. (before <b>/?></b> if it exists.)</p>';
$text .='<pre style="padding:20px">require_once($PLUGINS_DIRECTORY."multisite/multisite.php");</pre>';
e107::getMessage()->addInfo( $text);
}
}
private function createSite($db)
{
if(!file_exists(e_MULTISITE_SQL))
{
return ("Please place a full database dump of a working e107 installation into a new file: ".e_MULTISITE_SQL);
}
$dump = file_get_contents(e_MULTISITE_SQL);
if(empty($dump))
{
return e_MULTISITE_SQL." is empty!";
}
$sql = e107::getDb();
$error = '';
$sql->database($db);
if(!$sql->db_Query($dump))
{
$error = $sql->getLastErrorText();
}
$defaultDB = e107::getMySQLConfig('defaultdb');
$sql->database($defaultDB);
return !empty($error) ? $error : false;
}
private function saveResults($data)
{
$arr= array();
foreach($data as $k=>$val)
{
if(empty($val['match']) || empty($val['haystack']))
{
continue;
}
$val['active'] = empty($val['active']) ? 0 : 1;
$val['match'] = trim(str_replace('/','',$val['match']));
$arr[] = $val;
}
if(!empty($arr))
{
$json = e107::serialize($arr,'json');
// e107::getMessage()->addDebug(print_a($json,true));
if(!file_put_contents(e_MULTISITE_CONFIG, $json))
{
e107::getMessage()->addError("Unable to save! ".e_SYSTEM." is not writable!");
}
else
{
e107::getMessage()->addSuccess(LAN_SETSAVED);
}
}
}
private function getDatabases($mode=null)
{
$sql = e107::getDb();
$result = $sql->retrieve("SHOW DATABASES",true);
$dbs = array();
foreach($result as $val)
{
$key = $val['Database'];
if($key === 'mysql' || $key === 'information_schema' || $key === 'performance_schema' || $key === 'phpmyadmin')
{
continue;
}
$sql->database($val['Database']);
$tabs = $sql->retrieve("SHOW TABLES",true);
if($mode === 'empty')
{
if(!empty($tabs))
{
continue;
}
}
else
{
if(empty($tabs))
{
continue;
}
}
$dbs[$key] = $val['Database'];
}
$defaultDB = e107::getMySQLConfig('defaultdb');
$sql->database($defaultDB);
return $dbs;
}
public function configurePage()
{
$frm = $this->getUI();
$dbs = $this->getDatabases();
$options = array('host'=>'HTTP_HOST','url'=>'SUBDIR');
$curval = multisite_adminArea::getMultisiteData();
$text = $frm->open('multi','post');
$text .= "<table class='table table-striped table-bordered'>
<tr><th>Name</th><th>Search</th><th>Match</th><th colspan='2'>Database</th><th class='center'>In Use</th><th class='center'>Active</th></tr>";
for ($i = 0; $i <= 10; $i++)
{
$database = !empty($curval[$i]['mysql']['database']) ? $curval[$i]['mysql']['database'] : '';
$prefix = !empty($curval[$i]['mysql']['prefix']) ? $curval[$i]['mysql']['prefix'] : '';
$active = !empty($curval[$i]['active']) ? $curval[$i]['active'] : 0;
$inuse = (!empty($database) && $database === defset('e_MULTISITE_IN_USE')) ? ADMIN_TRUE_ICON : "";
$text .= "<tr>
<td>".$frm->text('ms['.$i.'][name]', varset($curval[$i]['name']), 80,'size=block-level')."</td>
<td style='width:200px'>".$frm->select('ms['.$i.'][haystack]',$options, varset($curval[$i]['haystack']), 'size=block-level&default=blank' )."</td>
<td>".$frm->text('ms['.$i.'][match]', varset($curval[$i]['match']), 80,array('size'=>'block-level', 'pattern'=>''))."<div class='field-help'>eg. sub.mydomain.com or mydomain.com or regex to match sub-directory</td>
<td style='width:100px'>".$frm->text('ms['.$i.'][mysql][prefix]', $prefix, 25, 'size=block-level&placeholder=e107_' )."<div class='field-help'>Database Prefix. Leave blank to use 'e107_'</td>
<td style='width:200px'>".$frm->select('ms['.$i.'][mysql][database]',$dbs, $database, 'size=block-level&default=blank' )."</td>
<td class='center' style='width:100px'>".$inuse."</td>
<td class='center' style='width:10%'>".$frm->radio_switch('ms['.$i.'][active]',$active, strtoupper(LAN_ON), strtoupper(LAN_OFF),array('switch'=>'small'))."</td>
</tr>";
}
$text .= "</table>";
$text .= "<div class='buttons-bar center'>".$frm->button('multisite_submit',1,'submit',LAN_SAVE)."</div>";
$text .= $frm->close();
return $text;
}
// ------- Customize Create --------
public function beforeCreate($new_data,$old_data)
{
return $new_data;
}
public function afterCreate($new_data, $old_data, $id)
{
// do something
}
public function onCreateError($new_data, $old_data)
{
// do something
}
// ------- Customize Update --------
public function beforeUpdate($new_data, $old_data, $id)
{
return $new_data;
}
public function afterUpdate($new_data, $old_data, $id)
{
// do something
}
public function onUpdateError($new_data, $old_data, $id)
{
// do something
}
/*
// optional - a custom page.
public function customPage()
{
$text = 'Hello World!';
$otherField = $this->getController()->getFieldVar('other_field_name');
return $text;
}
*/
}
class multisite_form_ui extends e_admin_form_ui
{
}
new multisite_adminArea();
require_once(e_ADMIN."auth.php");
e107::getAdminUI()->runPage();
require_once(e_ADMIN."footer.php");
exit;