This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @package fwolflib |
||
4 | * @subpackage class |
||
5 | * @copyright Copyright 2010-2011, Fwolf |
||
6 | * @author Fwolf <[email protected]> |
||
7 | * @since 2010-01-07 |
||
8 | */ |
||
9 | |||
10 | |||
11 | require_once(dirname(__FILE__) . '/fwolflib.php'); |
||
12 | require_once(FWOLFLIB . 'func/filesystem.php'); |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Key - value like cache system, data store in filesystem. |
||
17 | * |
||
18 | * Key is split by '/', just like URL. |
||
19 | * |
||
20 | * Workflow: |
||
21 | * - CacheKey(), hash user request key. |
||
22 | * - CacheGet() |
||
23 | * - Try read, CacheGetType() |
||
24 | * - If fail, gen cache, CacheGen(), CacheSet() |
||
25 | * - CacheSet() |
||
26 | * - CacheDel() |
||
27 | * |
||
28 | * @deprecated use Fwlib\Cache\Cache |
||
29 | * @package fwolflib |
||
30 | * @subpackage class |
||
31 | * @copyright Copyright 2010-2011, Fwolf |
||
32 | * @author Fwolf <[email protected]> |
||
33 | * @since 2010-01-07 |
||
34 | */ |
||
35 | abstract class Cache extends Fwolflib { |
||
0 ignored issues
–
show
|
|||
36 | |||
37 | |||
38 | /** |
||
39 | * Compute path of a key's data file |
||
40 | * |
||
41 | * @param string $key |
||
42 | * @return string |
||
43 | */ |
||
44 | View Code Duplication | public function CacheFilePath($key) { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
45 | $s_path = $this->aCfg['cache-file-dir']; |
||
46 | |||
47 | $ar_rule = str_split($this->aCfg['cache-file-rule'], 2); |
||
48 | if (empty($ar_rule)) |
||
49 | return $s_path; |
||
50 | |||
51 | foreach ($ar_rule as $rule) |
||
52 | $s_path .= $this->CacheFilePathSec($rule, $key) . '/'; |
||
53 | |||
54 | // Filename |
||
55 | $s_path .= $this->CacheFilePathFile($key); |
||
56 | |||
57 | return $s_path; |
||
58 | } // end of func Path |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Compute name of a key's data file |
||
63 | * |
||
64 | * @param string $key |
||
65 | * @return string |
||
66 | */ |
||
67 | protected function CacheFilePathFile($key) { |
||
68 | return substr(md5($key), 0, 8); |
||
69 | } // end of func CacheFilePathFile |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Compute path of a key by a single rule section |
||
74 | * |
||
75 | * @param string $rule |
||
76 | * @param string $key |
||
77 | * @return string |
||
78 | * @see $sCacheRule |
||
79 | */ |
||
80 | View Code Duplication | protected function CacheFilePathSec($rule, $key) { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
81 | $i_len = 2; |
||
82 | |||
83 | if ($i_len > strlen($rule)) |
||
84 | return ''; |
||
85 | |||
86 | $i = intval($rule{1}); |
||
87 | if (1 == $rule{0}) { |
||
88 | // md5 from start |
||
89 | $i_start = $i_len * $i; |
||
90 | $s_seed = md5($key); |
||
91 | } elseif (2 == $rule{0}) { |
||
92 | // md5 from end |
||
93 | $i_start = -1 * $i_len * ($i + 1); |
||
94 | $s_seed = md5($key); |
||
95 | } elseif (3 == $rule{0}) { |
||
96 | // raw from start |
||
97 | $i_start = $i_len * $i; |
||
98 | $s_seed = $key; |
||
99 | } elseif (4 == $rule{0}) { |
||
100 | // raw from end |
||
101 | $i_start = -1 * $i_len * ($i + 1); |
||
102 | $s_seed = $key; |
||
103 | } elseif (5 == $rule{0}) { |
||
104 | // crc32 |
||
105 | if (3 < $i) |
||
106 | $i = $i % 3; |
||
107 | $i_start = $i_len * $i; |
||
108 | $s_seed = hash('crc32', $key); |
||
109 | } |
||
110 | return(substr($s_seed, $i_start, 2)); |
||
0 ignored issues
–
show
The variable
$s_seed does not seem to be defined for all execution paths leading up to this point.
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: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
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
![]() The variable
$i_start does not seem to be defined for all execution paths leading up to this point.
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: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
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
![]() |
|||
111 | } // end of func CacheFilePathSec |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Gen and write cache data file |
||
116 | * |
||
117 | * @param string $key |
||
118 | * @return mixed // Generated cache data. |
||
119 | */ |
||
120 | protected function CacheGen($key) { |
||
121 | $val = $this->CacheGenVal($key); |
||
122 | $this->CacheSet($key, $val); |
||
123 | return $val; |
||
124 | } // end of func CacheGen |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Gen cache data, implement by child class |
||
129 | * |
||
130 | * @param string $key |
||
131 | * @return string |
||
132 | */ |
||
133 | abstract protected function CacheGenVal($key); |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Load cache data |
||
138 | * |
||
139 | * @param string $key |
||
140 | * @param int $flag May used by type func. |
||
141 | * @return mixed |
||
142 | */ |
||
143 | View Code Duplication | public function CacheGet($key, $flag) { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
144 | // Error check |
||
145 | if (empty($this->aCfg['cache-type'])) { |
||
146 | $this->Log('Cache type is not set.', 5); |
||
147 | return NULL; |
||
148 | } |
||
149 | |||
150 | $s = 'CacheGet' . ucfirst($this->aCfg['cache-type']); |
||
151 | if (method_exists($this, $s)) |
||
152 | return $this->{$s}($key, $flag); |
||
153 | else { |
||
154 | $this->Log('Cache get method for type ' |
||
155 | . $this->aCfg['cache-type'] . ' not implement.', 5); |
||
156 | return NULL; |
||
157 | } |
||
158 | } // end of func CacheGet |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Read cache file and return value |
||
163 | * |
||
164 | * @param string $key |
||
165 | * @param int $flag Which type value shoud I return ? |
||
166 | * 0=string, 1=array, 2=object |
||
167 | * 3=raw string |
||
168 | * @return mixed |
||
169 | */ |
||
170 | protected function CacheGetFile($key, $flag = 0) { |
||
171 | if ($this->CacheNeedUpdate($key)) |
||
172 | return $this->CacheGen($key); |
||
173 | else { |
||
174 | // Read from file and parse it. |
||
175 | $s_file = $this->CacheFilePath($key); |
||
176 | $s_cache = file_get_contents($s_file); |
||
177 | |||
178 | $rs = null; |
||
179 | switch ($flag) { |
||
180 | case 0: |
||
181 | $rs = json_decode($s_cache, true); |
||
182 | if (is_array($rs)) |
||
183 | $rs = $rs[0]; |
||
184 | break; |
||
185 | case 1: |
||
186 | $rs = json_decode($s_cache, true); |
||
187 | break; |
||
188 | case 2: |
||
189 | $rs = json_decode($s_cache, false); |
||
190 | break; |
||
191 | case 3: |
||
192 | default: |
||
193 | $rs = &$s_cache; |
||
194 | } |
||
195 | return $rs; |
||
196 | } |
||
197 | } // end of func CacheGetFile |
||
198 | |||
199 | |||
200 | /** |
||
201 | * CacheLifetime of cache data, meature by second |
||
202 | * |
||
203 | * @param string $key |
||
204 | * @return int |
||
205 | */ |
||
206 | abstract public function CacheLifetime($key); |
||
207 | |||
208 | |||
209 | /** |
||
210 | * Is cache data file need update/create ? |
||
211 | * |
||
212 | * @param string $key |
||
213 | * @return boolean |
||
214 | */ |
||
215 | protected function CacheNeedUpdate($key) { |
||
216 | $s_file = $this->CacheFilePath($key); |
||
217 | |||
218 | // File doesn't exist |
||
219 | if (!file_exists($s_file)) |
||
220 | return true; |
||
221 | |||
222 | // Out of CacheLifetime |
||
223 | if ($this->CacheLifetime($key) |
||
224 | > (time() - filemtime($s_file))) |
||
225 | return false; |
||
226 | else |
||
227 | return true; |
||
228 | } // end of func CacheNeedUpdate |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Write data to cache |
||
233 | * |
||
234 | * @param string $key |
||
235 | * @param mixed $val |
||
236 | * @return $this |
||
237 | */ |
||
238 | View Code Duplication | public function CacheSet ($key, $val) { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
239 | // Error check |
||
240 | if (empty($this->aCfg['cache-type'])) { |
||
241 | $this->Log('Cache type is not set.', 5); |
||
242 | return this; |
||
243 | } |
||
244 | |||
245 | $s = 'CacheSet' . ucfirst($this->aCfg['cache-type']); |
||
246 | if (method_exists($this, $s)) { |
||
247 | $this->{$s}($key, $val); |
||
248 | } |
||
249 | else { |
||
250 | $this->Log('Cache set method for type ' |
||
251 | . $this->aCfg['cache-type'] . ' not implement.', 5); |
||
252 | } |
||
253 | return $this; |
||
254 | } // end of func CacheSet |
||
255 | |||
256 | |||
257 | /** |
||
258 | * Write data to cache, type file |
||
259 | * |
||
260 | * @param string $key |
||
261 | * @param mixed $val |
||
262 | * @return $this |
||
263 | */ |
||
264 | View Code Duplication | public function CacheSetFile ($key, $val) { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
265 | $s_file = $this->CacheFilePath($key); |
||
266 | $s_cache = json_encode($val); |
||
267 | |||
268 | // Create each level dir if not exists |
||
269 | $s_dir = DirName1($s_file); |
||
0 ignored issues
–
show
The function
DirName1() has been deprecated with message: Use native dirname()
This function has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead. ![]() |
|||
270 | if (!file_exists($s_dir)) |
||
271 | mkdir($s_dir, 0755, true); |
||
272 | |||
273 | // Finally write file |
||
274 | file_put_contents($s_file, $s_cache, LOCK_EX); |
||
275 | return $this; |
||
276 | } // end of func CacheSetFile |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Check config/cache store dir valid and writable |
||
281 | * If error, return error msg, else return empty str. |
||
282 | * |
||
283 | * @param string $dir |
||
284 | * @return string |
||
285 | */ |
||
286 | public function ChkCfgFileDir($dir) { |
||
287 | $s = ''; |
||
288 | |||
289 | if (empty($dir)) |
||
290 | $s = "Cache dir {$dir} is not defined."; |
||
291 | |||
292 | View Code Duplication | if (!file_exists($dir)) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
293 | if (false == mkdir($dir, 0755, true)) |
||
0 ignored issues
–
show
|
|||
294 | $s = "Fail to create cache dir {$dir}."; |
||
295 | } |
||
296 | else { |
||
297 | if (!is_writable($dir)) |
||
298 | $s = "Cache dir {$dir} is not writable."; |
||
299 | } |
||
300 | |||
301 | if (!empty($s)) |
||
302 | $this->Log($s, 5); |
||
303 | return $s; |
||
304 | } // end of func ChkCfgFileDir |
||
305 | |||
306 | |||
307 | /** |
||
308 | * Check cache rule exist and valid |
||
309 | * If error, return error msg, else return empty str. |
||
310 | * |
||
311 | * @param string $rule |
||
312 | * @return string |
||
313 | */ |
||
314 | View Code Duplication | public function ChkCfgFileRule($rule) { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
315 | if (2 > strlen($rule)) |
||
316 | return("Cache rule is not defined or too short."); |
||
317 | |||
318 | if (0 != (strlen($rule) % 2)) |
||
319 | return("Cache rule {$this->aCfg['cache-file-rule']} may not right."); |
||
320 | |||
321 | return ''; |
||
322 | } // end of func ChkCfgFileRule |
||
323 | |||
324 | |||
325 | /** |
||
326 | * Set default config |
||
327 | * |
||
328 | * @return this |
||
329 | */ |
||
330 | View Code Duplication | protected function SetCfgDefault () { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
331 | parent::SetCfgDefault(); |
||
332 | |||
333 | // Cache type: file, memcached |
||
334 | $this->aCfg['cache-type'] = ''; |
||
335 | |||
336 | // Type file: dir where data file store |
||
337 | $this->aCfg['cache-file-dir'] = ''; |
||
338 | /** |
||
339 | * Type file: cache file store rule |
||
340 | * |
||
341 | * Group by every 2-chars, their means: |
||
342 | * 10 first 2 char of md5 hash, 16 * 16 = 256 |
||
343 | * 11 3-4 char of md5 hash |
||
344 | * 20 last 2 char of md5 hash |
||
345 | * 30 first 2 char of key |
||
346 | * 40 last 2 char of key |
||
347 | * 5n crc32, n=0..3, 16 * 16 = 256 |
||
348 | * Join these str with '/', got full path of cache file. |
||
349 | */ |
||
350 | $this->aCfg['cache-file-rule'] = ''; |
||
351 | |||
352 | return $this; |
||
353 | } // end of func SetCfgDefault |
||
354 | |||
355 | |||
356 | /** |
||
357 | * Init treatment |
||
358 | */ |
||
359 | public function Init () { |
||
360 | parent::Init(); |
||
361 | |||
362 | // Check config |
||
363 | if (!empty($this->aCfg['cache-file-dir'])) { |
||
364 | $s = $this->ChkCfgFileDir($this->aCfg['cache-file-dir']); |
||
365 | if (!empty($s)) |
||
366 | die($s); |
||
367 | } |
||
368 | if (!empty($this->aCfg['cache-file-rule'])) { |
||
369 | $s = $this->ChkCfgFileRule($this->aCfg['cache-file-rule']); |
||
370 | if (!empty($s)) |
||
371 | die($s); |
||
372 | } |
||
373 | } // end of func Init |
||
374 | |||
375 | |||
376 | } // end of class Cache |
||
377 | |||
378 | ?> |
||
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. ![]() |
|||
379 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.