Issues (1061)

Sources/CacheAPI-xcache.php (3 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2020 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 RC2
12
 */
13
14
if (!defined('SMF'))
15
	die('Hacking attempt...');
16
17
/**
18
 * Our Cache API class
19
 *
20
 * @package cacheAPI
21
 */
22
class xcache_cache extends cache_api
23
{
24
	/**
25
	 * {@inheritDoc}
26
	 */
27
	public function __construct()
28
	{
29
		global $modSettings;
30
31
		parent::__construct();
32
33
		// Xcache requuires a admin username and password in order to issue a clear.
34
		if (!empty($modSettings['xcache_adminuser']) && !empty($modSettings['xcache_adminpass']))
35
		{
36
			ini_set('xcache.admin.user', $modSettings['xcache_adminuser']);
37
			ini_set('xcache.admin.pass', md5($modSettings['xcache_adminpass']));
38
		}
39
	}
40
41
	/**
42
	 * {@inheritDoc}
43
	 */
44
	public function isSupported($test = false)
45
	{
46
		$supported = function_exists('xcache_get') && function_exists('xcache_set') && ini_get('xcache.var_size') > 0;
47
48
		if ($test)
49
			return $supported;
50
		return parent::isSupported() && $supported;
51
	}
52
53
	/**
54
	 * {@inheritDoc}
55
	 */
56
	public function getData($key, $ttl = null)
57
	{
58
		$key = $this->prefix . strtr($key, ':/', '-_');
59
60
		return xcache_get($key);
61
	}
62
63
	/**
64
	 * {@inheritDoc}
65
	 */
66
	public function putData($key, $value, $ttl = null)
67
	{
68
		$key = $this->prefix . strtr($key, ':/', '-_');
69
70
		if ($value === null)
71
			return xcache_unset($key);
72
		else
73
			return xcache_set($key, $value, $ttl);
74
	}
75
76
	/**
77
	 * {@inheritDoc}
78
	 */
79
	public function cleanCache($type = '')
80
	{
81
		global $modSettings;
82
83
		// Xcache requuires a admin username and password in order to issue a clear. Ideally this would log an error, but it seems like something that could fill up the error log quickly.
84
		if (empty($modSettings['xcache_adminuser']) || empty($modSettings['xcache_adminpass']))
85
		{
86
			// We are going to at least invalidate it.
87
			$this->invalidateCache();
88
			return false;
89
		}
90
91
		// if passed a type, clear that type out
92
		if ($type === '' || $type === 'user')
93
			xcache_clear_cache(XC_TYPE_VAR, 0);
0 ignored issues
show
The constant XC_TYPE_VAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94
		if ($type === '' || $type === 'data')
95
			xcache_clear_cache(XC_TYPE_PHP, 0);
0 ignored issues
show
The constant XC_TYPE_PHP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
96
97
		$this->invalidateCache();
98
		return true;
99
	}
100
101
	/**
102
	 * {@inheritDoc}
103
	 */
104
	public function cacheSettings(array &$config_vars)
105
	{
106
		global $context, $txt;
107
108
		$config_vars[] = $txt['cache_xcache_settings'];
109
		$config_vars[] = array('xcache_adminuser', $txt['cache_xcache_adminuser'], 'db', 'text', 0, 'xcache_adminuser');
110
111
		// While we could md5 this when saving, this could be tricky to be sure it doesn't get corrupted on additional saves.
112
		$config_vars[] = array('xcache_adminpass', $txt['cache_xcache_adminpass'], 'db', 'text', 0);
113
114
		if (!isset($context['settings_post_javascript']))
115
			$context['settings_post_javascript'] = '';
116
117
		$context['settings_post_javascript'] .= '
118
			$("#cache_accelerator").change(function (e) {
119
				var cache_type = e.currentTarget.value;
120
				$("#xcache_adminuser").prop("disabled", cache_type != "xcache");
121
				$("#xcache_adminpass").prop("disabled", cache_type != "xcache");
122
			});';
123
	}
124
125
	/**
126
	 * {@inheritDoc}
127
	 */
128
	public function getVersion()
129
	{
130
		return XCACHE_VERSION;
0 ignored issues
show
The constant XCACHE_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
131
	}
132
}
133
134
?>