From 227d6f4bbef7247093d60dd2b54609fac8814393 Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 29 Nov 2016 13:10:32 +0000 Subject: [PATCH] Added the language and error handlers. --- TO-DO.md | 6 ++ hadron/class_cache.php | 40 ++++++------ hadron/class_error.php | 104 ++++++++++++++++++++++++++++++ hadron/class_language.php | 131 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 261 insertions(+), 20 deletions(-) create mode 100644 hadron/class_error.php create mode 100644 hadron/class_language.php diff --git a/TO-DO.md b/TO-DO.md index 77b7a21..4ee3212 100644 --- a/TO-DO.md +++ b/TO-DO.md @@ -1,3 +1,9 @@ Make Santa into a plugin rather than allowing him to loiter around the core code. Use an avatar generation API rather than having loads of no-avatars. Is there a cache for storing the unchanging parts of the forums? +Make the MCP front page super customisable. +Add gadgets for displaying reports / mod actions. +Add a cache_registry to make it easier for plugins to find out which ones are the HTML caches, which ones are data caches and which ones are compiled caches. +Catch the errors when it fails to connect to Mysql. +Test to see if memcached works and review it. Turn memcached into a plugin? +Add more unit tests. diff --git a/hadron/class_cache.php b/hadron/class_cache.php index e643508..6723213 100644 --- a/hadron/class_cache.php +++ b/hadron/class_cache.php @@ -19,19 +19,19 @@ class Cache protected $main = null; // Users.. - protected $users = array(); - protected $gids = array(); + protected $users = []; + protected $gids = []; // Global data.. - public $data = array(); - protected $original = array(); + public $data = []; + protected $original = []; // Other caches.. - public $other = array(); - protected $oOriginal = array(); + public $other = []; + protected $oOriginal = []; // Speed up finding data in the caches.. - protected $indexes = array(); + protected $indexes = []; // Memcached.. protected $mcache = null; @@ -44,7 +44,7 @@ class Cache function __construct(Container $main, array $config) { $this->main = $main; - $this->other['groups'] = array(); + $this->other['groups'] = []; $db = $main->getDatabase(); if(isset($config['memcached']) && $mem = $config['memcached']) @@ -62,19 +62,19 @@ function __construct(Container $main, array $config) } } - function init(array $user, array $group = array()) + function init(array $user, array $group = []) { $this->users[$user['uid']] = $user; - $this->gids[$user['gid']] = array($user['uid']); + $this->gids[$user['gid']] = [$user['uid']]; - $this->indexes['usernames'] = array(); + $this->indexes['usernames'] = []; $this->indexes['usernames'][$user['username']] = $user['uid']; if($this->mcache) { $this->mcache->set('user_'.$user['uid'], $user); $this->mcache->set('group_'.$user['gid'], $group); - $this->mcache->set('usernames', array($user['username'] => $user['uid'])); + $this->mcache->set('usernames', [$user['username'] => $user['uid']]); } } @@ -135,7 +135,7 @@ function addUser(array $user, array $group = null) if($this->mcache) { $this->mcache->set('user_'.$user['uid'], $user); - if(!$unames = $this->mcache->get('usernames')) $unames = array(); + if(!$unames = $this->mcache->get('usernames')) $unames = []; $unames[$user['username']] = $user['uid']; $this->mcache->set('usernames', $unames); if($group!=null) $this->mcache->add('group_'.$user['gid'], $group); @@ -144,12 +144,12 @@ function addUser(array $user, array $group = null) function addUsers(array $users, $shutdown = false) { - if(isset($users['uid'])) $users = array($users); + if(isset($users['uid'])) $users = [$users]; foreach($users as $user) { list($udata, $gdata) = $this->splitUserDataByGroup($user); - if(!$shutdown) register_shutdown_function(array($this,'addUser'), $udata, $gdata); + if(!$shutdown) register_shutdown_function([$this,'addUser'], $udata, $gdata); else addUser($udata, $gdata); } } @@ -171,7 +171,7 @@ function splitUserDataByGroup(array $data) { $user = array_diff_key($data, $this->main->settings['groupSchema']); $group = array_intersect_key($data, $this->main->settings['groupSchema']); - return array($user, $group); + return [$user,$group]; } /** @@ -213,8 +213,8 @@ function getUserByName($username) function getUsersInBulk(array $ids, $group = false) { - $result = array(); - $groups = array(); + $result = []; + $groups = []; // Anything in memory..? $source = array_intersect_key(array_flip($ids), $this->users); @@ -248,7 +248,7 @@ function getUsersInBulk(array $ids, $group = false) // Anything in the file cache system..? - if($group) return array($result, $groups); + if($group) return [$result,$groups]; return $result; } /* @@ -348,7 +348,7 @@ function writeCache($name, $data = null, $path = null, $raw = false, $ext = 'php $str = " $value) { if(is_array($value)) diff --git a/hadron/class_error.php b/hadron/class_error.php new file mode 100644 index 0000000..ad85f4a --- /dev/null +++ b/hadron/class_error.php @@ -0,0 +1,104 @@ +main = $main; + } + + public function setDevMode($switch) + { + $this->devmode = $switch; + } + + function query($msg, $critical = false) + { + trigger_error($msg,E_USER_ERROR); + if($critical && $this->devmode==1) { + echo "Failed to execute query: {$msg}.\n
Backtrace: "; + debug_print_backtrace(); + exit; + } elseif($critical) { + header("HTTP/1.1 500 Internal Server Error"); + die("Failed to execute query: {$msg}"); + } + else $this->log[] = "Failed to execute query: {$msg}"; + } + + function custom($msg, $critical = false) + { + if($critical && $this->devmode==1) { + echo "{$msg}\n
Backtrace: "; + debug_print_backtrace(); + exit; + } elseif(!$critical) { + header("HTTP/1.1 500 Internal Server Error"); + die($msg); + } + else $this->log[] = $msg; + } + + function raw($msg) { $this->log[] = $msg; } + + function getError($type, $msg = null) + { + $plugins = $this->main->getPlugins(); + + // Does the page not exist? + if($type==404) return $plugins->hook("error_404", $msg); + + // Not have permission to view this page? + elseif($type==403) return $plugins->hook("error_403", $msg); + + // An error in the server itself? + elseif($type==500) return $plugins->hook("error_500", $msg); + return false; + } + + function pop($offset = false) + { + if($offset) return $this->log[$offset]; + if(count($this->log)==0) die($this->getError(500,"An unknown error was detected.")); + $index = count($this->log) - 1; + $log = $this->log[$index]; + unset($this->log[$index]); + return $log; + } + + function output($offset = false) + { + if($offset) echo $this->log[$offset]; + elseif(count($this->log)!=0) { + $log = $this->log; + foreach($log as $l) echo $l."
\n"; + $this->log = []; + } + } + + // Make sure errors are always outputted.. + function __destruct() + { + if(count($this->log)!=0) + { + $log = $this->log; + foreach($log as $l) echo $l."
\n"; + } + } +} \ No newline at end of file diff --git a/hadron/class_language.php b/hadron/class_language.php new file mode 100644 index 0000000..d1f7208 --- /dev/null +++ b/hadron/class_language.php @@ -0,0 +1,131 @@ +main = $main; + $this->path = $path; + $plugins = $main->getPlugins(); + + // Does the language pack exist? + if(is_dir("{$path}/{$language}{$dir}")) $this->language = $language; + + // Does a plugin want to change this? + elseif($res = $plugins->hook("langs_init", $language)) $this->language = $res; + + // Default to english then.. + else $this->language = "english"; + + $this->dir = $dir; + + // Stick this.. + $tmpls = $this->main->getTemplates(); + $tmpls->stick("lang", $this->data); + + // Load the global language file.. + $this->load("global"); + } + + function load($name) + { + $plugins = $this->main->getPlugins(); + if($res = $plugins->hook("langs_load", $name)) + { + $this->data = array_merge($this->data,$res); + return true; + } + elseif($res===false) return false; + + if(!require_once("{$this->path}/{$this->language}{$this->dir}/{$name}.php")) + trigger_error("The '{$name}' language file was unable to be loaded.", E_USER_WARNING); + + if(!isset($l)) trigger_error("The '{$name}' language file doesn't contain any language strings.", E_USER_WARNING); + + if(($res = $plugins->hook("langs_load_end", $name))!==null) $l = array_merge($l,$res); + + // Add it to the loaded data.. + $this->data = array_merge($this->data,$l); + return true; + } + + function get($name) + { + if(!isset($this->data[$name])) throw new \Exception("Unable to find the '{$name}' language string."); + return $this->data[$name]; + } + + function __get($name) + { + if(!isset($this->data[$name])) throw new \Exception("Unable to find the '{$name}' language string."); + return $this->data[$name]; + } + + function sub($name, $data) + { + return str_replace("$1", $data, $this->data[$name]); + } + + function multi_sub($name, $data) + { + $str = $this->data[$name]; + $i = 1; + foreach($data as $item) + { + $str = str_replace('$'.$i, $item ,$str); + $i++; + } + return $str; + } + + function rand($name) + { + if(!is_array($this->data[$name])) return false; + return array_rand($this->data[$name]); + } + + function set($name, $data) + { + $this->data[$name] = $data; + } + + function __set($name, $data) + { + $this->data[$name] = $data; + } + + function import(array $data) + { + $this->data = array_merge($this->data, $data); + } + + function exists($name) + { + return isset($this->data[$name]); + } + + function __isset($name) + { + return isset($this->data[$name]); + } +} \ No newline at end of file