-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloudimage.php
780 lines (701 loc) · 31.7 KB
/
cloudimage.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
* You must not modify, adapt or create derivative works of this source code
* @author Scaleflex
* @copyright Since 2022 Scaleflex
* @license LICENSE.txt
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class Cloudimage extends Module
{
/**
* Override trans function to make it compatible with 1.6 bellow
* @param $id
* @param array $parameters
* @param $domain
* @param $locale
* @return string
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null) {
if (_PS_VERSION_ > '1.7') {
return parent::trans($id,$parameters, $domain, $locale);
} else {
return parent::l($id, false, $locale);
}
}
public function __construct()
{
$this->name = 'cloudimage';
$this->version = '1.0.2';
$this->tab = 'front_office_features';
$this->author = 'Scaleflex';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.6',
'max' => _PS_VERSION_,
];
if (_PS_VERSION_ > '1.7') {
$this->bootstrap = true;
}
$this->displayName = $this->l('Cloudimage by Scaleflex');
$this->description = $this->l('The easiest way to resize, compress, optimise and deliver lightning fast images to your users on any device via CDN.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
parent::__construct();
}
/**
* Install the Module
* @return bool
*/
public function install()
{
return parent::install() &&
$this->initConfigs() &&
$this->registerHook('displayHeader') &&
$this->registerHook('actionDispatcher') &&
$this->registerHook('actionFrontControllerSetMedia');
}
/**
* Uninstall the application
* @return bool
*/
public function uninstall()
{
return parent::uninstall() && $this->deleteConfigs();
}
/**
* Add JS to Front Page
* @return void
*/
public function hookActionFrontControllerSetMedia()
{
if (!$this->getConfigs('ciPrerender') && $this->getConfigs('ciActivation')) {
$this->context->controller->addJS($this->_path . 'views/js/cloudimage.js');
$this->context->controller->addJS($this->_path . 'views/js/product.js');
}
}
/**
* Override smarty template Hook
* @param $params
* @return void
* @throws SmartyException
*/
public function hookActionDispatcher($params)
{
$this->context->smarty->registerFilter(Smarty::FILTER_OUTPUT, [$this, 'changeImageSrc']);
}
public function checkExtensionIsImage($extension) {
$imageExtensions = [
'jpg', 'jpeg', 'png', 'gif', 'bmp',
'tiff', 'webp', 'svg', 'ico', 'jfif',
'pjpeg', 'pjp', 'avif', 'apng'
];
if (in_array($extension, $imageExtensions)) {
return true;
}
return false;
}
/**
* Override smarty template
* @param $html
* @param Smarty_Internal_Template $template
* @return array|mixed|string|string[]
*/
public function changeImageSrc($html, Smarty_Internal_Template $template)
{
$replaceHtml = false;
$ciActivation = $this->getConfigs('ciActivation');
$ignoreSvg = $this->getConfigs('ciIgnoreSvgImage');
$ciPrerender = $this->getConfigs('ciPrerender');
$ciToken = $this->getConfigs('ciToken');
$ciImageQuality = $this->getConfigs('ciImageQuality');
if (!$this->context->employee && $ciActivation) {
$bodyAttributes = "";
if (_PS_VERSION_ < '1.7' && !$this->context->employee && $this->getConfigs('ciActivation')) {
if (stripos($html, '<body') !== false) {
$pattern = '/<body([^>]*)>/i';
if (preg_match($pattern, $html, $matches)) {
$bodyAttributes = $matches[1]; // Extract the attributes part
$bodyAttributes = trim($bodyAttributes); // Remove any leading/trailing whitespaces
}
}
}
$dom = new DOMDocument();
$useErrors = libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $html);
libxml_use_internal_errors($useErrors);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
// Update <img> tags
$imgElements = $xpath->query('//img');
foreach ($imgElements as $element) {
$src = $element->getAttribute('src');
$extension = strtolower(pathinfo($src, PATHINFO_EXTENSION));
if ($ignoreSvg && $extension === 'svg') {
continue;
}
if ($ciPrerender) {
if (stripos($src, $ciToken) === false) {
$ciSrc = $this->buildUrl($src);
$element->setAttribute('src', $ciSrc);
$dataAttributes = ['data-image-medium-src', 'data-image-large-src', 'data-full-size-image-url'];
foreach ($dataAttributes as $attribute) {
if ($element->hasAttribute($attribute)) {
$imageSrc = $element->getAttribute($attribute);
$ciSrc = $this->buildUrl($imageSrc);
$element->setAttribute($attribute, $ciSrc);
}
}
}
} else {
$quality = $ciImageQuality < 100 ? '?q=' . $ciImageQuality : '';
$element->setAttribute('ci-src', $src . $quality);
$element->removeAttribute('src');
}
$replaceHtml = true;
}
// Update a tag // Only for Prerender
$anchorTags = $xpath->query('//a');
foreach ($anchorTags as $element) {
$src = $element->getAttribute('href');
$extension = strtolower(pathinfo($src, PATHINFO_EXTENSION));
if ($this->checkExtensionIsImage($extension)) {
if ($ignoreSvg && $extension === 'svg') {
continue;
}
if ($ciPrerender) {
if (stripos($src, $ciToken) === false) {
$ciSrc = $this->buildUrl($src);
$element->setAttribute('href', $ciSrc);
}
}
}
$replaceHtml = true;
}
// Update <source> tags
$sourceElements = $xpath->query('//source');
foreach ($sourceElements as $element) {
$srcset = $element->getAttribute('srcset');
$extension = strtolower(pathinfo($srcset, PATHINFO_EXTENSION));
if ($ignoreSvg && $extension === 'svg') {
continue;
}
if ($ciPrerender && stripos($srcset, $ciToken) === false) {
$ciSrc = $this->buildUrl($srcset);
$element->setAttribute('srcset', $ciSrc);
$replaceHtml = true;
}
}
if ($replaceHtml) {
$html = $dom->saveHTML($dom->documentElement);
$html = str_ireplace(['<html><body>', '</body></html>'], '', $html);
if (!$ciPrerender) {
$html = preg_replace('/<img([^>]*)\ssrc=[\'"]?([^\'"\s>]+)[\'"]?([^>]*)>/', '<img$1$3>', $html);
}
if (_PS_VERSION_ < '1.7') {
if (stripos($html, '<body') !== false) {
// Extract the body content
$pattern = '/<body([^>]*)>(.*?)<\/body>/is';
preg_match($pattern, $html, $matches);
$bodyContent = $matches[2]; // Extracted body content
$newBodyTag = '<body ' . $bodyAttributes . '>' . $bodyContent . '</body>';
// Replace the original body tag with the new body tag
$html = preg_replace($pattern, $newBodyTag, $html);
}
}
}
}
return $html;
}
/**
* Add to Header
* @param $params
* @return false|string
*/
public function hookDisplayHeader($params)
{
if (!$this->getConfigs('ciPrerender') && $this->getConfigs('ciActivation')) {
$this->context->smarty->assign($this->getConfigs());
return $this->display(__FILE__, 'views/templates/hook/cloudimage.tpl');
}
}
/** Get Configuration */
public function getConfigs($configName = null)
{
$configs = [
'ciActivation' => (bool)Configuration::get('CLOUDIMAGE_ACTIVATION'),
'ciToken' => (string)Configuration::get('CLOUDIMAGE_TOKEN'),
'ciCName' => (string)Configuration::get('CLOUDIMAGE_CNAME'),
'ciUseOriginalUrl' => (bool)Configuration::get('CLOUDIMAGE_USE_ORIGINAL_URL'),
'ciPrerender' => (bool)Configuration::get('CLOUDIMAGE_PRERENDER'),
'ciAutoBaseUrl' => (bool)Configuration::get('CLOUDIMAGE_AUTO_BASE_URL'),
'ciLazyLoading' => (bool)Configuration::get('CLOUDIMAGE_LAZY_LOADING'),
'ciIgnoreSvgImage' => (bool)Configuration::get('CLOUDIMAGE_IGNORE_SVG_IMAGE'),
'ciOrgIfSml' => (bool)Configuration::get('CLOUDIMAGE_ORG_IF_SML'),
'ciImageQuality' => (int)Configuration::get('CLOUDIMAGE_IMAGE_QUALITY'),
'ciMaximumPixelRatio' => (int)Configuration::get('CLOUDIMAGE_MAXIMUM_PIXEL_RATIO'),
'ciCustomJSFunction' => (string)Configuration::get('CLOUDIMAGE_CUSTOM_JS_FUNCTION'),
'ciCustomLibraryOption' => (string)Configuration::get('CLOUDIMAGE_CUSTOM_LIBRARY_OPTION'),
'ciRemoveV7' => (string)Configuration::get('CLOUDIMAGE_REMOVE_V7'),
];
if ($configName) {
return $configs[$configName];
}
return $configs;
}
/**
* Admin Config Page
* @return string
*/
public function getContent()
{
$output = '';
if (Tools::isSubmit('submit' . $this->name)) {
$submitStatus = true;
$ciToken = Tools::getValue('CLOUDIMAGE_TOKEN');
if (empty($ciToken)) {
Configuration::updateValue('CLOUDIMAGE_ACTIVATION', false);
if (Tools::getValue('CLOUDIMAGE_ACTIVATION')) {
$submitStatus = false;
}
} else {
Configuration::updateValue('CLOUDIMAGE_ACTIVATION', Tools::getValue('CLOUDIMAGE_ACTIVATION'));
}
Configuration::updateValue('CLOUDIMAGE_TOKEN', $ciToken);
Configuration::updateValue('CLOUDIMAGE_CNAME', Tools::getValue('CLOUDIMAGE_CNAME'));
Configuration::updateValue('CLOUDIMAGE_USE_ORIGINAL_URL', Tools::getValue('CLOUDIMAGE_USE_ORIGINAL_URL'));
Configuration::updateValue('CLOUDIMAGE_PRERENDER', Tools::getValue('CLOUDIMAGE_PRERENDER'));
Configuration::updateValue('CLOUDIMAGE_AUTO_BASE_URL', Tools::getValue('CLOUDIMAGE_AUTO_BASE_URL'));
Configuration::updateValue('CLOUDIMAGE_LAZY_LOADING', Tools::getValue('CLOUDIMAGE_LAZY_LOADING'));
Configuration::updateValue('CLOUDIMAGE_IGNORE_SVG_IMAGE', Tools::getValue('CLOUDIMAGE_IGNORE_SVG_IMAGE'));
Configuration::updateValue('CLOUDIMAGE_ORG_IF_SML', Tools::getValue('CLOUDIMAGE_ORG_IF_SML'));
Configuration::updateValue('CLOUDIMAGE_IMAGE_QUALITY', Tools::getValue('CLOUDIMAGE_IMAGE_QUALITY'));
Configuration::updateValue('CLOUDIMAGE_MAXIMUM_PIXEL_RATIO', Tools::getValue('CLOUDIMAGE_MAXIMUM_PIXEL_RATIO'));
Configuration::updateValue('CLOUDIMAGE_CUSTOM_JS_FUNCTION', Tools::getValue('CLOUDIMAGE_CUSTOM_JS_FUNCTION'));
Configuration::updateValue('CLOUDIMAGE_CUSTOM_LIBRARY_OPTION', Tools::getValue('CLOUDIMAGE_CUSTOM_LIBRARY_OPTION'));
Configuration::updateValue('CLOUDIMAGE_REMOVE_V7', Tools::getValue('CLOUDIMAGE_REMOVE_V7'));
$output .= $submitStatus ? $this->displayConfirmation($this->trans('Form submitted successfully')) :
$this->displayError($this->trans('You can not active Cloudimage because your Token is empty'));
}
return $output . $this->buildForm();
}
/**
* Build Admin Form
* @return string
*/
public function buildForm()
{
$ciActivation = (bool)Configuration::get('CLOUDIMAGE_ACTIVATION');
$ciToken = (string)Configuration::get('CLOUDIMAGE_TOKEN');
$ciCName = (string)Configuration::get('CLOUDIMAGE_CNAME');
$ciUseOriginalUrl = (bool)Configuration::get('CLOUDIMAGE_USE_ORIGINAL_URL');
$ciPrerender = (bool)Configuration::get('CLOUDIMAGE_PRERENDER');
$ciAutoBaseUrl = (bool)Configuration::get('CLOUDIMAGE_AUTO_BASE_URL');
$ciLazyLoading = (bool)Configuration::get('CLOUDIMAGE_LAZY_LOADING');
$ciIgnoreSvgImage = (bool)Configuration::get('CLOUDIMAGE_IGNORE_SVG_IMAGE');
$ciOrgIfSml = (bool)Configuration::get('CLOUDIMAGE_ORG_IF_SML');
$ciImageQuality = (int)Configuration::get('CLOUDIMAGE_IMAGE_QUALITY');
$ciMaximumPixelRatio = (int)Configuration::get('CLOUDIMAGE_MAXIMUM_PIXEL_RATIO');
$ciCustomJSFunction = (string)Configuration::get('CLOUDIMAGE_CUSTOM_JS_FUNCTION');
$ciCustomLibraryOption = (string)Configuration::get('CLOUDIMAGE_CUSTOM_LIBRARY_OPTION');
$ciRemoveV7 = (bool)Configuration::get('CLOUDIMAGE_REMOVE_V7');
$qualityOptions = [];
$pixelRatios = [
[
'id_option' => 1,
'name' => (string)1
],
[
'id_option' => 1.5,
'name' => (string)1.5
],
[
'id_option' => 2,
'name' => (string)2
]
];
for ($i = 1; $i <= 100; $i++) {
$qualityOptions[] = [
'id_option' => $i,
'name' => (string)$i
];
}
$form = [
'form' => [
'legend' => [
'title' => $this->l('Cloudimage Integration'),
],
'input' => [
[
'type' => 'switch',
'label' => $this->l('Activation'),
'desc' => $this->l('Enable/Disable the Module'),
'name' => 'CLOUDIMAGE_ACTIVATION',
'size' => 20,
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
]
],
],
[
'type' => 'text',
'label' => $this->l('Token'),
'desc' => $this->l('Cloudimage token'),
'name' => 'CLOUDIMAGE_TOKEN',
'size' => 20
],
[
'type' => 'text',
'label' => $this->l('CNAME'),
'desc' => $this->l('CNAME'),
'name' => 'CLOUDIMAGE_CNAME',
'size' => 20
],
[
'type' => 'switch',
'label' => $this->l('Use Original URL'),
'desc' => $this->l('If enabled, the plugin will only add query parameters to the image source URL, avoiding double CDNization in some cases, like if you have aliases configured.'),
'name' => 'CLOUDIMAGE_USE_ORIGINAL_URL',
'size' => 20,
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
]
],
],
[
'type' => 'switch',
'label' => $this->l('Auto Base URL Image'),
'desc' => $this->l('If enabled, Production with SSL only, The Plugin auto add base url to some missing Base URL Images'),
'name' => 'CLOUDIMAGE_AUTO_BASE_URL',
'size' => 20,
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
]
],
],
[
'type' => 'switch',
'label' => $this->l('Prerender'),
'desc' => $this->l('If enabled, the plugin will disable JS Responsive and Change URL to {token}.cloudimg.io/{origin_url}'),
'name' => 'CLOUDIMAGE_PRERENDER',
'size' => 20,
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
]
],
],
[
'type' => 'switch',
'label' => $this->l('Lazy Loading'),
'desc' => $this->l('If enabled, only images close to the current viewpoint will be loaded.'),
'name' => 'CLOUDIMAGE_LAZY_LOADING',
'size' => 20,
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
]
],
],
[
'type' => 'switch',
'label' => $this->l('Ignore SVG Image'),
'desc' => $this->l('By default, No.'),
'name' => 'CLOUDIMAGE_IGNORE_SVG_IMAGE',
'size' => 20,
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
]
],
],
[
'type' => 'switch',
'label' => $this->l('Prevent Image Resize'),
'desc' => $this->l('If you set Maximum "Pixel ratio" equal to 2, but some of your assets does not have min retina size(at least 2560x960), please enable this to prevent image resized. By default, yes.'),
'name' => 'CLOUDIMAGE_ORG_IF_SML',
'size' => 20,
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
]
],
],
[
'type' => 'select',
'label' => $this->l('Image Quality'),
'desc' => $this->l('The smaller the value, the more your image will be compressed. Careful — the quality of the image will decrease as well. By default, 90.'),
'name' => 'CLOUDIMAGE_IMAGE_QUALITY',
'required' => false,
'options' => [
'query' => $qualityOptions,
'id' => 'id_option',
'name' => 'name'
],
],
[
'type' => 'select',
'label' => $this->l('Maximum " Pixel ratio"'),
'desc' => $this->l('List of supported device pixel ratios, eg 2 for Retina devices'),
'name' => 'CLOUDIMAGE_MAXIMUM_PIXEL_RATIO',
'required' => false,
'options' => [
'query' => $pixelRatios,
'id' => 'id_option',
'name' => 'name'
],
],
[
'type' => 'switch',
'label' => $this->l('Remove V7'),
'desc' => $this->l('Removes the "/v7" part in URL format. Activate for token created after October 20th 2021.'),
'name' => 'CLOUDIMAGE_REMOVE_V7',
'size' => 20,
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
]
],
],
[
'type' => 'textarea',
'label' => $this->l('Custom js function'),
'desc' => $this->l('The valid js function starting with { and finishing with }'),
'name' => 'CLOUDIMAGE_CUSTOM_JS_FUNCTION',
'size' => 20
],
[
'type' => 'text',
'label' => $this->l('Custom Library Options'),
'desc' => $this->l('Modifies the library URL and must begin with the symbol &. Please read document before use.'),
'name' => 'CLOUDIMAGE_CUSTOM_LIBRARY_OPTION',
'size' => 20
]
],
'submit' => [
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right',
],
],
];
$helper = new HelperForm();
$helper->table = $this->table;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&' . http_build_query(['configure' => $this->name]);
$helper->submit_action = 'submit' . $this->name;
// Default language
$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
// Load current value into the form
$helper->fields_value['CLOUDIMAGE_ACTIVATION'] = $ciActivation;
$helper->fields_value['CLOUDIMAGE_TOKEN'] = $ciToken;
$helper->fields_value['CLOUDIMAGE_CNAME'] = $ciCName;
$helper->fields_value['CLOUDIMAGE_USE_ORIGINAL_URL'] = $ciUseOriginalUrl;
$helper->fields_value['CLOUDIMAGE_PRERENDER'] = $ciPrerender;
$helper->fields_value['CLOUDIMAGE_AUTO_BASE_URL'] = $ciAutoBaseUrl;
$helper->fields_value['CLOUDIMAGE_LAZY_LOADING'] = $ciLazyLoading;
$helper->fields_value['CLOUDIMAGE_IGNORE_SVG_IMAGE'] = $ciIgnoreSvgImage;
$helper->fields_value['CLOUDIMAGE_ORG_IF_SML'] = $ciOrgIfSml;
$helper->fields_value['CLOUDIMAGE_IMAGE_QUALITY'] = $ciImageQuality;
$helper->fields_value['CLOUDIMAGE_MAXIMUM_PIXEL_RATIO'] = $ciMaximumPixelRatio;
$helper->fields_value['CLOUDIMAGE_CUSTOM_JS_FUNCTION'] = $ciCustomJSFunction;
$helper->fields_value['CLOUDIMAGE_CUSTOM_LIBRARY_OPTION'] = $ciCustomLibraryOption;
$helper->fields_value['CLOUDIMAGE_REMOVE_V7'] = $ciRemoveV7;
return $helper->generateForm([$form]);
}
/**
* Using new translation system
*/
public function isUsingNewTranslationSystem()
{
return true;
}
/**
* Init configs if install
* @return bool
*/
private function initConfigs()
{
return Configuration::updateValue('CLOUDIMAGE_ACTIVATION', "0") &&
Configuration::updateValue('CLOUDIMAGE_TOKEN', "") &&
Configuration::updateValue('CLOUDIMAGE_CNAME', "") &&
Configuration::updateValue('CLOUDIMAGE_USE_ORIGINAL_URL', "1") &&
Configuration::updateValue('CLOUDIMAGE_PRERENDER', "0") &&
Configuration::updateValue('CLOUDIMAGE_AUTO_BASE_URL', "0") &&
Configuration::updateValue('CLOUDIMAGE_LAZY_LOADING', "1") &&
Configuration::updateValue('CLOUDIMAGE_IGNORE_SVG_IMAGE', "0") &&
Configuration::updateValue('CLOUDIMAGE_ORG_IF_SML', "1") &&
Configuration::updateValue('CLOUDIMAGE_IMAGE_QUALITY', "90") &&
Configuration::updateValue('CLOUDIMAGE_MAXIMUM_PIXEL_RATIO', "2") &&
Configuration::updateValue('CLOUDIMAGE_CUSTOM_JS_FUNCTION', "") &&
Configuration::updateValue('CLOUDIMAGE_CUSTOM_LIBRARY_OPTION', "") &&
Configuration::updateValue('CLOUDIMAGE_REMOVE_V7', "1");
}
/**
* Delete config if uninstalled
* @return bool
*/
private function deleteConfigs()
{
return Configuration::deleteByName('CLOUDIMAGE_ACTIVATION') &&
Configuration::deleteByName('CLOUDIMAGE_TOKEN') &&
Configuration::deleteByName('CLOUDIMAGE_CNAME') &&
Configuration::deleteByName('CLOUDIMAGE_USE_ORIGINAL_URL') &&
Configuration::deleteByName('CLOUDIMAGE_PRERENDER') &&
Configuration::deleteByName('CLOUDIMAGE_AUTO_BASE_URL') &&
Configuration::deleteByName('CLOUDIMAGE_LAZY_LOADING') &&
Configuration::deleteByName('CLOUDIMAGE_IGNORE_SVG_IMAGE') &&
Configuration::deleteByName('CLOUDIMAGE_ORG_IF_SML') &&
Configuration::deleteByName('CLOUDIMAGE_IMAGE_QUALITY') &&
Configuration::deleteByName('CLOUDIMAGE_MAXIMUM_PIXEL_RATIO') &&
Configuration::deleteByName('CLOUDIMAGE_CUSTOM_JS_FUNCTION') &&
Configuration::deleteByName('CLOUDIMAGE_CUSTOM_LIBRARY_OPTION') &&
Configuration::deleteByName('CLOUDIMAGE_REMOVE_V7');
}
/**
* Build URL
*
* @return string
*/
public function buildUrl($inputUrl) {
$baseUrl = 'https://' . $_SERVER['HTTP_HOST'];
if (stripos($inputUrl, "http") === false) {
$inputUrl = $this->removeFirstSplash($inputUrl);
if ($this->getConfigs('ciAutoBaseUrl')) {
$inputUrl = $this->addSplashIfMissing($baseUrl) . $inputUrl;
}
} else {
$inputUrl = $this->removeFirstSplash($inputUrl);
}
$config = $this->getConfigs();
$signedUrl = "//" . $config['ciToken'] . ".cloudimg.io/";
if (!empty($config['ciCName'])){
$signedUrl = $this->addSplashIfMissing("//" .$config['ciCName']);
}
if (!$config['ciRemoveV7']) {
$signedUrl .= "v7/";
}
$flagCheck = false;
if (!empty($config['ciCName'])) {
$inputUrl = str_replace('/' . $config['ciCName'], '', $inputUrl);
}
$ciUrl = $signedUrl . $inputUrl . "?";
if (!empty($config['ciCName'])) {
$ciUrl = str_replace($config['ciCName'] . '//', $config['ciCName'] . '/', $ciUrl);
}
if (!$config['ciRemoveV7']) {
$ciUrl = str_replace('//v7', '', $ciUrl);
}
if ($config['ciImageQuality'] < 100) {
if (!strpos($ciUrl, "?q=" . $config['ciImageQuality'])) {
$ciUrl .= "q=" . $config['ciImageQuality'];
}
$flagCheck = true;
}
$ciUrl = $this->removeLastQuestionMark($ciUrl);
if ($config['ciOrgIfSml']) {
$configParam = "org_if_sml=1";
if (!strpos($ciUrl, $configParam)){
if ($config['ciImageQuality'] === 100) {
$ciUrl .= "?" . $configParam;
} else {
$ciUrl .= $flagCheck ? "&" . $configParam : $configParam;
}
}
$flagCheck = true;
}
return $flagCheck ? $ciUrl : $this->removeLastQuestionMark($baseUrl . $inputUrl);
}
// Private support URL
private function removeLastQuestionMark($ciUrl) {
if (substr($ciUrl, -1) === '?') {
$ciUrl = substr($ciUrl, 0, -1);
}
return $ciUrl;
}
private function removeFirstSplash($inputUrl) {
if (substr($inputUrl, 0, 1) === "/") {
$inputUrl = substr($inputUrl, 1);
}
return $inputUrl;
}
private function addSplashIfMissing($baseUrl) {
if (substr($baseUrl, -1) !== "/") {
$baseUrl .= '/';
}
return $baseUrl;
}
}