-1) { $sql .= ' LIMIT '.$min; if ($nRec > -1) { $sql .= ', '.$nRec; } } else if ($nRec > -1) { $sql .= ' LIMIT '.$nRec; } return mysql_query($sql); } function getPosts($threadID, $min = -1, $nRec = -1, $sort_style="date-old") { $sql = 'SELECT *, (score * votes) AS rating FROM post WHERE thread = '. $threadID; switch($sort_style) { case 'date-old': $sql = $sql . ' ORDER BY timestamp ASC'; break; case 'date-new': $sql = $sql . ' ORDER BY timestamp DESC'; break; case 'score-high': $sql = $sql . ' ORDER BY score DESC'; break; case 'rating-high': $sql = $sql . ' ORDER BY rating DESC'; break; } if ($min > -1) { $sql .= ' LIMIT '.$min; if ($nRec > -1) { $sql .= ', '.$nRec; } } elseif ($nRec > -1) { $sql .= ' LIMIT '.$nRec; } return mysql_query($sql); } /* specific database functions */ function getCategory($categoryID) { $sql = "SELECT * FROM category WHERE id = ".$categoryID; $result = mysql_query($sql); if ($result) { return mysql_fetch_object($result); } else { return NULL; } } function getForum($forumID) { $sql = "SELECT * FROM forum WHERE id = " . $forumID; $result = mysql_query($sql); if ($result) { return mysql_fetch_object($result); } else { return NULL; } } function getThread($threadID) { $sql = "SELECT * FROM thread WHERE id = ".$threadID; $result = mysql_query($sql); if ($result) { return mysql_fetch_object($result); } else { return NULL; } } function getPost($postID) { $sql = "SELECT * FROM post WHERE id = ".$postID; $result = mysql_query($sql); if ($result) { return mysql_fetch_object($result); } else { return NULL; } } // Returns the post that started the thread with id = $threadId function getFirstPost($threadID) { $sql = "SELECT * FROM post WHERE thread = " . $threadID ." ORDER BY id ASC"; $result = mysql_query($sql); if ($result) { return mysql_fetch_object($result); } else { return NULL; } } function incThreadViews($threadID) { $sql = "UPDATE thread SET views = views + 1 WHERE id = " . $threadID . " LIMIT 1"; mysql_query($sql); } /* Forum modifying functions. */ function createThread($forumID, $ownerID, $title, $content) { $title = addslashes(htmlentities($title)); $content = addslashes(htmlentities($content)); $sql = "INSERT INTO thread (forum, owner, title, timestamp) VALUES (" . $forumID . ", " . $ownerID . ", '" . $title . "', UNIX_TIMESTAMP())"; $result = mysql_query($sql); if (!$result) return false; $threadID = mysql_insert_id(); $postID = addPost($threadID, $ownerID, NULL, $content); $sql = "UPDATE user SET posts = posts + 1 WHERE id = " . $ownerID . " LIMIT 1"; mysql_query($sql); $sql = "UPDATE forum SET threads = threads + 1, posts = posts + 1, timestamp = UNIX_TIMESTAMP() WHERE id = " . $forumID . " LIMIT 1"; mysql_query($sql); return $threadID; } function replyToThread($threadID, $userID, $content, $parent_post=NULL) { $thread = getThread($threadID); $content = addslashes(htmlentities($content)); addPost($threadID, $userID, $parent_post, $content); $sql = "UPDATE user SET posts = posts + 1 WHERE id = " . $userID . " LIMIT 1"; mysql_query($sql); $sql = "UPDATE thread SET replies = replies + 1, timestamp = UNIX_TIMESTAMP() WHERE id = " . $threadID . " LIMIT 1"; mysql_query($sql); $sql = "UPDATE forum SET posts = posts + 1, timestamp = UNIX_TIMESTAMP() WHERE id = " . $thread->forum . " LIMIT 1"; mysql_query($sql); } function addPost($threadID, $userID, $parentID, $content) { if ($parentID) { $sql = "INSERT INTO post (thread, user, timestamp, content, parent_post) VALUES (" . $threadID . ", " . $userID . ", UNIX_TIMESTAMP(), '" . $content . "', " . $parentID . ")"; } else { $sql = "INSERT INTO post (thread, user, timestamp, content) VALUES (" . $threadID . ", " . $userID . ", UNIX_TIMESTAMP(), '" . $content . "')"; } $result = mysql_query($sql); if (!$result) return false; //return ($post->id = mysql_insert_id()); mysql_insert_id(); return true; } function updatePost($postID, $content) { $sql = "UPDATE post SET content = \"$content\", modified = UNIX_TIMESTAMP() WHERE id = " . $postID; $result = mysql_query($sql); if (!$result) return false; return true; } /* display functions */ function show_posts($thread, $sort_style, $filter, $show_controls=true, $do_coloring=true, $is_helpdesk=false) { global $logged_in_user; $n = 0; if ($show_controls && !$is_helpdesk) { $controls = FORUM_CONTROLS; } else if ($show_controls && $is_helpdesk) { $controls = HELPDESK_CONTROLS; } else { $controls = NO_CONTROLS; } $posts = getPosts($thread->id, -1, -1, $sort_style); $firstPost = getFirstPost($thread->id); if ($is_helpdesk) { if ($firstPost) { show_post($firstPost, $thread, $logged_in_user, $n, $controls, true); } } while ($post = mysql_fetch_object($posts)) { if ($post->score >= $filter) { if (!$is_helpdesk || ($is_helpdesk && $post->id != $firstPost->id)) { show_post($post, $thread, $logged_in_user, $n, $controls, false); if ($do_coloring) $n = ($n+1)%2; } } } } function show_post($post, $thread, $logged_in_user, $n, $controls=FORUM_CONTROLS, $separate=false) { global $post_ratings; $user = lookup_user_id($post->user); $sql = "SELECT * FROM profile WHERE userid = " . $user->id; $result2 = mysql_query($sql); $user->has_profile = (mysql_num_rows($result2) > 0); $can_edit = $logged_in_user && $user->id == $logged_in_user->id; echo " id >

