-
Notifications
You must be signed in to change notification settings - Fork 6
/
functions.php
353 lines (314 loc) · 13.7 KB
/
functions.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
348
349
350
351
352
353
<?php
/**
* Copyright (C) 2017-2019 thirty bees
* Copyright (C) 2007-2016 PrestaShop SA
*
* thirty bees is an extension to the PrestaShop software by PrestaShop SA.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-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.
*
* @author thirty bees <[email protected]>
* @author PrestaShop SA <[email protected]>
* @copyright 2017-2019 thirty bees
* @copyright 2007-2016 PrestaShop SA
* @license Academic Free License (AFL 3.0)
* PrestaShop is an internationally registered trademark of PrestaShop SA.
*/
require_once __DIR__.'/classes/autoload.php';
use PsOneSixMigrator\Tools;
/**
* Generate a new settings file, only transmitted parameters are updated
*
* @param string $baseUri Base URI
* @param string $theme Theme name (eg. default)
* @param array $arrayDB Parameters in order to connect to database
*/
function rewriteSettingsFile($baseUrls = null, $theme = null, $arrayDB = null)
{
$defines = [];
$defines['__PS_BASE_URI__'] = ($baseUrls && $baseUrls['__PS_BASE_URI__']) ? $baseUrls['__PS_BASE_URI__'] : __PS_BASE_URI__;
$defines['_MEDIA_SERVER_1_'] = ($baseUrls && isset($baseUrls['_MEDIA_SERVER_1_'])) ? $baseUrls['_MEDIA_SERVER_1_'] : _MEDIA_SERVER_1_;
$defines['_PS_CACHING_SYSTEM_'] = _PS_CACHING_SYSTEM_;
$defines['_PS_CACHE_ENABLED_'] = _PS_CACHE_ENABLED_;
$defines['_THEME_NAME_'] = $theme ? $theme : _THEME_NAME_;
$defines['_DB_NAME_'] = (($arrayDB && isset($arrayDB['_DB_NAME_'])) ? $arrayDB['_DB_NAME_'] : _DB_NAME_);
$defines['_MYSQL_ENGINE_'] = (($arrayDB && isset($arrayDB['_MYSQL_ENGINE_'])) ? $arrayDB['_MYSQL_ENGINE_'] : _MYSQL_ENGINE_);
$defines['_DB_SERVER_'] = (($arrayDB && isset($arrayDB['_DB_SERVER_'])) ? $arrayDB['_DB_SERVER_'] : _DB_SERVER_);
$defines['_DB_USER_'] = (($arrayDB && isset($arrayDB['_DB_USER_'])) ? $arrayDB['_DB_USER_'] : _DB_USER_);
$defines['_DB_PREFIX_'] = (($arrayDB && isset($arrayDB['_DB_PREFIX_'])) ? $arrayDB['_DB_PREFIX_'] : _DB_PREFIX_);
$defines['_DB_PASSWD_'] = (($arrayDB && isset($arrayDB['_DB_PASSWD_'])) ? $arrayDB['_DB_PASSWD_'] : _DB_PASSWD_);
$defines['_DB_TYPE_'] = (($arrayDB && isset($arrayDB['_DB_TYPE_'])) ? $arrayDB['_DB_TYPE_'] : _DB_TYPE_);
$defines['_COOKIE_KEY_'] = addslashes(_COOKIE_KEY_);
$defines['_COOKIE_IV_'] = addslashes(_COOKIE_IV_);
if (defined('_RIJNDAEL_KEY_')) {
$defines['_RIJNDAEL_KEY_'] = addslashes(_RIJNDAEL_KEY_);
}
if (defined('_RIJNDAEL_IV_')) {
$defines['_RIJNDAEL_IV_'] = addslashes(_RIJNDAEL_IV_);
}
$defines['_PS_VERSION_'] = addslashes(_PS_VERSION_);
$content = "<?php\n\n";
foreach ($defines as $k => $value) {
$content .= 'define(\''.$k.'\', \''.addslashes($value).'\');'."\n";
}
$content .= "\n?>";
if ($fd = @fopen(PS_ADMIN_DIR.'/../app/config/parameters.php', 'w')) {
fwrite($fd, $content);
fclose($fd);
return true;
}
return false;
}
/**
* Display SQL date in friendly format
*
* @param string $sqlDate Date in SQL format (YYYY-MM-DD HH:mm:ss)
* @param boolean $withTime Display both date and time
*
* @todo Several formats (french : DD-MM-YYYY)
*/
function displayDate($sqlDate, $withTime = false)
{
return strftime('%Y-%m-%d'.($withTime ? ' %H:%M:%S' : ''), strtotime($sqlDate));
}
/**
* Return path to a product category
*
* @param string $urlBase Start URL
* @param integer $idCategory Start category
* @param string $path Current path
* @param string $highlight String to highlight (in XHTML/CSS)
* @param string $type Category type (products/cms)
*/
function getPath($urlBase, $idCategory, $path = '', $highlight = '', $categoryType = 'catalog')
{
global $cookie;
if ($categoryType == 'catalog') {
$category = Db::getInstance()->getRow(
'
SELECT id_category, level_depth, nleft, nright
FROM '._DB_PREFIX_.'category
WHERE id_category = '.(int) $idCategory
);
if (isset($category['id_category'])) {
$categories = Db::getInstance()->ExecuteS(
'
SELECT c.id_category, cl.name, cl.link_rewrite
FROM '._DB_PREFIX_.'category c
LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = c.id_category)
WHERE c.nleft <= '.(int) $category['nleft'].' AND c.nright >= '.(int) $category['nright'].' AND cl.id_lang = '.(int) ($cookie->id_lang).'
ORDER BY c.level_depth ASC
LIMIT '.(int) ($category['level_depth'] + 1)
);
$fullPath = '';
$n = 1;
$nCategories = (int) sizeof($categories);
foreach ($categories as $category) {
$edit = '<a href="'.$urlBase.'&id_category='.(int) $category['id_category'].'&'.($category['id_category'] == 1 ? 'viewcategory' : 'addcategory').'&token='.Tools::getAdminToken('AdminCatalog'.(int) (Tab::getIdFromClassName('AdminCatalog')).(int) ($cookie->id_employee)).'" title="'.($category['id_category'] == 1 ? 'Home' : 'Modify').'"><img src="../img/admin/'.($category['id_category'] == 1 ? 'home' : 'edit').'.gif" alt="" /></a> ';
$fullPath .= $edit.
($n < $nCategories ? '<a href="'.$urlBase.'&id_category='.(int) $category['id_category'].'&viewcategory&token='.Tools::getAdminToken('AdminCatalog'.(int) (Tab::getIdFromClassName('AdminCatalog')).(int) ($cookie->id_employee)).'" title="'.htmlentities($category['name'], ENT_NOQUOTES, 'UTF-8').'">' : '').
(!empty($highlight) ? str_ireplace($highlight, '<span class="highlight">'.htmlentities($highlight, ENT_NOQUOTES, 'UTF-8').'</span>', $category['name']) : $category['name']).
($n < $nCategories ? '</a>' : '').
(($n++ != $nCategories || !empty($path)) ? ' > ' : '');
}
return $fullPath.$path;
}
} elseif ($categoryType == 'cms') {
$category = new CMSCategory($idCategory, (int) ($cookie->id_lang));
if (!$category->id) {
return $path;
}
$name = ($highlight != null) ? str_ireplace($highlight, '<span class="highlight">'.$highlight.'</span>', CMSCategory::hideCMSCategoryPosition($category->name)) : CMSCategory::hideCMSCategoryPosition($category->name);
$edit = '<a href="'.$urlBase.'&id_cms_category='.$category->id.'&addcategory&token='.Tools::getAdminToken('AdminCMSContent'.(int) (Tab::getIdFromClassName('AdminCMSContent')).(int) ($cookie->id_employee)).'">
<img src="../img/admin/edit.gif" alt="Modify" /></a> ';
if ($category->id == 1) {
$edit = '<a href="'.$urlBase.'&id_cms_category='.$category->id.'&viewcategory&token='.Tools::getAdminToken('AdminCMSContent'.(int) (Tab::getIdFromClassName('AdminCMSContent')).(int) ($cookie->id_employee)).'">
<img src="../img/admin/home.gif" alt="Home" /></a> ';
}
$path = $edit.'<a href="'.$urlBase.'&id_cms_category='.$category->id.'&viewcategory&token='.Tools::getAdminToken('AdminCMSContent'.(int) (Tab::getIdFromClassName('AdminCMSContent')).(int) ($cookie->id_employee)).'">
'.$name.'</a> > '.$path;
if ($category->id == 1) {
return substr($path, 0, strlen($path) - 3);
}
return getPath($urlBase, $category->id_parent, $path, '', 'cms');
}
}
function getDirContent($path)
{
$content = [];
if (is_dir($path)) {
$d = dir($path);
while (false !== ($entry = $d->read())) {
if ($entry{0} != '.') {
$content[] = $entry;
}
}
$d->close();
}
return $content;
}
function createDir($path, $rights)
{
if (file_exists($path)) {
return true;
}
return @mkdir($path, $rights);
}
function recursiveTab($idTab)
{
global $cookie, $tabs;
$adminTab = Tab::getTab((int) $cookie->id_lang, $idTab);
$tabs[] = $adminTab;
if ($adminTab['id_parent'] > 0) {
recursiveTab($adminTab['id_parent']);
}
}
function checkingTab($tab)
{
global $ajaxUpgrader, $cookie;
$tab = trim($tab);
if (!Validate::isTabName($tab)) {
return false;
}
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql = 'SELECT id_tab, module FROM `'._DB_PREFIX_.'tab` WHERE class_name = \''.pSQL($tab).'\'');
if (!$row['id_tab']) {
if (isset(\AdminTab::$tabParenting[$tab])) {
Tools::redirectAdmin('?tab='.\AdminTab::$tabParenting[$tab].'&token='.Tools::getAdminTokenLite(\AdminTab::$tabParenting[$tab]));
}
echo Tools::displayError('Tab cannot be found.');
return false;
}
if ($row['module'] AND file_exists(_PS_MODULE_DIR_.'/'.$row['module'].'/'.$tab.'.php')) {
include_once(_PS_MODULE_DIR_.'/'.$row['module'].'/'.$tab.'.php');
} elseif (file_exists(PS_ADMIN_DIR.'/tabs/'.$tab.'.php')) {
include_once(PS_ADMIN_DIR.'/tabs/'.$tab.'.php');
}
if (!class_exists($tab, false) OR !$row['id_tab']) {
echo Tools::displayError('Tab file cannot be found.');
return false;
}
$ajaxUpgrader = new $tab;
if (!$ajaxUpgrader->viewAccess()) {
$ajaxUpgrader->_errors = [Tools::displayError('Access denied')];
echo $ajaxUpgrader->displayErrors();
return false;
}
return $row['id_tab'];
}
function checkTabRights($idTab)
{
global $cookie;
static $tabAccesses = null;
if ($tabAccesses === null) {
$tabAccesses = Profile::getProfileAccesses($cookie->profile);
}
if (isset($tabAccesses[(int) ($idTab)]['view'])) {
return ($tabAccesses[(int) ($idTab)]['view'] === '1');
}
return false;
}
/**
* Converts a simpleXML element into an array. Preserves attributes and everything.
* You can choose to get your elements either flattened, or stored in a custom index that
* you define.
* For example, for a given element
* <field name="someName" type="someType"/>
* if you choose to flatten attributes, you would get:
* $array['field']['name'] = 'someName';
* $array['field']['type'] = 'someType';
* If you choose not to flatten, you get:
* $array['field']['@attributes']['name'] = 'someName';
* _____________________________________
* Repeating fields are stored in indexed arrays. so for a markup such as:
* <parent>
* <child>a</child>
* <child>b</child>
* <child>c</child>
* </parent>
* you array would be:
* $array['parent']['child'][0] = 'a';
* $array['parent']['child'][1] = 'b';
* ...And so on.
* _____________________________________
*
* @param simpleXMLElement $xml the XML to convert
* @param boolean $flattenValues Choose wether to flatten values
* or to set them under a particular index.
* defaults to true;
* @param boolean $flattenAttributes Choose wether to flatten attributes
* or to set them under a particular index.
* Defaults to true;
* @param boolean $flattenChildren Choose wether to flatten children
* or to set them under a particular index.
* Defaults to true;
* @param string $valueKey index for values, in case $flattenValues was set to
* false. Defaults to "@value"
* @param string $attributesKey index for attributes, in case $flattenAttributes was set to
* false. Defaults to "@attributes"
* @param string $childrenKey index for children, in case $flattenChildren was set to
* false. Defaults to "@children"
*
* @return array the resulting array.
*/
function simpleXMLToArray($xml, $flattenValues = true, $flattenAttributes = true, $flattenChildren = true, $valueKey = '@value', $attributesKey = '@attributes', $childrenKey = '@children')
{
$return = [];
if (!($xml instanceof SimpleXMLElement)) {
return $return;
}
$name = $xml->getName();
$trimmedValue = trim((string) $xml);
if (strlen($trimmedValue) == 0) {
$trimmedValue = null;
}
if ($trimmedValue !== null) {
if (!$flattenValues) {
$return[$valueKey] = $trimmedValue;
} else {
$return = $trimmedValue;
}
}
$children = [];
$first = true;
foreach ($xml->children() as $elementName => $child) {
$value = simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
if (isset($children[$elementName])) {
if ($first) {
$temp = $children[$elementName];
unset($children[$elementName]);
$children[$elementName][] = $temp;
$first = false;
}
$children[$elementName][] = $value;
} else {
$children[$elementName] = $value;
}
}
if (count($children) > 0) {
if (!$flattenChildren) {
$return[$childrenKey] = $children;
} else {
$return = array_merge($return, $children);
}
}
$attributes = [];
foreach ($xml->attributes() as $name => $value) {
$attributes[$name] = trim($value);
}
if (count($attributes) > 0) {
if (!$flattenAttributes) {
$return[$attributesKey] = $attributes;
} else {
$return = array_merge($return, $attributes);
}
}
return $return;
}