Volkova

PHP Forum V0.3

Jul 18th, 2011
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.14 KB | None | 0 0
  1. <?php
  2. /*
  3. *    +--------------------------------------------------------------+
  4. *    | Changelog                                                    |
  5. *    +--------------------------------------------------------------+
  6. *    |                                                              |
  7. *    | Version: 0.3                                                 |
  8. *    |  0.3 > Simple Search                                         |
  9. *    |  0.3 > Admin Login, delete posts/threads                     |
  10. *    |  0.3 > Can not reply to non existing threads                 |
  11. *    |  0.2 > Some other minor things                               |
  12. *    |  0.2 > Threads with new reply go to the top                  |
  13. *    |  0.2 > No blank posts                                        |
  14. *    |  0.1 > Simple Threads, and posts                             |
  15. *    |                                                              |
  16. *    +--------------------------------------------------------------+
  17. *                                                                    
  18. *    +--------------------------------------------------------------+
  19. *    | Current MySQL User                                           |
  20. *    +--------------------------------------------------------------+
  21.  
  22. CREATE USER 'forum_user'@'localhost' IDENTIFIED BY 'some password';
  23.  
  24. GRANT CREATE ON forum_db.* TO 'forum_user'@'localhost';
  25. GRANT UPDATE ON forum_db.* TO 'forum_user'@'localhost';
  26. GRANT SELECT ON forum_db.* TO 'forum_user'@'localhost';
  27. GRANT INSERT ON forum_db.* TO 'forum_user'@'localhost';
  28. GRANT DELETE ON forum_db.* TO 'forum_user'@'localhost';
  29.  
  30. *    +--------------------------------------------------------------+
  31. */
  32. //INSERT INTO users VALUES (NULL, 'Admin', '5bc01684157d1fdfe1403bf87b44fafdfd4fb5db4df5033ee7bdd910dda5e8d36f7b39be4557542e444381448783948f90a906a780fca0a196aa29710d55c058', 'Admin');
  33. // To get an admin hash a password in sha512 with the default salt, the default admin password ( ^ that one ) is '123456' (no quotes). The default salt is: "[%6#llKZ`dW£v:Qb5%eJbdpUAZ6ta#j&W'nb{{iQ5*S'IGa@:W" and is found in the admin function
  34. session_start();
  35. if ( !$_POST ) include_once('include/header.php');
  36.  
  37. $host = 'localhost';
  38. $username = 'forum_user';
  39. $password = 'password';
  40. $database = 'forum_db';
  41. connect($host, $username, $password, $database);
  42.  
  43. function connect($host, $username, $password, $database) {
  44.     mysql_connect($host,$username,$password) or die("Could not connect. " . mysql_error());
  45.     mysql_select_db($database) or die("Could not select database. " . mysql_error());
  46.     return buildDB(); //Everytime the database is connected to, it will run the buildDB function to check if the tables exist and if not create them
  47. }
  48.  
  49. function buildDB() {
  50.     $SQL = <<<MySQL_QUERY
  51. CREATE TABLE IF NOT EXISTS forum (
  52. post_num INT NOT NULL AUTO_INCREMENT,
  53. title VARCHAR(150),
  54. user VARCHAR(50),
  55. bodytext TEXT,
  56. OP INT,
  57. created VARCHAR(100),
  58. last_post VARCHAR(100),
  59. PRIMARY KEY(post_num)
  60. );
  61. MySQL_QUERY;
  62.     mysql_query($SQL);
  63. $SQL = <<<MySQL_QUERY
  64. CREATE TABLE IF NOT EXISTS users (
  65. userid INT NOT NULL AUTO_INCREMENT,
  66. username VARCHAR(150),
  67. password VARCHAR(150),
  68. usergroup VARCHAR(40),
  69. PRIMARY KEY(userid)
  70. );
  71. MySQL_QUERY;
  72.     mysql_query($SQL);
  73. }
  74.  
  75. function post($title, $user, $body, $OP, $time) {
  76.     $title = mysql_real_escape_string($title);
  77.     $user = mysql_real_escape_string($user);
  78.     $body = mysql_real_escape_string($body);
  79.     if ( strlen($body) > 2500 ) $body = substr($body, 0, 2500);
  80.     $OP ? $OP = mysql_real_escape_string($OP) : $OP = 0;
  81.     //if (mysql_num_rows(mysql_query("SELECT post_num FROM forum WHERE op=$OP")) == 0) $OP = 0;
  82.     $SQL = "INSERT INTO forum VALUES (NULL, '$title', '$user', '$body', $OP,'$time', '$time');";
  83.     mysql_query($SQL);
  84.     $SQL = "UPDATE forum SET last_post='$time' WHERE post_num=$OP";
  85.     mysql_query($SQL);
  86.     header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $OP);
  87. }
  88.  
  89. function get_thread() {
  90.     $amount_page = 6;
  91.     print "<form name=\"search\" method=\"get\"> <input type=\"text\" name=\"search\" placeholder=\"Search\" autocomplete=\"off\"> </form> <span id=\"manage\"><a href=\"?admin=1\"> [Manage] </a></span>";
  92.     if ( !$_GET ) post_box();
  93.     if ( $_GET ) print "<h4> <a href=\"".$_SERVER['PHP_SELF']."\">Return </a> </h4>";
  94.     isset($_GET["page"]) ? $page = $_GET["page"] : $page = 1;
  95.     $start_from = ($page-1) * $amount_page;
  96.     if ( $_GET['id'] ) {
  97.         if (!is_numeric($_GET['id']) || is_float($_GET['id']) || $_GET['id'] <= 0) { exit; } else { $OP = $_GET['id']; }
  98.         $SQL = "SELECT * FROM forum WHERE OP=$OP OR post_num=$OP ORDER BY created ASC LIMIT $start_from, $amount_page";
  99.     } elseif( $_GET['search'] ) {
  100.         $OP = 0;
  101.         $searchstring = mysql_real_escape_string(urldecode($_GET['search']));
  102.         $SQL = "SELECT * FROM forum WHERE title LIKE '%$searchstring%' OR bodytext LIKE '%$searchstring%' ORDER BY last_post DESC LIMIT $start_from, $amount_page";
  103.     } else {
  104.         $OP = 0;
  105.         $SQL = "SELECT * FROM forum WHERE OP=$OP ORDER BY last_post DESC LIMIT $start_from, $amount_page";
  106.     }
  107.     $result=mysql_query($SQL);
  108.     //pages
  109.     $pages = "<div id=\"pages\">";
  110.     $pages_SQL = "SELECT COUNT($OP) from forum WHERE OP=$OP";
  111.     $pages_result = mysql_query($pages_SQL);
  112.     $pages_row = mysql_fetch_row($pages_result);
  113.     $pages_total_records = $pages_row[0];
  114.     $total_pages = ceil($pages_total_records / $amount_page);
  115.     if (isset($_GET["page"])) { $currentpage = $_GET["page"]; } else { $currentpage = 1; };
  116.     $currentpage = mysql_real_escape_string(htmlentities($currentpage));
  117.     $pages .= "<span class=\"pages\">";
  118.     if ($total_pages > 0 && $currentpage > 1) {
  119.             $pages .= "<a href=\"?page=" . (intval($currentpage) - 1) . "\">Prev &laquo; </a>";
  120.         }
  121.         if ($total_pages > 1) {
  122.             for ( $i= 1; $i <= $total_pages; $i++) {
  123.                 $pages .= "<a href=\"?page=$i\"> $i </a>  ";
  124.                 if ($i != $total_pages) {
  125.                     $pages .=  " - ";
  126.                 }
  127.             }
  128.         }
  129.         if ($total_pages > 0 && $currentpage != $total_pages && $total_pages > 1) {
  130.             $pages .= "<a href=\"?page=" . (intval($currentpage) + 1) . "\"> &raquo; Next </a>";
  131.         }
  132.     $pages .= "</span>\n</div>";
  133.     //end of pages
  134.     print $pages;
  135.     while ($db_field = mysql_fetch_assoc($result)) {
  136.         $replyquery = "SELECT COUNT(*) FROM forum WHERE OP=".$db_field["post_num"];
  137.         $replyresult = mysql_query($replyquery) or die(mysql_error());
  138.         while($replyrow = mysql_fetch_array($replyresult)){
  139.             $replies = $replyrow['COUNT(*)'];
  140.         }
  141.         print "<div class=\"thread\">\n<div class=\"title\">\n<h2>" . htmlentities(stripslashes($db_field["title"])) . "</h2>\n";
  142.         if ( $_GET['id'] ) {
  143.             print "<h4>" . htmlentities(stripslashes($db_field["user"])) . " - " . date('H:i', $db_field["created"]) . "</h4>\n";
  144.         } else {
  145.             print "<h4>" . htmlentities(stripslashes($db_field["user"])) . " - " . date('H:i', $db_field["created"]) . " - " . $replies . " Replies" . "</h4>\n";
  146.         }
  147.         print "</div>\n<div class=\"post_body\">\n<p>" . htmlentities(stripslashes($db_field["bodytext"])) . "</p>\n</div>\n";
  148.         if ( !$_GET['id'] ) print "<h4> <a href=\"".$_SERVER['PHP_SELF']."?id=".$db_field["post_num"]."\">Read more... </a> </h4>";
  149.         print "</div>";
  150.         if ( $db_field["OP"] == 0 && $_GET['id'] ) { print "<div id=\"replies\">"; }
  151.     }
  152.     if ($_GET['id']) print "</div>";
  153.     if ( $_GET['id'] ) post_box();
  154. }
  155.  
  156. function post_box() {
  157.     print <<<POST_FORM
  158. <a id="link1" href="javascript:display('show')"> Make Post </a>
  159. <div id="postform" style="display:none">
  160. <form method="post" >
  161.     <div class="clear"></div>
  162.     <label for="title">Title: </label><br />
  163.     <input name="title" id="title" type="text" maxlength="150" placeholder="Title"/><br>
  164.     <div class="clear"></div>
  165.     <label for="name">Name: </label><br />
  166.     <input name="name" id="name" type="text" maxlength="50" value="Anon"/><br>
  167.     <div class="clear"></div>
  168.     <label for="message">Message: </label>Maximum of 2500 Characters
  169.     <textarea name="bodytext" id="bodytext" maxlength=2500 placeholder="Message"></textarea><br>
  170.     <input type="submit" value="Submit" />
  171. </form>
  172. </div>
  173. POST_FORM;
  174. }
  175.  
  176. function admin() {
  177.     if(isset($_SESSION['username'])){
  178.         $username = mysql_real_escape_string($_SESSION['username']);
  179.         $result=mysql_query("SELECT username FROM users WHERE username='$username'");
  180.         $loggedin=mysql_num_rows($result);
  181.         if($loggedin!==1) exit;
  182.         print "Logged in as: <span class=\"user\"> " . $_SESSION['username'] . " <a href=\"?logout=1\"> [Logout] </a></span>";
  183.         $SQL = "SELECT * FROM forum ORDER BY last_post DESC";
  184.         $result = mysql_query($SQL);
  185.         print "<div id=\"admin_posts\">";
  186.         while ($db_field = mysql_fetch_assoc($result)) {
  187.             $title = htmlentities(stripslashes($db_field["title"]));
  188.             $user = htmlentities(stripslashes($db_field["user"]));
  189.             $text = htmlentities(stripslashes($db_field["bodytext"]));
  190.             $post_num = htmlentities(stripslashes($db_field["post_num"]));
  191.             $op = htmlentities(stripslashes($db_field["op"]));
  192.             print <<<ADMIN_PANEL
  193. <div class="thread"><div class="title"> Title: $title Author: $user Post #: $post_num</div>
  194. <p> $text </p> <form method="get"><input type="hidden" value="1" name="admin" /><input type="hidden" value="{$post_num}" name="edit" /><input type="submit" value="Edit" ></form><form method="get"><input type="hidden" value="1" name="admin" /><input type="hidden" value="{$post_num}" name="delete" /><input type="submit" value="Delete" > <input type="checkbox" name="all" value="true" /> Remove sub-posts </form></div>
  195. ADMIN_PANEL;
  196.     if ( $_GET['delete'] ) {
  197.         $post_num = mysql_real_escape_string($_GET['delete']);
  198.         if ( $_GET['all'] == "true" ) { $SQL = "DELETE FROM forum WHERE post_num=$post_num OR OP=$post_num"; } else { $SQL = "DELETE FROM forum WHERE post_num=$post_num"; }
  199.         mysql_query($SQL);
  200.     }
  201.     }
  202.     print "</div>";
  203.     } elseif ( $_POST ) {
  204.         $username = mysql_real_escape_string($_POST['username']);
  205.         $password = hash_hmac('sha512', mysql_real_escape_string($_POST['password']), "[%6#llKZ`dW£v:Qb5%eJbdpUAZ6ta#j&W'nb{{iQ5*S'IGa@:W"); //hashes the password in sha512 with a 50 character salt
  206.         $SQL = "SELECT username FROM users WHERE username='$username' and password='$password'";
  207.         $result=mysql_query($SQL);
  208.         $loggedin=mysql_num_rows($result);
  209.         while ($db_field = mysql_fetch_assoc($result)) {
  210.             $id = $db_field['user_id'];
  211.         }
  212.         if($loggedin==1) {
  213.             setCookie("username", $username, time()+36000);
  214.             $_SESSION['username'] = $username;
  215.             header("Location: " . $_SERVER['PHP_SELF'] . "?admin=1");
  216.         } else {
  217.             header("Location: " . $_SERVER['PHP_SELF']);
  218.         }
  219.     } else {
  220.     print <<<ADMIN
  221. <div id="adminform">
  222. <form method="post" action="{$_SERVER['PHP_SELF']}?admin=1">
  223.     <label for="Username">Username: </label><br />
  224.     <input name="username" id="username" type="text" maxlength="150" /><br>
  225.     <div class="clear"></div>
  226.     <label for="password">Password: </label><br />
  227.     <input name="password" id="password" type="password" maxlength="150" /><br>
  228.     <input type="submit" value="Submit" />
  229. </form>
  230. </div>
  231. ADMIN;
  232.     }
  233. }
  234.  
  235. if ( $_POST['bodytext'] ) {
  236.     post($_POST['title'],$_POST['name'],$_POST['bodytext'],$_GET['id'],time());
  237. } elseif ( $_GET['admin'] == 1 || $_POST['username']) {
  238.     admin();
  239. } elseif ( $_GET['logout'] == 1 ) {
  240.     session_unset();
  241.     session_destroy();
  242.     header("Location: " . $_SERVER['PHP_SELF']);
  243. } else {
  244.     get_thread();
  245. }
  246.  
  247. include_once('include/footer.php');
  248.  
  249. ?>
Advertisement
Add Comment
Please, Sign In to add comment