"; if ($user->has_profile) { echo "user\">", $user->name, ""; } else { echo $user->name; } echo "

Joined: ", date('M j, Y', $user->create_time), "
Posts: ", $user->posts, "

"; if ($controls == FORUM_CONTROLS || $controls == HELPDESK_CONTROLS) { echo "
id, "\" method=\"post\">"; } echo " \n"; if ($controls == FORUM_CONTROLS) { echo ""; } else if ($controls == HELPDESK_CONTROLS && $separate) { echo " "; } else if ($controls == HELPDESK_CONTROLS && !$separate) { echo " "; } echo "\n

Posted: ", date('D M j, Y g:i a', $post->timestamp) ; if ($post->parent_post) echo " in response to parent_post>Message ID $post->parent_post."; if ($can_edit && $controls != NO_CONTROLS) echo " id\">[Edit this post]"; if ($post->modified) echo "
Last Modified: ", date('D M j, Y g:i a', $post->modified); echo "

\n
Rate this post:"; show_combo_from_array("rating", $post_ratings, "0"); echo "
\n"; if ($controls == FORUM_CONTROLS || $controls == HELPDESK_CONTROLS) { echo "
"; } echo "

", nl2br(stripslashes($post->content)), "

"; echo ""; } else if ($controls == HELPDESK_CONTROLS && !$separate) { echo " / Score: ", ($post->score * $post->votes), ""; } else { echo " / Rating: ", $post->score, ""; } if ($controls == FORUM_CONTROLS) { echo ""; } else if ($controls == HELPDESK_CONTROLS && !$separate) { echo ""; } echo "
ID: ", $post->id; if ($controls == HELPDESK_CONTROLS && $separate) { echo "[id . "&post=" . $post->id . "#input\">Reply to this post][id . "&post=" . $post->id . "&helpdesk=1#input\">Reply to this answer]
"; if ($separate) { echo "

"; } } /* utility functions */ function start_forum_table($headings, $widths, $span=NULL) { echo "

Author Answers
"; for ($i = 0; $i < count($headings); $i++) { $cell = ""; } else { $cell = $cell . ">"; } echo $cell, $headings[$i], "\n"; } echo "\n"; } function end_forum_table() { echo "

\n"; } function show_combo_from_array($name, $array, $selection) { echo ""; } function show_forum_title($forum=NULL, $thread=NULL, $helpdesk=false) { echo "

\n"; if (!$forum && !$thread) { echo "

", PROJECT; if ($helpdesk) { echo " Help Desk

"; } else { echo " Forum

"; } } else if ($forum && !$thread) { echo "", $forum->title, "
"; if ($helpdesk) { echo "", PROJECT, " Help Desk"; } else { echo "", PROJECT, " Forum"; } } else if ($forum && $thread) { echo "", $thread->title, "
"; if ($helpdesk) { echo "", PROJECT, " Help Desk -> id, "\">", $forum->title, ""; } else { echo "", PROJECT, " Forum -> id, "\">", $forum->title, ""; } } else { echo "Invalid input to show_forum_title
"; } echo "

\n"; } ?>