* @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") { /* {{{ */ $len = strlen($alph)-1; $s = ""; for ($i = 0; $i != $n; ++$i) $s .= $alph[mt_rand(0, $len)]; 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');