diff options
Diffstat (limited to 'app/class')
-rw-r--r-- | app/class/Bookmark.php | 106 | ||||
-rw-r--r-- | app/class/Colors.php | 2 | ||||
-rw-r--r-- | app/class/Config.php | 2 | ||||
-rw-r--r-- | app/class/Controller.php | 9 | ||||
-rw-r--r-- | app/class/Controllerhome.php | 4 | ||||
-rw-r--r-- | app/class/Element.php | 2 | ||||
-rw-r--r-- | app/class/Item.php | 39 | ||||
-rw-r--r-- | app/class/Model.php | 7 | ||||
-rw-r--r-- | app/class/Modelrender.php | 66 | ||||
-rw-r--r-- | app/class/Modeluser.php | 2 | ||||
-rw-r--r-- | app/class/Opt.php | 6 | ||||
-rw-r--r-- | app/class/Page.php | 2 | ||||
-rw-r--r-- | app/class/User.php | 19 |
13 files changed, 214 insertions, 52 deletions
diff --git a/app/class/Bookmark.php b/app/class/Bookmark.php new file mode 100644 index 0000000..bc77310 --- /dev/null +++ b/app/class/Bookmark.php @@ -0,0 +1,106 @@ +<?php + +namespace Wcms; + +use DateTime; +use DateTimeImmutable; +use DateTimeZone; +use Exception; +use RuntimeException; + +class Bookmark extends Item +{ + /** @var string $id Bookmark ID */ + protected $id; + /** @var string $query */ + protected $query = ''; + /** @var string $route Can be `page|media` */ + protected $route; + /** @var array $params*/ + protected $params = []; + /** @var string $icon associated emoji */ + protected $icon = '⭐'; + + + public function __construct(array $datas = []) + { + $this->hydrate($datas); + } + + public function init(string $id, string $route, string $query, array $params = [], string $icon = '⭐') + { + $this->setid($id); + $this->setroute($route); + $this->setquery($query); + $this->setparams($params); + $this->seticon($icon); + } + + + + + // _____________________________ G E T __________________________________ + + + public function id() + { + return $this->id; + } + + public function query() + { + return $this->query; + } + + public function route() + { + return $this->route; + } + + public function params() + { + return $this->params; + } + + public function icon() + { + return $this->icon; + } + + // _____________________________ S E T __________________________________ + + public function setid($id) + { + if (is_string($id)) { + $this->id = idclean($id); + } + } + + public function setquery($query) + { + if (is_string($query)) { + $this->query = substr($query, 0, Model::MAX_QUERY_LENGH); + } + } + + public function setroute($route) + { + if ($route === 'home' || $route === 'media') { + $this->route = $route; + } + } + + public function setparams($params) + { + if (is_array($params)) { + $this->params = $params; + } + } + + public function seticon($icon) + { + if (is_string($icon)) { + $this->icon = substr(strip_tags($icon), 0, 16); + } + } +} diff --git a/app/class/Colors.php b/app/class/Colors.php index 87a5414..2e4af23 100644 --- a/app/class/Colors.php +++ b/app/class/Colors.php @@ -121,7 +121,7 @@ class Colors extends Item foreach ($this->tagcolor as $tag => $color) { $i = '<input type="color" name="tagcolor[' . $tag . ']" value="' . $color . '" id="color_' . $tag . '">'; $l = '<label for="color_' . $tag . '" >' . $tag . '</label>'; - $html .= "\n<li>" . $i . $l . '</li>'; + $html .= "\n<li>$i . $l</li>"; } $html .= PHP_EOL . '</ul>'; return $html; diff --git a/app/class/Config.php b/app/class/Config.php index 3c9afd9..45ba86f 100644 --- a/app/class/Config.php +++ b/app/class/Config.php @@ -21,7 +21,7 @@ abstract class Config protected static $privatepass = false; protected static $notpublishedpass = false; protected static $alertcss = false; - protected static $defaultbody = '%HEADER%\n\n%NAV%\n\n%ASIDE%\n\n%MAIN%\n\n%FOOTER%'; + protected static $defaultbody = "%HEADER%\n\n%NAV%\n\n%ASIDE%\n\n%MAIN%\n\n%FOOTER%"; protected static $defaultfavicon = ''; protected static $defaultthumbnail = ''; protected static $analytics = ''; diff --git a/app/class/Controller.php b/app/class/Controller.php index 34b3d11..46c9ee0 100644 --- a/app/class/Controller.php +++ b/app/class/Controller.php @@ -46,8 +46,8 @@ class Controller { $router = $this->router; $this->plates = new Engine(Model::TEMPLATES_DIR); - $this->plates->registerFunction('url', function (string $string, array $vars = []) { - return $this->generate($string, $vars); + $this->plates->registerFunction('url', function (string $string, array $vars = [], string $get = '') { + return $this->generate($string, $vars, $get); }); $this->plates->registerFunction('upage', function (string $string, string $id) { return $this->generate($string, ['page' => $id]); @@ -79,13 +79,14 @@ class Controller * * @param string $route The name of the route. * @param array $params Associative array of parameters to replace placeholders with. + * @param string $get Optionnal query GET parameters formated * @return string The URL of the route with named parameters in place. * @throws InvalidArgumentException If the route does not exist. */ - public function generate(string $route, array $params = []): string + public function generate(string $route, array $params = [], string $get = ''): string { try { - return $this->router->generate($route, $params); + return $this->router->generate($route, $params) . $get; } catch (Exception $e) { throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } diff --git a/app/class/Controllerhome.php b/app/class/Controllerhome.php index 6e2155a..1385652 100644 --- a/app/class/Controllerhome.php +++ b/app/class/Controllerhome.php @@ -182,9 +182,11 @@ class Controllerhome extends Controllerpage if ($this->user->iseditor() && isset($_POST['action']) && isset($_POST['id']) && !empty($_POST['id'])) { if ($_POST['action'] == 'add' && isset($_POST['query'])) { if (isset($_POST['user']) && $_POST['user'] == $this->user->id()) { + $bookmark = new Bookmark($_POST); + $bookmark->init($_POST['id'], 'home', $_POST['query'], [], $_POST['icon']); $usermanager = new Modeluser(); $user = $usermanager->get($_POST['user']); - $user->addbookmark($_POST['id'], $_POST['query']); + $user->addbookmark($bookmark); $usermanager->add($user); } else { Config::addbookmark($_POST['id'], $_POST['query']); diff --git a/app/class/Element.php b/app/class/Element.php index 638d624..7478256 100644 --- a/app/class/Element.php +++ b/app/class/Element.php @@ -46,7 +46,7 @@ class Element extends Item public function addtags() { - $this->content = '\n<' . $this->type() . '>\n' . $this->content() . '\n</' . $this->type() . '>\n'; + $this->content = "\n<' . $this->type() . '>\n' . $this->content() . '\n</' . $this->type() . '>\n"; } diff --git a/app/class/Item.php b/app/class/Item.php index a76e700..76fa78e 100644 --- a/app/class/Item.php +++ b/app/class/Item.php @@ -29,14 +29,47 @@ abstract class Item } } - public function dry() { $array = []; + $array = $this->obj2array($this, $array); + return $array; + } + + + public function obj2array($obj, &$arr) + { + if (!is_object($obj) && !is_array($obj)) { + $arr = $obj; + return $arr; + } + foreach ($obj as $key => $value) { + if (!empty($value)) { + $arr[$key] = array(); + $this->obj2array($value, $arr[$key]); + } else { + $arr[$key] = $value; + } + } + return $arr; + } + + public function dryold() + { + $array = []; foreach (get_object_vars($this) as $var => $value) { - $array[$var] = $this->$var(); + if (is_object($value) && is_subclass_of($value, get_class($this))) { + $array[$var] = $value->dry(); + } else { + if (method_exists($this, $var)) { + $array[$var] = $this->$var(); + } else { + $array[$var] = $value; + } + } } - return $array; + return get_object_vars($this); + // return $array; } diff --git a/app/class/Model.php b/app/class/Model.php index 692257a..7e891f1 100644 --- a/app/class/Model.php +++ b/app/class/Model.php @@ -80,6 +80,12 @@ abstract class Model '' => 'other' ]; + public const BOOKMARK_ICONS = [ + '🌘', '☂️', '⭐️', '✈️', '🚲', '💡', '💾', '💿', '💎', '🎞', ' ⚒', '💊', '📜', '📌', '🔍', '📦', '🔒', + '📒', '🔓', '🌡', '☎️', '🖤', '✝️', '☢️', '✅', '🌐', '🌍', '✳️', '🏴', '😎', '👻', '💩', '👍', '⚡️', '🍸', + '🍴', '⚽️', '🏭', '🚀', '⚓️' + ]; + public const LIST_STYLES = [ 'list' => 'list', 'card' => 'card' @@ -115,6 +121,7 @@ abstract class Model public const PASSWORD_MIN_LENGTH = 4; public const PASSWORD_MAX_LENGTH = 32; public const MAX_COOKIE_CONSERVATION = 365; + public const MAX_QUERY_LENGH = 256; /** RENDER OPTIONS */ // add class in html element indicating from witch page the content come. diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php index 7322955..1ad9e20 100644 --- a/app/class/Modelrender.php +++ b/app/class/Modelrender.php @@ -89,7 +89,7 @@ class Modelrender extends Modelpage $body = $this->getbody($this->readbody()); $parsebody = $this->bodyparser($body); - $html = '<!DOCTYPE html>\n<html>\n<head>\n' . $head . '\n</head>\n' . $parsebody . '\N</html>'; + $html = "<!DOCTYPE html>\n<html>\n<head>\n' . $head . '\n</head>\n' . $parsebody . '\N</html>"; return $html; } @@ -164,7 +164,7 @@ class Modelrender extends Modelpage if ($subcontent !== false) { if (empty($subcontent && self::RENDER_VERBOSE > 0)) { $message = 'The ' . strtoupper($type) . ' from page "' . $source . '" is currently empty !'; - $subcontent = '\n<!-- ' . $message . ' -->\n'; + $subcontent = "\n<!-- ' . $message . ' -->\n"; } } else { $read = '<h2>Rendering error :</h2>'; @@ -231,45 +231,51 @@ class Modelrender extends Modelpage public function gethead() { + $id = $this->page->id(); + $globalpath = Model::globalpath(); + $renderpath = Model::renderpath(); + $description = $this->page->description(); + $title = $this->page->title(); + $url = Config::url(); $head = ''; - $head .= '<meta charset="utf-8" />\n'; - $head .= '<title>' . $this->page->title() . '</title>\n'; + $head .= "<meta charset=\"utf-8\" />\n"; + $head .= "<title>$title</title>\n"; if (!empty($this->page->favicon())) { $href = Model::faviconpath() . $this->page->favicon(); - $head .= '<link rel="shortcut icon" href="' . $href . '" type="image/x-icon">'; + $head .= "<link rel=\"shortcut icon\" href=\"$href\" type=\"image/x-icon\">"; } elseif (!empty(Config::defaultfavicon())) { $href = Model::faviconpath() . Config::defaultfavicon(); - $head .= '<link rel="shortcut icon" href="' . $href . '" type="image/x-icon">'; + $head .= "<link rel=\"shortcut icon\" href=\"$href\" type=\"image/x-icon\">"; } - $head .= '<meta name="description" content="' . $this->page->description() . '" />\n'; - $head .= '<meta name="viewport" content="width=device-width" />\n'; + $head .= "<meta name=\"description\" content=\"$description\" />\n"; + $head .= "<meta name=\"viewport\" content=\"width=device-width\" />\n"; - $head .= '<meta property="og:title" content="' . $this->page->title() . '">\n'; - $head .= '<meta property="og:description" content="' . $this->page->description() . '">\n'; + $head .= "<meta property=\"og:title\" content=\"$title\">\n"; + $head .= "<meta property=\"og:description\" content=\"$description\">\n"; if (!empty($this->page->thumbnail())) { $content = Config::domain() . self::thumbnailpath() . $this->page->thumbnail(); - $head .= '<meta property="og:image" content="' . $content . '">\n'; + $head .= "<meta property=\"og:image\" content=\"$content\">\n"; } elseif (!empty(Config::defaultthumbnail())) { $content = Config::domain() . self::thumbnailpath() . Config::defaultthumbnail(); - $head .= '<meta property="og:image" content="' . $content . '">\n'; + $head .= "<meta property=\"og:image\" content=\"$content\">\n"; } - $head .= '<meta property="og:url" content="' . Config::url() . $this->page->id() . '/">\n'; + $head .= "<meta property=\"og:url\" content=\"$url . $id/\">\n"; foreach ($this->page->externalcss() as $externalcss) { - $head .= '<link href="' . $externalcss . '" rel="stylesheet" />\n'; + $head .= "<link href=\"$externalcss\" rel=\"stylesheet\" />\n"; } if (!empty($this->page->templatecss() && in_array('externalcss', $this->page->templateoptions()))) { $templatecss = $this->get($this->page->templatecss()); if ($templatecss !== false) { foreach ($templatecss->externalcss() as $externalcss) { - $head .= '<link href="' . $externalcss . '" rel="stylesheet" />\n'; + $head .= "<link href=\"$externalcss\" rel=\"stylesheet\" />\n"; } } } @@ -277,45 +283,45 @@ class Modelrender extends Modelpage $head .= PHP_EOL . $this->page->customhead() . PHP_EOL; - $head .= '<link href="' . Model::globalpath() . 'fonts.css" rel="stylesheet" />\n'; - $head .= '<link href="' . Model::globalpath() . 'global.css" rel="stylesheet" />\n'; + $head .= "<link href=\"{$globalpath}fonts.css\" rel=\"stylesheet\" />\n"; + $head .= "<link href=\"{$globalpath}global.css\" rel=\"stylesheet\" />\n"; if (!empty($this->page->templatecss())) { $tempaltecsspage = $this->page->templatecss(); - $head .= '<link href="' . Model::renderpath() . $tempaltecsspage . '.css" rel="stylesheet" />\n'; + $head .= "<link href=\"$renderpath . $tempaltecsspage.css\" rel=\"stylesheet\" />\n"; } - $head .= '<link href="' . Model::renderpath() . $this->page->id() . '.css" rel="stylesheet" />\n'; + $head .= "<link href=\"$renderpath . $id.css\" rel=\"stylesheet\" />\n"; if (!empty($this->page->templatejavascript())) { $templatejspage = $this->page->templatejavascript(); - $head .= '<script src="' . Model::renderpath() . $templatejspage . '.js" async/></script>\n'; + $head .= "<script src=\"$renderpath . $templatejspage.js\" async/></script>\n"; } if (!empty($this->page->javascript())) { - $head .= '<script src="' . Model::renderpath() . $this->page->id() . '.js" async/></script>\n'; + $head .= "<script src=\"$renderpath . $id.js\" async/></script>\n"; } if (!empty(Config::analytics())) { - $head .= '\n - <!-- Global site tag (gtag.js) - Google Analytics --> - <script async src="https://www.googletagmanager.com/gtag/js?id=' . Config::analytics() . '"></script> + $analitycs = Config::analytics(); + $head .= "\n + <!-- Global site tag (gtag.js) - Google Analytics --> + <script async src=\"https://www.googletagmanager.com/gtag/js?id=$analitycs\"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} - gtag(\'js\', new Date()); + gtag('js', new Date()); - gtag(\'config\', \'' . Config::analytics() . '\'); + gtag('config', 'Config::analytics()'); </script> - \n'; + \n"; } if (!empty($this->page->redirection())) { if (preg_match('%https?:\/\/\S*%', $this->page->redirection(), $out)) { $url = $out[0]; - $head .= '\n<meta http-equiv="refresh" content="' . $this->page->refresh() . '; URL=' . $url . '" />'; } elseif (key_exists($this->page->redirection(), $this->pagelist())) { $url = $this->upage($this->page->redirection()); - $head .= '\n<meta http-equiv="refresh" content="' . $this->page->refresh() . '; URL=' . $url . '" />'; } + $head .= "\n<meta http-equiv=\"refresh\" content=\"{$this->page->refresh()}; URL=$url\" />"; } @@ -503,7 +509,7 @@ class Modelrender extends Modelpage } else { $id = ' '; } - return '<article ' . $id . ' markdown="1" >\n\n' . $matches[3] . '\n\n</article>\n\n'; + return "<article $id markdown=\"1\" >\n\n$matches[3]\n\n</article>\n\n"; }, $text); $text = preg_replace('/\R\R[=]{3,}([\w-]*)\R/', '', $text); return $text; diff --git a/app/class/Modeluser.php b/app/class/Modeluser.php index 0e3cc5f..c9e38c6 100644 --- a/app/class/Modeluser.php +++ b/app/class/Modeluser.php @@ -55,7 +55,7 @@ class Modeluser extends Modeldb if (hash_equals($cookiemac, secrethash($dbtoken->getId()))) { $user = $this->get($dbtoken->user); if ($user !== false) { - $this->writesession($user, $_COOKIE['authtoken']); + $this->writesession($user); } return $user; } diff --git a/app/class/Opt.php b/app/class/Opt.php index df3cb50..24c7f90 100644 --- a/app/class/Opt.php +++ b/app/class/Opt.php @@ -165,7 +165,7 @@ class Opt extends Item $authorstring = ""; foreach ($authorlist as $author) { $href = $this->getfilteradress(['authorfilter' => [$author]]); - $authorstring .= '<a class="author author_' . $author . '" href="?' . $href . '" >' . $author . '</a>\n'; + $authorstring .= "<a class=\"author author_$author\" href=\"?$href\" >$author</a>\n"; } return $authorstring; } @@ -173,7 +173,7 @@ class Opt extends Item public function securelink(int $level, string $secure) { $href = $this->getfilteradress(['secure' => $level]); - return '<a class="secure ' . $secure . '" href="?' . $href . '">' . $secure . '</a>\n'; + return "<a class=\"secure $secure\" href=\"?$href\">$secure</a>\n"; } public function linktolink(array $linktolist) @@ -181,7 +181,7 @@ class Opt extends Item $linktostring = ""; foreach ($linktolist as $linkto) { $href = $this->getfilteradress(['linkto' => $linkto]); - $linktostring .= '<a class="linkto" href="?' . $href . '" >' . $linkto . '</a>\n'; + $linktostring .= "<a class=\"linkto\" href=\"?$href\" >$linkto</a>\n"; } return $linktostring; } diff --git a/app/class/Page.php b/app/class/Page.php index da149f7..43cf387 100644 --- a/app/class/Page.php +++ b/app/class/Page.php @@ -387,7 +387,7 @@ class Page extends Dbitem public function setid($id) { - if (strlen($id) <= Model::MAX_ID_LENGTH and is_string($id)) { + if (is_string($id) && strlen($id) <= Model::MAX_ID_LENGTH) { $this->id = strip_tags(strtolower(str_replace(" ", "", $id))); } } diff --git a/app/class/User.php b/app/class/User.php index a62091f..56e994d 100644 --- a/app/class/User.php +++ b/app/class/User.php @@ -16,6 +16,7 @@ class User extends Item protected $columns = ['title', 'datemodif', 'datecreation', 'secure', 'visitcount']; protected $connectcount = 0; protected $expiredate = false; + /** @var Bookmark[] Associative array as `id => Bookmark`*/ protected $bookmark = []; protected $display = ['bookmark' => false]; @@ -198,7 +199,15 @@ class User extends Item public function setbookmark($bookmark) { if (is_array($bookmark)) { - $this->bookmark = $bookmark; + $bookmark = array_map( + function ($datas) { + if (is_array($datas) && !empty($datas)) { + return new Bookmark($datas); + } + }, + $bookmark + ); + $this->bookmark = array_filter($bookmark); } } @@ -285,12 +294,10 @@ class User extends Item $this->connectcount ++; } - public function addbookmark(string $id, string $query) + public function addbookmark(Bookmark $bookmark) { - if (!empty($id) && !empty($query)) { - $id = idclean($id); - $id = substr($id, 0, 16); - $this->bookmark[$id] = $query; + if (!empty($bookmark->id()) && !empty($bookmark->query()) && !empty($bookmark->route())) { + $this->bookmark[$bookmark->id()] = $bookmark; } } |