1419 lines
37 KiB
PHP
1419 lines
37 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;
|
|
public const CACERT_LOCATION = 'https://curl.haxx.se/ca/cacert.pem';
|
|
|
|
// 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');
|
|
}
|
|
|
|
// ===== FILE OPERATIONS =====
|
|
|
|
/**
|
|
* Delete directory recursively
|
|
*/
|
|
public static function deleteDirectory($dirname, $delete_self = true)
|
|
{
|
|
$dirname = rtrim($dirname, '/') . '/';
|
|
if (file_exists($dirname)) {
|
|
if ($files = scandir($dirname, SCANDIR_SORT_NONE)) {
|
|
foreach ($files as $file) {
|
|
if ($file != '.' && $file != '..' && $file != '.svn') {
|
|
if (is_dir($dirname . $file)) {
|
|
self::deleteDirectory($dirname . $file);
|
|
} elseif (file_exists($dirname . $file)) {
|
|
unlink($dirname . $file);
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($delete_self) {
|
|
if (!rmdir($dirname)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Delete file
|
|
*/
|
|
public static function deleteFile($file, $exclude_files = [])
|
|
{
|
|
if (!is_array($exclude_files)) {
|
|
$exclude_files = [$exclude_files];
|
|
}
|
|
|
|
if (file_exists($file) && is_file($file) && array_search(basename($file), $exclude_files) === false) {
|
|
return unlink($file);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Clear XML cache
|
|
*/
|
|
public static function clearXMLCache()
|
|
{
|
|
$xml_dir = __DIR__ . '/../config/xml/';
|
|
if (is_dir($xml_dir)) {
|
|
foreach (scandir($xml_dir, SCANDIR_SORT_NONE) as $file) {
|
|
$path_info = pathinfo($file, PATHINFO_EXTENSION);
|
|
if (($path_info == 'xml') && ($file != 'default.xml')) {
|
|
self::deleteFile($xml_dir . $file);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* File exists with cache
|
|
*/
|
|
public static function file_exists_cache($filename)
|
|
{
|
|
if (!isset(self::$file_exists_cache[$filename])) {
|
|
self::$file_exists_cache[$filename] = file_exists($filename);
|
|
}
|
|
|
|
return self::$file_exists_cache[$filename];
|
|
}
|
|
|
|
/**
|
|
* File exists without cache
|
|
*/
|
|
public static function file_exists_no_cache($filename)
|
|
{
|
|
clearstatcache();
|
|
return file_exists($filename);
|
|
}
|
|
|
|
/**
|
|
* Refresh CA cert file
|
|
*/
|
|
public static function refreshCACertFile()
|
|
{
|
|
$ca_cert_file = __DIR__ . '/../cache/cacert.pem';
|
|
if (time() - @filemtime($ca_cert_file) > 1296000) {
|
|
$stream_context = @stream_context_create([
|
|
'http' => ['timeout' => 3],
|
|
'ssl' => ['verify_peer' => false]
|
|
]);
|
|
|
|
$ca_cert_content = @file_get_contents(self::CACERT_LOCATION, false, $stream_context);
|
|
if (!empty($ca_cert_content)) {
|
|
file_put_contents($ca_cert_file, $ca_cert_content);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* File get contents with curl
|
|
*/
|
|
private static function file_get_contents_curl($url, $curl_timeout, $opts)
|
|
{
|
|
$content = false;
|
|
|
|
if (function_exists('curl_init')) {
|
|
self::refreshCACertFile();
|
|
$curl = curl_init();
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, $curl_timeout);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
|
|
|
|
if ($opts != null) {
|
|
if (isset($opts['http']['method']) && self::strtolower($opts['http']['method']) == 'post') {
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
if (isset($opts['http']['content'])) {
|
|
parse_str($opts['http']['content'], $post_data);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
|
|
}
|
|
}
|
|
}
|
|
|
|
$content = curl_exec($curl);
|
|
curl_close($curl);
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* File get contents with fopen
|
|
*/
|
|
private static function file_get_contents_fopen($url, $use_include_path, $stream_context)
|
|
{
|
|
$content = false;
|
|
|
|
if (in_array(ini_get('allow_url_fopen'), ['On', 'on', '1'])) {
|
|
$content = @file_get_contents($url, $use_include_path, $stream_context);
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* File get contents
|
|
*/
|
|
public static function file_get_contents($url, $use_include_path = false, $stream_context = null, $curl_timeout = 5, $fallback = false)
|
|
{
|
|
$is_local_file = !preg_match('/^https?:\/\//', $url);
|
|
|
|
if ($is_local_file) {
|
|
$content = @file_get_contents($url, $use_include_path, $stream_context);
|
|
} else {
|
|
$content = self::file_get_contents_curl($url, $curl_timeout, null);
|
|
if (empty($content) && $fallback) {
|
|
$content = self::file_get_contents_fopen($url, $use_include_path, $stream_context);
|
|
}
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* Create file from URL
|
|
*/
|
|
public static function createFileFromUrl($url)
|
|
{
|
|
$scheme = parse_url($url, PHP_URL_SCHEME);
|
|
|
|
if (!in_array(strtolower($scheme), ['http', 'https'], true)) {
|
|
return false;
|
|
}
|
|
|
|
$remoteFile = fopen($url, 'rb');
|
|
if (!$remoteFile) {
|
|
return false;
|
|
}
|
|
|
|
$localFile = fopen(basename($url), 'wb');
|
|
if (!$localFile) {
|
|
fclose($remoteFile);
|
|
return false;
|
|
}
|
|
|
|
while (!feof($remoteFile)) {
|
|
$data = fread($remoteFile, 1024);
|
|
fwrite($localFile, $data, 1024);
|
|
}
|
|
|
|
fclose($remoteFile);
|
|
fclose($localFile);
|
|
|
|
return basename($url);
|
|
}
|
|
|
|
/**
|
|
* SimpleXML load file
|
|
*/
|
|
public static function simplexml_load_file($url, $class_name = null)
|
|
{
|
|
$cache_id = 'Tools::simplexml_load_file' . $url;
|
|
// TODO: Implement cache system
|
|
$result = @simplexml_load_string(self::file_get_contents($url), $class_name);
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Copy file
|
|
*/
|
|
public static function copy($source, $destination, $stream_context = null)
|
|
{
|
|
if (null === $stream_context && !preg_match('/^https?:\/\//', $source)) {
|
|
return @copy($source, $destination);
|
|
}
|
|
|
|
return @file_put_contents($destination, self::file_get_contents($source, false, $stream_context));
|
|
}
|
|
|
|
/**
|
|
* Recurse copy
|
|
*/
|
|
public static function recurseCopy($src, $dst, $del = false)
|
|
{
|
|
if (is_dir($src)) {
|
|
if (!is_dir($dst)) {
|
|
mkdir($dst, 0755, true);
|
|
}
|
|
$files = scandir($src);
|
|
foreach ($files as $file) {
|
|
if ($file != '.' && $file != '..') {
|
|
self::recurseCopy($src . '/' . $file, $dst . '/' . $file, $del);
|
|
}
|
|
}
|
|
if ($del && is_dir($src)) {
|
|
return rmdir($src);
|
|
}
|
|
} elseif (file_exists($src)) {
|
|
if (copy($src, $dst)) {
|
|
if ($del) {
|
|
return unlink($src);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Scan directory
|
|
*/
|
|
public static function scandir($path, $ext = 'php', $dir = '', $recursive = false)
|
|
{
|
|
$items = [];
|
|
$files = scandir($path);
|
|
foreach ($files as $file) {
|
|
if ($file != '.' && $file != '..') {
|
|
if (is_dir($path . $file) && $recursive) {
|
|
$items = array_merge($items, self::scandir($path . $file . '/', $ext, $dir . $file . '/', $recursive));
|
|
} elseif (preg_match('/^.*\.' . $ext . '$/i', $file)) {
|
|
$items[] = $dir . $file;
|
|
}
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
/**
|
|
* Change file modification time
|
|
*/
|
|
public static function changeFileMTime($file_name)
|
|
{
|
|
return @touch($file_name);
|
|
}
|
|
|
|
/**
|
|
* Wait until file is modified
|
|
*/
|
|
public static function waitUntilFileIsModified($file_name, $timeout = 180)
|
|
{
|
|
$time = time();
|
|
while (!file_exists($file_name)) {
|
|
if (time() - $time > $timeout) {
|
|
return false;
|
|
}
|
|
usleep(100000);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* File attachment
|
|
*/
|
|
public static function fileAttachment($input = 'fileUpload', $return_content = true)
|
|
{
|
|
$file_attachment = [];
|
|
if (isset($_FILES[$input])) {
|
|
$file_attachment['rename'] = false;
|
|
if (isset($_FILES[$input]['tmp_name']) && !empty($_FILES[$input]['tmp_name'])) {
|
|
$file_attachment['tmp_name'] = $_FILES[$input]['tmp_name'];
|
|
$file_attachment['name'] = $_FILES[$input]['name'];
|
|
$file_attachment['mime'] = $_FILES[$input]['type'];
|
|
$file_attachment['error'] = $_FILES[$input]['error'];
|
|
$file_attachment['size'] = $_FILES[$input]['size'];
|
|
}
|
|
}
|
|
return $file_attachment;
|
|
}
|
|
|
|
/**
|
|
* Normalize directory
|
|
*/
|
|
public static function normalizeDirectory($directory)
|
|
{
|
|
return rtrim($directory, '/\\') . DIRECTORY_SEPARATOR;
|
|
}
|
|
|
|
/**
|
|
* Get directories
|
|
*/
|
|
public static function getDirectories($path)
|
|
{
|
|
$dirs = [];
|
|
if (is_dir($path)) {
|
|
$files = scandir($path);
|
|
foreach ($files as $file) {
|
|
if ($file != '.' && $file != '..' && is_dir($path . $file)) {
|
|
$dirs[] = $file;
|
|
}
|
|
}
|
|
}
|
|
return $dirs;
|
|
}
|
|
|
|
/**
|
|
* Get directories with glob
|
|
*/
|
|
public static function getDirectoriesWithGlob($path)
|
|
{
|
|
$dirs = [];
|
|
if (is_dir($path)) {
|
|
$dirs = glob($path . '*', GLOB_ONLYDIR);
|
|
$dirs = array_map('basename', $dirs);
|
|
}
|
|
return $dirs;
|
|
}
|
|
|
|
/**
|
|
* Get directories with readdir
|
|
*/
|
|
public static function getDirectoriesWithReaddir($path)
|
|
{
|
|
$dirs = [];
|
|
if (is_dir($path)) {
|
|
$handle = opendir($path);
|
|
while (false !== ($file = readdir($handle))) {
|
|
if ($file != '.' && $file != '..' && is_dir($path . $file)) {
|
|
$dirs[] = $file;
|
|
}
|
|
}
|
|
closedir($handle);
|
|
}
|
|
return $dirs;
|
|
}
|
|
|
|
// ===== 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;
|
|
}
|
|
|
|
/**
|
|
* Round a value to specified precision using PrestaShop rounding mode
|
|
*
|
|
* @param float $value
|
|
* @param int $precision
|
|
* @param int|null $round_mode
|
|
* @return float
|
|
*/
|
|
public static function ps_round($value, $precision = 0, $round_mode = null)
|
|
{
|
|
if ($round_mode === null) {
|
|
if (self::$round_mode == null) {
|
|
self::$round_mode = (int) Configuration::get('PS_PRICE_ROUND_MODE');
|
|
}
|
|
$round_mode = self::$round_mode;
|
|
}
|
|
|
|
switch ($round_mode) {
|
|
case PS_ROUND_UP:
|
|
return self::ceilf($value, $precision);
|
|
case PS_ROUND_DOWN:
|
|
return self::floorf($value, $precision);
|
|
case PS_ROUND_HALF_DOWN:
|
|
case PS_ROUND_HALF_EVEN:
|
|
case PS_ROUND_HALF_ODD:
|
|
case PS_ROUND_HALF_UP:
|
|
default:
|
|
return round($value, $precision, $round_mode - 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Math round wrapper for backward compatibility
|
|
*
|
|
* @param float $value
|
|
* @param int $places
|
|
* @param int $mode
|
|
* @return float
|
|
*/
|
|
public static function math_round($value, $places, $mode = PS_ROUND_HALF_UP)
|
|
{
|
|
return self::ps_round($value, $places, $mode);
|
|
}
|
|
|
|
/**
|
|
* Round helper function
|
|
*
|
|
* @param float $value
|
|
* @param int $mode
|
|
* @return float
|
|
*/
|
|
public static function round_helper($value, $mode)
|
|
{
|
|
if ($value >= 0.0) {
|
|
$tmp_value = floor($value + 0.5);
|
|
|
|
if (
|
|
($mode == PS_ROUND_HALF_DOWN && $value == (-0.5 + $tmp_value))
|
|
|| ($mode == PS_ROUND_HALF_EVEN && $value == (0.5 + 2 * floor($tmp_value / 2.0)))
|
|
|| ($mode == PS_ROUND_HALF_ODD && $value == (0.5 + 2 * floor($tmp_value / 2.0) - 1.0))
|
|
) {
|
|
$tmp_value = $tmp_value - 1.0;
|
|
}
|
|
} else {
|
|
$tmp_value = ceil($value - 0.5);
|
|
|
|
if (
|
|
($mode == PS_ROUND_HALF_DOWN && $value == (0.5 + $tmp_value))
|
|
|| ($mode == PS_ROUND_HALF_EVEN && $value == (-0.5 + 2 * ceil($tmp_value / 2.0)))
|
|
|| ($mode == PS_ROUND_HALF_ODD && $value == (-0.5 + 2 * ceil($tmp_value / 2.0) + 1.0))
|
|
) {
|
|
$tmp_value = $tmp_value + 1.0;
|
|
}
|
|
}
|
|
|
|
return $tmp_value;
|
|
}
|
|
|
|
/**
|
|
* Returns the rounded value up of $value to specified precision
|
|
*
|
|
* @param float $value
|
|
* @param int $precision
|
|
* @return float
|
|
*/
|
|
public static function ceilf($value, $precision = 0)
|
|
{
|
|
$precision_factor = $precision == 0 ? 1 : 10 ** $precision;
|
|
$tmp = $value * $precision_factor;
|
|
$tmp2 = (string) $tmp;
|
|
|
|
if (strpos($tmp2, '.') === false) {
|
|
return $value;
|
|
}
|
|
if ($tmp2[strlen($tmp2) - 1] == 0) {
|
|
return $value;
|
|
}
|
|
|
|
return ceil($tmp) / $precision_factor;
|
|
}
|
|
|
|
/**
|
|
* Returns the rounded value down of $value to specified precision
|
|
*
|
|
* @param float $value
|
|
* @param int $precision
|
|
* @return float
|
|
*/
|
|
public static function floorf($value, $precision = 0)
|
|
{
|
|
$precision_factor = $precision == 0 ? 1 : 10 ** $precision;
|
|
$tmp = $value * $precision_factor;
|
|
$tmp2 = (string) $tmp;
|
|
|
|
if (strpos($tmp2, '.') === false) {
|
|
return $value;
|
|
}
|
|
if ($tmp2[strlen($tmp2) - 1] == 0) {
|
|
return $value;
|
|
}
|
|
|
|
return floor($tmp) / $precision_factor;
|
|
}
|
|
|
|
/**
|
|
* Spread amount across rows with precision
|
|
*
|
|
* @param float $amount
|
|
* @param int $precision
|
|
* @param array $rows
|
|
* @param string $column
|
|
*/
|
|
public static function spreadAmount($amount, $precision, &$rows, $column)
|
|
{
|
|
$total = 0;
|
|
$count = count($rows);
|
|
|
|
if ($count == 0) {
|
|
return;
|
|
}
|
|
|
|
foreach ($rows as $row) {
|
|
$total += $row[$column];
|
|
}
|
|
|
|
if ($total == 0) {
|
|
return;
|
|
}
|
|
|
|
$diff = $amount - $total;
|
|
$diff = self::ps_round($diff, $precision);
|
|
|
|
if ($diff != 0) {
|
|
$rows[0][$column] += $diff;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enable cache for Smarty
|
|
*
|
|
* @param int $level
|
|
* @param Context|null $context
|
|
*/
|
|
public static function enableCache($level = 1, ?Context $context = null)
|
|
{
|
|
if (!$context) {
|
|
$context = Context::getContext();
|
|
}
|
|
$smarty = $context->smarty;
|
|
if (!Configuration::get('PS_SMARTY_CACHE')) {
|
|
return;
|
|
}
|
|
if ($smarty->force_compile == 0 && $smarty->caching == $level) {
|
|
return;
|
|
}
|
|
self::$_forceCompile = (int) $smarty->force_compile;
|
|
self::$_caching = (int) $smarty->caching;
|
|
$smarty->force_compile = false;
|
|
$smarty->caching = (int) $level;
|
|
$smarty->cache_lifetime = 31536000; // 1 Year
|
|
}
|
|
|
|
/**
|
|
* Restore cache settings
|
|
*
|
|
* @param Context|null $context
|
|
*/
|
|
public static function restoreCacheSettings(?Context $context = null)
|
|
{
|
|
if (!$context) {
|
|
$context = Context::getContext();
|
|
}
|
|
|
|
if (isset(self::$_forceCompile)) {
|
|
$context->smarty->force_compile = (bool) self::$_forceCompile;
|
|
}
|
|
if (isset(self::$_caching)) {
|
|
$context->smarty->caching = (int) self::$_caching;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clear cache for Smarty
|
|
*
|
|
* @param Smarty|null $smarty
|
|
* @param bool|string $tpl
|
|
* @param string|null $cache_id
|
|
* @param string|null $compile_id
|
|
* @return int|null
|
|
*/
|
|
public static function clearCache($smarty = null, $tpl = false, $cache_id = null, $compile_id = null)
|
|
{
|
|
if ($smarty === null) {
|
|
$smarty = Context::getContext()->smarty;
|
|
}
|
|
|
|
if ($smarty === null) {
|
|
return null;
|
|
}
|
|
|
|
if (!$tpl && $cache_id === null && $compile_id === null) {
|
|
return $smarty->clearAllCache();
|
|
}
|
|
|
|
$ret = $smarty->clearCache($tpl, $cache_id, $compile_id);
|
|
|
|
Hook::exec('actionClearCache');
|
|
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* Clear compile for Smarty
|
|
*
|
|
* @param Smarty|null $smarty
|
|
* @return int|null
|
|
*/
|
|
public static function clearCompile($smarty = null)
|
|
{
|
|
if ($smarty === null) {
|
|
$smarty = Context::getContext()->smarty;
|
|
}
|
|
|
|
if ($smarty === null) {
|
|
return null;
|
|
}
|
|
|
|
$ret = $smarty->clearCompiledTemplate();
|
|
|
|
Hook::exec('actionClearCompileCache');
|
|
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* Clear Smarty cache and compile folders
|
|
*/
|
|
public static function clearSmartyCache()
|
|
{
|
|
$smarty = Context::getContext()->smarty;
|
|
self::clearCache($smarty);
|
|
self::clearCompile($smarty);
|
|
}
|
|
|
|
/**
|
|
* Clear Symfony cache
|
|
*
|
|
* @param string|null $env
|
|
*/
|
|
public static function clearSf2Cache($env = null)
|
|
{
|
|
if (null === $env) {
|
|
$env = _PS_ENV_;
|
|
}
|
|
|
|
$dir = _PS_ROOT_DIR_ . '/var/cache/' . $env . '/';
|
|
|
|
register_shutdown_function(function () use ($dir) {
|
|
$fs = new Filesystem();
|
|
$fs->remove($dir);
|
|
Hook::exec('actionClearSf2Cache');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Clear both Smarty and Symfony cache
|
|
*/
|
|
public static function clearAllCache()
|
|
{
|
|
self::clearSmartyCache();
|
|
self::clearSf2Cache();
|
|
}
|
|
|
|
/**
|
|
* Get memory limit in octets
|
|
*
|
|
* @return int|string
|
|
*/
|
|
public static function getMemoryLimit()
|
|
{
|
|
$memory_limit = @ini_get('memory_limit');
|
|
|
|
return self::getOctets($memory_limit);
|
|
}
|
|
|
|
/**
|
|
* Get octets from configuration option
|
|
*
|
|
* @param string $option
|
|
* @return int|string
|
|
*/
|
|
public static function getOctets($option)
|
|
{
|
|
if (preg_match('/[0-9]+k/i', $option)) {
|
|
return 1024 * (int) $option;
|
|
}
|
|
|
|
if (preg_match('/[0-9]+m/i', $option)) {
|
|
return 1024 * 1024 * (int) $option;
|
|
}
|
|
|
|
if (preg_match('/[0-9]+g/i', $option)) {
|
|
return 1024 * 1024 * 1024 * (int) $option;
|
|
}
|
|
|
|
return $option;
|
|
}
|
|
|
|
/**
|
|
* Check if server uses 64bit architecture
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function isX86_64arch()
|
|
{
|
|
return PHP_INT_MAX == '9223372036854775807';
|
|
}
|
|
|
|
/**
|
|
* Check if php-cli is used
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function isPHPCLI()
|
|
{
|
|
return defined('STDIN') || (self::strtolower(PHP_SAPI) == 'cli' && (!isset($_SERVER['REMOTE_ADDR']) || empty($_SERVER['REMOTE_ADDR'])));
|
|
}
|
|
|
|
/**
|
|
* Convert argv to GET parameters
|
|
*
|
|
* @param int $argc
|
|
* @param array $argv
|
|
*/
|
|
public static function argvToGET($argc, $argv)
|
|
{
|
|
if ($argc <= 1) {
|
|
return;
|
|
}
|
|
|
|
parse_str($argv[1], $args);
|
|
if (!is_array($args) || !count($args)) {
|
|
return;
|
|
}
|
|
$_GET = array_merge($args, $_GET);
|
|
$_SERVER['QUERY_STRING'] = $argv[1];
|
|
}
|
|
|
|
/**
|
|
* Get max file upload size
|
|
*
|
|
* @param int $max_size
|
|
* @return int
|
|
*/
|
|
public static function getMaxUploadSize($max_size = 0)
|
|
{
|
|
$values = [self::convertBytes(ini_get('upload_max_filesize'))];
|
|
|
|
if ($max_size > 0) {
|
|
$values[] = $max_size;
|
|
}
|
|
|
|
$post_max_size = self::convertBytes(ini_get('post_max_size'));
|
|
if ($post_max_size > 0) {
|
|
$values[] = $post_max_size;
|
|
}
|
|
|
|
return min($values);
|
|
}
|
|
}
|