This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Context.php
executable file
·347 lines (299 loc) · 6.7 KB
/
Context.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* 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 [email protected] 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 <[email protected]>
* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if ((bool)Configuration::get('PS_MOBILE_DEVICE'))
require_once(_PS_MODULE_DIR_ . '/mobile_theme/Mobile_Detect.php');
// Retro 1.3, 'class_exists' cause problem with autoload...
if (version_compare(_PS_VERSION_, '1.4', '<'))
{
// Not exist for 1.3
class Shop extends ObjectModel
{
public $id = 1;
public $id_shop_group = 1;
public function __construct()
{
}
public static function getShops()
{
return array(
array('id_shop' => 1, 'name' => 'Default shop')
);
}
public static function getCurrentShop()
{
return 1;
}
}
class Logger
{
public static function AddLog($message, $severity = 2)
{
$fp = fopen(dirname(__FILE__).'/../logs.txt', 'a+');
fwrite($fp, '['.(int)$severity.'] '.Tools::safeOutput($message));
fclose($fp);
}
}
}
// Not exist for 1.3 and 1.4
class Context
{
/**
* @var Context
*/
protected static $instance;
/**
* @var Cart
*/
public $cart;
/**
* @var Customer
*/
public $customer;
/**
* @var Cookie
*/
public $cookie;
/**
* @var Link
*/
public $link;
/**
* @var Country
*/
public $country;
/**
* @var Employee
*/
public $employee;
/**
* @var Controller
*/
public $controller;
/**
* @var Language
*/
public $language;
/**
* @var Currency
*/
public $currency;
/**
* @var AdminTab
*/
public $tab;
/**
* @var Shop
*/
public $shop;
/**
* @var Smarty
*/
public $smarty;
/**
* @ var Mobile Detect
*/
public $mobile_detect;
/**
* @var boolean|string mobile device of the customer
*/
protected $mobile_device;
public function __construct()
{
global $cookie, $cart, $smarty, $link;
$this->tab = null;
$this->cookie = $cookie;
$this->cart = $cart;
$this->smarty = $smarty;
$this->link = $link;
$this->controller = new ControllerBackwardModule();
if (is_object($cookie))
{
$this->currency = new Currency((int)$cookie->id_currency);
$this->language = new Language((int)$cookie->id_lang);
$this->country = new Country((int)$cookie->id_country);
$this->customer = new CustomerBackwardModule((int)$cookie->id_customer);
$this->employee = new Employee((int)$cookie->id_employee);
}
else
{
$this->currency = null;
$this->language = null;
$this->country = null;
$this->customer = null;
$this->employee = null;
}
$this->shop = new ShopBackwardModule();
if ((bool)Configuration::get('PS_MOBILE_DEVICE'))
$this->mobile_detect = new Mobile_Detect();
}
public function getMobileDevice()
{
if (is_null($this->mobile_device))
{
$this->mobile_device = false;
if ($this->checkMobileContext())
{
switch ((int)Configuration::get('PS_MOBILE_DEVICE'))
{
case 0: // Only for mobile device
if ($this->mobile_detect->isMobile() && !$this->mobile_detect->isTablet())
$this->mobile_device = true;
break;
case 1: // Only for touchpads
if ($this->mobile_detect->isTablet() && !$this->mobile_detect->isMobile())
$this->mobile_device = true;
break;
case 2: // For touchpad or mobile devices
if ($this->mobile_detect->isMobile() || $this->mobile_detect->isTablet())
$this->mobile_device = true;
break;
}
}
}
return $this->mobile_device;
}
protected function checkMobileContext()
{
return isset($_SERVER['HTTP_USER_AGENT'])
&& (bool)Configuration::get('PS_MOBILE_DEVICE')
&& !Context::getContext()->cookie->no_mobile;
}
/**
* Get a singleton context
*
* @return Context
*/
public static function getContext()
{
if (!isset(self::$instance))
self::$instance = new Context();
return self::$instance;
}
/**
* Clone current context
*
* @return Context
*/
public function cloneContext()
{
return clone($this);
}
/**
* @return int Shop context type (Shop::CONTEXT_ALL, etc.)
*/
public static function shop()
{
if (!self::$instance->shop->getContextType())
return ShopBackwardModule::CONTEXT_ALL;
return self::$instance->shop->getContextType();
}
}
/**
* Class Shop for Backward compatibility
*/
class ShopBackwardModule extends Shop
{
const CONTEXT_ALL = 1;
public $id = 1;
public $id_shop_group = 1;
public function getContextType()
{
return ShopBackwardModule::CONTEXT_ALL;
}
// Simulate shop for 1.3 / 1.4
public function getID()
{
return 1;
}
/**
* Get shop theme name
*
* @return string
*/
public function getTheme()
{
return _THEME_NAME_;
}
public function isFeatureActive()
{
return false;
}
}
/**
* Class Controller for a Backward compatibility
* Allow to use method declared in 1.5
*/
class ControllerBackwardModule
{
/**
* @param $js_uri
* @return void
*/
public function addJS($js_uri)
{
Tools::addJS($js_uri);
}
/**
* @param $css_uri
* @param string $css_media_type
* @return void
*/
public function addCSS($css_uri, $css_media_type = 'all')
{
Tools::addCSS($css_uri, $css_media_type);
}
public function addJquery()
{
if (_PS_VERSION_ < '1.5')
$this->addJS(_PS_JS_DIR_.'jquery/jquery-1.4.4.min.js');
elseif (_PS_VERSION_ >= '1.5')
$this->addJS(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js');
}
}
/**
* Class Customer for a Backward compatibility
* Allow to use method declared in 1.5
*/
class CustomerBackwardModule extends Customer
{
public $logged = false;
/**
* Check customer informations and return customer validity
*
* @since 1.5.0
* @param boolean $with_guest
* @return boolean customer validity
*/
public function isLogged($with_guest = false)
{
if (!$with_guest && $this->is_guest == 1)
return false;
/* Customer is valid only if it can be load and if object password is the same as database one */
if ($this->logged == 1 && $this->id && Validate::isUnsignedId($this->id) && Customer::checkPassword($this->id, $this->passwd))
return true;
return false;
}
}