basic skeleton of control panel

This commit is contained in:
Adrian Preuß committed 2019-04-07 16:08:17 +02:00
1 parent 399e634a79
commit 5effea0c0c
92 files changed
+68296

No files matched your search

+85
View File
@@ -0,0 +1,85 @@
<?php
namespace fruithost;
use \fruithost\Session;
class AuthFactory {
private static $instance = NULL;
public static function getInstance() {
if(self::$instance === NULL) {
self::$instance = new self();
}
return self::$instance;
}
public function isLoggedIn() {
$user_id = Session::get('user_id');
return (!empty($user_id) && $user_id > 0);
}
public function getID() {
return Session::get('user_id');
}
public function getUsername() {
return Session::get('user_name');
}
public function logout() {
Session::remove('user_id');
Session::remove('user_name');
}
public function login($username, $password) {
$result = Database::single('SELECT `id`, `username`, `password`, UPPER(SHA2(CONCAT(`id`, :salt, :password), 512)) as `crypted` FROM `fh_users` WHERE `username`=:username LIMIT 1', [
'username' => $username,
'password' => $password,
'salt' => MYSQL_PASSWORTD_SALT
]);
if($result == false) {
throw new \Exception('Unknown User');
return false;
}
if($result->password !== $result->crypted) {
throw new \Exception('Password mismatched.');
return false;
}
if($result->id > 0) {
Session::set('user_name', $result->username);
Session::set('user_id', $result->id);
} else {
throw new \Exception('Unknown User');
return false;
}
return true;
}
}
class Auth {
public static function isLoggedIn() {
return AuthFactory::getInstance()->isLoggedIn();
}
public static function login($username, $password) {
return AuthFactory::getInstance()->login($username, $password);
}
public static function logout() {
return AuthFactory::getInstance()->logout();
}
public static function getID() {
return AuthFactory::getInstance()->getID();
}
public static function getUsername() {
return AuthFactory::getInstance()->getUsername();
}
}
?>
+158
View File
@@ -0,0 +1,158 @@
<?php
namespace fruithost;
class Core {
private $modules = null;
private $hooks = null;
private $router = null;
private $template = null;
public function __construct() {
spl_autoload_register([ $this, 'load' ]);
$this->init();
}
public function load($class) {
$this->require('.security');
$this->require('config');
$file = trim($class, BS);
$file_array = explode(BS, $file);
array_shift($file_array);
array_unshift($file_array, 'classes');
$path = sprintf('%s%s.class.php', PATH, implode(DS, $file_array));
if(!file_exists($path)) {
// Check it's an Library
$file_array = explode(BS, $file);
array_unshift($file_array, 'libraries');
$path = sprintf('%s%s.php', PATH, implode(DS, $file_array));
if(file_exists($path)) {
require_once($path);
return;
}
print 'Error Loading: ' . $path;
return;
}
require_once($path);
}
private function require($file) {
$path = sprintf('%s%s.php', PATH, $file);
if(!file_exists($path)) {
print 'ERROR loading: ' . $path;
return;
}
require_once($path);
}
public function getModules() {
return $this->modules;
}
public function getHooks() {
return $this->hooks;
}
public function getTemplate() {
return $this->template;
}
public function getRouter() {
return $this->router;
}
private function init() {
$this->hooks = new Hooks();
$this->modules = new Modules($this);
$this->template = new Template($this);
$this->router = new Router($this);
$this->router->addRoute('/', function() {
if(Auth::isLoggedIn()) {
Response::redirect('/overview');
}
Response::redirect('/login');
});
$this->router->addRoute('/logout', function() {
if(Auth::isLoggedIn()) {
Auth::logout();
}
Response::redirect('/');
});
$this->router->addRoute('/overview', function() {
if(!Auth::isLoggedIn()) {
Response::redirect('/');
}
$this->template->display('overview');
});
$this->router->addRoute('/settings', function() {
if(!Auth::isLoggedIn()) {
Response::redirect('/');
}
$this->template->display('settings');
});
$this->router->addRoute('/account', function() {
if(!Auth::isLoggedIn()) {
Response::redirect('/');
}
$this->template->display('account');
});
$this->router->addRoute('^/module(?:/([a-zA-Z0-9\-_]+)(?:/([a-zA-Z0-9\-_]+))?)?$', function($module = null, $submodule = null) {
if(!Auth::isLoggedIn()) {
Response::redirect('/');
}
if(empty($module)) {
$this->template->display('error/module', [
'module' => $module,
'submodule' => $submodule
]);
return;
}
$this->template->display('module', [
'module' => $module,
'submodule' => $submodule
]);
});
$this->router->addRoute('^/lost-password(?:/(.*))?$', function($token = null) {
if(Auth::isLoggedIn()) {
Response::redirect('/');
}
$this->template->display('lost-password', [
'token' => $token
]);
});
$this->router->addRoute('/login', function() {
if(Auth::isLoggedIn()) {
Response::redirect('/');
}
$this->template->display('login');
});
$this->router->run();
}
}
?>
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace fruithost;
class Database {
public static function query($query, $parameters = []) {
return DatabaseFactory::getInstance()->query($query, $parameters);
}
public static function single($query, $parameters = []) {
return DatabaseFactory::getInstance()->single($query, $parameters);
}
public static function count($query, $parameters = []) {
return DatabaseFactory::getInstance()->count($query, $parameters);
}
public static function fetch($query, $parameters = []) {
return DatabaseFactory::getInstance()->fetch($query, $parameters);
}
public static function update($table, $where, $parameters = []) {
return DatabaseFactory::getInstance()->update($table, $where, $parameters);
}
public static function insert($table, $parameters = []) {
return DatabaseFactory::getInstance()->insert($table, $parameters);
}
public static function delete($table, $parameters = []) {
return DatabaseFactory::getInstance()->delete($table, $parameters);
}
public static function deleteWhereNot($table, $delete_not = [], $parameters = []) {
return DatabaseFactory::getInstance()->deleteWhereNot($table, $delete_not, $parameters);
}
}
?>
+95
View File
@@ -0,0 +1,95 @@
<?php
namespace fruithost;
class DatabaseFactory extends \PDO {
private static $instance = NULL;
public static function getInstance() {
if(self::$instance === NULL) {
self::$instance = new self(sprintf('mysql:host=%s;port=%d;dbname=%s', DATABASE_HOSTNAME, DATABASE_PORT, DATABASE_NAME), DATABASE_USERNAME, DATABASE_PASSWORD, [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
]);
}
return self::$instance;
}
public function query($query, $parameters = []) {
$stmt = $this->prepare($query);
$stmt->execute($parameters);
return $stmt;
}
public function single($query, $parameters = []) {
return $this->query($query, $parameters)->fetch(\PDO::FETCH_OBJ);
}
public function count($query, $parameters = []) {
return $this->query($query, $parameters)->rowCount();
}
public function fetch($query, $parameters = []) {
return $this->query($query, $parameters)->fetchAll(\PDO::FETCH_OBJ);
}
public function update($table, $where, $parameters = []) {
$fields = '';
foreach($parameters AS $name => $value) {
$fields .= sprintf(' `%1$s`=:%1$s,', $name);
}
return $this->query(sprintf('UPDATE `%1$s` SET %2$s WHERE `%3$s`=:%3$s', $table, rtrim($fields, ','), $where), $parameters)->fetchAll(\PDO::FETCH_OBJ);
}
public function reset($table, $where, $old, $new) {
return $this->query(sprintf('UPDATE `%1$s` SET %2$s=%4$d WHERE `%2$s`=:%3$d', $table, $where, $old, $new));
}
public function delete($table, $parameters = []) {
$where = [];
foreach($parameters AS $name => $value) {
$where[] = sprintf('`%s`=:%s', $name, $name);
}
return $this->query(sprintf('DELETE FROM `%s` WHERE %s', $table, implode(', ', $where)), $parameters);
}
public function deleteWhereNot($table, $delete_not = [], $parameters = []) {
$where = [];
$default_parameters = [];
foreach($parameters AS $name => $value) {
$default_parameters[] = sprintf('`%s`=:%s', $name, $name);
}
foreach($delete_not AS $name => $values) {
if(is_array($values)) {
foreach($values AS $index => $value) {
$parameters[$name . '_' . $index] = $value;
$where[] = sprintf('(`%s`!=:%s_%d AND %s)', $name, $name, $index, implode(' AND ', $default_parameters));
}
} else {
$parameters[$name] = $values;
$where[] = sprintf('(`%s`!=:%s AND ?)', $name, $name);
}
}
return $this->query(sprintf('DELETE FROM `%s` WHERE %s', $table, implode(' AND ', $where)), $parameters);
}
public function insert($table, $parameters = []) {
$names = [];
$values = [];
foreach($parameters AS $name => $value) {
$names[] = '`' . $name . '`';
$values[] = ':' . $name;
}
$this->query(sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $table, implode(', ', $names), implode(', ', $values)), $parameters);
return $this->lastInsertId();
}
}
?>
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace fruithost;
class Encryption {
public static function encrypt($data, $secret) {
return base64_encode(openssl_encrypt($data, 'aes-256-cbc', $secret, 0, substr(ENCRYPTION_SALT, 0, 16)));
}
public static function decrypt($data, $secret) {
return openssl_decrypt(base64_decode($data), 'aes-256-cbc', $secret, 0, substr(ENCRYPTION_SALT, 0, 16));
}
}
?>
+216
View File
@@ -0,0 +1,216 @@
<?php
namespace fruithost;
class Hooks {
protected $filters = [];
protected $merged_filters = [];
protected $actions = [];
protected $current_filter = [];
protected function createID($method) {
if(is_string($method)) {
return $method;
}
if(is_object($method)) {
$method = [ $method, '' ];
} else {
$method = (array) $method;
}
if(is_object($method[0])) {
return spl_object_hash($method[0]) . $method[1];
}
if(is_string($method[0])) {
return $method[0] . $method[1];
}
return false;
}
public function callAllHook($args) {
reset($this->filters['all']);
do {
foreach(current($this->filters['all']) AS $entry) {
if(!empty($entry['method'])) {
call_user_func_array($entry['method'], $args);
}
}
} while(next($this->filters['all']) !== false);
}
/* Filter */
public function addFilter($name, $method, $priority = 50) {
$this->filters[$name][$priority][$this->createID($method)] = [
'method' => $method
];
unset($this->merged_filters[$name]);
return true;
}
public function removeFilter($name, $method, $priority = 50) {
$method = $this->createID($method);
if(!isset($this->filters[$name][$priority][$method])) {
return false;
}
unset($this->filters[$name][$priority][$method]);
if(empty($this->filters[$name][$priority])) {
unset($this->filters[$name][$priority]);
}
unset($this->merged_filters[$name]);
return true;
}
public function hasFilter($name, $method = false) {
$exists = isset($this->filters[$name]);
if($method === false || !$exists) {
return $exists;
}
if(!($id = $this->createID($method))) {
return false;
}
foreach(array_keys($this->filters[$name]) AS $priority) {
if(isset($this->filters[$name][$priority][$id])) {
return $priority;
}
}
return false;
}
public function applyFilter($name, $arguments) {
$args = [];
$value = $arguments;
if(isset($this->filters['all'])) {
$this->current_filter[] = $name;
$args = func_get_args();
$this->callAllHook($args);
}
if(!isset($this->filters[$name])) {
if(isset($this->filters['all'])) {
array_pop($this->current_filter);
}
return $value;
}
if(!isset($this->filters['all'])) {
$this->current_filter[] = $name;
}
if(!isset($this->merged_filters[$name])) {
ksort($this->filters[$name]);
$this->merged_filters[$name] = true;
}
reset($this->filters[$name]);
if(empty($args)) {
$args = func_get_args();
}
array_shift($args);
do {
foreach(current($this->filters[$name]) AS $entry) {
if(!empty($entry['method'])) {
$args[0] = $value;
$value = call_user_func_array($entry['method'], $args);
}
}
} while(next($this->filters[$name]) !== false);
array_pop($this->current_filter);
return $value;
}
/* Actions */
public function addAction($name, $method, $priority = 50) {
return $this->addFilter($name, $method, $priority);
}
public function removeAction($name, $method, $priority = 50) {
return $this->removeFilter($name, $method, $priority);
}
public function hasAction($name, $method = false) {
return $this->hasFilter($name, $method);
}
public function runAction($name, $arguments = null) {
if(!is_array($this->actions)) {
$this->actions = [];
}
if(isset($this->actions[$name])) {
++$this->actions[$name];
} else {
$this->actions[$name] = 1;
}
if(isset($this->filters['all'])) {
$this->current_filter[] = $name;
$all_args = func_get_args();
$this->callAllHook($all_args);
}
if(!isset($this->filters[$name])) {
if(isset($this->filters['all'])) {
array_pop($this->current_filter);
}
return false;
}
if(!isset($this->filters['all'])) {
$this->current_filter[] = $name;
}
$args = [];
if(is_array($arguments) && isset($arguments[0]) && is_object($arguments[0]) && count($arguments) == 1) {
$args[] =& $arguments[0];
} else {
$args[] = $arguments;
}
for($a = 2; $a < func_num_args(); $a++) {
$args[] = func_get_arg($a);
}
if(!isset($this->merged_filters[$name])) {
ksort($this->filters[$name]);
$this->merged_filters[$name] = true;
}
reset($this->filters[$name]);
do {
foreach(current($this->filters[$name]) AS $entry) {
if(!empty($entry['method'])) {
call_user_func_array($entry['method'], $args);
}
}
} while(next($this->filters[$name]) !== false);
array_pop($this->current_filter);
return true;
}
}
?>
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace fruithost;
class Module {
private $path = null;
private $info = null;
private $instance = null;
public function __construct($path) {
$this->path = $path;
$this->info = new ModuleInfo($path);
}
public function isValid() {
return (!empty($this->info) && !$this->info->isValid());
}
public function getInstance() {
return $this->instance;
}
public function init($core) {
$script = sprintf('%s%smodule.php', $this->path, DS);
if(!file_exists($script)) {
//throw new \Exception('[Module] had no class: ' . $script);
return;
}
$old = get_declared_classes();
require_once($script);
$new = get_declared_classes();
foreach(array_diff($new, $old) AS $class) {
if(is_subclass_of($class, 'fruithost\\ModuleInterface', true)) {
$reflect = new \ReflectionClass($class);
$instance = $reflect->newInstanceArgs([ $core ]);
$this->instance = $instance;
}
}
if(!empty($this->info->getCategory())) {
$core->getHooks()->addFilter($this->info->getCategory(), function($entries) use ($core) {
$entries[] = (object) [
'name' => $this->info->getName(),
'icon' => $this->info->getIcon(),
'order' => $this->info->getOrder(),
'url' => sprintf('/module/%s', basename($this->path)),
'active' => $core->getRouter()->is(sprintf('/module/%s', basename($this->path)))
];
return $entries;
});
}
return $this;
}
}
?>
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace fruithost;
class ModuleAuthor {
private $name = null;
private $email = null;
private $url = null;
public function __construct($object) {
if(!empty($object->name)) {
$this->name = $object->name;
}
if(!empty($object->email)) {
$this->email = $object->email;
}
if(!empty($object->url)) {
$this->url = $object->url;
}
}
public function getName() {
return $this->name;
}
public function getMail() {
return $this->email;
}
public function getWebsite() {
return $this->url;
}
}
?>
+118
View File
@@ -0,0 +1,118 @@
<?php
namespace fruithost;
class ModuleInfo {
private $name = null;
private $version = null;
private $category = null;
private $icon = null;
private $order = null;
private $description = null;
private $author = null;
private $repository = null;
private $is_valid = false;
public function __construct($path) {
$file = sprintf('%s%smodule.package', $path, DS);
if(!file_exists($file)) {
// throw new \Exception('[Module] module.package not exists: ' . $path);
// @ToDo Exception
return;
}
$content = file_get_contents($file);
if(empty($file)) {
// throw new \Exception('[Module] module.package is empty: ' . $path);
// @ToDo Exception
return;
}
$data = json_decode($content);
if(json_last_error() !== JSON_ERROR_NONE) {
// throw new \Exception('[Module] module.package is broken: ' . $path);
// @ToDo Exception
return;
}
if(!empty($data->name)) {
$this->name = $data->name;
}
if(!empty($data->version)) {
$this->version = $data->version;
}
// @ToDo check Enum
if(!empty($data->category)) {
$this->category = $data->category;
}
if(!empty($data->icon)) {
$this->icon = $data->icon;
}
if(!empty($data->order)) {
$this->order = $data->order;
}
if(!empty($data->description)) {
$this->description = $data->description;
}
if(!empty($data->author)) {
$this->author = new ModuleAuthor($data->author);
}
if(!empty($data->repository)) {
$this->repository = $data->repository;
}
$this->is_valid = true;
}
public function isValid() {
return $this->is_valid;
}
public function getName() {
return $this->name;
}
public function getVersion() {
return $this->version;
}
public function getCategory() {
return $this->category;
}
public function getOrder() {
return $this->order;
}
public function getIcon($raw = false) {
if($raw) {
return $this->icon;
}
// @ToDo If starts with http/https or slash or base64
return sprintf('<i class="material-icons">%s</i>', $this->icon);
}
public function getDescription() {
return $this->description;
}
public function getAuthor() {
return $this->author;
}
public function getRepository() {
return $this->repository;
}
}
?>
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace fruithost;
class ModuleInterface {
private $core = null;
public function __construct($core) {
$this->core = $core;
$this->init();
}
protected function init() {
/* Override Me */
}
public function getCore() {
return $this->core;
}
/* Filter */
public function addFilter($name, $method, $priority = 10) {
return $this->core->getHooks()->addFilter($name, $method, $priority);
}
public function removeFilter($name, $method, $priority = 10) {
return $this->core->getHooks()->removeFilter($name, $method, $priority);
}
public function hasFilter($name, $method = false) {
return $this->core->getHooks()->hasFilter($name, $method);
}
public function applyFilter($name, $arguments) {
return $this->core->getHooks()->applyFilter($name, $arguments);
}
/* Actions */
public function addAction($name, $method, $priority = 10) {
return $this->core->getHooks()->addAction($name, $method, $priority);
}
public function removeAction($name, $method, $priority = 10) {
return $this->core->getHooks()->removeAction($name, $method, $priority);
}
public function hasAction($name, $method = false) {
return $this->core->getHooks()->hasAction($name, $method);
}
public function runAction($name, $arguments) {
return $this->core->getHooks()->runAction($name, $arguments);
}
}
?>
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace fruithost;
class Modules {
private $core = null;
private $modules = [];
public function __construct($core) {
$this->core = $core;
foreach(new \DirectoryIterator($this->getPath()) AS $info) {
if($info->isDot()) {
continue;
}
$this->addModule(new Module(sprintf('%s%s%s', $this->getPath(), DS, $info->getFilename())));
}
}
public function getPath() {
return sprintf('%s%s%s', dirname(PATH), DS, 'modules');
}
public function addModule($module) {
$this->modules[] = $module->init($this->core);
}
}
?>
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace fruithost;
class Request {
public static function url() {
return $_SERVER['REQUEST_URI'];
}
}
?>
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace fruithost;
class Response {
public static function addHeader($name, $value) {
ResponseFactory::getInstance()->addHeader($name, $value);
}
public static function header() {
ResponseFactory::getInstance()->header();
}
public static function redirect($url) {
ResponseFactory::getInstance()->redirect($url);
}
}
?>
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace fruithost;
class ResponseFactory {
private static $instance = NULL;
private $headers = [
'Content-Type' => 'text/html; charset=UTF-8'
];
public static function getInstance() {
if(self::$instance === NULL) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
/* do Nothing */
}
public function addHeader($name, $value) {
$this->headers[$name] = $value;
}
public function header() {
if(count($this->headers) > 0) {
foreach($this->headers AS $name => $value) {
header(sprintf('%s: %s', $name, $value));
}
}
}
public function redirect($url) {
$this->addHeader('Location', $url);
$this->header();
exit();
}
}
?>
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace fruithost;
class Route {
private $path = '/';
private $callback = NULL;
public function getPath() {
return $this->path;
}
public function setPath($path) {
$this->path = $path;
}
public function getCallback() {
return $this->callback;
}
public function setCallback($callback) {
$this->callback = $callback;
}
}
?>
+99
View File
@@ -0,0 +1,99 @@
<?php
namespace fruithost;
class Router {
private $routes = [];
private $redirect = NULL;
private $current = NULL;
private $core;
public function __construct($core) {
$this->core = $core;
}
public function addRoute($name, $callback) {
$route = new Route();
$this->routes[$name] = $route;
$route->setPath($name);
$route->setCallback($callback);
return $route;
}
public function routeExists($name) {
foreach($this->routes AS $route) {
if(preg_match('/(\(|\)|\[|\])/Uis', $route->getPath()) && preg_match('#' . $route->getPath() . '#Uis', $name)) {
return true;
}
}
return array_key_exists($name, $this->routes);
}
public function executeRoute($name) {
$this->current = $name;
foreach($this->routes AS $route) {
if(preg_match('/(\(|\)|\[|\])/Uis', $route->getPath())) {
if(preg_match('#' . $route->getPath() . '#Uis', $name, $matches)) {
array_shift($matches);
call_user_func_array($route->getCallback(), $matches);
return;
}
}
}
$route = $this->routes[$name];
$callback = $route->getCallback();
$callback();
}
public function getCurrent() {
return $this->current;
}
public function is($name) {
return (strtolower($this->getCurrent()) === strtolower($name));
}
public function redirectTo($redirect) {
$this->redirect = $redirect;
}
public function run() {
$uri = explode('/', $_SERVER['REQUEST_URI']);
$name = explode('/', $_SERVER['SCRIPT_NAME']);
for($i = 0; $i < sizeof($name); $i++) {
if($uri[$i] == $name[$i]) {
unset($uri[$i]);
}
}
$command = array_values($uri);
$route = '';
foreach($command AS $index => $directory) {
$route .= '/' . $directory;
}
// Remove "GET"
if(preg_match('/\?/Uis', $route)) {
$split = explode('?', $route);
$route = $split[0];
}
Response::header();
if($this->routeExists($route)) {
$this->executeRoute($route);
} else {
if(empty($this->redirect)) {
$this->core->getTemplate()->display('error/404');
} else {
Response::redirect($this->redirect);
}
}
}
}
?>
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace fruithost;
class Session {
public static function init() {
if(!isset($_SESSION)) {
session_save_path(sprintf('%s%stemp', dirname(PATH), DS));
session_start([
'cookie_lifetime' => 3600,
'read_and_close' => false
]);
}
}
public static function get($name) {
self::init();
if(isset($_SESSION[$name])) {
return $_SESSION[$name];
}
return NULL;
}
public static function set($name, $value) {
self::init();
$_SESSION[$name] = $value;
}
public static function remove($name) {
self::init();
$_SESSION[$name] = NULL;
unset($_SESSION[$name]);
}
}
?>
+176
View File
@@ -0,0 +1,176 @@
<?php
namespace fruithost;
use \fruithost\Database;
class Template extends TemplateDefaults {
private $core;
private $theme = null;
private $assigns = [];
private $files = null;
private $navigation = null;
public function __construct($core) {
$this->core = $core;
$this->files = new TemplateFiles();
$this->navigation = new TemplateNavigation($this->core);
$this->theme = $this->core->getHooks()->applyFilter('theme_name', 'default');
ob_start('ob_gzhandler');
$this->core->getHooks()->addAction('html_head', [ $this, 'head_robots' ]);
$this->core->getHooks()->addAction('html_head', [ $this, 'head_scripts' ]);
$this->core->getHooks()->addAction('html_foot', [ $this, 'foot_scripts' ]);
$this->files->addStylesheet('bootstrap', $this->url('css/bootstrap/bootstrap.min.css'), '4.3.1');
$this->files->addStylesheet('jquery-ui', $this->url('css/jquery-ui/jquery-ui.css'), '1.12.1');
$this->files->addStylesheet('material-icons', $this->url('fonts/material-icons/material-icons.css'), '3.0.1', [ 'bootstrap' ]);
$this->files->addJavascript('jquery', $this->url('js/jquery/jquery-3.3.1.min.js'), '3.3.1', [], TemplateFiles::FOOTER);
$this->files->addJavascript('bootstrap', $this->url('js/bootstrap/bootstrap.bundle.min.js'), '4.3.1', [ 'jquery' ], TemplateFiles::FOOTER);
$this->navigation->addCategory('account', 'Account');
$this->navigation->addCategory('database', 'Databases');
$this->navigation->addCategory('domain', 'Domains');
$this->navigation->addCategory('mail', 'E-Mails');
$this->navigation->addCategory('ftp', 'FTP');
$this->navigation->addCategory('hosting', 'Hosting');
$this->navigation->addCategory('extended', 'Extended');
$this->navigation->addCategory('support', 'Support');
$this->assign('navigation', $this->navigation);
}
public function getCore() {
return $this->core;
}
public function getFiles() {
return $this->files;
}
public function assign($name, $value) {
$this->assigns[$name] = $value;
}
public function display($file, $arguments = []) {
$template = $this;
foreach($arguments AS $name => $value) {
${$name} = $value;
}
$path = sprintf('%1$s%2$sthemes%2$s%4$s%2$s%3$s.php', dirname(PATH), DS, $file, $this->theme);
$handler = sprintf('%1$s%2$shandler%2$s%3$s.php', PATH, DS, $file);
foreach($this->assigns AS $name => $value) {
${$name} = $value;
}
if(file_exists($handler)) {
require_once($handler);
foreach($this->assigns AS $name => $value) {
${$name} = $value;
}
}
if(file_exists($path)) {
require_once($path);
} else {
if(!Auth::isLoggedIn()) {
$this->getFiles()->addStylesheet('login', $this->url('css/login.css'), '1.0.0', [ 'bootstrap', 'material-icons' ]);
} else {
$this->getFiles()->addStylesheet('style', $this->url('css/style.css'), '1.0.0', [ 'bootstrap', 'material-icons' ]);
$this->getFiles()->addJavascript('jquery-ui', $this->url('js/jquery-ui/jquery-ui.min.js'), '1.12.1', [ 'jquery' ], TemplateFiles::FOOTER);
$this->getFiles()->addJavascript('ui', $template->url('js/ui.js'), '1.0.0', [ 'jquery', 'jquery-ui', 'bootstrap' ], TemplateFiles::FOOTER);
}
$path = sprintf('%1$s%2$stheme%2$s%3$s.php', PATH, DS, $file);
if(file_exists($path)) {
require_once($path);
}
}
}
public function header() {
$template = $this;
$path = sprintf('%1$s%2$sthemes%2$s%4$s%2$s%3$s.php', dirname(PATH), DS, 'header', $this->theme);
foreach($this->assigns AS $name => $value) {
${$name} = $value;
}
if(file_exists($path)) {
require_once($path);
} else {
$path = sprintf('%1$s%2$stheme%2$s%3$s.php', PATH, DS, 'header');
if(file_exists($path)) {
require_once($path);
}
}
}
public function footer() {
$template = $this;
$path = sprintf('%1$s%2$sthemes%2$s%4$s%2$s%3$s.php', dirname(PATH), DS, 'footer', $this->theme);
foreach($this->assigns AS $name => $value) {
${$name} = $value;
}
if(file_exists($path)) {
require_once($path);
} else {
$path = sprintf('%1$s%2$stheme%2$s%3$s.php', PATH, DS, 'footer');
if(file_exists($path)) {
require_once($path);
}
}
}
public function getLanguage($short = false) {
// @ToDo
if($short) {
return 'en';
}
return 'en_US';
}
public function head() {
$this->core->getHooks()->runAction('html_head');
}
public function foot() {
$this->core->getHooks()->runAction('html_foot');
}
public function url($path) {
$scheme = 'http';
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$scheme = 'https';
}
if($path === true) {
$path = $_SERVER['REQUEST_URI'];
}
if(substr($path, 0, 1) === '/') {
$path = substr($path, 1);
}
if(!empty($parameters)) {
if(strpos($path, '?')) {
$path = explode('?', $path);
$path = $path[0];
}
$path .= '?' . http_build_query(array_merge($_GET, $parameters));
}
return $scheme . '://' . $_SERVER['HTTP_HOST'] . '/' . $path;
}
}
?>
+108
View File
@@ -0,0 +1,108 @@
<?php
namespace fruithost;
class TemplateDefaults {
public function head_robots() {
printf('<meta name="robots" content="%s" />', $this->getCore()->getHooks()->applyFilter('meta_robots', 'noindex,follow'));
}
public function head_scripts() {
$loaded = [];
foreach($this->getFiles()->getHeaderStylesheets() AS $name => $entry) {
if(!empty($entry->depencies)) {
$continue = true;
foreach($entry->depencies AS $needed) {
if(!array_search($needed, $loaded)) {
$continue = false;
}
}
if($continue) {
continue;
}
}
if(!in_array($name, $loaded)) {
printf('<link rel="stylesheet" type="text/css" href="%s%sv=%s" />', $entry->file, (strpos($entry->file, '?') === false ? '?t=' . time() . '&' : '&t=' . time() . '&'), $entry->version);
$loaded[] = $name;
}
}
$loaded = [];
foreach($this->getFiles()->getHeaderJavascripts() AS $name => $entry) {
if(!empty($entry->depencies)) {
$continue = true;
foreach($entry->depencies AS $needed) {
if(!array_search($needed, $loaded)) {
$continue = false;
}
}
if($continue) {
continue;
}
}
if(!in_array($name, $loaded)) {
printf('<script type="text/javascript" src="%s%sv=%s"></script>', $entry->file, (strpos($entry->file, '?') === false ? '?' : '&'), $entry->version);
$loaded[] = $name;
}
}
}
public function foot_scripts() {
$loaded = [];
foreach($this->getFiles()->getFooterStylesheets() AS $name => $entry) {
if(!empty($entry->depencies)) {
$continue = true;
foreach($entry->depencies AS $needed) {
if(!array_search($needed, $loaded)) {
$continue = false;
}
}
if($continue) {
continue;
}
}
if(!in_array($name, $loaded)) {
printf('<link rel="stylesheet" type="text/css" href="%s%sv=%s" />', $entry->file, (strpos($entry->file, '?') === false ? '?' : '&'), $entry->version);
$loaded[] = $name;
}
}
$loaded = [];
foreach($this->getFiles()->getFooterJavascripts() AS $name => $entry) {
if(!empty($entry->depencies)) {
$continue = true;
foreach($entry->depencies AS $needed) {
if(!array_search($needed, $loaded)) {
$continue = false;
}
}
if($continue) {
continue;
}
}
if(!in_array($name, $loaded)) {
printf('<script type="text/javascript" src="%s%sv=%s"></script>', $entry->file, (strpos($entry->file, '?') === false ? '?' : '&'), $entry->version);
$loaded[] = $name;
}
}
}
}
?>
+135
View File
@@ -0,0 +1,135 @@
<?php
namespace fruithost;
class TemplateFiles {
const HEADER = 1;
const FOOTER = 2;
const STYLESHEET = 3;
const JAVASCRIPT = 4;
private $header = [
'javascripts' => [],
'stylesheets' => []
];
private $footer = [
'javascripts' => [],
'stylesheets' => []
];
public function addJavaScript($name, $file, $version, $depencies = null, $position = TemplateFiles::HEADER) {
$this->add($name, $file, $version, $depencies, $position, TemplateFiles::JAVASCRIPT);
}
public function addStyleSheet($name, $file, $version, $depencies = null, $position = TemplateFiles::HEADER) {
$this->add($name, $file, $version, $depencies, $position, TemplateFiles::STYLESHEET);
}
private function add($name, $file, $version, $depencies, $position = TemplateFiles::HEADER, $type = null) {
if($type === null) {
return;
}
$destination = null;
switch($type) {
case TemplateFiles::JAVASCRIPT:
$destination = 'javascripts';
break;
case TemplateFiles::STYLESHEET:
$destination = 'stylesheets';
break;
}
if($destination === null) {
return;
}
switch($position) {
case TemplateFiles::HEADER:
$this->header[$destination][$name] = (object) [
'name' => $name,
'file' => $file,
'version' => $version,
'depencies' => $depencies
];
break;
case TemplateFiles::FOOTER:
$this->footer[$destination][$name] = (object) [
'name' => $name,
'file' => $file,
'version' => $version,
'depencies' => $depencies
];
break;
}
}
private function sortDepencies($input) {
$cloned = array_replace([], $input);
$depencies = [];
$output = [];
do {
$added = false;
foreach($cloned AS $name => $entry) {
$depencies = empty($entry->depencies) ? [] : $entry->depencies;
if(count(array_diff($depencies, $depencies)) === 0) {
$depencies[] = $name;
$added = true;
unset($cloned[$name]);
break;
}
}
} while($added);
if(count($cloned)) {
//trigger_error("unable to resolve a dependency",E_USER_ERROR);
}
foreach($depencies AS $name) {
$output[$name] = $input[$name];
}
return $output;
}
public function getHeaderStylesheets($depency_order = true) {
if($depency_order) {
return $this->sortDepencies($this->header['stylesheets']);
}
return $this->header['stylesheets'];
}
public function getFooterStylesheets($depency_order = true) {
if($depency_order) {
return $this->sortDepencies($this->footer['stylesheets']);
}
return $this->footer['stylesheets'];
}
public function getHeaderJavascripts($depency_order = true) {
if($depency_order) {
return $this->sortDepencies($this->header['javascripts']);
}
return $this->header['javascripts'];
}
public function getFooterJavascripts($depency_order = true) {
if($depency_order) {
return $this->sortDepencies($this->footer['javascripts']);
}
return $this->footer['javascripts'];
}
public function getFooter() {
return $this->footer;
}
}
?>
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace fruithost;
class TemplateNavigation {
private $core = null;
private $entries = [];
public function __construct($core) {
$this->core = $core;
}
public function addCategory($name, $label) {
$category = new TemplateNavigationCategory($this, $name, $label);
$this->entries[$category->getID()] = $category;
}
public function getCore() {
return $this->core;
}
public function isEmpty() {
return (count($this->entries) === 0);
}
public function getEntries() {
return $this->entries;
}
}
?>
@@ -0,0 +1,81 @@
<?php
namespace fruithost;
class TemplateNavigationCategory {
private $navigation = null;
private $id = null;
private $label = null;
public function __construct($navigation, $id, $label) {
$this->navigation = $navigation;
$this->id = $id;
$this->label = $label;
}
public function getID() {
return $this->id;
}
public function getLabel() {
return $this->label;
}
public function isEmpty() {
return (count($this->getEntries()) === 0);
}
public function getEntries() {
$hardcoded = [];
if($this->id === 'account') {
$hardcoded = [
(object) [
'name' => 'Account',
'icon' => '<i class="material-icons">account_circle</i>',
'order' => 1,
'url' => '/account',
'active' => $this->navigation->getCore()->getRouter()->is('/account')
],
(object) [
'name' => 'Settings',
'icon' => '<i class="material-icons">settings</i>',
'order' => 2,
'url' => '/settings',
'active' => $this->navigation->getCore()->getRouter()->is('/settings')
],
(object) [
'name' => 'Logout',
'icon' => '<i class="material-icons">power_settings_new</i>',
'order' => 99999,
'url' => '/logout',
'active' => $this->navigation->getCore()->getRouter()->is('/logout')
]
];
}
$results = $this->navigation->getCore()->getHooks()->applyFilter(strtoupper(sprintf('%s_MANAGEMENT', $this->id)), $hardcoded);
usort($results, function ($a, $b) {
if($a->{'order'} === $b->{'order'}) {
return 0;
}
return ($a->{'order'} < $b->{'order'}) ? -1 : 1;
});
usort($results, function($a, $b) {
if(empty($a->order)) {
$a->order = 0;
}
if(empty($b->order)) {
$b->order = 999;
}
return $a->order > $b->order;
});
return $results;
}
}
?>