651 lines
17 KiB
PHP
651 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* Copyright seit 2024 Webshop System
|
|
*
|
|
* Hilfsfunktionen für das Webshop-System
|
|
*
|
|
* @author Webshop System
|
|
* @license GPL v3
|
|
*/
|
|
|
|
class Tools
|
|
{
|
|
// Security-Konstanten
|
|
public const PASSWORDGEN_FLAG_NUMERIC = 1;
|
|
public const PASSWORDGEN_FLAG_NO_NUMERIC = 2;
|
|
public const PASSWORDGEN_FLAG_RANDOM = 4;
|
|
public const PASSWORDGEN_FLAG_ALPHANUMERIC = 8;
|
|
|
|
// Cache-Konstanten
|
|
public const CACHE_LIFETIME_SECONDS = 604800;
|
|
|
|
// Statische Variablen
|
|
protected static $file_exists_cache = [];
|
|
protected static $_forceCompile;
|
|
protected static $_caching;
|
|
protected static $_string_modifier;
|
|
protected static $_user_plateform;
|
|
protected static $_user_browser;
|
|
protected static $request;
|
|
protected static $cldr_cache = [];
|
|
protected static $colorBrightnessCalculator;
|
|
protected static $fallbackParameters = [];
|
|
|
|
public static $round_mode = null;
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct()
|
|
{
|
|
// Initialisierung
|
|
}
|
|
|
|
/**
|
|
* Redirect to install
|
|
*/
|
|
public static function redirectToInstall()
|
|
{
|
|
header('Location: /install');
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Convert bytes to integer
|
|
*/
|
|
public static function convertBytes($value)
|
|
{
|
|
if (is_numeric($value)) {
|
|
return (int)$value;
|
|
}
|
|
$value_length = strlen($value);
|
|
$qty = (int)substr($value, 0, $value_length - 1);
|
|
$unit = strtolower(substr($value, $value_length - 1));
|
|
switch ($unit) {
|
|
case 'k':
|
|
$qty *= 1024;
|
|
break;
|
|
case 'm':
|
|
$qty *= 1048576;
|
|
break;
|
|
case 'g':
|
|
$qty *= 1073741824;
|
|
break;
|
|
}
|
|
return $qty;
|
|
}
|
|
|
|
/**
|
|
* Check if running in CLI
|
|
*/
|
|
public static function isPHPCLI()
|
|
{
|
|
return (php_sapi_name() === 'cli' || defined('STDIN'));
|
|
}
|
|
|
|
/**
|
|
* Convert argv to GET parameters
|
|
*/
|
|
public static function argvToGET($argc, $argv)
|
|
{
|
|
for ($i = 1; $i < $argc; $i++) {
|
|
if (strpos($argv[$i], '=') !== false) {
|
|
list($key, $value) = explode('=', $argv[$i], 2);
|
|
$_GET[$key] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
// ===== SECURITY FUNCTIONS =====
|
|
|
|
/**
|
|
* Hash password
|
|
*/
|
|
public static function hash($passwd)
|
|
{
|
|
return hash('sha256', $passwd . (defined('_COOKIE_KEY_') ? _COOKIE_KEY_ : 'webshop_key'));
|
|
}
|
|
|
|
/**
|
|
* Hash data with IV
|
|
*/
|
|
public static function hashIV($data)
|
|
{
|
|
return hash('sha256', $data . (defined('_COOKIE_IV_') ? _COOKIE_IV_ : 'webshop_iv'));
|
|
}
|
|
|
|
/**
|
|
* Generate random password
|
|
*/
|
|
public static function passwdGen($length = 8, $flag = self::PASSWORDGEN_FLAG_ALPHANUMERIC)
|
|
{
|
|
$chars = '';
|
|
if ($flag & self::PASSWORDGEN_FLAG_NUMERIC) {
|
|
$chars .= '0123456789';
|
|
}
|
|
if ($flag & self::PASSWORDGEN_FLAG_NO_NUMERIC) {
|
|
$chars .= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
}
|
|
if ($flag & self::PASSWORDGEN_FLAG_ALPHANUMERIC) {
|
|
$chars .= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
}
|
|
if ($flag & self::PASSWORDGEN_FLAG_RANDOM) {
|
|
$chars .= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()';
|
|
}
|
|
|
|
$password = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$password .= $chars[rand(0, strlen($chars) - 1)];
|
|
}
|
|
|
|
return $password;
|
|
}
|
|
|
|
/**
|
|
* Get CSRF token
|
|
*/
|
|
public static function getToken($page = true, $context = null)
|
|
{
|
|
if (!$context) {
|
|
$context = Context::getContext();
|
|
}
|
|
|
|
if ($page === true) {
|
|
$customer_id = isset($context->customer) ? $context->customer->id : 0;
|
|
$customer_passwd = isset($context->customer) ? $context->customer->passwd : '';
|
|
$script_name = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '';
|
|
return self::hash($customer_id . $customer_passwd . $script_name);
|
|
} else {
|
|
$customer_id = isset($context->customer) ? $context->customer->id : 0;
|
|
$customer_passwd = isset($context->customer) ? $context->customer->passwd : '';
|
|
return self::hash($customer_id . $customer_passwd . $page);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get admin token
|
|
*/
|
|
public static function getAdminToken($string)
|
|
{
|
|
return !empty($string) ? self::hash($string) : false;
|
|
}
|
|
|
|
/**
|
|
* Get admin token lite
|
|
*/
|
|
public static function getAdminTokenLite($tab, $context = null)
|
|
{
|
|
if (!$context) {
|
|
$context = Context::getContext();
|
|
}
|
|
|
|
$employee_id = isset($context->employee) ? $context->employee->id : 0;
|
|
return self::getAdminToken($tab . $employee_id);
|
|
}
|
|
|
|
/**
|
|
* Get admin token for Smarty
|
|
*/
|
|
public static function getAdminTokenLiteSmarty($params)
|
|
{
|
|
$context = Context::getContext();
|
|
$employee_id = isset($context->employee) ? $context->employee->id : 0;
|
|
return self::getAdminToken($params['tab'] . $employee_id);
|
|
}
|
|
|
|
/**
|
|
* Get admin URL
|
|
*/
|
|
public static function getAdminUrl($url = null, $entities = false)
|
|
{
|
|
$link = self::getHttpHost(true) . '/admin/';
|
|
|
|
if (isset($url)) {
|
|
$link .= ($entities ? self::htmlentitiesUTF8($url) : $url);
|
|
}
|
|
|
|
return $link;
|
|
}
|
|
|
|
/**
|
|
* Get admin image URL
|
|
*/
|
|
public static function getAdminImageUrl($image = null, $entities = false)
|
|
{
|
|
return self::getAdminUrl('img/' . $image, $entities);
|
|
}
|
|
|
|
// ===== STRING FUNCTIONS =====
|
|
|
|
/**
|
|
* Convert string to URL-friendly format
|
|
*/
|
|
public static function str2url($str)
|
|
{
|
|
$str = strtolower($str);
|
|
$str = preg_replace('/[^a-z0-9\s-]/', '', $str);
|
|
$str = preg_replace('/[\s-]+/', '-', $str);
|
|
$str = trim($str, '-');
|
|
return $str;
|
|
}
|
|
|
|
/**
|
|
* Replace accented characters
|
|
*/
|
|
public static function replaceAccentedChars($str)
|
|
{
|
|
$search = ['à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ'];
|
|
$replace = ['a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'th', 'y'];
|
|
return str_replace($search, $replace, $str);
|
|
}
|
|
|
|
/**
|
|
* Truncate string
|
|
*/
|
|
public static function truncate($str, $max_length, $suffix = '...')
|
|
{
|
|
if (self::strlen($str) <= $max_length) {
|
|
return $str;
|
|
}
|
|
return substr($str, 0, $max_length - self::strlen($suffix)) . $suffix;
|
|
}
|
|
|
|
/**
|
|
* String length with encoding
|
|
*/
|
|
public static function strlen($str, $encoding = 'UTF-8')
|
|
{
|
|
return mb_strlen($str, $encoding);
|
|
}
|
|
|
|
/**
|
|
* String to lower
|
|
*/
|
|
public static function strtolower($str)
|
|
{
|
|
return mb_strtolower($str, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* String to upper
|
|
*/
|
|
public static function strtoupper($str)
|
|
{
|
|
return mb_strtoupper($str, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* Substring with encoding
|
|
*/
|
|
public static function substr($str, $start, $length = false, $encoding = 'UTF-8')
|
|
{
|
|
if ($length === false) {
|
|
return mb_substr($str, $start, null, $encoding);
|
|
}
|
|
return mb_substr($str, $start, $length, $encoding);
|
|
}
|
|
|
|
/**
|
|
* String position
|
|
*/
|
|
public static function strpos($str, $find, $offset = 0, $encoding = 'UTF-8')
|
|
{
|
|
return mb_strpos($str, $find, $offset, $encoding);
|
|
}
|
|
|
|
/**
|
|
* String reverse position
|
|
*/
|
|
public static function strrpos($str, $find, $offset = 0, $encoding = 'UTF-8')
|
|
{
|
|
return mb_strrpos($str, $find, $offset, $encoding);
|
|
}
|
|
|
|
/**
|
|
* Uppercase first
|
|
*/
|
|
public static function ucfirst($str)
|
|
{
|
|
return mb_strtoupper(mb_substr($str, 0, 1, 'UTF-8'), 'UTF-8') . mb_substr($str, 1, null, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* Uppercase words
|
|
*/
|
|
public static function ucwords($str)
|
|
{
|
|
return mb_convert_case($str, MB_CASE_TITLE, 'UTF-8');
|
|
}
|
|
|
|
// ===== UTILITY FUNCTIONS =====
|
|
|
|
/**
|
|
* Check if form is submitted
|
|
*/
|
|
public static function isSubmit($submit)
|
|
{
|
|
return isset($_POST[$submit]) || isset($_GET[$submit]) || isset($_REQUEST[$submit]);
|
|
}
|
|
|
|
/**
|
|
* Get value from POST/GET
|
|
*/
|
|
public static function getValue($key, $default_value = false)
|
|
{
|
|
if (!isset($key) || empty($key)) {
|
|
return false;
|
|
}
|
|
|
|
$ret = (isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : $default_value));
|
|
|
|
if (is_string($ret)) {
|
|
return stripslashes(urldecode(preg_replace('/((\%5C0+)|(\%00+)|(\%08+)|(\%09+)|(\%0A+)|(\%0B+)|(\%0C+)|(\%0D+)|(\%0E+)|(\%0F+))/i', '', $ret)));
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* Get all values
|
|
*/
|
|
public static function getAllValues()
|
|
{
|
|
return $_POST + $_GET;
|
|
}
|
|
|
|
/**
|
|
* Check if value is set
|
|
*/
|
|
public static function getIsset($key)
|
|
{
|
|
return isset($_POST[$key]) || isset($_GET[$key]) || isset($_REQUEST[$key]);
|
|
}
|
|
|
|
/**
|
|
* Safe output
|
|
*/
|
|
public static function safeOutput($string, $html = false)
|
|
{
|
|
if (!$html) {
|
|
$string = strip_tags($string);
|
|
}
|
|
return $string;
|
|
}
|
|
|
|
/**
|
|
* HTML entities UTF8
|
|
*/
|
|
public static function htmlentitiesUTF8($string, $type = ENT_QUOTES)
|
|
{
|
|
return htmlentities($string, $type, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* HTML entities decode UTF8
|
|
*/
|
|
public static function htmlentitiesDecodeUTF8($string)
|
|
{
|
|
return html_entity_decode($string, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* Get HTTP host
|
|
*/
|
|
public static function getHttpHost($http = false, $entities = false, $ignore_port = false)
|
|
{
|
|
$host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST']);
|
|
if ($ignore_port && $pos = strpos($host, ':')) {
|
|
$host = substr($host, 0, $pos);
|
|
}
|
|
if ($entities) {
|
|
$host = htmlspecialchars($host, ENT_COMPAT, 'UTF-8');
|
|
}
|
|
if ($http) {
|
|
$host = self::getCurrentUrlProtocolPrefix() . $host;
|
|
}
|
|
return $host;
|
|
}
|
|
|
|
/**
|
|
* Get current URL protocol prefix
|
|
*/
|
|
public static function getCurrentUrlProtocolPrefix()
|
|
{
|
|
if (self::usingSecureMode()) {
|
|
return 'https://';
|
|
}
|
|
return 'http://';
|
|
}
|
|
|
|
/**
|
|
* Check if using secure mode
|
|
*/
|
|
public static function usingSecureMode()
|
|
{
|
|
return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
|
|
}
|
|
|
|
/**
|
|
* Get remote address
|
|
*/
|
|
public static function getRemoteAddr()
|
|
{
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
|
|
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')) {
|
|
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
|
return trim($ips[0]);
|
|
} else {
|
|
return $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
}
|
|
} elseif (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']) {
|
|
return $_SERVER['HTTP_CLIENT_IP'];
|
|
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR']) {
|
|
return $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Redirect
|
|
*/
|
|
public static function redirect($url, $base_uri = '/', $link = null, $headers = null)
|
|
{
|
|
if (!preg_match('@^https?://@i', $url)) {
|
|
if (strpos($url, $base_uri) === 0) {
|
|
$url = substr($url, strlen($base_uri));
|
|
}
|
|
$url = $base_uri . $url;
|
|
}
|
|
|
|
if ($headers) {
|
|
if (!is_array($headers)) {
|
|
$headers = [$headers];
|
|
}
|
|
foreach ($headers as $header) {
|
|
header($header);
|
|
}
|
|
}
|
|
|
|
header('Location: ' . $url);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Redirect admin
|
|
*/
|
|
public static function redirectAdmin($url)
|
|
{
|
|
header('Location: ' . $url);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Display error
|
|
*/
|
|
public static function displayError($errorMessage = null, $htmlentities = null, $context = null)
|
|
{
|
|
if ($htmlentities === null) {
|
|
$htmlentities = true;
|
|
}
|
|
|
|
if ($errorMessage === null) {
|
|
$errorMessage = 'Fatal error';
|
|
}
|
|
|
|
if ($htmlentities) {
|
|
$errorMessage = htmlentities($errorMessage, ENT_COMPAT, 'UTF-8');
|
|
}
|
|
|
|
echo '<div class="error">' . $errorMessage . '</div>';
|
|
}
|
|
|
|
/**
|
|
* Die object
|
|
*/
|
|
public static function dieObject($object, $kill = true)
|
|
{
|
|
echo '<pre style="text-align: left;">';
|
|
print_r($object);
|
|
echo '</pre><br />';
|
|
if ($kill) {
|
|
exit;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Debug backtrace
|
|
*/
|
|
public static function debug_backtrace($start = 0, $limit = null)
|
|
{
|
|
$backtrace = debug_backtrace();
|
|
if ($limit) {
|
|
$backtrace = array_slice($backtrace, $start, $limit);
|
|
} else {
|
|
$backtrace = array_slice($backtrace, $start);
|
|
}
|
|
return $backtrace;
|
|
}
|
|
|
|
/**
|
|
* Error log
|
|
*/
|
|
public static function error_log($object, $message_type = null, $destination = null, $extra_headers = null)
|
|
{
|
|
if (is_object($object) || is_array($object)) {
|
|
$object = print_r($object, true);
|
|
}
|
|
error_log($object, $message_type, $destination, $extra_headers);
|
|
}
|
|
|
|
/**
|
|
* Reset static cache
|
|
*/
|
|
public static function resetStaticCache()
|
|
{
|
|
static::$cldr_cache = [];
|
|
}
|
|
|
|
/**
|
|
* Reset request
|
|
*/
|
|
public static function resetRequest()
|
|
{
|
|
self::$request = null;
|
|
}
|
|
|
|
/**
|
|
* Replace first occurrence
|
|
*/
|
|
public static function strReplaceFirst($search, $replace, $subject, $cur = 0)
|
|
{
|
|
$strPos = strpos($subject, $search, $cur);
|
|
return $strPos !== false ? substr_replace($subject, $replace, (int) $strPos, strlen($search)) : $subject;
|
|
}
|
|
|
|
/**
|
|
* Replace once
|
|
*/
|
|
public static function str_replace_once($needle, $replace, $haystack)
|
|
{
|
|
$pos = strpos($haystack, $needle);
|
|
if ($pos === false) {
|
|
return $haystack;
|
|
}
|
|
return substr_replace($haystack, $replace, $pos, strlen($needle));
|
|
}
|
|
|
|
/**
|
|
* Check if empty
|
|
*/
|
|
public static function isEmpty($field)
|
|
{
|
|
return ($field === '' || $field === null);
|
|
}
|
|
|
|
/**
|
|
* Format bytes
|
|
*/
|
|
public static function formatBytes($size, $precision = 2)
|
|
{
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
for ($i = 0; $size > 1024 && $i < count($units) - 1; $i++) {
|
|
$size /= 1024;
|
|
}
|
|
return round($size, $precision) . ' ' . $units[$i];
|
|
}
|
|
|
|
/**
|
|
* Boolean value
|
|
*/
|
|
public static function boolVal($value)
|
|
{
|
|
if (is_string($value)) {
|
|
$value = strtolower($value);
|
|
}
|
|
return in_array($value, [true, 1, '1', 'on', 'yes', 'true'], true);
|
|
}
|
|
|
|
/**
|
|
* Get user platform
|
|
*/
|
|
public static function getUserPlatform()
|
|
{
|
|
if (!isset(self::$_user_plateform)) {
|
|
self::$_user_plateform = '';
|
|
if (isset($_SERVER['HTTP_USER_AGENT'])) {
|
|
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Win')) {
|
|
self::$_user_plateform = 'Windows';
|
|
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Mac')) {
|
|
self::$_user_plateform = 'Mac';
|
|
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Linux')) {
|
|
self::$_user_plateform = 'Linux';
|
|
}
|
|
}
|
|
}
|
|
return self::$_user_plateform;
|
|
}
|
|
|
|
/**
|
|
* Get user browser
|
|
*/
|
|
public static function getUserBrowser()
|
|
{
|
|
if (!isset(self::$_user_browser)) {
|
|
self::$_user_browser = '';
|
|
if (isset($_SERVER['HTTP_USER_AGENT'])) {
|
|
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome')) {
|
|
self::$_user_browser = 'Chrome';
|
|
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
|
|
self::$_user_browser = 'Firefox';
|
|
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari')) {
|
|
self::$_user_browser = 'Safari';
|
|
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Edge')) {
|
|
self::$_user_browser = 'Edge';
|
|
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') || strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')) {
|
|
self::$_user_browser = 'Internet Explorer';
|
|
}
|
|
}
|
|
}
|
|
return self::$_user_browser;
|
|
}
|
|
}
|