Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(frontend): improved notification messages system #1446

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ private function prep_out($data, $crypt_key) {
*/
private function configured() {
if (!$this->server || !$this->port) {
Hm_Debug::add(sprintf('%s enabled but no server or port found', $this->type));
Hm_Debug::add(sprintf('%s enabled but no server or port found', $this->type), 'warning');
return false;
}
if (!$this->supported) {
Hm_Debug::add(sprintf('%s enabled but not supported by PHP', $this->type));
Hm_Debug::add(sprintf('%s enabled but not supported by PHP', $this->type), 'warning');
return false;
}
return true;
Expand Down Expand Up @@ -399,7 +399,7 @@ public function __construct($config, $session) {
if (!$this->check_redis($config) && !$this->check_memcache($config)) {
$this->check_session($config);
}
Hm_Debug::add(sprintf('CACHE backend using: %s', $this->type));
Hm_Debug::add(sprintf('CACHE backend using: %s', $this->type), 'info');
}

/**
Expand Down Expand Up @@ -454,16 +454,16 @@ protected function check_memcache($config) {
protected function log($key, $msg_type) {
switch ($msg_type) {
case 'save':
Hm_Debug::add(sprintf('CACHE: saving "%s" using %s', $key, $this->type));
Hm_Debug::add(sprintf('CACHE: saving "%s" using %s', $key, $this->type), 'info');
break;
case 'hit':
Hm_Debug::add(sprintf('CACHE: hit for "%s" using %s', $key, $this->type));
Hm_Debug::add(sprintf('CACHE: hit for "%s" using %s', $key, $this->type), 'info');
break;
case 'miss':
Hm_Debug::add(sprintf('CACHE: miss for "%s" using %s', $key, $this->type));
Hm_Debug::add(sprintf('CACHE: miss for "%s" using %s', $key, $this->type), 'warning');
break;
case 'del':
Hm_Debug::add(sprintf('CACHE: deleting "%s" using %s', $key, $this->type));
Hm_Debug::add(sprintf('CACHE: deleting "%s" using %s', $key, $this->type), 'info');
break;
}
}
Expand Down Expand Up @@ -633,7 +633,7 @@ public function __construct($config, $session) {
*/
public function setup_cache() {
$cache_class = $this->get_cache_class();
Hm_Debug::add(sprintf('Using %s for cache', $cache_class));
Hm_Debug::add(sprintf('Using %s for cache', $cache_class), 'info');
return new $cache_class($this->config, $this->session);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function __construct($config) {
*/
private function new_settings($username) {
$res = Hm_DB::execute($this->dbh, 'insert into hm_user_settings values(?,?)', [$username, '']);
Hm_Debug::add(sprintf("created new row in hm_user_settings for %s", $username));
Hm_Debug::add(sprintf("created new row in hm_user_settings for %s", $username), 'success');
$this->config = [];
return $res ? true : false;
}
Expand Down Expand Up @@ -417,7 +417,7 @@ public function save($username, $key) {
$this->connect();
if (Hm_DB::execute($this->dbh, 'select settings from hm_user_settings where username=?', [$username])) {
Hm_DB::execute($this->dbh, 'update hm_user_settings set settings=? where username=?', [$config, $username]);
Hm_Debug::add(sprintf("Saved user data to DB for %s", $username));
Hm_Debug::add(sprintf("Saved user data to DB for %s", $username), 'success');
$res = true;
} else {
$res = Hm_DB::execute($this->dbh, 'insert into hm_user_settings values(?,?)', [$username, $config]);
Expand Down Expand Up @@ -518,19 +518,19 @@ function load_user_config_object($config) {
switch ($type) {
case 'DB':
$user_config = new Hm_User_Config_DB($config);
Hm_Debug::add("Using DB user configuration");
Hm_Debug::add("Using DB user configuration", 'info');
break;
case 'custom':
if (class_exists($class)) {
$user_config = new $class($config);
Hm_Debug::add("Using custom user configuration: {$class}");
Hm_Debug::add("Using custom user configuration: {$class}", 'info');
break;
} else {
Hm_Debug::add("User configuration class does not exist: {$class}");
}
default:
$user_config = new Hm_User_Config_File($config);
Hm_Debug::add("Using file based user configuration");
Hm_Debug::add("Using file based user configuration", "info");
break;
}
return $user_config;
Expand Down
6 changes: 3 additions & 3 deletions lib/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static public function execute($dbh, $sql, $args, $type = false, $all = false) {
}
return $sql->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $oops) {
Hm_Msgs::add('ERRDatabase error. Please try again.');
Hm_Msgs::add('Database error. Please try again.', 'danger');
Hm_Debug::add($oops->getMessage());
return false;
}
Expand Down Expand Up @@ -146,11 +146,11 @@ static public function connect($site_config) {
self::$dbh[$key] = new PDO($dsn, self::$config['db_user'], self::$config['db_pass']);
self::$dbh[$key]->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$dbh[$key]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Hm_Debug::add(sprintf('Connecting to dsn: %s', $dsn));
Hm_Debug::add(sprintf('Connecting to dsn: %s', $dsn), "info");
return self::$dbh[$key];
} catch (Exception $oops) {
Hm_Debug::add($oops->getMessage());
Hm_Msgs::add('ERRUnable to connect to the database. Please check your configuration settings and try again.');
Hm_Msgs::add('Unable to connect to the database. Please check your configuration settings and try again.', 'danger');
self::$dbh[$key] = false;
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function redirect_to_url($mod_exec) {
* @return void
*/
private function forward_messages($session, $request) {
$msgs = Hm_Msgs::get();
$msgs = Hm_Msgs::getRaw();
if (!empty($msgs)) {
$session->secure_cookie($request, 'hm_msgs', base64_encode(json_encode($msgs)));
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public function unpack_messages($request, $session) {
if (!empty($request->cookie['hm_msgs'])) {
$msgs = @json_decode(base64_decode($request->cookie['hm_msgs']), true);
if (is_array($msgs)) {
array_walk($msgs, function($v) { Hm_Msgs::add($v); });
array_walk($msgs, function($v) { Hm_Msgs::add($v['text'], $v['type']); });
}
$session->delete_cookie($request, 'hm_msgs');
return true;
Expand All @@ -130,12 +130,12 @@ public function unpack_messages($request, $session) {
*/
static public function page_redirect($url, $status = false) {
if (DEBUG_MODE) {
Hm_Debug::add(sprintf('Redirecting to %s', $url));
Hm_Debug::add(sprintf('Redirecting to %s', $url), 'info');
Hm_Debug::load_page_stats();
Hm_Debug::show();
}
if ($status == 303) {
Hm_Debug::add('Redirect loop found');
Hm_Debug::add('Redirect loop found', 'warning');
Hm_Functions::cease('Redirect loop discovered');
}
Hm_Functions::header('HTTP/1.1 303 Found');
Expand Down Expand Up @@ -197,7 +197,7 @@ private function load_site_lib() {
return;
}
if (is_readable(APP_PATH.'modules/site/lib.php')) {
Hm_Debug::add('Including site module set lib.php');
Hm_Debug::add('Including site module set lib.php', 'info');
require APP_PATH.'modules/site/lib.php';
}
}
Expand Down Expand Up @@ -337,7 +337,7 @@ public function get_page($filters, $request) {
$this->page = 'home';
}
$this->module_exec->page = $this->page;
Hm_Debug::add('Page ID: '.$this->page);
Hm_Debug::add('Page ID: '.$this->page, 'info');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function load($file = '.env') {
}
$envFile = static::get('CYPHT_DOTENV');
if (!file_exists($envFile)) {
Hm_Msgs::add('ERR.env file not found at: "' . $envFile . '"');
Hm_Msgs::add('.env file not found at: "' . $envFile . '"', 'danger');
return;
}
$dotenvLoader->load($envFile);
Expand Down
2 changes: 1 addition & 1 deletion lib/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Hm_Format_JSON extends HM_Format {
* @return string encoded data to be sent to the browser
*/
public function content($output, $allowed_output) {
$output['router_user_msgs'] = Hm_Msgs::get();
$output['router_user_msgs'] = Hm_Msgs::getRaw();
$output = $this->filter_all_output($output, $allowed_output);
if ($this->config->get('encrypt_ajax_requests', false)) {
$output = ['payload' => Hm_Crypt_Base::ciphertext(json_encode($output, JSON_FORCE_OBJECT), Hm_Request_Key::generate())];
Expand Down
8 changes: 4 additions & 4 deletions lib/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Hm_Functions {
*/
public static function setcookie($name, $value, $lifetime = 0, $path = '', $domain = '', $secure = false, $html_only = false, $same_site = 'Strict') {
$prefix = ($lifetime != 0 && $lifetime < time()) ? 'Deleting' : 'Setting';
Hm_Debug::add(sprintf('%s cookie: name: %s, lifetime: %s, path: %s, domain: %s, secure: %s, html_only %s',$prefix, $name, $lifetime, $path, $domain, $secure, $html_only));
Hm_Debug::add(sprintf('%s cookie: name: %s, lifetime: %s, path: %s, domain: %s, secure: %s, html_only %s',$prefix, $name, $lifetime, $path, $domain, $secure, $html_only), 'info');
return setcookie($name, $value, [
'expires' => $lifetime,
'path' => $path,
Expand Down Expand Up @@ -140,7 +140,7 @@ public static function c_init() {
if (extension_loaded('curl')) {
return curl_init();
} else {
Hm_Msgs::add('ERRPlease enable the cURL extension.');
Hm_Msgs::add('Please enable the cURL extension.', 'warning');
return false;
}
}
Expand All @@ -152,7 +152,7 @@ public static function c_exec($handle) {
$response = curl_exec($handle);
if ($response === false) {
$error = curl_error($handle);
Hm_Msgs::add('ERRcURL error: '.$error);
Hm_Msgs::add('cURL error: '.$error, 'danger');
}
return $response;
}
Expand Down Expand Up @@ -265,7 +265,7 @@ function hm_exists($name) {
$caller = array_shift($bt);
$module = hm_get_module_from_path($caller['file']);
if (function_exists($name)) {
Hm_Debug::add(sprintf('Function in %s replaced: %s', $module, $name));
Hm_Debug::add(sprintf('Function in %s replaced: %s', $module, $name), 'warning');
return true;
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions lib/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function get($name, $default = NULL, $typed = true) {
$val = $this->output[$name];
if (!is_null($default) && $typed) {
if (gettype($default) != gettype($val)) {
Hm_Debug::add(sprintf('TYPE CONVERSION: %s to %s for %s', gettype($val), gettype($default), $name));
Hm_Debug::add(sprintf('TYPE CONVERSION: %s to %s for %s', gettype($val), gettype($default), $name), 'info');
settype($val, gettype($default));
}
}
Expand Down Expand Up @@ -439,7 +439,7 @@ public function module_is_supported($name) {
}

public function save_hm_msgs() {
$msgs = Hm_Msgs::get();
$msgs = Hm_Msgs::getRaw();
if (!empty($msgs)) {
Hm_Msgs::flush();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(json_encode($msgs)));
Expand Down Expand Up @@ -499,7 +499,7 @@ public function trans($string) {
}
}
else {
Hm_Debug::add(sprintf('TRANSLATION NOT FOUND :%s:', $string));
Hm_Debug::add(sprintf('TRANSLATION NOT FOUND :%s:', $string), 'warning');
}
return str_replace('\n', '<br />', strip_tags($string));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static function add($page, $module, $logged_in, $marker=false, $placement
self::add_page($page);

if (array_key_exists($module, self::$module_list[$page])) {
Hm_Debug::add(sprintf("Already registered module for %s re-attempted: %s", $page, $module));
Hm_Debug::add(sprintf("Already registered module for %s re-attempted: %s", $page, $module), 'warning');
return true;
}
$source === false ? $source = self::$source : false;
Expand Down
38 changes: 28 additions & 10 deletions lib/output.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ trait Hm_List {
* @param string $string message to add
* @return void
*/
public static function add($string) {
self::$msgs[] = self::str($string, false);
public static function add($string, $type = 'success') {
self::$msgs[] = ['type' => $type, 'text' => self::str($string, false)];
}

/**
* Return all messages
* @return array all messages
*/
public static function get() {
public static function getRaw() {
return self::$msgs;
}

Expand Down Expand Up @@ -110,12 +110,21 @@ public static function str($mixed, $return_type = true) {
return $str;
}

public static function get() {
return array_map(function ($msg) {
return $msg['text'];
}, self::$msgs);
}

/**
* Log all messages
* @return bool
*/
public static function show() {
return Hm_Functions::error_log(print_r(self::$msgs, true));
$msgs = array_map(function ($msg) {
return strtoupper($msg['type']) . ': ' . $msg['text'];
}, self::$msgs);
return Hm_Functions::error_log(print_r($msgs, true));
}
}

Expand All @@ -129,18 +138,27 @@ class Hm_Msgs { use Hm_List; }
*/
class Hm_Debug {

use Hm_List;
use Hm_List {
add as protected self_add;
}

/**
* @override
*/
public static function add($string, $type = 'danger') {
self::self_add($string, $type);
}

/**
* Add page execution stats to the Hm_Debug list
* @return void
*/
public static function load_page_stats() {
self::add(sprintf("PHP version %s", phpversion()));
self::add(sprintf("Zend version %s", zend_version()));
self::add(sprintf("Peak Memory: %d", (memory_get_peak_usage(true)/1024)));
self::add(sprintf("PID: %d", getmypid()));
self::add(sprintf("Included files: %d", count(get_included_files())));
self::add(sprintf("PHP version %s", phpversion()), 'info');
self::add(sprintf("Zend version %s", zend_version()), 'info');
self::add(sprintf("Peak Memory: %d", (memory_get_peak_usage(true)/1024)), 'info');
self::add(sprintf("PID: %d", getmypid()), 'info');
self::add(sprintf("Included files: %d", count(get_included_files())), 'info');
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public function __construct($filters, $config) {
if (!$config->get('disable_empty_superglobals', false)) {
$this->empty_super_globals();
}
Hm_Debug::add('Using sapi: '.$this->sapi);
Hm_Debug::add('Request type: '.$this->type);
Hm_Debug::add('Request path: '.$this->path);
Hm_Debug::add('TLS request: '.intval($this->tls));
Hm_Debug::add('Mobile request: '.intval($this->mobile));
Hm_Debug::add('Using sapi: '.$this->sapi, 'info');
Hm_Debug::add('Request type: '.$this->type, 'info');
Hm_Debug::add('Request path: '.$this->path, 'info');
Hm_Debug::add('TLS request: '.intval($this->tls), 'info');
Hm_Debug::add('Mobile request: '.intval($this->mobile), 'info');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/session_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public function setup_session() {
if (!Hm_Functions::class_exists($auth_class)) {
Hm_Functions::cease('Invalid auth configuration');
}
Hm_Debug::add(sprintf('Using %s with %s', $session_class, $auth_class));
Hm_Debug::add(sprintf('Using %s with %s', $session_class, $auth_class), 'info');
return new $session_class($this->config, $auth_class);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/session_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function start_new($request) {
$this->session_key = Hm_Crypt::unique_id();
$this->secure_cookie($request, $this->cname, $this->session_key, '', '', 'Lax');
if ($this->insert_session_row()) {
Hm_Debug::add('LOGGED IN');
Hm_Debug::add('LOGGED IN', 'success');
$this->active = true;
return;
}
Expand All @@ -81,7 +81,7 @@ public function start_existing($key) {
$this->session_key = $key;
$data = $this->get_session_data($key);
if (is_array($data)) {
Hm_Debug::add('LOGGED IN');
Hm_Debug::add('LOGGED IN', 'success');
$this->active = true;
$this->data = $data;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/session_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function validate_session_data($request) {
if ($this->existing && count($this->data) == 0) {
$this->destroy($request);
} else {
Hm_Debug::add('LOGGED IN');
Hm_Debug::add('LOGGED IN', 'success');
$this->active = true;
}
}
Expand All @@ -88,7 +88,7 @@ protected function start_session_data($request) {
$this->data = $data;
} elseif (!$this->loaded) {
$this->destroy($request);
Hm_Debug::add('Mismatched session level encryption key');
Hm_Debug::add('Mismatched session level encryption key', 'warning');
}
}
}
Expand Down
Loading