aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/class/Colors.php4
-rw-r--r--app/class/Controller.php2
-rw-r--r--app/class/Controllerconnect.php7
-rw-r--r--app/class/Controllermedia.php6
-rw-r--r--app/class/Controllerpage.php3
-rw-r--r--app/class/Dbitem.php2
-rw-r--r--app/class/Event.php7
-rw-r--r--app/class/Flywheel/Formatter/JSON.php6
-rw-r--r--app/class/Item.php6
-rw-r--r--app/class/Logger.php54
-rw-r--r--app/class/Media.php1
-rw-r--r--app/class/Modelhome.php6
-rw-r--r--app/class/Modelmedia.php2
-rw-r--r--app/class/Modelpage.php5
-rw-r--r--app/class/Modelrender.php13
-rw-r--r--app/class/Modeltimeline.php5
-rw-r--r--app/class/Page.php15
-rw-r--r--app/class/User.php2
-rw-r--r--app/fn/fn.php4
19 files changed, 96 insertions, 54 deletions
diff --git a/app/class/Colors.php b/app/class/Colors.php
index 3c6fb19..87a5414 100644
--- a/app/class/Colors.php
+++ b/app/class/Colors.php
@@ -5,7 +5,7 @@ namespace Wcms;
class Colors extends Item
{
- protected $file = 'tagcolors.css';
+ protected $file = Model::CSS_DIR . 'tagcolors.css';
protected $rawcss = "";
@@ -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 .= '<li>' . $i . $l . '</li>';
+ $html .= "\n<li>" . $i . $l . '</li>';
}
$html .= PHP_EOL . '</ul>';
return $html;
diff --git a/app/class/Controller.php b/app/class/Controller.php
index 4185be8..34b3d11 100644
--- a/app/class/Controller.php
+++ b/app/class/Controller.php
@@ -33,7 +33,7 @@ class Controller
$this->router = $router;
$this->pagemanager = new Modelpage();
$this->initplates();
- $this->now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $this->now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
}
public function setuser()
diff --git a/app/class/Controllerconnect.php b/app/class/Controllerconnect.php
index 24b0c3c..9dc788b 100644
--- a/app/class/Controllerconnect.php
+++ b/app/class/Controllerconnect.php
@@ -83,7 +83,7 @@ class Controllerconnect extends Controller
/**
* Create a token stored in the database and then a cookie
*
- * @return string|bool Token in cas of success, otherwise, false.
+ * @return string|false Token in cas of success, otherwise, false.
*/
public function createauthtoken()
{
@@ -95,9 +95,8 @@ class Controllerconnect extends Controller
if ($cookiecreation) {
return $tokenid;
}
- } else {
- return false;
}
+ return false;
}
/**
@@ -112,7 +111,7 @@ class Controllerconnect extends Controller
{
$hash = secrethash($token);
$cookie = $token . ':' . $hash;
- return setcookie('authtoken', $cookie, time() + $conservation * 24 * 3600, null, null, false, true);
+ return setcookie('authtoken', $cookie, time() + $conservation * 24 * 3600, "", "", false, true);
}
/**
diff --git a/app/class/Controllermedia.php b/app/class/Controllermedia.php
index 06885b3..95b4f9b 100644
--- a/app/class/Controllermedia.php
+++ b/app/class/Controllermedia.php
@@ -18,6 +18,9 @@ class Controllermedia extends Controller
$this->mediamanager = new Modelmedia();
}
+ /**
+ * @throws Exception
+ */
public function desktop()
{
if ($this->user->iseditor()) {
@@ -81,8 +84,9 @@ class Controllermedia extends Controller
$dir = $_POST['dir'] ?? Model::MEDIA_DIR;
$name = idclean($_POST['foldername']) ?? 'new-folder';
$this->mediamanager->adddir($dir, $name);
+ $this->redirect($this->generate('media') . '?path=/' . $dir . DIRECTORY_SEPARATOR . $name);
}
- $this->redirect($this->generate('media') . '?path=/' . $dir . DIRECTORY_SEPARATOR . $name);
+ $this->routedirect('home');
}
public function folderdelete()
diff --git a/app/class/Controllerpage.php b/app/class/Controllerpage.php
index 9d20ded..ad6bf82 100644
--- a/app/class/Controllerpage.php
+++ b/app/class/Controllerpage.php
@@ -99,7 +99,7 @@ class Controllerpage extends Controller
*/
public function renderpage(Page $page): Page
{
- $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
$renderengine = new Modelrender($this->router);
@@ -128,6 +128,7 @@ class Controllerpage extends Controller
$this->setpage($id, 'pageread/');
$pageexist = $this->importpage();
+ $canread = false;
if ($pageexist) {
$canread = $this->user->level() >= $this->page->secure();
diff --git a/app/class/Dbitem.php b/app/class/Dbitem.php
index 44dec2c..29f7a2c 100644
--- a/app/class/Dbitem.php
+++ b/app/class/Dbitem.php
@@ -11,7 +11,7 @@ abstract class Dbitem extends Item
public function dry()
{
$array = [];
- foreach ($this as $var => $value) {
+ foreach (get_object_vars($this) as $var => $value) {
if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
$array[$var] = $this->$var('string');
} else {
diff --git a/app/class/Event.php b/app/class/Event.php
index 3317a1b..069050a 100644
--- a/app/class/Event.php
+++ b/app/class/Event.php
@@ -40,7 +40,7 @@ class Event extends Dbitem
public function stamp()
{
- $this->date = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $this->date = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
$this->user = idclean($this->user);
if (in_array($this->type, self::EVENT_ART)) {
$this->target = idclean($this->target);
@@ -66,16 +66,13 @@ class Event extends Dbitem
switch ($type) {
case 'datetime':
return $this->date;
- break;
case 'string':
return $this->date->format(DateTime::ISO8601);
- break;
case 'hrdi':
- $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
return hrdi($this->date->diff($now));
- break;
}
}
diff --git a/app/class/Flywheel/Formatter/JSON.php b/app/class/Flywheel/Formatter/JSON.php
index d55c3b6..108c756 100644
--- a/app/class/Flywheel/Formatter/JSON.php
+++ b/app/class/Flywheel/Formatter/JSON.php
@@ -10,13 +10,11 @@ class JSON implements \JamesMoss\Flywheel\Formatter\FormatInterface
}
public function encode(array $data)
{
- $options = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : null;
- $options .= JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE;
+ $options = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE : null;
return json_encode($data, $options);
}
public function decode($data)
{
- $options = defined('JSON_OBJECT_AS_ARRAY') ? JSON_OBJECT_AS_ARRAY : null;
- return json_decode($data, $options);
+ return json_decode($data, true);
}
}
diff --git a/app/class/Item.php b/app/class/Item.php
index 8a8f2d6..a76e700 100644
--- a/app/class/Item.php
+++ b/app/class/Item.php
@@ -33,7 +33,7 @@ abstract class Item
public function dry()
{
$array = [];
- foreach ($this as $var => $value) {
+ foreach (get_object_vars($this) as $var => $value) {
$array[$var] = $this->$var();
}
return $array;
@@ -74,13 +74,13 @@ abstract class Item
} elseif ($option == 'date' || $option == 'sort') {
return $this->$property;
} elseif ($option == 'hrdi') {
- $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
return hrdi($this->$property->diff($now));
} elseif ($option == 'pdate') {
return $this->$property->format('Y-m-d');
} elseif ($option == 'ptime') {
return $this->$property->format('H:i');
- } elseif ($option = 'dmy') {
+ } elseif ($option == 'dmy') {
return $this->$property->format('d/m/Y');
}
} else {
diff --git a/app/class/Logger.php b/app/class/Logger.php
index 5d2695c..e2ab4b6 100644
--- a/app/class/Logger.php
+++ b/app/class/Logger.php
@@ -2,7 +2,7 @@
namespace Wcms;
-use InvalidArgumentException;
+use RuntimeException;
use Throwable;
/**
@@ -19,17 +19,18 @@ class Logger
*
* @param string $path the logfile's path
* @param int $verbosity 0: no log, 1: errors only, 2: add warn, 3: add info, 4: add debug.
+ * @throws RuntimeException if failed to create logfile.
*/
- public static function init(string $path, int $verbosity = 4)
+ public static function init(string $path, int $verbosity = 4): void
{
if (!is_dir(dirname($path))) {
- throw new InvalidArgumentException("Parent directory of '$path' does not exist.");
+ throw new RuntimeException("Parent directory of '$path' does not exist.");
}
if (!is_writable(dirname($path))) {
- throw new InvalidArgumentException("Parent directory of '$path' is not writable.");
+ throw new RuntimeException("Parent directory of '$path' is not writable.");
}
if (is_file($path) && !is_writable($path)) {
- throw new InvalidArgumentException("The logfile '$path' is not writable.");
+ throw new RuntimeException("The logfile '$path' is not writable.");
}
self::$file = fopen($path, "a");
if (self::$file === false) {
@@ -49,6 +50,11 @@ class Logger
vfprintf(self::$file, date('c') . " %-9s %s(%d) $msg\n", $args);
}
+ protected static function exceptionmessage(Throwable $e): string
+ {
+ return "{$e->getMessage()} in {$e->getFile()}({$e->getLine()})";
+ }
+
/**
* Log an error message using printf format.
*/
@@ -60,7 +66,7 @@ class Logger
}
/**
- * Log a xarning message using printf format.
+ * Log a warning message using printf format.
*/
public static function warning(string $msg, ...$args)
{
@@ -90,12 +96,12 @@ class Logger
}
/**
- * Log an exception.
+ * Log an exception as an error.
*/
- public static function exception(Throwable $e, bool $withtrace = false)
+ public static function errorex(Throwable $e, bool $withtrace = false)
{
if (self::$verbosity > 0) {
- $msg = $e->getMessage();
+ $msg = self::exceptionmessage($e);
if ($withtrace) {
// TODO: Maybe print a more beautiful stack trace.
$msg .= PHP_EOL . $e->getTraceAsString();
@@ -103,4 +109,34 @@ class Logger
self::write('ERROR', $msg);
}
}
+
+ /**
+ * Log an exception as a warning.
+ */
+ public static function warningex(Throwable $e)
+ {
+ if (self::$verbosity > 1) {
+ self::write('WARN', self::exceptionmessage($e));
+ }
+ }
+
+ /**
+ * Log an exception as an info.
+ */
+ public static function infoex(Throwable $e)
+ {
+ if (self::$verbosity > 2) {
+ self::write('INFO', self::exceptionmessage($e));
+ }
+ }
+
+ /**
+ * Log an exception as a debug.
+ */
+ public static function debugex(Throwable $e)
+ {
+ if (self::$verbosity > 3) {
+ self::write('DEBUG', self::exceptionmessage($e));
+ }
+ }
}
diff --git a/app/class/Media.php b/app/class/Media.php
index 9df2fef..b08726d 100644
--- a/app/class/Media.php
+++ b/app/class/Media.php
@@ -5,6 +5,7 @@ namespace Wcms;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
+use Exception;
class Media extends Item
{
diff --git a/app/class/Modelhome.php b/app/class/Modelhome.php
index b35fe6d..b798f56 100644
--- a/app/class/Modelhome.php
+++ b/app/class/Modelhome.php
@@ -29,7 +29,7 @@ class Modelhome extends Modelpage
* @param Opt $opt
*
* @param string $regex Regex to match.
- * @param array $options Option search, could be `content` `title` `description`.
+ * @param array $searchopt Option search, could be `content` `title` `description`.
*
* @return array associative array of `Page` objects *
*/
@@ -98,7 +98,7 @@ class Modelhome extends Modelpage
/**
* Search for regex and count occurences
*
- * @param array $page list Array of Pages.
+ * @param array $pagelist list Array of Pages.
* @param string $regex Regex to match.
* @param array $options Option search, could be `content` `title` `description`.
*
@@ -279,7 +279,7 @@ class Modelhome extends Modelpage
/**
- * @param array array of the columns to show from the user
+ * @param array $columns array of the columns to show from the user
*
* @return array assoc each key columns to a boolean value to show or not
*/
diff --git a/app/class/Modelmedia.php b/app/class/Modelmedia.php
index ceb9bc7..bd0ff03 100644
--- a/app/class/Modelmedia.php
+++ b/app/class/Modelmedia.php
@@ -75,7 +75,7 @@ class Modelmedia extends Model
*
* @param array $medialist
* @param string $sortby
- * @param int order Can be 1 or -1
+ * @param int $order Can be 1 or -1
*/
public function medialistsort(array &$medialist, string $sortby = 'id', int $order = 1): bool
{
diff --git a/app/class/Modelpage.php b/app/class/Modelpage.php
index 848ba3f..95b6a98 100644
--- a/app/class/Modelpage.php
+++ b/app/class/Modelpage.php
@@ -5,6 +5,7 @@ namespace Wcms;
use Exception;
use JamesMoss\Flywheel\Document;
use DateTimeImmutable;
+use LogicException;
class Modelpage extends Modeldb
{
@@ -141,7 +142,7 @@ class Modelpage extends Modeldb
/**
* Delete a page and it's linked rendered html and css files
*
- * @param Page|string $id could be an Page object or a id string
+ * @param Page|string $page could be an Page object or a id string
*
* @return bool true if success otherwise false
*/
@@ -421,7 +422,7 @@ class Modelpage extends Modeldb
*/
public function reset(Page $page, array $reset): Page
{
- $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
if ($reset['tag']) {
$page->settag([]);
}
diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php
index 553857b..7322955 100644
--- a/app/class/Modelrender.php
+++ b/app/class/Modelrender.php
@@ -3,6 +3,7 @@
namespace Wcms;
use Exception;
+use LogicException;
use Michelf\MarkdownExtra;
class Modelrender extends Modelpage
@@ -56,7 +57,11 @@ class Modelrender extends Modelpage
*/
public function upage(string $id): string
{
- return $this->generate('pageread/', ['page' => $id]);
+ try {
+ return $this->router->generate('pageread/', ['page' => $id]);
+ } catch (Exception $e) {
+ throw new LogicException($e->getMessage(), $e->getCode(), $e);
+ }
}
@@ -128,7 +133,7 @@ class Modelrender extends Modelpage
$matches = $this->match($body, $regex);
// First, analyse the synthax and call the corresponding methods
- if (isset($matches)) {
+ if (!empty($matches)) {
foreach ($matches as $key => $match) {
$element = new Element($this->page->id(), $match);
$element->setcontent($this->getelementcontent($element->sources(), $element->type()));
@@ -534,7 +539,7 @@ class Modelrender extends Modelpage
{
$matches = $this->match($text, 'MEDIA');
- if (isset($matches)) {
+ if (!empty($matches)) {
foreach ($matches as $match) {
$medialist = new Medialist($match);
$medialist->readoptions();
@@ -575,7 +580,7 @@ class Modelrender extends Modelpage
$modelhome = new Modelhome();
- if (isset($matches)) {
+ if (!empty($matches)) {
foreach ($matches as $match) {
$optlist = new Optlist(['render' => $this]);
$optlist->parsehydrate($match['options']);
diff --git a/app/class/Modeltimeline.php b/app/class/Modeltimeline.php
index db9c4f5..be80211 100644
--- a/app/class/Modeltimeline.php
+++ b/app/class/Modeltimeline.php
@@ -19,7 +19,7 @@ class Modeltimeline extends Modeldb
public function get(int $id)
{
- $eventdata = $this->repo->findById($id);
+ $eventdata = $this->repo->findById("$id");
if ($eventdata !== false) {
return new Event($eventdata);
} else {
@@ -62,7 +62,7 @@ class Modeltimeline extends Modeldb
/**
* Store event
*
- * @param Event The event to be stored in the repositery
+ * @param Event $event The event to be stored in the repositery
*
* @return bool retrun true if it works, false if it fails
*/
@@ -97,6 +97,7 @@ class Modeltimeline extends Modeldb
$id = 0;
$subid = 0;
$lastuser = null;
+ $groupedevents = [];
foreach ($events as $event) {
if ($event->user() !== $lastuser) {
$subid = 0;
diff --git a/app/class/Page.php b/app/class/Page.php
index 760521b..da149f7 100644
--- a/app/class/Page.php
+++ b/app/class/Page.php
@@ -53,8 +53,7 @@ class Page extends Dbitem
public const TABS = ['main', 'css', 'header', 'body', 'nav', 'aside', 'footer', 'javascript'];
public const VAR_DATE = ['date', 'datecreation', 'datemodif', 'daterender'];
-
-
+
// _____________________________________________________ F U N ____________________________________________________
@@ -71,7 +70,7 @@ class Page extends Dbitem
public function reset()
{
- $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
$this->settitle($this->id());
$this->setdescription('');
@@ -460,7 +459,7 @@ class Page extends Dbitem
if ($datecreation instanceof DateTimeImmutable) {
$this->datecreation = $datecreation;
} elseif ($datecreation === true) {
- $this->datecreation = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $this->datecreation = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
} else {
$this->datecreation = DateTimeImmutable::createFromFormat(
DateTime::ISO8601,
@@ -508,7 +507,7 @@ class Page extends Dbitem
public function setjavascript($javascript)
{
- if (strlen($javascript < self::LENTEXT && is_string($javascript))) {
+ if (strlen($javascript) < self::LENTEXT && is_string($javascript)) {
$this->javascript = $javascript;
}
}
@@ -516,14 +515,14 @@ class Page extends Dbitem
public function setbody($body)
{
- if (strlen($body < self::LENTEXT && is_string($body))) {
+ if (strlen($body) < self::LENTEXT && is_string($body)) {
$this->body = $body;
}
}
public function setheader($header)
{
- if (strlen($header < self::LENTEXT && is_string($header))) {
+ if (strlen($header) < self::LENTEXT && is_string($header)) {
$this->header = $header;
}
}
@@ -756,7 +755,7 @@ class Page extends Dbitem
public function updateedited()
{
- $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
$this->setdatemodif($now);
$this->addeditcount();
}
diff --git a/app/class/User.php b/app/class/User.php
index ecb4507..a62091f 100644
--- a/app/class/User.php
+++ b/app/class/User.php
@@ -90,7 +90,7 @@ class User extends Item
if (empty($this->expiredate)) {
return 'never';
} else {
- $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ $now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
if ($this->expiredate < $now) {
return 'expired';
} else {
diff --git a/app/fn/fn.php b/app/fn/fn.php
index a7fd929..737590f 100644
--- a/app/fn/fn.php
+++ b/app/fn/fn.php
@@ -24,7 +24,7 @@ function readablesize($bytes, $base = 2 ** 10)
} elseif ($bytes < $base ** 3) {
$num = round($bytes / $base ** 2, 1);
$unit = 'M' . $i;
- } elseif ($bytes < $base ** 4) {
+ } else {
$num = round($bytes / $base ** 3, 1);
$unit = 'G' . $i;
}
@@ -87,7 +87,7 @@ function arrayclean($input)
return $output;
}
-function idclean(string $input)
+function idclean(string $input): string
{
$input = urldecode($input);
$search = ['é', 'à', 'è', 'ç', 'ù', 'ï', 'î', ' '];