|
1
|
|
|
<?php |
|
2
|
|
|
/*------------------------------------------------------------------------------ |
|
3
|
|
|
|
|
4
|
|
|
Project : CachingProxy |
|
5
|
|
|
Filename : src/CssCachingProxy.php |
|
6
|
|
|
Autor : (c) Sebastian Krüger <[email protected]> |
|
7
|
|
|
Date : 15.09.2013 |
|
8
|
|
|
|
|
9
|
|
|
For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
file that was distributed with this source code. |
|
11
|
|
|
|
|
12
|
|
|
Description: extends the CachingProxy with css |
|
13
|
|
|
|
|
14
|
|
|
----------------------------------------------------------------------------*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace secra\CachingProxy; |
|
17
|
|
|
|
|
18
|
|
|
class CssCachingProxy extends AbstractCachingProxy |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Delivers the set of html tags for webpage inclusion |
|
22
|
|
|
* |
|
23
|
|
|
* @return string the html .css link tags |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getIncludeHtml() |
|
27
|
|
|
{ |
|
28
|
|
|
// return include html code |
|
29
|
|
|
$filelist = $this->getIncludeFileset(); |
|
30
|
|
|
|
|
31
|
|
|
$htmlreturn = ""; |
|
32
|
|
|
|
|
33
|
|
|
foreach ($filelist as $file) { |
|
34
|
|
|
$htmlreturn .= "<link rel=\"stylesheet\" type=\"text/css\" "; |
|
35
|
|
|
$htmlreturn .= "href=\"".$file."\" />\n"; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $htmlreturn; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Delivers extension for cached files |
|
43
|
|
|
* |
|
44
|
|
|
* @return string file extension |
|
45
|
|
|
* |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getCacheFileExtension() |
|
48
|
|
|
{ |
|
49
|
|
|
// return Extension of css files |
|
50
|
|
|
return '.css'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Modifiy filepath definations in css files because of |
|
55
|
|
|
* different cachefolder path |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $csscontent content processed of css file |
|
58
|
|
|
* @param string $cssfilepath absolut path to css file |
|
59
|
|
|
* |
|
60
|
|
|
* @return string modified css content |
|
61
|
|
|
* |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function modifyFilecontent($csscontent, $cssfilepath) |
|
64
|
|
|
{ |
|
65
|
|
|
// Remove the path to docroot from full filepath and remove filename from cssfilepath |
|
66
|
|
|
$relativeCssPath = dirname(preg_replace("#^".$this->docrootpath."#", "", $cssfilepath)); |
|
67
|
|
|
|
|
68
|
|
|
// Use # insted of / in regexpress! |
|
69
|
|
|
// Search for every url() expr in css files only if the start with ./ |
|
70
|
|
|
// Don't matter if url in " or not |
|
71
|
|
|
$csscontent = preg_replace_callback( |
|
72
|
|
|
'#url\("?\./([^"]+)"?\)#i', |
|
73
|
|
|
function ($matches) use ($relativeCssPath) |
|
74
|
|
|
{ |
|
75
|
|
|
// $matches[1] contain first subpattern |
|
76
|
|
|
return 'url("/'.$relativeCssPath.'/'.$matches[1].'")'; |
|
77
|
|
|
}, |
|
78
|
|
|
$csscontent |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
// Now search for path with ../ sequences |
|
82
|
|
|
$csscontent = preg_replace_callback( |
|
83
|
|
|
'#url\(("|\')?(../){1,20}([^"\']+)("|\')?\)#i', |
|
84
|
|
|
function ($matches) use ($relativeCssPath) |
|
85
|
|
|
{ |
|
86
|
|
|
// $matches[0] contains whole matching pattern |
|
87
|
|
|
// $matches[2] contains ../ subpattern !! but only one time !! |
|
88
|
|
|
// $matches[3] contain path subpattern |
|
89
|
|
|
|
|
90
|
|
|
// Now count only how much ../ in the begining of the string to avoid counting of ../ in the middle |
|
91
|
|
|
|
|
92
|
|
|
// Where is the first . |
|
93
|
|
|
$posFirstDot = strpos($matches[0], "."); |
|
94
|
|
|
|
|
95
|
|
|
// Number of char that countain only ./ from the beginning of the string |
|
96
|
|
|
$charCount = strspn($matches[0], "./", $posFirstDot); |
|
97
|
|
|
|
|
98
|
|
|
// Cut the first part of the string |
|
99
|
|
|
$pathstring = substr($matches[0], $posFirstDot, $charCount); |
|
100
|
|
|
|
|
101
|
|
|
// Count the ../ |
|
102
|
|
|
$pathdepth = substr_count($pathstring, '../'); |
|
103
|
|
|
|
|
104
|
|
|
// Add starting slash, in case all folders has to be replaced, so the last can match in for loop |
|
105
|
|
|
// later the / is need because we transform the relativ to absolut path, so we need a starting slash |
|
106
|
|
|
$relativeCssPath = "/".$relativeCssPath; |
|
107
|
|
|
|
|
108
|
|
|
// Remove all folder that dots stand for |
|
109
|
|
|
for ($i=0; $i<$pathdepth; $i++) { |
|
110
|
|
|
// find last occurrence of / in csspath, remove folder depth from last to first |
|
111
|
|
|
$lastSlashPos = strrpos($relativeCssPath, "/"); |
|
112
|
|
|
// remove the last folder now, substr replace everything to the end of the string as default |
|
113
|
|
|
$relativeCssPath = substr_replace($relativeCssPath, "", $lastSlashPos); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return 'url("'.$relativeCssPath.'/'.$matches[3].'")'; |
|
117
|
|
|
}, |
|
118
|
|
|
$csscontent |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
// return css content |
|
122
|
|
|
return $csscontent; |
|
123
|
|
|
} |
|
124
|
|
|
} |