Issues (1752)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

class/cache/cache-file.php (21 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
require_once(dirname(__FILE__) . '/cache.php');
3
require_once(FWOLFLIB . 'func/filesystem.php');
4
5
6
/**
7
 * Key - value cache system, data store in file.
8
 *
9
 * @deprecated  Use Fwlib\Cache\CacheFile
10
 * @package		fwolflib
11
 * @subpackage	class.cache
12
 * @copyright	Copyright 2010-2012, Fwolf
13
 * @author		Fwolf <[email protected]>
14
 * @since		2010-01-07
15
 */
16
class CacheFile extends Cache {
0 ignored issues
show
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: CacheGenVal, CacheLifetime
Loading history...
Deprecated Code introduced by
The class Cache has been deprecated with message: use Fwlib\Cache\Cache

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.

Loading history...
17
18
19
	/**
20
	 * Constructor
21
	 *
22
	 * @param	array	$ar_cfg
23
	 */
24
	public function __construct ($ar_cfg = array()) {
25
		parent::__construct($ar_cfg);
26
27
		// Unset for auto new
28
	} // end of func __construct
29
30
31
	/**
32
	 * Check if cache is ready for use.
33
	 *
34
	 * @return	boolean
35
	 */
36
	public function ChkCfg () {
37
		$b_pass = true;
38
39 View Code Duplication
		if (empty($this->aCfg['cache-file-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.

Loading history...
40
			$b_pass = false;
41
		else {
42
			$s = $this->ChkCfgFileDir($this->aCfg['cache-file-dir']);
43
			if (!empty($s)) {
44
				$this->Log('Cache file cfg dir error: ' . $s, 4);
45
				$b_pass = false;
46
			}
47
		}
48
49 View Code Duplication
		if (empty($this->aCfg['cache-file-rule']))
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.

Loading history...
50
			$b_pass = false;
51
		else {
52
			$s = $this->ChkCfgFileRule($this->aCfg['cache-file-rule']);
53
			if (!empty($s)) {
54
				$this->Log('Cache file cfg rule error: ' . $s, 4);
55
				$b_pass = false;
56
			}
57
		}
58
59
		return $b_pass;
60
	} // end of func ChkCfg
61
62
63
	/**
64
	 * Check config/cache store dir valid and writable
65
	 * If error, return error msg, else return empty str.
66
	 *
67
	 * @param	string	$dir
68
	 * @return	string
69
	 */
70
	public function ChkCfgFileDir ($dir) {
71
		$s = '';
72
73
		if (empty($dir))
74
			$s = "Cache dir {$dir} is not defined.";
75
76 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.

Loading history...
77
			if (false == mkdir($dir, 0755, true))
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
78
				$s = "Fail to create cache dir {$dir}.";
79
		}
80
		else {
81
			if (!is_writable($dir))
82
				$s = "Cache dir {$dir} is not writable.";
83
		}
84
85
		return $s;
86
	} // end of func ChkCfgFileDir
87
88
89
	/**
90
	 * Check cache rule exist and valid
91
	 * If error, return error msg, else return empty str.
92
	 *
93
	 * @param	string	$rule
94
	 * @return	string
95
	 */
96 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.

Loading history...
97
		if (2 > strlen($rule))
98
			return("Cache rule is not defined or too short.");
99
100
		if (0 != (strlen($rule) % 2))
101
			return("Cache rule {$this->aCfg['cache-file-rule']} may not right.");
102
103
		return '';
104
	} // end of func ChkCfgFileRule
105
106
107
	/**
108
	 * Delete cache data
109
	 * @param	string	$key
110
	 * @return	$this
111
	 */
112
	public function Del ($key) {
113
		$s_file = $this->FilePath($key);
114
115
		if (file_exists($s_file)) {
116
			unlink($s_file);
117
		}
118
119
		return $this;
120
	} // end of func Del
121
122
123
	/**
124
	 * Is cache data expire ?
125
	 *
126
	 * File cache does not keep lifetime in cache,
127
	 * so it need a lifetime from outside,
128
	 * or use default lifetime config.
129
	 *
130
	 * @param	string	$key
131
	 * @param	int		$lifetime	Cache lifetime, in second.
132
	 * @return	boolean				True means it IS expired.
133
	 */
134
	public function Expire ($key, $lifetime = NULL) {
135
		$s_file = $this->FilePath($key);
136
137
		// File doesn't exist
138
		if (!file_exists($s_file))
139
			return true;
140
141
		if (0 == $lifetime)
0 ignored issues
show
It seems like you are loosely comparing $lifetime of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
142
			return false;
143
144
		// Check file expire time
145
		$t_expire = $this->ExpireTime($lifetime, filemtime($s_file));
0 ignored issues
show
The method ExpireTime() does not exist on CacheFile. Did you maybe mean Expire()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
146
		if (time() > $t_expire)
147
			return true;
148
		else
149
			return false;
150
	} // end of func Expire
151
152
153
	/**
154
	 * Compute path of a key's data file
155
	 *
156
	 * @param	string	$key
157
	 * @return	string
158
	 */
159 View Code Duplication
	public function FilePath ($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.

Loading history...
160
		$s_path = $this->aCfg['cache-file-dir'];
161
162
		$ar_rule = str_split($this->aCfg['cache-file-rule'], 2);
163
		if (empty($ar_rule))
164
			return $s_path;
165
166
		foreach ($ar_rule as $rule)
167
			$s_path .= $this->FilePathSec($rule, $key) . '/';
168
169
		// Filename
170
		$s_path .= $this->FilePathFilename($key);
171
172
		return $s_path;
173
	} // end of func FilePath
174
175
176
	/**
177
	 * Compute name of a key's data file
178
	 *
179
	 * @param	string	$key
180
	 * @return	string
181
	 */
182
	protected function FilePathFilename($key) {
183
		return substr(md5($key), 0, 8);
184
	} // end of func FilePathFilename
185
186
187
	/**
188
	 * Compute path of a key by a single rule section
189
	 *
190
	 * @param	string	$rule
191
	 * @param	string	$key
192
	 * @return	string
193
	 * @see	$sCacheRule
194
	 */
195 View Code Duplication
	protected function FilePathSec($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.

Loading history...
196
		$i_len = 2;
197
198
		if ($i_len > strlen($rule))
199
			return '';
200
201
		$i = intval($rule{1});
202
		if (1 == $rule{0}) {
203
			// md5 from start
204
			$i_start = $i_len * $i;
205
			$s_seed = md5($key);
206
		} elseif (2 == $rule{0}) {
207
			// md5 from end
208
			$i_start = -1 * $i_len * ($i + 1);
209
			$s_seed = md5($key);
210
		} elseif (3 == $rule{0}) {
211
			// raw from start
212
			$i_start = $i_len * $i;
213
			$s_seed = $key;
214
		} elseif (4 == $rule{0}) {
215
			// raw from end
216
			$i_start = -1 * $i_len * ($i + 1);
217
			$s_seed = $key;
218
		} elseif (5 == $rule{0}) {
219
			// crc32
220
			if (3 < $i)
221
				$i = $i % 3;
222
			$i_start = $i_len * $i;
223
			$s_seed = hash('crc32', $key);
224
		}
225
		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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
226
	} // end of func FilePathSec
227
228
229
	/**
230
	 * Read cache and return value
231
	 *
232
	 * File cache should check lifetime when get,
233
	 * return NULL when fail.
234
	 *
235
	 * @param	string	$key
236
	 * @param	int		$lifetime		Cache lifetime, 0/no check, NULL/cfg
237
	 * @return	mixed
238
	 */
239
	public function Get ($key, $lifetime = NULL) {
240
		if ($this->Expire($key, $lifetime)) {
241
			parent::$aLogGet[] = array(
242
				'key'	=> $key,
243
				'success'	=> false,
244
			);
245
			return NULL;
246
		}
247
248
		// Read from file and parse it.
249
		$s_file = $this->FilePath($key);
250
		$s_cache = file_get_contents($s_file);
251
		parent::$aLogGet[] = array(
252
			'key'	=> $key,
253
			'success'	=> !(false === $s_cache),
254
		);
255
256
		return $this->ValDecode($s_cache);
0 ignored issues
show
The method ValDecode() does not seem to exist on object<CacheFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
257
	} // end of func Get
258
259
260
	/**
261
	 * Init treatment
262
	 *
263
	 * @param	array	$ar_cfg
0 ignored issues
show
There is no parameter named $ar_cfg. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
264
	 * @return	object
265
	 */
266
	public function Init () {
267
		parent::Init();
268
269
		$this->ChkCfg();
270
271
		return $this;
272
	} // end of func Init
273
274
275
	/**
276
	 * Write data to cache
277
	 *
278
	 * Lifetime check when get.
279
	 *
280
	 * @param	string	$key
281
	 * @param	mixed	$val
282
	 * @param	int		$lifetime
283
	 * @return	$this
284
	 */
285 View Code Duplication
	public function Set ($key, $val, $lifetime = NULL) {
0 ignored issues
show
The parameter $lifetime is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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.

Loading history...
286
		$s_file = $this->FilePath($key);
287
		$s_cache = $this->ValEncode($val);
0 ignored issues
show
The method ValEncode() does not seem to exist on object<CacheFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
288
289
		// Create each level dir if not exists
290
		$s_dir = DirName1($s_file);
0 ignored issues
show
Deprecated Code introduced by
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.

Loading history...
291
		if (!file_exists($s_dir))
292
			mkdir($s_dir, 0755, true);
293
294
		// Finally write file
295
		file_put_contents($s_file, $s_cache, LOCK_EX);
296
297
		return $this;
298
	} // end of func Set
299
300
301
	/**
302
	 * Set default config
303
	 *
304
	 * @return	this
305
	 */
306 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.

Loading history...
307
		parent::SetCfgDefault();
308
309
		// Cache type: file
310
		$this->aCfg['cache-type'] = 'file';
311
312
313
		// Dir where data file store
314
		$this->aCfg['cache-file-dir'] = '/tmp/cache/';
315
316
		/**
317
		 * Cache file store rule
318
		 *
319
		 * Group by every 2-chars, their means:
320
		 * 10	first 2 char of md5 hash, 16 * 16 = 256
321
		 * 11	3-4 char of md5 hash
322
		 * 20	last 2 char of md5 hash
323
		 * 30	first 2 char of key
324
		 * 40	last 2 char of key
325
		 * 5n	crc32, n=0..3, 16 * 16 = 256
326
		 * Join these str with '/', got full path of cache file.
327
		 */
328
		$this->aCfg['cache-file-rule'] = '10';
329
330
331
		return $this;
332
	} // end of func SetCfgDefault
333
334
335
} // end of class CacheFile
336
?>
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.

Loading history...
337