-
Notifications
You must be signed in to change notification settings - Fork 211
/
genericPage.class.php
1152 lines (932 loc) · 44.5 KB
/
genericPage.class.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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if (!defined('AOWOW_REVISION'))
die('illegal access');
trait TrDetailPage
{
protected $category = null; // not used on detail pages
protected $lvTabs = []; // most pages have this
protected $ssError = null;
protected $coError = null;
protected $viError = null;
protected $subject = null; // so it will not get cached
// template vars
protected $extraText = '';
protected $infobox = [];
protected $transfer = []; // faction transfer equivalent data
protected $redButtons = []; // see template/redButtons.tpl.php
protected $smartAI = null;
protected $map = null;
protected $article = null;
protected $headIcons = [];
protected $expansion = EXP_CLASSIC;
protected $contribute = CONTRIBUTE_ANY;
protected function generateCacheKey(bool $withStaff = true) : string
{
$staff = intVal($withStaff && User::isInGroup(U_GROUP_EMPLOYEE));
// mode, type, typeId, employee-flag, localeId, category, filter
$key = [$this->mode, $this->type, $this->typeId, $staff, User::$localeId, '-1', '-1'];
// item special: can modify tooltips
if (isset($this->enhancedTT))
$key[] = md5(serialize($this->enhancedTT));
return implode('_', $key);
}
protected function applyCCErrors() : void
{
if (!empty($_SESSION['error']['co']))
$this->coError = $_SESSION['error']['co'];
if (!empty($_SESSION['error']['ss']))
$this->ssError = $_SESSION['error']['ss'];
if (!empty($_SESSION['error']['vi']))
$this->viError = $_SESSION['error']['vi'];
unset($_SESSION['error']);
}
}
trait TrListPage
{
protected $category = null;
protected $subCat = '';
protected $filter = [];
protected $lvTabs = []; // most pages have this
protected $redButtons = []; // see template/redButtons.tpl.php
private $filterObj = null;
protected function generateCacheKey(bool $withStaff = true) : string
{
$staff = intVal($withStaff && User::isInGroup(U_GROUP_EMPLOYEE));
// mode, type, typeId, employee-flag, localeId,
$key = [$this->mode, $this->type, '-1', $staff, User::$localeId];
//category
$key[] = $this->category ? implode('.', $this->category) : '-1';
// filter
$key[] = $this->filterObj ? md5(serialize($this->filterObj)) : '-1';
return implode('_', $key);
}
}
trait TrProfiler
{
protected $region = '';
protected $realm = '';
protected $realmId = 0;
protected $battlegroup = ''; // not implemented, since no pserver supports it
protected $subjectName = '';
protected $subjectGUID = 0;
protected $sumSubjects = 0;
protected $doResync = null;
protected function generateCacheKey(bool $withStaff = true) : string
{
$staff = intVal($withStaff && User::isInGroup(U_GROUP_EMPLOYEE));
// mode, type, typeId, employee-flag, localeId, category, filter
$key = [$this->mode, $this->type, $this->subject->getField('id'), $staff, User::$localeId, '-1', '-1'];
return implode('_', $key);
}
protected function getSubjectFromUrl(string $pageParam) : void
{
if (!$pageParam)
return;
// cat[0] is always region
// cat[1] is realm or bGroup (must be realm if cat[2] is set)
// cat[2] is arena-team, guild or player
$cat = explode('.', $pageParam, 3);
$cat = array_map('urldecode', $cat);
if (array_search($cat[0], Util::$regions) === false)
return;
$this->region = $cat[0];
// if ($cat[1] == Profiler::urlize(Cfg::get('BATTLEGROUP')))
// $this->battlegroup = Cfg::get('BATTLEGROUP');
if (isset($cat[1]))
{
foreach (Profiler::getRealms() as $rId => $r)
{
if (Profiler::urlize($r['name'], true) == $cat[1])
{
$this->realm = $r['name'];
$this->realmId = $rId;
if (isset($cat[2]) && mb_strlen($cat[2]) >= 2)
$this->subjectName = $cat[2]; // cannot reconstruct original name from urlized form; match against special name field
break;
}
}
}
}
protected function initialSync() : void
{
$this->prepareContent();
$this->contribute = CONTRIBUTE_NONE;
$this->notFound = array(
'title' => sprintf(Lang::profiler('firstUseTitle'), $this->subjectName, $this->realm),
'msg' => ''
);
if (isset($this->tabId))
$this->pageTemplate['activeTab'] = $this->tabId;
$this->sumSQLStats();
$this->display('text-page-generic');
exit();
}
protected function generatePath() : void
{
if ($this->region)
{
$this->path[] = $this->region;
if ($this->realm)
$this->path[] = Profiler::urlize($this->realm, true);
// else
// $this->path[] = Profiler::urlize(Cfg::get('BATTLEGROUP'));
}
}
}
class GenericPage
{
use TrRequestData;
protected $tpl = '';
protected $reqUGroup = U_GROUP_NONE;
protected $reqAuth = false;
protected $mode = CACHE_TYPE_NONE;
// protected $contribute; // defined in __construct()
protected $wowheadLink = 'https://wowhead.com/';
protected $jsGlobals = [];
protected $lvData = [];
protected $title = []; // for title-Element
protected $name = ''; // for h1-Element
protected $tabId = null;
protected $gDataKey = false; // adds the dataKey to the user vars
protected $scripts = array(
[SC_JS_FILE, 'js/jquery-3.7.0.min.js', SC_FLAG_NO_TIMESTAMP ],
[SC_JS_FILE, 'js/basic.js' ],
[SC_JS_FILE, 'widgets/power.js', SC_FLAG_NO_TIMESTAMP | SC_FLAG_APPEND_LOCALE],
[SC_JS_FILE, 'js/locale_%s.js', SC_FLAG_LOCALIZED ],
[SC_JS_FILE, 'js/global.js' ],
[SC_JS_FILE, 'js/locale.js' ],
[SC_JS_FILE, 'js/Markup.js' ],
[SC_CSS_FILE, 'css/basic.css' ],
[SC_CSS_FILE, 'css/global.css' ],
[SC_CSS_FILE, 'css/aowow.css' ],
[SC_CSS_FILE, 'css/locale_%s.css', SC_FLAG_LOCALIZED ]
);
// private vars don't get cached
private $time = 0;
private $cacheDir = 'cache/template/';
private $jsgBuffer = [];
private $gPageInfo = [];
private $gUser = [];
private $pageTemplate = [];
private $community = ['co' => [], 'sc' => [], 'vi' => []];
private $cacheLoaded = [];
private $skipCache = 0x0;
private $memcached = null;
private $mysql = ['time' => 0, 'count' => 0];
private $js = [];
private $css = [];
private $headerLogo = '';
private $fullParams = '';
private $lvTemplates = array(
'achievement' => ['template' => 'achievement', 'id' => 'achievements', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_achievements' ],
'areatrigger' => ['template' => 'areatrigger', 'id' => 'areatrigger', 'parent' => 'lv-generic', 'data' => [], ],
'calendar' => ['template' => 'holidaycal', 'id' => 'calendar', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_calendar' ],
'class' => ['template' => 'classs', 'id' => 'classes', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_classes' ],
'commentpreview' => ['template' => 'commentpreview', 'id' => 'comments', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_comments' ],
'npc' => ['template' => 'npc', 'id' => 'npcs', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_npcs' ],
'currency' => ['template' => 'currency', 'id' => 'currencies', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_currencies' ],
'emote' => ['template' => 'emote', 'id' => 'emotes', 'parent' => 'lv-generic', 'data' => [] ],
'enchantment' => ['template' => 'enchantment', 'id' => 'enchantments', 'parent' => 'lv-generic', 'data' => [] ],
'event' => ['template' => 'holiday', 'id' => 'holidays', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_holidays' ],
'faction' => ['template' => 'faction', 'id' => 'factions', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_factions' ],
'genericmodel' => ['template' => 'genericmodel', 'id' => 'same-model-as', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_samemodelas' ],
'icongallery' => ['template' => 'icongallery', 'id' => 'icons', 'parent' => 'lv-generic', 'data' => [] ],
'item' => ['template' => 'item', 'id' => 'items', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_items' ],
'itemset' => ['template' => 'itemset', 'id' => 'itemsets', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_itemsets' ],
'mail' => ['template' => 'mail', 'id' => 'mails', 'parent' => 'lv-generic', 'data' => [] ],
'model' => ['template' => 'model', 'id' => 'gallery', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_gallery' ],
'object' => ['template' => 'object', 'id' => 'objects', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_objects' ],
'pet' => ['template' => 'pet', 'id' => 'hunter-pets', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_pets' ],
'profile' => ['template' => 'profile', 'id' => 'profiles', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_profiles' ],
'quest' => ['template' => 'quest', 'id' => 'quests', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_quests' ],
'race' => ['template' => 'race', 'id' => 'races', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_races' ],
'replypreview' => ['template' => 'replypreview', 'id' => 'comment-replies', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_commentreplies'],
'reputationhistory' => ['template' => 'reputationhistory', 'id' => 'reputation', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_reputation' ],
'screenshot' => ['template' => 'screenshot', 'id' => 'screenshots', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_screenshots' ],
'skill' => ['template' => 'skill', 'id' => 'skills', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_skills' ],
'sound' => ['template' => 'sound', 'id' => 'sounds', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.types[19][2]' ],
'spell' => ['template' => 'spell', 'id' => 'spells', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_spells' ],
'title' => ['template' => 'title', 'id' => 'titles', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_titles' ],
'topusers' => ['template' => 'topusers', 'id' => 'topusers', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.topusers' ],
'video' => ['template' => 'video', 'id' => 'videos', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_videos' ],
'zone' => ['template' => 'zone', 'id' => 'zones', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_zones' ],
'guide' => ['template' => 'guide', 'id' => 'guides', 'parent' => 'lv-generic', 'data' => [], ]
);
public function __construct(string $pageCall = '', string $pageParam = '')
{
$this->time = microtime(true);
$this->initRequestData();
$this->title[] = Cfg::get('NAME');
if (!isset($this->contribute))
$this->contribute = CONTRIBUTE_NONE;
$this->fullParams = $pageCall;
if ($pageParam)
$this->fullParams .= '='.$pageParam;
$cacheDir = Cfg::get('CACHE_DIR');
if ($cacheDir && Util::writeDir($cacheDir))
$this->cacheDir = mb_substr($cacheDir, -1) != '/' ? $cacheDir.'/' : $cacheDir;
// force page refresh
if (isset($_GET['refresh']) && User::isInGroup(U_GROUP_ADMIN | U_GROUP_BUREAU | U_GROUP_DEV))
{
if ($_GET['refresh'] == 'filecache')
$this->skipCache = CACHE_MODE_FILECACHE;
else if ($_GET['refresh'] == 'memcached')
$this->skipCache = CACHE_MODE_MEMCACHED;
else if ($_GET['refresh'] == '')
$this->skipCache = CACHE_MODE_FILECACHE | CACHE_MODE_MEMCACHED;
}
// prep js+css includes
$parentVars = get_class_vars(__CLASS__);
if ($parentVars['scripts'] != $this->scripts) // additions set in child class
$this->scripts = array_merge($parentVars['scripts'], $this->scripts);
$this->addScript(...$this->scripts);
if (User::isInGroup(U_GROUP_STAFF | U_GROUP_SCREENSHOT | U_GROUP_VIDEO))
$this->addScript([SC_CSS_FILE, 'css/staff.css'], [SC_JS_FILE, 'js/staff.js']);
// display modes
if (isset($_GET['power']) && method_exists($this, 'generateTooltip'))
$this->mode = CACHE_TYPE_TOOLTIP;
else if (isset($_GET['xml']) && method_exists($this, 'generateXML'))
$this->mode = CACHE_TYPE_XML;
else
{
// get alt header logo
if ($ahl = DB::Aowow()->selectCell('SELECT altHeaderLogo FROM ?_home_featuredbox WHERE ?d BETWEEN startDate AND endDate ORDER BY id DESC', time()))
$this->headerLogo = Util::defStatic($ahl);
$this->gUser = User::getUserGlobals();
$this->gFavorites = User::getFavorites();
$this->pageTemplate['pageName'] = strtolower($pageCall);
$this->wowheadLink = sprintf(WOWHEAD_LINK, Util::$subDomains[User::$localeId], $pageCall, $pageParam);
if (!$this->isValidPage())
$this->error();
}
// requires authed user
if ($this->reqAuth && !User::$id)
$this->forwardToSignIn($_SERVER['QUERY_STRING'] ?? '');
// restricted access
if ($this->reqUGroup && !User::isInGroup($this->reqUGroup))
{
if (User::$id)
$this->error();
else
$this->forwardToSignIn($_SERVER['QUERY_STRING'] ?? '');
}
if (Cfg::get('MAINTENANCE') && !User::isInGroup(U_GROUP_EMPLOYEE))
$this->maintenance();
else if (Cfg::get('MAINTENANCE') && User::isInGroup(U_GROUP_EMPLOYEE))
Util::addNote('Maintenance mode enabled!');
// get errors from previous page from session and apply to template
if (method_exists($this, 'applyCCErrors'))
$this->applyCCErrors();
}
/**********/
/* Checks */
/**********/
// "template_exists"
private function isSaneInclude(string $path, string $file) : bool
{
if (preg_match('/[^\w\-]/i', str_replace('admin/', '', $file)))
return false;
if (!is_file($path.$file.'.tpl.php'))
return false;
return true;
}
// has a valid combination of categories
private function isValidPage() : bool
{
if (!isset($this->category) || empty($this->validCats))
return true;
$c = $this->category; // shorthand
switch (count($c))
{
case 0: // no params works always
return true;
case 1: // null is valid || value in a 1-dim-array || (key for a n-dim-array && ( has more subcats || no further subCats ))
$filtered = array_filter($this->validCats, function ($x) { return is_int($x); });
return $c[0] === null || in_array($c[0], $filtered) || (!empty($this->validCats[$c[0]]) && (is_array($this->validCats[$c[0]]) || $this->validCats[$c[0]] === true));
case 2: // first param has to be a key. otherwise invalid
if (!isset($this->validCats[$c[0]]))
return false;
// check if the sub-array is n-imensional
if (is_array($this->validCats[$c[0]]) && count($this->validCats[$c[0]]) == count($this->validCats[$c[0]], COUNT_RECURSIVE))
return in_array($c[1], $this->validCats[$c[0]]); // second param is value in second level array
else
return isset($this->validCats[$c[0]][$c[1]]); // check if params is key of another array
case 3: // 3 params MUST point to a specific value
return isset($this->validCats[$c[0]][$c[1]]) && in_array($c[2], $this->validCats[$c[0]][$c[1]]);
}
return false;
}
/****************/
/* Prepare Page */
/****************/
// get from cache ?: run generators
protected function prepareContent() : void
{
if (!$this->loadCache())
{
$this->generateContent();
$this->generatePath();
$this->generateTitle();
$this->addArticle();
$this->applyGlobals();
$this->saveCache();
}
if ($this instanceof GuidePage)
{
$this->gPageInfo = ['name' => $this->name];
if (isset($this->author))
$this->gPageInfo['author'] = $this->author;
}
else if (isset($this->type) && isset($this->typeId))
{
$this->gPageInfo = array( // varies slightly for special pages like maps, user-dashboard or profiler
'type' => $this->type,
'typeId' => $this->typeId,
'name' => $this->name
);
}
// only adds edit links to the staff menu: precursor to guides?
if (!empty($this->articleUrl) && !($this instanceof GuidePage && $this->show == GuidePage::SHOW_GUIDE))
{
$this->gPageInfo = array(
'articleUrl' => $this->fullParams, // is actually be the url-param
'editAccess' => isset($this->editAccess) ? $this->editAccess : (U_GROUP_ADMIN | U_GROUP_EDITOR | U_GROUP_BUREAU)
);
}
if (!empty($this->path))
$this->pageTemplate['breadcrumb'] = $this->path;
if (!empty($this->filter))
$this->pageTemplate['filter'] = empty($this->filter['query']) ? 0 : 1;
if (method_exists($this, 'postCache')) // e.g. update dates for events and such
$this->postCache();
// determine contribute tabs
if (isset($this->subject) && !isset($this->contribute))
{
$x = get_class($this->subject);
$this->contribute = $x::$contribute;
}
if ($this->contribute & CONTRIBUTE_CO)
$this->community['co'] = CommunityContent::getComments($this->type, $this->typeId);
if ($this->contribute & CONTRIBUTE_SS)
$this->community['ss'] = CommunityContent::getScreenshots($this->type, $this->typeId);
if ($this->contribute & CONTRIBUTE_VI)
$this->community['vi'] = CommunityContent::getVideos($this->type, $this->typeId);
// as comments are not cached, those globals cant be either
if ($this->contribute)
{
$this->extendGlobalData(CommunityContent::getJSGlobals());
$this->applyGlobals();
}
$this->time = microtime(true) - $this->time;
$this->sumSQLStats();
}
public function addScript(array ...$structs) : void
{
array_walk($structs, function(&$x) { $x = array_pad($x, 3, 0); });
foreach ($structs as [$type, $str, $flags])
{
if (empty($str))
{
trigger_error('GenericPage::addScript - content empty', E_USER_WARNING);
continue;
}
$dynData = strpos($str, '?data=') === 0;
$app = [];
// insert locale string
if ($flags & SC_FLAG_LOCALIZED)
$str = sprintf($str, User::$localeString);
if ($dynData)
{
$app[] = 'locale='.User::$localeId;
$app[] = 't='.$_SESSION['dataKey'];
}
else if (($flags & SC_FLAG_APPEND_LOCALE) && User::$localeId)
$app[] = 'lang='.Util::$subDomains[User::$localeId];
// append anti-cache timestamp
if (!($flags & SC_FLAG_NO_TIMESTAMP) && !$dynData)
if ($type == SC_JS_FILE || $type == SC_CSS_FILE)
$app[] = filemtime('static/'.$str) ?: 0;
if ($app)
$str .= ($dynData ? '&' : '?').implode('&', $app);
switch ($type)
{
case SC_JS_FILE:
$str = ($dynData ? Cfg::get('HOST_URL') : Cfg::get('STATIC_URL')).'/'.$str;
case SC_JS_STRING:
if ($flags & SC_FLAG_PREFIX)
array_unshift($this->js, [$type, $str]);
else
$this->js[] = [$type, $str];
break;
case SC_CSS_FILE:
$str = Cfg::get('STATIC_URL').'/'.$str;
case SC_CSS_STRING:
if ($flags & SC_FLAG_PREFIX)
array_unshift($this->css, [$type, $str]);
else
$this->css[] = [$type, $str];
break;
default:
trigger_error('GenericPage::addScript - unknown script type #'.$type, E_USER_WARNING);
}
}
}
// get article & static infobox (run before processing jsGlobals)
private function addArticle() :void
{
if (isset($this->article))
return;
$article = [];
if (isset($this->guideRevision))
$article = DB::Aowow()->selectRow('SELECT `article`, `quickInfo`, `locale`, `editAccess` FROM ?_articles WHERE `type` = ?d AND `typeId` = ?d AND `rev` = ?d',
Type::GUIDE, $this->typeId, $this->guideRevision);
else if (!empty($this->articleUrl))
$article = DB::Aowow()->selectRow('SELECT `article`, `quickInfo`, `locale`, `editAccess` FROM ?_articles WHERE `url` = ? AND `locale` IN (?a) ORDER BY `locale` DESC, `rev` DESC LIMIT 1',
$this->articleUrl, [User::$localeId, LOCALE_EN]);
else if (!empty($this->type) && isset($this->typeId))
$article = DB::Aowow()->selectRow('SELECT `article`, `quickInfo`, `locale`, `editAccess` FROM ?_articles WHERE `type` = ?d AND `typeId` = ?d AND `locale` IN (?a) ORDER BY `locale` DESC, `rev` DESC LIMIT 1',
$this->type, $this->typeId, [User::$localeId, LOCALE_EN]);
if ($article)
{
if ($article['article'])
(new Markup($article['article']))->parseGlobalsFromText($this->jsgBuffer);
if ($article['quickInfo'])
(new Markup($article['quickInfo']))->parseGlobalsFromText($this->jsgBuffer);
$this->article = array(
'text' => Util::jsEscape(Util::defStatic($article['article'])),
'params' => []
);
if (!empty($this->type) && isset($this->typeId))
$this->article['params']['dbpage'] = true;
// convert U_GROUP_* to MARKUP.CLASS_* (as seen in js-object Markup)
if ($article['editAccess'] & (U_GROUP_ADMIN | U_GROUP_VIP | U_GROUP_DEV))
$this->article['params']['allow'] = '$Markup.CLASS_ADMIN';
else if ($article['editAccess'] & U_GROUP_STAFF)
$this->article['params']['allow'] = '$Markup.CLASS_STAFF';
else if ($article['editAccess'] & U_GROUP_PREMIUM)
$this->article['params']['allow'] = '$Markup.CLASS_PREMIUM';
else if ($article['editAccess'] & U_GROUP_PENDING)
$this->article['params']['allow'] = '$Markup.CLASS_PENDING';
else
$this->article['params']['allow'] = '$Markup.CLASS_USER';
$this->editAccess = $article['editAccess'];
if (empty($this->infobox) && !empty($article['quickInfo']))
$this->infobox = $article['quickInfo'];
if ($article['locale'] != User::$localeId)
$this->article['params']['prepend'] = '<div class="notice-box"><span class="icon-bubble">'.Lang::main('langOnly', [Lang::lang($article['locale'])]).'</span></div>';
if (method_exists($this, 'postArticle')) // e.g. update variables in article
$this->postArticle($this->article['text']);
}
}
// get announcements and notes for user
private function addAnnouncements(bool $pagespecific = true) : void
{
if (!isset($this->announcements))
$this->announcements = [];
// display occured notices
if (([$notes, $level] = Util::getNotes()) && $notes)
{
array_unshift($notes, 'One or more issues occured, while generating this page.');
$colors = array( // [border, text]
CLI::LOG_ERROR => ['C50F1F', 'E51223'],
CLI::LOG_WARN => ['C19C00', 'E5B700'],
CLI::LOG_INFO => ['3A96DD', '42ADFF']
);
$this->announcements[0] = array(
'parent' => 'announcement-0',
'id' => 0,
'mode' => 1,
'status' => 1,
'name' => 'internal error',
'style' => 'color: #'.($colors[$level][1] ?? 'fff').'; font-weight: bold; font-size: 14px; padding-left: 40px; background-image: url('.Cfg::get('STATIC_URL').'/images/announcements/warn-small.png); background-size: 15px 15px; background-position: 12px center; border: dashed 2px #'.($colors[$level][0] ?? 'fff').';',
'text' => '[span]'.implode("[br]", $notes).'[/span]'
);
}
// fetch announcements
if ($this->pageTemplate['pageName'])
{
$ann = DB::Aowow()->Select('SELECT ABS(id) AS ARRAY_KEY, a.* FROM ?_announcements a WHERE status = 1 AND (page = ? OR page = "*") AND (groupMask = 0 OR groupMask & ?d)', $pagespecific ? $this->pageTemplate['pageName'] : '', User::$groups);
foreach ($ann as $k => $v)
{
if ($t = Util::localizedString($v, 'text'))
{
$_ = array(
'parent' => 'announcement-'.$k,
'id' => $v['id'],
'mode' => $v['mode'],
'status' => $v['status'],
'name' => $v['name'],
'text' => Util::defStatic($t)
);
if ($v['style']) // may be empty
$_['style'] = Util::defStatic($v['style']);
$this->announcements[$k] = $_;
}
}
}
}
protected function getCategoryFromUrl(string $urlParam) : void
{
$arr = explode('.', $urlParam);
$params = [];
foreach ($arr as $v)
if (is_numeric($v))
$params[] = (int)$v;
$this->category = $params;
}
protected function forwardToSignIn(string $next = '') : void
{
$next = $next ? '&next='.$next : '';
header('Location: ?account=signin'.$next, true, 302);
}
protected function sumSQLStats() : void
{
Util::arraySumByKey($this->mysql, DB::Aowow()->getStatistics(), DB::World()->getStatistics());
}
/*******************/
/* Special Display */
/*******************/
// unknown entry
public function notFound(string $title = '', string $msg = '') : void
{
if ($this->mode == CACHE_TYPE_TOOLTIP && method_exists($this, 'generateTooltip'))
{
header(MIME_TYPE_JSON);
echo $this->generateTooltip();
}
else if ($this->mode == CACHE_TYPE_XML && method_exists($this, 'generateXML'))
{
header(MIME_TYPE_XML);
echo $this->generateXML();
}
else
{
header('HTTP/1.0 404 Not Found', true, 404);
array_unshift($this->title, Lang::main('nfPageTitle'));
$this->contribute = CONTRIBUTE_NONE;
$this->notFound = array(
'title' => isset($this->typeId) ? Util::ucFirst($title).' #'.$this->typeId : $title,
'msg' => !$msg && isset($this->typeId) ? sprintf(Lang::main('pageNotFound'), $title) : $msg
);
if (isset($this->tabId))
$this->pageTemplate['activeTab'] = $this->tabId;
$this->sumSQLStats();
$this->display('list-page-generic');
}
exit();
}
// unknown page
public function error() : void
{
$this->path = null;
$this->tabId = null;
$this->articleUrl = 'page-not-found';
$this->title[] = Lang::main('errPageTitle');
$this->name = Lang::main('errPageTitle');
$this->lvTabs = [];
$this->addArticle();
$this->sumSQLStats();
header('HTTP/1.0 404 Not Found', true, 404);
$this->display('list-page-generic');
exit();
}
// display brb gnomes
public function maintenance() : void
{
header('HTTP/1.0 503 Service Temporarily Unavailable', true, 503);
header('Retry-After: '.(3 * HOUR));
$this->display('maintenance');
exit();
}
/*******************/
/* General Display */
/*******************/
// load given template string or GenericPage::$tpl
public function display(string $override = '') : void
{
// Heisenbug: IE11 and FF32 will sometimes (under unknown circumstances) cache 302 redirects and stop
// re-requesting them from the server but load them from local cache, thus breaking menu features.
Util::sendNoCacheHeader();
if ($this->mode == CACHE_TYPE_TOOLTIP && method_exists($this, 'generateTooltip'))
$this->displayExtra([$this, 'generateTooltip']);
else if ($this->mode == CACHE_TYPE_XML && method_exists($this, 'generateXML'))
$this->displayExtra([$this, 'generateXML'], MIME_TYPE_XML);
else
{
if (isset($this->tabId))
$this->pageTemplate['activeTab'] = $this->tabId;
if ($override)
{
$this->addAnnouncements(false);
include('template/pages/'.$override.'.tpl.php');
die();
}
else if ($this->tpl)
{
$this->prepareContent();
if (!$this->isSaneInclude('template/pages/', $this->tpl))
{
trigger_error('Error: nonexistant template requested: template/pages/'.$this->tpl.'.tpl.php', E_USER_ERROR);
$this->error();
}
$this->addAnnouncements();
if (isset($this->lvTabs))
array_walk($this->lvTabs, function (&$x) { $x = array_pad($x, 3, null); });
include('template/pages/'.$this->tpl.'.tpl.php');
die();
}
else
$this->error();
}
}
// generate and cache
public function displayExtra(callable $generator, string $mime = MIME_TYPE_JSON) : void
{
$outString = '';
if (!$this->loadCache($outString))
{
$outString = $generator();
$this->saveCache($outString);
}
header($mime);
if (method_exists($this, 'postCache') && ($pc = $this->postCache()))
die(sprintf($outString, ...$pc));
else
die($outString);
}
// load jsGlobal
public function writeGlobalVars() : string
{
$buff = '';
if (!empty($this->guideRating))
$buff .= sprintf(Util::$guideratingString, ...$this->guideRating);
foreach ($this->jsGlobals as $type => $struct)
{
$buff .= " var _ = ".$struct[0].';';
foreach ($struct[1] as $key => $data)
{
foreach ($data as $k => $v)
{
// localizes expected fields .. except for icons .. icons are special
if (in_array($k, ['name', 'namefemale']) && $struct[0] != Type::getJSGlobalString(Type::ICON))
{
$data[$k.'_'.User::$localeString] = $v;
unset($data[$k]);
}
}
$buff .= ' _['.(is_numeric($key) ? $key : "'".$key."'")."]=".Util::toJSON($data).';';
}
$buff .= "\n";
if (!empty($this->typeId) && !empty($struct[2][$this->typeId]))
{
$x = $struct[2][$this->typeId];
// spell
if (!empty($x['tooltip'])) // spell + item
$buff .= "\n _[".$x['id'].'].tooltip_'.User::$localeString.' = '.Util::toJSON($x['tooltip']).";\n";
if (!empty($x['buff'])) // spell
$buff .= " _[".$x['id'].'].buff_'.User::$localeString.' = '.Util::toJSON($x['buff']).";\n";
if (!empty($x['spells'])) // spell + item
$buff .= " _[".$x['id'].'].spells_'.User::$localeString.' = '.Util::toJSON($x['spells']).";\n";
if (!empty($x['buffspells'])) // spell
$buff .= " _[".$x['id'].'].buffspells_'.User::$localeString.' = '.Util::toJSON($x['buffspells']).";\n";
$buff .= "\n";
}
}
return $buff;
}
// load brick
public function brick(string $file, array $localVars = []) : void
{
foreach ($localVars as $n => $v)
$$n = $v;
if (!$this->isSaneInclude('template/bricks/', $file))
trigger_error('Nonexistant template requested: template/bricks/'.$file.'.tpl.php', E_USER_ERROR);
else
include('template/bricks/'.$file.'.tpl.php');
}
// load listview addIns
public function lvBrick(string $file) : void
{
if (!$this->isSaneInclude('template/listviews/', $file))
trigger_error('Nonexistant Listview addin requested: template/listviews/'.$file.'.tpl.php', E_USER_ERROR);
else
include('template/listviews/'.$file.'.tpl.php');
}
// load brick with more text then vars
public function localizedBrick(string $file, int $loc = LOCALE_EN) : void
{
if (!$this->isSaneInclude('template/localized/', $file.'_'.$loc))
{
if ($loc == LOCALE_EN || !$this->isSaneInclude('template/localized/', $file.'_'.LOCALE_EN))
trigger_error('Nonexistant template requested: template/localized/'.$file.'_'.$loc.'.tpl.php', E_USER_ERROR);
else
include('template/localized/'.$file.'_'.LOCALE_EN.'.tpl.php');
}
else
include('template/localized/'.$file.'_'.$loc.'.tpl.php');
}
/**********************/
/* Prepare js-Globals */
/**********************/
// add typeIds <int|array[int]> that should be displayed as jsGlobal on the page
public function extendGlobalIds(int $type, int ...$ids) : void
{
if (!$type || !$ids)
return;
if (!isset($this->jsgBuffer[$type]))
$this->jsgBuffer[$type] = [];
foreach ($ids as $id)
$this->jsgBuffer[$type][] = $id;
}
// add jsGlobals or typeIds (can be mixed in one array: TYPE => [mixeddata]) to display on the page
public function extendGlobalData(array $data, ?array $extra = null) : void
{
foreach ($data as $type => $globals)
{
if (!is_array($globals) || !$globals)
continue;
$this->initJSGlobal($type);
// can be id => data
// or idx => id
// and may be mixed
foreach ($globals as $k => $v)
{
if (is_array($v))
$this->jsGlobals[$type][1][$k] = $v;
else if (is_numeric($v))
$this->extendGlobalIds($type, $v);
}
}
if (is_array($extra) && $extra)
$this->jsGlobals[$type][2] = $extra;
}
// init store for type
private function initJSGlobal(int $type) : void
{
$jsg = &$this->jsGlobals; // shortcut
if (isset($jsg[$type]))
return;
if ($tpl = Type::getJSGlobalTemplate($type))
$jsg[$type] = $tpl;
}
// lookup jsGlobals from collected typeIds
private function applyGlobals() : void
{
foreach ($this->jsgBuffer as $type => $ids)
{
foreach ($ids as $k => $id) // filter already generated data, maybe we can save a lookup or two
if (isset($this->jsGlobals[$type][1][$id]))
unset($ids[$k]);
if (!$ids)
continue;
$this->initJSGlobal($type);
$obj = Type::newList($type, [Cfg::get('SQL_LIMIT_NONE'), ['id', array_unique($ids, SORT_NUMERIC)]]);
if (!$obj)
continue;
$this->extendGlobalData($obj->getJSGlobals(GLOBALINFO_SELF));
// delete processed ids
$this->jsgBuffer[$type] = [];
}
}
/*********/
/* Cache */
/*********/
// visible properties or given strings are cached
private function saveCache(string $saveString = '') : void
{
if ($this->mode == CACHE_TYPE_NONE)
return;
if (!Cfg::get('CACHE_MODE') || Cfg::get('DEBUG'))
return;
$noCache = ['coError', 'ssError', 'viError'];
$cKey = $this->generateCacheKey();
$cache = [];
if (!$saveString)
{
foreach ($this as $key => $val)