init.php 5.05 KB
Newer Older
Rémi Gaillard's avatar
Rémi Gaillard committed
1 2
<?php
/*
3
* 2007-2015 PrestaShop
Rémi Gaillard's avatar
Rémi Gaillard committed
4 5 6
*
* NOTICE OF LICENSE
*
7
* This source file is subject to the Open Software License (OSL 3.0)
Rémi Gaillard's avatar
Rémi Gaillard committed
8 9
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
10
* http://opensource.org/licenses/osl-3.0.php
Rémi Gaillard's avatar
Rémi Gaillard committed
11 12 13 14 15 16 17 18 19 20 21
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <contact@prestashop.com>
22
*  @copyright  2007-2015 PrestaShop SA
23
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
Rémi Gaillard's avatar
Rémi Gaillard committed
24 25 26 27 28 29
*  International Registered Trademark & Property of PrestaShop SA
*/

ob_start();
$timerStart = microtime(true);

30 31 32
//	$_GET['tab'] = $_GET['controller'];
//	$_POST['tab'] = $_POST['controller'];
//	$_REQUEST['tab'] = $_REQUEST['controller'];
33 34 35 36 37
try
{
	$context = Context::getContext();
	if (isset($_GET['logout']))
		$context->employee->logout();
38

39 40
	if (!isset($context->employee) || !$context->employee->isLoggedBack())
		Tools::redirectAdmin('index.php?controller=AdminLogin&redirect='.$_SERVER['REQUEST_URI']);
Rémi Gaillard's avatar
Rémi Gaillard committed
41

42
	// Set current index
43
	// @deprecated global
44
	global $currentIndex; // retrocompatibility;
45
	$currentIndex = $_SERVER['SCRIPT_NAME'].(($controller = Tools::getValue('controller')) ? '?controller='.$controller: '');
tDidierjean's avatar
tDidierjean committed
46

47 48 49
	if ($back = Tools::getValue('back'))
		$currentIndex .= '&back='.urlencode($back);
	AdminTab::$currentIndex = $currentIndex;
50

51
	$iso = $context->language->iso_code;
52 53 54 55 56 57
	if (file_exists(_PS_TRANSLATIONS_DIR_.$iso.'/errors.php'))
		include(_PS_TRANSLATIONS_DIR_.$iso.'/errors.php');
	if (file_exists(_PS_TRANSLATIONS_DIR_.$iso.'/fields.php'))
		include(_PS_TRANSLATIONS_DIR_.$iso.'/fields.php');
	if (file_exists(_PS_TRANSLATIONS_DIR_.$iso.'/admin.php'))
		include(_PS_TRANSLATIONS_DIR_.$iso.'/admin.php');
58

59 60 61 62 63
	/* Server Params */
	$protocol_link = (Configuration::get('PS_SSL_ENABLED')) ? 'https://' : 'http://';
	$protocol_content = (isset($useSSL) AND $useSSL AND Configuration::get('PS_SSL_ENABLED')) ? 'https://' : 'http://';
	$link = new Link($protocol_link, $protocol_content);
	$context->link = $link;
64 65 66 67
	if (!defined('_PS_BASE_URL_'))
		define('_PS_BASE_URL_', Tools::getShopDomain(true));
	if (!defined('_PS_BASE_URL_SSL_'))
		define('_PS_BASE_URL_SSL_', Tools::getShopDomainSsl(true));
rMalie's avatar
rMalie committed
68

69 70 71 72
	$path = dirname(__FILE__).'/themes/';
	// if the current employee theme is not valid (check layout.tpl presence), 
	// reset to default theme
	if (empty($context->employee->bo_theme) ||
mMarinetti's avatar
mMarinetti committed
73
		!file_exists($path.$context->employee->bo_theme.'/template/layout.tpl'))
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	{
		// default admin theme is "default".
		$context->employee->bo_theme = '';
		if (file_exists($path.'default/template/layout.tpl'))
			$context->employee->bo_theme = 'default';
		else
		{
			// if default theme doesn't exists, try to find one, otherwise throw exception
			foreach (scandir($path) as $theme)
				if ($theme[0] != '.' && file_exists($path.$theme.'/template/layout.tpl'))
				{
					$context->employee->bo_theme = $theme;
					break;
				}
			// if no theme is found, admin can't work.
			if (empty($context->employee->bo_theme))
				throw new PrestaShopException('Unable to load theme for employee, and no valid theme found');
		}
		$context->employee->update();
	}
Rémi Gaillard's avatar
Rémi Gaillard committed
94

95 96 97 98 99 100 101 102
	// Change shop context ?
	if (Shop::isFeatureActive() && Tools::getValue('setShopContext') !== false)
	{
		$context->cookie->shopContext = Tools::getValue('setShopContext');
		$url = parse_url($_SERVER['REQUEST_URI']);
		$query = (isset($url['query'])) ? $url['query'] : '';
		parse_str($query, $parseQuery);
		unset($parseQuery['setShopContext']);
103
		Tools::redirectAdmin($url['path'] . '?' . http_build_query($parseQuery, '', '&'));
104
	}
105

106
	$context->currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
107

108 109

	if ($context->employee->isLoggedBack())
110
	{
111 112 113
		$shop_id = '';
		Shop::setContext(Shop::CONTEXT_ALL);
		if ($context->cookie->shopContext)
114
		{
115 116
			$split = explode('-', $context->cookie->shopContext);
			if (count($split) == 2)
117
			{
118 119 120 121 122 123 124 125 126 127
				if ($split[0] == 'g')
				{
					if ($context->employee->hasAuthOnShopGroup($split[1]))
						Shop::setContext(Shop::CONTEXT_GROUP, $split[1]);
					else
					{
						$shop_id = $context->employee->getDefaultShopID();
						Shop::setContext(Shop::CONTEXT_SHOP, $shop_id);
					}
				}
dMetzger's avatar
dMetzger committed
128
				elseif ($context->employee->hasAuthOnShop($split[1]))
129 130 131 132
				{
					$shop_id = $split[1];
					Shop::setContext(Shop::CONTEXT_SHOP, $shop_id);
				}
133 134
				else
				{
135
					$shop_id = $context->employee->getDefaultShopID();
136 137 138
					Shop::setContext(Shop::CONTEXT_SHOP, $shop_id);
				}
			}
139 140
		}

141 142 143 144 145 146
		// Replace existing shop if necessary
		if (!$shop_id)
			$context->shop = new Shop(Configuration::get('PS_SHOP_DEFAULT'));
		else if ($context->shop->id != $shop_id)
			$context->shop = new Shop($shop_id);
	}
147 148
}
catch(PrestaShopException $e)
149
{
150
	$e->displayMessage();
151
}