php 7.4 dev branch

This commit is contained in:
Roman Hergenreder 2020-04-03 15:56:04 +02:00
parent 2636719583
commit a8fc52b42a
48 changed files with 456 additions and 530 deletions

2
.idea/.gitignore vendored Normal file

@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

6
.idea/misc.xml Normal file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
.idea/modules.xml Normal file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/web-base.iml" filepath="$PROJECT_DIR$/.idea/web-base.iml" />
</modules>
</component>
</project>

4
.idea/php.xml Normal file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpProjectSharedConfiguration" php_language_level="7.4" />
</project>

6
.idea/vcs.xml Normal file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

8
.idea/web-base.iml Normal file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -2,6 +2,10 @@
Web-Base is a php framework which provides basic web functionalities. Web-Base is a php framework which provides basic web functionalities.
### Requirements
- PHP >= 7.4
- One of these php extensions: mysqli, postgres
### Current Functionalities: ### Current Functionalities:
- Installation Guide with automatic database setup - Installation Guide with automatic database setup
- REST API - REST API

@ -3,10 +3,11 @@
namespace Api\ApiKey; namespace Api\ApiKey;
use \Api\Request; use \Api\Request;
class Create extends Request { class Create extends Request {
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array()); parent::__construct($user, $externalCall, array());
$this->apiKeyAllowed = false; $this->apiKeyAllowed = false;
$this->loginRequired = true; $this->loginRequired = true;
} }
@ -39,6 +40,4 @@ class Create extends Request {
} }
return $this->success; return $this->success;
} }
}; }
?>

@ -3,12 +3,13 @@
namespace Api\ApiKey; namespace Api\ApiKey;
use \Api\Request; use \Api\Request;
use DateTime;
use \Driver\SQL\Condition\Compare; use \Driver\SQL\Condition\Compare;
class Fetch extends Request { class Fetch extends Request {
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array()); parent::__construct($user, $externalCall, array());
$this->loginRequired = true; $this->loginRequired = true;
} }
@ -31,16 +32,20 @@ class Fetch extends Request {
if($this->success) { if($this->success) {
$this->result["api_keys"] = array(); $this->result["api_keys"] = array();
foreach($res as $row) { foreach($res as $row) {
try {
$validUntil = (new DateTime($row["valid_until"]))->getTimestamp();
} catch (\Exception $e) {
$validUntil = $row["valid_until"];
}
$this->result["api_keys"][] = array( $this->result["api_keys"][] = array(
"uid" => $row["uid"], "uid" => $row["uid"],
"api_key" => $row["api_key"], "api_key" => $row["api_key"],
"valid_until" => (new \DateTime($row["valid_until"]))->getTimestamp(), "valid_until" => $validUntil,
); );
} }
} }
return $this->success; return $this->success;
} }
}; }
?>

@ -8,8 +8,8 @@ use \Driver\SQL\Condition\Compare;
class Refresh extends Request { class Refresh extends Request {
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array( parent::__construct($user, $externalCall, array(
"id" => new Parameter("id", Parameter::TYPE_INT), "id" => new Parameter("id", Parameter::TYPE_INT),
)); ));
$this->loginRequired = true; $this->loginRequired = true;
@ -62,6 +62,4 @@ class Refresh extends Request {
return $this->success; return $this->success;
} }
}; }
?>

@ -8,8 +8,8 @@ use \Driver\SQL\Condition\Compare;
class Revoke extends Request { class Revoke extends Request {
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array( parent::__construct($user, $externalCall, array(
"id" => new Parameter("id", Parameter::TYPE_INT), "id" => new Parameter("id", Parameter::TYPE_INT),
)); ));
$this->loginRequired = true; $this->loginRequired = true;
@ -57,6 +57,4 @@ class Revoke extends Request {
return $this->success; return $this->success;
} }
}; }
?>

@ -4,8 +4,8 @@ namespace Api;
class GetLanguages extends Request { class GetLanguages extends Request {
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array()); parent::__construct($user, $externalCall, array());
} }
public function execute($values = array()) { public function execute($values = array()) {
@ -34,6 +34,4 @@ class GetLanguages extends Request {
return $this->success; return $this->success;
} }
}; }
?>

@ -9,8 +9,8 @@ use \Driver\SQL\Condition\Compare;
class Create extends Request { class Create extends Request {
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array( parent::__construct($user, $externalCall, array(
'groupId' => new Parameter('groupId', Parameter::TYPE_INT, true), 'groupId' => new Parameter('groupId', Parameter::TYPE_INT, true),
'userId' => new Parameter('userId', Parameter::TYPE_INT, true), 'userId' => new Parameter('userId', Parameter::TYPE_INT, true),
'title' => new StringType('title', 32), 'title' => new StringType('title', 32),
@ -130,6 +130,4 @@ class Create extends Request {
return $this->success; return $this->success;
} }
}; }
?>

@ -7,10 +7,10 @@ use \Driver\SQL\Condition\Compare;
class Fetch extends Request { class Fetch extends Request {
private $notifications; private array $notifications;
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array()); parent::__construct($user, $externalCall, array());
$this->loginRequired = true; $this->loginRequired = true;
} }
@ -89,6 +89,4 @@ class Fetch extends Request {
return $this->success; return $this->success;
} }
}; }
?>

