aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/class/Colors.php98
-rw-r--r--app/class/Controlleradmin.php6
-rw-r--r--app/class/Controllerfont.php2
-rw-r--r--app/class/Controllerhome.php10
-rw-r--r--app/class/Model.php23
-rw-r--r--app/class/Modelfont.php9
-rw-r--r--app/class/Modelpage.php4
-rw-r--r--app/fn/fn.php28
-rw-r--r--app/view/templates/homemenu.php2
9 files changed, 125 insertions, 57 deletions
diff --git a/app/class/Colors.php b/app/class/Colors.php
index ffa56b6..87a5414 100644
--- a/app/class/Colors.php
+++ b/app/class/Colors.php
@@ -12,30 +12,40 @@ class Colors extends Item
protected $tagcolor = [];
-
- public function __construct(array $taglist = [])
+ public function __construct(string $file = 'tagcolors.css', array $taglist = [])
{
- if ($this->readcssfile()) {
- $this->parsetagcss();
+ $this->setfile($file);
+ if (file_exists($this->file)) {
+ $this->rawcss = $this->readcssfile();
+ $this->tagcolor = $this->parsetagcss($this->rawcss);
}
+
if (!empty($taglist)) {
- $this->removeaddtags($taglist);
- $this->tocss();
- $this->writecssfile();
+ $this->tagcolor = $this->removeaddtags($taglist);
+ $this->rawcss = $this->tocss($this->tagcolor);
+ $this->writecssfile($this->file, $this->rawcss);
}
}
- public function readcssfile(): bool
+ /**
+ * Read file containing css
+ * @return string raw css or empty string
+ */
+ public function readcssfile(): string
{
- if (Model::dircheck(Model::CSS_DIR) && file_exists($this->file)) {
- $this->rawcss = file_get_contents($this->file);
- return true;
- } else {
- return false;
+ $rawcss = file_get_contents($this->file);
+ if (is_string($rawcss)) {
+ return $rawcss;
}
+ return '';
}
- public function removeaddtags(array $taglist = [])
+ /**
+ * Check if new tags have been created and generate them a background color
+ * @param array $taglist associative array using tag as key
+ * @return array associative array of `tag => background-color`
+ */
+ public function removeaddtags(array $taglist = []): array
{
$tagcolor = [];
foreach ($taglist as $tag => $tagcount) {
@@ -45,45 +55,66 @@ class Colors extends Item
$tagcolor[$tag] = '#' . dechex(rand(50, 255)) . dechex(rand(50, 255)) . dechex(rand(50, 255));
}
}
- $this->tagcolor = $tagcolor;
+ return $tagcolor;
}
/**
- * Transform a CSS string in a array of `tag => background-color`
- *
- * @return bool Ouput array using TAG as key and Hex Color as value
+ * Transform a CSS string in a array of datas
+ * @param string $rawcss CSS string to parse
+ * @return array associative array of `tag => background-color`
*/
- public function parsetagcss()
+ public function parsetagcss(string $rawcss): array
{
$pattern = '%.tag\_([a-z0-9\-\_]*)\s*\{\s*background-color:\s*(#[A-Fa-f0-9]{6})\;\s*\}%';
- preg_match_all($pattern, $this->rawcss, $matches);
+ preg_match_all($pattern, $rawcss, $matches);
$tagcolor = array_combine($matches[1], $matches[2]);
if ($tagcolor !== false) {
- $this->tagcolor = $tagcolor;
- return true;
+ return $tagcolor;
} else {
- return false;
+ return [];
}
}
- public function tocss()
+ /**
+ * Generate CSS string from datas
+ * @param array $tagcolor associative array of `tag => background-color`
+ * @return string CSS
+ */
+ public function tocss(array $tagcolor): string
{
$css = "";
- foreach ($this->tagcolor as $tag => $color) {
+ foreach ($tagcolor as $tag => $color) {
$css .= PHP_EOL . '.tag_' . $tag . ' { background-color: ' . $color . '; }';
}
- $this->rawcss = $css;
+ return $css;
}
- public function writecssfile()
+ /**
+ * Write css in the file
+ * @param string $rawcss
+ * @throws \InvalidArgumentException If cant create
+ */
+ public function writecssfile(string $file, string $rawcss)
{
- if (Model::dircheck(Model::CSS_DIR)) {
- return file_put_contents($this->file, $this->rawcss);
+ accessfile($file, true);
+ if (!file_put_contents($file, $rawcss)) {
+ throw new \InvalidArgumentException("cant create file : $this->file", 1);
}
}
+ /**
+ * Update tagcolors based on datas
+ * @param array $tagcolor associative array of `tag => background-color`
+ */
+ public function update(array $tagcolor)
+ {
+ $this->settagcolor($tagcolor);
+ $this->rawcss = $this->tocss($this->tagcolor);
+ $this->writecssfile($this->file, $this->rawcss);
+ }
+
public function htmlcolorpicker(): string
{
$html = '<ul>';
@@ -111,6 +142,15 @@ class Colors extends Item
// _______________________ S E T _________________________
+ /**
+ * @throws \InvalidArgumentException If cant access file
+ */
+ public function setfile(string $path)
+ {
+ accessfile($path);
+ $this->file = $path;
+ }
+
public function setrawcss($rawcss)
{
if (is_string($rawcss)) {
diff --git a/app/class/Controlleradmin.php b/app/class/Controlleradmin.php
index f68cb82..ec437fa 100644
--- a/app/class/Controlleradmin.php
+++ b/app/class/Controlleradmin.php
@@ -26,7 +26,7 @@ class Controlleradmin extends Controller
$datas['thumbnaillist'] = $this->mediamanager->listthumbnail();
$datas['interfacecsslist'] = $this->mediamanager->listinterfacecss();
- $globalcssfile = Model::GLOBAL_DIR . 'global.css';
+ $globalcssfile = Model::GLOBAL_CSS_FILE;
if (is_file($globalcssfile)) {
$datas['globalcss'] = file_get_contents($globalcssfile);
@@ -45,9 +45,9 @@ class Controlleradmin extends Controller
public function update()
{
- Model::dircheck(Model::GLOBAL_DIR);
+ accessfile(Model::GLOBAL_CSS_FILE);
- $globalcss = file_put_contents(Model::GLOBAL_DIR . 'global.css', $_POST['globalcss']);
+ $globalcss = file_put_contents(Model::GLOBAL_CSS_FILE, $_POST['globalcss']);
Config::hydrate($_POST);
if (Config::savejson() !== false && $globalcss !== false) {
diff --git a/app/class/Controllerfont.php b/app/class/Controllerfont.php
index 91a7936..7df1a2f 100644
--- a/app/class/Controllerfont.php
+++ b/app/class/Controllerfont.php
@@ -18,7 +18,7 @@ class Controllerfont extends Controller
public function desktop()
{
if ($this->user->iseditor()) {
- $this->fontmanager->fontdircheck();
+ Model::dircheck(Model::FONT_DIR);
$fontlist = $this->fontmanager->getfontlist();
diff --git a/app/class/Controllerhome.php b/app/class/Controllerhome.php
index d1c7a11..6e2155a 100644
--- a/app/class/Controllerhome.php
+++ b/app/class/Controllerhome.php
@@ -28,7 +28,9 @@ class Controllerhome extends Controllerpage
$pagelist = $this->modelhome->pagelist();
$this->opt = $this->modelhome->optinit($pagelist);
- $vars['colors'] = new Colors($this->opt->taglist());
+
+ $vars['colors'] = new Colors(Model::COLORS_FILE, $this->opt->taglist());
+
$deepsearch = $this->deepsearch();
@@ -130,10 +132,8 @@ class Controllerhome extends Controllerpage
public function colors()
{
if (isset($_POST['tagcolor']) && $this->user->issupereditor()) {
- $colors = new Colors();
- $colors->hydrate($_POST);
- $colors->tocss();
- $colors->writecssfile();
+ $colors = new Colors(Model::COLORS_FILE);
+ $colors->update($_POST['tagcolor']);
}
$this->routedirect('home');
}
diff --git a/app/class/Model.php b/app/class/Model.php
index bc4c801..692257a 100644
--- a/app/class/Model.php
+++ b/app/class/Model.php
@@ -8,6 +8,7 @@ abstract class Model
public const CONFIG_FILE = 'config.json';
public const MAN_FILE = 'MANUAL.md';
public const CSS_DIR = 'assets/css/';
+ public const COLORS_FILE = self::CSS_DIR . 'tagcolors.css';
public const JS_DIR = 'assets/js/';
public const ICONS_DIR = 'assets/icons/';
public const FONT_DIR = 'fonts/';
@@ -18,6 +19,7 @@ abstract class Model
public const RENDER_DIR = 'assets/render/';
public const HTML_RENDER_DIR = 'render/';
public const GLOBAL_DIR = 'assets/global/';
+ public const GLOBAL_CSS_FILE = self::GLOBAL_DIR . 'global.css';
public const DATABASE_DIR = './database/';
public const PAGES_DIR = self::DATABASE_DIR . 'pages/';
@@ -183,16 +185,17 @@ abstract class Model
* Check if dir exist. If not, create it
*
* @param string $dir Directory to check
- *
* @return bool return true if the dir already exist or was created succesfullt. Otherwise return false
+ * @throws \InvalidArgumentException If folder creation is impossible
*/
public static function dircheck(string $dir): bool
{
if (!is_dir($dir)) {
- return mkdir($dir);
- } else {
- return true;
+ if (!mkdir($dir)) {
+ throw new \InvalidArgumentException("Cannot create directory : $dir");
+ }
}
+ return true;
}
/**
@@ -203,7 +206,12 @@ abstract class Model
return array_unique(array_values(self::MEDIA_EXT));
}
- public static function getflashmessages()
+ /**
+ * Read then empty session to get flash messages
+ *
+ * @return array ordered array containing array with content and type as keys or empty array
+ */
+ public static function getflashmessages(): array
{
if (!empty($_SESSION['user' . Config::basepath()]['flashmessages'])) {
$flashmessage = $_SESSION['user' . Config::basepath()]['flashmessages'];
@@ -213,7 +221,8 @@ abstract class Model
} else {
return [];
}
- return $flashmessage;
+ } else {
+ return [];
}
}
@@ -221,7 +230,7 @@ abstract class Model
* Add a message to flash message list
*
* @param string $content The message content
- * @param string $type Message Type, can be `info|warning|success`
+ * @param string $type Message Type, can be `info|warning|success|eror`
*/
public static function sendflashmessage(string $content, string $type = 'info')
{
diff --git a/app/class/Modelfont.php b/app/class/Modelfont.php
index be8de81..3dac3ff 100644
--- a/app/class/Modelfont.php
+++ b/app/class/Modelfont.php
@@ -7,14 +7,7 @@ class Modelfont extends Model
protected const FONT_TYPES = ['woff2', 'woff', 'otf', 'ttf', 'eot', 'svg'];
- public function fontdircheck()
- {
- if (!is_dir(Model::FONT_DIR)) {
- return mkdir(Model::FONT_DIR);
- } else {
- return true;
- }
- }
+
public function getfontlist()
{
diff --git a/app/class/Modelpage.php b/app/class/Modelpage.php
index e2a2718..95b6a98 100644
--- a/app/class/Modelpage.php
+++ b/app/class/Modelpage.php
@@ -17,9 +17,7 @@ class Modelpage extends Modeldb
{
$this->dbinit(Model::PAGES_DIR);
$this->storeinit(Config::pagetable());
- if (!$this->dircheck(Model::HTML_RENDER_DIR)) {
- throw new LogicException("Media error : Cant create /render folder");
- }
+ $this->dircheck(Model::HTML_RENDER_DIR);
}
/**
diff --git a/app/fn/fn.php b/app/fn/fn.php
index 6b312a2..737590f 100644
--- a/app/fn/fn.php
+++ b/app/fn/fn.php
@@ -364,3 +364,31 @@ function secrethash(string $token): string
{
return hash_hmac('sha256', $token, Wcms\Config::secretkey());
}
+
+
+/**
+ * Check if a file is accessible or can be writen
+ * @param string $path file path to check
+ * @param bool $createdir create directory if does not exist
+ * @throws \InvalidArgumentException if :
+ * parent directory does not exist | is not writable | file exist and not writable
+ */
+function accessfile(string $path, bool $createdir = false)
+{
+ $dir = dirname($path);
+ if (!is_dir($dir)) {
+ if ($createdir) {
+ if (!mkdir($dir)) {
+ throw new \InvalidArgumentException("Cannot create directory : $dir");
+ }
+ } else {
+ throw new \InvalidArgumentException("Directory '$dir' does not exist.");
+ }
+ }
+ if (!is_writable($dir)) {
+ throw new \InvalidArgumentException("Directory '$dir' is not writable.");
+ }
+ if (is_file($path) && !is_writable($path)) {
+ throw new \InvalidArgumentException("The file '$path' is not writable.");
+ }
+}
diff --git a/app/view/templates/homemenu.php b/app/view/templates/homemenu.php
index ff39f1c..540806b 100644
--- a/app/view/templates/homemenu.php
+++ b/app/view/templates/homemenu.php
@@ -325,7 +325,7 @@
</ul>
<input type="submit" value="update columns">
</form>
- <?php if($user->issupereditor()) { ?>
+ <?php if($user->issupereditor() && !empty($colors)) { ?>
<h2>Tag colors</h2>
<form action="<?= $this->url('homecolors') ?>" method="post">
<?= $colors->htmlcolorpicker() ?>