Skip to content

Commit

Permalink
Added the language and error handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Azareal committed Nov 29, 2016
1 parent f6ef5e2 commit 227d6f4
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 20 deletions.
6 changes: 6 additions & 0 deletions TO-DO.md
Original file line number Diff line number Diff line change
@@ -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.
40 changes: 20 additions & 20 deletions hadron/class_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'])
Expand All @@ -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']]);
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand All @@ -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];
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
/*
Expand Down Expand Up @@ -348,7 +348,7 @@ function writeCache($name, $data = null, $path = null, $raw = false, $ext = 'php
$str = "<?php\nif(!defined('HADRON_START')) die('You\'re not supposed to be here');\n";
if(is_array($data))
{
$str .= "\$cached_data = array();\n";
$str .= "\$cached_data = [];\n";
foreach($data as $key => $value)
{
if(is_array($value))
Expand Down
104 changes: 104 additions & 0 deletions hadron/class_error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/*
Hadron Framework: Error Handling Object.
Created by Azareal.
Licensed under the terms of the GPLv3.
Copyright Azareal (c) 2013 - 2017
*/

// Hadron Framework namespace..
namespace Hadron;

// Is someone trying to access this directly?
if(!defined("HADRON_START")) die("You are not allowed to access this file directly.");

class Error
{
protected $log = [];
protected $devmode = 0;
protected $main = null;

public function __construct(Container $main)
{
$this->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<br />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<br />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."<br />\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."<br />\n";
}
}
}
131 changes: 131 additions & 0 deletions hadron/class_language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
*
* Hadron Framework: Language Handling Object.
* Created by Azareal.
* Licensed under the terms of the GPLv3.
* Copyright Azareal (c) 2013 - 2017
*
**/

// Hadron Framework namespace..
namespace Hadron;

// Is someone trying to access this directly?
if(!defined("HADRON_START")) die("You are not allowed to access this file directly.");

class Language
{
public $language;
protected $data = [];
public $dir = "";
protected $main = null;
protected $path = null;

function __construct(Container $main, $path, $language = "english", $dir = "")
{
$this->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]);
}
}

0 comments on commit 227d6f4

Please sign in to comment.