rewrite for cleaner dev aber geht noch nicht
All checks were successful
Build and push / Pulling repo on server (push) Successful in 2s

This commit is contained in:
2023-10-21 13:04:53 +02:00
parent 47543076bb
commit 3bb9f4ea76
18 changed files with 43 additions and 44 deletions

2
web/inc/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vendor
config.inc.php

View File

@ -0,0 +1,120 @@
<?php
/**
* Page is the Controller Class
* which should be extended by any page
*
* @author Christian Haschek
*/
class Page
{
protected $_controller;
protected $_action;
protected $_template;
public $variables;
public $params;
public $render;
public $menu_text;
public $menu_image;
public $menu_priority;
public $submenu;
private $r;
function __construct($controller, $action, $r = 1, $params = [])
{
$this->_controller = $controller;
$this->_action = $action;
$this->render = $r;
$this->submenu = array();
$this->setMenu();
$this->params = $params;
if(is_array($GLOBALS['vars']) && is_array($this->params))
$this->params = array_merge($this->params, $GLOBALS['vars']);
$this->menu_image = '/css/imgs/empty.png';
if($GLOBALS['redis'] !== false)
$this->r = $GLOBALS['redis'];
else
$this->r = false;
}
function setMenu()
{
$this->menu_text = '';
$this->menu_priority = 1;
}
function redirect($url)
{
header("Location: $url");
exit();
}
/**
* override this function to check if a user can use this object
* @return true -> user will be able to access
* @return false -> user will not be able to access and this page won't
* be shown in the menu
*
*/
public function maySeeThisPage()
{
return true;
}
function set($name, $value)
{
$this->variables[$name] = $value;
}
function get($name)
{
return $this->variables[$name];
}
function __destruct()
{
// if($this->render)
// $this->_template->render();
}
function addHelpers($m)
{
$m->addHelper('case', [
'lower' => function($value) { return strtolower((string) $value); },
'upper' => function($value) { return strtoupper((string) $value); }
]);
$m->addHelper('!!', function($value) { return $value . '!!'; });
return $m;
}
function renderPagecontent()
{
$controller_dir = ROOT . DS . 'pages' . DS . $this->_controller . DS;
$partials = [];
if(is_dir($controller_dir.'partials'))
$partials[] = new Mustache_Loader_FilesystemLoader($controller_dir.'partials');
$partials[] = new Mustache_Loader_FilesystemLoader(ROOT.DS.DS.'templates'.DS.'partials');
$m = new Mustache_Engine(array(
'entity_flags' => ENT_QUOTES,
'partials_loader' => new Mustache_Loader_CascadingLoader($partials)
));
$this->variables['translate'] = function($value) { return translate($value); };
$m = $this->addHelpers($m);
if($this->variables['template'] && file_exists(ROOT . DS . 'pages' . DS . $this->_controller . DS . $this->variables['template']))
$template = file_get_contents(ROOT . DS . 'pages' . DS . $this->_controller . DS . $this->variables['template']);
else
$template = file_get_contents(ROOT . DS . 'views' . DS . 'defaultcontainer.mustache');
$pagecontent = $m->render($template, $this->variables);
return $pagecontent;
}
}

5
web/inc/composer.json Normal file
View File

@ -0,0 +1,5 @@
{
"require": {
"mustache/mustache": "^2.14"
}
}

69
web/inc/composer.lock generated Normal file
View File

@ -0,0 +1,69 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "e982fabf51f3a80b1d9fcbcdb4621616",
"packages": [
{
"name": "mustache/mustache",
"version": "v2.14.2",
"source": {
"type": "git",
"url": "https://github.com/bobthecow/mustache.php.git",
"reference": "e62b7c3849d22ec55f3ec425507bf7968193a6cb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/bobthecow/mustache.php/zipball/e62b7c3849d22ec55f3ec425507bf7968193a6cb",
"reference": "e62b7c3849d22ec55f3ec425507bf7968193a6cb",
"shasum": ""
},
"require": {
"php": ">=5.2.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~1.11",
"phpunit/phpunit": "~3.7|~4.0|~5.0"
},
"type": "library",
"autoload": {
"psr-0": {
"Mustache": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Justin Hileman",
"email": "justin@justinhileman.info",
"homepage": "http://justinhileman.com"
}
],
"description": "A Mustache implementation in PHP.",
"homepage": "https://github.com/bobthecow/mustache.php",
"keywords": [
"mustache",
"templating"
],
"support": {
"issues": "https://github.com/bobthecow/mustache.php/issues",
"source": "https://github.com/bobthecow/mustache.php/tree/v2.14.2"
},
"time": "2022-08-23T13:07:01+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.2.0"
}

81
web/inc/core.php Normal file
View File

@ -0,0 +1,81 @@
<?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');
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');
//DB
if(defined('REDIS_SERVER') && REDIS_SERVER !='')
$GLOBALS['redis'] = new Redis();
else
$GLOBALS['redis'] = false;
}
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';
$dispatch = new $componentName('error', $action, true);
} else
$dispatch = new $componentName($component, $action, true, $queryString);
if (method_exists($componentName, $action)) {
$response = call_user_func_array(array($dispatch, $action), $queryString);
} else if (method_exists($componentName, 'catchAll'))
$response = call_user_func_array(array($dispatch, 'catchAll'), array($params));
if(is_string($response))
return $response;
else
return $dispatch->renderPagecontent();
}

