|
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 DomComponentsByDanielGPwithCDN |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
private function knownCloudFlareJavascript($jsFileName) |
|
40
|
|
|
{ |
|
41
|
|
|
$justFile = pathinfo($jsFileName)['basename']; |
|
42
|
|
|
switch ($justFile) { |
|
43
|
|
|
case 'jquery.placeholder.min.js': |
|
44
|
|
|
$version = 'jquery-placeholder/2.0.8/'; |
|
45
|
|
|
$fnExistanceToVerify = 'jQuery.placeholder'; |
|
46
|
|
|
break; |
|
47
|
|
|
case 'jquery.easing.1.3.min.js': |
|
48
|
|
|
$version = 'jquery-easing/1.3/'; |
|
49
|
|
|
$fnExistanceToVerify = 'jQuery.easing["jswing"]'; |
|
50
|
|
|
$justFile = str_replace('.1.3', '', $justFile); |
|
51
|
|
|
break; |
|
52
|
|
|
} |
|
53
|
|
|
return ['justFile' => $justFile, 'version' => $version, 'ExistanceToVerify' => $fnExistanceToVerify]; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Manages all known CSS that can be handled through CDNs |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $cssFileName |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function setCssFileCDN($cssFileName) |
|
63
|
|
|
{ |
|
64
|
|
|
$onlyFileName = pathinfo($cssFileName)['basename']; |
|
65
|
|
|
if (strpos($onlyFileName, 'font-awesome-') !== false) { |
|
66
|
|
|
$patternFound = $this->setCssFileCDNforFontAwesome($cssFileName); |
|
67
|
|
|
} else { |
|
68
|
|
|
$patternFound = null; |
|
69
|
|
|
} |
|
70
|
|
View Code Duplication |
if (is_null($patternFound)) { |
|
|
|
|
|
|
71
|
|
|
$patternFound = [ |
|
72
|
|
|
false, |
|
73
|
|
|
filter_var($cssFileName, FILTER_SANITIZE_STRING), |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
return $patternFound; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns css link to a given file |
|
81
|
|
|
* Returns an array with CDN call of a known Font-websome css |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $cssFile |
|
|
|
|
|
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
private function setCssFileCDNforFontAwesome($cssFileName) |
|
87
|
|
|
{ |
|
88
|
|
|
$patternFound = [ |
|
89
|
|
|
true, |
|
90
|
|
|
implode('', [ |
|
91
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/font-awesome/', |
|
92
|
|
|
str_replace(['font-awesome-', '.min.css'], '', pathinfo($cssFileName)['basename']), |
|
93
|
|
|
'/css/font-awesome.min.css', |
|
94
|
|
|
]) |
|
95
|
|
|
]; |
|
96
|
|
|
return $patternFound; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Manages all known Javascript that can be handled through CDNs |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $jsFileName |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function setJavascriptFileCDN($jsFileName) |
|
106
|
|
|
{ |
|
107
|
|
|
$onlyFileName = pathinfo($jsFileName)['basename']; |
|
108
|
|
|
/** |
|
109
|
|
|
* if within local network makes no sense to use CDNs |
|
110
|
|
|
*/ |
|
111
|
|
|
if (strpos($onlyFileName, 'jquery-') !== false) { |
|
112
|
|
|
$patternFound = $this->setJavascriptFileCDNjQuery($jsFileName); |
|
113
|
|
|
} elseif (strpos($onlyFileName, 'jquery.placeholder.min.js') !== false) { |
|
114
|
|
|
$patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName); |
|
115
|
|
|
} elseif (strpos($onlyFileName, 'jquery.easing.1.3.min.js') !== false) { |
|
116
|
|
|
$patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName); |
|
117
|
|
|
} elseif (strpos($onlyFileName, 'highcharts-') !== false) { |
|
118
|
|
|
$patternFound = $this->setJavascriptFileCDNforHighCharts($jsFileName); |
|
119
|
|
|
} elseif (strpos($onlyFileName, 'exporting-') !== false) { |
|
120
|
|
|
$patternFound = $this->setJavascriptFileCDNforHighChartsExporting($jsFileName); |
|
121
|
|
|
} else { |
|
122
|
|
|
$patternFound = null; |
|
123
|
|
|
} |
|
124
|
|
View Code Duplication |
if (is_null($patternFound)) { |
|
|
|
|
|
|
125
|
|
|
$patternFound = [ |
|
126
|
|
|
false, |
|
127
|
|
|
filter_var($jsFileName, FILTER_SANITIZE_STRING), |
|
128
|
|
|
'', |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
return $patternFound; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
136
|
|
|
* and fall-back line that points to local cache of it |
|
137
|
|
|
* specific for HighCharts |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $jsFileName |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
View Code Duplication |
private function setJavascriptFileCDNforHighCharts($jsFileName) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
$patternFound = null; |
|
145
|
|
|
$jQueryPosition = strpos($jsFileName, 'highcharts'); |
|
146
|
|
|
if ($jQueryPosition !== false) { |
|
147
|
|
|
$patternFound = [ |
|
148
|
|
|
true, |
|
149
|
|
|
implode('', [ |
|
150
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/highcharts/', |
|
151
|
|
|
str_replace(['highcharts-', '.js'], '', pathinfo($jsFileName)['basename']), |
|
152
|
|
|
'/highcharts.js', |
|
153
|
|
|
]), |
|
154
|
|
|
implode('', [ |
|
155
|
|
|
'<script>!window.Highcharts && document.write(\'<script src="', |
|
156
|
|
|
filter_var($jsFileName, FILTER_SANITIZE_STRING), |
|
157
|
|
|
'">\x3C/script>\')</script>' |
|
158
|
|
|
]) |
|
159
|
|
|
]; |
|
160
|
|
|
} |
|
161
|
|
|
return $patternFound; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
166
|
|
|
* and fall-back line that points to local cache of it |
|
167
|
|
|
* specific for HighCharts Exporting feature |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $jsFileName |
|
170
|
|
|
* @return array |
|
171
|
|
|
*/ |
|
172
|
|
View Code Duplication |
private function setJavascriptFileCDNforHighChartsExporting($jsFileName) |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
$patternFound = null; |
|
175
|
|
|
$jQueryPosition = strpos($jsFileName, 'exporting'); |
|
176
|
|
|
if ($jQueryPosition !== false) { |
|
177
|
|
|
$patternFound = [ |
|
178
|
|
|
true, |
|
179
|
|
|
implode('', [ |
|
180
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/highcharts/', |
|
181
|
|
|
str_replace(['exporting-', '.js'], '', pathinfo($jsFileName)['basename']), |
|
182
|
|
|
'/modules/exporting.js', |
|
183
|
|
|
]), |
|
184
|
|
|
implode('', [ |
|
185
|
|
|
'<script>!window.Highcharts.post && document.write(\'<script src="', |
|
186
|
|
|
filter_var($jsFileName, FILTER_SANITIZE_STRING), |
|
187
|
|
|
'">\x3C/script>\')</script>' |
|
188
|
|
|
]) |
|
189
|
|
|
]; |
|
190
|
|
|
} |
|
191
|
|
|
return $patternFound; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
196
|
|
|
* and fall-back line that points to local cache of it |
|
197
|
|
|
* specific for jQuery |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $jsFileName |
|
200
|
|
|
* @return array |
|
201
|
|
|
*/ |
|
202
|
|
|
private function setJavascriptFileCDNjQuery($jsFileName) |
|
203
|
|
|
{ |
|
204
|
|
|
$patternFound = null; |
|
205
|
|
|
$jQueryPosition = strpos($jsFileName, 'jquery-'); |
|
206
|
|
|
$jQueryMajorVersion = substr($jsFileName, 7, 1); |
|
207
|
|
|
if (($jQueryPosition !== false) && is_numeric($jQueryMajorVersion) && (substr($jsFileName, -7) == '.min.js')) { |
|
208
|
|
|
$patternFound = [ |
|
209
|
|
|
true, |
|
210
|
|
|
implode('', [ |
|
211
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/jquery/', |
|
212
|
|
|
str_replace(['jquery-', '.min.js'], '', pathinfo($jsFileName)['basename']), |
|
213
|
|
|
'/jquery.min.js', |
|
214
|
|
|
]), |
|
215
|
|
|
implode('', [ |
|
216
|
|
|
'<script>window.jQuery || document.write(\'<script src="', |
|
217
|
|
|
filter_var($jsFileName, FILTER_SANITIZE_STRING), |
|
218
|
|
|
'">\x3C/script>\')</script>' |
|
219
|
|
|
]) |
|
220
|
|
|
]; |
|
221
|
|
|
} |
|
222
|
|
|
return $patternFound; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Returns an array with CDN call of a known Javascript library |
|
227
|
|
|
* and fall-back line that points to local cache of it |
|
228
|
|
|
* specific for jQuery Libraries |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $jsFileName |
|
231
|
|
|
* @return array |
|
232
|
|
|
*/ |
|
233
|
|
|
private function setJavascriptFileCDNjQueryLibs($jsFileName) |
|
234
|
|
|
{ |
|
235
|
|
|
$patternFound = null; |
|
236
|
|
|
$eArray = $this->knownCloudFlareJavascript(filter_var($jsFileName, FILTER_SANITIZE_STRING)); |
|
237
|
|
|
if (!is_null($eArray['version'])) { |
|
238
|
|
|
$patternFound = [ |
|
239
|
|
|
true, |
|
240
|
|
|
implode('', [ |
|
241
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/', |
|
242
|
|
|
$eArray['version'], |
|
243
|
|
|
$eArray['justFile'], |
|
244
|
|
|
]), |
|
245
|
|
|
implode('', [ |
|
246
|
|
|
'<script>' . $eArray['ExistanceToVerify'] . ' || document.write(\'<script src="', |
|
247
|
|
|
filter_var($jsFileName, FILTER_SANITIZE_STRING), |
|
248
|
|
|
'">\x3C/script>\')</script>' |
|
249
|
|
|
]) |
|
250
|
|
|
]; |
|
251
|
|
|
} |
|
252
|
|
|
return $patternFound; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: