-
Notifications
You must be signed in to change notification settings - Fork 0
/
staticassetdomainreplace.php
102 lines (84 loc) · 2.79 KB
/
staticassetdomainreplace.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
<?php
// no direct access
defined( '_JEXEC' ) or die;
class plgSystemStaticAssetDomainReplace extends JPlugin
{
protected $domaininsert = "";
protected $r_filetypes;
// automatically get application instance
protected $app;
/**
* Plugin method with the same name as the event will be called automatically.
*/
public function onAfterRender()
{
/*
* Plugin code goes here.
* Here the html has been rendered.
*/
if ($this->app->isClient('site')) {
$html = $this->app->getBody();
$this->domaininsert = $this->params->get('domain_scheme') . $this->params->get('insert_domain');
$this->r_filetypes =
str_replace(',', '|', // replace the commas with the regex OR|
preg_quote( // puts the \escape for min\.js etc.
preg_replace('/\s+/', '', // strip all whitespace
$this->params->get('filetypes')
)));
$html_new = $this->replaceEnabled($html, $this->params);
if (empty($html_new)) {
// error some stuff?
// $this->app->enqueueMessage(JText::_('SADM ERROR!'), 'error');
} else { // then it probably worked
$html = $html_new;
// done, apply changes.
$this->app->setBody($html);
}
}
else { // then we're not at the frontend
// don't do anything...
}
}
/*
* Call the enabled replace functions, according to which are enabled
* via the plugin settings.
*/
protected function replaceEnabled($html, $plg_params) {
// <link> tags
if ($plg_params->get('enable_tag_link')) {
}
// <img> tags
if ($plg_params->get('enable_tag_img')) {
}
// <script> tags
/*
* Regex: ((?:<script[^>]+(?:src)\s*=\s*)(?!['"]?(?:data|http|\/\/))['"]?)((\/[^'"\)\s>]+\/([^'"\)\s>\.]+\.(pack\.js|min\.js|js|jpg|png|gif|bmp|ico)))[^[:alnum:]]\??[^'"\)\s>]*)
* use: $1<insert>$2
* source: $2
* no querystring: $3
* filename without path: $4
* file extension: $5
*/
if ($plg_params->get('enable_tag_script')) {
$pattern = '/((?:<script[^>]+(?:src)\s*=\s*)(?![\'"]?(?:data|http|\/\/))[\'"]?)((\/[^\'"\)\s>]+\/([^\'"\)\s>\.]+\.(' . $this->r_filetypes . ')))[^[:alnum:]]\??[^\'"\)\s>]*)/';
$replacement = '${1}' . $this->domaininsert . '${2}';
$html = preg_replace($pattern, $replacement, $html);
}
// url()s in <style> tags
/*
* Regex: ((?:url\()(?!['"]?(?:data|http))['"]?)(([^'"\)\s>]+\/([^'"\)\s>]+\.(?:js|jpg|png|gif|bmp|ico)))[^[:alnum:]]+?\??[^'"\)\s>]*)
* use: $1<insert>$2
* source: $2
* no querystring: $3
* filename without path: $4
* file extension: $5
*/
if ($plg_params->get('enable_tag_style_url')) {
$pattern = '/((?:url\()(?![\'"]?(?:data|http))[\'"]?)(([^\'"\)\s>]+\/([^\'"\)\s>]+\.(?:' . $this->r_filetypes . ')))[^[:alnum:]]+?\??[^\'"\)\s>]*)/';
$replacement = '${1}' . $this->domaininsert . '${2}';
$html = preg_replace($pattern, $replacement, $html);
}
return $html;
}
}
?>