@ -2,6 +2,8 @@
namespace Api\Parameter; namespace Api\Parameter;
use DateTime;
class Parameter { class Parameter {
const TYPE_INT = 0; const TYPE_INT = 0;
const TYPE_FLOAT = 1; const TYPE_FLOAT = 1;
@ -18,11 +20,11 @@ class Parameter {
const names = array('Integer', 'Float', 'Boolean', 'String', 'Date', 'Time', 'DateTime', 'E-Mail', 'Raw', 'Array'); const names = array('Integer', 'Float', 'Boolean', 'String', 'Date', 'Time', 'DateTime', 'E-Mail', 'Raw', 'Array');
public $name; public string $name;
public $value; public $value;
public $optional; public $optional;
public $type; public int $type;
public $typeName; public string $typeName;
public function __construct($name, $type, $optional = FALSE, $defaultValue = NULL) { public function __construct($name, $type, $optional = FALSE, $defaultValue = NULL) {
$this->name = $name; $this->name = $name;
@ -59,11 +61,11 @@ class Parameter {
return Parameter::TYPE_BOOLEAN; return Parameter::TYPE_BOOLEAN;
else if(is_a($value, 'DateTime')) else if(is_a($value, 'DateTime'))
return Parameter::TYPE_DATE_TIME; return Parameter::TYPE_DATE_TIME;
else if(($d = \DateTime::createFromFormat('Y-m-d', $value)) && $d->format('Y-m-d') === $value) else if(($d = DateTime::createFromFormat('Y-m-d', $value)) && $d->format('Y-m-d') === $value)
return Parameter::TYPE_DATE; return Parameter::TYPE_DATE;
else if(($d = \DateTime::createFromFormat('H:i:s', $value)) && $d->format('H:i:s') === $value) else if(($d = DateTime::createFromFormat('H:i:s', $value)) && $d->format('H:i:s') === $value)
return Parameter::TYPE_TIME; return Parameter::TYPE_TIME;
else if(($d = \DateTime::createFromFormat('Y-m-d H:i:s', $value)) && $d->format('Y-m-d H:i:s') === $value) else if(($d = DateTime::createFromFormat('Y-m-d H:i:s', $value)) && $d->format('Y-m-d H:i:s') === $value)
return Parameter::TYPE_DATE_TIME; return Parameter::TYPE_DATE_TIME;
else if (filter_var($value, FILTER_VALIDATE_EMAIL)) else if (filter_var($value, FILTER_VALIDATE_EMAIL))
return Parameter::TYPE_EMAIL; return Parameter::TYPE_EMAIL;
@ -157,5 +159,3 @@ class Parameter {
} }
} }
} }
?>

@ -4,10 +4,10 @@ namespace Api\Parameter;
class StringType extends Parameter { class StringType extends Parameter {
public $maxLength; public int $maxLength;
public function __construct($name, $maxLength = -1, $optional = FALSE, $defaultValue = NULL) { public function __construct($name, $maxLength = -1, $optional = FALSE, $defaultValue = NULL) {
parent::__construct($name, Parameter::TYPE_STRING, $optional, $defaultValue);
$this->maxLength = $maxLength; $this->maxLength = $maxLength;
parent::__construct($name, Parameter::TYPE_STRING, $optional, $defaultValue);
} }
public function parseParam($value) { public function parseParam($value) {
@ -39,5 +39,3 @@ class StringType extends Parameter {
return $str; return $str;
} }
} }
?>

@ -2,30 +2,32 @@
namespace Api; namespace Api;
use Objects\User;
class Request { class Request {
protected $user; protected User $user;
protected $params; protected array $params;
protected $lastError; protected string $lastError;
protected $result; protected array $result;
protected $success; protected bool $success;
protected $isPublic; protected bool $isPublic;
protected $loginRequired; protected bool $loginRequired;
protected $variableParamCount; protected bool $variableParamCount;
protected $isDisabled; protected bool $isDisabled;
protected $apiKeyAllowed; protected bool $apiKeyAllowed;
private $aDefaultParams; private array $aDefaultParams;
private $allowedMethods; private array $allowedMethods;
private $externCall; private bool $externCall;
public function __construct($user, $externCall = false, $params = array()) { public function __construct(User $user, bool $externalCall = false, array $params = array()) {
$this->user = $user; $this->user = $user;
$this->aDefaultParams = $params; $this->aDefaultParams = $params;
$this->lastError = ''; $this->lastError = '';
$this->success = false; $this->success = false;
$this->result = array(); $this->result = array();
$this->externCall = $externCall; $this->externCall = $externalCall;
$this->isPublic = true; $this->isPublic = true;
$this->isDisabled = false; $this->isDisabled = false;
$this->loginRequired = false; $this->loginRequired = false;
@ -40,19 +42,6 @@ class Request {
} }
} }
public function getParamsString() {
$str = "";
$count = count($this->params);
$i = 0;
foreach($this->params as $param) {
$str .= $param->toString();
if($i < $count - 1) $str .= ", ";
$i++;
}
return "($str)";
}
public function parseParams($values) { public function parseParams($values) {
foreach($this->params as $name => $param) { foreach($this->params as $name => $param) {
$value = (isset($values[$name]) ? $values[$name] : NULL); $value = (isset($values[$name]) ? $values[$name] : NULL);
@ -149,47 +138,24 @@ class Request {
return true; return true;
} }
protected function isValidString($str, $regex) {
return preg_replace($regex, "", $str) === $str;
}
protected function createError($err) { protected function createError($err) {
$this->success = false; $this->success = false;
$this->lastError = $err; $this->lastError = $err;
return false; return false;
} }
//
// public static function callDirectly($class, $db) {
// header('Content-Type: application/json');
// require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/api/objects/User.php';
// require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/sql.php';
// require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/conf/sql.php';
//
// $sql = connectSQL(getSqlData($db));
// $user = new CUser($sql);
// $request = new $class($user, true);
// $request->execute();
// $sql->close();
// $user->sendCookies();
// return $request->getJsonResult();
// }
protected function getParam($name) { return isset($this->params[$name]) ? $this->params[$name]->value : NULL; } protected function getParam($name) { return isset($this->params[$name]) ? $this->params[$name]->value : NULL; }
public function isPublic() { return $this->isPublic; } public function isPublic() { return $this->isPublic; }
public function getDescription() { return ''; }
public function getSection() { return 'Default'; }
public function getLastError() { return $this->lastError; } public function getLastError() { return $this->lastError; }
public function getResult() { return $this->result; } public function getResult() { return $this->result; }
public function success() { return $this->success; } public function success() { return $this->success; }
public function loginRequired() { return $this->loginRequired; } public function loginRequired() { return $this->loginRequired; }
public function isExternCall() { return $this->externCall; } public function isExternalCall() { return $this->externCall; }
public function getJsonResult() { public function getJsonResult() {
$this->result['success'] = $this->success; $this->result['success'] = $this->success;
$this->result['msg'] = $this->lastError; $this->result['msg'] = $this->lastError;
return json_encode($this->result); return json_encode($this->result);
} }
}; }
?>

@ -3,11 +3,13 @@
namespace Api; namespace Api;
use Api\Parameter\Parameter; use Api\Parameter\Parameter;
use Api\Parameter\StringType; use Api\Parameter\StringType;
use External\PHPMailer\Exception;
use External\PHPMailer\PHPMailer;
class SendMail extends Request { class SendMail extends Request {
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array( parent::__construct($user, $externalCall, array(
'from' => new Parameter('from', Parameter::TYPE_EMAIL), 'from' => new Parameter('from', Parameter::TYPE_EMAIL),
'to' => new Parameter('to', Parameter::TYPE_EMAIL), 'to' => new Parameter('to', Parameter::TYPE_EMAIL),
'subject' => new StringType('subject', -1), 'subject' => new StringType('subject', -1),
@ -23,8 +25,9 @@ class SendMail extends Request {
return false; return false;
} }
try {
$mailConfig = $this->user->getConfiguration()->getMail(); $mailConfig = $this->user->getConfiguration()->getMail();
$mail = new \External\PHPMailer\PHPMailer; $mail = new PHPMailer;
$mail->IsSMTP(); $mail->IsSMTP();
$mail->setFrom($this->getParam('from'), $this->getParam('fromName')); $mail->setFrom($this->getParam('from'), $this->getParam('fromName'));
$mail->addAddress($this->getParam('to')); $mail->addAddress($this->getParam('to'));
@ -47,12 +50,14 @@ class SendMail extends Request {
$this->success = @$mail->Send(); $this->success = @$mail->Send();
if (!$this->success) { if (!$this->success) {
$this->lastError = 'Error sending Mail: ' . $mail->ErrorInfo; $this->lastError = "Error sending Mail: $mail->ErrorInfo";
error_log("sendMail() failed: " . $mail->ErrorInfo); error_log("sendMail() failed: $mail->ErrorInfo");
}
} catch (Exception $e) {
$this->success = false;
$this->lastError = "Error sending Mail: $e";
} }
return $this->success; return $this->success;
} }
}; }
?>

@ -6,13 +6,14 @@ use Api\Parameter\Parameter;
use Api\Parameter\StringType; use Api\Parameter\StringType;
use Driver\SQL\Condition\CondOr; use Driver\SQL\Condition\CondOr;
use Driver\SQL\Condition\Compare; use Driver\SQL\Condition\Compare;
use Objects\Language;
class SetLanguage extends Request { class SetLanguage extends Request {
private $language; private Language $language;
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array( parent::__construct($user, $externalCall, array(
'langId' => new Parameter('langId', Parameter::TYPE_INT, true, NULL), 'langId' => new Parameter('langId', Parameter::TYPE_INT, true, NULL),
'langCode' => new StringType('langCode', 5, true, NULL), 'langCode' => new StringType('langCode', 5, true, NULL),
)); ));
@ -40,7 +41,7 @@ class SetLanguage extends Request {
return $this->createError(L("This Language does not exist")); return $this->createError(L("This Language does not exist"));
} else { } else {
$row = $res[0]; $row = $res[0];
$this->language = \Objects\Language::newInstance($row['uid'], $row['code'], $row['name']); $this->language = Language::newInstance($row['uid'], $row['code'], $row['name']);
if(!$this->language) { if(!$this->language) {
return $this->createError(L("Error while loading language")); return $this->createError(L("Error while loading language"));
} }
@ -75,9 +76,7 @@ class SetLanguage extends Request {
$this->updateLanguage(); $this->updateLanguage();
} }
$this->user->setLangauge($this->language); $this->user->setLanguage($this->language);
return $this->success; return $this->success;
} }
}; }
?>

@ -9,10 +9,10 @@ use \Driver\SQL\Condition\Compare;
class Login extends Request { class Login extends Request {
private $startedAt; private int $startedAt;
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall, array( parent::__construct($user, $externalCall, array(
'username' => new StringType('username', 32), 'username' => new StringType('username', 32),
'password' => new StringType('password'), 'password' => new StringType('password'),
'stayLoggedIn' => new Parameter('stayLoggedIn', Parameter::TYPE_BOOLEAN, true, true) 'stayLoggedIn' => new Parameter('stayLoggedIn', Parameter::TYPE_BOOLEAN, true, true)
@ -77,6 +77,4 @@ class Login extends Request {
return $this->success; return $this->success;
} }
}; }
?>

@ -6,8 +6,8 @@ use \Api\Request;
class Logout extends Request { class Logout extends Request {
public function __construct($user, $externCall = false) { public function __construct($user, $externalCall = false) {
parent::__construct($user, $externCall); parent::__construct($user, $externalCall);
$this->loginRequired = true; $this->loginRequired = true;
$this->apiKeyAllowed = false; $this->apiKeyAllowed = false;
} }
@ -21,6 +21,4 @@ class Logout extends Request {
$this->lastError = $this->user->getSQL()->getLastError(); $this->lastError = $this->user->getSQL()->getLastError();
return $this->success; return $this->success;
} }
}; }
?>

@ -2,11 +2,14 @@
namespace Configuration; namespace Configuration;
use Error;
use Objects\ConnectionData;
class Configuration { class Configuration {
private $database; private ?ConnectionData $database;
private $mail; private ?ConnectionData $mail;
private $jwt; private ?KeyData $jwt;
function __construct() { function __construct() {
} }
@ -34,7 +37,7 @@ class Configuration {
} }
return $success; return $success;
} catch(\Error $e) { } catch(Error $e) {
die($e); die($e);
} }
} }
@ -48,7 +51,7 @@ class Configuration {
return file_exists($path); return file_exists($path);
} }
public function create($className, $data) { public function create(string $className, $data) {
$path = getClassPath("\\Configuration\\$className"); $path = getClassPath("\\Configuration\\$className");
if($data) { if($data) {
@ -59,22 +62,15 @@ class Configuration {
namespace Configuration; namespace Configuration;
class $className { class $className extends KeyData {
private \$key;
public function __construct() { public function __construct() {
\$this->key = '$key'; parent::__construct('$key');
} }
public function getKey() { }", false
return \$this->key;
}
}
?>", false
); );
} else { } else if($data instanceof ConnectionData) {
$superClass = get_class($data); $superClass = get_class($data);
$host = addslashes($data->getHost()); $host = addslashes($data->getHost());
$port = intval($data->getPort()); $port = intval($data->getPort());
@ -102,18 +98,17 @@ class Configuration {
?>", false ?>", false
); );
} else {
return false;
} }
} else { } else {
$code = intendCode( $code = "<?php";
"<?php
?>", false);
} }
return @file_put_contents($path, $code); return @file_put_contents($path, $code);
} }
public function delete($className) { public function delete(string $className) {
$path = getClassPath("\\Configuration\\$className"); $path = getClassPath("\\Configuration\\$className");
if(file_exists($path)) { if(file_exists($path)) {
return unlink($path); return unlink($path);
@ -121,6 +116,4 @@ class Configuration {
return true; return true;
} }
}; }
?>

@ -2,16 +2,13 @@
namespace Configuration; namespace Configuration;
use \Driver\SQL\Query\CreateTable; use Driver\SQL\SQL;
use \Driver\SQL\Query\Insert;
use \Driver\SQL\Column\Column;
use \Driver\SQL\Strategy\UpdateStrategy;
use \Driver\SQL\Strategy\SetNullStrategy; use \Driver\SQL\Strategy\SetNullStrategy;
use \Driver\SQL\Strategy\CascadeStrategy; use \Driver\SQL\Strategy\CascadeStrategy;
class CreateDatabase { class CreateDatabase {
public static function createQueries($sql) { public static function createQueries(SQL $sql) {
$queries = array(); $queries = array();
// Language // Language
@ -111,5 +108,3 @@ class CreateDatabase {
return $queries; return $queries;
} }
} }
?>

@ -0,0 +1,17 @@
<?php
namespace Configuration;
class KeyData {
protected string $key;
public function __construct(string $key) {
$this->key = $key;
}
public function getKey() {
return $this->key;
}
}

@ -1,16 +1,28 @@
<?php <?php
namespace Documents { namespace Documents {
class Admin extends \Elements\Document {
use Documents\Admin\AdminBody;
use Documents\Admin\AdminHead;
use Elements\Document;
class Admin extends Document {
public function __construct($user) { public function __construct($user) {
parent::__construct($user, Admin\Head::class, Admin\Body::class); parent::__construct($user, AdminHead::class, AdminBody::class);
} }
} }
} }
namespace Documents\Admin { namespace Documents\Admin {
class Head extends \Elements\Head { use Elements\Body;
use Elements\Head;
use Elements\Link;
use Elements\Script;
use Views\Admin;
use Views\Login;
class AdminHead extends Head {
public function __construct($document) { public function __construct($document) {
parent::__construct($document); parent::__construct($document);
@ -20,10 +32,10 @@ namespace Documents\Admin {
$this->loadJQuery(); $this->loadJQuery();
$this->loadBootstrap(); $this->loadBootstrap();
$this->loadFontawesome(); $this->loadFontawesome();
$this->addJS(\Elements\Script::CORE); $this->addJS(Script::CORE);
$this->addCSS(\Elements\Link::CORE); $this->addCSS(Link::CORE);
$this->addJS(\Elements\Script::ADMIN); $this->addJS(Script::ADMIN);
$this->addCSS(\Elements\Link::ADMIN); $this->addCSS(Link::ADMIN);
} }
protected function initMetas() { protected function initMetas() {
@ -45,7 +57,7 @@ namespace Documents\Admin {
} }
} }
class Body extends \Elements\Body { class AdminBody extends Body {
public function __construct($document) { public function __construct($document) {
parent::__construct($document); parent::__construct($document);
@ -56,14 +68,12 @@ namespace Documents\Admin {
$document = $this->getDocument(); $document = $this->getDocument();
if(!$document->getUser()->isLoggedIn()) { if(!$document->getUser()->isLoggedIn()) {
$html .= new \Views\Login($document); $html .= new Login($document);
} else { } else {
$html .= new \Views\Admin($document); $html .= new Admin($document);
} }
return $html; return $html;
} }
} }
} }
?>

@ -1,16 +1,24 @@
<?php <?php
namespace Documents { namespace Documents {
class Document404 extends \Elements\Document {
use Documents\Document404\Body404;
use Documents\Document404\Head404;
use Elements\Document;
class Document404 extends Document {
public function __construct($user) { public function __construct($user) {
parent::__construct($user, Document404\Head404::class, Document404\Body404::class); parent::__construct($user, Head404::class, Body404::class);
} }
} }
} }
namespace Documents\Document404 { namespace Documents\Document404 {
class Head404 extends \Elements\Head { use Elements\Body;
use Elements\Head;
class Head404 extends Head {
public function __construct($document) { public function __construct($document) {
parent::__construct($document); parent::__construct($document);
@ -43,7 +51,7 @@ namespace Documents\Document404 {
} }
} }
class Body404 extends \Elements\Body { class Body404 extends Body {
public function __construct($document) { public function __construct($document) {
parent::__construct($document); parent::__construct($document);
@ -56,5 +64,3 @@ namespace Documents\Document404 {
} }
} }
} }
?>

@ -1,9 +1,14 @@
<?php <?php
namespace Documents { namespace Documents {
class Install extends \Elements\Document {
use Documents\Install\InstallBody;
use Documents\Install\InstallHead;
use Elements\Document;
class Install extends Document {
public function __construct($user) { public function __construct($user) {
parent::__construct($user, Install\Head::class, Install\Body::class); parent::__construct($user, InstallHead::class, InstallBody::class);
$this->databaseRequired = false; $this->databaseRequired = false;
} }
} }
@ -11,7 +16,18 @@ namespace Documents {
namespace Documents\Install { namespace Documents\Install {
class Head extends \Elements\Head { use Api\Notifications\Create;
use Configuration\CreateDatabase;
use Driver\SQL\SQL;
use Elements\Body;
use Elements\Head;
use Elements\Link;
use Elements\Script;
use External\PHPMailer\Exception;
use External\PHPMailer\PHPMailer;
use Objects\ConnectionData;
class InstallHead extends Head {
public function __construct($document) { public function __construct($document) {
parent::__construct($document); parent::__construct($document);
@ -21,9 +37,9 @@ namespace Documents\Install {
$this->loadJQuery(); $this->loadJQuery();
$this->loadBootstrap(); $this->loadBootstrap();
$this->loadFontawesome(); $this->loadFontawesome();
$this->addJS(\Elements\Script::CORE); $this->addJS(Script::CORE);
$this->addCSS(\Elements\Link::CORE); $this->addCSS(Link::CORE);
$this->addJS(\Elements\Script::INSTALL); $this->addJS(Script::INSTALL);
} }
protected function initMetas() { protected function initMetas() {
@ -46,27 +62,31 @@ namespace Documents\Install {
} }
class Body extends \Elements\Body { class InstallBody extends Body {
// Status enum // Status enum
const NOT_STARTED = 0; const NOT_STARTED = 0;
const PENDING = 1; const PENDING = 1;
const SUCCESFULL = 2; const SUCCESSFUL = 2;
const ERROR = 3; const ERROR = 3;
// Step enum // Step enum
const CHECKING_REQUIRMENTS = 1; const CHECKING_REQUIREMENTS = 1;
const DATABASE_CONFIGURATION = 2; const DATABASE_CONFIGURATION = 2;
const CREATE_USER = 3; const CREATE_USER = 3;
const ADD_MAIL_SERVICE = 4; const ADD_MAIL_SERVICE = 4;
const FINISH_INSTALLATION = 5; const FINISH_INSTALLATION = 5;
// //
private $errorString; private string $errorString;
private int $currentStep;
private array $steps;
function __construct($document) { function __construct($document) {
parent::__construct($document); parent::__construct($document);
$this->errorString = ""; $this->errorString = "";
$this->currentStep = InstallBody::CHECKING_REQUIREMENTS;
$this->steps = array();
} }
private function getParameter($name) { private function getParameter($name) {
@ -80,7 +100,7 @@ namespace Documents\Install {
private function getCurrentStep() { private function getCurrentStep() {
if(!$this->checkRequirements()["success"]) { if(!$this->checkRequirements()["success"]) {
return self::CHECKING_REQUIRMENTS; return self::CHECKING_REQUIREMENTS;
} }
$user = $this->getDocument()->getUser(); $user = $this->getDocument()->getUser();
@ -109,7 +129,7 @@ namespace Documents\Install {
if(!$config->isFilePresent("JWT") && !$config->create("JWT", generateRandomString(32))) { if(!$config->isFilePresent("JWT") && !$config->create("JWT", generateRandomString(32))) {
$this->errorString = "Unable to create jwt file"; $this->errorString = "Unable to create jwt file";
} else { } else {
$req = new \Api\Notifications\Create($user); $req = new Create($user);
$success = $req->execute(array( $success = $req->execute(array(
"title" => "Welcome", "title" => "Welcome",
"message" => "Your Web-base was successfully installed. Check out the admin dashboard. Have fun!", "message" => "Your Web-base was successfully installed. Check out the admin dashboard. Have fun!",
@ -145,8 +165,8 @@ namespace Documents\Install {
} }
} }
if(version_compare(PHP_VERSION, '7.1', '<')) { if(version_compare(PHP_VERSION, '7.4', '<')) {
$failedRequirements[] = "PHP Version <b>>= 7.1</b> is required. Got: <b>" . PHP_VERSION . "</b>"; $failedRequirements[] = "PHP Version <b>>= 7.4</b> is required. Got: <b>" . PHP_VERSION . "</b>";
$success = false; $success = false;
} }
@ -213,14 +233,14 @@ namespace Documents\Install {
$msg = "Unsupported database type. Must be one of: " . implode(", ", $supportedTypes); $msg = "Unsupported database type. Must be one of: " . implode(", ", $supportedTypes);
$success = false; $success = false;
} else { } else {
$connectionData = new \Objects\ConnectionData($host, $port, $username, $password); $connectionData = new ConnectionData($host, $port, $username, $password);
$connectionData->setProperty('database', $database); $connectionData->setProperty('database', $database);
$connectionData->setProperty('encoding', $encoding); $connectionData->setProperty('encoding', $encoding);
$connectionData->setProperty('type', $type); $connectionData->setProperty('type', $type);
$sql = \Driver\SQL\SQL::createConnection($connectionData); $sql = SQL::createConnection($connectionData);
$success = false; $success = false;
if(!($sql instanceof \Driver\SQL\SQL)) { if(is_string($sql)) {
$msg = "Error connecting to database: " . str($sql); $msg = "Error connecting to database: $sql";
} else if(!$sql->isConnected()) { } else if(!$sql->isConnected()) {
if (!$sql->checkRequirements()["success"]) { if (!$sql->checkRequirements()["success"]) {
$driverName = $sql->getDriverName(); $driverName = $sql->getDriverName();
@ -234,7 +254,7 @@ namespace Documents\Install {
$msg = ""; $msg = "";
$success = true; $success = true;
$queries = \Configuration\CreateDatabase::createQueries($sql); $queries = CreateDatabase::createQueries($sql);
foreach($queries as $query) { foreach($queries as $query) {
if (!($res = $query->execute())) { if (!($res = $query->execute())) {
$msg = "Error creating tables: " . $sql->getLastError(); $msg = "Error creating tables: " . $sql->getLastError();
@ -375,7 +395,7 @@ namespace Documents\Install {
} else { } else {
$success = false; $success = false;
$mail = new \External\PHPMailer\PHPMailer(true); $mail = new PHPMailer(true);
$mail->IsSMTP(); $mail->IsSMTP();
$mail->SMTPAuth = true; $mail->SMTPAuth = true;
$mail->Username = $username; $mail->Username = $username;
@ -395,12 +415,12 @@ namespace Documents\Install {
$msg = ""; $msg = "";
$mail->smtpClose(); $mail->smtpClose();
} }
} catch(\External\PHPMailer\Exception $error) { } catch(Exception $error) {
$msg = "Could not connect to SMTP Server: " . $error->errorMessage(); $msg = "Could not connect to SMTP Server: " . $error->errorMessage();
} }
if($success) { if($success) {
$connectionData = new \Objects\ConnectionData($address, $port, $username, $password); $connectionData = new ConnectionData($address, $port, $username, $password);
if(!$user->getConfiguration()->create("Mail", $connectionData)) { if(!$user->getConfiguration()->create("Mail", $connectionData)) {
$success = false; $success = false;
$msg = "Unable to create file"; $msg = "Unable to create file";
@ -416,7 +436,7 @@ namespace Documents\Install {
switch($this->currentStep) { switch($this->currentStep) {
case self::CHECKING_REQUIRMENTS: case self::CHECKING_REQUIREMENTS:
return $this->checkRequirements(); return $this->checkRequirements();
case self::DATABASE_CONFIGURATION: case self::DATABASE_CONFIGURATION:
@ -451,7 +471,7 @@ namespace Documents\Install {
$statusColor = "muted"; $statusColor = "muted";
break; break;
case self::SUCCESFULL: case self::SUCCESSFUL:
$statusIcon = '<i class="fas fa-check-circle"></i>'; $statusIcon = '<i class="fas fa-check-circle"></i>';
$statusText = "Successfull"; $statusText = "Successfull";
$statusColor = "success"; $statusColor = "success";
@ -552,7 +572,7 @@ namespace Documents\Install {
private function createProgessMainview() { private function createProgessMainview() {
$views = array( $views = array(
self::CHECKING_REQUIRMENTS => array( self::CHECKING_REQUIREMENTS => array(
"title" => "Application Requirements", "title" => "Application Requirements",
"progressText" => "Checking requirements, please wait a moment…" "progressText" => "Checking requirements, please wait a moment…"
), ),
@ -661,7 +681,7 @@ namespace Documents\Install {
); );
if($this->currentStep != self::FINISH_INSTALLATION) { if($this->currentStep != self::FINISH_INSTALLATION) {
if ($this->currentStep == self::CHECKING_REQUIRMENTS) { if ($this->currentStep == self::CHECKING_REQUIREMENTS) {
$buttons[] = array("title" => "Retry", "type" => "success", "id" => "btnRetry", "float" => "right"); $buttons[] = array("title" => "Retry", "type" => "success", "id" => "btnRetry", "float" => "right");
} else { } else {
$buttons[] = array("title" => "Submit", "type" => "success", "id" => "btnSubmit", "float" => "right"); $buttons[] = array("title" => "Submit", "type" => "success", "id" => "btnSubmit", "float" => "right");
@ -706,7 +726,7 @@ namespace Documents\Install {
$html = parent::getCode(); $html = parent::getCode();
$this->steps = array( $this->steps = array(
self::CHECKING_REQUIRMENTS => array( self::CHECKING_REQUIREMENTS => array(
"title" => "Checking requirements", "title" => "Checking requirements",
"status" => self::ERROR "status" => self::ERROR
), ),
@ -731,12 +751,12 @@ namespace Documents\Install {
$this->currentStep = $this->getCurrentStep(); $this->currentStep = $this->getCurrentStep();
// set status // set status
for($step = self::CHECKING_REQUIRMENTS; $step < $this->currentStep; $step++) { for($step = self::CHECKING_REQUIREMENTS; $step < $this->currentStep; $step++) {
$this->steps[$step]["status"] = self::SUCCESFULL; $this->steps[$step]["status"] = self::SUCCESSFUL;
} }
if($this->currentStep == self::FINISH_INSTALLATION) { if($this->currentStep == self::FINISH_INSTALLATION) {
$this->steps[$this->currentStep]["status"] = self::SUCCESFULL; $this->steps[$this->currentStep]["status"] = self::SUCCESSFUL;
} }
// POST // POST
@ -783,7 +803,4 @@ namespace Documents\Install {
} }
} }
} }
?>

@ -2,9 +2,10 @@
namespace Elements; namespace Elements;
abstract class Body extends \View { use View;
abstract class Body extends View {
public function __construct($document) { public function __construct($document) {
parent::__construct($document); parent::__construct($document);
} }
}; }
?>

@ -2,14 +2,16 @@
namespace Elements; namespace Elements;
use Objects\User;
abstract class Document { abstract class Document {
protected $head; protected Head $head;
protected $body; protected Body $body;
protected $user; protected User $user;
protected $databaseRequired; protected bool $databaseRequired;
public function __construct($user, $headClass, $bodyClass) { public function __construct(User $user, $headClass, $bodyClass) {
$this->head = new $headClass($this); $this->head = new $headClass($this);
$this->body = new $bodyClass($this); $this->body = new $bodyClass($this);
$this->user = $user; $this->user = $user;
@ -29,30 +31,6 @@ abstract class Document {
return new $documentClass($user); return new $documentClass($user);
} }
public static function createDocument($class) {
// TODO: check instance, configuration, ..
require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/sql.php';
// require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/conf/config.php';
// require_once realpath($_SERVER['DOCUMENT_ROOT']) . "/php/pages/$file.php";
require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/api/objects/User.php';
$connectionData = getSqlData($database);
$sql = connectSQL($connectionData);
if(!$sql->isConnected()) {
http_response_code(500);
die('Internal Database error');
}
$user = new CUser($sql);
$document = new $class($user);
$code = $document->getCode();
$document->sendHeaders();
$user->sendCookies();
die($code);
}
function getCode() { function getCode() {
if ($this->databaseRequired) { if ($this->databaseRequired) {
@ -75,6 +53,4 @@ abstract class Document {
return $html; return $html;
} }
}; }
?>

@ -2,15 +2,17 @@
namespace Elements; namespace Elements;
abstract class Head extends \View { use View;
protected $sources; abstract class Head extends View {
protected $title;
protected $metas; protected array $sources;
protected $rawFields; protected string $title;
protected $keywords; protected array $metas;
protected $description; protected array $rawFields;
protected $baseUrl; protected array $keywords;
protected string $description;
protected string $baseUrl;
function __construct($document) { function __construct($document) {
parent::__construct($document); parent::__construct($document);
@ -54,19 +56,6 @@ abstract class Head extends \View {
$this->addCSS(Link::FONTAWESOME); $this->addCSS(Link::FONTAWESOME);
} }
public function loadSyntaxHighlighting() {
$this->addJS(Script::HIGHLIGHT);
$this->addJSCode(Script::HIGHLIGHT_JS_LOADER);
$this->addCSS(Link::HIGHLIGHT);
$this->addCSS(Link::HIGHLIGHT_THEME);
}
public function loadJQueryTerminal($unixFormatting = true) {
$this->addJS(Script::JQUERY_TERMINAL);
if($unixFormatting) $this->addJS(Script::JQUERY_TERMINAL_UNIX);
$this->addCSS(Link::JQUERY_TERMINAL);
}
public function loadGoogleRecaptcha($siteKey) { public function loadGoogleRecaptcha($siteKey) {
$this->addJS("https://www.google.com/recaptcha/api.js?render=$siteKey"); $this->addJS("https://www.google.com/recaptcha/api.js?render=$siteKey");
} }
@ -80,11 +69,6 @@ abstract class Head extends \View {
$this->addJS(Script::BOOTSTRAP); $this->addJS(Script::BOOTSTRAP);
} }
public function loadChartJS() {
$this->addJS(Script::MOMENT);
$this->addJS(Script::CHART);
}
public function getCode() { public function getCode() {
$header = "<head>"; $header = "<head>";
@ -123,4 +107,3 @@ abstract class Head extends \View {
return $header; return $header;
} }
} }
?>

@ -2,7 +2,9 @@
namespace Elements; namespace Elements;
class Link extends Source { use View;
class Link extends View {
const STYLESHEET = "stylesheet"; const STYLESHEET = "stylesheet";
const MIME_TEXT_CSS = "text/css"; const MIME_TEXT_CSS = "text/css";
@ -23,20 +25,18 @@ class Link extends Source {
// const REVEALJS_THEME_MOON = "/css/reveal_moon.css"; // const REVEALJS_THEME_MOON = "/css/reveal_moon.css";
// const REVEALJS_THEME_BLACK = "/css/reveal_black.css"; // const REVEALJS_THEME_BLACK = "/css/reveal_black.css";
private $type; private string $type;
private $rel; private string $rel;
private string $href;
function __construct($rel, $href, $type = "") { function __construct($rel, $href, $type = "") {
parent::__construct('link', $href); $this->href = $href;
$this->type = $type; $this->type = $type;
$this->rel = $rel; $this->rel = $rel;
} }
function getCode() { function getCode() {
$type = (empty($this->type) ? "" : " type=\"$this->type\""); $type = (empty($this->type) ? "" : " type=\"$this->type\"");
$link = "<link rel=\"$this->rel\" href=\"$this->url\" $type/>"; return "<link rel=\"$this->rel\" href=\"$this->href\"$type/>";
return $link;
} }
} }
?>

@ -2,7 +2,7 @@
namespace Elements; namespace Elements;
class Script extends Source { class Script extends \View {
const MIME_TEXT_JAVASCRIPT = "text/javascript"; const MIME_TEXT_JAVASCRIPT = "text/javascript";
@ -32,11 +32,11 @@ class Script extends Source {
const HIGHLIGHT_JS_LOADER = "\$(document).ready(function(){\$('code').each(function(i, block) { hljs.highlightBlock(block); }); })"; const HIGHLIGHT_JS_LOADER = "\$(document).ready(function(){\$('code').each(function(i, block) { hljs.highlightBlock(block); }); })";
private $type; private string $type;
private $content; private string $content;
private string $url;
function __construct($type, $src, $content = "") { function __construct($type, $src, $content = "") {
parent::__construct('script', $src);
$this->type = $type; $this->type = $type;
$this->content = $content; $this->content = $content;
} }
@ -49,5 +49,3 @@ class Script extends Source {
return $script; return $script;
} }
} }
?>

@ -1,22 +0,0 @@
<?php
namespace Elements;
class Source extends \View {
protected $sourceType;
protected $url;
public function __construct($sourceType, $url) {
$this->sourceType = $sourceType;
$this->url = $url;
}
public function getCode() {
return "<$sourceType />";
}
public function getUrl() { return $this->url; }
}
?>

@ -2,12 +2,11 @@
namespace Elements; namespace Elements;
class Style extends Source { class Style extends View {
private $style; private string $style;
function __construct($style) { function __construct($style) {
parent::__construct('style', '');
$this->style = $style; $this->style = $style;
} }
@ -15,5 +14,3 @@ class Style extends Source {
return "<style>$this->style</style>"; return "<style>$this->style</style>";
} }
} }
?>

@ -6,9 +6,6 @@ abstract class ApiObject implements \JsonSerializable {
public abstract function jsonSerialize(); public abstract function jsonSerialize();
public function __construct() { }
public function __toString() { return json_encode($this); } public function __toString() { return json_encode($this); }
} }
?>

@ -4,11 +4,11 @@ namespace Objects;
class ConnectionData { class ConnectionData {
private $host; private string $host;
private $port; private int $port;
private $login; private string $login;
private $password; private string $password;
private $properties; private array $properties;
public function __construct($host, $port, $login, $password) { public function __construct($host, $port, $login, $password) {
$this->host = $host; $this->host = $host;
@ -32,6 +32,7 @@ class ConnectionData {
} }
$this->properties[$key] = $val; $this->properties[$key] = $val;
return true;
} }
public function getHost() { return $this->host; } public function getHost() { return $this->host; }
@ -39,5 +40,3 @@ class ConnectionData {
public function getLogin() { return $this->login; } public function getLogin() { return $this->login; }
public function getPassword() { return $this->password; } public function getPassword() { return $this->password; }
} }
?>

@ -2,16 +2,18 @@
namespace Objects { namespace Objects {
use LanguageModule;
class Language extends ApiObject { class Language extends ApiObject {
const LANG_CODE_PATTERN = "/^[a-zA-Z]+_[a-zA-Z]+$/"; const LANG_CODE_PATTERN = "/^[a-zA-Z]+_[a-zA-Z]+$/";
private $languageId; private int $languageId;
private $langCode; private string $langCode;
private $langName; private string $langName;
private $modules; private array $modules;
protected $entries; protected array $entries;
public function __construct($languageId, $langCode, $langName) { public function __construct($languageId, $langCode, $langName) {
$this->languageId = $languageId; $this->languageId = $languageId;
@ -29,7 +31,7 @@ namespace Objects {
public function getEntries() { return $this->entries; } public function getEntries() { return $this->entries; }
public function getModules() { return $this->modules; } public function getModules() { return $this->modules; }
public function loadModule($module) { public function loadModule(LanguageModule $module) {
if(!is_object($module)) if(!is_object($module))
$module = new $module; $module = new $module;
@ -100,6 +102,7 @@ namespace Objects {
} }
namespace { namespace {
function L($key) { function L($key) {
if(!array_key_exists('LANGUAGE', $GLOBALS)) if(!array_key_exists('LANGUAGE', $GLOBALS))
return $key; return $key;
@ -132,4 +135,3 @@ namespace {
return $LANGUAGE->getShortCode(); return $LANGUAGE->getShortCode();
} }
} }
?>

@ -2,21 +2,24 @@
namespace Objects; namespace Objects;
use DateTime;
use \Driver\SQL\Condition\Compare; use \Driver\SQL\Condition\Compare;
use Exception;
use External\JWT;
class Session extends ApiObject { class Session extends ApiObject {
const DURATION = 120; const DURATION = 120;
private $sessionId; private ?int $sessionId;
private $user; private User $user;
private $expires; private int $expires;
private $ipAddress; private string $ipAddress;
private $os; private ?string $os;
private $browser; private ?string $browser;
private $stayLoggedIn; private bool $stayLoggedIn;
public function __construct($user, $sessionId) { public function __construct(User $user, ?int $sessionId) {
$this->user = $user; $this->user = $user;
$this->sessionId = $sessionId; $this->sessionId = $sessionId;
$this->stayLoggedIn = true; $this->stayLoggedIn = true;
@ -38,7 +41,7 @@ class Session extends ApiObject {
$userAgent = @get_browser($_SERVER['HTTP_USER_AGENT'], true); $userAgent = @get_browser($_SERVER['HTTP_USER_AGENT'], true);
$this->os = $userAgent['platform'] ?? "Unknown"; $this->os = $userAgent['platform'] ?? "Unknown";
$this->browser = $userAgent['parent'] ?? "Unknown"; $this->browser = $userAgent['parent'] ?? "Unknown";
} catch(\Exception $ex) { } catch(Exception $ex) {
$this->os = "Unknown"; $this->os = "Unknown";
$this->browser = "Unknown"; $this->browser = "Unknown";
} }
@ -59,7 +62,7 @@ class Session extends ApiObject {
$jwt = $this->user->getConfiguration()->getJwt(); $jwt = $this->user->getConfiguration()->getJwt();
if($jwt) { if($jwt) {
$token = array('userId' => $this->user->getId(), 'sessionId' => $this->sessionId); $token = array('userId' => $this->user->getId(), 'sessionId' => $this->sessionId);
$sessionCookie = \External\JWT::encode($token, $jwt->getKey()); $sessionCookie = JWT::encode($token, $jwt->getKey());
$secure = strcmp(getProtocol(), "https") === 0; $secure = strcmp(getProtocol(), "https") === 0;
setcookie('session', $sessionCookie, $this->getExpiresTime(), "/", "", $secure); setcookie('session', $sessionCookie, $this->getExpiresTime(), "/", "", $secure);
} }
@ -94,7 +97,7 @@ class Session extends ApiObject {
$success = $sql $success = $sql
->insert("Session", $columns) ->insert("Session", $columns)
->addRow( ->addRow(
(new \DateTime)->modify("+$hours hour"), (new DateTime())->modify("+$hours hour"),
$this->user->getId(), $this->user->getId(),
$this->ipAddress, $this->ipAddress,
$this->os, $this->os,
@ -113,13 +116,11 @@ class Session extends ApiObject {
} }
public function destroy() { public function destroy() {
$success = $this->user->getSQL()->update("Session") return $this->user->getSQL()->update("Session")
->set("active", false) ->set("active", false)
->where(new Compare("Session.uid", $this->sessionId)) ->where(new Compare("Session.uid", $this->sessionId))
->where(new Compare("Session.user_id", $this->user->getId())) ->where(new Compare("Session.user_id", $this->user->getId()))
->execute(); ->execute();
return $success;
} }
public function update() { public function update() {
@ -127,8 +128,8 @@ class Session extends ApiObject {
$hours = Session::DURATION; $hours = Session::DURATION;
$sql = $this->user->getSQL(); $sql = $this->user->getSQL();
$success = $sql->update("Session") return $sql->update("Session")
->set("Session.expires", (new \DateTime)->modify("+$hours hour")) ->set("Session.expires", (new DateTime())->modify("+$hours hour"))
->set("Session.ipAddress", $this->ipAddress) ->set("Session.ipAddress", $this->ipAddress)
->set("Session.os", $this->os) ->set("Session.os", $this->os)
->set("Session.browser", $this->browser) ->set("Session.browser", $this->browser)
@ -136,9 +137,5 @@ class Session extends ApiObject {
->where(new Compare("Session.uid", $this->sessionId)) ->where(new Compare("Session.uid", $this->sessionId))
->where(new Compare("Session.user_id", $this->user->getId())) ->where(new Compare("Session.user_id", $this->user->getId()))
->execute(); ->execute();
return $success;
} }
} }
?>

@ -2,25 +2,28 @@
namespace Objects; namespace Objects;
use \External\JWT; use Api\SetLanguage;
use Driver\SQL\Column\Column; use Configuration\Configuration;
use Exception;
use External\JWT;
use Driver\SQL\SQL;
use Driver\SQL\Condition\Compare; use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondBool; use Driver\SQL\Condition\CondBool;
class User extends ApiObject { class User extends ApiObject {
private $sql; private ?SQL $sql;
private $configuration; private Configuration $configuration;
private $loggedIn; private bool $loggedIn;
private $session; private ?Session $session;
private $uid; private int $uid;
private $username; private string $username;
private $language; private Language $language;
public function __construct($configuration) { public function __construct($configuration) {
session_start(); session_start();
$this->configuration = $configuration; $this->configuration = $configuration;
$this->setLangauge(Language::DEFAULT_LANGUAGE()); $this->setLanguage(Language::DEFAULT_LANGUAGE());
$this->reset(); $this->reset();
$this->connectDb(); $this->connectDb();
$this->parseCookies(); $this->parseCookies();
@ -35,7 +38,9 @@ class User extends ApiObject {
private function connectDb() { private function connectDb() {
$databaseConf = $this->configuration->getDatabase(); $databaseConf = $this->configuration->getDatabase();
if($databaseConf) { if($databaseConf) {
$this->sql = \Driver\SQL\SQL::createConnection($databaseConf); $this->sql = SQL::createConnection($databaseConf);
} else {
$this->sql = null;
} }
} }
@ -44,7 +49,7 @@ class User extends ApiObject {
public function getUsername() { return $this->username; } public function getUsername() { return $this->username; }
public function getSQL() { return $this->sql; } public function getSQL() { return $this->sql; }
public function getLanguage() { return $this->language; } public function getLanguage() { return $this->language; }
public function setLangauge($language) { $this->language = $language; $language->load(); } public function setLanguage(Language $language) { $this->language = $language; $language->load(); }
public function getSession() { return $this->session; } public function getSession() { return $this->session; }
public function getConfiguration() { return $this->configuration; } public function getConfiguration() { return $this->configuration; }
@ -75,7 +80,7 @@ class User extends ApiObject {
$this->uid = 0; $this->uid = 0;
$this->username = ''; $this->username = '';
$this->loggedIn = false; $this->loggedIn = false;
$this->session = false; $this->session = null;
} }
public function logout() { public function logout() {
@ -90,8 +95,10 @@ class User extends ApiObject {
public function updateLanguage($lang) { public function updateLanguage($lang) {
if($this->sql) { if($this->sql) {
$request = new \Api\SetLanguage($this); $request = new SetLanguage($this);
return $request->execute(array("langCode" => $lang)); return $request->execute(array("langCode" => $lang));
} else {
return false;
} }
} }
@ -130,7 +137,7 @@ class User extends ApiObject {
if($sessionUpdate) $this->session->update(); if($sessionUpdate) $this->session->update();
$this->loggedIn = true; $this->loggedIn = true;
if(!is_null($row['langId'])) { if(!is_null($row['langId'])) {
$this->setLangauge(Language::newInstance($row['langId'], $row['langCode'], $row['langName'])); $this->setLanguage(Language::newInstance($row['langId'], $row['langCode'], $row['langName']));
} }
} }
} }
@ -153,7 +160,7 @@ class User extends ApiObject {
$this->readData($userId, $sessionId); $this->readData($userId, $sessionId);
} }
} }
} catch(\Exception $e) { } catch(Exception $e) {
// ignored // ignored
} }
} }
@ -187,7 +194,7 @@ class User extends ApiObject {
->leftJoin("Language", "User.language_id", "Language.uid") ->leftJoin("Language", "User.language_id", "Language.uid")
->where(new Compare("ApiKey.api_key", $apiKey)) ->where(new Compare("ApiKey.api_key", $apiKey))
->where(new Compare("valid_until", $this->sql->currentTimestamp(), ">")) ->where(new Compare("valid_until", $this->sql->currentTimestamp(), ">"))
->where(new COmpare("ApiKey.active", 1)) ->where(new Compare("ApiKey.active", 1))
->execute(); ->execute();
$success = ($res !== FALSE); $success = ($res !== FALSE);
@ -200,7 +207,7 @@ class User extends ApiObject {
$this->username = $row['username']; $this->username = $row['username'];
if(!is_null($row['langId'])) { if(!is_null($row['langId'])) {
$this->setLangauge(Language::newInstance($row['langId'], $row['langCode'], $row['langName'])); $this->setLanguage(Language::newInstance($row['langId'], $row['langCode'], $row['langName']));
} }
} }
} }
@ -208,5 +215,3 @@ class User extends ApiObject {
return $success; return $success;
} }
} }
?>

@ -1,17 +0,0 @@
<?php
require_once './LanguageModule.php';
abstract class CLanguageModuleGeneral {
public static function getEntries($langCode) {
switch($langCode) {
case "de_DE":
$this->entries[""] = "";
break;
}
}
}
?>

@ -1,9 +1,6 @@
<?php <?php
abstract class CLanguageModule { abstract class LanguageModule {
public abstract static function getEntries($langCode);
public abstract function getEntries(string $langCode);
} }
?>

@ -1,18 +1,19 @@
<?php <?php
use Elements\Document;
abstract class View { abstract class View {
private $document; private Document $document;
private $loadView; private bool $loadView;
protected $searchable; protected bool $searchable;
protected $reference; protected string $reference;
protected $title; protected string $title;
protected $langModules; protected array $langModules;
public function __construct($document, $loadView = true) { public function __construct(Document $document, $loadView = true) {
$this->document = $document; $this->document = $document;
$this->searchable = false; $this->searchable = false;
$this->printable = false;
$this->reference = ""; $this->reference = "";
$this->title = "Untitled View"; $this->title = "Untitled View";
$this->langModules = array(); $this->langModules = array();
@ -224,7 +225,4 @@ abstract class View {
$hidden = ($hidden?" hidden" : ""); $hidden = ($hidden?" hidden" : "");
return "<div class=\"alert alert-$type$hidden\" role=\"alert\"$id>$text</div>"; return "<div class=\"alert alert-$type$hidden\" role=\"alert\"$id>$text</div>";
} }
}; }
?>

@ -50,5 +50,3 @@ class LanguageFlags extends \View {
return implode('', $flags); return implode('', $flags);
} }
} }
?>

@ -2,7 +2,9 @@
namespace Views; namespace Views;
class Login extends \View { use View;
class Login extends View {
public function __construct($document) { public function __construct($document) {
parent::__construct($document); parent::__construct($document);
} }
@ -28,7 +30,7 @@ class Login extends \View {
</div>'; </div>';
} }
$html = " $html .= "
<div class=\"container margin-top-xxl\"> <div class=\"container margin-top-xxl\">
<div class=\"title text-center\"> <div class=\"title text-center\">
<h2>Admin Control Panel</h2> <h2>Admin Control Panel</h2>
@ -51,5 +53,3 @@ class Login extends \View {
return $html; return $html;
} }
} }
?>

@ -2,5 +2,3 @@
const USER_GROUP_DEFAULT = 1; const USER_GROUP_DEFAULT = 1;
const USER_GROUP_ADMIN = 2; const USER_GROUP_ADMIN = 2;
?>

@ -1,20 +1,5 @@
<?php <?php
function clamp($val, $min, $max) {
if($val < $min) return $min;
if($val > $max) return $max;
return $val;
}
function downloadFile($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function getSubclassesOf($parent) { function getSubclassesOf($parent) {
$result = array(); $result = array();
foreach (get_declared_classes() as $class) { foreach (get_declared_classes() as $class) {
@ -28,30 +13,18 @@
return stripos($_SERVER['SERVER_PROTOCOL'],'https') === 0 ? 'https' : 'http'; return stripos($_SERVER['SERVER_PROTOCOL'],'https') === 0 ? 'https' : 'http';
} }
function includeDir($dir, $aIgnore = array(), $recursive = false) { function generateRandomString($length) : string {
$aIgnore[] = '.';
$aIgnore[] = '..';
$aFiles = array_diff(scandir($dir), $aIgnore);
foreach($aFiles as $file) {
$file = $dir . '/' . $file;
if(is_dir($file)) {
if($recursive) {
includeDir($file, $aIgnore, true);
}
} else {
require_once $file;
}
}
}
function generateRandomString($length) {
$randomString = ''; $randomString = '';
if($length > 0) { if($length > 0) {
$numCharacters = 26 + 26 + 10; // a-z + A-Z + 0-9 $numCharacters = 26 + 26 + 10; // a-z + A-Z + 0-9
for ($i = 0; $i < $length; $i++) for ($i = 0; $i < $length; $i++)
{ {
try {
$num = random_int(0, $numCharacters - 1); $num = random_int(0, $numCharacters - 1);
} catch (Exception $e) {
$num = rand(0, $numCharacters - 1);
}
if($num < 26) $randomString .= chr(ord('a') + $num); if($num < 26) $randomString .= chr(ord('a') + $num);
else if($num - 26 < 26) $randomString .= chr(ord('A') + $num - 26); else if($num - 26 < 26) $randomString .= chr(ord('A') + $num - 26);
else $randomString .= chr(ord('0') + $num - 26 - 26); else $randomString .= chr(ord('0') + $num - 26 - 26);
@ -61,20 +34,6 @@
return $randomString; return $randomString;
} }
function cleanPath($path) {
if($path === '')
return $path;
$path = str_replace('\\', '/', $path);
$path = str_replace('/./', '/', $path);
if($path[0] !== '/')
$path = '/' . $path;
$path = str_replace('/../', '/', $path);
return $path;
}
function startsWith($haystack, $needle) { function startsWith($haystack, $needle) {
$length = strlen($needle); $length = strlen($needle);
return (substr($haystack, 0, $length) === $needle); return (substr($haystack, 0, $length) === $needle);
@ -88,21 +47,6 @@
return (substr($haystack, -$length) === $needle); return (substr($haystack, -$length) === $needle);
} }
function isCalledDirectly($file) {
return $_SERVER['SCRIPT_FILENAME'] === $file;
}
function anonymzeEmail($mail) {
if(($pos = strpos($mail, '@')) !== -1) {
$name = substr($mail, 0, $pos);
$host = substr($mail, $pos + 1);
if(strlen($name) > 2) $mail = substr($name, 0, 2) . str_repeat('*', strlen($name) - 2) . "@$host";
else $mail = $mail = str_repeat('*', strlen($name)) . "@$host";
}
return $mail;
}
function intendCode($code, $escape=true) { function intendCode($code, $escape=true) {
$newCode = ""; $newCode = "";
$first = true; $first = true;
@ -138,4 +82,13 @@
function replaceCssSelector($sel) { function replaceCssSelector($sel) {
return preg_replace("~[.#<>]~", "_", preg_replace("~[:\-]~", "", $sel)); return preg_replace("~[.#<>]~", "_", preg_replace("~[:\-]~", "", $sel));
} }
?>
function getClassPath($class, $suffix=true) {
$path = str_replace('\\', '/', $class);
$suffix = ($suffix ? ".class" : "");
return "core/$path$suffix.php";
}
function createError($msg) {
return json_encode(array("success" => false, "msg" => $msg));
}

@ -1,5 +1,26 @@
<?php <?php
function setTimezone($default) {
$timezone = "";
if (is_link("/etc/localtime")) {
$filename = readlink("/etc/localtime");
$pos = strpos($filename, "zoneinfo");
if ($pos) {
$timezone = substr($filename, $pos + strlen("zoneinfo/"));
} else {
$timezone = $default;
}
} else {
$timezone = file_get_contents("/etc/timezone");
if (!strlen($timezone)) {
$timezone = $default;
}
}
date_default_timezone_set($timezone);
}
setTimezone("UTC");
function getFirstWeekDayOfMonth($d = NULL) { function getFirstWeekDayOfMonth($d = NULL) {
if(is_null($d)) $d = date('Y-m-d H:i:s'); if(is_null($d)) $d = date('Y-m-d H:i:s');
$dt = new DateTime($d); $dt = new DateTime($d);
@ -58,8 +79,8 @@ function formatPeriod($d1, $d2) {
function now() { return new DateTime(); } function now() { return new DateTime(); }
function getTime($d = NULL) { return dateFunction('H:i', $d); } function getTime($d = NULL) { return dateFunction('H:i', $d); }
function getHour($d = NULL){ return dateFunction('H', $d); } function getHour($d = NULL) { return dateFunction('H', $d); }
function getMinute($d = NULL){ return dateFunction('i', $d); } function getMinute($d = NULL) { return dateFunction('i', $d); }
function getYear($d = NULL) { return intval(dateFunction('Y', $d)); } function getYear($d = NULL) { return intval(dateFunction('Y', $d)); }
function getMonth($d = NULL) { return intval(dateFunction('n', $d)); } function getMonth($d = NULL) { return intval(dateFunction('n', $d)); }
function getDay($d = NULL) { return intval(dateFunction('d', $d)); } function getDay($d = NULL) { return intval(dateFunction('d', $d)); }
@ -118,4 +139,25 @@ function dateFunction($str, $d = NULL) {
return $d->format($str); return $d->format($str);
} }
?> function getPeriodString($d) {
if(!is_a($d, "DateTime")) $d = new DateTime($d);
$diff = datetimeDiff(new DateTime(), $d);
$diff = abs($diff);
if ($diff < 60) {
$str = "< %d min";
$diff = 1;
} else if($diff < 60*60) {
$diff = intval($diff / 60);
$str = "%d min.";
} else if($diff < 60*60*24) {
$diff = intval($diff / (60*60));
$str = "%d h.";
} else {
$diff = intval($diff / (60*60*24));
$str = "%d d.";
}
return L(sprintf($str, $diff));
}

@ -1,14 +1,8 @@
<?php <?php
function getClassPath($class, $suffix=true) { include_once 'core/core.php';
$path = str_replace('\\', '/', $class); include_once 'core/datetime.php';
$suffix = ($suffix ? ".class" : ""); include_once 'core/constants.php';
return "core/$path$suffix.php";
}
function createError($msg) {
return json_encode(array("success" => false, "msg" => $msg));
}
spl_autoload_extensions(".php"); spl_autoload_extensions(".php");
spl_autoload_register(function($class) { spl_autoload_register(function($class) {
@ -19,10 +13,6 @@ spl_autoload_register(function($class) {
include_once getClassPath($class, false); include_once getClassPath($class, false);
}); });
include_once 'core/core.php';
include_once 'core/datetime.php';
include_once 'core/constants.php';
$config = new Configuration\Configuration(); $config = new Configuration\Configuration();
$installation = (!$config->load()); $installation = (!$config->load());
$user = new Objects\User($config); $user = new Objects\User($config);