From 2d963a3c6f00d94fbc10b7edf21894924370b4dd Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Fri, 14 Nov 2025 15:06:30 +0100 Subject: [PATCH] use some functions into class Utitilities --- inc/inc.ClassUtilities.php | 94 ++++++++++++++++++++++++++++++++++++++ inc/inc.Utils.php | 77 +------------------------------ 2 files changed, 96 insertions(+), 75 deletions(-) create mode 100644 inc/inc.ClassUtilities.php diff --git a/inc/inc.ClassUtilities.php b/inc/inc.ClassUtilities.php new file mode 100644 index 000000000..eeb853c62 --- /dev/null +++ b/inc/inc.ClassUtilities.php @@ -0,0 +1,94 @@ + + * @copyright Copyright (C) 2025 Uwe Steinmann + * @version Release: @package_version@ + */ + +namespace Seeddms\Seeddms; + +/** + * Class with various methods + * + * @category DMS + * @package SeedDMS + * @author Uwe Steinmann + * @copyright Copyright (C) 2025 Uwe Steinmann + * @version Release: @package_version@ + */ +class Utilities { /* {{{ */ + + /** + * Recursively remove a directory on disc + * + * @param string $dir name of directory + */ + static public function rrmdir($dir) { /* {{{ */ + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (filetype($dir."/".$object) == "dir") self::rrmdir($dir."/".$object); else unlink($dir."/".$object); + } + } + reset($objects); + rmdir($dir); + } + } /* }}} */ + + /** + * Create a random string + * + * @param integer $n number of chars + * @param string $alph alphabet used as source for chars + * @return string random string + */ + static public function makeRandomString($n, $alph = "0123456789abcdefghijklmnopqrstuvwxyz") { /* {{{ */ + $s = ""; + for ($i = 0; $i != $n; ++$i) + $s .= $alph[mt_rand(0, 35)]; + return $s; + } /* }}} */ + + /** + * Create a real uniqid for cryptographic purposes + * + * @ return string + */ + static public function uniqidReal($lenght = 13) { /* {{{ */ + // uniqid gives 13 chars, but you could adjust it to your needs. + if (function_exists("random_bytes")) { + $bytes = random_bytes(ceil($lenght / 2)); + } elseif (function_exists("openssl_random_pseudo_bytes")) { + $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2)); + } else { + throw new Exception("no cryptographically secure random function available"); + } + return substr(bin2hex($bytes), 0, $lenght); + } /* }}} */ + + /** + * Return nonce for CSP + * + * @return string + */ + static public function createNonce() { /* {{{ */ + $length = 16; + $usable = true; + $bytes = openssl_random_pseudo_bytes($length, $usable); + if ($usable === false) { + // weak + // @TODO do something? + } + return base64_encode($bytes); + } /* }}} */ + +} /* }}} */ + +class_alias('Seeddms\Seeddms\Utilities', 'SeedDMS_Utils'); diff --git a/inc/inc.Utils.php b/inc/inc.Utils.php index b86b2031c..ec8c69cb1 100644 --- a/inc/inc.Utils.php +++ b/inc/inc.Utils.php @@ -650,7 +650,7 @@ function checkFormKey($formid='', $method='POST') { /* {{{ */ * quota is reached. Negative values indicate a disk usage above quota. */ function checkQuota($user) { /* {{{ */ - global $settings, $dms; + global $settings; /* check if quota is turn off system wide */ if($settings->_quota == 0) @@ -1000,39 +1000,6 @@ function seed_pass_verify($password, $hash) { /* {{{ */ return $hash === md5($password); } /* }}} */ -/** - * Return nonce for CSP - * - * @return string - */ -function createNonce() { /* {{{ */ - $length = 16; - $usable = true; - $bytes = openssl_random_pseudo_bytes($length, $usable); - if ($usable === false) { - // weak - // @TODO do something? - } - return base64_encode($bytes); -} /* }}} */ - -/** - * Create a real uniqid for cryptographic purposes - * - * @ return string - */ -function uniqidReal($lenght = 13) { - // uniqid gives 13 chars, but you could adjust it to your needs. - if (function_exists("random_bytes")) { - $bytes = random_bytes(ceil($lenght / 2)); - } elseif (function_exists("openssl_random_pseudo_bytes")) { - $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2)); - } else { - throw new Exception("no cryptographically secure random function available"); - } - return substr(bin2hex($bytes), 0, $lenght); -} - /** * Compare function for sorting users by login * @@ -1265,47 +1232,7 @@ function getMandatoryApprovers($folder, $document, $user) { /* {{{ */ return $approvers; } /* }}} */ -/** - * Class with various utility methods - * - * This class will sooner or later comprise the functions above - * - */ -class SeedDMS_Utils { /* {{{ */ - - /** - * Recursively remove a directory on disc - * - * @param string $dir name of directory - */ - static public function rrmdir($dir) { /* {{{ */ - if (is_dir($dir)) { - $objects = scandir($dir); - foreach ($objects as $object) { - if ($object != "." && $object != "..") { - if (filetype($dir."/".$object) == "dir") self::rrmdir($dir."/".$object); else unlink($dir."/".$object); - } - } - reset($objects); - rmdir($dir); - } - } /* }}} */ - - /** - * Create a random string - * - * @param integer $n number of chars - * @param string $alph alphabet used as source for chars - * @return string random string - */ - static public function makeRandomString($n, $alph = "0123456789abcdefghijklmnopqrstuvwxyz") { /* {{{ */ - $s = ""; - for ($i = 0; $i != $n; ++$i) - $s .= $alph[mt_rand(0, 35)]; - return $s; - } /* }}} */ - -} /* }}} */ +require_once "inc/inc.ClassUtilities.php"; /** * Class for creating encrypted api keys