fontawesome und jeeede menge reworks
All checks were successful
Build and push / Pulling repo on server (push) Successful in 20s
All checks were successful
Build and push / Pulling repo on server (push) Successful in 20s
This commit is contained in:
194
web/inc/classes/Model.class.php
Normal file
194
web/inc/classes/Model.class.php
Normal file
@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
class Model {
|
||||
public $data;
|
||||
public $id = false;
|
||||
public $redis;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
//redis
|
||||
if ($GLOBALS['redis'])
|
||||
$this->redis = $GLOBALS['redis'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic getter function
|
||||
*
|
||||
* @param $name Variable name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
if (isset($this->data[$name]))
|
||||
return $this->data[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic setter function
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
|
||||
public function getDBFields()
|
||||
{
|
||||
return $this->dbFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all data fields but removes filtered datapoints like passwords, etc
|
||||
*/
|
||||
public function getDataFiltered()
|
||||
{
|
||||
$data = $this->data;
|
||||
if(is_array($this->hidden))
|
||||
foreach($this->hidden as $secretfield)
|
||||
unset($data[$secretfield]);
|
||||
foreach($this->dbFields as $field=>$options)
|
||||
if($options['type']=='csv')
|
||||
$data[$field] = ($data[$field]?explode(';',$data[$field]):[]);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
if (!$this->validate())
|
||||
return false;
|
||||
foreach($this->dbFields as $field=>$options)
|
||||
{
|
||||
if(isset($this->data[$field]))
|
||||
$this->redis->hset($this->dbTable.':'.$this->id,$field,$this->data[$field]);
|
||||
}
|
||||
}
|
||||
|
||||
public function load($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
if(!$this->redis->exists($this->dbTable.':'.$this->id))
|
||||
return false;
|
||||
$keys = array_keys($this->dbFields);
|
||||
|
||||
foreach($keys as $key)
|
||||
{
|
||||
$value = $this->redis->hget($this->dbTable.':'.$this->id,$key);
|
||||
|
||||
if($value!==NULL) //we'll leave null values
|
||||
switch($this->dbFields[$key]['type'])
|
||||
{
|
||||
case 'int': $value = intval($value);break;
|
||||
case 'bool': $value = boolval($value);break;
|
||||
case 'float': $value = floatval($value);break;
|
||||
case 'double': $value = doubleval($value);break;
|
||||
}
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
private function validate()
|
||||
{
|
||||
if (!$this->dbFields)
|
||||
return true;
|
||||
|
||||
$data = $this->data;
|
||||
|
||||
foreach ($this->dbFields as $key => $options) {
|
||||
$type = null;
|
||||
$required = false;
|
||||
|
||||
if(in_array('autoupdate',$options))
|
||||
$this->data[$key] = NULL;
|
||||
|
||||
if (isset($data[$key]))
|
||||
$value = $data[$key];
|
||||
else
|
||||
$value = null;
|
||||
|
||||
if (is_array($value))
|
||||
continue;
|
||||
|
||||
if (isset($desc[0]))
|
||||
$type = $desc[0];
|
||||
if (in_array('required',$options))
|
||||
$required = true;
|
||||
|
||||
if($value===null && $options['autoValMethod'])
|
||||
{
|
||||
if(method_exists($this,$options['autoValMethod']))
|
||||
$value = $this->{$options['autoValMethod']}();
|
||||
else if(function_exists($options['autoValMethod']))
|
||||
$value = $options['autoValMethod']();
|
||||
else
|
||||
$value = null;
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
if($options['default']!==null && $value===null)
|
||||
$value = $options['default'];
|
||||
|
||||
if ($required && strlen($value) == 0) {
|
||||
throw new Exception($this->dbTable . "." . $key . " is required");
|
||||
}
|
||||
if ($value == null)
|
||||
continue;
|
||||
|
||||
switch ($type) {
|
||||
case 'email':
|
||||
$regexp = null;
|
||||
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new Exception("$type validation failed");
|
||||
}
|
||||
break;
|
||||
case 'csv':
|
||||
case "text":
|
||||
$regexp = null;
|
||||
break;
|
||||
case "int":
|
||||
$regexp = "/^[0-9]*$/";
|
||||
break;
|
||||
case "double":
|
||||
$regexp = "/^[0-9\.]*$/";
|
||||
break;
|
||||
case "bool":
|
||||
$regexp = '/^(yes|no|0|1|true|false)$/i';
|
||||
break;
|
||||
case "datetime":
|
||||
$regexp = "/^[0-9a-zA-Z -:]*$/";
|
||||
break;
|
||||
default:
|
||||
$regexp = $type;
|
||||
break;
|
||||
}
|
||||
if (!$regexp)
|
||||
continue;
|
||||
|
||||
if (!preg_match($regexp, $value)) {
|
||||
throw new Exception("$type validation failed");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function delete()
|
||||
{
|
||||
return $this->redis->del($this->dbTable.':'.$this->id);
|
||||
}
|
||||
|
||||
function gen_shorthash(){
|
||||
return substr(uniqid(),-7);
|
||||
}
|
||||
|
||||
function getDateTime($time=false)
|
||||
{
|
||||
return date('Y-m-d H:i:s',($time?$time:time()));
|
||||
}
|
||||
}
|
@ -48,7 +48,8 @@ class Page
|
||||
|
||||
function redirect($url)
|
||||
{
|
||||
header("Location: $url");
|
||||
//header("Location: $url");
|
||||
header("HX-Redirect: ". $url);
|
||||
exit();
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,8 @@ function autoload($className)
|
||||
//one of the global classes?
|
||||
if (file_exists(ROOT . DS . 'inc'. DS. 'classes' . DS . $className . '.class.php'))
|
||||
require_once(ROOT . DS . 'inc'. DS. 'classes' . DS . $className . '.class.php');
|
||||
else if (file_exists(ROOT . DS . 'models' . DS . $className . '.model.php'))
|
||||
require_once(ROOT . DS . 'models' . DS . $className . '.model.php');
|
||||
else if (file_exists(ROOT . DS . 'pages' . DS . strtolower($className) . DS . 'controller.php'))
|
||||
require_once(ROOT . DS . 'pages' . DS . strtolower($className) . DS . 'controller.php');
|
||||
}
|
||||
@ -25,7 +27,22 @@ function includeManagement()
|
||||
|
||||
//DB
|
||||
if(defined('REDIS_SERVER') && REDIS_SERVER !='')
|
||||
$GLOBALS['redis'] = new Redis();
|
||||
{
|
||||
$redis = new Redis();
|
||||
try{
|
||||
$redis->pconnect(REDIS_SERVER, REDIS_PORT);
|
||||
if (defined('REDIS_PASS') && REDIS_PASS)
|
||||
$redis->auth(REDIS_PASS);
|
||||
if (defined('REDIS_PREFIX') && REDIS_PREFIX)
|
||||
$redis->setOption(Redis::OPT_PREFIX, REDIS_PREFIX);
|
||||
if (defined('REDIS_DB') && REDIS_DB)
|
||||
$redis->select(REDIS_DB);
|
||||
$GLOBALS['redis'] = $redis;
|
||||
}
|
||||
catch (Exception $e) {
|
||||
$GLOBALS['redis'] = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
$GLOBALS['redis'] = false;
|
||||
}
|
||||
@ -78,4 +95,40 @@ function callHook($url)
|
||||
return $response;
|
||||
else
|
||||
return $dispatch->renderPagecontent();
|
||||
}
|
||||
|
||||
function getMenu()
|
||||
{
|
||||
//first let's find all possible menu items
|
||||
$arr = array();
|
||||
if ($handle = opendir(ROOT . DS . 'pages')) {
|
||||
while (false !== ($file = readdir($handle))) {
|
||||
if (file_exists(ROOT . DS . 'pages' . DS . $file.DS.'controller.php') && class_exists($file)) {
|
||||
|
||||
$instance = new $file($file, 'index', false);
|
||||
$instance->setMenu();
|
||||
|
||||
if($instance->maySeeThisPage()===true)
|
||||
{
|
||||
$menu_text = $instance->menu_text;
|
||||
$menu_priority = $instance->menu_priority;
|
||||
|
||||
if($menu_text)
|
||||
{
|
||||
while($arr[$menu_priority])
|
||||
$menu_priority++;
|
||||
$arr[$menu_priority] = array('text'=>$menu_text,'image'=>$instance->menu_image, 'url'=>$file,'menu_class'=>$instance->menu_class);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($handle);
|
||||
}
|
||||
|
||||
//sort the menu
|
||||
ksort($arr);
|
||||
|
||||
$arr = array_values($arr);
|
||||
|
||||
return $arr;
|
||||
}
|
@ -3,5 +3,6 @@
|
||||
define('REDIS_SERVER', 'ingress');
|
||||
define('REDIS_PORT', 6379);
|
||||
define('REDIS_DB', 1);
|
||||
define('REDIS_PREFIX', 'dogstats:');
|
||||
|
||||
define('DEV',true);
|
@ -195,4 +195,18 @@ function ulid_to_timestamp($ulid)
|
||||
function escape($str)
|
||||
{
|
||||
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
function uuid4($data = null) {
|
||||
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
|
||||
$data = $data ?? random_bytes(16);
|
||||
assert(strlen($data) == 16);
|
||||
|
||||
// Set version to 0100
|
||||
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
|
||||
// Set bits 6-7 to 10
|
||||
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
|
||||
|
||||
// Output the 36 character UUID.
|
||||
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
||||
}
|
Reference in New Issue
Block a user