forked from appixia/cartapi-plugin-prestashop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.php
258 lines (220 loc) · 7.62 KB
/
Helpers.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
<?php
/*
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0).
* It is 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 info@appixia.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize the module for your
* needs please look at the /overrides module directory or refer to
* http://kb.appixia.com for more information.
*
*/
// this is include and not require because it can fail if no override class is found
include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'overrides' . DIRECTORY_SEPARATOR . 'Helpers.php');
class CartAPI_Handlers_Helpers
{
protected static $APPIXIA_COOKIE_NAME = 'appixia';
public static function isAppixiaMobileEngine()
{
global $cookie;
if (isset($cookie))
{
if ($cookie->{self::$APPIXIA_COOKIE_NAME} == 1) return true;
}
return false;
}
public static function setAppixiaMobileEngine()
{
global $cookie;
if (isset($cookie))
{
if ($cookie->{self::$APPIXIA_COOKIE_NAME} != 1) $cookie->{self::$APPIXIA_COOKIE_NAME} = 1;
}
}
public static function unsetAppixiaMobileEngine()
{
global $cookie;
if (isset($cookie))
{
unset($cookie->{self::$APPIXIA_COOKIE_NAME});
}
}
public static function setServerNotices()
{
// enable this if we want to see notices
/*
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/errorlog');
error_reporting(E_ALL);
*/
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/errorlog');
}
public static function getShopDomain()
{
// use built-in method if found
if (method_exists('Tools', 'getShopDomain')) return Tools::getShopDomain(true);
// discover the domain ourselves
if (!($domain = Configuration::get('PS_SHOP_DOMAIN')))
$domain = Tools::getHttpHost();
return 'http://'.$domain;
}
// does end with a slash, last dir is appixiacartapi
public static function getCartApiHomeUrl()
{
return CartAPI_Handlers_Helpers::getShopBaseUrl() . 'modules/appixiacartapi/';
}
// does end with a slash
public static function getShopBaseUrl()
{
return CartAPI_Handlers_Helpers::getShopDomain() . __PS_BASE_URI__;
}
public static function isAbsoluteUrl($url)
{
$prefix = substr($url, 0, 5);
if ($prefix == 'http:') return true;
if ($prefix == 'https') return true;
if ($prefix == 'conf:') return true;
return false;
}
public static function removeHtmlTagsFromString($string)
{
$string = strip_tags($string);
$string = str_replace("\xc2\xa0",' ', $string); // change   to regular spaces
$string = html_entity_decode($string, ENT_QUOTES , 'UTF-8');
return trim($string);
}
// remove html tags from string or array
public static function removeHtmlTags($from)
{
if (is_array($from))
{
$res = array();
foreach ($from as $string) $res[] = CartAPI_Handlers_Helpers::removeHtmlTagsFromString($string);
return $res;
}
else
{
return CartAPI_Handlers_Helpers::removeHtmlTagsFromString($from);
}
}
public static function preInit(&$metadata = array())
{
Configuration::set('PS_CANONICAL_REDIRECT', 0);
// language
if (isset($metadata['X-LANGUAGE'])) $_GET['isolang'] = $metadata['X-LANGUAGE'];
// currency
if (isset($metadata['X-CURRENCY']))
{
$_POST['SubmitCurrency'] = 1;
$_POST['id_currency'] = Currency::getIdByIsoCode($metadata['X-CURRENCY']);
}
// register a new translation smarty function
global $smarty;
if( function_exists('smartyRegisterFunction') ) smartyRegisterFunction($smarty, 'function', 'l2', 'smartyTranslate2');
else $smarty->register_function('l2', 'smartyTranslate2');
// call the override function if found
if (method_exists('CartAPI_Handlers_Override_Helpers', 'preInit')) CartAPI_Handlers_Override_Helpers::preInit($metadata);
}
public static function getLocale()
{
global $cookie;
$locale = array();
// get the currency
$currency = Currency::getCurrencyInstance($cookie->id_currency);
if (is_object($currency) AND $currency->id) $locale['Currency'] = $currency->iso_code;
return $locale;
}
public static function newHandlerInstance($encoder, $handler)
{
$handler_filename = $handler . '.php';
$class_name = 'CartAPI_Handlers_'.$handler;
// load the correct file
if (!file_exists(dirname(__FILE__).'/overrides/'.$handler_filename))
{
// load the base
require_once(dirname(__FILE__).'/'.$handler_filename);
}
else
{
// load the override
$class_name = 'CartAPI_Handlers_Override_'.$handler;
require_once(dirname(__FILE__).'/overrides/'.$handler_filename);
}
// init the class
if (!class_exists($class_name, false)) CartAPI_Helpers::dieOnError($encoder, 'UnsupportedOperation', 'Cannot create instance of '.$handler.' handler');
return new $class_name();
}
public static function compatibilityMailTranslate($str, $id_lang)
{
if (method_exists('Mail','l')) return Mail::l($str, $id_lang);
else return $str; // older prestashop version don't have Mail:l and don't translate using it
}
}
// code taken from config/smarty.config.inc.php smartyTranslate()
function smartyTranslate2($params, &$smarty)
{
/*
* Warning in Smarty-v2 : 2 lines have been added to the Smarty class.
* "public $currentTemplate = null;" into the class itself
* "$this->currentTemplate = Tools::substr(basename($resource_name), 0, -4);" into the "fetch" method
* Notice : before 1.4.2.5, this modification was in the display method
*
* In Smarty-v3 : No modifications, using the existing var $this->smarty->_current_file instead
*/
global $_LANG, $_MODULES, $cookie, $_MODULE;
if (!isset($params['js'])) $params['js'] = 0;
if (!isset($params['mod'])) $params['mod'] = false;
$string = str_replace('\'', '\\\'', $params['s']);
$key = '';
if (Configuration::get('PS_FORCE_SMARTY_2')) /* Keep a backward compatibility for Smarty v2 */
$key = $smarty->currentTemplate.'_'.md5($string);
else
{
// CHANGE //////////////////////////////////
$filename = $params['tpl'].'.tpl';
// CHANGE /////////////////////////////////
$key = Tools::substr(basename($filename), 0, -4).'_'.md5($string);
}
$lang_array = $_LANG;
if ($params['mod'])
{
$iso = Language::getIsoById($cookie->id_lang);
if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$params['mod'].'/'.$iso.'.php'))
{
$translationsFile = _PS_THEME_DIR_.'modules/'.$params['mod'].'/'.$iso.'.php';
$key = '<{'.$params['mod'].'}'._THEME_NAME_.'>'.$key;
}
else
{
$translationsFile = _PS_MODULE_DIR_.$params['mod'].'/'.$iso.'.php';
$key = '<{'.$params['mod'].'}prestashop>'.$key;
}
if(!is_array($_MODULES))
$_MODULES = array();
if (@include_once($translationsFile))
if (is_array($_MODULE))
$_MODULES = array_merge($_MODULES, $_MODULE);
$lang_array = $_MODULES;
}
if (is_array($lang_array) AND key_exists($key, $lang_array))
$msg = $lang_array[$key];
elseif (is_array($lang_array) AND key_exists(Tools::strtolower($key), $lang_array))
$msg = $lang_array[Tools::strtolower($key)];
else
$msg = $params['s'];
if ($msg != $params['s'])
$msg = $params['js'] ? addslashes($msg) : stripslashes($msg);
return $params['js'] ? $msg : Tools::htmlentitiesUTF8($msg);
}
?>