View File

@ -0,0 +1,7 @@
<?php
define('REDIS_SERVER', 'ingress');
define('REDIS_PORT', 6379);
define('REDIS_DB', 1);
define('DEV',true);

170
web/inc/helpers.php Normal file
View File

@ -0,0 +1,170 @@
<?php
function sendMail($rcpt,$subject,$markdown)
{
$mail = new PHPMailer();
$pd = new Parsedown();
$html = $pd->text($markdown);
ob_start();
$mail->CharSet ="UTF-8";
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = SMTP_HOST; // Set the SMTP server to send through
$mail->SMTPAuth = (defined('SMTP_AUTH')?SMTP_AUTH:true); // Enable SMTP authentication
$mail->Username = SMTP_USER; // SMTP username
$mail->Password = SMTP_PW; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = (defined('SMTP_PORT')?SMTP_PORT:587); // TCP port to connect to
if(defined('SMTP_EHLO_DOMAIN') && SMTP_EHLO_DOMAIN)
$mail->Hostname = SMTP_EHLO_DOMAIN;
//make sure we use ipv4
$mail->SMTPOptions = [
'socket' => [
'bindto' => "0:0",
],
];
//Recipients
$mail->setFrom(EMAIL_FROM_EMAIL, EMAIL_FROM_NAME);
$mail->addAddress($rcpt); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $markdown;
$mail->send();
$output = ob_get_clean();
addToMailLog($rcpt,$subject,$output);
return $output;
}
// found on https://html-online.com/articles/php-get-ip-cloudflare-proxy/
function getUserIP() {
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP)) { $ip = $client; }
elseif(filter_var($forward, FILTER_VALIDATE_IP)) { $ip = $forward; }
else { $ip = $remote; }
return $ip;
}
// from https://stackoverflow.com/a/834355/1174516
function startsWith( $haystack, $needle ) {
$length = strlen( $needle );
return substr( $haystack, 0, $length ) === $needle;
}
function endsWith( $haystack, $needle ) {
$length = strlen( $needle );
if( !$length ) {
return true;
}
return substr( $haystack, -$length ) === $needle;
}
function is_cli()
{
if ( defined('STDIN') )
return true;
if ( php_sapi_name() === 'cli' )
return true;
if ( array_key_exists('SHELL', $_ENV) )
return true;
if ( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0)
return true;
if ( !array_key_exists('REQUEST_METHOD', $_SERVER) )
return true;
return false;
}
function addToLog($text,$module='general')
{
$fp = fopen(ROOT.DS.'..'.DS.'log'.DS.$module.'.log','a');
fwrite($fp,'['.date("y.m.d H:i").']'.$text.PHP_EOL);
fclose($fp);
}
function addToMailLog($rcpt,$subject,$response)
{
$rcpt_esc = str_replace('@','_at_',$rcpt);
$dir = ROOT.DS.'..'.DS.'log'.DS.'maillog';
if(!is_dir($dir))
mkdir($dir);
$fp = fopen($dir.DS.$rcpt_esc.'.log','a');
fwrite($fp,"========= NEW MAIL ========\n[".date("y.m.d H:i")."] To: $rcpt\nSubject: $subject\n\n$response\n\n");
fclose($fp);
}
function translate($what)
{
$what = trim($what);
return ($GLOBALS['translations'][$what]?:$what);
}
function getFilesOfFolder($dir)
{
return array_diff(scandir($dir), array('.', '..'));
}
function dbNeedsToBeUpgraded()
{
if(DB_TYPE=='sqlite' && !file_exists(ROOT.DS.'..'.DS.'data'.DS.'db.sqlite3'))
return true;
else if(!file_exists(ROOT.DS.'..'.DS.'log'.DS.'db_version'))
return true;
else if($GLOBALS['db_version']<getHighestSQLVersion())
return true;
return false;
}
function getHighestSQLVersion()
{
$dir = ROOT.DS.'..'.DS.'sql'.DS;
$files = array_diff(scandir($dir), array('..', '.'));
$files = array_map(function($e){
return pathinfo($e, PATHINFO_FILENAME);
}, $files);
sort($files);
return end($files);
}
function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}