Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

314 lines
7.1 KiB

  1. <?php
  2. # Copyright (C) 2006-2011 Tobias Leupold <tobias.leupold@web.de>
  3. #
  4. # This file is part of the b8 package
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU Lesser General Public License as published by
  8. # the Free Software Foundation in version 2.1 of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. # License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18. /**
  19. * The MySQL abstraction layer for communicating with the database.
  20. * Copyright (C) 2009 Oliver Lillie (aka buggedcom)
  21. * Copyright (C) 2010-2011 Tobias Leupold <tobias.leupold@web.de>
  22. *
  23. * @license LGPL
  24. * @access public
  25. * @package b8
  26. * @author Oliver Lillie (aka buggedcom) (original PHP 5 port and optimizations)
  27. * @author Tobias Leupold
  28. */
  29. class b8_storage_frndc extends b8_storage_base
  30. {
  31. public $config = array(
  32. 'database' => 'b8_wordlist',
  33. 'table_name' => 'b8_wordlist',
  34. 'host' => 'localhost',
  35. 'user' => FALSE,
  36. 'pass' => FALSE,
  37. 'connection' => NULL
  38. );
  39. public $b8_config = array(
  40. 'degenerator' => NULL,
  41. 'today' => NULL
  42. );
  43. private $_connection = NULL;
  44. private $_deletes = array();
  45. private $_puts = array();
  46. private $_updates = array();
  47. private $uid = 0;
  48. const DATABASE_CONNECTION_FAIL = 'DATABASE_CONNECTION_FAIL';
  49. const DATABASE_CONNECTION_ERROR = 'DATABASE_CONNECTION_ERROR';
  50. const DATABASE_CONNECTION_BAD_RESOURCE = 'DATABASE_CONNECTION_BAD_RESOURCE';
  51. const DATABASE_SELECT_ERROR = 'DATABASE_SELECT_ERROR';
  52. const DATABASE_TABLE_ACCESS_FAIL = 'DATABASE_TABLE_ACCESS_FAIL';
  53. const DATABASE_WRONG_VERSION = 'DATABASE_WRONG_VERSION';
  54. /**
  55. * Constructs the database layer.
  56. *
  57. * @access public
  58. * @param string $config
  59. */
  60. function __construct($config, $degenerator, $today)
  61. {
  62. # Pass some variables of the main b8 config to this class
  63. $this->b8_config['degenerator'] = $degenerator;
  64. $this->b8_config['today'] = $today;
  65. # Validate the config items
  66. if(count($config) > 0) {
  67. foreach ($config as $name => $value) {
  68. switch($name) {
  69. case 'table_name':
  70. case 'host':
  71. case 'user':
  72. case 'pass':
  73. case 'database':
  74. $this->config[$name] = (string) $value;
  75. break;
  76. case 'connection':
  77. if($value !== NULL) {
  78. if(is_resource($value) === TRUE) {
  79. $resource_type = get_resource_type($value);
  80. $this->config['connection'] = $resource_type !== 'mysql link' && $resource_type !== 'mysql link persistent' ? FALSE : $value;
  81. }
  82. else
  83. $this->config['connection'] = FALSE;
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * Closes the database connection.
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. function __destruct()
  97. {
  98. if ($this->_connection === NULL) {
  99. return;
  100. }
  101. // Commit any changes before closing
  102. $this->_commit();
  103. // Just close the connection if no link-resource was passed and b8 created it's own connection
  104. if ($this->config['connection'] === NULL) {
  105. mysql_close($this->_connection);
  106. }
  107. $this->connected = FALSE;
  108. }
  109. /**
  110. * Connect to the database and do some checks.
  111. *
  112. * @access public
  113. * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8.
  114. */
  115. public function connect()
  116. {
  117. $this->connected = TRUE;
  118. return TRUE;
  119. }
  120. /**
  121. * Does the actual interaction with the database when fetching data.
  122. *
  123. * @access protected
  124. * @param array $tokens
  125. * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
  126. */
  127. protected function _get_query($tokens, $uid)
  128. {
  129. // Construct the query ...
  130. if (count($tokens) > 0) {
  131. $where = array();
  132. foreach ($tokens as $token) {
  133. $token = dbesc($token);
  134. array_push($where, $token);
  135. }
  136. $where = 'term IN ("' . implode('", "', $where) . '")';
  137. } else {
  138. $token = dbesc($token);
  139. $where = 'term = "' . $token . '"';
  140. }
  141. // ... and fetch the data
  142. $result = q('SELECT * FROM `spam` WHERE ' . $where . ' AND `uid` = ' . $uid );
  143. $returned_tokens = array();
  144. if (dbm::is_result($result)) {
  145. foreach ($result as $rr) {
  146. $returned_tokens[] = $rr['term'];
  147. }
  148. }
  149. $to_create = array();
  150. if (count($tokens) > 0) {
  151. foreach($tokens as $token)
  152. if(! in_array($token,$returned_tokens))
  153. $to_create[] = str_tolower($token);
  154. }
  155. if (count($to_create)) {
  156. $sql = '';
  157. foreach ($to_create as $term) {
  158. if (strlen($sql)) {
  159. $sql .= ',';
  160. }
  161. $sql .= sprintf("(`term`,`datetime`,`uid`) VALUES('%s','%s',%d)",
  162. dbesc(str_tolower($term)),
  163. dbesc(datetime_convert()),
  164. intval($uid)
  165. );
  166. }
  167. q("INSERT INTO `spam` " . $sql);
  168. }
  169. return $result;
  170. }
  171. /**
  172. * Store a token to the database.
  173. *
  174. * @access protected
  175. * @param string $token
  176. * @param string $count
  177. * @return void
  178. */
  179. protected function _put($token, $count, $uid) {
  180. $token = dbesc($token);
  181. $count = dbesc($count);
  182. $uid = dbesc($uid);
  183. array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
  184. }
  185. /**
  186. * Update an existing token.
  187. *
  188. * @access protected
  189. * @param string $token
  190. * @param string $count
  191. * @return void
  192. */
  193. protected function _update($token, $count, $uid)
  194. {
  195. $token = dbesc($token);
  196. $count = dbesc($count);
  197. $uid = dbesc($uid);
  198. array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
  199. }
  200. /**
  201. * Remove a token from the database.
  202. *
  203. * @access protected
  204. * @param string $token
  205. * @return void
  206. */
  207. protected function _del($token, $uid)
  208. {
  209. $token = dbesc($token);
  210. $uid = dbesc($uid);
  211. $this->uid = $uid;
  212. array_push($this->_deletes, $token);
  213. }
  214. /**
  215. * Commits any modification queries.
  216. *
  217. * @access protected
  218. * @return void
  219. */
  220. protected function _commit($uid)
  221. {
  222. if(count($this->_deletes) > 0) {
  223. $result = q('
  224. DELETE FROM ' . $this->config['table_name'] . '
  225. WHERE token IN ("' . implode('", "', $this->_deletes) . '") AND uid = ' . $this->uid);
  226. $this->_deletes = array();
  227. }
  228. if(count($this->_puts) > 0) {
  229. $result = q('
  230. INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
  231. VALUES ' . implode(', ', $this->_puts));
  232. $this->_puts = array();
  233. }
  234. if(count($this->_updates) > 0) {
  235. // this still needs work
  236. $result = q("select * from " . $this->config['table_name'] . ' where token = ');
  237. $result = q('
  238. INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
  239. VALUES ' . implode(', ', $this->_updates) . ', ' . $uid . '
  240. ON DUPLICATE KEY UPDATE ' . $this->config['table_name'] . '.count = VALUES(count);', $this->_connection);
  241. $this->_updates = array();
  242. }
  243. }
  244. }