PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/b8/storage/storage_sqlite.php

https://github.com/felixding/LonelyThinker
PHP | 282 lines | 163 code | 72 blank | 47 comment | 23 complexity | 04dc1a97ed3575f896d12293e8b68c4f 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 SQLite table
  22. class storage_sqlite extends b8SharedFunctions
  23. {
  24. # This contains the connection ID
  25. var $db;
  26. # Constructor
  27. # Looks if the SQLite binding is working and trys to create a new table if requested
  28. function storage_sqlite()
  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" => "sqliteFile", "type" => "path", "default" => "wordlist.db");
  36. $config[] = array("name" => "tableName", "type" => "string", "default" => "b8_wordlist");
  37. # Get the configuration
  38. $configFile = "config_storage";
  39. if(!$this->loadConfig($configFile, $config)) {
  40. $this->echoError("Failed initializing the configuration.");
  41. $this->constructed = FALSE;
  42. }
  43. if($this->constructed) {
  44. # Check if we want to create a new database
  45. if($this->config['createDB']) {
  46. # Check if the file already exists
  47. if($this->checkFile($this->config['sqliteFile'], "e", FALSE)) {
  48. $this->echoError("<kbd>" . $this->config['sqliteFile'] . "</kbd> already exists. Please remove <kbd>createDB = TRUE</kbd> from <kbd>$configFile</kbd> to use this database or delete it to re-create it with no content.");
  49. $this->constructed = FALSE;
  50. }
  51. # Check if we have write permissions on the directory
  52. # where the database should be created
  53. $targetDir = dirname($this->config['sqliteFile']);
  54. if(!$this->checkFile($targetDir, "dw", TRUE)) {
  55. $this->echoError("A new database can't be created here. Please fix the permissions of this directory or choose another one.");
  56. $this->constructed = FALSE;
  57. }
  58. }
  59. else {
  60. # Check if the requested database exists
  61. if(!$this->checkFile($this->config['sqliteFile'], "efrw", TRUE)) {
  62. if($this->checkFile($this->config['sqliteFile'], "d", FALSE)) {
  63. $this->echoError("A directory can't be used as a database. Please fix your config.");
  64. }
  65. elseif(!$this->checkFile($this->config['sqliteFile'], "f", FALSE)) {
  66. $this->echoError("Please add <kbd>createDB = TRUE</kbd> to <kbd>$configFile</kbd> if you want to create a new database with this path.");
  67. }
  68. elseif(!$this->checkFile($this->config['sqliteFile'], "rw", FALSE)) {
  69. $this->echoError("The database has the wrong permissions. It has to be readable and writable. Please fix this file's permissions.");
  70. }
  71. $this->constructed = FALSE;
  72. }
  73. }
  74. }
  75. if($this->constructed) {
  76. # Get the SQLite link resource to use
  77. $arg = FALSE;
  78. if(func_num_args() > 0)
  79. $arg = func_get_arg(0);
  80. if($arg != FALSE) {
  81. # A resource was passed, so use this one ...
  82. $this->db = $arg;
  83. # ... and check if it's really a SQLite-link resource
  84. $argType = gettype($this->db);
  85. if(!is_resource($this->db)) {
  86. $this->echoError("The argument passed to b8 is not a resource (passed variable: \"$argType\"). Please be sure to pass a SQLite-link resource to b8 or pass nothing and make sure that all of the following values are set in <kbd>$configFile</kbd>: <i><kbd>sqliteFile</kbd></i> so that a separate SQLite connection can be set up by b8.");
  87. $this->constructed = FALSE;
  88. }
  89. $resType = get_resource_type($this->db);
  90. if($resType != "sqlite link" and $this->constructed) {
  91. $this->echoError("The passed resource is not a SQLite-link resource (passed resource: \"$resType\"). Please be sure to pass a SQLite-link resource to b8 or pass nothing and make sure that all of the following values are set in <kbd>$configFile</kbd>: <i><kbd>sqliteFile</kbd></i> so that a separate SQLite connection can be set up by b8.");
  92. $this->constructed = FALSE;
  93. }
  94. }
  95. else {
  96. # No resource was passed, so we want to set up our own connection
  97. # Set up the SQLite connection
  98. $this->db = sqlite_open($this->config['sqliteFile']);
  99. # Check if it's okay
  100. if($this->db == FALSE) {
  101. $this->echoError("Could not connect to SQLite.");
  102. $this->constructed = FALSE;
  103. }
  104. }
  105. }
  106. if($this->constructed) {
  107. # Here, we should have a working SQLite connection, so ...
  108. # Check if we want to create a new database
  109. if($this->config['createDB']) {
  110. # Check if the wordlist table already exists
  111. if(sqlite_single_query("SELECT type FROM SQLITE_MASTER WHERE name = '" . $this->config['tableName'] . "'", $this->db)) {
  112. $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.");
  113. $this->constructed = FALSE;
  114. }
  115. else {
  116. # If not, create it
  117. if(sqlite_query(
  118. "CREATE TABLE " . $this->config['tableName'] . " (
  119. token TEXT NOT NULL PRIMARY KEY,
  120. count TEXT NOT NULL
  121. )", $this->db)) {
  122. $this->echoError("Successfully created the table <kbd>" . $this->config['tableName'] . "</kbd>.");
  123. # Try to put in the "version 2" tag
  124. if($this->put("bayes*dbversion", "2") == FALSE) {
  125. $this->echoError("Error accessing the new table.");
  126. $this->constructed = FALSE;
  127. }
  128. else {
  129. # Everything worked smoothly
  130. # Anyway -- don't let the user use b8 (although it would work now!)
  131. # before the "create database" flag isn't removed from the config file
  132. $this->echoError("Successfully created the new database. Please remove <kbd>createDB = TRUE</kbd> from <kbd>$configFile</kbd> to use b8.");
  133. $this->constructed = FALSE;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. if($this->constructed) {
  140. # Check if the table is accessible
  141. if(!sqlite_query("SELECT type FROM SQLITE_MASTER WHERE name = '" . $this->config['tableName'] . "'", $this->db)) {
  142. $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.");
  143. $this->constructed = FALSE;
  144. }
  145. }
  146. # If the above query worked, we now shoule be able to use b8.
  147. }
  148. # Get a token from the database
  149. function get($token)
  150. {
  151. $res = sqlite_fetch_all(sqlite_query("
  152. SELECT count
  153. FROM " . $this->config['tableName'] . "
  154. WHERE token='" . sqlite_escape_string($token) . "'
  155. ", $this->db), SQLITE_ASSOC);
  156. if($res)
  157. return $res[0]['count'];
  158. else
  159. return FALSE;
  160. }
  161. # Store a token to the database
  162. function put($token, $count)
  163. {
  164. $res = @sqlite_query("
  165. INSERT INTO " . $this->config['tableName'] . " (
  166. token,
  167. count
  168. )
  169. VALUES(
  170. '" . sqlite_escape_string($token) . "',
  171. '$count'
  172. )
  173. ", $this->db);
  174. return $res;
  175. }
  176. # Update an existing token
  177. function update($token, $count)
  178. {
  179. return sqlite_query(
  180. "UPDATE " . $this->config['tableName'] . "
  181. SET count='$count'
  182. WHERE token='" . sqlite_escape_string($token) . "'
  183. ", $this->db);
  184. }
  185. # Remove a token from the database
  186. function del($token)
  187. {
  188. return sqlite_query("
  189. DELETE FROM " . $this->config['tableName'] . "
  190. WHERE token='" . sqlite_escape_string($token) . "'
  191. ", $this);
  192. }
  193. }
  194. ?>