]> git.mxchange.org Git - friendica.git/blob - library/spam/b8/storage/storage_frndc.php
Merge remote-tracking branch 'upstream/develop' into develop
[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
130                 // Commit any changes before closing
131                 $this->_commit();
132
133                 // Just close the connection if no link-resource was passed and b8 created it's own connection
134                 if ($this->config['connection'] === NULL) {
135                         mysql_close($this->_connection);
136                 }
137
138                 $this->connected = FALSE;
139
140         }
141
142         /**
143          * Connect to the database and do some checks.
144          *
145          * @access public
146          * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8.
147          */
148
149         public function connect()
150         {
151
152                 $this->connected = TRUE;
153                 return TRUE;
154
155         }
156
157         /**
158          * Does the actual interaction with the database when fetching data.
159          *
160          * @access protected
161          * @param array $tokens
162          * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
163          */
164
165         protected function _get_query($tokens, $uid)
166         {
167
168                 // Construct the query ...
169                 if (count($tokens) > 0) {
170
171                         $where = array();
172
173                         foreach ($tokens as $token) {
174                                 $token = dbesc($token);
175                                 array_push($where, $token);
176                         }
177
178                         $where = 'term IN ("' . implode('", "', $where) . '")';
179                 } else {
180                         $token = dbesc($token);
181                         $where = 'term = "' . $token . '"';
182                 }
183
184                 // ... and fetch the data
185
186                 $result = q('SELECT * FROM `spam` WHERE ' . $where . ' AND `uid` = ' . $uid );
187
188
189                 $returned_tokens = array();
190                 if (dbm::is_result($result)) {
191                         foreach ($result as $rr) {
192                                 $returned_tokens[] = $rr['term'];
193                         }
194                 }
195                 $to_create = array();
196
197                 if (count($tokens) > 0) {
198                         foreach($tokens as $token)
199                                 if(! in_array($token,$returned_tokens))
200                                         $to_create[] = str_tolower($token); 
201                 }
202                 if (count($to_create)) {
203                         $sql = '';
204                         foreach ($to_create as $term) {
205                                 if (strlen($sql)) {
206                                         $sql .= ',';
207                                 }
208                                 $sql .= sprintf("(`term`,`datetime`,`uid`) VALUES('%s','%s',%d)",
209                                         dbesc(str_tolower($term)),
210                                         dbesc(datetime_convert()),
211                                         intval($uid)
212                                 );
213                         }
214                         q("INSERT INTO `spam` " . $sql);
215                 }
216
217                 return $result;
218
219         }
220
221         /**
222          * Store a token to the database.
223          *
224          * @access protected
225          * @param string $token
226          * @param string $count
227          * @return void
228          */
229         protected function _put($token, $count, $uid) {
230                 $token = dbesc($token);
231                 $count = dbesc($count);
232                 $uid = dbesc($uid);
233                 array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
234         }
235
236         /**
237          * Update an existing token.
238          *
239          * @access protected
240          * @param string $token
241          * @param string $count
242          * @return void
243          */
244         protected function _update($token, $count, $uid)
245         {
246                 $token = dbesc($token);
247                 $count = dbesc($count);
248                 $uid = dbesc($uid);
249                 array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
250         }
251
252         /**
253          * Remove a token from the database.
254          *
255          * @access protected
256          * @param string $token
257          * @return void
258          */
259         protected function _del($token, $uid)
260         {
261                 $token = dbesc($token);
262                 $uid = dbesc($uid);
263                 $this->uid = $uid;
264                 array_push($this->_deletes, $token);
265         }
266
267         /**
268          * Commits any modification queries.
269          *
270          * @access protected
271          * @return void
272          */
273         protected function _commit($uid)
274         {
275
276                 if(count($this->_deletes) > 0) {
277
278                         $result = q('
279                                 DELETE FROM ' . $this->config['table_name'] . '
280                                 WHERE token IN ("' . implode('", "', $this->_deletes) . '") AND uid = ' . $this->uid);
281
282                         $this->_deletes = array();
283
284                 }
285
286                 if(count($this->_puts) > 0) {
287
288                         $result = q('
289                                 INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
290                                 VALUES ' . implode(', ', $this->_puts));
291
292                         $this->_puts = array();
293
294                 }
295
296                 if(count($this->_updates) > 0) {
297
298                         // this still needs work
299                         $result = q("select * from " . $this->config['table_name'] . ' where token = ');
300
301                         
302                         $result = q('
303                                 INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
304                                 VALUES ' . implode(', ', $this->_updates) . ', ' . $uid . '
305                                 ON DUPLICATE KEY UPDATE ' . $this->config['table_name'] . '.count = VALUES(count);', $this->_connection);
306
307                         $this->_updates = array();
308
309                 }
310
311         }
312
313 }