use some functions into class Utitilities

This commit is contained in:
Uwe Steinmann 2025-11-14 15:06:30 +01:00
parent 7414443acb
commit 2d963a3c6f
2 changed files with 96 additions and 75 deletions

View File

@ -0,0 +1,94 @@
<?php
/**
* Various utility functions
*
* @category DMS
* @package SeedDMS
* @license GPL 2
* @version @version@
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2025 Uwe Steinmann
* @version Release: @package_version@
*/
namespace Seeddms\Seeddms;
/**
* Class with various methods
*
* @category DMS
* @package SeedDMS
* @author Uwe Steinmann <uwe@steinmann.cx>
* @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');

View File

@ -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