/
ModuleDataProvider.php
306 lines (270 loc) · 9.48 KB
/
ModuleDataProvider.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
<?php
/**
* 2007-2019 PrestaShop SA and Contributors
*
* 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:
* https://opensource.org/licenses/OSL-3.0
* 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 https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\PrestaShop\Adapter\Module;
use Db;
use Doctrine\ORM\EntityManager;
use Module as LegacyModule;
use PhpParser;
use PrestaShop\PrestaShop\Adapter\Shop\Context;
use PrestaShop\PrestaShop\Core\Addon\Module\AddonListFilterDeviceStatus;
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Tools;
use Validate;
/**
* This class will provide data from DB / ORM about Module.
*/
class ModuleDataProvider
{
/**
* Logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* Translator.
*
* @var \Symfony\Component\Translation\TranslatorInterface
*/
private $translator;
/**
* EntityManager for module history.
*
* @var EntityManager
*/
private $entityManager;
/**
* @var int
*/
private $employeeID;
public function __construct(LoggerInterface $logger, TranslatorInterface $translator, EntityManager $entityManager = null)
{
$this->logger = $logger;
$this->translator = $translator;
$this->entityManager = $entityManager;
$this->employeeID = 0;
}
/**
* @param $employeeID
*/
public function setEmployeeId($employeeID)
{
$this->employeeID = (int) $employeeID;
}
/**
* Return all module information from database.
*
* @param string $name The technical module name to search
*
* @return array
*/
public function findByName($name)
{
$result = Db::getInstance()->getRow('SELECT `id_module` as `id`, `active`, `version` FROM `' . _DB_PREFIX_ . 'module` WHERE `name` = "' . pSQL($name) . '"');
if ($result) {
$result['installed'] = 1;
$result['active'] = $this->isEnabled($name);
$result['active_on_mobile'] = (bool) ($this->getDeviceStatus($name) & AddonListFilterDeviceStatus::DEVICE_MOBILE);
$lastAccessDate = '0000-00-00 00:00:00';
if (!Tools::isPHPCLI() && null !== $this->entityManager && $this->employeeID) {
$moduleID = (int) $result['id'];
$qb = $this->entityManager->createQueryBuilder();
$qb->select('mh')
->from('PrestaShopBundle:ModuleHistory', 'mh', 'mh.idModule')
->where('mh.idEmployee = ?1')
->setParameter(1, $this->employeeID);
$query = $qb->getQuery();
$query->useResultCache(true);
$modulesHistory = $query->getResult();
if (array_key_exists($moduleID, $modulesHistory)) {
$lastAccessDate = $modulesHistory[$moduleID]->getDateUpd()->format('Y-m-d H:i:s');
}
}
$result['last_access_date'] = $lastAccessDate;
return $result;
}
return ['installed' => 0];
}
/**
* Return translated module *Display Name*.
*
* @param string $module The technical module name
*
* @return string The translated Module displayName
*/
public function getModuleName($module)
{
return LegacyModule::getModuleName($module);
}
/**
* Check current employee permission on a given module.
*
* @param string $action
* @param string $name
*
* @return bool True if allowed
*/
public function can($action, $name)
{
$module_id = LegacyModule::getModuleIdByName($name);
if (empty($module_id)) {
return false;
}
return LegacyModule::getPermissionStatic($module_id, $action);
}
/**
* Check if a module is enabled in the current shop context.
*
* @param bool $name The technical module name
*
* @return bool True if enable
*/
public function isEnabled($name)
{
$id_shops = (new Context())->getContextListShopID();
// ToDo: Load list of all installed modules ?
$result = Db::getInstance()->getRow('SELECT m.`id_module` as `active`, ms.`id_module` as `shop_active`
FROM `' . _DB_PREFIX_ . 'module` m
LEFT JOIN `' . _DB_PREFIX_ . 'module_shop` ms ON m.`id_module` = ms.`id_module`
WHERE `name` = "' . pSQL($name) . '"
AND ms.`id_shop` IN (' . implode(',', array_map('intval', $id_shops)) . ')');
if ($result) {
return (bool) ($result['active'] && $result['shop_active']);
} else {
return false;
}
}
public function isInstalled($name)
{
// ToDo: Load list of all installed modules ?
return (bool) $this->getModuleIdByName($name);
}
/**
* Returns the Module Id
*
* @param string $name The technical module name
*
* @return int the Module Id, or 0 if not found
*/
public function getModuleIdByName($name)
{
return (int) Db::getInstance()->getValue(
'SELECT `id_module` FROM `' . _DB_PREFIX_ . 'module` WHERE `name` = "' . pSQL($name) . '"'
);
}
/**
* We won't load an invalid class. This function will check any potential parse error.
*
* @param string $name The technical module name to check
*
* @return bool true if valid
*/
public function isModuleMainClassValid($name)
{
if (!Validate::isModuleName($name)) {
return false;
}
$file_path = _PS_MODULE_DIR_ . $name . '/' . $name . '.php';
// Check if file exists (slightly faster than file_exists)
if (!(int) @filemtime($file_path)) {
return false;
}
$parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::PREFER_PHP7);
try {
$parser->parse(file_get_contents($file_path));
} catch (PhpParser\Error $exception) {
$this->logger->critical(
$this->translator->trans(
'Parse error detected in main class of module %module%: %parse_error%',
array(
'%module%' => $name,
'%parse_error%' => $exception->getMessage(),
),
'Admin.Modules.Notification'
)
);
return false;
}
$logger = $this->logger;
// -> Even if we do not detect any parse error in the file, we may have issues
// when trying to load the file. (i.e with additional require_once).
// -> We use an anonymous function here because if a test is made twice
// on the same module, the test on require_once would immediately return true
// (as the file would have already been evaluated).
$require_correct = function ($name) use ($file_path, $logger) {
try {
require_once $file_path;
} catch (\Exception $e) {
$logger->error(
$this->translator->trans(
'Error while loading file of module %module%. %error_message%',
array(
'%module%' => $name,
'%error_message%' => $e->getMessage(), ),
'Admin.Modules.Notification'
)
);
return false;
}
return true;
};
return $require_correct($name);
}
/**
* Check if the module is in the modules folder, with a valid class.
*
* @param string $name The technical module name to find
*
* @return bool True if found
*/
public function isOnDisk($name)
{
$path = _PS_MODULE_DIR_ . $name . '/' . $name . '.php';
return file_exists($path);
}
/**
* Check if the module has been enabled on mobile.
*
* @param string $name The technical module name to check
*
* @return int The devices enabled for this module
*/
private function getDeviceStatus($name)
{
$id_shops = (new Context())->getContextListShopID();
// ToDo: Load list of all installed modules ?
$result = Db::getInstance()->getRow('SELECT m.`id_module` as `active`, ms.`id_module` as `shop_active`, ms.`enable_device` as `enable_device`
FROM `' . _DB_PREFIX_ . 'module` m
LEFT JOIN `' . _DB_PREFIX_ . 'module_shop` ms ON m.`id_module` = ms.`id_module`
WHERE `name` = "' . pSQL($name) . '"
AND ms.`id_shop` IN (' . implode(',', array_map('intval', $id_shops)) . ')');
if ($result) {
return (int) $result['enable_device'];
}
return false;
}
}