aboutsummaryrefslogtreecommitdiffstats
path: root/library/spam/b8/storage/storage_frndc.php
blob: 02253635093a280d949e4eeb48e4a2fcfc2a6a5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php

#   Copyright (C) 2006-2011 Tobias Leupold <tobias.leupold@web.de>
#
#   This file is part of the b8 package
#
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU Lesser General Public License as published by
#   the Free Software Foundation in version 2.1 of the License.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
#   License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software Foundation,
#   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

/**
 * The MySQL abstraction layer for communicating with the database.
 * Copyright (C) 2009 Oliver Lillie (aka buggedcom)
 * Copyright (C) 2010-2011 Tobias Leupold <tobias.leupold@web.de>
 *
 * @license LGPL
 * @access public
 * @package b8
 * @author Oliver Lillie (aka buggedcom) (original PHP 5 port and optimizations)
 * @author Tobias Leupold
 */

class b8_storage_mysql extends b8_storage_base
{

	public $config = array(
		'database'        => 'b8_wordlist',
		'table_name'      => 'b8_wordlist',
		'host'            => 'localhost',
		'user'            => FALSE,
		'pass'            => FALSE,
		'connection'      => NULL
	);

	public $b8_config = array(
		'degenerator'     => NULL,
		'today'           => NULL
	);

	private $_connection                   = NULL;
	private $_deletes                      = array();
	private $_puts                         = array();
	private $_updates                      = array();

	const DATABASE_CONNECTION_FAIL         = 'DATABASE_CONNECTION_FAIL';
	const DATABASE_CONNECTION_ERROR        = 'DATABASE_CONNECTION_ERROR';
	const DATABASE_CONNECTION_BAD_RESOURCE = 'DATABASE_CONNECTION_BAD_RESOURCE';
	const DATABASE_SELECT_ERROR            = 'DATABASE_SELECT_ERROR';
	const DATABASE_TABLE_ACCESS_FAIL       = 'DATABASE_TABLE_ACCESS_FAIL';
	const DATABASE_WRONG_VERSION           = 'DATABASE_WRONG_VERSION';

	/**
	 * Constructs the database layer.
	 *
	 * @access public
	 * @param string $config
	 */

	function __construct($config, $degenerator, $today)
	{

		# Pass some variables of the main b8 config to this class
		$this->b8_config['degenerator'] = $degenerator;
		$this->b8_config['today']       = $today;

		# Validate the config items

		if(count($config) > 0) {

			foreach ($config as $name => $value) {

				switch($name) {

					case 'table_name':
					case 'host':
					case 'user':
					case 'pass':
					case 'database':
						$this->config[$name] = (string) $value;
						break;

					case 'connection':

						if($value !== NULL) {

							if(is_resource($value) === TRUE) {
								$resource_type = get_resource_type($value);
								$this->config['connection'] = $resource_type !== 'mysql link' && $resource_type !== 'mysql link persistent' ? FALSE : $value;
							}

							else
								$this->config['connection'] = FALSE;

						}

						break;

				}

			}

		}

	}

	/**
	 * Closes the database connection.
	 *
	 * @access public
	 * @return void
	 */

	function __destruct()
	{

		if($this->_connection === NULL)
			return;

		# Commit any changes before closing
		$this->_commit();

		# Just close the connection if no link-resource was passed and b8 created it's own connection
		if($this->config['connection'] === NULL)
			mysql_close($this->_connection);

		$this->connected = FALSE;

	}

	/**
	 * Connect to the database and do some checks.
	 *
	 * @access public
	 * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8.
	 */

