]> git.mxchange.org Git - friendica.git/blob - library/spam/b8/storage/storage_frndc.php
fixed syntax errors
[friendica.git] / library / spam / b8 / storage / storage_frndc.php
1 <?php
2
3 #   Copyright (C) 2006-2011 Tobias Leupold <tobias.leupold@web.de>
4 #
5 #   This file is part of the b8 package
6 #
7 #   This program is free software; you can redistribute it and/or modify it
8 #   under the terms of the GNU Lesser General Public License as published by
9 #   the Free Software Foundation in version 2.1 of the License.
10 #
11 #   This program is distributed in the hope that it will be useful, but
12 #   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 #   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14 #   License for more details.
15 #
16 #   You should have received a copy of the GNU Lesser General Public License
17 #   along with this program; if not, write to the Free Software Foundation,
18 #   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19
20 /**
21  * The MySQL abstraction layer for communicating with the database.
22  * Copyright (C) 2009 Oliver Lillie (aka buggedcom)
23  * Copyright (C) 2010-2011 Tobias Leupold <tobias.leupold@web.de>
24  *
25  * @license LGPL
26  * @access public
27  * @package b8
28  * @author Oliver Lillie (aka buggedcom) (original PHP 5 port and optimizations)
29  * @author Tobias Leupold
30  */
31
32 class b8_storage_frndc extends b8_storage_base
33 {
34
35         public $config = array(
36                 'database'        => 'b8_wordlist',
37                 'table_name'      => 'b8_wordlist',
38                 'host'            => 'localhost',
39                 'user'            => FALSE,
40                 'pass'            => FALSE,
41                 'connection'      => NULL
42         );
43
44         public $b8_config = array(
45                 'degenerator'     => NULL,
46                 'today'           => NULL
47         );
48
49         private $_connection                   = NULL;
50         private $_deletes                      = array();
51         private $_puts                         = array();
52         private $_updates                      = array();
53         private $uid                           = 0;
54
55         const DATABASE_CONNECTION_FAIL         = 'DATABASE_CONNECTION_FAIL';
56         const DATABASE_CONNECTION_ERROR        = 'DATABASE_CONNECTION_ERROR';
57         const DATABASE_CONNECTION_BAD_RESOURCE = 'DATABASE_CONNECTION_BAD_RESOURCE';
58         const DATABASE_SELECT_ERROR            = 'DATABASE_SELECT_ERROR';
59         const DATABASE_TABLE_ACCESS_FAIL       = 'DATABASE_TABLE_ACCESS_FAIL';
60         const DATABASE_WRONG_VERSION           = 'DATABASE_WRONG_VERSION';
61
62         /**
63          * Constructs the database layer.
64          *
65          * @access public
66          * @param string $config
67          */
68
69         function __construct($config, $degenerator, $today)
70         {
71
72                 # Pass some variables of the main b8 config to this class
73                 $this->b8_config['degenerator'] = $degenerator;
74                 $this->b8_config['today']       = $today;
75
76                 # Validate the config items
77
78                 if(count($config) > 0) {
79
80                         foreach ($config as $name => $value) {
81
82                                 switch($name) {
83
84                                         case 'table_name':
85                                         case 'host':
86                                         case 'user':
87                                         case 'pass':
88                                         case 'database':
89                                                 $this->config[$name] = (string) $value;
90                                                 break;
91
92                                         case 'connection':
93
94                                                 if($value !== NULL) {
95
96                                                         if(is_resource($value) === TRUE) {
97                                                                 $resource_type = get_resource_type($value);
98                                                                 $this->config['connection'] = $resource_type !== 'mysql link' && $resource_type !== 'mysql link persistent' ? FALSE : $value;
99                                                         }
100
101                                                         else
102                                                                 $this->config['connection'] = FALSE;
103
104                                                 }
105
106                                                 break;
107
108                                 }
109
110                         }
111
112                 }
113
114         }
115
116         /**
117          * Closes the database connection.
118          *
119          * @access public
120          * @return void
121          */
122
123         function __destruct()
124         {
125
126                 if($this->_connection === NULL)
127                         return;
128
129                 # Commit any changes before closing
130                 $this->_commit();
131
132                 # Just close the connection if no link-resource was passed and b8 created it's own connection
133                 if($this->config['connection'] === NULL)
134                         mysql_close($this->_connection);
135
136                 $this->connected = FALSE;
137
138         }
139
140         /**
141          * Connect to the database and do some checks.
142          *
143          * @access public
144          * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8.
145          */
146
147         public function connect()
148         {
149
150                 return TRUE;
151
152                 # Are we already connected?
153                 if($this->connected === TRUE)
154                         return TRUE;
155
156                 # Are we using an existing passed resource?
157                 if($this->config['connection'] === FALSE) {
158                         # ... yes we are, but the connection is not a resource, so return an error
159                         $this->connected = FALSE;
160                         return self::DATABASE_CONNECTION_BAD_RESOURCE;
161                 }
162
163                 elseif($this->config['connection'] === NULL) {
164
165                         # ... no we aren't so we have to connect.
166
167                         if($this->_connection = mysql_connect($this->config['host'], $this->config['user'], $this->config['pass'])) {
168                                 if(mysql_select_db($this->config['database'], $this->_connection) === FALSE) {
169                                         $this->connected = FALSE;
170                                         return self::DATABASE_SELECT_ERROR . ": " . mysql_error();
171                                 }
172                         }
173                         else {
174                                 $this->connected = FALSE;
175                                 return self::DATABASE_CONNECTION_ERROR;
176                         }
177
178                 }
179
180                 else {
181                         # ... yes we are
182                         $this->_connection = $this->config['connection'];
183                 }
184
185                 # Just in case ...
186                 if($this->_connection === NULL) {
187                         $this->connected = FALSE;
188                         return self::DATABASE_CONNECTION_FAIL;
189                 }
190
191                 # Check to see if the wordlist table exists
192                 if(mysql_query('DESCRIBE ' . $this->config['table_name'], $this->_connection) === FALSE) {
193                         $this->connected = FALSE;
194                         return self::DATABASE_TABLE_ACCESS_FAIL . ": " . mysql_error();
195                 }
196
197                 # Everything is okay and connected
198                 $this->connected = TRUE;
199
200                 # Let's see if this is a b8 database and the version is okay
201                 return $this->check_database();
202
203         }
204
205         /**
206          * Does the actual interaction with the database when fetching data.
207          *
208          * @access protected
209          * @param array $tokens
210          * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
211          */
212
213         protected function _get_query($tokens, $uid)
214         {
215
216                 # Construct the query ...
217
218                 if(count($tokens) > 0) {
219
220                         $where = array();
221
222                         foreach ($tokens as $token) {
223                                 $token = dbesc($token);
224                                 array_push($where, $token);
225                         }
226
227                         $where = 'token IN ("' . implode('", "', $where) . '")';
228                 }
229
230                 else {
231                         $token = dbesc($token);
232                         $where = 'token = "' . $token . '"';
233                 }
234
235                 # ... and fetch the data
236
237                 $result = q('
238                         SELECT token, count
239                         FROM ' . $this->config['table_name'] . '
240                         WHERE ' . $where . ' AND uid = ' . $uid );
241
242                 return $result;
243
244         }
245
246         /**
247          * Store a token to the database.
248          *
249          * @access protected
250          * @param string $token
251          * @param string $count
252          * @return void
253          */
254
255         protected function _put($token, $count, $uid) {
256                 $token = dbesc($token);
257                 $count = dbesc($count);
258                 $uid = dbesc($uid);
259                 array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
260         }
261
262         /**
263          * Update an existing token.
264          *
265          * @access protected
266          * @param string $token
267          * @param string $count
268          * @return void
269          */
270
271         protected function _update($token, $count, $uid)
272         {
273                 $token = dbesc($token);
274                 $count = dbesc($count);
275                 $uid = dbesc($uid);
276                 array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
277         }
278
279         /**
280          * Remove a token from the database.
281          *
282          * @access protected
283          * @param string $token
284          * @return void
285          */
286
287         protected function _del($token, $uid)
288         {
289                 $token = dbesc($token);
290                 $uid = dbesc($uid);
291                 $this->uid = $uid;
292                 array_push($this->_deletes, $token);
293         }
294
295         /**
296          * Commits any modification queries.
297          *
298          * @access protected
299          * @return void
300          */
301
302         protected function _commit($uid)
303         {
304
305                 if(count($this->_deletes) > 0) {
306
307                         $result = q('
308                                 DELETE FROM ' . $this->config['table_name'] . '
309                                 WHERE token IN ("' . implode('", "', $this->_deletes) . '") AND uid = ' . $this->uid);
310
311                         $this->_deletes = array();
312
313                 }
314
315                 if(count($this->_puts) > 0) {
316
317                         $result = q('
318                                 INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
319                                 VALUES ' . implode(', ', $this->_puts));
320
321                         $this->_puts = array();
322
323                 }
324
325                 if(count($this->_updates) > 0) {
326
327                         // this still needs work
328                         $result = q("select * from " . $this->config['table_name'] . ' where token = ');
329
330                         
331                         $result = q('
332                                 INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
333                                 VALUES ' . implode(', ', $this->_updates) . ', ' . $uid . '
334                                 ON DUPLICATE KEY UPDATE ' . $this->config['table_name'] . '.count = VALUES(count);', $this->_connection);
335
336                         $this->_updates = array();
337
338                 }
339
340         }
341
342 }
343
344 ?>