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) { $admin_url = defined('_WEBSHOP_ADMIN_DIR_') ? _WEBSHOP_ADMIN_DIR_ : '/admin/'; if ($url) { $admin_url .= $url; } if ($entities) { $admin_url = htmlentities($admin_url); } return $admin_url; } /** * Get admin image URL */ public static function getAdminImageUrl($image = null, $entities = false) { $admin_img_url = defined('_WEBSHOP_ADMIN_IMG_') ? _WEBSHOP_ADMIN_IMG_ : '/admin/img/'; if ($image) { $admin_img_url .= $image; } if ($entities) { $admin_img_url = htmlentities($admin_img_url); } return $admin_img_url; } /** * Convert string to URL-friendly format */ public static function str2url($str) { $str = self::replaceAccentedChars($str); $str = preg_replace('/[^a-zA-Z0-9\s\-\_]/', '', $str); $str = preg_replace('/[\s\-\_]+/', '-', $str); $str = strtolower(trim($str, '-')); return $str; } /** * Replace accented characters */ public static function replaceAccentedChars($str) { $unwanted_array = [ 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' ]; return strtr($str, $unwanted_array); } /** * Truncate string */ public static function truncate($str, $max_length, $suffix = '...') { if (self::strlen($str) <= $max_length) { return $str; } return self::substr($str, 0, $max_length - self::strlen($suffix)) . $suffix; } /** * Get string length */ public static function strlen($str, $encoding = 'UTF-8') { return mb_strlen($str, $encoding); } /** * Convert to lowercase */ public static function strtolower($str) { return mb_strtolower($str, 'UTF-8'); } /** * Convert to uppercase */ public static function strtoupper($str) { return mb_strtoupper($str, 'UTF-8'); } /** * Get substring */ public static function substr($str, $start, $length = false, $encoding = 'UTF-8') { if ($length === false) { return mb_substr($str, $start, mb_strlen($str, $encoding), $encoding); } return mb_substr($str, $start, $length, $encoding); } /** * Find position of substring */ public static function strpos($str, $find, $offset = 0, $encoding = 'UTF-8') { return mb_strpos($str, $find, $offset, $encoding); } /** * Find last position of substring */ public static function strrpos($str, $find, $offset = 0, $encoding = 'UTF-8') { return mb_strrpos($str, $find, $offset, $encoding); } /** * Convert first character to uppercase */ public static function ucfirst($str) { return self::strtoupper(self::substr($str, 0, 1)) . self::substr($str, 1); } /** * Convert first character of each word to uppercase */ public static function ucwords($str) { return mb_convert_case($str, MB_CASE_TITLE, 'UTF-8'); } /** * Delete directory recursively */ public static function deleteDirectory($dirname, $delete_self = true) { if (!is_dir($dirname)) { return false; } $files = array_diff(scandir($dirname), ['.', '..']); foreach ($files as $file) { $path = $dirname . DIRECTORY_SEPARATOR . $file; if (is_dir($path)) { self::deleteDirectory($path, true); } else { unlink($path); } } if ($delete_self) { return rmdir($dirname); } return true; } /** * Delete file */ public static function deleteFile($file, $exclude_files = []) { if (in_array(basename($file), $exclude_files)) { return true; } if (is_file($file)) { return unlink($file); } return false; } /** * Clear XML cache */ public static function clearXMLCache() { $files = glob(_PS_CACHE_DIR_ . '*.xml'); foreach ($files as $file) { if (is_file($file)) { unlink($file); } } return true; } /** * Check if 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]; } /** * Check if file exists (without cache) */ public static function file_exists_no_cache($filename) { return file_exists($filename); } /** * Refresh CA certificate file */ public static function refreshCACertFile() { $cacert_file = _PS_CACHE_DIR_ . 'cacert.pem'; if (!file_exists($cacert_file) || (time() - filemtime($cacert_file)) > self::CACHE_LIFETIME_SECONDS) { $cacert_content = self::file_get_contents(self::CACERT_LOCATION); if ($cacert_content) { file_put_contents($cacert_file, $cacert_content); return true; } } return false; } /** * Get file contents with cURL */ private static function file_get_contents_curl($url, $curl_timeout, $opts) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, $curl_timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 3); if (is_array($opts)) { curl_setopt_array($ch, $opts); } $content = curl_exec($ch); curl_close($ch); return $content; } /** * Get file contents with fopen */ private static function file_get_contents_fopen($url, $use_include_path, $stream_context) { $opts = [ 'http' => [ 'method' => 'GET', 'timeout' => 5, 'user_agent' => 'Webshop System' ] ]; if ($stream_context) { $context = stream_context_create($opts); } else { $context = null; } return file_get_contents($url, $use_include_path, $context); } /** * Get file contents */ public static function file_get_contents($url, $use_include_path = false, $stream_context = null, $curl_timeout = 5, $fallback = false) { $content = self::file_get_contents_curl($url, $curl_timeout, []); if ($content === false && $fallback) { $content = self::file_get_contents_fopen($url, $use_include_path, $stream_context); } return $content; } /** * Create file from URL */ public static function createFileFromUrl($url) { $content = self::file_get_contents($url); if ($content === false) { return false; } $filename = tempnam(_PS_CACHE_DIR_, 'webshop_'); if (file_put_contents($filename, $content)) { return $filename; } return false; } /** * Load simple XML from file */ public static function simplexml_load_file($url, $class_name = null) { $content = self::file_get_contents($url); if ($content === false) { return false; } return simplexml_load_string($content, $class_name); } /** * Copy file */ public static function copy($source, $destination, $stream_context = null) { if (!$stream_context) { return copy($source, $destination); } return copy($source, $destination, $stream_context); } /** * Recursively copy directory */ public static function recurseCopy($src, $dst, $del = false) { if (!is_dir($src)) { return false; } if (!is_dir($dst)) { mkdir($dst, 0755, true); } $files = array_diff(scandir($src), ['.', '..']); foreach ($files as $file) { $src_file = $src . DIRECTORY_SEPARATOR . $file; $dst_file = $dst . DIRECTORY_SEPARATOR . $file; if (is_dir($src_file)) { self::recurseCopy($src_file, $dst_file, $del); if ($del) { rmdir($src_file); } } else { copy($src_file, $dst_file); if ($del) { unlink($src_file); } } } return true; } /** * Scan directory */ public static function scandir($path, $ext = 'php', $dir = '', $recursive = false) { $files = []; if (!is_dir($path)) { return $files; } $items = scandir($path); foreach ($items as $item) { if ($item == '.' || $item == '..') { continue; } $item_path = $path . DIRECTORY_SEPARATOR . $item; if (is_dir($item_path) && $recursive) { $files = array_merge($files, self::scandir($item_path, $ext, $dir . $item . DIRECTORY_SEPARATOR, $recursive)); } elseif (is_file($item_path) && pathinfo($item_path, PATHINFO_EXTENSION) == $ext) { $files[] = $dir . $item; } } return $files; } /** * 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(); $filemtime = filemtime($file_name); while ($timeout > 0 && ($time - $filemtime) < 0) { sleep(1); $timeout--; clearstatcache(); $filemtime = filemtime($file_name); } return ($time - $filemtime) >= 0; } /** * File attachment */ public static function fileAttachment($input = 'fileUpload', $return_content = true) { if (!isset($_FILES[$input])) { return false; } $file = $_FILES[$input]; if ($file['error'] !== UPLOAD_ERR_OK) { return false; } if ($return_content) { return file_get_contents($file['tmp_name']); } return $file; } /** * Normalize directory */ public static function normalizeDirectory($directory) { return rtrim($directory, '/\\') . DIRECTORY_SEPARATOR; } /** * Get directories */ public static function getDirectories($path) { $directories = []; if (!is_dir($path)) { return $directories; } $items = scandir($path); foreach ($items as $item) { if ($item == '.' || $item == '..') { continue; } $item_path = $path . DIRECTORY_SEPARATOR . $item; if (is_dir($item_path)) { $directories[] = $item; } } return $directories; } /** * Get directories with glob */ public static function getDirectoriesWithGlob($path) { $directories = glob($path . '/*', GLOB_ONLYDIR); if ($directories === false) { return []; } return array_map('basename', $directories); } /** * Get directories with readdir */ public static function getDirectoriesWithReaddir($path) { $directories = []; if (!is_dir($path)) { return $directories; } $handle = opendir($path); while (($item = readdir($handle)) !== false) { if ($item == '.' || $item == '..') { continue; } $item_path = $path . DIRECTORY_SEPARATOR . $item; if (is_dir($item_path)) { $directories[] = $item; } } closedir($handle); return $directories; } /** * Check if form is submitted */ public static function isSubmit($submit) { return isset($_POST[$submit]) || isset($_GET[$submit]); } /** * Get value from POST or GET */ public static function getValue($key, $default_value = false) { if (!isset($key) || empty($key) || !is_string($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+))/i', '', urlencode($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]); } /** * 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_QUOTES, 'UTF-8'); } if ($http) { $host = (self::usingSecureMode() ? 'https://' : 'http://') . $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') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'); } /** * Get remote address */ public static function getRemoteAddr() { if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && (!isset($_SERVER['REMOTE_ADDR']) || preg_match('/^127\.|^10\.|^172\.16|^192\.168\./', $_SERVER['REMOTE_ADDR']))) { $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); return trim($ips[0]); } return $_SERVER['REMOTE_ADDR']; } /** * Redirect */ public static function redirect($url, $base_uri = '/', $link = null, $headers = null) { if (!$link) { $link = Context::getContext()->link; } if (strpos($url, 'http') !== 0) { if (strpos($url, $base_uri) === 0) { $url = substr($url, strlen($base_uri)); } $url = $link->getBaseLink() . ltrim($url, '/'); } if (is_array($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 (!$context) { $context = Context::getContext(); } if (!$htmlentities) { $htmlentities = (bool)Configuration::get('PS_HTML_ERRORS'); } if ($errorMessage) { $errorMessage = $htmlentities ? htmlentities($errorMessage, ENT_QUOTES, 'UTF-8') : $errorMessage; } $context->smarty->assign('error', $errorMessage); } /** * Die object */ public static function dieObject($object, $kill = true) { echo '
';
print_r($object);
echo '