Issues (1014)

Sources/Cache/CacheApi.php (2 issues)

1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2022 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1.0
12
 */
13
14
namespace SMF\Cache;
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
abstract class CacheApi
20
{
21
	const APIS_FOLDER = 'APIs';
22
	const APIS_NAMESPACE = 'SMF\Cache\APIs\\';
23
	const APIS_DEFAULT = 'FileBased';
24
25
	/**
26
	 * @var string The maximum SMF version that this will work with.
27
	 */
28
	protected $version_compatible = '2.1.999';
29
30
	/**
31
	 * @var string The minimum SMF version that this will work with.
32
	 */
33
	protected $min_smf_version = '2.1 RC1';
34
35
	/**
36
	 * @var string The prefix for all keys.
37
	 */
38
	protected $prefix = '';
39
40
	/**
41
	 * @var int The default TTL.
42
	 */
43
	protected $ttl = 120;
44
45
	/**
46
	 * Does basic setup of a cache method when we create the object but before we call connect.
47
	 *
48
	 * @access public
49
	 */
50
	public function __construct()
51
	{
52
		$this->setPrefix();
53
	}
54
55
	/**
56
	 * Checks whether we can use the cache method performed by this API.
57
	 *
58
	 * @access public
59
	 * @param bool $test Test if this is supported or enabled.
60
	 * @return bool Whether or not the cache is supported
61
	 */
62
	public function isSupported($test = false)
63
	{
64
		global $cache_enable;
65
66
		if ($test)
67
			return true;
68
69
		return !empty($cache_enable);
70
	}
71
72
	/**
73
	 * Sets the cache prefix.
74
	 *
75
	 * @access public
76
	 * @param string $prefix The prefix to use.
77
	 *     If empty, the prefix will be generated automatically.
78
	 * @return bool If this was successful or not.
79
	 */
80
	public function setPrefix($prefix = '')
81
	{
82
		global $boardurl, $cachedir, $boarddir;
83
84
		if (!is_string($prefix))
0 ignored issues
show
The condition is_string($prefix) is always true.
Loading history...
85
			$prefix = '';
86
87
		// Use the supplied prefix, if there is one.
88
		if (!empty($prefix))
89
		{
90
			$this->prefix = $prefix;
91
92
			return true;
93
		}
94
95
		// Ideally the prefix should reflect the last time the cache was reset.
96
		if (!empty($cachedir) && file_exists($cachedir . '/index.php'))
97
		{
98
			$mtime = filemtime($cachedir . '/index.php');
99
		}
100
		// Fall back to the last time that Settings.php was updated.
101
		elseif (!empty($boarddir) && file_exists($boarddir . '/Settings.php'))
102
		{
103
			$mtime = filemtime($boarddir . '/Settings.php');
104
		}
105
		// This should never happen, but just in case...
106
		else
107
		{
108
			$mtime = filemtime(realpath($_SERVER['SCRIPT_FILENAME']));
109
		}
110
111
		$this->prefix = md5($boardurl . $mtime) . '-SMF-';
112
113
		return true;
114
	}
115
116
	/**
117
	 * Gets the prefix as defined from set or the default.
118
	 *
119
	 * @access public
120
	 * @return string the value of $key.
121
	 */
122
	public function getPrefix()
123
	{
124
		return $this->prefix;
125
	}
126
127
	/**
128
	 * Sets a default Time To Live, if this isn't specified we let the class define it.
129
	 *
130
	 * @access public
131
	 * @param int $ttl The default TTL
132
	 * @return bool If this was successful or not.
133
	 */
134
	public function setDefaultTTL($ttl = 120)
135
	{
136
		$this->ttl = $ttl;
137
138
		return true;
139
	}
140
141
	/**
142
	 * Gets the TTL as defined from set or the default.
143
	 *
144
	 * @access public
145
	 * @return int the value of $ttl.
146
	 */
147
	public function getDefaultTTL()
148
	{
149
		return $this->ttl;
150
	}
151
152
	/**
153
	 * Invalidate all cached data.
154
	 *
155
	 * @return bool Whether or not we could invalidate the cache.
156
	 */
157
	public function invalidateCache()
158
	{
159
		global $cachedir;
160
161
		// Invalidate cache, to be sure!
162
		// ... as long as index.php can be modified, anyway.
163
		if (is_writable($cachedir . '/' . 'index.php'))
164
			@touch($cachedir . '/' . 'index.php');
165
166
		return true;
167
	}
168
169
	/**
170
	 * Closes connections to the cache method.
171
	 *
172
	 * @access public
173
	 * @return bool Whether the connections were closed.
174
	 */
175
	public function quit()
176
	{
177
		return true;
178
	}
179
180
	/**
181
	 * Specify custom settings that the cache API supports.
182
	 *
183
	 * @access public
184
	 * @param array $config_vars Additional config_vars, see ManageSettings.php for usage.
185
	 */
186
	public function cacheSettings(array &$config_vars)
0 ignored issues
show
The parameter $config_vars is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

186
	public function cacheSettings(/** @scrutinizer ignore-unused */ array &$config_vars)

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

Loading history...
187
	{
188
	}
189
190
	/**
191
	 * Gets the latest version of SMF this is compatible with.
192
	 *
193
	 * @access public
194
	 * @return string the value of $key.
195
	 */
196
	public function getCompatibleVersion()
197
	{
198
		return $this->version_compatible;
199
	}
200
201
	/**
202
	 * Gets the min version that we support.
203
	 *
204
	 * @access public
205
	 * @return string the value of $key.
206
	 */
207
	public function getMinimumVersion()
208
	{
209
		return $this->min_smf_version;
210
	}
211
212
	/**
213
	 * Gets the Version of the Caching API.
214
	 *
215
	 * @access public
216
	 * @return string the value of $key.
217
	 */
218
	public function getVersion()
219
	{
220
		return $this->min_smf_version;
221
	}
222
223
	/**
224
	 * Run housekeeping of this cache
225
	 * exp. clean up old data or do optimization
226
	 *
227
	 * @access public
228
	 * @return void
229
	 */
230
	public function housekeeping()
231
	{
232
	}
233
234
	/**
235
	 * Gets the class identifier of the current caching API implementation.
236
	 *
237
	 * @access public
238
	 * @return string the unique identifier for the current class implementation.
239
	 */
240
	public function getImplementationClassKeyName()
241
	{
242
		$class_name = get_class($this);
243
244
		if ($position = strrpos($class_name, '\\'))
245
			return substr($class_name, $position + 1);
246
247
		else
248
			return get_class($this);
249
	}
250
}
251
252
?>