1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26class Tools extends ToolsCore
27{
28    public static function redirect($url, $base_uri = __PS_BASE_URI__, Link $link = null, $headers = null)
29    {
30        if (!$link) {
31            $link = Context::getContext()->link;
32        }
33
34        if (strpos($url, 'http://') === false && strpos($url, 'https://') === false && $link) {
35            if (strpos($url, $base_uri) === 0) {
36                $url = substr($url, strlen($base_uri));
37            }
38            if (strpos($url, 'index.php?controller=') !== false && strpos($url, 'index.php/') == 0) {
39                $url = substr($url, strlen('index.php?controller='));
40                if (Configuration::get('PS_REWRITING_SETTINGS')) {
41                    $url = static::strReplaceFirst('&', '?', $url);
42                }
43            }
44
45            $explode = explode('?', $url);
46            // don't use ssl if url is home page
47            // used when logout for example
48            $use_ssl = !empty($url);
49            $url = $link->getPageLink($explode[0], $use_ssl);
50            if (isset($explode[1])) {
51                $url .= '?' . $explode[1];
52            }
53        }
54
55        // Send additional headers
56        if ($headers) {
57            if (!is_array($headers)) {
58                $headers = [$headers];
59            }
60
61            foreach ($headers as $header) {
62                header($header);
63            }
64        }
65
66        Context::getContext()->controller->setRedirectAfter($url);
67    }
68
69    public static function getDefaultControllerClass()
70    {
71        if (isset(Context::getContext()->employee) && Validate::isLoadedObject(Context::getContext()->employee) && isset(Context::getContext()->employee->default_tab)) {
72            $default_controller = Tab::getClassNameById((int) Context::getContext()->employee->default_tab);
73        }
74        if (empty($default_controller)) {
75            $default_controller = 'AdminDashboard';
76        }
77        $controllers = Dispatcher::getControllers([_PS_ADMIN_DIR_ . '/tabs/', _PS_ADMIN_CONTROLLER_DIR_, _PS_OVERRIDE_DIR_ . 'controllers/admin/']);
78        if (!isset($controllers[strtolower($default_controller)])) {
79            $default_controller = 'adminnotfound';
80        }
81        $controller_class = $controllers[strtolower($default_controller)];
82
83        return $controller_class;
84    }
85
86    public static function redirectLink($url)
87    {
88        if (!preg_match('@^https?://@i', $url)) {
89            if (strpos($url, __PS_BASE_URI__) !== false && strpos($url, __PS_BASE_URI__) == 0) {
90                $url = substr($url, strlen(__PS_BASE_URI__));
91            }
92            $explode = explode('?', $url);
93            $url = Context::getContext()->link->getPageLink($explode[0]);
94            if (isset($explode[1])) {
95                $url .= '?' . $explode[1];
96            }
97        }
98    }
99
100    public static function redirectAdmin($url)
101    {
102        if (!is_object(Context::getContext()->controller)) {
103            try {
104                $controller = Controller::getController(static::getDefaultControllerClass());
105                $controller->setRedirectAfter($url);
106                $controller->run();
107                Context::getContext()->controller = $controller;
108                die();
109            } catch (PrestaShopException $e) {
110                $e->displayMessage();
111            }
112        } else {
113            Context::getContext()->controller->setRedirectAfter($url);
114        }
115    }
116}
117