|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* The MIT License (MIT) |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
11
|
|
|
* in the Software without restriction, including without limitation the rights |
|
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
17
|
|
|
* copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
25
|
|
|
* SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* DOM component functions |
|
33
|
|
|
* |
|
34
|
|
|
* @author Daniel Popiniuc |
|
35
|
|
|
*/ |
|
36
|
|
|
trait DomCssAndJavascriptByDanielGPwithCDN |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
private $sCloundFlareUrl = '//cdnjs.cloudflare.com/ajax/libs/'; |
|
40
|
|
|
|
|
41
|
|
|
private function getCmpltVers($sFileName, $rootFileName) |
|
42
|
|
|
{ |
|
43
|
|
|
return str_replace([$rootFileName, '.min.js'], '', pathinfo($sFileName)['basename']); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function knownCloudFlareJavascript($jsFileName) |
|
47
|
|
|
{ |
|
48
|
|
|
$justFile = pathinfo($jsFileName)['basename']; |
|
49
|
|
|
switch ($justFile) { |
|
50
|
|
|
case 'jquery.placeholder.min.js': |
|
51
|
|
|
return [ |
|
52
|
|
|
'justFile' => $justFile, |
|
53
|
|
|
'version' => 'jquery-placeholder/2.0.8/', |
|
54
|
|
|
'eVerify' => 'jQuery.placeholder', |
|
55
|
|
|
]; |
|
56
|
|
|
// intentionally left blank |
|
57
|
|
|
case 'jquery.easing.1.3.min.js': |
|
58
|
|
|
return [ |
|
59
|
|
|
'justFile' => str_replace('.1.3', '', $justFile), |
|
60
|
|
|
'version' => 'jquery-easing/1.3/', |
|
61
|
|
|
'eVerify' => 'jQuery.easing["jswing"]', |
|
62
|
|
|
]; |
|
63
|
|
|
// intentionally left blank |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function sanitizeString($sFileName) |
|
68
|
|
|
{ |
|
69
|
|
|
return filter_var($sFileName, FILTER_SANITIZE_STRING); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Manages all known CSS that can be handled through CDNs |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $cssFileName |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function setCssFileCDN($cssFileName) |
|
79
|
|
|
{ |
|
80
|
|
|
$patternFound = null; |
|
81
|
|
|
if (strpos(pathinfo($cssFileName)['basename'], 'font-awesome-') !== false) { |
|
82
|
|
|
$patternFound = $this->setCssFileCDNforFontAwesome($cssFileName); |
|
83
|
|
|
} |
|
84
|
|
|
if (is_null($patternFound)) { |
|
85
|
|
|
$patternFound = [false, $this->sanitizeString($cssFileName)]; |
|
86
|
|
|
} |
|
87
|
|
|
return $patternFound; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns css link to a given file |
|
92
|
|
|
* Returns an array with CDN call of a known Font-websome css |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $cssFileName |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
private function setCssFileCDNforFontAwesome($cssFileName) |
|
98
|
|
|
{ |
|
99
|
|
|
return [ |
|
100
|
|
|
true, |
|
101
|
|
|
$this->sCloundFlareUrl . 'font-awesome/' . $this->getCmpltVers($cssFileName, 'font-awesome-') |
|
102
|
|
|
. '/css/font-awesome.min.css', |
|
103
|
|
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Manages all known Javascript that can be handled through CDNs |
|
108
|
|
|
* (if within local network makes no sense to use CDNs) |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $jsFileName |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function setJavascriptFileCDN($jsFileName) |
|
114
|
|
|
{ |
|
115
|
|
|
$onlyFileName = pathinfo($jsFileName)['basename']; |
|
116
|
|
|
$patternFound = null; |
|
117
|
|
|
if (in_array($onlyFileName, ['jquery.placeholder.min.js', 'jquery.easing.1.3.min.js'])) { |
|
118
|
|
|
$patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName); |
|
119
|
|
|
} elseif (strpos($onlyFileName, '-') !== false) { |
|
120
|
|
|
$patternFound = $this->setJavascriptFileCDNbyPattern($jsFileName); |
|
121
|
|
|
} |
|
122
|
|
|
if (is_null($patternFound)) { |
|
123
|
|
|
$patternFound = [false, $this->sanitizeString($jsFileName), '']; |
|
124
|
|
|
} |
|
125
|
|
|
return $patternFound; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function setJavascriptFileCDNbyPattern($jsFileName) |
|
129
|
|
|
{ |
|
130
|
|
|
$sFileParts = explode('-', $jsFileName); |
|
131
|
|
|
$knownFNs = [ |
|
132
|
|
|
'jquery' => 'setJavascriptFileCDNjQuery', |
|
133
|
|
|
'highcharts' => 'setJavascriptFileCDNforHighCharts', |
|
134
|
|
|
'exporting' => 'setJavascriptFileCDNforHighChartsExporting', |
|
135
|
|
|
]; |
|
136
|
|
|
$rootFN = pathinfo($sFileParts[0])['basename']; |
|
137
|
|
|
if (array_key_exists($rootFN, $knownFNs)) { |
|
138
|
|
|
return call_user_func([$this, $knownFNs[$rootFN]], pathinfo($jsFileName)['basename']); |
|
139
|
|
|
} |
|
140
|
|
|
return null; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
145
|
|
|
* and fall-back line that points to local cache of it |
|
146
|
|
|
* specific for HighCharts |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $jsFileName |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
|
|
private function setJavascriptFileCDNforHighCharts($jsFileName) |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->setJavascriptFileCDNforHighChartsMain($jsFileName, 'highcharts'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
158
|
|
|
* and fall-back line that points to local cache of it |
|
159
|
|
|
* specific for HighCharts Exporting feature |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $jsFileName |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
private function setJavascriptFileCDNforHighChartsExporting($jsFileName) |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->setJavascriptFileCDNforHighChartsMain($jsFileName, 'exporting'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
171
|
|
|
* and fall-back line that points to local cache of it |
|
172
|
|
|
* specific for HighCharts |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $jsFileName |
|
175
|
|
|
* @param string $libName |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
|
|
private function setJavascriptFileCDNforHighChartsMain($jsFileName, $libName) |
|
179
|
|
|
{ |
|
180
|
|
|
$jsFN = $this->sanitizeString($jsFileName); |
|
181
|
|
|
$jsVersionlessFN = str_replace([$libName . '-', '.js'], '', pathinfo($jsFileName)['basename']) |
|
182
|
|
|
. ($libName === 'exporting' ? '/modules' : ''); |
|
183
|
|
|
if (strpos($jsFileName, $libName) !== false) { |
|
184
|
|
|
return [ |
|
185
|
|
|
true, |
|
186
|
|
|
$this->sCloundFlareUrl . 'highcharts/' . $jsVersionlessFN . '/' . $libName . '.js', |
|
187
|
|
|
'<script>!window.Highcharts && document.write(\'<script src="' . $jsFN . '">\x3C/script>\')</script>', |
|
188
|
|
|
]; |
|
189
|
|
|
} |
|
190
|
|
|
return null; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
195
|
|
|
* and fall-back line that points to local cache of it |
|
196
|
|
|
* specific for jQuery |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $jsFileName |
|
199
|
|
|
* @return array |
|
200
|
|
|
*/ |
|
201
|
|
|
private function setJavascriptFileCDNjQuery($jsFileName) |
|
202
|
|
|
{ |
|
203
|
|
|
$jQueryPosition = strpos($jsFileName, 'jquery-'); |
|
204
|
|
|
$jQueryMajorVersion = substr($jsFileName, 7, 1); |
|
205
|
|
|
if (($jQueryPosition !== false) && is_numeric($jQueryMajorVersion) && (substr($jsFileName, -7) == '.min.js')) { |
|
206
|
|
|
return [ |
|
207
|
|
|
true, |
|
208
|
|
|
$this->sCloundFlareUrl . 'jquery/' . $this->getCmpltVers($jsFileName, 'jquery-') . '/jquery.min.js', |
|
209
|
|
|
'<script>window.jQuery || document.write(\'<script src="' . $this->sanitizeString($jsFileName) |
|
210
|
|
|
. '">\x3C/script>\')</script>', |
|
211
|
|
|
]; |
|
212
|
|
|
} |
|
213
|
|
|
return null; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
218
|
|
|
* and fall-back line that points to local cache of it |
|
219
|
|
|
* specific for jQuery Libraries |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $jsFileName |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
|
|
private function setJavascriptFileCDNjQueryLibs($jsFileName) |
|
225
|
|
|
{ |
|
226
|
|
|
$sFN = $this->sanitizeString($jsFileName); |
|
227
|
|
|
$eArray = $this->knownCloudFlareJavascript($sFN); |
|
228
|
|
|
if (!is_null($eArray['version'])) { |
|
229
|
|
|
return [ |
|
230
|
|
|
true, |
|
231
|
|
|
$this->sCloundFlareUrl . $eArray['version'] . $eArray['justFile'], |
|
232
|
|
|
'<script>' . $eArray['eVerify'] . ' || document.write(\'<script src="' . $sFN |
|
233
|
|
|
. '">\x3C/script>\')</script>', |
|
234
|
|
|
]; |
|
235
|
|
|
} |
|
236
|
|
|
return null; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|