Skip to content

Commit

Permalink
close #148 change permissions when writing a file
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-peugnet committed Oct 19, 2021
1 parent 423da90 commit 3f3d666
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 16 deletions.
16 changes: 16 additions & 0 deletions app/class/Chmodexception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Wcms;

class Chmodexception extends Ioexception
{
public function __construct(
string $message,
int $permissions,
int $code = 0,
?\Throwable $previous = null
) {
$message .= sprintf(' permissions: %o', $permissions);
parent::__construct($message, $code, $previous);
}
}
5 changes: 1 addition & 4 deletions app/class/Colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,11 @@ public function tocss(array $tagcolor): string
/**
* Write css in the file
* @param string $rawcss
* @throws \InvalidArgumentException If cant create
*/
public function writecssfile(string $file, string $rawcss)
{
accessfile($file, true);
if (!file_put_contents($file, $rawcss)) {
throw new \InvalidArgumentException("cant create file : $this->file", 1);
}
Model::writefile($file, $rawcss);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/class/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public static function createconfig(array $datas)
public static function savejson()
{
$json = self::tojson();
return file_put_contents(Model::CONFIG_FILE, $json);

return Model::writefile(Model::CONFIG_FILE, $json);
}


Expand Down
2 changes: 1 addition & 1 deletion app/class/Controlleradmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function update()
{
accessfile(Model::GLOBAL_CSS_FILE, true);

$globalcss = file_put_contents(Model::GLOBAL_CSS_FILE, $_POST['globalcss']);
$globalcss = Model::writefile(Model::GLOBAL_CSS_FILE, $_POST['globalcss']);

Config::hydrate($_POST);
if (Config::savejson() !== false && $globalcss !== false) {
Expand Down
9 changes: 9 additions & 0 deletions app/class/Flywheel/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wcms\Flywheel;

use Wcms\Model;

class Repository extends \JamesMoss\Flywheel\Repository
{
/**
Expand All @@ -28,4 +30,11 @@ public function getAllIds()
return $this->getIdFromPath($path, $ext);
}, $this->getAllFiles());
}

protected function write($path, $contents): bool
{
$ret = parent::write($path, $contents);
chmod($path, Model::PERMISSION);
return $ret;
}
}
10 changes: 10 additions & 0 deletions app/class/Ioexception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Wcms;

use RuntimeException;

class Ioexception extends RuntimeException
{

}
37 changes: 37 additions & 0 deletions app/class/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wcms;

use RuntimeException;

abstract class Model
{

Expand All @@ -23,6 +25,8 @@ abstract class Model
public const DATABASE_DIR = './database/';
public const PAGES_DIR = self::DATABASE_DIR . 'pages/';

public const PERMISSION = 0660;

public const MEDIA_SORTBY = [
'id' => 'id',
'size' => 'size',
Expand Down Expand Up @@ -235,4 +239,37 @@ public static function sendflashmessage(string $content, string $type = 'info')
}
$_SESSION['user' . Config::basepath()]['flashmessages'][] = ['content' => $content, 'type' => $type];
}

/**
* @param string $filename
* @param mixed $data
* @param int $permissions optionnal permission in octal format (a zero before three numbers)
*
* @return bool False if file is not writen, otherwise true (even if permission warning)
*
* Send flash messages:
* - error when problem writing the file
* - warning when problem with permissions
*/
public static function writefile(string $filename, $data, int $permissions = self::PERMISSION): bool
{
if ($permissions < 0600 || $permissions > 0777) {
self::sendflashmessage(
sprintf("Invalid permissions 0%o, using default: 0%o", $permissions, self::PERMISSION),
'warning'
);
$permissions = self::PERMISSION;
}
try {
file_put_content_chmod($filename, $data, $permissions);
} catch (Chmodexception $e) {
Logger::warning($e->getMessage());
self::sendflashmessage($e->getMessage(), 'warning');
} catch (Ioexception $e) {
Logger::error($e->getMessage());
self::sendflashmessage($e->getMessage(), 'error');
return false;
}
return true;
}
}
6 changes: 2 additions & 4 deletions app/class/Modelfont.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ public function fontface(array $fontlist)
}


public function write(string $fontface)
public function write(string $fontface): bool
{
$write = file_put_contents(Model::ASSETS_CSS_DIR . 'fonts.css', $fontface);
if ($write !== false) {
}
return self::writefile(self::ASSETS_CSS_DIR . 'fonts.css', $fontface);
}

public function upload(array $file, $maxsize = 2 ** 24, $id = null)
Expand Down
12 changes: 6 additions & 6 deletions app/class/Modelrender.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ public function elementparser(Element $element)
*/
public function write(string $html)
{
file_put_contents(Model::HTML_RENDER_DIR . $this->page->id() . '.html', $html);
file_put_contents(Model::RENDER_DIR . $this->page->id() . '.css', $this->page->css());
//file_put_contents(Model::RENDER_DIR . $this->page->id() . '.quick.css', $this->page->quickcss());
file_put_contents(Model::RENDER_DIR . $this->page->id() . '.js', $this->page->javascript());
self::writefile(self::HTML_RENDER_DIR . $this->page->id() . '.html', $html);
self::writefile(self::RENDER_DIR . $this->page->id() . '.css', $this->page->css());
//self::writefile(self::RENDER_DIR . $this->page->id() . '.quick.css', $this->page->quickcss());
self::writefile(self::RENDER_DIR . $this->page->id() . '.js', $this->page->javascript());
}


Expand All @@ -218,11 +218,11 @@ public function writetemplates()
{
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());
self::writefile(Model::RENDER_DIR . $tempaltecsspage->id() . '.css', $tempaltecsspage->css());
}
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());
self::writefile(Model::RENDER_DIR . $templatejspage->id() . '.js', $templatejspage->javascript());
}
}

Expand Down
29 changes: 29 additions & 0 deletions app/fn/fn.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use Wcms\Chmodexception;
use Wcms\Ioexception;
use Wcms\Mediaopt;
use Wcms\Model;

use function Clue\StreamFilter\fun;

Expand Down Expand Up @@ -469,3 +472,29 @@ function parse_size($size)
return round($size);
}
}

/**
* @param string $filename Path to the file where to write the data.
* @param mixed $data The data to write. Can be either a string, an array or a stream resource.
* @param int $permissions in octal value
*
* @throws Ioexception when file_put_contents fails
* @throws Chmodexception when chmod fails
*/
function file_put_content_chmod(string $filename, $data, int $permissions): int
{
$create = !file_exists($filename);
$length = file_put_contents($filename, $data);
if ($length === false) {
throw new Ioexception("Error while writing $filename");
}
if ($create) {
if ($permissions < 0700 || $permissions > 0777) {

This comment has been minimized.

Copy link
@n-peugnet

n-peugnet Oct 19, 2021

Collaborator

Woops we left 0700 here. Maybe you should completely remove this if block.

This comment has been minimized.

Copy link
@vincent-peugnet

vincent-peugnet Oct 20, 2021

Author Owner

Mmh, sorry but I don't understand what you mean boss.

This comment has been minimized.

Copy link
@n-peugnet

n-peugnet Oct 21, 2021

Collaborator

We changed the minimum to 0600 above

But we forgot to change it here.

throw new Chmodexception("Incorrect value", $permissions);
}
if (!chmod($filename, $permissions)) {
throw new Chmodexception("Error while setting file permissions $filename", $permissions);
}
}
return $length;
}

0 comments on commit 3f3d666

Please sign in to comment.