-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the language and error handlers.
- Loading branch information
Showing
4 changed files
with
261 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |