Misc/Cleanup

* moving commonly used strings to defines
 * moving commonly reused/similar page generation functions to the parent
 * generally using consistent return types, more type hints and less strings
 * prevent browser context menu when right clicking on UI elements with their own context menus
 * fixed menu path for icons
This commit is contained in:
Sarjuuk
2020-05-23 13:01:10 +02:00
parent b044488308
commit 6cabfd3864
38 changed files with 418 additions and 618 deletions

View File

@@ -18,7 +18,7 @@ trait TrDetailPage
protected $contribute = CONTRIBUTE_ANY;
protected function generateCacheKey($withStaff = true)
protected function generateCacheKey(bool $withStaff = true) : string
{
$staff = intVal($withStaff && User::isInGroup(U_GROUP_EMPLOYEE));
@@ -32,7 +32,7 @@ trait TrDetailPage
return implode('_', $key);
}
protected function applyCCErrors()
protected function applyCCErrors() : void
{
if (!empty($_SESSION['error']['co']))
$this->coError = $_SESSION['error']['co'];
@@ -56,7 +56,7 @@ trait TrListPage
private $filterObj = null;
protected function generateCacheKey($withStaff = true)
protected function generateCacheKey(bool $withStaff = true) : string
{
$staff = intVal($withStaff && User::isInGroup(U_GROUP_EMPLOYEE));
@@ -73,6 +73,7 @@ trait TrListPage
}
}
trait TrProfiler
{
protected $region = '';
@@ -85,7 +86,7 @@ trait TrProfiler
protected $doResync = null;
protected function generateCacheKey($withStaff = true)
protected function generateCacheKey(bool $withStaff = true) : string
{
$staff = intVal($withStaff && User::isInGroup(U_GROUP_EMPLOYEE));
@@ -95,15 +96,15 @@ trait TrProfiler
return implode('_', $key);
}
protected function getSubjectFromUrl($str)
protected function getSubjectFromUrl(string $pageParam) : void
{
if (!$str)
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('.', $str, 3);
$cat = explode('.', $pageParam, 3);
$cat = array_map('urldecode', $cat);
@@ -131,7 +132,7 @@ trait TrProfiler
}
}
protected function initialSync()
protected function initialSync() : void
{
$this->prepareContent();
@@ -150,7 +151,7 @@ trait TrProfiler
exit();
}
protected function generatePath()
protected function generatePath() : void
{
if ($this->region)
{
@@ -164,6 +165,7 @@ trait TrProfiler
}
}
class GenericPage
{
protected $tpl = '';
@@ -232,7 +234,7 @@ class GenericPage
'zone' => ['template' => 'zone', 'id' => 'zones', 'parent' => 'lv-generic', 'data' => [], 'name' => '$LANG.tab_zones' ]
);
public function __construct($pageCall, $pageParam = null)
public function __construct(string $pageCall = '', string $pageParam = '')
{
$this->time = microtime(true);
@@ -296,11 +298,13 @@ class GenericPage
$this->applyCCErrors();
}
/**********/
/* Checks */
/**********/
private function isSaneInclude($path, $file) // "template_exists"
// "template_exists"
private function isSaneInclude(string $path, string $file) : bool
{
if (preg_match('/[^\w\-]/i', str_replace('admin/', '', $file)))
return false;
@@ -311,7 +315,8 @@ class GenericPage
return true;
}
private function isValidPage() // has a valid combination of categories
// has a valid combination of categories
private function isValidPage() : bool
{
if (!isset($this->category) || empty($this->validCats))
return true;
@@ -341,11 +346,13 @@ class GenericPage
return false;
}
/****************/
/* Prepare Page */
/****************/
protected function prepareContent() // get from cache ?: run generators
// get from cache ?: run generators
protected function prepareContent() : void
{
if (!$this->loadCache())
{
@@ -404,7 +411,7 @@ class GenericPage
$this->sumSQLStats();
}
public function addJS($name, $unshift = false)
public function addJS($name, bool $unshift = false) : void
{
if (is_array($name))
{
@@ -420,7 +427,7 @@ class GenericPage
}
}
public function addCSS($struct, $unshift = false)
public function addCSS(array $struct, bool $unshift = false) : void
{
if (is_array($struct) && empty($struct['path']) && empty($struct['string']))
{
@@ -436,7 +443,8 @@ class GenericPage
}
}
private function addArticle() // get article & static infobox (run before processing jsGlobals)
// get article & static infobox (run before processing jsGlobals)
private function addArticle() :void
{
$article = [];
if (!empty($this->type) && isset($this->typeId))
@@ -492,7 +500,8 @@ class GenericPage
}
}
private function addAnnouncements() // get announcements and notes for user
// get announcements and notes for user
private function addAnnouncements() : void
{
if (!isset($this->announcements))
$this->announcements = [];
@@ -539,9 +548,9 @@ class GenericPage
}
}
protected function getCategoryFromUrl($str)
protected function getCategoryFromUrl(string $urlParam) : void
{
$arr = explode('.', $str);
$arr = explode('.', $urlParam);
$params = [];
foreach ($arr as $v)
@@ -551,43 +560,61 @@ class GenericPage
$this->category = $params;
}
protected function forwardToSignIn($next = '')
protected function forwardToSignIn(string $next = '') : void
{
$next = $next ? '&next='.$next : '';
header('Location: ?account=signin'.$next, true, 302);
}
protected function sumSQLStats()
protected function sumSQLStats() : void
{
Util::arraySumByKey($this->mysql, DB::Aowow()->getStatistics(), DB::World()->getStatistics());
}
/*******************/
/* Special Display */
/*******************/
public function notFound($title, $msg = '') // unknown entry
// unknown entry
public function notFound(string $title = '', string $msg = '') : void
{
array_unshift($this->title, Lang::main('nfPageTitle'));
$this->hasComContent = false;
$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();
header('HTTP/1.0 404 Not Found', true, 404);
$this->display('list-page-generic');
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
{
array_unshift($this->title, Lang::main('nfPageTitle'));
$this->hasComContent = false;
$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();
}
public function error() // unknown page
// unknown page
public function error() : void
{
$this->path = null;
$this->tabId = null;
@@ -606,7 +633,8 @@ class GenericPage
exit();
}
public function maintenance() // display brb gnomes
// display brb gnomes
public function maintenance() : void
{
header('HTTP/1.0 503 Service Temporarily Unavailable', true, 503);
header('Retry-After: '.(3 * HOUR));
@@ -615,46 +643,74 @@ class GenericPage
exit();
}
/*******************/
/* General Display */
/*******************/
public function display($override = '') // load given template string or GenericPage::$tpl
// 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 (isset($this->tabId))
$this->pageTemplate['activeTab'] = $this->tabId;
if ($override)
{
$this->addAnnouncements();
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();
include('template/pages/'.$this->tpl.'.tpl.php');
die();
}
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
$this->error();
{
if (isset($this->tabId))
$this->pageTemplate['activeTab'] = $this->tabId;
if ($override)
{
$this->addAnnouncements();
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();
include('template/pages/'.$this->tpl.'.tpl.php');
die();
}
else
$this->error();
}
}
public function writeGlobalVars() // load jsGlobal
// 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 = '';
@@ -700,7 +756,8 @@ class GenericPage
return $buff;
}
public function brick($file, array $localVars = []) // load brick
// load brick
public function brick(string $file, array $localVars = []) : void
{
foreach ($localVars as $n => $v)
$$n = $v;
@@ -711,7 +768,8 @@ class GenericPage
include('template/bricks/'.$file.'.tpl.php');
}
public function lvBrick($file) // load listview addIns
// 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);
@@ -719,7 +777,8 @@ class GenericPage
include('template/listviews/'.$file.'.tpl.php');
}
public function localizedBrick($file, $loc = LOCALE_EN) // load brick with more text then vars
// load brick with more text then vars
public function localizedBrick(string $file, int $loc = LOCALE_EN) : void
{
if (!$this->isSaneInclude('template/localized/', $file.'_'.$loc))
{
@@ -732,32 +791,27 @@ class GenericPage
include('template/localized/'.$file.'_'.$loc.'.tpl.php');
}
/**********************/
/* Prepare js-Globals */
/**********************/
public function extendGlobalIds($type, $data) // add typeIds <int|array[int]> that should be displayed as jsGlobal on the page
// add typeIds <int|array[int]> that should be displayed as jsGlobal on the page
public function extendGlobalIds(int $type, int ...$ids) : void
{
if (!$type || !$data)
return false;
if (!$type || !$ids)
return;
if (!isset($this->jsgBuffer[$type]))
$this->jsgBuffer[$type] = [];
if (is_array($data))
{
foreach ($data as $id)
$this->jsgBuffer[$type][] = (int)$id;
}
else if (is_numeric($data))
$this->jsgBuffer[$type][] = (int)$data;
foreach ($ids as $id)
$this->jsgBuffer[$type][] = $id;
}
public function extendGlobalData($data, $extra = null) // add jsGlobals or typeIds (can be mixed in one array: TYPE => [mixeddata]) to display on the page
// 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
{
if ($data === null)
throw new ErrorException('ffffuuuu.....!');
foreach ($data as $type => $globals)
{
if (!is_array($globals) || !$globals)
@@ -781,7 +835,8 @@ class GenericPage
$this->jsGlobals[$type][2] = $extra;
}
private function initJSGlobal($type) // init store for type
// init store for type
private function initJSGlobal(int $type) : void
{
$jsg = &$this->jsGlobals; // shortcut
@@ -815,7 +870,8 @@ class GenericPage
}
}
private function applyGlobals() // lookup jsGlobals from collected typeIds
// lookup jsGlobals from collected typeIds
private function applyGlobals() : void
{
foreach ($this->jsgBuffer as $type => $ids)
{
@@ -864,14 +920,16 @@ class GenericPage
}
}
/*********/
/* Cache */
/*********/
public function saveCache($saveString = null) // visible properties or given strings are cached
// visible properties or given strings are cached
private function saveCache(string $saveString = '') : void
{
if ($this->mode == CACHE_TYPE_NONE)
return false;
return;
if (!CFG_CACHE_MODE || CFG_DEBUG)
return;
@@ -894,7 +952,7 @@ class GenericPage
}
}
else
$cache = (string)$saveString;
$cache = $saveString;
if (CFG_CACHE_MODE & CACHE_MODE_MEMCACHED)
{
@@ -949,7 +1007,7 @@ class GenericPage
}
}
public function loadCache(&$saveString = null)
private function loadCache(string &$saveString = '') : bool
{
if ($this->mode == CACHE_TYPE_NONE)
return false;
@@ -1021,7 +1079,7 @@ class GenericPage
return false;;
}
private function memcached()
private function memcached() : Memcached
{
if (!$this->memcached && (CFG_CACHE_MODE & CACHE_MODE_MEMCACHED))
{