Completed
Push — release-2.1 ( aa21c4...7040ad )
by Mathias
09:20
created

Sources/CacheAPI-sqlite.php (4 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
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines http://www.simplemachines.org
8
 * @copyright 2017 Simple Machines and individual contributors
9
 * @license http://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 Beta 4
12
 */
13
14
if (!defined('SMF'))
15
	die('Hacking attempt...');
16
17
/**
18
 * SQLite Cache API class
19
 * @package cacheAPI
20
 */
21
class sqlite_cache extends cache_api
22
{
23
	/**
24
	 * @var string The path to the current $cachedir directory.
25
	 */
26
	private $cachedir = null;
27
28
	/**
29
	 * @var SQLite3
30
	 */
31
	private $cacheDB = null;
32
33
	/**
34
	 * @var int
35
	 */
36
	private $cacheTime = 0;
37
38
	public function __construct()
39
	{
40
41
		parent::__construct();
42
43
		// Set our default cachedir.
44
		$this->setCachedir();
45
46
	}
47
48
	/**
49
	 * {@inheritDoc}
50
	 */
51
	public function connect()
52
	{
53
54
		$database = $this->cachedir . '/' . 'SQLite3Cache.db3';
55
		$this->cacheDB = new SQLite3($database);
56
		$this->cacheDB->busyTimeout(1000);
57
		if (filesize($database) == 0)
58
		{
59
			$this->cacheDB->exec('CREATE TABLE cache (key text unique, value blob, ttl int);');
60
			$this->cacheDB->exec('CREATE INDEX ttls ON cache(ttl);');
61
		}
62
		$this->cacheTime = time();
63
64
	}
65
66
	/**
67
	 * {@inheritDoc}
68
	 */
69 View Code Duplication
	public function isSupported($test = false)
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...
70
	{
71
		$supported = class_exists("SQLite3") && is_writable($this->cachedir);
72
73
		if ($test)
74
		{
75
			return $supported;
76
		}
77
78
		return parent::isSupported() && $supported;
79
	}
80
81
	/**
82
	 * {@inheritDoc}
83
	 */
84
	public function getData($key, $ttl = null)
85
	{
86
		$ttl = time() - $ttl;
87
		$query = 'SELECT value FROM cache WHERE key = \'' . $this->cacheDB->escapeString($key) . '\' AND ttl >= ' . $ttl . ' LIMIT 1';
88
		$result = $this->cacheDB->query($query);
89
90
		$value = null;
91
		while ($res = $result->fetchArray(SQLITE3_ASSOC))
92
		{
93
			$value = $res['value'];
94
		}
95
96
		return !empty($value) ? $value : null;
97
	}
98
99
	/**
100
	 * {@inheritDoc}
101
	 */
102
	public function putData($key, $value, $ttl = null)
103
	{
104
105
		$ttl = $this->cacheTime + $ttl;
106
		$query = 'REPLACE INTO cache VALUES (\'' . $this->cacheDB->escapeString($key) . '\', \'' . $this->cacheDB->escapeString($value) . '\', ' . $this->cacheDB->escapeString($ttl) . ');';
107
		$result = $this->cacheDB->exec($query);
108
109
		return $result;
110
	}
111
112
	/**
113
	 * {@inheritDoc}
114
	 */
115
	public function cleanCache($type = '')
116
	{
117
118
		$query = 'DELETE FROM cache;';
119
		$result = $this->cacheDB->exec($query);
120
121
		return $result;
122
123
	}
124
125
	/**
126
	 * {@inheritDoc}
127
	 */
128 View Code Duplication
	public function cacheSettings(array &$config_vars)
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...
129
	{
130
		global $context, $txt;
131
132
		$config_vars[] = $txt['cache_sqlite_settings'];
133
		$config_vars[] = array('cachedir_sqlite', $txt['cachedir_sqlite'], 'file', 'text', 36, 'cache_sqlite_cachedir');
134
135
		if (!isset($context['settings_post_javascript']))
136
		{
137
			$context['settings_post_javascript'] = '';
138
		}
139
140
		$context['settings_post_javascript'] .= '
141
			$("#cache_accelerator").change(function (e) {
142
				var cache_type = e.currentTarget.value;
143
				$("#cachedir_sqlite").prop("disabled", cache_type != "sqlite");
144
			});';
145
	}
146
147
	/**
148
	 * Sets the $cachedir or uses the SMF default $cachedir..
149
	 *
150
	 * @access public
151
	 *
152
	 * @param string $dir A valid path
153
	 *
154
	 * @return boolean If this was successful or not.
155
	 */
156 View Code Duplication
	public function setCachedir($dir = null)
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...
157
	{
158
		global $cachedir_sqlite;
159
160
		// If its invalid, use SMF's.
161
		if (is_null($dir) || !is_writable($dir))
162
		{
163
			$this->cachedir = $cachedir_sqlite;
164
		}
165
		else
166
		{
167
			$this->cachedir = $dir;
168
		}
169
	}
170
171
}
172
173
?>
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...