| 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 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		// Set our default cachedir. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$this->setCachedir(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	public function connect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$database = $this->cachedir . '/' . 'SQLite3Cache.db3'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$this->cacheDB = new SQLite3($database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$this->cacheDB->busyTimeout(1000); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		if (filesize($database) == 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			$this->cacheDB->exec('CREATE TABLE cache (key text unique, value blob, ttl int);'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			$this->cacheDB->exec('CREATE INDEX ttls ON cache(ttl);'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$this->cacheTime = time(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 67 |  | View Code Duplication | 	public function isSupported($test = false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$supported = class_exists("SQLite3") && is_writable($this->cachedir); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		if ($test) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			return $supported; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		return parent::isSupported() && $supported; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	public function getData($key, $ttl = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$ttl = time() - $ttl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$query = 'SELECT value FROM cache WHERE key = \'' . $this->cacheDB->escapeString($key) . '\' AND ttl >= ' . $ttl . ' LIMIT 1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$result = $this->cacheDB->query($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$value = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		while ($res = $result->fetchArray(SQLITE3_ASSOC)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			$value = $res['value']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		return !empty($value) ? $value : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	public function putData($key, $value, $ttl = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$ttl = $this->cacheTime + $ttl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$query = 'REPLACE INTO cache VALUES (\'' . $this->cacheDB->escapeString($key) . '\', \'' . $this->cacheDB->escapeString($value) . '\', ' . $this->cacheDB->escapeString($ttl) . ');'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		$result = $this->cacheDB->exec($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	public function cleanCache($type = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		if($type == 'expired') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			$query = 'DELETE FROM cache WHERE ttl >= ' . time().';'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			$query = 'DELETE FROM cache;'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$result = $this->cacheDB->exec($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		$query	= 'VACUUM;'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$this->cacheDB->exec($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 127 |  | View Code Duplication | 	public function cacheSettings(array &$config_vars) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		global $context, $txt; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$config_vars[] = $txt['cache_sqlite_settings']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		$config_vars[] = array('cachedir_sqlite', $txt['cachedir_sqlite'], 'file', 'text', 36, 'cache_sqlite_cachedir'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		if (!isset($context['settings_post_javascript'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			$context['settings_post_javascript'] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$context['settings_post_javascript'] .= ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			$("#cache_accelerator").change(function (e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				var cache_type = e.currentTarget.value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 				$("#cachedir_sqlite").prop("disabled", cache_type != "sqlite"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			});'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * Sets the $cachedir or uses the SMF default $cachedir.. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 * @param string $dir A valid path | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * @return boolean If this was successful or not. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	public function setCachedir($dir = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		global $cachedir, $cachedir_sqlite; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		// If its invalid, use SMF's. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		if (is_null($dir) || !is_writable($dir)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 			if(is_null($cachedir_sqlite) || !is_writable($cachedir_sqlite)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 				$this->cachedir = $cachedir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 			else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 				$this->cachedir = $cachedir_sqlite; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 			$this->cachedir = $dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	public function getVersion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		$temp = $this->cacheDB->version(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		return $temp['versionString']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 178 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  | 	public function housekeeping() | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  | 		$this->cleanCache('expired'); | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 185 |  |  | ?> | 
            
                                                        
            
                                    
            
            
                | 186 |  |  |  | 
            
                        
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.