From 9090550d241f9f7b3246b24bfd323bd988add749 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Tue, 28 Apr 2020 20:18:17 +0200 Subject: listen to brother Stan and clean some things --- app/class/Bookmark.php | 9 +- app/class/Config.php | 2 +- app/class/Controllermedia.php | 6 +- app/class/Controllerpage.php | 40 ------- app/class/Controlleruser.php | 4 +- app/class/Media.php | 6 +- app/class/Medialist.php | 236 ------------------------------------------ app/class/Mediaopt.php | 236 ++++++++++++++++++++++++++++++++++++++++++ app/class/Modelconfig.php | 28 ----- app/class/Modelfont.php | 6 +- app/class/Modelhome.php | 2 +- app/class/Modelmedia.php | 3 +- app/class/Modelpage.php | 1 - app/class/Modelrender.php | 14 ++- app/class/Page.php | 27 +---- app/class/Session.php | 2 +- app/class/User.php | 17 ++- app/fn/fn.php | 13 +-- app/view/templates/alert.php | 1 + app/view/templates/media.php | 6 +- 20 files changed, 278 insertions(+), 381 deletions(-) delete mode 100644 app/class/Medialist.php create mode 100644 app/class/Mediaopt.php delete mode 100644 app/class/Modelconfig.php (limited to 'app') diff --git a/app/class/Bookmark.php b/app/class/Bookmark.php index 5c4d2fc..8b4d2f7 100644 --- a/app/class/Bookmark.php +++ b/app/class/Bookmark.php @@ -74,12 +74,11 @@ class Bookmark extends Item public function setid($id): bool { if (is_string($id)) { - try { - $this->id = idclean($id, Model::MAX_ID_LENGTH, 1); - } catch (\Throwable $th) { - return false; + $id = idclean($id); + if (!empty($id)) { + $this->id = $id; + return true; } - return true; } return false; } diff --git a/app/class/Config.php b/app/class/Config.php index b629bc5..37321e4 100644 --- a/app/class/Config.php +++ b/app/class/Config.php @@ -81,7 +81,7 @@ abstract class Config public static function tojson() { - $arr = get_class_vars(__class__); + $arr = get_class_vars(get_class()); $json = json_encode($arr, JSON_FORCE_OBJECT | JSON_PRETTY_PRINT); return $json; } diff --git a/app/class/Controllermedia.php b/app/class/Controllermedia.php index a957df8..c6e5d6c 100644 --- a/app/class/Controllermedia.php +++ b/app/class/Controllermedia.php @@ -2,8 +2,8 @@ namespace Wcms; -use \Exception; -use \LogicException; +use Exception; +use LogicException; class Controllermedia extends Controller { @@ -34,7 +34,7 @@ class Controllermedia extends Controller throw new LogicException($exception->getMessage()); } - $mediaopt = new Medialist($_GET); + $mediaopt = new Mediaopt($_GET); if (empty($mediaopt->path())) { $mediaopt->setpath(DIRECTORY_SEPARATOR . Model::MEDIA_DIR); } diff --git a/app/class/Controllerpage.php b/app/class/Controllerpage.php index b5679a9..c7e1f98 100644 --- a/app/class/Controllerpage.php +++ b/app/class/Controllerpage.php @@ -13,8 +13,6 @@ class Controllerpage extends Controller protected $fontmanager; protected $mediamanager; - public const COMBINE = false; - public function __construct($router) { parent::__construct($router); @@ -368,9 +366,6 @@ class Controllerpage extends Controller $oldpage = clone $this->page; $this->page->hydrate($_POST); - if (self::COMBINE && $_POST['thisdatemodif'] === $oldpage->datemodif('string')) { - } - $this->page->updateedited(); $this->page->addauthor($this->user->id()); $this->page->removeeditby($this->user->id()); @@ -389,41 +384,6 @@ class Controllerpage extends Controller $this->routedirect('pageedit', ['page' => $this->page->id()]); } - /** - * This function set the actual editor of the page - * - * @param string $pageid as the page id - */ - public function editby(string $pageid) - { - $this->page = new Page(['id' => $pageid]); - if ($this->importpage($pageid)) { - $this->page->addeditby($this->user->id()); - $this->pagemanager->update($this->page); - echo json_encode(['success' => true]); - } else { - $this->error(400); - } - } - - /** - * This function remove the actual editor of the page - * - * @param string $pageid as the page id - */ - public function removeeditby(string $pageid) - { - $this->page = new Page(['id' => $pageid]); - if ($this->importpage($pageid)) { - $this->page->removeeditby($this->user->id()); - $this->pagemanager->update($this->page); - echo json_encode(['success' => true]); - } else { - $this->error(400); - } - } - - public function movepanels() { $_SESSION['workspace']['showrightpanel'] = isset($_POST['workspace']['showrightpanel']); diff --git a/app/class/Controlleruser.php b/app/class/Controlleruser.php index 755b491..53cf9b9 100644 --- a/app/class/Controlleruser.php +++ b/app/class/Controlleruser.php @@ -37,9 +37,9 @@ class Controlleruser extends Controller if ($this->user->iseditor()) { $user = $this->usermanager->get($this->user); try { - $user->hydrate($_POST, true); + $user->hydrateexception($_POST); } catch (\Throwable $th) { - Model::sendflashmessage('There was a problem when updating preferences : ' . $th->getMessage(), 'error'); + Model::sendflashmessage('There was a problem when updating preference : ' . $th->getMessage(), 'error'); } if ($_POST['passwordhash']) { $user->hashpassword(); diff --git a/app/class/Media.php b/app/class/Media.php index b08726d..7105830 100644 --- a/app/class/Media.php +++ b/app/class/Media.php @@ -265,7 +265,11 @@ class Media extends Item public function setdate() { $timestamp = filemtime($this->getfulldir()); - $this->date = new DateTimeImmutable("@$timestamp"); + try { + $this->date = new DateTimeImmutable("@$timestamp"); + } catch (\Throwable $th) { + $this->date = new DateTimeImmutable(); + } } public function setwidth($width) diff --git a/app/class/Medialist.php b/app/class/Medialist.php deleted file mode 100644 index e716845..0000000 --- a/app/class/Medialist.php +++ /dev/null @@ -1,236 +0,0 @@ -type = Model::mediatypes(); - $this->hydrate($datas); - } - - public function readoptions() - { - parse_str($this->options, $datas); - $this->hydrate($datas); - } - - public function generatecontent() - { - $mediamanager = new Modelmedia(); - $medialist = $mediamanager->getlistermedia($this->dir(), $this->type); - if (!$medialist) { - return false; - } else { - $mediamanager->medialistsort($medialist, $this->sortby, $this->order); - - $dirid = str_replace('/', '-', $this->path); - - $div = '
' . PHP_EOL; - - foreach ($medialist as $media) { - $div .= '
'; - $id = 'id="media_' . $media->id() . '"'; - $path = $media->getincludepath(); - $ext = $media->extension(); - if ($media->type() == 'image') { - $div .= '' . $media->id() . ''; - } elseif ($media->type() == 'sound') { - $div .= '
' . PHP_EOL; - } - - $div .= '
' . PHP_EOL; - - return $div; - } - } - - /** - * Generate link adress for table header - * - * @param string $sortby - * @return string link adress - */ - public function getsortbyadress(string $sortby): string - { - if (!in_array($sortby, Model::MEDIA_SORTBY)) { - $sortby = 'id'; - } - if ($this->sortby === $sortby) { - $order = $this->order * -1; - } else { - $order = $this->order; - } - $query = ['path' => $this->path, 'sortby' => $sortby, 'order' => $order]; - if (array_diff(self::TYPES, $this->type) != []) { - $query['type'] = $this->type; - } - return '?' . urldecode(http_build_query($query)); - } - - public function getpathadress(string $path): string - { - $query = ['path' => '/' . $path, 'sortby' => $this->sortby, 'order' => $this->order]; - if (array_diff(self::TYPES, $this->type) != []) { - $query['type'] = $this->type; - } - return '?' . urldecode(http_build_query($query)); - } - - public function getquery() - { - $query = ['path' => $this->path, 'sortby' => $this->sortby, 'order' => $this->order]; - if (array_diff(self::TYPES, $this->type) != []) { - $query['type'] = $this->type; - } - return urldecode(http_build_query($query)); - } - - /** - * Get the code to insert directly - */ - public function getcode(): string - { - return '%MEDIA?' . $this->getquery() . '%'; - } - - public function getadress(): string - { - return '?' . $this->getquery(); - } - - - // ______________________________________________ G E T ________________________________________________________ - - - public function fullmatch() - { - return $this->fullmatch; - } - - public function options() - { - return $this->options; - } - - /** - * @return string formated like `/media/` - */ - public function path() - { - return $this->path; - } - - /** - * @return string formated like `media//` - */ - public function dir() - { - return ltrim($this->path, '/') . '/'; - } - - public function sortby() - { - return $this->sortby; - } - - public function order() - { - return $this->order; - } - - public function type() - { - return $this->type; - } - - // ______________________________________________ S E T ________________________________________________________ - - - public function setfullmatch(string $fullmatch) - { - $this->fullmatch = $fullmatch; - } - - - public function setoptions(string $options) - { - if (!empty($options)) { - $this->options = $options; - } - } - - public function setpath(string $path) - { - if (preg_match('%^\/' . rtrim(Model::MEDIA_DIR, DIRECTORY_SEPARATOR) . '%', $path)) { - $this->path = rtrim($path, DIRECTORY_SEPARATOR); - } elseif (!preg_match('%^\/%', $path)) { - $this->path = '/' . Model::MEDIA_DIR . rtrim($path, DIRECTORY_SEPARATOR); - } - } - - public function setsortby(string $sortby) - { - if (in_array($sortby, Model::MEDIA_SORTBY)) { - $this->sortby = $sortby; - } - } - - public function setorder(int $order) - { - if ($order === -1 || $order === 1) { - $this->order = $order; - } - } - - public function settype($type) - { - if (is_array($type)) { - $this->type = array_intersect(Model::mediatypes(), array_unique($type)); - } - } -} diff --git a/app/class/Mediaopt.php b/app/class/Mediaopt.php new file mode 100644 index 0000000..8bed1e0 --- /dev/null +++ b/app/class/Mediaopt.php @@ -0,0 +1,236 @@ +type = Model::mediatypes(); + $this->hydrate($datas); + } + + public function readoptions() + { + parse_str($this->options, $datas); + $this->hydrate($datas); + } + + public function generatecontent() + { + $mediamanager = new Modelmedia(); + $medialist = $mediamanager->getlistermedia($this->dir(), $this->type); + if (!$medialist) { + return false; + } else { + $mediamanager->medialistsort($medialist, $this->sortby, $this->order); + + $dirid = str_replace('/', '-', $this->path); + + $div = '
' . PHP_EOL; + + foreach ($medialist as $media) { + $div .= '
'; + $id = 'id="media_' . $media->id() . '"'; + $path = $media->getincludepath(); + $ext = $media->extension(); + if ($media->type() == 'image') { + $div .= '' . $media->id() . ''; + } elseif ($media->type() == 'sound') { + $div .= '
' . PHP_EOL; + } + + $div .= '
' . PHP_EOL; + + return $div; + } + } + + /** + * Generate link adress for table header + * + * @param string $sortby + * @return string link adress + */ + public function getsortbyadress(string $sortby): string + { + if (!in_array($sortby, Model::MEDIA_SORTBY)) { + $sortby = 'id'; + } + if ($this->sortby === $sortby) { + $order = $this->order * -1; + } else { + $order = $this->order; + } + $query = ['path' => $this->path, 'sortby' => $sortby, 'order' => $order]; + if (array_diff(self::TYPES, $this->type) != []) { + $query['type'] = $this->type; + } + return '?' . urldecode(http_build_query($query)); + } + + public function getpathadress(string $path): string + { + $query = ['path' => '/' . $path, 'sortby' => $this->sortby, 'order' => $this->order]; + if (array_diff(self::TYPES, $this->type) != []) { + $query['type'] = $this->type; + } + return '?' . urldecode(http_build_query($query)); + } + + public function getquery() + { + $query = ['path' => $this->path, 'sortby' => $this->sortby, 'order' => $this->order]; + if (array_diff(self::TYPES, $this->type) != []) { + $query['type'] = $this->type; + } + return urldecode(http_build_query($query)); + } + + /** + * Get the code to insert directly + */ + public function getcode(): string + { + return '%MEDIA?' . $this->getquery() . '%'; + } + + public function getadress(): string + { + return '?' . $this->getquery(); + } + + + // ______________________________________________ G E T ________________________________________________________ + + + public function fullmatch() + { + return $this->fullmatch; + } + + public function options() + { + return $this->options; + } + + /** + * @return string formated like `/media/` + */ + public function path() + { + return $this->path; + } + + /** + * @return string formated like `media//` + */ + public function dir() + { + return ltrim($this->path, '/') . '/'; + } + + public function sortby() + { + return $this->sortby; + } + + public function order() + { + return $this->order; + } + + public function type() + { + return $this->type; + } + + // ______________________________________________ S E T ________________________________________________________ + + + public function setfullmatch(string $fullmatch) + { + $this->fullmatch = $fullmatch; + } + + + public function setoptions(string $options) + { + if (!empty($options)) { + $this->options = $options; + } + } + + public function setpath(string $path) + { + if (preg_match('%^\/' . rtrim(Model::MEDIA_DIR, DIRECTORY_SEPARATOR) . '%', $path)) { + $this->path = rtrim($path, DIRECTORY_SEPARATOR); + } elseif (!preg_match('%^\/%', $path)) { + $this->path = '/' . Model::MEDIA_DIR . rtrim($path, DIRECTORY_SEPARATOR); + } + } + + public function setsortby(string $sortby) + { + if (in_array($sortby, Model::MEDIA_SORTBY)) { + $this->sortby = $sortby; + } + } + + public function setorder(int $order) + { + if ($order === -1 || $order === 1) { + $this->order = $order; + } + } + + public function settype($type) + { + if (is_array($type)) { + $this->type = array_intersect(Model::mediatypes(), array_unique($type)); + } + } +} diff --git a/app/class/Modelconfig.php b/app/class/Modelconfig.php deleted file mode 100644 index 6c074eb..0000000 --- a/app/class/Modelconfig.php +++ /dev/null @@ -1,28 +0,0 @@ - $page) { - if ($showorphans || (!$showorphans && in_array($id, $notorphans))) { + if ($showorphans || in_array($id, $notorphans)) { $node['group'] = 'nodes'; $node['data']['id'] = $page->id(); $node['data']['edit'] = $page->id() . DIRECTORY_SEPARATOR . 'edit'; diff --git a/app/class/Modelmedia.php b/app/class/Modelmedia.php index 2894783..0b1d700 100644 --- a/app/class/Modelmedia.php +++ b/app/class/Modelmedia.php @@ -31,7 +31,7 @@ class Modelmedia extends Model } } - public function medialistopt(Medialist $mediaopt) + public function medialistopt(Mediaopt $mediaopt) { $medialist = $this->getlistermedia($mediaopt->dir(), $mediaopt->type()); $this->medialistsort($medialist, $mediaopt->sortby(), $mediaopt->order()); @@ -179,6 +179,7 @@ class Modelmedia extends Model $this->listpath($content, $parent . $dir . DIRECTORY_SEPARATOR, $pathlist); } } + return $pathlist; } /** diff --git a/app/class/Modelpage.php b/app/class/Modelpage.php index 834ba01..f6b0c8e 100644 --- a/app/class/Modelpage.php +++ b/app/class/Modelpage.php @@ -308,7 +308,6 @@ class Modelpage extends Modeldb /** * @param array $pagelist List of Page * @param int $secure secure level - * * @return array $array of `string` page id */ diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php index 4722057..fd8531a 100644 --- a/app/class/Modelrender.php +++ b/app/class/Modelrender.php @@ -17,8 +17,6 @@ class Modelrender extends Modelpage protected $internallinkblank = ''; protected $externallinkblank = ''; - public const RENDER_VERBOSE = 1; - public function __construct(\AltoRouter $router) { parent::__construct(); @@ -162,7 +160,7 @@ class Modelrender extends Modelpage if ($source !== $this->page->id()) { $subcontent = $this->getpageelement($source, $type); if ($subcontent !== false) { - if (empty($subcontent && self::RENDER_VERBOSE > 0)) { + if (empty($subcontent)) { $message = 'The ' . strtoupper($type) . ' from page "' . $source . '" is currently empty !'; $subcontent = "\n\n"; } @@ -216,12 +214,12 @@ class Modelrender extends Modelpage public function writetemplates() { - if (array_key_exists('css', $this->page->template('array'))) { - $tempaltecsspage = $this->get($this->page->template('array')['css']); + if (array_key_exists('css', $this->page->template())) { + $tempaltecsspage = $this->get($this->page->template()['css']); file_put_contents(Model::RENDER_DIR . $tempaltecsspage->id() . '.css', $tempaltecsspage->css()); } - if (array_key_exists('javascript', $this->page->template('array'))) { - $templatejspage = $this->get($this->page->template('array')['javascript']); + if (array_key_exists('javascript', $this->page->template())) { + $templatejspage = $this->get($this->page->template()['javascript']); file_put_contents(Model::RENDER_DIR . $templatejspage->id() . '.js', $templatejspage->javascript()); } } @@ -547,7 +545,7 @@ class Modelrender extends Modelpage if (!empty($matches)) { foreach ($matches as $match) { - $medialist = new Medialist($match); + $medialist = new Mediaopt($match); $medialist->readoptions(); $text = str_replace($medialist->fullmatch(), $medialist->generatecontent(), $text); } diff --git a/app/class/Page.php b/app/class/Page.php index 43cf387..b1c6b49 100644 --- a/app/class/Page.php +++ b/app/class/Page.php @@ -234,22 +234,11 @@ class Page extends Dbitem if ($this->secure == 2) { $secure = 'not_published'; } - return $secure; } else { return $this->secure; } } - public function invitepassword($type = 'string') - { - return $this->invitepassword; - } - - public function readpassword($type = 'string') - { - return $this->readpassword; - } - public function interface($type = 'string') { return $this->interface; @@ -266,7 +255,7 @@ class Page extends Dbitem } elseif ($option == 'string') { return implode(', ', $this->linkto); } - return $linkto; + return $this->linkto; } public function templatebody($type = 'string') @@ -576,20 +565,6 @@ class Page extends Dbitem } } - public function setinvitepassword($invitepassword) - { - if (is_string($invitepassword) && strlen($invitepassword) < self::LEN) { - $this->invitepassword = $invitepassword; - } - } - - public function setreadpassword($readpassword) - { - if (is_string($readpassword) && strlen($readpassword) < self::LEN) { - $this->readpassword = $readpassword; - } - } - public function setinterface($interface) { if (in_array($interface, self::TABS)) { diff --git a/app/class/Session.php b/app/class/Session.php index 96a4ffd..64b6b26 100644 --- a/app/class/Session.php +++ b/app/class/Session.php @@ -78,4 +78,4 @@ class Session extends Item $this->mediadisplay = $mediadisplay; } } -} \ No newline at end of file +} diff --git a/app/class/User.php b/app/class/User.php index 417b5f3..b2b6193 100644 --- a/app/class/User.php +++ b/app/class/User.php @@ -39,13 +39,9 @@ class User extends Item return $this->level; } - public function password($type = 'string') + public function password() { - if ($type === 'int') { - return strlen($this->password); - } elseif ($type = 'string') { - return $this->password; - } + return $this->password; } public function signature() @@ -117,12 +113,11 @@ class User extends Item public function setid($id): bool { if (is_string($id)) { - try { - $this->id = idclean($id, Model::MAX_ID_LENGTH, 1); - } catch (\Throwable $th) { - return false; + $id = idclean($id); + if (!empty($id)) { + $this->id = $id; + return true; } - return true; } return false; } diff --git a/app/fn/fn.php b/app/fn/fn.php index afadae8..103062a 100644 --- a/app/fn/fn.php +++ b/app/fn/fn.php @@ -1,6 +1,6 @@ ' . $dirname . ''; diff --git a/app/view/templates/alert.php b/app/view/templates/alert.php index 4ee71d3..1672443 100644 --- a/app/view/templates/alert.php +++ b/app/view/templates/alert.php @@ -34,6 +34,7 @@ $this->stop(); $form = '

+ diff --git a/app/view/templates/media.php b/app/view/templates/media.php index a5b8c68..960ff33 100644 --- a/app/view/templates/media.php +++ b/app/view/templates/media.php @@ -69,7 +69,7 @@ $this->layout('layout', ['title' => 'media', 'stylesheets' => [$css . 'home.css'

/dir() ?> - >list / >gallery + >list / >gallery

@@ -84,8 +84,7 @@ $this->layout('layout', ['title' => 'media', 'stylesheets' => [$css . 'home.css'