aboutsummaryrefslogtreecommitdiff
path: root/app/class
diff options
context:
space:
mode:
Diffstat (limited to 'app/class')
-rw-r--r--app/class/Bookmark.php2
-rw-r--r--app/class/Controller.php20
-rw-r--r--app/class/Controllerconnect.php6
-rw-r--r--app/class/Controllermedia.php19
-rw-r--r--app/class/Dbitem.php2
-rw-r--r--app/class/Item.php71
-rw-r--r--app/class/Modeluser.php20
-rw-r--r--app/class/Session.php81
-rw-r--r--app/class/User.php2
9 files changed, 156 insertions, 67 deletions
diff --git a/app/class/Bookmark.php b/app/class/Bookmark.php
index 9e038aa..5c4d2fc 100644
--- a/app/class/Bookmark.php
+++ b/app/class/Bookmark.php
@@ -26,7 +26,7 @@ class Bookmark extends Item
*/
public function __construct(array $datas = [])
{
- $this->hydrate($datas, true);
+ $this->hydrateexception($datas);
}
public function init(string $id, string $route, string $query, array $params = [], string $icon = '⭐')
diff --git a/app/class/Controller.php b/app/class/Controller.php
index 7bfa3a2..56d4dfe 100644
--- a/app/class/Controller.php
+++ b/app/class/Controller.php
@@ -7,9 +7,13 @@ use DateTimeImmutable;
use Exception;
use InvalidArgumentException;
use League\Plates\Engine;
+use Throwable;
class Controller
{
+ /** @var Session */
+ protected $session;
+
/** @var User */
protected $user;
@@ -29,6 +33,9 @@ class Controller
public function __construct($router)
{
+ $this->session = new Session($_SESSION['user' . Config::basepath()] ?? []);
+ $this->usermanager = new Modeluser();
+
$this->setuser();
$this->router = $router;
$this->pagemanager = new Modelpage();
@@ -38,8 +45,17 @@ class Controller
public function setuser()
{
- $this->usermanager = new Modeluser();
- $this->user = $this->usermanager->readsession();
+ if (empty($this->session->user)) {
+ $this->user = new User();
+ } else {
+ if (!$this->user = $this->usermanager->get($this->session->user)) {
+ if (!$this->user = $this->usermanager->readcookie()) {
+ $this->user = new User();
+ } else {
+ $this->session->addtosession('user', $this->user->id());
+ }
+ }
+ }
}
public function initplates()
diff --git a/app/class/Controllerconnect.php b/app/class/Controllerconnect.php
index 9dc788b..8347a8f 100644
--- a/app/class/Controllerconnect.php
+++ b/app/class/Controllerconnect.php
@@ -46,9 +46,7 @@ class Controllerconnect extends Controller
) {
$this->user->connectcounter();
$this->usermanager->add($this->user);
- $this->usermanager->writesession($this->user);
- $_SESSION['workspace']['showleftpanel'] = true;
- $_SESSION['workspace']['showrightpanel'] = false;
+ $this->session->addtosession('user', $this->user->id());
if ($_POST['rememberme'] && $this->user->cookie() > 0) {
$token = $this->createauthtoken();
@@ -69,7 +67,7 @@ class Controllerconnect extends Controller
public function logout($route, $id = null)
{
$this->user = $this->usermanager->logout();
- $this->usermanager->writesession($this->user);
+ $this->session->addtosession('user', '');
if (!empty($_SESSION['user' . Config::basepath()]['authtoken'])) {
$this->destroyauthtoken($_SESSION['user' . Config::basepath()]['authtoken']);
}
diff --git a/app/class/Controllermedia.php b/app/class/Controllermedia.php
index 6a585eb..a957df8 100644
--- a/app/class/Controllermedia.php
+++ b/app/class/Controllermedia.php
@@ -47,16 +47,17 @@ class Controllermedia extends Controller
$pathlist = [];
$this->mediamanager->listpath($dirlist, '', $pathlist);
+ if (isset($_GET['display'])) {
+ $this->session->addtosession('mediadisplay', $_GET['display']);
+ }
+
+ $vars['display'] = $this->session->mediadisplay;
+ $vars['medialist'] = $medialist;
+ $vars['dirlist'] = $dirlist;
+ $vars['pathlist'] = $pathlist;
+ $vars['mediaopt'] = $mediaopt;
- $this->showtemplate(
- 'media',
- [
- 'medialist' => $medialist,
- 'dirlist' => $dirlist,
- 'pathlist' => $pathlist,
- 'mediaopt' => $mediaopt
- ]
- );
+ $this->showtemplate('media', $vars);
} else {
$this->routedirect('media');
}
diff --git a/app/class/Dbitem.php b/app/class/Dbitem.php
index 29f7a2c..f8bc4b7 100644
--- a/app/class/Dbitem.php
+++ b/app/class/Dbitem.php
@@ -8,7 +8,7 @@ use DateTimeImmutable;
abstract class Dbitem extends Item
{
- public function dry()
+ public function dry(): array
{
$array = [];
foreach (get_object_vars($this) as $var => $value) {
diff --git a/app/class/Item.php b/app/class/Item.php
index 44e383a..091516c 100644
--- a/app/class/Item.php
+++ b/app/class/Item.php
@@ -14,11 +14,39 @@ abstract class Item
/**
* Hydrate Object with corresponding `set__VAR__`
* @param array|object $datas associative array using key as var name or object
- * @param bool $sendexception throw exception if error setting variable
* @return bool true if no error, otherwise false
+ */
+ public function hydrate($datas = []): bool
+ {
+ $errors = $this->hydratejob($datas);
+ if (in_array(false, $errors)) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * Hydrate Object with corresponding `set__VAR__`
+ * @param array|object $datas associative array using key as var name or object
* @throws RuntimeException listing var settings errors
*/
- public function hydrate($datas = [], bool $sendexception = false): bool
+ public function hydrateexception($datas = [])
+ {
+ $errors = $this->hydratejob($datas);
+ if (!empty($errors)) {
+ $errors = implode(', ', $errors);
+ $class = get_class($this);
+ throw new RuntimeException("objects vars : $errors can't be set in $class object");
+ }
+ }
+
+ /**
+ * Concrete action of hydrate
+ * @param mixed $datas
+ * @return array associative array where key are methods and value is bool
+ */
+ public function hydratejob($datas = []): array
{
$seterrors = [];
if (is_array($datas) || is_object($datas)) {
@@ -31,26 +59,23 @@ abstract class Item
}
}
}
- if (!empty($seterrors)) {
- if ($sendexception) {
- $errors = implode(', ', $seterrors);
- $class = get_class($this);
- throw new RuntimeException("objects vars : $errors can't be set in $class object");
- }
- return false;
- } else {
- return true;
- }
+ return $seterrors;
}
- public function dry()
+ /**
+ * Export whole object
+ * @return array Associative array
+ */
+ public function dry(): array
{
$array = [];
$array = $this->obj2array($this, $array);
return $array;
}
-
+ /**
+ * Reccursive transform obj vars to array
+ */
public function obj2array($obj, &$arr)
{
if (!is_object($obj) && !is_array($obj)) {
@@ -68,24 +93,6 @@ abstract class Item
return $arr;
}
- public function dryold()
- {
- $array = [];
- foreach (get_object_vars($this) as $var => $value) {
- 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 get_object_vars($this);
- // return $array;
- }
-
/**
* Return any asked vars and their values of an object as associative array
diff --git a/app/class/Modeluser.php b/app/class/Modeluser.php
index c9e38c6..256ad55 100644
--- a/app/class/Modeluser.php
+++ b/app/class/Modeluser.php
@@ -33,19 +33,9 @@ class Modeluser extends Modeldb
$_SESSION['user' . Config::basepath()]['columns'] = $user->columns();
}
- public function readsession()
+
+ public function readcookie()
{
- $userdatas = [];
- if (
- array_key_exists('user' . Config::basepath(), $_SESSION)
- && isset($_SESSION['user' . Config::basepath()]['id'])
- ) {
- $userdatas = $_SESSION['user' . Config::basepath()];
- $user = new User($userdatas);
- $user = $this->get($user);
- return $user;
- }
-
if (isset($_COOKIE['authtoken']) && strpos($_COOKIE['authtoken'], ':')) {
list($cookietoken, $cookiemac) = explode(':', $_COOKIE['authtoken']);
$authtokenmanager = new Modelauthtoken();
@@ -54,15 +44,11 @@ class Modeluser extends Modeldb
if ($dbtoken !== false) {
if (hash_equals($cookiemac, secrethash($dbtoken->getId()))) {
$user = $this->get($dbtoken->user);
- if ($user !== false) {
- $this->writesession($user);
- }
return $user;
}
}
}
-
- return new User(['id' => '', 'level' => 0]);
+ return false;
}
diff --git a/app/class/Session.php b/app/class/Session.php
new file mode 100644
index 0000000..96a4ffd
--- /dev/null
+++ b/app/class/Session.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Wcms;
+
+use RuntimeException;
+
+class Session extends Item
+{
+ public $visitor = false;
+ public $user = '';
+ public $showleftpanel = true;
+ public $showrightpanel = false;
+ public $homedisplay = 'list';
+ public $mediadisplay = 'list';
+
+ public function __construct($datas = [])
+ {
+ $this->hydrate($datas);
+ }
+
+ public function writesession()
+ {
+ $_SESSION['user' . Config::basepath()] = $this->dry();
+ }
+
+ /**
+ * Ajust a session variable
+ * @param string $var
+ * @param mixed $value
+ * @return bool if var exist
+ */
+ public function addtosession(string $var, $value): bool
+ {
+ $method = 'set' . $var;
+ if (method_exists($this, $method)) {
+ $this->$method($value);
+ $_SESSION['user' . Config::basepath()][$var] = $this->$var;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ // _________________________ S E T ________________________
+
+ public function setvisitor($visitor)
+ {
+ $this->visitor = boolval($visitor);
+ }
+
+ public function setuser($id)
+ {
+ if (is_string($id)) {
+ $this->user = strip_tags($id);
+ }
+ }
+
+ public function setshowleftpanel($showleftpanel)
+ {
+ $this->showleftpanel = boolval($showleftpanel);
+ }
+
+ public function setshowrightpanel($showrightpanel)
+ {
+ $this->showrightpanel = boolval($showrightpanel);
+ }
+
+ public function sethomedisplay($homedisplay)
+ {
+ if (in_array($homedisplay, ['list', 'map'])) {
+ $this->homedisplay = $homedisplay;
+ }
+ }
+
+ public function setmediadisplay($mediadisplay)
+ {
+ if (in_array($mediadisplay, ['list', 'gallery'])) {
+ $this->mediadisplay = $mediadisplay;
+ }
+ }
+} \ No newline at end of file
diff --git a/app/class/User.php b/app/class/User.php
index 3462ee7..417b5f3 100644
--- a/app/class/User.php
+++ b/app/class/User.php
@@ -7,7 +7,7 @@ use DateTimeZone;
class User extends Item
{
- protected $id;
+ protected $id = '';
protected $level = 0;
protected $signature = '';
protected $password;