Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Tools extends ToolsCore
- {
- public static function redirect($url, $base_uri = __PS_BASE_URI__, Link $link = null, $headers = null)
- {
- if (!$link)
- $link = Context::getContext()->link;
- //exit();
- if(self::isValidUrl($url))
- if(self::isExternalUrl($url))
- $url="/index.php";
- if (strpos($url, 'http://') === false && strpos($url, 'https://') === false && $link)
- {
- if (strpos($url, $base_uri) === 0)
- $url = substr($url, strlen($base_uri));
- if (strpos($url, 'index.php?controller=') !== false && strpos($url, 'index.php/') == 0)
- {
- $url = substr($url, strlen('index.php?controller='));
- if (Configuration::get('PS_REWRITING_SETTINGS'))
- $url = Tools::strReplaceFirst('&', '?', $url);
- }
- $explode = explode('?', $url);
- // don't use ssl if url is home page
- // used when logout for example
- $use_ssl = !empty($url);
- $url = $link->getPageLink($explode[0], $use_ssl);
- if (isset($explode[1]))
- $url .= '?'.$explode[1];
- }
- // Send additional headers
- if ($headers)
- {
- if (!is_array($headers))
- $headers = array($headers);
- foreach ($headers as $header)
- header($header);
- }
- header('Location: '.$url);
- exit;
- }
- public static function redirectAdmin($url)
- {
- if(self::isValidUrl($url))
- if(self::isExternalUrl($url))
- $url="/index.php";
- header('Location: '.$url);
- exit;
- }
- public static function isExternalUrl($url=null)
- {
- $url=parse_url($url);
- $url=$url['host'];
- if(!is_null($url))
- if(strtolower($_SERVER['SERVER_NAME'])!=strtolower($url))
- return TRUE;
- return FALSE;
- }
- public static function isValidUrl($url){
- if(!$url || !is_string($url))
- return false;
- if(!preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $url))
- return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment