PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/b8/storage/storage_mysql.php

https://github.com/felixding/LonelyThinker
PHP | 270 lines | 158 code | 65 blank | 47 comment | 25 complexity | 065255d42a1bf81a5e7c766173b4e969 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. # Copyright (C) 2006-2008 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
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation in version 2
  9. # of the License.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  19. # Get the shared functions class file (if not already loaded)
  20. require_once dirname(__FILE__) . "/../shared_functions.php";
  21. # Use a MySQL table
  22. class storage_mysql extends b8SharedFunctions
  23. {
  24. # This contains the connection ID
  25. var $mysqlRes;
  26. # Constructor
  27. # Looks if the MySQL binding is working and trys to create a new table if requested
  28. function storage_mysql()
  29. {
  30. # Till now, everything's fine
  31. # Yes, I know that this is crap ;-)
  32. $this->constructed = TRUE;
  33. # Default values for the configuration
  34. $config[] = array("name" => "createDB", "type" => "bool", "default" => FALSE);
  35. $config[] = array("name" => "tableName", "type" => "string", "default" => "b8_wordlist");
  36. $config[] = array("name" => "host", "type" => "string", "default" => "localhost");
  37. $config[] = array("name" => "user", "type" => "string", "default" => "");
  38. $config[] = array("name" => "pass", "type" => "string", "default" => "");
  39. $config[] = array("name" => "db", "type" => "string", "default" => "");
  40. # Get the configuration
  41. $configFile = "config_storage";
  42. if(!$this->loadConfig($configFile, $config)) {
  43. $this->echoError("Failed initializing the configuration.");
  44. $this->constructed = FALSE;
  45. }
  46. if($this->constructed) {
  47. # Get the MySQL link resource to use
  48. $arg = FALSE;
  49. if(func_num_args() > 0)
  50. $arg = func_get_arg(0);
  51. if($arg != FALSE) {
  52. if(!is_array($arg)) {
  53. # Only a resource was passed, so use this one (behave like in older versions)
  54. $this->mysqlRes = $arg;
  55. }
  56. else {
  57. # An array was passed, so look what we have
  58. if(isset($arg['mysqlRes'])) {
  59. # We have a 'resource' entry
  60. $this->mysqlRes = $arg['mysqlRes'];
  61. }
  62. if(isset($arg['tableName'])) {
  63. # We have a 'tableName' entry
  64. # Check if it's really a string
  65. if(!is_string($arg['tableName'])) {
  66. $this->echoError("The 'tableName' argument passed to b8 is not a string (passed variable: \"" . gettype($arg['tableName']) . "\"). Please be sure to pass a string containing the MySQL table's name that should be used instead of the tableName variable set in the config file.");
  67. $this->constructed = FALSE;
  68. }
  69. else
  70. $this->config['tableName'] = $arg['tableName'];
  71. }
  72. }
  73. # Check if the resource to use is really a MySQL-link resource
  74. $argType = gettype($this->mysqlRes);
  75. if(!is_resource($this->mysqlRes)) {
  76. $this->echoError("The argument passed to b8 is not a resource (passed variable: \"$argType\"). Please be sure to pass a MySQL-link resource to b8 or pass nothing and make sure that all of the following values are set in <kbd>$configFile</kbd>: <i><kbd>host</kbd></i>, <i><kbd>user</kbd></i>, <i><kbd>pass</kbd></i> and <i><kbd>db</kbd></i> so that a separate MySQL connection can be set up by b8.");
  77. $this->constructed = FALSE;
  78. }
  79. $resType = get_resource_type($this->mysqlRes);
  80. if($resType != "mysql link" and $resType != "mysql link persistent" and $this->constructed) {
  81. $this->echoError("The passed resource is not a MySQL-link resource (passed resource: \"$resType\"). Please be sure to pass a MySQL-link resource to b8 or pass nothing and make sure that all of the following values are set in <kbd>$configFile</kbd>: <i><kbd>host</kbd></i>, <i><kbd>user</kbd></i>, <i><kbd>pass</kbd></i> and <i><kbd>db</kbd></i> so that a separate MySQL connection can be set up by b8.");
  82. $this->constructed = FALSE;
  83. }
  84. }
  85. else {
  86. # No resource was passed, so we want to set up our own connection
  87. # Set up the MySQL connection
  88. $this->mysqlRes = mysql_connect($this->config['host'], $this->config['user'], $this->config['pass']);
  89. //modified by handaoliang.fix utf-8 charset bug.
  90. @mysql_query("SET NAMES 'utf8'");
  91. # Check if it's okay
  92. if($this->mysqlRes == FALSE) {
  93. $this->echoError("Could not connect to MySQL.");
  94. $this->constructed = FALSE;
  95. }
  96. if($this->constructed) {
  97. # Open the database where the wordlist is/will be stored
  98. if(!mysql_select_db($this->config['db'])) {
  99. $this->echoError("Could not select the database \"$db\".");
  100. $this->constructed = FALSE;
  101. }
  102. }
  103. }
  104. }
  105. if($this->constructed) {
  106. # Here, we should have a working MySQL connection, so ...
  107. # Check if we want to create a new database
  108. if($this->config['createDB']) {
  109. # Check if the wordlist table already exists
  110. if(mysql_query("DESCRIBE " . $this->config['tableName'], $this->mysqlRes)) {
  111. $this->echoError("The table <kbd>" . $this->config['tableName'] . "</kbd> already exists in the selected database. Please remove <kbd>createDB = TRUE</kbd> from <kbd>$configFile</kbd> to use this table or drop it to re-create it with no content.");
  112. $this->constructed = FALSE;
  113. }
  114. else {
  115. # If not, create it
  116. if(mysql_query(
  117. "CREATE TABLE " . $this->config['tableName'] . " (
  118. token VARCHAR(255) BINARY PRIMARY KEY,
  119. count VARCHAR(255)
  120. )", $this->mysqlRes)) {
  121. $this->echoError("Successfully created the table <kbd>" . $this->config['tableName'] . "</kbd>.");
  122. # Try to put in the "version 2" tag
  123. if($this->put("bayes*dbversion", "2") == FALSE) {
  124. $this->echoError("Error accessing the new table.");
  125. $this->constructed = FALSE;
  126. }
  127. else {
  128. # Everything worked smoothly
  129. # Anyway -- don't let the user use b8 (although it would work now!)
  130. # before the "create database" flag isn't removed from the config file
  131. $this->echoError("Successfully created the new database. Please remove <kbd>createDB = TRUE</kbd> from <kbd>$configFile</kbd> to use b8.");
  132. $this->constructed = FALSE;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. if($this->constructed) {
  139. # Check if the table is accessible
  140. if(!mysql_query("DESCRIBE " . $this->config['tableName'], $this->mysqlRes)) {
  141. $this->echoError("The table <kbd>" . $this->config['tableName'] . "</kbd> does not exist in the selected database. Please add <kbd>createDB = TRUE</kbd> to <kbd>$configFile</kbd> to create this table of select another one.");
  142. $this->constructed = FALSE;
  143. }
  144. }
  145. # If the above query worked, we now shoule be able to use b8.
  146. }
  147. # Get a token from the database
  148. function get($token)
  149. {
  150. $res = mysql_fetch_assoc(mysql_query("
  151. SELECT count
  152. FROM " . $this->config['tableName'] . "
  153. WHERE token='" . mysql_real_escape_string($token, $this->mysqlRes) . "'
  154. ", $this->mysqlRes));
  155. if($res)
  156. return $res['count'];
  157. else
  158. return FALSE;
  159. }
  160. # Store a token to the database
  161. function put($token, $count)
  162. {
  163. return mysql_query("
  164. INSERT INTO " . $this->config['tableName'] . " (
  165. token,
  166. count
  167. )
  168. VALUES(
  169. '" . mysql_real_escape_string($token, $this->mysqlRes) . "',
  170. '$count'
  171. )
  172. ", $this->mysqlRes);
  173. }
  174. # Update an existing token
  175. function update($token, $count)
  176. {
  177. return mysql_query(
  178. "UPDATE " . $this->config['tableName'] . "
  179. SET count='$count'
  180. WHERE token='" . mysql_real_escape_string($token, $this->mysqlRes) . "'
  181. ", $this->mysqlRes);
  182. }
  183. # Remove a token from the database
  184. function del($token)
  185. {
  186. return mysql_query("
  187. DELETE FROM " . $this->config['tableName'] . "
  188. WHERE token='" . mysql_real_escape_string($token, $this->mysqlRes) . "'
  189. ", $this->mysqlRes);
  190. }
  191. }
  192. ?>