This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
dogstats/web/inc/core.php

172 lines
5.3 KiB
PHP
Raw Permalink Normal View History

2023-10-18 22:42:01 +02:00
<?php
spl_autoload_register('autoload');
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');
2023-10-22 01:46:22 +02:00
else if (file_exists(ROOT . DS . 'models' . DS . $className . '.model.php'))
require_once(ROOT . DS . 'models' . DS . $className . '.model.php');
2023-10-18 22:42:01 +02:00
else if (file_exists(ROOT . DS . 'pages' . DS . strtolower($className) . DS . 'controller.php'))
require_once(ROOT . DS . 'pages' . DS . strtolower($className) . DS . 'controller.php');
}
function includeManagement()
{
require_once(ROOT.DS.'inc'.DS.'helpers.php');
require_once(ROOT.DS.'inc'.DS.'config.inc.php');
//settings from config
if(defined('DEV') && DEV===true)
ini_set("display_errors", 1);
else ini_set("display_errors", 0);
if(file_exists(ROOT.DS.'inc'.DS.'vendor'.DS.'autoload.php'))
require_once(ROOT.DS.'inc'.DS.'vendor'.DS.'autoload.php');
2023-10-18 19:08:38 +02:00
//DB
if(defined('REDIS_SERVER') && REDIS_SERVER !='')
2023-10-22 01:46:22 +02:00
{
$redis = new Redis();
try{
2023-10-22 23:05:24 +02:00
$redis->connect(REDIS_SERVER, REDIS_PORT);
2023-10-22 01:46:22 +02:00
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;
}
}
2023-10-18 19:08:38 +02:00
else
$GLOBALS['redis'] = false;
2023-10-18 22:42:01 +02:00
}
function callHook($url)
{
$queryString = array();
if (!$url[0]) {
$component = 'home';
$action = 'index';
} else {
$urlArray = $url;
$component = $urlArray[0];
array_shift($urlArray);
$params = $urlArray;
if (isset($urlArray[0])) {
$action = $urlArray[0];
array_shift($urlArray);
} else
$action = 'index'; // Default Action
$queryString = $urlArray;
}
if (!file_exists(ROOT . DS . 'pages' . DS . $component . DS . 'controller.php')) {
$component = 'err';
$action = 'notfound';
$queryString = array($url);
}
$componentName = ucfirst($component);
$dispatch = new $componentName($component, $action, false);
if (!$dispatch->maySeeThisPage()) {
$componentName = 'err';
$action = 'notallowed';
2023-10-26 22:04:05 +02:00
$dispatch = new $componentName('err', $action, true);
2023-10-18 22:42:01 +02:00
} else
$dispatch = new $componentName($component, $action, true, $queryString);
if (method_exists($componentName, $action))
2023-10-20 20:41:49 +02:00
$response = call_user_func_array(array($dispatch, $action), $queryString);
else if (method_exists($componentName, 'catchAll'))
2023-10-20 20:41:49 +02:00
$response = call_user_func_array(array($dispatch, 'catchAll'), array($params));
else
{
$dispatch = new Err('err', 'notfound', false);
$response = call_user_func_array(array($dispatch, 'notfound'), array($params));
}
2023-10-18 22:42:01 +02:00
2023-10-20 20:41:49 +02:00
if(is_string($response))
return $response;
else
return $dispatch->renderPagecontent();
2023-10-22 01:46:22 +02:00
}
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();
2023-10-22 09:57:29 +02:00
$instance->setSubmenu();
2023-10-22 01:46:22 +02:00
if($instance->maySeeThisPage()===true)
{
$menu_text = $instance->menu_text;
$menu_priority = $instance->menu_priority;
if($menu_text)
{
while($arr[$menu_priority])
$menu_priority++;
2023-10-23 13:59:26 +02:00
$arr[$menu_priority] = array('text'=>$menu_text,'image'=>$instance->menu_image, 'url'=>$file,'menu_classes'=>$instance->menu_classes,'submenu'=>$instance->submenu);
2023-10-22 01:46:22 +02:00
}
}
}
}
closedir($handle);
}
//sort the menu
ksort($arr);
$arr = array_values($arr);
return $arr;
2023-10-29 19:52:08 +01:00
}
function autoLoginCheck()
{
//check if user has a cookie and if so, logg them in and refresh the page
if(isset($_COOKIE['token']) && $_COOKIE['token'] != '' && !$_SESSION['user'])
{
$u = new User();
$allusers = $u->getAll(false);
foreach($allusers as $user)
{
if($user['token'] && $user['token'] == $_COOKIE['token'])
{
$u->id = $user['id'];
break;
}
}
if($u->id) //valid cookie, users gets logged in
{
$u->load($u->id);
$u->login();
$url = '/'.implode('/',$GLOBALS['url']);
header("HX-Redirect: ". $url);
exit('<meta http-equiv="Refresh" content="seconds; url='. $url.'"> <script>window.location.href="'. $url.'"</script> ');
}
else //invalid cookie gets deleted
{
setcookie('token', '', time() - 3600, "/");
}
}
2023-10-18 22:42:01 +02:00
}