2012-02-02 03:23:05 +00:00
|
|
|
<?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
|
|
|
|
*/
|
|
|
|
|
2012-02-02 04:37:05 +00:00
|
|
|
class b8_storage_frndc extends b8_storage_base
|
2012-02-02 03:23:05 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
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();
|
2012-02-02 04:37:05 +00:00
|
|
|
private $uid = 0;
|
2012-02-02 03:23:05 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
|
2016-12-23 19:09:10 +00:00
|
|
|
if ($this->_connection === NULL) {
|
2012-02-02 03:23:05 +00:00
|
|
|
return;
|
2016-12-23 19:09:10 +00:00
|
|
|
}
|
2012-02-02 03:23:05 +00:00
|
|
|
|
2016-12-23 19:09:10 +00:00
|
|
|
// Commit any changes before closing
|
2012-02-02 03:23:05 +00:00
|
|
|
$this->_commit();
|
|
|
|
|
2016-12-23 19:09:10 +00:00
|
|
|
// Just close the connection if no link-resource was passed and b8 created it's own connection
|
|
|
|
if ($this->config['connection'] === NULL) {
|
2012-02-02 03:23:05 +00:00
|
|
|
mysql_close($this->_connection);
|
2016-12-23 19:09:10 +00:00
|
|
|
}
|
2012-02-02 03:23:05 +00:00
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->connected = TRUE;
|
2012-02-28 07:27:16 +00:00
|
|
|
return TRUE;
|
2012-02-02 03:23:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-02-02 04:37:05 +00:00
|
|
|
protected function _get_query($tokens, $uid)
|
2012-02-02 03:23:05 +00:00
|
|
|
{
|
|
|
|
|
2016-12-23 19:09:10 +00:00
|
|
|
// Construct the query ...
|
|
|
|
if (count($tokens) > 0) {
|
2012-02-02 03:23:05 +00:00
|
|
|
|
|
|
|
$where = array();
|
|
|
|
|
|
|
|
foreach ($tokens as $token) {
|
2012-02-02 04:37:05 +00:00
|
|
|
$token = dbesc($token);
|
2012-02-02 03:23:05 +00:00
|
|
|
array_push($where, $token);
|
|
|
|
}
|
|
|
|
|
2012-03-06 07:20:42 +00:00
|
|
|
$where = 'term IN ("' . implode('", "', $where) . '")';
|
2016-12-23 19:09:10 +00:00
|
|
|
} else {
|
2012-02-02 04:37:05 +00:00
|
|
|
$token = dbesc($token);
|
2012-03-06 07:20:42 +00:00
|
|
|
$where = 'term = "' . $token . '"';
|
2012-02-02 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 19:09:10 +00:00
|
|
|
// ... and fetch the data
|
2012-02-02 03:23:05 +00:00
|
|
|
|
2016-12-23 19:09:10 +00:00
|
|
|
$result = q('SELECT * FROM `spam` WHERE ' . $where . ' AND `uid` = ' . $uid );
|
2012-03-06 07:20:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
$returned_tokens = array();
|
2016-12-23 19:09:10 +00:00
|
|
|
if (dbm::is_result($result)) {
|
|
|
|
foreach ($result as $rr) {
|
2012-03-06 07:20:42 +00:00
|
|
|
$returned_tokens[] = $rr['term'];
|
2016-12-23 19:09:10 +00:00
|
|
|
}
|
2012-03-06 07:20:42 +00:00
|
|
|
}
|
|
|
|
$to_create = array();
|
|
|
|
|
2016-12-23 19:09:10 +00:00
|
|
|
if (count($tokens) > 0) {
|
2012-03-06 07:20:42 +00:00
|
|
|
foreach($tokens as $token)
|
|
|
|
if(! in_array($token,$returned_tokens))
|
|
|
|
$to_create[] = str_tolower($token);
|
|
|
|
}
|
2016-12-23 19:09:10 +00:00
|
|
|
if (count($to_create)) {
|
2012-03-06 07:20:42 +00:00
|
|
|
$sql = '';
|
2016-12-23 19:09:10 +00:00
|
|
|
foreach ($to_create as $term) {
|
|
|
|
if (strlen($sql)) {
|
2012-03-06 07:20:42 +00:00
|
|
|
$sql .= ',';
|
2016-12-23 19:09:10 +00:00
|
|
|
}
|
|
|
|
$sql .= sprintf("(`term`,`datetime`,`uid`) VALUES('%s','%s',%d)",
|
|
|
|
dbesc(str_tolower($term)),
|
2012-03-06 07:20:42 +00:00
|
|
|
dbesc(datetime_convert()),
|
|
|
|
intval($uid)
|
|
|
|
);
|
2016-12-23 19:09:10 +00:00
|
|
|
}
|
|
|
|
q("INSERT INTO `spam` " . $sql);
|
2012-03-06 07:20:42 +00:00
|
|
|
}
|
2012-02-02 03:23:05 +00:00
|
|
|
|
2012-02-02 04:37:05 +00:00
|
|
|
return $result;
|
2012-02-02 03:23:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a token to the database.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @param string $token
|
|
|
|
* @param string $count
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-02 04:37:05 +00:00
|
|
|
protected function _put($token, $count, $uid) {
|
|
|
|
$token = dbesc($token);
|
|
|
|
$count = dbesc($count);
|
|
|
|
$uid = dbesc($uid);
|
2012-02-27 13:12:44 +00:00
|
|
|
array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
|
2012-02-02 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing token.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @param string $token
|
|
|
|
* @param string $count
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-02 04:37:05 +00:00
|
|
|
protected function _update($token, $count, $uid)
|
2012-02-02 03:23:05 +00:00
|
|
|
{
|
2012-02-02 04:37:05 +00:00
|
|
|
$token = dbesc($token);
|
|
|
|
$count = dbesc($count);
|
|
|
|
$uid = dbesc($uid);
|
2012-02-27 13:12:44 +00:00
|
|
|
array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
|
2012-02-02 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a token from the database.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @param string $token
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-02 04:37:05 +00:00
|
|
|
protected function _del($token, $uid)
|
2012-02-02 03:23:05 +00:00
|
|
|
{
|
2012-02-02 04:37:05 +00:00
|
|
|
$token = dbesc($token);
|
|
|
|
$uid = dbesc($uid);
|
|
|
|
$this->uid = $uid;
|
2012-02-02 03:23:05 +00:00
|
|
|
array_push($this->_deletes, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits any modification queries.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-02 04:37:05 +00:00
|
|
|
protected function _commit($uid)
|
2012-02-02 03:23:05 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if(count($this->_deletes) > 0) {
|
|
|
|
|
2012-02-02 04:37:05 +00:00
|
|
|
$result = q('
|
2012-02-02 03:23:05 +00:00
|
|
|
DELETE FROM ' . $this->config['table_name'] . '
|
2012-02-02 04:37:05 +00:00
|
|
|
WHERE token IN ("' . implode('", "', $this->_deletes) . '") AND uid = ' . $this->uid);
|
2012-02-02 03:23:05 +00:00
|
|
|
|
|
|
|
$this->_deletes = array();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($this->_puts) > 0) {
|
|
|
|
|
2012-02-02 04:37:05 +00:00
|
|
|
$result = q('
|
|
|
|
INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
|
|
|
|
VALUES ' . implode(', ', $this->_puts));
|
2012-02-02 03:23:05 +00:00
|
|
|
|
|
|
|
$this->_puts = array();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($this->_updates) > 0) {
|
|
|
|
|
2012-02-02 04:37:05 +00:00
|
|
|
// this still needs work
|
2012-02-27 13:12:44 +00:00
|
|
|
$result = q("select * from " . $this->config['table_name'] . ' where token = ');
|
2012-02-02 03:23:05 +00:00
|
|
|
|
2012-02-02 04:37:05 +00:00
|
|
|
|
|
|
|
$result = q('
|
|
|
|
INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
|
|
|
|
VALUES ' . implode(', ', $this->_updates) . ', ' . $uid . '
|
|
|
|
ON DUPLICATE KEY UPDATE ' . $this->config['table_name'] . '.count = VALUES(count);', $this->_connection);
|
2012-02-02 03:23:05 +00:00
|
|
|
|
|
|
|
$this->_updates = array();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|