-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.php
812 lines (672 loc) · 23.2 KB
/
hub.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<?php if (!defined('PmWiki')) { extServe(); exit();}
/**
ExtensionHub: Configuration panel for PmWiki recipes
Written by (c) Petko Yotov 2023-2024
This text is written for PmWiki; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
See pmwiki.php for full details and lack of warranty.
*/
$RecipeInfo['ExtensionHub']['Version'] = '2024-08-13a';
SDVA($FmtPV, [
'$ExtHubVersion' => '$GLOBALS["RecipeInfo"]["ExtensionHub"]["Version"]',
'$ExtPubDirUrl' => 'extFarmPubDirUrl()',
]);
SDVA($HandleActions, ['hub'=>'HandleHub']);
SDVA($HandleAuth, ['hub' => 'admin']);
SDVA($ActionTitleFmt,['hub' => '| $[Extensions]']);
SDVA($PostConfig, [
'extGetIncluded' => 0.5,
'extOutputResources' => 300.5,
]);
SDVA($xHub, [
'ExtDir' => dirname(__FILE__),
'DataDir' => "$WorkDir/.extensions",
'DataPageName' => "Extensions.Config",
'Resources'=>[ [], [] ], # header, footer
'ResourcesSeen'=>[],
'ResourcesSent'=>0,
'=included'=>[],
'StatusIcons'=>[
'new' => '🟡', # yellow circle
'active' => '🟢', # green circle
'inactive' => '🔴', # red circle
],
'EncodeFn' => 'base64_encode',
'DecodeFn' => 'base64_decode',
]);
$Conditions['extension_enabled'] = 'CondExtEnabled($condparm)';
if(!isset($xHub['Pages']['{$SiteAdminGroup}.ExtensionHub']))
$xHub['Pages']['{$SiteAdminGroup}.ExtensionHub'] = <<<'TEMPLATE'
(:messages:)
(:Summary: Cookbook:ExtensionHub Template used when listing extensions and editing configurations:)
(:if20240205 enabled EnableExtSaved:)
>>frame note<<
$[Configuration saved.] $[Return to] [[{*$FullName}|+]]
>><<
(:if20240205 enabled EnableExtDeleted:)
>>frame note<<
$[Configuration deleted.]
>><<
(:if20240205end:)
(:if20240205 enabled EnableExtConfig:)
!! {*$ExtName} configuration
(:if202402051 exists Site.{*$ExtName}Form:)
(:include Site.{*$ExtName}Form##form:)
(:else202402051:)
>>frame<<
Cookbook:{*$ExtName}
>><<
(:if202402051end:)
(:input form "{*$PageUrl}?action=hub&x={*$ExtName}&i={*$ExtIndex}" method=post:)
(:input default request=1:)
(:input hidden n {*$FullName}:)
(:input hidden action hub:)
(:input pmtoken:)
(:input hidden x:)
(:input hidden i:)
(:input checkbox xEnabled 1 "$[Enable configuration]":)
(:if202402052 enabled EnableExtPgCust:)
$[Applies to pages:]\\
(:input textarea xNamePatterns placeholder=* required=required cols=60 rows={*$NamePatternsRows}:) \\
''$[Glob patterns like @@Group1.*,Group2.*,-*.HomePage@@]''
(:else202402052:)
(:input hidden xName *:)
(:if202402052end:)
(:if202402051 exists Site.{*$ExtName}Form:)
(:include Site.{*$ExtName}Form#form#formend:)
''Leave fields empty to reset to default values.''
(:if202402051end:)
(:input submit xPost "$[Save]":) [[{*$FullName}| $[Cancel] ]]
(:input submit xReset "$[Delete configuration]" data-pmconfirm="$[Confirm deletion?]":)
(:input end:)
(:include Site.{*$ExtName}Form#formend:)
(:else20240205:)
>>recipeinfo frame<<
$[Summary]: Configuration panel for PmWiki extensions \\
%hlt php%@@$ExtPubDirUrl@@: {$ExtPubDirUrl}\\
$[Version]: {$ExtHubVersion}\\
$[Cookbook]: [[(Cookbook:)ExtensionHub]]\\
$[Maintainer]: [[https://www.pmwiki.org/petko|Petko]]
>><<
$[Here you can enable and configure your PmWiki extensions.]
>>padding=.5em<<
(:extlist:)
>><<
$[See Cookbook:Extensions to find new extensions.]
(:if20240205end:)
TEMPLATE;
SDVA($xHub['injectFmt'], [
'css' => "<link rel='stylesheet' href='%s' %s />\n",
'js' => "<script src='%s' %s></script>\n",
]);
SDVA($MarkupDirectiveFunctions, ['extlist'=>'FmtExtList']);
# This allows us to ship template pages as variables rather than files.
class HubPageStore extends PageStore {
private $pagetexts = [];
private $pagetime = 0;
function __construct() {
global $xHub, $RecipeInfo;
foreach($xHub['Pages'] as $pnfmt=>$text) {
$pn = FmtPageName($pnfmt, 'SiteAdmin.SiteAdmin');
$this->pagetexts[$pn] = $text;
}
$date = substr($RecipeInfo['ExtensionHub']['Version'], 0, 10);
$this->pagetime = strtotime($date) + 12*3600;
}
function read($pagename, $since=0) {
if(!isset($this->pagetexts[$pagename])) return [];
list($g, $n) = explode('.', $pagename);
$page = [
'text' => $this->pagetexts[$pagename],
'time' => $this->pagetime,
'title' => $n,
'passwdattr' => '@lock',
'passwdedit' => '@lock',
'passwdread' => '@lock',
];
return $page;
}
function write($pagename,$page) {
Abort('?Not supported.');
}
function exists($pagename) {
return isset($this->pagetexts[$pagename]);
}
function delete($pagename) {
Abort('?Not supported.');
}
function ls($pats=NULL) {
$list = array_keys($this->pagetexts);
if($pats) $list = MatchPageNames($list, $pats);
return $list;
}
}
# Read plain text files (PmWiki source text) as pages.
# These are easier to review from version control
class HubPlainPageStore extends PageStore {
function read($pagename, $since=0) {
$pagefile = $this->pagefile($pagename);
if(!$pagefile || !file_exists($pagefile)) return false;
$page = $this->attr;
$page['time'] = filemtime($pagefile);
$page['text'] = file_get_contents($pagefile);
return $page;
}
function write($pagename,$page) {
Abort('?Not supported.');
}
function exists($pagename) {
$pagefile = $this->pagefile($pagename);
return file_exists($pagefile);
}
function delete($pagename) {
Abort('?Not supported.');
}
}
$extInc = extInit();
foreach($extInc as $path=>$priority) {
include_once($path);
}
unset($extInc, $path, $priority);
function CondExtEnabled($condparm) {
global $xHub, $action;
if(!$condparm) {
if($action == 'hub') {
$condparm = $_REQUEST['x'];
}
else return false;
}
$enabled = array_keys($xHub['=included']);
return (boolean)MatchNames($enabled, $condparm);
}
function extInit() {
global $xHub, $action, $WikiLibDirs, $PostConfig;
static $done = 0; if($done++) return [];
$WikiLibDirs[] = new HubPageStore();
$xHub["ConfigStore"] = new PageStore("{$xHub['DataDir']}/{\$FullName}");
extScanDir();
if($action == 'recipecheck') {
$PostConfig['extRecipeCheck'] = 0.4;
}
return extGetIncluded('');
}
function extFarmPubDirUrl() {
global $ExtPubDirUrl, $FarmPubDirUrl, $PubDirUrl, $xHub;
if(isset($ExtPubDirUrl)) return $ExtPubDirUrl;
$extbasedir = basename($xHub['ExtDir']);
$pub = $FarmPubDirUrl ?? $PubDirUrl;
return preg_replace('#/[^/]*$#', "/$extbasedir", $pub, 1);
}
function extCaller() {
global $xHub;
$extbasedir = preg_quote(strtr($xHub['ExtDir'], '\\', '/'));
$trace = debug_backtrace();
for($i=1; $i<count($trace); $i++) {
$path = strtr($trace[$i]['file'], '\\', '/');
$xname = basename($path, '.php');
$qname = preg_quote($xname, '!');
$rx = "!^(phar://)? # ? compressed
$extbasedir / # /extensions path
($qname(-\\w[-\\w.]*)?\\.zip / )? # ? compressed name
$qname(-\\w[-\\w.]*)? / # extension own directory
$qname\\.php$ # script
!x";
if(!preg_match($rx, $path, $m)) continue;
return $xname;
}
return false;
}
function extAddWikiPlainDir($path = 'wikiplain.d') {
global $WikiLibDirs;
$xname = extCaller();
if(!$xname) return; # abort?
$active = extHubGetConfig('active');
$c = @$active[$xname];
if(!$c) return;
$dir = @$c['=dir'];
if(!$dir) return;
$path = "$dir/$path/{\$FullName}";
$WikiLibDirs[$path] = new HubPlainPageStore($path);
}
function extAddWikiLibDir($path = 'wikilib.d') {
global $WikiLibDirs;
$xname = extCaller();
if(!$xname) return; # abort?
$active = extHubGetConfig('active');
$c = @$active[$xname];
if(!$c) return;
$dir = @$c['=dir'];
if(!$dir) return;
$path = "$dir/$path/\$FullName";
$WikiLibDirs[$path] = new PageStore($path);
}
function extAddHeaderResource($files, $attrs=[]) {
return extAddResource($files, $attrs, 0);
}
function extAddFooterResource($files, $attrs=[]) {
return extAddResource($files, $attrs, 1);
}
function extAddResource($files, $attrs=[], $footer=0) {
global $xHub;
$i = $footer? 1:0;
$xname = extCaller();
if(!$xname) $xname = 'unknown';
if(!isset($xHub['Resources'][$i][$xname])) $xHub['Resources'][$i][$xname] = [];
$xHub['Resources'][$i][$xname][] = [$files, $attrs];
if($xHub['ResourcesSent']) { # injected by page markup rather than globally
extOutputResources();
}
}
function extOutputResources() {
global $xHub, $HTMLHeaderFmt, $HTMLFooterFmt;
$fmts = [ &$HTMLHeaderFmt, &$HTMLFooterFmt ];
for($f=0; $f<2; $f++) {
$fmt = &$fmts[$f];
$css = $js = $raw = [];
foreach($xHub['Resources'][$f] as $xname=>$a) {
# a $HTMLHeaderFmt[$xname] defined in config.php
# overrides the one suggested by the recipe.
if(isset($fmt[$xname])) continue;
$out = '';
foreach($a as $x) {
$out .= extFormatResource($xname, $x[0], $x[1]);
}
if(!isset($fmt["ext~$xname"])) $fmt["ext~$xname"] = '';
$fmt["ext~$xname"] .= $out;
unset($xHub['Resources'][$f][$xname]);
}
}
$xHub['ResourcesSent']++;
}
function extFormatResource($xname, $a, $htmlattr) {
global $xHub;
$active = extHubGetConfig('active');
$out = '';
if(is_array($a)) foreach($a as $v)
$out .= extFormatResource($xname, $v, $htmlattr);
if(is_string($a)) {
if(strpos($a, '<')!==false) return $out . $a;
if(preg_match('/\\s/', $a)) {
$x = preg_split('!\\s+!', $a, -1, PREG_SPLIT_NO_EMPTY);
return $out . extFormatResource($xname, $x, $htmlattr);
}
$attr = '';
if(@$htmlattr[$a]) foreach($htmlattr[$a] as $k=>$v) {
if(is_string($v)) $v = PHSC($v);
elseif(is_array($v)||is_object($v)) $v = pm_json_encode($v, true);
if(is_int($k)) $attr .= " $v";
else $attr .= " $k=\"$v\"";
}
if(strpos($a, ':') === false && $a[0] !== '/') {
$conf = $active[$xname];
$url = $conf['=url'];
$a = "$url/$a";
if(isset($xHub['ResourcesSeen'][$a])) return $out;
}
if(preg_match('/\\.(css|js)(?:[?#].+)?$/i', $a, $m)) {
$fmt = $xHub['injectFmt'][strtolower($m[1])];
$out .= sprintf($fmt, $a, $attr);
$xHub['ResourcesSeen'][$a] = 1;
}
}
return $out;
}
function extScanDir() {
global $xHub;
$xHub['ExtPaths'] = $list = [];
$dir = $xHub['ExtDir'];
if(!is_dir($dir)) return;
$dirlist = scandir($dir);
# Extension downloaded from repository may have its tag as suffix
$suffix = '/-(master|main|latest|v?\\d\\d\\d\\d-?\\d\\d-?\\d\\d[a-z]?|v?\\d+(\\.\\d+)*)$/';
foreach($dirlist as $fname) {
if($fname[0] == '.') continue;
if(strpos($fname, ',') !== false) continue;
# Uncompressed extensions override compressed ones
if(substr($fname, -4) === '.zip') {
$base = substr($fname, 0, -4);
$xname = preg_replace($suffix, '', $base);
SDVA($list, [$xname=>"phar://$dir/$fname/$base/$xname.php"]);
}
elseif(is_dir("$dir/$fname")) {
$xname = preg_replace($suffix, '', $fname);
$list[$xname] = "$dir/$fname/$xname.php";
}
}
$xHub['ExtPaths'] = $list;
}
# May be called from an extension, with default values to be merged
function extGetConfig($default = []) {
$args = ['mode'=> 'extension'];
$conf = extHubGetConfig($args);
$x = explode(' ', '=conf =path xAction xEnabled xNamePatterns xPriority');
foreach($x as $k) unset($conf[$k]);
return array_merge($default, $conf);
}
function extHubGetConfig($args = ['mode'=> 'extension']) {
# Can be called:
# - initially to populate the full static array
# - to populate extGetIncluded
# - to get extension configuration - full or merged per page
# - to get configuration for ?action=hub (list, form, or save)
global $xHub;
static $Extensions = [], $Active = [], $page = [];
if(is_string($args)) $args = ['mode'=> $args];
if(!$Extensions) {
$page = $xHub["ConfigStore"]->read($xHub["DataPageName"], READPAGE_CURRENT);
if(!$page) $page = [];
$keys = preg_grep('/^x\\./', array_keys($page));
$list = $xHub['ExtPaths'];
$x = [];
foreach($keys as $key) {
$value = $page[$key];
$parts = explode('.', substr($key, 2), 4);
@list($xname, $index, $xkey) = $parts;
$xpath = @$list[$xname];
if(!$xpath) continue;
if(count($parts)==1) {
list($priority, $actions) = explode(' ', $value, 2);
$serve = (strncmp($xpath, 'phar://', 7)===0) ? '/' . basename(__FILE__): '';
$extdir = basename(dirname($xpath)); // may have -tag suffix
$x[$xname] = [
'xPriority'=>intval($priority),
'xAction'=>$actions,
'=path' => $xpath,
'=dir' => dirname($xpath),
'=url' => "{\$ExtPubDirUrl}$serve/$extdir",
'=conf' => [],
];
continue;
}
if(count($parts)==2) {
list($enabled, $patterns) = explode(' ', $value, 2);
$x[$xname]['=conf'][$index] = [
'xEnabled' => intval($enabled),
'xNamePatterns'=> $patterns,
];
continue;
}
# else count($parts)==3
if(preg_match('/^(enc_|passwd)/', $xkey)) {
$cfn = $xHub['DecodeFn'];
$value = $cfn($value);
}
if(substr($xkey, -1) == '~') {
$xkey = substr($xkey, 0, -1);
$value = unserialize($value);
}
$x[$xname]['=conf'][$index][$xkey] = $value;
}
uasort($x, 'extSort');
$Extensions = $x;
}
$mode = $args['mode'];
if($mode == 'all') return $Extensions;
if($mode == 'page') return $page;
if($mode == 'extension') {
$xname = extCaller();
if(isset($Active[$xname])) return $Active[$xname];
return [ '=conf'=>[], ];
}
if($mode == 'full') {
$xname = $args['xname'];
if(isset($Extensions[$xname])) return $Extensions[$xname];
return [ '=conf'=>[], ];
}
if($mode == 'put') {
$Active = array_merge($Active, $args['new']);
}
if($mode == 'active') {
return $Active;
}
}
function extSort($a, $b) {
return $a['xPriority'] <=> $b['xPriority'];
}
function extGetIncluded($pagename = '') { # ''=initial
global $PostConfig, $xHub, $action;
$x = extHubGetConfig('all');
$merged = $list = [];
foreach($x as $xname=>$conf) {
if(@$conf['xAction'] && !MatchNames($action, $conf['xAction'], false)) continue;
if($pagename==='' && $conf['xPriority']<=100) { # initial
$my = @$conf['=conf'][0];
if(!isset($my) || !$my['xEnabled']!=1)
$list[ $conf['=path'] ] = $conf['xPriority'];
$merged[$xname] = @array_merge($x[$xname], (array)$conf['=conf'][0]);
$xHub['=included'][$xname] = 1;
}
elseif($pagename !== '' && $conf['xPriority']>100) {
foreach($conf['=conf'] as $a) {
if (!$a['xEnabled']) continue;
$pat = preg_replace('/[,\\s]+/', ',', $a['xNamePatterns']);
if($pat === '*' || $pat === '*.*' || MatchNames($pagename, $pat)) {
$priority = $conf['xPriority']<=200
? 1+$conf['xPriority']/100
: 51+$conf['xPriority']/200;
$PostConfig[ $conf['=path'] ] = $priority;
$merged[$xname] = array_merge($x[$xname], $a);
$xHub['=included'][$xname] = 1;
}
}
}
}
extHubGetConfig(['mode'=>'put', 'new'=>$merged]);
asort($list);
asort($PostConfig); # required
return $list;
}
function extSaveConfig($pagename, $xname, $index) {
global $xHub, $EnableExtSaved, $MessagesFmt;
if(!@$_POST['xPost'] && !@$_POST['xReset'] || !$xname) return;
if(! pmtoken(1)) {
$MessagesFmt[] = XL('Token invalid or missing');
return;
}
$kpat = '/^(n|action|pmtoken|i|x|x[A-Z]\\w*|.*[^a-zA-Z0-9_].*)$/';
$postedkeys = preg_grep($kpat, array_keys($_POST), PREG_GREP_INVERT);
$priority = intval(@$_POST['xPriority']);
if(!$priority) $priority = 150;
$xaction = $_POST['xAction'] ?? '*';
$enabled = intval(@$_POST['xEnabled']);
$_POST['xEnabled'] = $enabled;
$patterns = strval($_POST['xNamePatterns']);
if($patterns === '') $patterns = '*';
$page = $old = extHubGetConfig('page');
$dpat = "/^x\\.$xname\\.$index($|\\.)/";
$deletedkeys = preg_grep($dpat, array_keys($page));
foreach($deletedkeys as $key) {
unset($page[$key]);
}
if(! @$_POST['xReset']) {
$prefix = "x.$xname";
$page[$prefix] = "$priority $xaction";
$page["$prefix.$index"] = "$enabled $patterns";
foreach($postedkeys as $k) {
$v = $_POST[$k];
if(is_numeric($v)) $v = floatval($v);
elseif(is_array($v)) {
$k .= "~";
$v = serialize($v);
}
$xkey = "$prefix.$index.$k";
if($v === '') unset($page[$xkey]);
else {
if(preg_match('/^(enc_|passwd)/', $k)) {
$cfn = $xHub['EncodeFn'];
$v = $cfn($v);
}
$page[$xkey] = $v;
}
}
}
if($page != $old) {
$xHub['ConfigStore']->write($xHub['DataPageName'], $page);
Redirect($pagename, "{\$PageUrl}?action=hub&x=$xname&i=$index&saved=1");
}
if(@$_POST['xReset']) {
Redirect($pagename, '{*$PageUrl}?deleted=1');
exit;
}
}
function HandleHub($pagename, $auth='admin') {
global $xHub, $FmtPV, $WikiLibDirs, $PageStartFmt, $PageEndFmt,
$EnableExtDeleted, $EnableExtSaved, $EnableExtConfig, $EnableExtPgCust,
$InputValues, $HTMLStylesFmt, $CurrentExtension;
$page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT);
if(!$page) return Abort('?No permissions');
$HTMLStylesFmt['hub-form'] = '.wikiexthub input:checked + label { font-weight: bold;}';
$paths = $xHub['ExtPaths'];
$index = intval(@$_REQUEST['i']);
$xname = @$_REQUEST['x'];
if(@$_REQUEST['deleted']) $EnableExtDeleted = 1;
if($xname && isset($paths[$xname])) {
extSaveConfig($pagename, $xname, $index);
if(@$_REQUEST['saved']) $EnableExtSaved = 1;
$EnableExtConfig = 1;
$CurrentExtension = $xname;
$FmtPV['$ExtName'] = '$GLOBALS["CurrentExtension"]';
$FmtPV['$ExtIndex'] = $index;
$FmtPV['$ExtVersion'] = 'extGetVersion($GLOBALS["CurrentExtension"])';
$extconf = extHubGetConfig(['mode' => 'full', 'xname'=>$xname]);
$currentconf = $extconf['=conf'][$index] ?? [];
if(!$index && !$currentconf) $currentconf['xNamePatterns'] = '*';
$nplines = explode("\n", trim(strval(@$currentconf['xNamePatterns'])));
$FmtPV['$NamePatternsRows'] = min(4, count($nplines)+1);
foreach($currentconf as $k => $v) {
if(isset($_POST[$k])) continue;
if (is_array($v)) {
foreach($v as $vk=>$vv) {
if (is_numeric($vk)) $InputValues["{$k}[]"][] = PHSC($vv, ENT_NOQUOTES);
else $InputValues["{$k}[{$vk}]"] = PHSC($vv, ENT_NOQUOTES);
}
}
else {
$InputValues[$k] = PHSC($v, ENT_NOQUOTES);
}
}
$priority = $extconf['xPriority'] ?? 150;
$EnableExtPgCust = $priority <= 100? 0:1;
$wlpath = preg_replace('!/[^/]+$!', '', $paths[$xname]);
if(file_exists("$wlpath/wikiplain.d")) {
$WikiLibDirs[] = new HubPlainPageStore("$wlpath/wikiplain.d/{\$FullName}");
}
elseif(file_exists("$wlpath/wikilib.d")) {
$WikiLibDirs[] = new PageStore("$wlpath/wikilib.d/{\$FullName}");
}
}
HandleBrowse($pagename);
}
# Parse extension scripts to get their RecipeInfo versions
function extGetVersion($xname) {
global $xHub;
static $versions = [];
if(isset($versions[$xname])) return $versions[$xname];
$path = $xHub['ExtPaths'][$xname];
if(!file_exists($path)) return '0000-00-00'; # not extension
$f = file_get_contents($path);
if(preg_match('!\\$RecipeInfo\\[.*?\\]\\[.*?\\] *= *([\'"])(.+?)\\1!', $f, $m)) {
$version = $m[2];
}
else {
$mtime = filemtime($path);
$version = PSFT('%F', $mtime) . '^';
}
$versions[$xname] = $version;
return $version;
}
# Populate extension versions for ?action=recipecheck
function extRecipeCheck(){
FmtExtList('', '', ['onlyRecipeInfo'=>1]);
}
function FmtExtList($pagename, $d, $args) {
global $HandleAuth, $xHub, $RecipeInfo, $HTMLStylesFmt;
$page = RetrieveAuthPage($pagename, $HandleAuth['hub'], false, READPAGE_CURRENT);
if(!$page) return '$[No permissions]';
$paths = $xHub['ExtPaths'];
ksort($paths);
if(!count($paths)) {
$out = "$[No extensions currently available.]";
return $out;
}
$HTMLStylesFmt['hublist-form'] = 'form.hublistform select { width: 12em; text-overflow: ellipsis; }';
$out = "|| class='simpletable sortable filterable' \n";
$out .= "||! $[Extension] ||! $[Version] ||! $[Priority] "
. "||! $[Actions] ||! $[Configurations] ||\n";
$keepz = Keep('');
foreach($paths as $xname=>$path) {
$version = extGetVersion($xname);
if(@$args['onlyRecipeInfo']) {
SDV($RecipeInfo[$xname]['Version'], $version);
continue;
}
$conf = extHubGetConfig(['mode' => 'full', 'xname'=>$xname]);
$compressed = (strncmp('phar://', $path, 7)===0)? '*' : '';
$select = "(:input form '{*\$PageUrl}' get class=hublistform:)";
$select .= "(:input hidden n $pagename:)(:input hidden action hub:)";
$select .= "(:input hidden x $xname:)";
$j=0;
foreach($conf['=conf'] as $i=>$a) {
$icon = $a['xEnabled']
? $xHub['StatusIcons']['active']
: $xHub['StatusIcons']['inactive'];
$np = preg_replace('/[,\\s]+/', ',', $a['xNamePatterns']);
$select .= "(:input select i $i \"$icon $np\":)";
$j = $i+1;
}
if(!isset($conf['xPriority']) || $conf['xPriority']>100 || !$j)
$select .= "(:input select i $j \"{$xHub['StatusIcons']['new']} $[New configuration]\":)";
$select .= "$keepz (:input submit '' \"$[Edit]\":)";
$select .= "(:input end:)";
$out .= @"||'''$xname'''$compressed || $version || {$conf['xPriority']} "
. @"|| {$conf['xAction']} || $select ||\n";
}
return $out;
}
function extServe() {
$ServeExts = array(
'css' => 'text/css', 'js'=>'application/javascript', 'json'=>'application/json',
'ttf' => 'font/ttf', 'woff' => 'font/woff', 'woff2' => 'font/woff2',
'gif' => 'image/gif', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg',
'png' => 'image/png', 'apng' => 'image/apng', 'ico' => 'image/x-icon',
'webp' => 'image/webp', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml',
);
$ExtRx = "/\\.(".implode('|', array_keys($ServeExts)).")$/i";
$pi = strval(@$_SERVER['PATH_INFO']);
$pi = preg_replace('!\\.+/+!', '/', $pi);
$pi = preg_replace('!^/+|[^-\\w./]+!', '', $pi);
if(! $pi) {
extReject("Bad request");
}
if(!preg_match($ExtRx, $pi, $m)) {
extReject("Unsupported file type");
}
$contenttype = $ServeExts[strtolower($m[1])];
list($basename, $path) = explode('/', $pi, 2);
$fname = "$basename.zip";
if(!file_exists($fname)) {
extReject("File not found", 404);
}
# Let the browser cache the compressed resource for 1 hour.
# (Most actual visits are 2-3 minutes.)
header("Cache-Control: max-age=3600, must-revalidate");
$filelastmod = gmdate('D, d M Y H:i:s \G\M\T', filemtime($fname));
if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] == $filelastmod) extReject('', 304);
header("Last-Modified: $filelastmod");
$xpath = "phar://$fname/$basename/$path";
if(!file_exists($xpath)) {
extReject("File not found", 404);
}
header("Content-type: $contenttype");
$filesize = filesize($xpath);
header("Content-Length: $filesize");
@readfile($xpath);
exit;
}
function extReject($msg, $code = 400) {
http_response_code($code);
if($msg) echo $msg;
exit;
}