From: Banana Date: Sun, 2 Jun 2024 08:26:38 +0000 (+0200) Subject: more cleanups X-Git-Tag: 1.1~9 X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=073bc7858cbc83fd2ecc9228bedf8452e5fbfb83;p=scientia.git more cleanups Signed-off-by: Banana --- diff --git a/documentation/requirements.md b/documentation/requirements.md index b39e342..acd720c 100644 --- a/documentation/requirements.md +++ b/documentation/requirements.md @@ -1,5 +1,5 @@ # Requirements + A Webserver (tested on Apache 2.4) -+ PHP 8+ with mysqli, mbstring, curl ++ PHP 8.1+ with mysqli, mbstring, curl + MySQL 8+ / MariaDB 10.2 diff --git a/documentation/upgrade-from-1.0.md b/documentation/upgrade-from-1.0.md new file mode 100644 index 0000000..3a9fa07 --- /dev/null +++ b/documentation/upgrade-from-1.0.md @@ -0,0 +1,21 @@ +# Config changes + +Update your existing `config.php` file with the following changes. +Missing those changes, errors will happen. + +# i18n +New config for i18n. Open config.php and add the following: +``` +# language settings +const FRONTEND_LANGUAGE = 'en'; +``` +currently only en (default) and de are available. + +# Installation URL + +To provide the correct URL please add this. + +``` +# Installation Domain. Webrootpath will be added automatically +const INSTALL_URL = 'http://localhost'; +``` diff --git a/documentation/upgrade-from-1.0.txt b/documentation/upgrade-from-1.0.txt deleted file mode 100644 index 5b5b9d4..0000000 --- a/documentation/upgrade-from-1.0.txt +++ /dev/null @@ -1,6 +0,0 @@ -New config for i18n. Open config.php and add the following: - -# language settings -const FRONTEND_LANGUAGE = 'en'; - -currently only en (default) and de are available. diff --git a/webroot/api.php b/webroot/api.php index 2791c29..062e51c 100644 --- a/webroot/api.php +++ b/webroot/api.php @@ -2,7 +2,7 @@ /** * scientia * - * Copyright 2023 Johannes Keßler + * Copyright 2023 - 2024 Johannes Keßler * * https://www.bananas-playground.net/projekt/scientia/ * @@ -22,10 +22,10 @@ ini_set('error_reporting',-1); // E_ALL & E_STRICT ## check request $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); if(!empty($_urlToParse)) { - # see http://de2.php.net/manual/en/regexp.reference.unicode.php - if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) { - die('Malformed request. Make sure you know what you are doing.'); - } + # see http://de2.php.net/manual/en/regexp.reference.unicode.php + if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) { + die('Malformed request. Make sure you know what you are doing.'); + } } ## config @@ -53,21 +53,21 @@ if(DEBUG) error_log("Dump SERVER ".var_export($_SERVER,true)); $_create = false; $filteredData = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE'] === 'application/json; charset=UTF-8') { - $payload = json_decode(file_get_contents('php://input'), true); - if(DEBUG) error_log("[DEBUG] Dump payload ".var_export($payload,true)); - if(!empty($payload)) { - if(isset($payload['asl']) && !empty($payload['asl']) - && isset($payload['data']) && !empty($payload['data']) - && isset(UPLOAD_SECRET[$payload['asl']]) - ) { - if(DEBUG) error_log("[DEBUG] Valid payload so far"); - $filteredData = filter_var($payload['data'],FILTER_SANITIZE_FULL_SPECIAL_CHARS); - if(!empty($filteredData)) { - if(DEBUG) error_log("[DEBUG] Validated payload"); - $_create = true; - } - } - } + $payload = json_decode(file_get_contents('php://input'), true); + if(DEBUG) error_log("[DEBUG] Dump payload ".var_export($payload,true)); + if(!empty($payload)) { + if(isset($payload['asl']) && !empty($payload['asl']) + && isset($payload['data']) && !empty($payload['data']) + && isset(UPLOAD_SECRET[$payload['asl']]) + ) { + if(DEBUG) error_log("[DEBUG] Valid payload so far"); + $filteredData = filter_var($payload['data'],FILTER_SANITIZE_FULL_SPECIAL_CHARS); + if(!empty($filteredData)) { + if(DEBUG) error_log("[DEBUG] Validated payload"); + $_create = true; + } + } + } } ## default response @@ -99,13 +99,13 @@ require_once 'lib/entry.class.php'; $Entry = new Entry($DB); $do = $Entry->create($filteredData); if(!empty($do)) { - $contentBody['message'] = INSTALL_URL . PATH_WEBROOT . date('/Y/m/d/').$do; + $contentBody['message'] = INSTALL_URL . PATH_WEBROOT . date('/Y/m/d/').$do; } else { - $hash = md5($do.time()); - error_log("[ERROR] $hash Can not create. ". var_export($do,true)); - $contentBody['message'] = "Something went wrong. $hash"; - $contentBody['status'] = 500; + $hash = md5($do.time()); + error_log("[ERROR] $hash Can not create. ". var_export($do,true)); + $contentBody['message'] = "Something went wrong. $hash"; + $contentBody['status'] = 500; } # return diff --git a/webroot/config/config.php.default b/webroot/config/config.php.default index cb416a3..7911865 100644 --- a/webroot/config/config.php.default +++ b/webroot/config/config.php.default @@ -2,7 +2,7 @@ /** * scientia * - * Copyright 2022 Johannes Keßler + * Copyright 2022 - 2024 Johannes Keßler * * https://www.bananas-playground.net/projekt/scientia/ * diff --git a/webroot/index.php b/webroot/index.php index 97d81eb..4de0689 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -2,7 +2,7 @@ /** * scientia * - * Copyright 2023 Johannes Keßler + * Copyright 2023 - 2024 Johannes Keßler * * https://www.bananas-playground.net/projekt/scientia/ * @@ -22,10 +22,10 @@ ini_set('error_reporting',-1); // E_ALL & E_STRICT ## check request $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); if(!empty($_urlToParse)) { - # see http://de2.php.net/manual/en/regexp.reference.unicode.php - if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) { - die('Malformed request. Make sure you know what you are doing.'); - } + # see http://de2.php.net/manual/en/regexp.reference.unicode.php + if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) { + die('Malformed request. Make sure you know what you are doing.'); + } } ## config @@ -35,10 +35,10 @@ require_once('config/config.php'); ini_set('log_errors',true); ini_set('error_log',PATH_SYSTEMOUT.'/error.log'); if(DEBUG === true) { - ini_set('display_errors',true); + ini_set('display_errors',true); } else { - ini_set('display_errors',false); + ini_set('display_errors',false); } # time settings @@ -71,23 +71,23 @@ $_requestDateProvided = ''; $TemplateData = array(); if(isset($_GET['y']) && Summoner::validate($_GET['y'], 'digit')) { - $_year = trim($_GET['y']); - $_requestDateProvided .= 'Y'; + $_year = trim($_GET['y']); + $_requestDateProvided .= 'Y'; } if(isset($_GET['m']) && Summoner::validate($_GET['m'], 'digit')) { - $_month = trim($_GET['m']); - $_requestDateProvided .= '-m'; + $_month = trim($_GET['m']); + $_requestDateProvided .= '-m'; } if(isset($_GET['d']) && Summoner::validate($_GET['d'], 'digit')) { - $_day = trim($_GET['d']); - $_requestDateProvided .= '-d'; + $_day = trim($_GET['d']); + $_requestDateProvided .= '-d'; } if(isset($_GET['p']) && Summoner::validate($_GET['p'], 'nospace') && $_GET['p'] == "new") { - $_view = 'entry'; + $_view = 'entry'; } if(isset($_GET['id']) && Summoner::validate($_GET['id'], 'shortlink',4)) { - $_id = trim($_GET['id']); - $_view = 'entry'; + $_id = trim($_GET['id']); + $_view = 'entry'; } require_once 'view/'.$_view.'/'.$_view.'.php'; @@ -98,8 +98,8 @@ header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); if(isset($TemplateData['refresh']) && !empty($TemplateData['refresh'])) { - header('Location: '.PATH_WEBROOT.$TemplateData['refresh']); - exit(); + header('Location: '.PATH_WEBROOT.$TemplateData['refresh']); + exit(); } require_once 'view/_head.php'; diff --git a/webroot/lib/entry.class.php b/webroot/lib/entry.class.php index 629a9e3..bbf236d 100644 --- a/webroot/lib/entry.class.php +++ b/webroot/lib/entry.class.php @@ -2,7 +2,7 @@ /** * scientia * - * Copyright 2022 Johannes Keßler + * Copyright 2022 - 2024 Johannes Keßler * * https://www.bananas-playground.net/projekt/scientia/ * @@ -21,149 +21,149 @@ * Entry loading and creation */ class Entry { - /** - * the global DB object - * - * @var mysqli - */ - private mysqli $_DB; - - /** - * Entry constructor. - * - * @param mysqli $db - */ - public function __construct(mysqli $db) { - $this->_DB = $db; - } - - /** - * Create a new entry with given data - * Data is not validated anymore - * - * @param string $data - * @return string - */ - public function create(string $data): string { - $ret = ''; - - $_words = implode(' ', $this->_words($data)); - $_ident = Summoner::b64sl_pack_id(rand(111111, 999999)); - $queryStr = "INSERT INTO `".DB_PREFIX."_entry` SET - `created` = NOW(), - `date` = CURRENT_DATE(), - `ident` = '".$this->_DB->real_escape_string($_ident)."', - `body` = '".$this->_DB->real_escape_string($data)."', - `words` = '".$this->_DB->real_escape_string($_words)."'"; - if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true)); - - try { - $this->_DB->query($queryStr); - $ret = $_ident; - } - catch(Exception $e) { - error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage()); - } - - return $ret; - } - - /** - * Load an entry by given $id. Use date info to make sure that the context is correct - * - * @param string $y Year Y - * @param string $m Month m - * @param string $d Day d - * @param string $id Id of the entry - * @return array - */ - public function load(string $y, string $m, string $d, string $id): array { - $ret = array(); - - if(!empty($id) && !empty($y) && !empty($m) && !empty($d)) { - $queryStr = "SELECT `created`,`modified`,`body` - FROM `".DB_PREFIX."_entry` - WHERE `ident` = '".$this->_DB->real_escape_string($id)."' - AND `date` = '".$this->_DB->real_escape_string($y.'-'.$m.'-'.$d)."'"; - if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true)); - try { - $query = $this->_DB->query($queryStr); - if($query !== false && $query->num_rows > 0) { - $ret = $query->fetch_assoc(); - } - } - catch(Exception $e) { - error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage()); - } - } - - return $ret; - } - - /** - * Update an entry by given $id and $data - * - * @param string $data - * @param string $id - * @return string - */ - public function update(string $data, string $id): string { - $ret = ''; - - if(!empty($data) && !empty($id)) { - $_words = implode(' ', $this->_words($data)); - $queryStr = "UPDATE `".DB_PREFIX."_entry` SET - `body` = '".$this->_DB->real_escape_string($data)."', - `words` = '".$this->_DB->real_escape_string($_words)."' - WHERE `ident` = '".$this->_DB->real_escape_string($id)."'"; - if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true)); - try { - $this->_DB->query($queryStr); - $ret = $id; - } - catch(Exception $e) { - error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage()); - } - } - - return $ret; - } - - /** - * Delete given id from _entry table - * - * @param string $id - * @return bool - */ - public function delete(string $id): bool { - $ret = false; - - if(!empty($id)) { - $queryStr = "DELETE FROM `".DB_PREFIX."_entry` - WHERE `ident` = '".$this->_DB->real_escape_string($id)."'"; - if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true)); - try { - $this->_DB->query($queryStr); - $ret = true; - } - catch(Exception $e) { - error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage()); - } - } - - return $ret; - } - - /** - * Create unique words from the given data - * - * @param $data string - * @return array - * @todo ignores - * - */ - private function _words(string $data): array { - preg_match_all('/\w{3,}+/u',$data,$matches); - return array_unique($matches[0]); - } + /** + * the global DB object + * + * @var mysqli + */ + private mysqli $_DB; + + /** + * Entry constructor. + * + * @param mysqli $db + */ + public function __construct(mysqli $db) { + $this->_DB = $db; + } + + /** + * Create a new entry with given data + * Data is not validated anymore + * + * @param string $data + * @return string + */ + public function create(string $data): string { + $ret = ''; + + $_words = implode(' ', $this->_words($data)); + $_ident = Summoner::b64sl_pack_id(rand(111111, 999999)); + $queryStr = "INSERT INTO `".DB_PREFIX."_entry` SET + `created` = NOW(), + `date` = CURRENT_DATE(), + `ident` = '".$this->_DB->real_escape_string($_ident)."', + `body` = '".$this->_DB->real_escape_string($data)."', + `words` = '".$this->_DB->real_escape_string($_words)."'"; + if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true)); + + try { + $this->_DB->query($queryStr); + $ret = $_ident; + } + catch(Exception $e) { + error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage()); + } + + return $ret; + } + + /** + * Load an entry by given $id. Use date info to make sure that the context is correct + * + * @param string $y Year Y + * @param string $m Month m + * @param string $d Day d + * @param string $id Id of the entry + * @return array + */ + public function load(string $y, string $m, string $d, string $id): array { + $ret = array(); + + if(!empty($id) && !empty($y) && !empty($m) && !empty($d)) { + $queryStr = "SELECT `created`,`modified`,`body` + FROM `".DB_PREFIX."_entry` + WHERE `ident` = '".$this->_DB->real_escape_string($id)."' + AND `date` = '".$this->_DB->real_escape_string($y.'-'.$m.'-'.$d)."'"; + if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true)); + try { + $query = $this->_DB->query($queryStr); + if($query !== false && $query->num_rows > 0) { + $ret = $query->fetch_assoc(); + } + } + catch(Exception $e) { + error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage()); + } + } + + return $ret; + } + + /** + * Update an entry by given $id and $data + * + * @param string $data + * @param string $id + * @return string + */ + public function update(string $data, string $id): string { + $ret = ''; + + if(!empty($data) && !empty($id)) { + $_words = implode(' ', $this->_words($data)); + $queryStr = "UPDATE `".DB_PREFIX."_entry` SET + `body` = '".$this->_DB->real_escape_string($data)."', + `words` = '".$this->_DB->real_escape_string($_words)."' + WHERE `ident` = '".$this->_DB->real_escape_string($id)."'"; + if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true)); + try { + $this->_DB->query($queryStr); + $ret = $id; + } + catch(Exception $e) { + error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage()); + } + } + + return $ret; + } + + /** + * Delete given id from _entry table + * + * @param string $id + * @return bool + */ + public function delete(string $id): bool { + $ret = false; + + if(!empty($id)) { + $queryStr = "DELETE FROM `".DB_PREFIX."_entry` + WHERE `ident` = '".$this->_DB->real_escape_string($id)."'"; + if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true)); + try { + $this->_DB->query($queryStr); + $ret = true; + } + catch(Exception $e) { + error_log("[ERROR] ".__METHOD__." catch: ".$e->getMessage()); + } + } + + return $ret; + } + + /** + * Create unique words from the given data + * + * @param $data string + * @return array + * @todo ignores + * + */ + private function _words(string $data): array { + preg_match_all('/\w{3,}+/u',$data,$matches); + return array_unique($matches[0]); + } } diff --git a/webroot/lib/i18n.class.php b/webroot/lib/i18n.class.php index a7c63e3..2678898 100644 --- a/webroot/lib/i18n.class.php +++ b/webroot/lib/i18n.class.php @@ -2,7 +2,7 @@ /** * scientia * - * Copyright 2023 Johannes Keßler + * Copyright 2023 - 2024 Johannes Keßler * * https://www.bananas-playground.net/projekt/scientia/ * @@ -16,49 +16,49 @@ */ class I18n { - /** - * @var string The lang code - */ - private string $_defaultLangToUse = 'en'; + /** + * @var string The lang code + */ + private string $_defaultLangToUse = 'en'; - /** - * @var array The loaded lang information from the file - */ - private array $_langData = array(); + /** + * @var array The loaded lang information from the file + */ + private array $_langData = array(); - /** - * i18n constructor. - */ - public function __construct() { - $_langFile = PATH_ABSOLUTE.'/lib/i18n/'.$this->_defaultLangToUse.'.ini'; - if(defined('FRONTEND_LANGUAGE')) { - $_langFile = PATH_ABSOLUTE.'/lib/i18n/'.FRONTEND_LANGUAGE.'.ini'; - if(file_exists($_langFile)) { - $_langData = parse_ini_file($_langFile); - if($_langData !== false) { - $this->_langData = $_langData; - } - } - } - else { - $_langData = parse_ini_file($_langFile); - if($_langData !== false) { - $this->_langData = $_langData; - } - } - } + /** + * i18n constructor. + */ + public function __construct() { + $_langFile = PATH_ABSOLUTE.'/lib/i18n/'.$this->_defaultLangToUse.'.ini'; + if(defined('FRONTEND_LANGUAGE')) { + $_langFile = PATH_ABSOLUTE.'/lib/i18n/'.FRONTEND_LANGUAGE.'.ini'; + if(file_exists($_langFile)) { + $_langData = parse_ini_file($_langFile); + if($_langData !== false) { + $this->_langData = $_langData; + } + } + } + else { + $_langData = parse_ini_file($_langFile); + if($_langData !== false) { + $this->_langData = $_langData; + } + } + } - /** - * Return text for given key for currently loaded lang - * - * @param string $key - * @return string - */ - public function t(string $key): string { - $ret = $key; - if(isset($this->_langData[$key])) { - $ret = $this->_langData[$key]; - } - return $ret; - } + /** + * Return text for given key for currently loaded lang + * + * @param string $key + * @return string + */ + public function t(string $key): string { + $ret = $key; + if(isset($this->_langData[$key])) { + $ret = $this->_langData[$key]; + } + return $ret; + } } diff --git a/webroot/lib/i18n/de.ini b/webroot/lib/i18n/de.ini index 5eeb16b..187aeb3 100644 --- a/webroot/lib/i18n/de.ini +++ b/webroot/lib/i18n/de.ini @@ -1,6 +1,6 @@ ; scientia ; -; Copyright 2022 Johannes Keßler +; Copyright 2022 - 2024 Johannes Keßler ; ; https://www.bananas-playground.net/projekt/scientia/ ; @@ -18,4 +18,4 @@ form.checkbox.deleteentry=Eintrag löschen text.home=Index text.new=Neu -text.noentries=Nichts vorhanden. \ No newline at end of file +text.noentries=Nichts vorhanden. diff --git a/webroot/lib/i18n/en.ini b/webroot/lib/i18n/en.ini index 1967707..f56c741 100644 --- a/webroot/lib/i18n/en.ini +++ b/webroot/lib/i18n/en.ini @@ -1,6 +1,6 @@ ; scientia ; -; Copyright 2022 Johannes Keßler +; Copyright 2022 - 2024 Johannes Keßler ; ; https://www.bananas-playground.net/projekt/scientia/ ; @@ -19,4 +19,3 @@ form.checkbox.deleteentry=Delete Entry text.home=Home text.new=New text.noentries=Nothing here. - diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index 4ee5b87..d05407a 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -2,7 +2,7 @@ /** * scientia * - * Copyright 2023 Johannes Keßler + * Copyright 2023 - 2024 Johannes Keßler * * https://www.bananas-playground.net/projekt/scientia/ * @@ -21,23 +21,23 @@ * A static helper class */ class Summoner { - /** - * validate the given string with the given type. Optional check the string - * length - * - * @param string $input The string to check - * @param string $mode How the string should be checked - * @param string $limit If int given the string is checked for length - * - * @return bool - * - * @see http://de.php.net/manual/en/regexp.reference.unicode.php - * http://www.sql-und-xml.de/unicode-database/#pc - * - * the pattern replaces all that is allowed. the correct result after - * the replace should be empty, otherwise are there chars which are not - * allowed - */ + /** + * validate the given string with the given type. Optional check the string + * length + * + * @param string $input The string to check + * @param string $mode How the string should be checked + * @param string $limit If int given the string is checked for length + * + * @return bool + * + * @see http://de.php.net/manual/en/regexp.reference.unicode.php + * http://www.sql-und-xml.de/unicode-database/#pc + * + * the pattern replaces all that is allowed. the correct result after + * the replace should be empty, otherwise are there chars which are not + * allowed + */ static function validate(string $input, string $mode='text', string $limit=''): bool { // check if we have input $input = trim($input); @@ -54,7 +54,7 @@ class Summoner { else { return false; } - break; + break; case 'url': if(filter_var($input,FILTER_VALIDATE_URL) === $input) { @@ -63,41 +63,41 @@ class Summoner { else { return false; } - break; + break; case 'nospace': // text without any whitespace and special chars $pattern = '/[\p{L}\p{N}]/u'; - break; + break; case 'nospaceP': // text without any whitespace and special chars // but with Punctuation other # http://www.sql-und-xml.de/unicode-database/po.html $pattern = '/[\p{L}\p{N}\p{Po}\-]/u'; - break; + break; case 'digit': // only numbers and digit // warning with negative numbers... $pattern = '/[\p{N}\-]/'; - break; + break; case 'pageTitle': // text with whitespace and without special chars // but with Punctuation $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s-]/u'; - break; + break; # strange. the \p{M} is needed.. don't know why.. case 'filename': $pattern = '/[\p{L}\p{N}\p{M}\-_\.\p{Zs}]/u'; - break; + break; - case 'shortlink': - // special char string based on https://www.jwz.org/base64-shortlinks/ - $pattern = '/[\p{L}\p{N}\-_]/u'; - break; + case 'shortlink': + // special char string based on https://www.jwz.org/base64-shortlinks/ + $pattern = '/[\p{L}\p{N}\-_]/u'; + break; case 'text': default: @@ -121,25 +121,25 @@ class Summoner { return $ret; } - /** - * check if a string starts with a given string - * - * @param string $haystack - * @param string $needle - * @return boolean - */ + /** + * check if a string starts with a given string + * + * @param string $haystack + * @param string $needle + * @return boolean + */ static function startsWith(string $haystack, string $needle): bool { $length = strlen($needle); return (substr($haystack, 0, $length) === $needle); } - /** - * check if a string ends with a given string - * - * @param string $haystack - * @param string $needle - * @return boolean - */ + /** + * check if a string ends with a given string + * + * @param string $haystack + * @param string $needle + * @return boolean + */ static function endsWith(string $haystack, string $needle): bool { $length = strlen($needle); if ($length == 0) { @@ -150,13 +150,13 @@ class Summoner { } - /** - * create a short string based on a integer - * - * @see https://www.jwz.org/base64-shortlinks/ - * @param int $id - * @return string - */ + /** + * create a short string based on a integer + * + * @see https://www.jwz.org/base64-shortlinks/ + * @param int $id + * @return string + */ static function b64sl_pack_id(int $id): string { $id = intval($id); $ida = ($id > 0xFFFFFFFF ? $id >> 32 : 0); // 32 bit big endian, top @@ -170,13 +170,13 @@ class Summoner { return $id; } - /** - * Decode a base64-encoded big-endian integer of up to 64 bits. - * - * @see https://www.jwz.org/base64-shortlinks/ - * @param string $id - * @return int - */ + /** + * Decode a base64-encoded big-endian integer of up to 64 bits. + * + * @see https://www.jwz.org/base64-shortlinks/ + * @param string $id + * @return int + */ static function b64sl_unpack_id(string $id): int { $id = str_replace ('-', '+', $id); // decode URL-unsafe "+" "/" $id = str_replace ('_', '/', $id); @@ -187,33 +187,18 @@ class Summoner { return $id; } - /** - * simulate the Null coalescing operator in php5 - * - * this only works with arrays and checking if the key is there and echo/return it. - * - * http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op - * - * @param $array array - * @param $key string - * @return mixed - */ - static function ifset(array $array, string $key): mixed { - return isset($array[$key]) ? $array[$key] : false; - } - - /** - * a very simple HTTP_AUTH authentication. - * Needs FRONTEND_USERNAME and FRONTEND_PASSWORD defined - */ - static function simpleAuth(): void { - if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) - || $_SERVER['PHP_AUTH_USER'] !== FRONTEND_USERNAME || $_SERVER['PHP_AUTH_PW'] !== FRONTEND_PASSWORD - ) { - header('WWW-Authenticate: Basic realm="Protected area"'); - header('HTTP/1.0 401 Unauthorized'); - echo 'No Access.'; - exit; - } - } + /** + * a very simple HTTP_AUTH authentication. + * Needs FRONTEND_USERNAME and FRONTEND_PASSWORD defined + */ + static function simpleAuth(): void { + if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) + || $_SERVER['PHP_AUTH_USER'] !== FRONTEND_USERNAME || $_SERVER['PHP_AUTH_PW'] !== FRONTEND_PASSWORD + ) { + header('WWW-Authenticate: Basic realm="Protected area"'); + header('HTTP/1.0 401 Unauthorized'); + echo 'No Access.'; + exit; + } + } } diff --git a/webroot/view/_foot.php b/webroot/view/_foot.php index 45716f3..dfd91ea 100644 --- a/webroot/view/_foot.php +++ b/webroot/view/_foot.php @@ -1,5 +1,5 @@ - + diff --git a/webroot/view/_head.php b/webroot/view/_head.php index 918a5a4..de04365 100644 --- a/webroot/view/_head.php +++ b/webroot/view/_head.php @@ -1,11 +1,11 @@ - - - + + + diff --git a/webroot/view/entry/entry.html b/webroot/view/entry/entry.html index e8f5c95..23a3c0d 100644 --- a/webroot/view/entry/entry.html +++ b/webroot/view/entry/entry.html @@ -1,21 +1,21 @@