	public function connect()
	{

		# Are we already connected?
		if($this->connected === TRUE)
			return TRUE;

		# Are we using an existing passed resource?
		if($this->config['connection'] === FALSE) {
			# ... yes we are, but the connection is not a resource, so return an error
			$this->connected = FALSE;
			return self::DATABASE_CONNECTION_BAD_RESOURCE;
		}

		elseif($this->config['connection'] === NULL) {

			# ... no we aren't so we have to connect.

			if($this->_connection = mysql_connect($this->config['host'], $this->config['user'], $this->config['pass'])) {
				if(mysql_select_db($this->config['database'], $this->_connection) === FALSE) {
					$this->connected = FALSE;
					return self::DATABASE_SELECT_ERROR . ": " . mysql_error();
				}
			}
			else {
				$this->connected = FALSE;
				return self::DATABASE_CONNECTION_ERROR;
			}

		}

		else {
			# ... yes we are
			$this->_connection = $this->config['connection'];
		}

		# Just in case ...
		if($this->_connection === NULL) {
			$this->connected = FALSE;
			return self::DATABASE_CONNECTION_FAIL;
		}

		# Check to see if the wordlist table exists
		if(mysql_query('DESCRIBE ' . $this->config['table_name'], $this->_connection) === FALSE) {
			$this->connected = FALSE;
			return self::DATABASE_TABLE_ACCESS_FAIL . ": " . mysql_error();
		}

		# Everything is okay and connected
		$this->connected = TRUE;

		# Let's see if this is a b8 database and the version is okay
		return $this->check_database();

	}

	/**
	 * Does the actual interaction with the database when fetching data.
	 *
	 * @access protected
	 * @param array $tokens
	 * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
	 */

	protected function _get_query($tokens)
	{

		# Construct the query ...

		if(count($tokens) > 0) {

			$where = array();

			foreach ($tokens as $token) {
				$token = mysql_real_escape_string($token, $this->_connection);
				array_push($where, $token);
			}

			$where = 'token IN ("' . implode('", "', $where) . '")';
		}

		else {
			$token = mysql_real_escape_string($token, $this->_connection);
			$where = 'token = "' . $token . '"';
		}

		# ... and fetch the data

		$result = mysql_query('
			SELECT token, count
			FROM ' . $this->config['table_name'] . '
			WHERE ' . $where . ';
		', $this->_connection);

		$data = array();

		while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
			$data[$row['token']] = $row['count'];

		mysql_free_result($result);

		return $data;

	}

	/**
	 * Store a token to the database.
	 *
	 * @access protected
	 * @param string $token
	 * @param string $count
	 * @return void
	 */

	protected function _put($token, $count) {
		$token = mysql_real_escape_string($token, $this->_connection);
		$count = mysql_real_escape_string($count, $this->_connection);;
		array_push($this->_puts, '("' . $token . '", "' . $count . '")');
	}

	/**
	 * Update an existing token.
	 *
	 * @access protected
	 * @param string $token
	 * @param string $count
	 * @return void
	 */

	protected function _update($token, $count)
	{
		$token = mysql_real_escape_string($token, $this->_connection);
		$count = mysql_real_escape_string($count, $this->_connection);
		array_push($this->_updates, '("' . $token . '", "' . $count . '")');
	}

	/**
	 * Remove a token from the database.
	 *
	 * @access protected
	 * @param string $token
	 * @return void
	 */

	protected function _del($token)
	{
		$token = mysql_real_escape_string($token, $this->_connection);
		array_push($this->_deletes, $token);
	}

	/**
	 * Commits any modification queries.
	 *
	 * @access protected
	 * @return void
	 */

	protected function _commit()
	{

		if(count($this->_deletes) > 0) {

			$result = mysql_query('
				DELETE FROM ' . $this->config['table_name'] . '
				WHERE token IN ("' . implode('", "', $this->_deletes) . '");
			', $this->_connection);

			if(is_resource($result) === TRUE)
				mysql_free_result($result);

			$this->_deletes = array();

		}

		if(count($this->_puts) > 0) {

			$result = mysql_query('
				INSERT INTO ' . $this->config['table_name'] . '(token, count)
				VALUES ' . implode(', ', $this->_puts) . ';', $this->_connection);

			if(is_resource($result) === TRUE)
				mysql_free_result($result);

			$this->_puts = array();

		}

		if(count($this->_updates) > 0) {

			$result = mysql_query('
				INSERT INTO ' . $this->config['table_name'] . '(token, count)
				VALUES ' . implode(', ', $this->_updates) . '
				ON DUPLICATE KEY UPDATE ' . $this->config['table_name'] . '.count = VALUES(count);', $this->_connection);

			if(is_resource($result) === TRUE)
				mysql_free_result($result);

			$this->_updates = array();

		}

	}

}

?>