PageRenderTime 70ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/database_mysql.php

https://github.com/tigerdan/TinyIB
PHP | 220 lines | 193 code | 23 blank | 4 comment | 36 complexity | 2336282d564f564289db5ba999d538fb MD5 | raw file
  1. <?php
  2. if (!defined('TINYIB_BOARD')) { die(''); }
  3. $link = mysql_connect(TINYIB_DBHOST, TINYIB_DBUSERNAME, TINYIB_DBPASSWORD);
  4. if (!$link) {
  5. fancyDie("Could not connect to database: " . mysql_error());
  6. }
  7. $db_selected = mysql_select_db(TINYIB_DBNAME, $link);
  8. if (!$db_selected) {
  9. fancyDie("Could not select database: " . mysql_error());
  10. }
  11. // Create the posts table if it does not exist
  12. if (mysql_num_rows(mysql_query("SHOW TABLES LIKE '" . TINYIB_DBPOSTS . "'")) == 0) {
  13. mysql_query("CREATE TABLE `" . TINYIB_DBPOSTS . "` (
  14. `id` mediumint(7) unsigned NOT NULL auto_increment,
  15. `parent` mediumint(7) unsigned NOT NULL,
  16. `timestamp` int(20) NOT NULL,
  17. `bumped` int(20) NOT NULL,
  18. `ip` varchar(15) NOT NULL,
  19. `name` varchar(75) NOT NULL,
  20. `tripcode` varchar(10) NOT NULL,
  21. `email` varchar(75) NOT NULL,
  22. `nameblock` varchar(255) NOT NULL,
  23. `subject` varchar(75) NOT NULL,
  24. `message` text NOT NULL,
  25. `password` varchar(255) NOT NULL,
  26. `file` varchar(75) NOT NULL,
  27. `file_hex` varchar(75) NOT NULL,
  28. `file_original` varchar(255) NOT NULL,
  29. `file_size` int(20) unsigned NOT NULL default '0',
  30. `file_size_formatted` varchar(75) NOT NULL,
  31. `image_width` smallint(5) unsigned NOT NULL default '0',
  32. `image_height` smallint(5) unsigned NOT NULL default '0',
  33. `thumb` varchar(255) NOT NULL,
  34. `thumb_width` smallint(5) unsigned NOT NULL default '0',
  35. `thumb_height` smallint(5) unsigned NOT NULL default '0',
  36. PRIMARY KEY (`id`),
  37. KEY `parent` (`parent`),
  38. KEY `bumped` (`bumped`)
  39. ) ENGINE=MyISAM");
  40. }
  41. // Create the bans table if it does not exist
  42. if (mysql_num_rows(mysql_query("SHOW TABLES LIKE '" . TINYIB_DBBANS . "'")) == 0) {
  43. mysql_query("CREATE TABLE `" . TINYIB_DBBANS . "` (
  44. `id` mediumint(7) unsigned NOT NULL auto_increment,
  45. `ip` varchar(15) NOT NULL,
  46. `timestamp` int(20) NOT NULL,
  47. `expire` int(20) NOT NULL,
  48. `reason` text NOT NULL,
  49. PRIMARY KEY (`id`),
  50. KEY `ip` (`ip`)
  51. ) ENGINE=MyISAM");
  52. }
  53. # Post Functions
  54. function uniquePosts() {
  55. $row = mysql_fetch_row(mysql_query("SELECT COUNT(DISTINCT(`ip`)) FROM " . TINYIB_DBPOSTS));
  56. return $row[0];
  57. }
  58. function postByID($id) {
  59. $result = mysql_query("SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `id` = '" . mysql_real_escape_string($id) . "' LIMIT 1");
  60. if ($result) {
  61. while ($post = mysql_fetch_assoc($result)) {
  62. return $post;
  63. }
  64. }
  65. }
  66. function threadExistsByID($id) {
  67. return mysql_result(mysql_query("SELECT COUNT(*) FROM `" . TINYIB_DBPOSTS . "` WHERE `id` = '" . mysql_real_escape_string($id) . "' AND `parent` = 0 LIMIT 1"), 0, 0) > 0;
  68. }
  69. function insertPost($post) {
  70. mysql_query("INSERT INTO `" . TINYIB_DBPOSTS . "` (`parent`, `timestamp`, `bumped`, `ip`, `name`, `tripcode`, `email`, `nameblock`, `subject`, `message`, `password`, `file`, `file_hex`, `file_original`, `file_size`, `file_size_formatted`, `image_width`, `image_height`, `thumb`, `thumb_width`, `thumb_height`) VALUES (" . $post['parent'] . ", " . time() . ", " . time() . ", '" . $_SERVER['REMOTE_ADDR'] . "', '" . mysql_real_escape_string($post['name']) . "', '" . mysql_real_escape_string($post['tripcode']) . "', '" . mysql_real_escape_string($post['email']) . "', '" . mysql_real_escape_string($post['nameblock']) . "', '" . mysql_real_escape_string($post['subject']) . "', '" . mysql_real_escape_string($post['message']) . "', '" . mysql_real_escape_string($post['password']) . "', '" . $post['file'] . "', '" . $post['file_hex'] . "', '" . mysql_real_escape_string($post['file_original']) . "', " . $post['file_size'] . ", '" . $post['file_size_formatted'] . "', " . $post['image_width'] . ", " . $post['image_height'] . ", '" . $post['thumb'] . "', " . $post['thumb_width'] . ", " . $post['thumb_height'] . ")");
  71. return mysql_insert_id();
  72. }
  73. function bumpThreadByID($id) {
  74. mysql_query("UPDATE `" . TINYIB_DBPOSTS . "` SET `bumped` = " . time() . " WHERE `id` = " . $id . " LIMIT 1");
  75. }
  76. function countThreads() {
  77. return mysql_result(mysql_query("SELECT COUNT(*) FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = 0"), 0, 0);
  78. }
  79. function allThreads() {
  80. $threads = array();
  81. $result = mysql_query("SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = 0 ORDER BY `bumped` DESC");
  82. if ($result) {
  83. while ($thread = mysql_fetch_assoc($result)) {
  84. $threads[] = $thread;
  85. }
  86. }
  87. return $threads;
  88. }
  89. function postsInThreadByID($id) {
  90. $posts = array();
  91. $result = mysql_query("SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `id` = " . $id . " OR `parent` = " . $id . " ORDER BY `id` ASC");
  92. if ($result) {
  93. while ($post = mysql_fetch_assoc($result)) {
  94. $posts[] = $post;
  95. }
  96. }
  97. return $posts;
  98. }
  99. function latestRepliesInThreadByID($id) {
  100. $posts = array();
  101. $replies = mysql_query("SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = " . $id . " ORDER BY `id` DESC LIMIT 3");
  102. if ($replies) {
  103. while ($post = mysql_fetch_assoc($replies)) {
  104. $posts[] = $post;
  105. }
  106. }
  107. return $posts;
  108. }
  109. function postsByHex($hex) {
  110. $posts = array();
  111. $result = mysql_query("SELECT `id`, `parent` FROM `" . TINYIB_DBPOSTS . "` WHERE `file_hex` = '" . mysql_real_escape_string($hex) . "' LIMIT 1");
  112. if ($result) {
  113. while ($post = mysql_fetch_assoc($result)) {
  114. $posts[] = $post;
  115. }
  116. }
  117. return $posts;
  118. }
  119. function deletePostByID($id) {
  120. $posts = postsInThreadByID($id);
  121. foreach ($posts as $post) {
  122. if ($post['id'] != $id) {
  123. deletePostImages($post);
  124. mysql_query("DELETE FROM `" . TINYIB_DBPOSTS . "` WHERE `id` = " . $post['id'] . " LIMIT 1");
  125. } else {
  126. $thispost = $post;
  127. }
  128. }
  129. if (isset($thispost)) {
  130. if ($thispost['parent'] == 0) {
  131. @unlink('res/' . $thispost['id'] . '.html');
  132. }
  133. deletePostImages($thispost);
  134. mysql_query("DELETE FROM `" . TINYIB_DBPOSTS . "` WHERE `id` = " . $thispost['id'] . " LIMIT 1");
  135. }
  136. }
  137. function trimThreads() {
  138. if (TINYIB_MAXTHREADS > 0) {
  139. $result = mysql_query("SELECT `id` FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = 0 ORDER BY `bumped` DESC LIMIT " . TINYIB_MAXTHREADS. ", 10");
  140. if ($result) {
  141. while ($post = mysql_fetch_assoc($result)) {
  142. deletePostByID($post['id']);
  143. }
  144. }
  145. }
  146. }
  147. function lastPostByIP() {
  148. $replies = mysql_query("SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `ip` = '" . $_SERVER['REMOTE_ADDR'] . "' ORDER BY `id` DESC LIMIT 1");
  149. if ($replies) {
  150. while ($post = mysql_fetch_assoc($replies)) {
  151. return $post;
  152. }
  153. }
  154. }
  155. # Ban Functions
  156. function banByID($id) {
  157. $result = mysql_query("SELECT * FROM `" . TINYIB_DBBANS . "` WHERE `id` = '" . mysql_real_escape_string($id) . "' LIMIT 1");
  158. if ($result) {
  159. while ($ban = mysql_fetch_assoc($result)) {
  160. return $ban;
  161. }
  162. }
  163. }
  164. function banByIP($ip) {
  165. $result = mysql_query("SELECT * FROM `" . TINYIB_DBBANS . "` WHERE `ip` = '" . mysql_real_escape_string($ip) . "' LIMIT 1");
  166. if ($result) {
  167. while ($ban = mysql_fetch_assoc($result)) {
  168. return $ban;
  169. }
  170. }
  171. }
  172. function allBans() {
  173. $bans = array();
  174. $result = mysql_query("SELECT * FROM `" . TINYIB_DBBANS . "` ORDER BY `timestamp` DESC");
  175. if ($result) {
  176. while ($ban = mysql_fetch_assoc($result)) {
  177. $bans[] = $ban;
  178. }
  179. }
  180. return $bans;
  181. }
  182. function insertBan($ban) {
  183. mysql_query("INSERT INTO `" . TINYIB_DBBANS . "` (`ip`, `timestamp`, `expire`, `reason`) VALUES ('" . mysql_real_escape_string($ban['ip']) . "', " . time() . ", '" . mysql_real_escape_string($ban['expire']) . "', '" . mysql_real_escape_string($ban['reason']) . "')");
  184. return mysql_insert_id();
  185. }
  186. function clearExpiredBans() {
  187. $result = mysql_query("SELECT * FROM `" . TINYIB_DBBANS . "` WHERE `expire` > 0 AND `expire` <= " . time());
  188. if ($result) {
  189. while ($ban = mysql_fetch_assoc($result)) {
  190. mysql_query("DELETE FROM `" . TINYIB_DBBANS . "` WHERE `id` = " . $ban['id'] . " LIMIT 1");
  191. }
  192. }
  193. }
  194. function deleteBanByID($id) {
  195. mysql_query("DELETE FROM `" . TINYIB_DBBANS . "` WHERE `id` = " . mysql_real_escape_string($id) . " LIMIT 1");
  196. }
  197. ?>