Files
dbois/app/Helpers/common_helper.php
T
2026-01-09 11:41:28 +01:00

332 lines
8.5 KiB
PHP

<?php
/**
* Common helper functions for DBOIS application
* Migrated from CodeIgniter 3 to CodeIgniter 4
*/
if (!function_exists('dump')) {
/**
* Dump helper - dumps variables to the screen in a nicely formatted manner
*/
function dump($var, string $label = 'Dump', bool $echo = true): ?string
{
ob_start();
var_dump($var);
$output = ob_get_clean();
// Add formatting
$output = preg_replace("/]=>n(s+)/m", "] => ", $output);
$output = '<pre style="background: #FFFEEF; color: #000; border: 1px dotted #000; padding: 10px; margin: 10px 0; text-align: left;">' . $label . ' => ' . $output . '</pre>';
if ($echo) {
echo $output;
return null;
}
return $output;
}
}
if (!function_exists('dump_exit')) {
/**
* Dump and exit
*/
function dump_exit($var, string $label = 'Dump', bool $echo = true): void
{
dump($var, $label, $echo);
exit;
}
}
if (!function_exists('mysql2text')) {
function mysql2text(?string $message): string
{
$regeleinden = ['\r\n', '\r', '\n'];
$message = str_replace($regeleinden, chr(13), $message ?? '');
$message = stripslashes($message);
return $message;
}
}
if (!function_exists('mysql2html')) {
function mysql2html(?string $message): string
{
$regeleinden = ['\r\n', '\r', '\n'];
$message = str_replace($regeleinden, "<br />", $message ?? '');
$message = stripslashes($message);
return $message;
}
}
if (!function_exists('textarea2html')) {
function textarea2html(?string $message): string
{
$regeleinden = [chr(13)];
$message = str_replace($regeleinden, "<br />", $message ?? '');
$message = stripslashes($message);
return $message;
}
}
if (!function_exists('date2mysql')) {
function date2mysql(?string $date): ?string
{
if (!check_date($date)) {
return null;
}
return date('Y-m-d', strtotime($date));
}
}
if (!function_exists('mysql2date')) {
function mysql2date(?string $date): string
{
if (!check_date($date)) {
return '';
}
return date('d-m-Y', strtotime($date));
}
}
if (!function_exists('mysql2dateWithTime')) {
function mysql2dateWithTime(?string $date): string
{
if (!check_date($date)) {
return '';
}
return date('d-m-Y H:i:s', strtotime($date));
}
}
if (!function_exists('check_date')) {
function check_date(?string $date): bool
{
if (is_null($date) || strlen($date) < 6) {
return false;
}
return true;
}
}
if (!function_exists('mysql2timestamp')) {
function mysql2timestamp(string $date): int
{
$a = strptime($date, '%Y-%m-%d');
$timestamp = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], $a['tm_year'] + 1900);
return $timestamp;
}
}
if (!function_exists('readTextFromStorage')) {
function readTextFromStorage(?string $text): string
{
return base64_decode($text ?? '');
}
}
if (!function_exists('cleanTextForStorage')) {
function cleanTextForStorage(?string $text): string
{
$text = str_replace('/r/n', '', $text ?? '');
$text = str_replace('\r\n', '', $text);
$text = str_replace('/n', '', $text);
$text = str_replace('\n', '', $text);
$text = stripslashes($text);
return base64_encode($text);
}
}
if (!function_exists('setTextareaForStorage')) {
function setTextareaForStorage(?string $text): string
{
$text = setTextArea($text);
return base64_encode($text);
}
}
if (!function_exists('setTextArea')) {
function setTextArea(?string $text): string
{
$text = str_replace('/r/n', chr(13), $text ?? '');
$text = str_replace('\r\n', chr(13), $text);
$text = str_replace('/n', chr(13), $text);
$text = str_replace('\n', chr(13), $text);
$text = str_replace('\r', chr(13), $text);
$text = str_replace('/r', chr(13), $text);
$text = str_replace('\n\n', chr(13) . chr(13), $text);
$text = stripslashes($text);
return $text;
}
}
if (!function_exists('setTextAreaHtml')) {
function setTextAreaHtml(?string $text): string
{
$text = str_replace('/r/n', '<br />', $text ?? '');
$text = str_replace('\r\n', '<br />', $text);
$text = str_replace('/n', '<br />', $text);
$text = str_replace('\n', '<br />', $text);
$text = str_replace('\r', '<br />', $text);
$text = str_replace('/r', '<br />', $text);
$text = str_replace('\n\n', '<br /><br />', $text);
$text = stripslashes($text);
return $text;
}
}
if (!function_exists('checkBsn')) {
function checkBsn(string $bsn): bool
{
return true;
}
}
if (!function_exists('isNotNullOrEmpty')) {
/**
* Validate an object property
*/
function isNotNullOrEmpty($in, ?string $property = null, bool $zeroAllowed = false): bool
{
if ($property === null) {
if (is_object($in) && count((array)$in) === 0) {
return false;
}
$isNotEmpty = $zeroAllowed ? !empty($in) : $in === 0 || $in === '0' || !empty($in);
if (is_string($in)) {
return $isNotEmpty && strtoupper($in) !== 'NULL' && strtoupper($in) !== 'FALSE' && strtoupper($in) !== 'UNDEFINED';
}
return $isNotEmpty;
}
if (!is_object($in)) {
throw new Exception('Provided test $in must be a class instance(object) or a valid classname(string)');
}
if (!property_exists($in, $property)) {
return false;
}
$prop = $in->{$property};
$isNotEmpty = $zeroAllowed ? !empty($prop) : $prop === 0 || $prop === '0' || !empty($prop);
if (is_string($prop)) {
return $isNotEmpty && strtoupper($prop) !== 'NULL' && strtoupper($prop) !== 'FALSE' && strtoupper($prop) !== 'UNDEFINED';
}
return $isNotEmpty;
}
}
if (!function_exists('isNotNullOrEmptyOrZero')) {
function isNotNullOrEmptyOrZero($in, ?string $property = null): bool
{
return isNotNullOrEmpty($in, $property, true);
}
}
if (!function_exists('isNullOrEmpty')) {
function isNullOrEmpty($in, ?string $property = null): bool
{
return !isNotNullOrEmpty($in, $property);
}
}
if (!function_exists('isNullOrEmptyOrZero')) {
function isNullOrEmptyOrZero($in, ?string $property = null): bool
{
return !isNotNullOrEmpty($in, $property, true);
}
}
if (!function_exists('parseNotNullField')) {
function parseNotNullField($in): string
{
if (isNullOrEmpty($in)) {
return '';
}
return (string)$in;
}
}
if (!function_exists('unique_multidim_array')) {
function unique_multidim_array(array $array, string $key): array
{
$temp_array = [];
$key_array = [];
foreach ($array as $val) {
$keyValue = is_array($val) ? $val[$key] : $val->$key;
if (!in_array($keyValue, $key_array)) {
$key_array[] = $keyValue;
$temp_array[] = $val;
}
}
return $temp_array;
}
}
if (!function_exists('validate_password_strength')) {
/**
* Validate password strength
* Password must contain:
* - At least 8 characters
* - At least 1 uppercase letter
* - At least 1 lowercase letter
* - At least 1 number
* - At least 1 special character
*/
function validate_password_strength(string $password): bool
{
if (strlen($password) < 8) {
return false;
}
if (!preg_match('/[A-Z]/', $password)) {
return false;
}
if (!preg_match('/[a-z]/', $password)) {
return false;
}
if (!preg_match('/[0-9]/', $password)) {
return false;
}
if (!preg_match('/[^A-Za-z0-9]/', $password)) {
return false;
}
return true;
}
}
if (!function_exists('get_password_requirements_nl')) {
/**
* Get password validation requirements in Dutch
*/
function get_password_requirements_nl(): array
{
return [
'Minimaal 8 karakters',
'Minimaal 1 hoofdletter',
'Minimaal 1 kleine letter',
'Minimaal 1 cijfer',
'Minimaal 1 speciaal teken (!@#$%^&* etc.)'
];
}
}