You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
9.5 KiB
296 lines
9.5 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* @author Robert Strutts
|
|
* @copyright Copyright (c) 2022, Robert Strutts.
|
|
* @license MIT
|
|
*/
|
|
|
|
namespace CodeHydrater\bootstrap;
|
|
|
|
final class common {
|
|
|
|
protected function __construct() {
|
|
|
|
}
|
|
|
|
public static function return_bool_as_int_bit(bool $b_data): int {
|
|
return ($b_data) ? 1 : 0; // if true=1, else =0
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param type $ret option to check for false error condition
|
|
* @return bool true if false/error found
|
|
*/
|
|
public static function is_error($ret): bool {
|
|
$lr = self::string_to_lowercase(trim($ret));
|
|
return ($ret === false || $lr === 'false') ? true : false;
|
|
}
|
|
|
|
public static function return_bool_as_yes_no(bool $b_data): string {
|
|
return ($b_data) ? 'y' : 'n'; // if true=y, else =n
|
|
}
|
|
|
|
public static function get_bool($bool, $throw = true): bool {
|
|
if (is_bool($bool)) {
|
|
return $bool;
|
|
}
|
|
if (is_string($bool)) {
|
|
$bool = self::string_to_lowercase(trim($bool));
|
|
}
|
|
switch ($bool) {
|
|
case '0':
|
|
case 'false':
|
|
case ':false':
|
|
case ':null':
|
|
case null:
|
|
case 'no':
|
|
case 'n':
|
|
case 'disable':
|
|
case 'disabled':
|
|
return false;
|
|
case '1':
|
|
case 'true':
|
|
case ':true':
|
|
case 'yes':
|
|
case 'y':
|
|
case 'enable':
|
|
case 'enabled':
|
|
return true;
|
|
default:
|
|
if ($throw === true) {
|
|
throw new \CodeHydrater\exceptions\Bool_Exception("Value: ({$bool})");
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function get_count($i): int {
|
|
return (is_array($i) || is_object($i)) ? count($i) : 0;
|
|
}
|
|
|
|
// Begin Strings Functions here::
|
|
|
|
public static function string_to_lowercase(string $string, $encoding = null): string {
|
|
if (null === $encoding) {
|
|
$encoding = mb_internal_encoding();
|
|
}
|
|
|
|
return (extension_loaded('mbstring')) ? mb_strtolower($string, $encoding) : strtolower($string);
|
|
}
|
|
|
|
public static function string_to_uppercase(string $string, $encoding = null): string {
|
|
if (null === $encoding) {
|
|
$encoding = mb_internal_encoding();
|
|
}
|
|
|
|
return (extension_loaded('mbstring')) ? mb_strtoupper($string, $encoding) : strtoupper($string);
|
|
}
|
|
|
|
public static function string_length(string $string, $encoding = null) {
|
|
if (null === $encoding) {
|
|
$encoding = mb_internal_encoding();
|
|
}
|
|
|
|
return (extension_loaded('mbstring')) ? mb_strlen($string, $encoding) : strlen($string);
|
|
}
|
|
|
|
public static function string_position(string $string, string $needle, int $offset = 0, $encoding = null) {
|
|
if (null === $encoding) {
|
|
$encoding = mb_internal_encoding();
|
|
}
|
|
|
|
return (extension_loaded('mbstring')) ? mb_strpos($string, $needle, $offset, $encoding) : strpos($string, $needle, $offset);
|
|
}
|
|
|
|
public static function string_last_position(string $string, string $needle, int $offset = 0, $encoding = null) {
|
|
if (null === $encoding) {
|
|
$encoding = mb_internal_encoding();
|
|
}
|
|
|
|
return (extension_loaded('mbstring')) ? mb_strrpos($string, $needle, $offset, $encoding) : strrpos($string, $needle, $offset);
|
|
}
|
|
|
|
public static function string_trim($string, $charlist = null) {
|
|
if (is_null($charlist)) {
|
|
return trim($string);
|
|
} else {
|
|
$charlist = preg_quote($charlist, '/');
|
|
return preg_replace("/(^[$charlist]+)|([$charlist]+$)/us", '', $string);
|
|
}
|
|
}
|
|
|
|
public static function string_rtrim($string, $charlist = null) {
|
|
if (is_null($charlist)) {
|
|
return rtrim($string);
|
|
} else {
|
|
$charlist = preg_quote($charlist, '/');
|
|
return preg_replace("/([$charlist]+$)/us", '', $string);
|
|
}
|
|
}
|
|
|
|
public static function string_ltrim($string, $charlist = null) {
|
|
if (is_null($charlist)) {
|
|
return ltrim($string);
|
|
} else {
|
|
$charlist = preg_quote($charlist, '/');
|
|
return preg_replace("/(^[$charlist]+)/us", '', $string);
|
|
}
|
|
}
|
|
|
|
public static function string_cmp($str1, $str2, $encoding = null) {
|
|
if (null === $encoding) {
|
|
$encoding = mb_internal_encoding();
|
|
}
|
|
return strcmp(mb_strtoupper($str1, $encoding), mb_strtoupper($str2, $encoding));
|
|
}
|
|
|
|
/*
|
|
* Case-Insensitive Find
|
|
*/
|
|
|
|
public static function string_first_position(string $string, string $needle, int $offset = 0, $encoding = null) {
|
|
if (null === $encoding) {
|
|
$encoding = mb_internal_encoding();
|
|
}
|
|
|
|
return (extension_loaded('mbstring')) ? mb_stripos($string, $needle, $offset, $encoding) : stripos($string, $needle, $offset);
|
|
}
|
|
|
|
public static function string_sub_part(string $string, int $offset = 0, int $length = null, $encoding = null) {
|
|
if (null === $encoding) {
|
|
$encoding = mb_internal_encoding();
|
|
}
|
|
|
|
if ($length === null) {
|
|
return (extension_loaded('mbstring')) ? mb_substr($string, $offset, self::string_length($string), $encoding) : substr($string, $offset, strlen($string));
|
|
} else {
|
|
return (extension_loaded('mbstring')) ? mb_substr($string, $offset, $length, $encoding) : substr($string, $offset, $length);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Not really needed as str_contains($string, $needle) now exists!
|
|
* Will determine if data was found in string find and returns true if found.
|
|
* @param string $data
|
|
* @param string $find
|
|
* @retval bool
|
|
*/
|
|
public static function is_string_found(string $data, string $find): bool {
|
|
return (self::string_first_position($data, $find) !== false);
|
|
}
|
|
|
|
/**
|
|
* Will get only left part of string by length.
|
|
* @param string $str
|
|
* @param int $length
|
|
* @retval type string or false
|
|
*/
|
|
public static function get_string_left(string $str, int $length): false | string {
|
|
return self::string_sub_part($str, 0, $length);
|
|
}
|
|
|
|
public static function is_json(string $maybeJSON): bool {
|
|
$version = (float) phpversion();
|
|
if ($version >= 8.3) {
|
|
return json_validate($maybeJSON);
|
|
} else {
|
|
$obj = json_decode($maybeJSON);
|
|
return (json_last_error() === JSON_ERROR_NONE) ? true : false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Will get only the right part of string by length.
|
|
* @param string $str
|
|
* @param int $length
|
|
* @retval type string or false
|
|
*/
|
|
public static function get_string_right(string $str, int $length): false | string {
|
|
return self::string_sub_part($str, -$length);
|
|
}
|
|
|
|
public static function real_time_output(): void {
|
|
header("Content-type: text/plain");
|
|
// Turn off output buffering
|
|
ini_set('output_buffering', 'off');
|
|
// Turn off PHP output compression
|
|
ini_set('zlib.output_compression', false);
|
|
|
|
// Implicitly flush the buffer(s)
|
|
ini_set('implicit_flush', true);
|
|
ob_implicit_flush(true);
|
|
while (ob_get_level() > 0) {
|
|
// Get the curent level
|
|
$level = ob_get_level();
|
|
// End the buffering
|
|
ob_end_clean();
|
|
// If the current level has not changed, abort
|
|
if (ob_get_level() == $level) break;
|
|
}
|
|
}
|
|
/**
|
|
* Clear out from memory given variable by Reference!
|
|
* @param type $sensitive_data
|
|
*/
|
|
public static function wipe(& $sensitive_data): void {
|
|
if (function_exists("sodium_memzero")) {
|
|
sodium_memzero($sensitive_data);
|
|
}
|
|
unset($sensitive_data);
|
|
}
|
|
|
|
/**
|
|
* Variable Dump and exit
|
|
* Configure of security for show_dumps must be true for debugging.
|
|
* @param var - any type will display type and value of contents
|
|
* @param bool end - if true ends the script
|
|
*/
|
|
public static function dump($var = 'nothing', $end = true): void {
|
|
if (\CodeHydrater\bootstrap\configure::get('security', 'show_dumps') !== true) {
|
|
return;
|
|
}
|
|
if (!is_object($var)) {
|
|
var_dump($var);
|
|
echo '<br>';
|
|
}
|
|
|
|
if ($var === false) {
|
|
echo 'It is FALSE!';
|
|
} elseif ($var === true) {
|
|
echo 'It is TRUE!';
|
|
} elseif (is_resource($var)) {
|
|
echo 'VAR IS a RESOURCE';
|
|
} elseif (is_array($var) && self::get_count($var) == 0) {
|
|
echo 'VAR IS an EMPTY ARRAY!';
|
|
} elseif (is_numeric($var)) {
|
|
echo 'VAR is a NUMBER = ' . $var;
|
|
} elseif (empty($var) && !is_null($var)) {
|
|
echo 'VAR IS EMPTY!';
|
|
} elseif ($var == 'nothing') {
|
|
echo 'MISSING VAR!';
|
|
} elseif (is_null($var)) {
|
|
echo 'VAR IS NULL!';
|
|
} elseif (is_string($var)) {
|
|
echo 'VAR is a STRING = ' . $var;
|
|
} else {
|
|
echo "<pre style=\"border: 1px solid #000; overflow: auto; margin: 0.5em;\">";
|
|
print_r($var);
|
|
echo '</pre>';
|
|
}
|
|
echo '<br><br>';
|
|
|
|
if ($end === true) {
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public static function nl2br(string $text): string {
|
|
return strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />'));
|
|
}
|
|
|
|
}
|
|
|