-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_config.php
583 lines (453 loc) · 17.3 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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?php
// Generated e107 Plugin Admin Area
require_once('../../class2.php');
if (!getperms('P'))
{
e107::redirect('admin');
exit;
}
// e107::lan('githubSync',true);
e107::coreLan('db', true);
define("ADMIN_GITSYNC_ICON", e107::getParser()->toGlyph('fa-file-text-o', array('fw' => 1)));
class githubSync_adminArea extends e_admin_dispatcher
{
protected $modes = array(
'main' => array(
'controller' => 'github_sync_ui',
'path' => null,
'ui' => 'github_sync_form_ui',
'uipath' => null
),
);
protected $adminMenu = array(
'main/list' => array('caption' => LAN_MANAGE, 'perm' => 'P'),
'main/create' => array('caption' => LAN_CREATE, 'perm' => 'P'),
// 'main/div0' => array('divider'=> true),
// 'main/custom' => array('caption'=> 'Custom Page', 'perm' => 'P'),
);
protected $adminMenuAliases = array(
'main/edit' => 'main/list'
);
protected $menuTitle = 'Github Sync';
}
class github_sync_ui extends e_admin_ui
{
private $sync_organization = '';
private $sync_repo = '';
private $sync_branch = '';
private $sync_type = '';
private $sync_id = '';
protected $pluginTitle = 'Github Sync';
protected $pluginName = 'githubSync';
// protected $eventName = 'githubSync-github_sync'; // remove comment to enable event triggers in admin.
protected $table = 'github_sync';
protected $pid = 'id';
protected $perPage = 20;
protected $batchDelete = true;
protected $batchExport = true;
protected $batchCopy = true;
// protected $sortField = 'somefield_order';
// protected $sortParent = 'somefield_parent';
// protected $treePrefix = 'somefield_title';
// 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 = 'id DESC';
protected $fields = array(
'checkboxes' => array('title' => '', 'type' => null, 'data' => null, 'width' => '5%', 'thclass' => 'center', 'forced' => 'value', 'class' => 'center', 'toggle' => 'e-multiselect', 'readParms' => array(), 'writeParms' => array(),),
'id' => array('title' => LAN_ID, 'type' => 'number', 'data' => 'int', 'width' => '5%', 'help' => '', 'readParms' => array(), 'writeParms' => array(), 'class' => 'left', 'thclass' => 'left',),
'type' => array('title' => LAN_TYPE, 'type' => 'dropdown', 'data' => 'safestr', 'width' => 'auto', 'batch' => 'value', 'filter' => 'value', 'inline' => 'value', 'help' => '', 'readParms' => array(), 'writeParms' => array(), 'class' => 'left', 'thclass' => 'left',),
'organization' => array(
'title' => 'Organization', 'type' => 'text', 'data' => 'safestr', 'width' => 'auto',
'filter' => 'value', 'help' => 'e107Inc, Jimako-e107-plugins', 'readParms' => array(), 'writeParms' => array(),
'class' => 'left', 'thclass' => 'left',
),
'repo' => array('title' => 'Repo', 'type' => 'text', 'data' => 'safestr', 'width' => 'auto', 'filter' => 'value', 'help' => '', 'readParms' => array(), 'writeParms' => array(), 'class' => 'left', 'thclass' => 'left',),
'branch' => array('title' => 'Branch', 'type' => 'text', 'data' => 'safestr', 'width' => 'auto', 'filter' => 'value', 'help' => '', 'readParms' => array(), 'writeParms' => array(), 'class' => 'left', 'thclass' => 'left',),
'folder' => array('title' => 'Folder', 'type' => 'text', 'data' => 'safestr', 'width' => 'auto', 'filter' => 'value', 'help' => 'Folder name if different than repo name', 'readParms' => array(), 'writeParms' => array(), 'class' => 'left', 'thclass' => 'left',),
'lastsynced' => array('title' => 'Last Synced', 'type' => 'datestamp', 'writeParms' => 'type=datetime', 'readonly' => true, 'noedit' => true, 'data' => 'int', 'readParms' => array(), 'class' => 'left', 'thclass' => 'left',),
'note' => array('title' => 'Note', 'type' => 'textarea', 'data' => 'str', 'readParms' => array(), 'class' => 'left', 'thclass' => 'left',),
'options' => array('title' => LAN_OPTIONS, 'type' => 'method', 'data' => null, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center last', 'forced' => 'value', 'readParms' => array(), 'writeParms' => array(),),
);
protected $fieldpref = array('type', 'organization', 'repo', 'branch', 'folder', 'note', 'lastsynced');
// protected $preftabs = array('General', 'Other' );
protected $prefs = array();
public function __construct($request, $response, $params = [])
{
parent::__construct($request, $response, $params = []);
if (!empty($_POST['githubSyncProcess']))
{
$this->getRequest()->setAction('synced');
$id = e107::getParser()->filter($_GET['id'], "int");
$data = e107::getDb()->retrieve('github_sync', '*', 'WHERE id=' . $id);
$this->sync_organization = $data['organization'];
$this->sync_repo = $data['repo'];
$this->sync_branch = $data['branch'];
$this->sync_type = $data['type'];
$this->sync_id = $id;
$this->sync_folder = !empty($data['folder']) ? $data['folder'] : $this->sync_repo;
if($this->sync_type == "core") {
$this->sync_folder = $this->sync_repo;
}
if ($this->sync_type == "themepack")
{
$this->sync_folder = $this->sync_repo;
}
if ($this->sync_type == "pluginspack")
{
$this->sync_folder = $this->sync_repo;
}
if ($this->sync_type == "languagepack")
{
$this->sync_folder = $this->sync_repo;
}
}
}
public function init()
{
// This code may be removed once plugin development is complete.
if (!e107::isInstalled('githubSync'))
{
e107::getMessage()->addWarning("This plugin is not yet installed. Saving and loading of preference or table data will fail.");
}
// Set drop-down values (if any).
$this->fields['type']['writeParms']['optArray'] = array(
'core' => 'core',
'plugin' => 'plugin',
'theme' => 'theme',
'themepack' => 'Theme + plugins - 2 folders',
'pluginspack' => 'More plugins in one repo in e107_plugins folder',
'languagepack' => 'Language pack - 3 folders',
); // Example Drop-down array.
}
// ------- 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
}
// left-panel help menu area. (replaces e_help.php used in old plugins)
public function renderHelp()
{
$caption = LAN_HELP;
$text = 'Some help text';
return array('caption' => $caption, 'text' => $text);
}
public function syncPage()
{
$frm = e107::getForm();
$mes = e107::getMessage();
$pref = e107::pref();
$key = 'github';
$val['label'] = DBLAN_112;
$val['diz'] = DBLAN_115;
//get data
$id = e107::getParser()->filter($_GET['id'], "int");
$data = e107::getDb()->retrieve('github_sync', '*', 'WHERE id=' . $id);
$organization = $data['organization'];
$repo = $data['repo'];
$branch = $data['branch'];
$text = "<h4 style='margin-bottom:3px'><a href='" . e_SELF . '?mode=' . $key . "' title=\"" . $val['label'] . '">' . $val['label'] . '</a></h4><small>' . $val['diz'] . '</small>';
if (!getperms('0'))
{
$text = e107::getMessage()->addError('Only main admin can use this functionality!');
$text = $mes->render();
return $text;
}
$remotefile = "https://codeload.github.com/{$organization}/{$repo}/zip/{$branch}";
$note = 'You are syncing with repo: <b>' . $remotefile;
$note .= '</b><br>You can put this URL to the browser and download the file manually. If you click on the button below, you will overwrite existing files.';
$note .= '</b><br>If there are ignored files, run sync second time - it happens if new folders are created at first.';
e107::getMessage()->addWarning($note);
$text = $mes->render();
$min_php_version = '7.4';
if (version_compare(PHP_VERSION, $min_php_version, '<'))
{
$mes->addWarning('The minimum required PHP version is <strong>' . $min_php_version . '</strong>. You are using PHP version <strong>' . PHP_VERSION . '</strong>. <br /> Syncing with Github has been disabled to avoid broken fuctionality.'); // No need to translate, developer mode only
}
else
{
$message = $frm->open('githubSync');
$message .= '<p>' . DBLAN_116 . ' <b>' . e_SYSTEM . 'temp</b> ' . DBLAN_117 . ' </p>';
$message .= $frm->button('githubSyncProcess', 1, 'delete', DBLAN_113);
$message .= $frm->close();
$mes->addInfo($message);
}
$text .= $mes->render();
return $text;
}
public function syncedPage()
{
$result = $this->unzipGithubArchive();
if ($result === false)
{
e107::getMessage()->addError(DBLAN_118);
return null;
}
$success = $result['success'];
$error = $result['error'];
$skipped = $result['skipped'];
// $message = e107::getParser()->lanVars(DBLAN_121, array('x'=>$oldPath, 'y'=>$newPath));
if (!empty($success))
{
e107::getMessage()->addSuccess(print_a($success, true));
}
if (!empty($skipped))
{
e107::getMessage()->setTitle('Skipped', E_MESSAGE_INFO)->addInfo(print_a($skipped, true));
}
if (!empty($error))
{
//e107::getMessage()->addError(print_a($error,true));
e107::getMessage()->setTitle('Ignored', E_MESSAGE_WARNING)->addWarning(print_a($error, true));
}
e107::getRender()->tablerender(SEP . DBLAN_112, e107::getMessage()->render());
e107::getCache()->clearAll('system');
}
/**
* Download and extract a zipped copy of e107 plugin. Copy of method from file_class.php
*
* @param string $url "core" to download the e107 core from Git master or
* a custom download URL
* @param string $destination_path The e107 root where the downloaded archive should be extracted,
* with a directory separator at the end
*
* @return array|bool FALSE on failure;
* An array of successful and failed path extractions
*/
public function unzipGithubArchive($url = 'plugin', $destination_path = e_BASE)
{
$organization = $this->sync_organization;
$repo = $this->sync_repo;
$branch = $this->sync_branch;
$folder = $this->sync_folder;
$newFolders = array();
if ($this->sync_type == 'plugin')
{
$destination_path = $destination_path . e107::getFolder('PLUGINS');
$localfile = "{$repo}.zip";
$newFolders = [
"{$repo}-{$branch}" => $destination_path . "{$folder}",
];
}
elseif ($this->sync_type == 'theme')
{
$destination_path = $destination_path . e107::getFolder('THEMES');
$localfile = "{$repo}.zip";
$newFolders = [
"{$repo}-{$branch}" => $destination_path . "{$folder}",
];
}
elseif ($this->sync_type == 'core')
{
$localfile = "{$folder}-{$branch}.zip";
$destination_path = e_BASE;
}
elseif ($this->sync_type == 'themepack')
{
$localfile = "{$folder}-{$branch}.zip";
$destination_path = e_BASE;
}
elseif ($this->sync_type == 'pluginspack')
{
$localfile = "{$folder}-{$branch}.zip";
$destination_path = e_BASE;
}
elseif ($this->sync_type == 'languagepack')
{
$localfile = "{$folder}-{$branch}.zip";
$destination_path = e_BASE;
}
else return false;
$remotefile = "https://codeload.github.com/{$organization}/{$repo}/zip/{$branch}";
$excludes = [
"{$repo}-{$branch}/.codeclimate.yml",
"{$repo}-{$branch}/.editorconfig",
"{$repo}-{$branch}/.gitignore",
"{$repo}-{$branch}/.gitmodules",
"{$repo}-{$branch}/CONTRIBUTING.md", // moved to ./.github/CONTRIBUTING.md
"{$repo}-{$branch}/LICENSE",
// "{$repo}-{$branch}/README.md",
"{$repo}-{$branch}/composer.json",
"{$repo}-{$branch}/composer.lock",
"{$repo}-{$branch}/install.php",
"{$repo}-{$branch}/favicon.ico",
"{$repo}-{$branch}/e107_config.php",
];
$excludeMatch = [
'/.github/',
'/e107_tests/',
];
// Delete any existing file.
if (file_exists(e_TEMP . $localfile))
{
unlink(e_TEMP . $localfile);
}
$result = e107::getFile()->getRemoteFile($remotefile, $localfile);
if ($result === false)
{
return false;
}
chmod(e_TEMP . $localfile, 0755);
require_once e_HANDLER . 'pclzip.lib.php';
$zipBase = str_replace('.zip', '', $localfile); // eg. e107-master
$excludes[] = $zipBase;
if ($this->sync_type == 'core')
{
$newFolders = array(
$zipBase . '/e107_admin/' => $destination_path . e107::getFolder('ADMIN'),
$zipBase . '/e107_core/' => $destination_path . e107::getFolder('CORE'),
$zipBase . '/e107_docs/' => $destination_path . e107::getFolder('DOCS'),
$zipBase . '/e107_handlers/' => $destination_path . e107::getFolder('HANDLERS'),
$zipBase . '/e107_images/' => $destination_path . e107::getFolder('IMAGES'),
$zipBase . '/e107_languages/' => $destination_path . e107::getFolder('LANGUAGES'),
$zipBase . '/e107_media/' => $destination_path . e107::getFolder('MEDIA'),
$zipBase . '/e107_plugins/' => $destination_path . e107::getFolder('PLUGINS'),
$zipBase . '/e107_system/' => $destination_path . e107::getFolder('SYSTEM'),
$zipBase . '/e107_themes/' => $destination_path . e107::getFolder('THEMES'),
$zipBase . '/e107_web/' => $destination_path . e107::getFolder('WEB'),
$zipBase . '/' => $destination_path
);
}
if ($this->sync_type == 'themepack')
{
$newFolders = array(
$zipBase . '/e107_plugins/' => $destination_path . e107::getFolder('PLUGINS'),
$zipBase . '/e107_themes/' => $destination_path . e107::getFolder('THEMES'),
$zipBase . '/' => $destination_path
);
}
if ($this->sync_type == 'pluginspack')
{
$newFolders = array(
$zipBase . '/e107_plugins/' => $destination_path . e107::getFolder('PLUGINS'),
$zipBase . '/' => $destination_path
);
}
if ($this->sync_type == 'languagepack')
{
$newFolders = array(
$zipBase . '/e107_languages/' => $destination_path . e107::getFolder('LANGUAGES'),
$zipBase . '/e107_plugins/' => $destination_path . e107::getFolder('PLUGINS'),
$zipBase . '/e107_themes/' => $destination_path . e107::getFolder('THEMES'),
$zipBase . '/' => $destination_path
);
}
$srch = array_keys($newFolders);
$repl = array_values($newFolders);
$archive = new PclZip(e_TEMP . $localfile);
$unarc = ($fileList = $archive->extract(PCLZIP_OPT_PATH, e_TEMP, PCLZIP_OPT_SET_CHMOD, 0755)); // Store in TEMP first.
$error = [];
$success = [];
$skipped = [];
foreach ($unarc as $k => $v)
{
if (
$this->matchFound($v['stored_filename'], $excludeMatch) ||
in_array($v['stored_filename'], $excludes)
)
{
$skipped[] = $v['stored_filename'];
continue;
}
$oldPath = $v['filename'];
$newPath = str_replace($srch, $repl, $v['stored_filename']);
if ($v['folder'] == 1 && is_dir($newPath))
{
// $skipped[] = $newPath. " (already exists)";
continue;
}
@mkdir(dirname($newPath), 0755, true);
if (!rename($oldPath, $newPath))
{
$error[] = $oldPath . " - " . $newPath;
}
else
{
$success[] = $oldPath . " - ". $newPath;
}
}
//UPDATE `e107jm_github_sync` SET `lastsynced` = '11' WHERE `e107jm_github_sync`.`id` = 2;
$query = "UPDATE #github_sync SET lastsynced = '" . time() . "' WHERE id=" . $this->sync_id;
e107::getDb()->gen($query);
return ['success' => $success, 'error' => $error, 'skipped' => $skipped];
}
/**
* @param $file
* @param $array
*
* @return bool
*/
private function matchFound($file, $array)
{
if (empty($array))
{
return false;
}
foreach ($array as $term)
{
if (strpos($file, $term) !== false)
{
return true;
}
}
return false;
}
}
class github_sync_form_ui extends e_admin_form_ui
{
// Override the default Options field.
function options($parms, $value, $id, $attributes)
{
if ($attributes['mode'] !== 'read')
{
return;
}
if ($attributes['mode'] == 'read')
{
$id = $this->getController()->getListModel()->get('id');
$query['action'] = 'edit';
$query['id'] = $id;
$query = http_build_query($query, '', '&');
$text = "<a href='" . e_SELF . "?{$query}' class='btn btn-default' title='" . LAN_EDIT . "' data-toggle='tooltip' data-bs-toggle='tooltip' data-placement='left'>
<i class='S16 e-edit-16' ></i></a>";
if (getperms('0')) // only main admins
{
$text .= $this->submit_image('etrigger_delete[' . $id . ']', $id, 'delete', LAN_DELETE . ' [ ID: ' . $id . ' ]', array('class' => 'action delete btn btn-default'));
}
if (true) //change to active
{
$query2['action'] = 'sync';
$query2['id'] = $id;
$query2 = http_build_query($query2, '', '&');
$link = e_SELF . "?{$query2}";
$text .= "<a href='" . $link . "' class='btn btn-info' title='Run Sync'>" . ADMIN_GITSYNC_ICON . "</a>"; //
}
return $text;
}
}
}
new githubSync_adminArea();
require_once(e_ADMIN . "auth.php");
e107::getAdminUI()->runPage();
require_once(e_ADMIN . "footer.php");
exit;