diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2020-04-22 22:13:28 +0200 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2020-04-22 22:13:28 +0200 |
commit | bd16dc7c6fc630a6b59ca3b041dde146ab50c72b (patch) | |
tree | b2f6a42c0bdb23606c1e9fdba0c28f0aee6b1786 /app/fn/fn.php | |
parent | eba4d7aa0ee5ea1bcb29e8254c28a272023ea54f (diff) | |
download | wcms-bd16dc7c6fc630a6b59ca3b041dde146ab50c72b.tar.gz wcms-bd16dc7c6fc630a6b59ca3b041dde146ab50c72b.zip |
colors refactor with exceptions
Diffstat (limited to 'app/fn/fn.php')
-rw-r--r-- | app/fn/fn.php | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/app/fn/fn.php b/app/fn/fn.php index 243e056..a7fd929 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."); + } +} |