- t('text.home'); ?> - '.$be.' | '; - } - ?> - t('text.new'); ?> - + t('text.home'); ?> + '.$be.' | '; + } + ?> + t('text.new'); ?> +

-

Created: , Updated:

+

Created: , Updated:

- - - t('form.checkbox.deleteentry'); ?> + + + t('form.checkbox.deleteentry'); ?>
diff --git a/webroot/view/entry/entry.php b/webroot/view/entry/entry.php index 704b56a..1dbe0fb 100644 --- a/webroot/view/entry/entry.php +++ b/webroot/view/entry/entry.php @@ -2,7 +2,7 @@ /** * scientia * - * Copyright 2022 Johannes Keßler + * Copyright 2022 - 2024 Johannes Keßler * * https://www.bananas-playground.net/projekt/scientia/ * @@ -20,30 +20,30 @@ $Entry = new Entry($DB); $TemplateData['data'] = array(); if(!empty($_id)) { - $TemplateData['data'] = $Entry->load($_year,$_month,$_day,$_id); - $TemplateData['data']['breadcrumb'] = array($_year,$_month,$_day); + $TemplateData['data'] = $Entry->load($_year,$_month,$_day,$_id); + $TemplateData['data']['breadcrumb'] = array($_year,$_month,$_day); } if(isset($_POST['submitForm']) && isset($_POST['fdata'])) { - $fdata = $_POST['fdata']; - if(isset($fdata['entry']) && Summoner::validate($fdata['entry'])) { - $_dataToSave = trim($fdata['entry']); + $fdata = $_POST['fdata']; + if(isset($fdata['entry']) && Summoner::validate($fdata['entry'])) { + $_dataToSave = trim($fdata['entry']); - if(!empty($_id) && isset($_POST['deleteEntry']) && $_POST['deleteEntry'] == "yes") { - $do = $Entry->delete($_id); - $_r = '/'; - } - elseif(!empty($_id)) { - $do = $Entry->update($_dataToSave,$_id); - $_r = '/'.$_year.'/'.$_month.'/'.$_day.'/'.$_id; - } - else { - $do = $Entry->create($_dataToSave); - $_r = date('/Y/m/d/').$do;; - } + if(!empty($_id) && isset($_POST['deleteEntry']) && $_POST['deleteEntry'] == "yes") { + $do = $Entry->delete($_id); + $_r = '/'; + } + elseif(!empty($_id)) { + $do = $Entry->update($_dataToSave,$_id); + $_r = '/'.$_year.'/'.$_month.'/'.$_day.'/'.$_id; + } + else { + $do = $Entry->create($_dataToSave); + $_r = date('/Y/m/d/').$do;; + } - if($do !== false) { - $TemplateData['refresh'] = $_r; - } - } + if($do !== false) { + $TemplateData['refresh'] = $_r; + } + } } diff --git a/webroot/view/list/list.html b/webroot/view/list/list.html index 4c8111b..1e5517e 100644 --- a/webroot/view/list/list.html +++ b/webroot/view/list/list.html @@ -1,27 +1,27 @@
- - + +

- t('text.home'); ?> | + t('text.home'); ?> | $v) { - $_link = ''; - foreach($v['breadcrumb'] as $be) { - $_link .= '/'.$be; - echo ''.$be.' | '; - } + foreach($TemplateData['entries'] as $k=>$v) { + $_link = ''; + foreach($v['breadcrumb'] as $be) { + $_link .= '/'.$be; + echo ''.$be.' | '; + } ?> - t('text.new'); ?> + t('text.new'); ?>

t('text.noentries'); ?>

diff --git a/webroot/view/list/list.php b/webroot/view/list/list.php index 1a3553e..23a03be 100644 --- a/webroot/view/list/list.php +++ b/webroot/view/list/list.php @@ -2,7 +2,7 @@ /** * scientia * - * Copyright 2022 Johannes Keßler + * Copyright 2022 - 2024 Johannes Keßler * * https://www.bananas-playground.net/projekt/scientia/ * @@ -22,9 +22,9 @@ $queryLimit = " LIMIT 100"; $searchTerm = ''; if(isset($_POST['submitForm']) && isset($_POST['searchInput'])) { - if(Summoner::validate($_POST['searchInput'])) { - $searchTerm = trim($_POST['searchInput']); - } + if(Summoner::validate($_POST['searchInput'])) { + $searchTerm = trim($_POST['searchInput']); + } } // why? @@ -33,41 +33,41 @@ if(isset($_POST['submitForm']) && isset($_POST['searchInput'])) { $_groupByFormat = $_year; $breadcrumb = array('Y'); if(!empty($_requestDateProvided)) { - $_intervalStart = ''; - $_intervalEnd = ''; + $_intervalStart = ''; + $_intervalEnd = ''; - if($_requestDateProvided === 'Y-m-d') { - $queryLimit = ""; - $_groupByFormat = $_year.'-'.$_month.'-'.$_day; - $_intervalStart = $_groupByFormat; - $_intervalEnd = $_groupByFormat; - $breadcrumb = array('Y','m','d'); - } - elseif ($_requestDateProvided === 'Y-m') { - $queryLimit = ""; - $_groupByFormat = $_year.'-'.$_month; - $_intervalStart = $_groupByFormat.'-01'; - $_tDate = new DateTime( $_intervalStart ); - $_monthDays = $_tDate->format( 't' ); - $_intervalEnd = $_groupByFormat.'-'.$_monthDays; - $breadcrumb = array('Y','m'); - } - elseif ($_requestDateProvided === 'Y') { - $_intervalStart = $_groupByFormat.'-01-01'; - $_intervalEnd = $_groupByFormat.'-12-31'; - } + if($_requestDateProvided === 'Y-m-d') { + $queryLimit = ""; + $_groupByFormat = $_year.'-'.$_month.'-'.$_day; + $_intervalStart = $_groupByFormat; + $_intervalEnd = $_groupByFormat; + $breadcrumb = array('Y','m','d'); + } + elseif ($_requestDateProvided === 'Y-m') { + $queryLimit = ""; + $_groupByFormat = $_year.'-'.$_month; + $_intervalStart = $_groupByFormat.'-01'; + $_tDate = new DateTime( $_intervalStart ); + $_monthDays = $_tDate->format( 't' ); + $_intervalEnd = $_groupByFormat.'-'.$_monthDays; + $breadcrumb = array('Y','m'); + } + elseif ($_requestDateProvided === 'Y') { + $_intervalStart = $_groupByFormat.'-01-01'; + $_intervalEnd = $_groupByFormat.'-12-31'; + } - if(!empty($_intervalStart) && !empty($_intervalEnd)) { - $queryStr .= " WHERE e.date >= '".$_intervalStart."' AND e.date <= '".$_intervalEnd."'"; - if(!empty($searchTerm)) { - $queryStr .= " AND MATCH(e.words) AGAINST('".$DB->real_escape_string($searchTerm)."' IN BOOLEAN MODE)"; - } - } + if(!empty($_intervalStart) && !empty($_intervalEnd)) { + $queryStr .= " WHERE e.date >= '".$_intervalStart."' AND e.date <= '".$_intervalEnd."'"; + if(!empty($searchTerm)) { + $queryStr .= " AND MATCH(e.words) AGAINST('".$DB->real_escape_string($searchTerm)."' IN BOOLEAN MODE)"; + } + } } else { - $_requestDateProvided = 'Y'; - if(!empty($searchTerm)) { - $queryStr .= " WHERE MATCH(e.words) AGAINST('".$DB->real_escape_string($searchTerm)."' IN BOOLEAN MODE)"; - } + $_requestDateProvided = 'Y'; + if(!empty($searchTerm)) { + $queryStr .= " WHERE MATCH(e.words) AGAINST('".$DB->real_escape_string($searchTerm)."' IN BOOLEAN MODE)"; + } } $queryStr .= " ORDER BY `created` DESC"; @@ -75,20 +75,20 @@ $queryStr .= $queryLimit; if(QUERY_DEBUG) error_log("[QUERY] query: ".var_export($queryStr,true)); try { - $query = $DB->query($queryStr); - if($query !== false && $query->num_rows > 0) { - while(($result = $query->fetch_assoc()) != false) { - $_d = new DateTime($result['date']); - $_breadcrumb = array(); - foreach($breadcrumb as $_b) { - $_breadcrumb[] = $_d->format($_b); - } - $TemplateData['entries'][$_d->format($_requestDateProvided)]['breadcrumb'] = $_breadcrumb; - $TemplateData['entries'][$_d->format($_requestDateProvided)]['e'][$result['ident']] = $result; - $TemplateData['entries'][$_d->format($_requestDateProvided)]['e'][$result['ident']]['link'] = str_replace('-','/',$result['date']).'/'.$result['ident']; - } - } + $query = $DB->query($queryStr); + if($query !== false && $query->num_rows > 0) { + while(($result = $query->fetch_assoc()) != false) { + $_d = new DateTime($result['date']); + $_breadcrumb = array(); + foreach($breadcrumb as $_b) { + $_breadcrumb[] = $_d->format($_b); + } + $TemplateData['entries'][$_d->format($_requestDateProvided)]['breadcrumb'] = $_breadcrumb; + $TemplateData['entries'][$_d->format($_requestDateProvided)]['e'][$result['ident']] = $result; + $TemplateData['entries'][$_d->format($_requestDateProvided)]['e'][$result['ident']]['link'] = str_replace('-','/',$result['date']).'/'.$result['ident']; + } + } } catch(Exception $e) { - error_log("[ERROR] catch: ".$e->getMessage()); + error_log("[ERROR] catch: ".$e->getMessage()); }