@@ -28,7 +28,7 @@ class Cache extends SingletonAbstract
28
28
protected function __construct ()
29
29
{
30
30
parent ::__construct ();
31
- $ tempdir = sys_get_temp_dir () . self ::$ DS . Scanner::getName () . self ::$ DS ;
31
+ $ tempdir = sys_get_temp_dir () . self ::$ DS . Scanner::getName () . self ::$ DS . ' cache ' . self :: $ DS ;
32
32
33
33
if (!is_dir ($ tempdir ) && (!mkdir ($ tempdir , 0777 , true ) && !is_dir ($ tempdir ))) {
34
34
throw new RuntimeException (sprintf ('Temp Directory "%s" was not created ' , $ tempdir ));
@@ -50,18 +50,19 @@ public function set($key, $value, $ttl = 3600)
50
50
{
51
51
$ key = $ this ->key ($ key );
52
52
53
- if ($ data = json_encode (['ttl ' => $ ttl > 0 ? time () + $ ttl : $ ttl , 'data ' => $ value ])) {
54
- if (!Scanner::isCacheEnabled ()) {
55
- self ::$ cache [$ key ] = $ data ;
53
+ $ data = ['ttl ' => $ ttl > 0 ? time () + $ ttl : $ ttl , 'data ' => $ value ];
56
54
57
- return true ;
58
- }
55
+ if (!Scanner:: isCacheEnabled ()) {
56
+ self :: $ cache [ $ key ] = $ data ;
59
57
60
- if ( file_put_contents ( $ this -> tempdir . $ key , $ data ) !== false ) {
61
- self :: $ cache [ $ key ] = $ data ;
58
+ return true ;
59
+ }
62
60
63
- return true ;
64
- }
61
+ $ fileData = '<?php return ' . var_export ($ data , true ) . '; ' ;
62
+ if (file_put_contents ($ this ->tempdir . $ key . '. ' . self ::$ ext , $ fileData ) !== false ) {
63
+ self ::$ cache [$ key ] = $ data ;
64
+
65
+ return true ;
65
66
}
66
67
67
68
return false ;
@@ -99,20 +100,18 @@ public function touch($key, $ttl = 3600)
99
100
public function get ($ key , $ default = null )
100
101
{
101
102
$ key = $ this ->key ($ key );
102
- $ file = $ this ->tempdir . $ key ;
103
+ $ file = $ this ->tempdir . $ key . ' . ' . self :: $ ext ;
103
104
104
105
$ data = false ;
105
106
if (isset (self ::$ cache [$ key ])) {
106
107
$ data = self ::$ cache [$ key ];
107
108
} elseif (Scanner::isCacheEnabled () && is_file ($ file )) {
108
- $ data = file_get_contents ( $ file) ;
109
+ $ data = include $ file ;
109
110
self ::$ cache [$ key ] = $ data ;
110
111
}
111
112
112
113
if ($ data !== false ) {
113
114
if (!empty ($ data )) {
114
- $ data = json_decode ($ data , true );
115
-
116
115
if (isset ($ data ['ttl ' ], $ data ['data ' ])) {
117
116
if ($ data ['ttl ' ] <= 0 || $ data ['ttl ' ] >= time ()) {
118
117
return $ data ['data ' ];
@@ -175,16 +174,46 @@ public function deleteRegex($pattern = '*')
175
174
/**
176
175
* Generate key.
177
176
*
177
+ * @see https://stackoverflow.com/a/42058764
178
+ *
178
179
* @param string $key
179
180
*
180
181
* @return string
181
182
*/
182
183
protected function key ($ key )
183
184
{
184
- $ filename = $ key . '. ' . self ::$ ext ;
185
- $ specialChars = ['? ' , '[ ' , '] ' , '/ ' , '\\' , '= ' , '< ' , '> ' , ': ' , '; ' , ', ' , "' " , '" ' , '& ' , '$ ' , '# ' , '* ' , '( ' , ') ' , '| ' , '~ ' , '` ' , '! ' , '{ ' , '} ' ];
186
- $ filename = str_replace ($ specialChars , '' , $ filename );
187
- $ filename = preg_replace ('/[\s-]+/ ' , '- ' , $ filename );
185
+ $ filename = preg_replace (
186
+ '~ ' .
187
+ '[<>:"/ \\|?*]| ' . // file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
188
+ '[\x00-\x1F]| ' . // control characters http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
189
+ '[\x7F\xA0\xAD]| ' . // non-printing characters DEL, NO-BREAK SPACE, SOFT HYPHEN
190
+ '[#\[\]@!$& \'()+,;=]| ' . // URI reserved https://tools.ietf.org/html/rfc3986#section-2.2
191
+ '[{}^\~`] ' . // URL unsafe characters https://www.ietf.org/rfc/rfc1738.txt
192
+ '~x ' ,
193
+ '- ' ,
194
+ $ key
195
+ );
196
+ // avoids ".", ".." or ".hiddenFiles"
197
+ $ filename = ltrim ($ filename , '.- ' );
198
+ // maximize filename length to 255 bytes http://serverfault.com/a/9548/44086
199
+
200
+ // reduce consecutive characters
201
+ $ filename = preg_replace ([
202
+ '/ +/ ' , // "file name.zip" becomes "file-name.zip"
203
+ '/_+/ ' , // "file___name.zip" becomes "file-name.zip"
204
+ '/-+/ ' , // "file---name.zip" becomes "file-name.zip"
205
+ ], '- ' , $ filename );
206
+ $ filename = preg_replace ([
207
+ '/-*\.-*/ ' , // "file--.--.-.--name.zip" becomes "file.name.zip"
208
+ '/\.{2,}/ ' , // "file...name..zip" becomes "file.name.zip"
209
+ ], '. ' , $ filename );
210
+ // lowercase for windows/unix interoperability http://support.microsoft.com/kb/100625
211
+ $ filename = mb_strtolower ($ filename , mb_detect_encoding ($ filename ));
212
+ // ".file-name.-" becomes "file-name"
213
+ $ filename = trim ($ filename , '.- ' );
214
+
215
+ $ ext = pathinfo ($ filename , PATHINFO_EXTENSION );
216
+ $ filename = mb_strcut (pathinfo ($ filename , PATHINFO_FILENAME ), 0 , 255 - ($ ext ? strlen ($ ext ) + 1 : 0 ), mb_detect_encoding ($ filename )) . ($ ext ? '. ' . $ ext : '' );
188
217
189
218
return trim ($ filename , '.-_ ' );
190
219
}
0 commit comments