Repository navigation
Expand file tree
/
Copy pathdfcodeinjector.php
More file actions
325 lines (272 loc) · 9.45 KB
/
Copy pathdfcodeinjector.php
File metadata and controls
325 lines (272 loc) · 9.45 KB
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
<?php
/**
* Header/Footer Code Injector (CSS/JS/HTML)
*
* @author DataFirefly
* @copyright DataFirefly Limited
* @license Commercial
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once __DIR__ . '/classes/DfCodeSnippet.php';
class DfCodeInjector extends Module
{
/** @var array|null Snippets of the current request, grouped by location */
protected static $requestSnippets;
/** @var array Body locations whose theme hook ran during the current request */
protected static $renderedByTheme = [];
public function __construct()
{
$this->name = 'dfcodeinjector';
$this->tab = 'administration';
$this->version = '1.1.0';
$this->author = 'DataFirefly';
$this->need_instance = 0;
$this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Header/Footer Code Injector');
$this->description = $this->l('Inject custom CSS, JS or HTML code into the head tag, right after the opening body tag or right before the closing body tag. Page targeting, ordering, test mode and multistore.');
$this->confirmUninstall = $this->l('All saved code snippets will be permanently deleted. Continue?');
}
public function install()
{
return parent::install()
&& DfCodeSnippet::ensureSchema()
&& $this->installTab()
&& $this->registerHook([
'displayHeader',
'displayAfterBodyOpeningTag',
'displayBeforeBodyClosingTag',
'actionOutputHTMLBefore',
]);
}
public function uninstall()
{
return $this->uninstallTab()
&& DfCodeSnippet::dropSchema()
&& parent::uninstall();
}
protected function installTab()
{
if (Tab::getIdFromClassName('AdminDfCodeInjector')) {
return true;
}
$names = [
'en' => 'Code Injector',
'fr' => 'Injecteur de code',
'es' => 'Inyector de código',
'de' => 'Code-Injektor',
'it' => 'Iniettore di codice',
'nl' => 'Code-injector',
'pl' => 'Wstrzykiwanie kodu',
'pt' => 'Injetor de código',
];
$tab = new Tab();
$tab->class_name = 'AdminDfCodeInjector';
$tab->module = $this->name;
$tab->id_parent = -1; // hidden tab, opened through the module "Configure" button
$tab->active = true;
$tab->name = [];
foreach (Language::getLanguages(false) as $lang) {
$iso = Tools::strtolower($lang['iso_code']);
$tab->name[(int) $lang['id_lang']] = isset($names[$iso]) ? $names[$iso] : $names['en'];
}
return (bool) $tab->add();
}
protected function uninstallTab()
{
$idTab = (int) Tab::getIdFromClassName('AdminDfCodeInjector');
if (!$idTab) {
return true;
}
return (bool) (new Tab($idTab))->delete();
}
/**
* The "Configure" button opens the snippet manager.
*/
public function getContent()
{
Tools::redirectAdmin($this->context->link->getAdminLink('AdminDfCodeInjector'));
}
/* ----------------------------------------------------------------------
* INJECTION HOOKS
* -------------------------------------------------------------------- */
public function hookDisplayHeader($params)
{
return $this->renderLocation(DfCodeSnippet::LOCATION_HEAD);
}
public function hookDisplayAfterBodyOpeningTag($params)
{
self::$renderedByTheme[DfCodeSnippet::LOCATION_BODY_OPEN] = true;
return $this->renderLocation(DfCodeSnippet::LOCATION_BODY_OPEN);
}
public function hookDisplayBeforeBodyClosingTag($params)
{
self::$renderedByTheme[DfCodeSnippet::LOCATION_BODY_CLOSE] = true;
return $this->renderLocation(DfCodeSnippet::LOCATION_BODY_CLOSE);
}
/**
* Safety net for themes that do not call the body hooks: once the page HTML
* is built, the code is spliced right after <body ...> and right before </body>.
* $params['html'] is passed by reference by FrontController::smartyOutputContent().
*/
public function hookActionOutputHTMLBefore($params)
{
if (!isset($params['html']) || !is_string($params['html']) || $params['html'] === '') {
return;
}
$html = $params['html'];
$changed = false;
if (empty(self::$renderedByTheme[DfCodeSnippet::LOCATION_BODY_OPEN])) {
$code = $this->renderLocation(DfCodeSnippet::LOCATION_BODY_OPEN);
if ($code !== '') {
$html = self::insertAfterBodyOpening($html, $code, $changed);
}
}
if (empty(self::$renderedByTheme[DfCodeSnippet::LOCATION_BODY_CLOSE])) {
$code = $this->renderLocation(DfCodeSnippet::LOCATION_BODY_CLOSE);
if ($code !== '') {
$html = self::insertBeforeBodyClosing($html, $code, $changed);
}
}
if ($changed) {
$params['html'] = $html;
}
}
/**
* @param string $html
* @param string $code
* @param bool $changed
*
* @return string
*/
public static function insertAfterBodyOpening($html, $code, &$changed)
{
// Start after </head> so a "<body" string inside a head script is never matched.
$offset = stripos($html, '</head>');
$offset = ($offset === false) ? 0 : $offset;
if (!preg_match('/<body\b[^>]*>/i', $html, $match, PREG_OFFSET_CAPTURE, $offset)) {
return $html;
}
$position = $match[0][1] + strlen($match[0][0]);
$changed = true;
return substr($html, 0, $position) . "\n" . $code . substr($html, $position);
}
/**
* @param string $html
* @param string $code
* @param bool $changed
*
* @return string
*/
public static function insertBeforeBodyClosing($html, $code, &$changed)
{
$position = strripos($html, '</body>');
if ($position === false) {
return $html;
}
$changed = true;
return substr($html, 0, $position) . $code . substr($html, $position);
}
/* ----------------------------------------------------------------------
* RENDERING
* -------------------------------------------------------------------- */
/**
* @param string $location
*
* @return string
*/
protected function renderLocation($location)
{
$snippets = $this->getRequestSnippets();
if (empty($snippets[$location])) {
return '';
}
return implode("\n", $snippets[$location]) . "\n";
}
/**
* Loads, filters and groups the snippets once per request.
*
* @return array
*/
protected function getRequestSnippets()
{
if (self::$requestSnippets !== null) {
return self::$requestSnippets;
}
self::$requestSnippets = [];
$rows = DfCodeSnippet::getActiveForShop((int) $this->context->shop->id);
if (!$rows) {
return self::$requestSnippets;
}
$pageName = $this->getCurrentPageName();
$isTestVisitor = null;
foreach ($rows as $row) {
if (!empty($row['test_mode'])) {
if ($isTestVisitor === null) {
$isTestVisitor = $this->isTestVisitor();
}
if (!$isTestVisitor) {
continue;
}
}
if (!DfCodeSnippet::rowMatchesPage($row, $pageName)) {
continue;
}
$code = trim((string) $row['code']);
if ($code === '') {
continue;
}
self::$requestSnippets[(string) $row['location']][] = $code;
}
return self::$requestSnippets;
}
/**
* Same page name as the theme's {$page.page_name}: php_self for core pages
* (index, product, order-confirmation...), module-<module>-<controller> for modules.
*
* @return string
*/
protected function getCurrentPageName()
{
$controller = $this->context->controller;
if (!$controller) {
return '';
}
try {
if (method_exists($controller, 'getPageName')) {
return (string) $controller->getPageName();
}
} catch (Throwable $e) {
// Fall through to php_self.
}
return isset($controller->php_self) ? (string) $controller->php_self : '';
}
/**
* Test mode: the visitor IP must be listed in Shop Parameters > General >
* Maintenance (PS_MAINTENANCE_IP). Ranges in CIDR notation are accepted.
*
* @return bool
*/
protected function isTestVisitor()
{
$ip = (string) Tools::getRemoteAddr();
$allowed = array_values(array_filter(array_map('trim', explode(',', (string) Configuration::get('PS_MAINTENANCE_IP')))));
if ($ip === '' || !$allowed) {
return false;
}
if (in_array($ip, $allowed, true)) {
return true;
}
if (class_exists('Symfony\Component\HttpFoundation\IpUtils')) {
try {
return Symfony\Component\HttpFoundation\IpUtils::checkIp($ip, $allowed);
} catch (Throwable $e) {
return false;
}
}
return false;
}
}