Issues (438)

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.

classes/FileCache.php (9 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
/* zCache
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Affero General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Affero General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 * FileCache for zKillboard (Basically copies what Memcached does)
20
 */
21
class FileCache extends AbstractCache
22
{
23
	/**
24
	 * @param $cacheDir the default cache dir
25
	 * @param $cacheTime the default cache time (5 minutes)
26
	 */
27
28
	protected $cacheDir = "cache/queryCache/";
29
	protected $cacheTime = 300;
30
31
	/**
32
	 * @param string $cd a dir to use instead of default
33
	 */
34
	public function __construct($cd = null)
35
	{
36
		global $cacheDir;
37
		if (isset($cacheDir) && $cd === null) {
38
			$cd = $cacheDir;
39
		}
40
41
		if (!is_null($cd)) $this->cacheDir = $cd;
42
		if (!is_dir($this->cacheDir)) mkdir($this->cacheDir);
43
	}
44
45
	/**
46
	 * Gets the data
47
	 *
48
	 * @param string $key
49
	 * @return array|boolean
50
	 */
51
	public function get($key)
52
	{
53
		if(file_exists($this->cacheDir.sha1($key)))
54
		{
55
			$time = time();
56
			$data = self::getData($key);
57
			$age = $data["age"];
58
			$data = json_decode($data["data"], true);
59
			if($age <= $time)
60
			{
61
				@unlink($this->cacheDir.sha1($key));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
62
				return false;
63
			}
64
			return $data;
65
		}
66
		else
67
			return false;
68
	}
69
70
	/**
71
	 * Sets data
72
	 *
73
	 * @param string $key
74
	 * @param string|array $value
75
	 * @param string $timeout
76
	 *
77
	 * return bool
78
	 */
79
	public function set($key, $value, $timeout)
80
	{
81
		return self::setData($key, $value, $timeout) !== false;
82
	}
83
84
	/**
85
	 * Replaces data
86
	 *
87
	 * @param string $key
88
	 * @param string|array $value
89
	 * @param string $timeout
90
	 * @return boolean
91
	 */
92
	public function replace($key, $value, $timeout)
93
	{
94
		if(file_exists($this->cacheDir.sha1($key)))
95
		{
96
			@unlink($this->cacheDir.sha1($key));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
			if(self::setData($key, $value, $timeout) !== false)
98
				return true;
99
		}
100
101
		return false;
102
	}
103
104
	/**
105
	 * Deletes a key
106
	 *
107
	 * @param string $key
108
	 * @return bool
109
	 */
110
	public function delete($key)
111
	{
112
		try
113
		{
114
			@unlink($this->cacheDir.sha1($key));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
115
		}
116
		catch (Exception $e)
117
		{
118
			return false;
119
		}
120
		return true;
121
	}
122
123
	/**
124
	 * Increments value
125
	 *
126
	 * @param string $key
127
	 * @param int $step
128
	 * @param int $timeout
129
	 * @return bool
130
	 */
131
	public function increment($key, $step = 1, $timeout = 0)
132
	{
133
		$data = self::getData($key);
134
		$data = json_decode($data["data"], true);
135
136
		try
137
		{
138
			@unlink($this->cacheDir.sha1($key));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
139
			return self::setData($key, $data+$step, $timeout);
140
		}
141
		catch (Exception $e)
142
		{
143
			return false;
144
		}
145
	}
146
147
	/**
148
	 * Decrements value
149
	 *
150
	 * @param string $key
151
	 * @param int $step
152
	 * @param int $timeout
153
	 * @return bool
154
	 */
155
	public function decrement($key, $step = 1, $timeout = 0)
156
	{
157
		$data = self::getData($key);
158
		$data = json_decode($data["data"], true);
159
160
		try
161
		{
162
			@unlink($this->cacheDir.sha1($key));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
163
			return self::setData($key, $data-$step, $timeout);
164
		}
165
		catch (Exception $e)
166
		{
167
			return false;
168
		}
169
	}
170
171
	/**
172
	 * Flushes the cache
173
	 */
174
	public function flush()
175
	{
176
		$dir = opendir($this->cacheDir);
177
		while($file = readdir($dir))
178
		{
179
			@unlink($this->cacheDir.$file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
180
		}
181
	}
182
183
	/**
184
	 * Sets data to cache file
185
	 *
186
	 * @param string $key
187
	 * @param string|array $value
188
	 * @param int $timeout
189
	 *
190
	 * return bool
191
	 */
192
	private function setData($key, $value, $timeout = NULL)
193
	{
194
		if(!$timeout)
0 ignored issues
show
Bug Best Practice introduced by
The expression $timeout of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
195
			$timeout = $this->cacheTime;
196
197
		try
198
		{
199
			// fix, so timeout will be timestamp based
200
			$timeout = time() + $timeout;
201
202
			$data = $timeout."%".json_encode($value);
203
			if(@file_put_contents($this->cacheDir.sha1($key), $data) === false)
204
				return false;
205
		}
206
		catch (Exception $e)
207
		{
208
			return false;
209
		}
210
		return $value;
211
	}
212
213
	/**
214
	 * Gets the data from the cache
215
	 *
216
	 * @param string $key
217
	 * @param bool $sha
218
	 * @return array
219
	 */
220
	private function getData($key, $sha = true)
221
	{
222
		// @todo real error handling, not just surpression.
223
		if($sha == 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...
224
			$data = @file_get_contents($this->cacheDir.sha1($key));
225
		else
226
			$data = @file_get_contents($this->cacheDir.$key);
227
228
		$f = explode("%", $data, 2); // We only want the first occurance of % exploded, not everything else aswell.
229
		$age = array_shift($f);
230
		$data = implode($f);
231
		return array("age" => (int) $age, "data" => $data);
232
	}
233
234
	/**
235
	 * Cleans up old and unused query cache files
236
	 */
237
	public function cleanUp()
238
	{
239
		$dir = opendir($this->cacheDir);
240
		while($file = readdir($dir))
241
		{
242
			if($file != "." && $file != "..")
243
			{
244
				$data = self::getData($file, false);
245
				$age = $data["age"];
246
				$time = time();
247
				if($age <= $time)
248
				{
249
					@unlink($this->cacheDir.$file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
250
				}
251
			}
252
		}
253
	}
254
}
255