2003-07-18 21:38:51 +00:00
< ? php
2003-08-13 22:05:36 +00:00
require_once ( '../db.inc' );
2003-12-04 22:42:51 +00:00
require_once ( '../sanitize_html.inc' );
2003-07-18 21:38:51 +00:00
2003-08-01 20:27:53 +00:00
define ( 'NO_CONTROLS' , 0 );
define ( 'FORUM_CONTROLS' , 1 );
define ( 'HELPDESK_CONTROLS' , 2 );
2003-08-13 22:05:36 +00:00
define ( 'SOLUTION' , 'This answered my question' );
define ( 'SUFFERER' , 'I also have this question' );
2003-08-01 20:27:53 +00:00
define ( 'OFF_TOPIC' , 'Off-topic' );
2003-08-13 22:05:36 +00:00
$forum_sort_styles [ 'modified-new' ] = " Most recent post first " ;
$forum_sort_styles [ 'modified-old' ] = " Least recent post first " ;
2003-11-30 21:05:57 +00:00
//$forum_sort_styles['activity-most'] = "Most recent activity first";
2003-08-13 22:05:36 +00:00
$forum_sort_styles [ 'views-most' ] = " Most views first " ;
2003-11-30 21:05:57 +00:00
$forum_sort_styles [ 'replies-most' ] = " Most posts first " ;
2003-08-13 22:05:36 +00:00
$thread_sort_styles [ 'date-old' ] = " Oldest first " ;
$thread_sort_styles [ 'date-new' ] = " Newest first " ;
$thread_sort_styles [ 'rating-high' ] = " Highest rated first " ;
2003-08-22 19:08:14 +00:00
2003-08-13 22:05:36 +00:00
$thread_filter_styles [ '2' ] = " \" Very helpful \" " ;
$thread_filter_styles [ '1' ] = " At least \" helpful \" " ;
$thread_filter_styles [ '0' ] = " At least \" neutral \" " ;
$thread_filter_styles [ '-1' ] = " At least \" unhelpful \" " ;
$thread_filter_styles [ '-2' ] = " All posts " ;
2003-08-14 00:56:20 +00:00
$post_ratings [ '2' ] = " Very helpful (+2) " ;
$post_ratings [ '1' ] = " Helpful (+1) " ;
$post_ratings [ '0' ] = " Neutral " ;
$post_ratings [ '-1' ] = " Unhelpful (-1) " ;
$post_ratings [ '-2' ] = " Off topic (-2) " ;
2003-07-18 21:38:51 +00:00
2003-08-14 00:56:20 +00:00
db_init ( '../' );
2003-07-18 21:38:51 +00:00
function getCategories () {
2003-11-30 21:05:57 +00:00
$langID = ( ! empty ( $_SESSION [ 'lang' ][ 'id' ])) ? $_SESSION [ 'lang' ][ 'id' ] : 1 ;
$sql = " SELECT * FROM category WHERE lang = " . $langID . " AND is_helpdesk = 0 ORDER BY orderID ASC " ;
return mysql_query ( $sql );
2003-08-13 22:05:36 +00:00
}
function getHelpDeskCategories () {
2003-11-30 21:05:57 +00:00
$sql = " SELECT * FROM category WHERE is_helpdesk = 1 ORDER BY orderID ASC " ;
return mysql_query ( $sql );
2003-08-13 22:05:36 +00:00
}
function getForums ( $categoryID ) {
2003-11-30 21:05:57 +00:00
$sql = 'SELECT * FROM forum WHERE category = ' . $categoryID . ' ORDER BY orderID ASC' ;
2003-08-22 19:08:14 +00:00
return mysql_query ( $sql );
2003-07-18 21:38:51 +00:00
}
2003-08-14 00:56:20 +00:00
function getThreads ( $forumID , $min = - 1 , $nRec = - 1 , $sort_style = 'modified-new' ) {
2003-11-30 21:05:57 +00:00
$sql = 'SELECT * FROM thread WHERE forum = ' . $forumID ;
switch ( $sort_style ) {
case 'modified-new' :
$sql = $sql . ' ORDER BY timestamp DESC' ;
break ;
case 'modified-old' :
$sql = $sql . ' ORDER BY timestamp ASC' ;
break ;
case 'views-most' :
$sql = $sql . ' ORDER BY views DESC' ;
break ;
case 'replies-most' :
$sql = $sql . ' ORDER BY replies DESC' ;
break ;
// case 'activity-most':
// $sql = $sql . ' ORDER by activity DESC, timestamp DESC';
// break;
2003-12-03 23:29:22 +00:00
case 'help-activity-most' :
$sql = $sql . ' ORDER by sufferers DESC' ;
break ;
2003-11-30 21:05:57 +00:00
}
if ( $min > - 1 ) {
$sql .= ' LIMIT ' . $min ;
if ( $nRec > - 1 ) {
$sql .= ', ' . $nRec ;
}
} else if ( $nRec > - 1 ) {
$sql .= ' LIMIT ' . $nRec ;
2003-08-14 00:56:20 +00:00
}
2003-08-22 19:08:14 +00:00
2003-08-14 00:56:20 +00:00
return mysql_query ( $sql );
2003-07-18 21:38:51 +00:00
}
2003-08-14 00:56:20 +00:00
function getPosts ( $threadID , $min = - 1 , $nRec = - 1 , $sort_style = " date-old " ) {
2003-11-30 21:05:57 +00:00
$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 ;
2003-08-14 00:56:20 +00:00
}
if ( $min > - 1 ) {
2003-11-30 21:05:57 +00:00
$sql .= ' LIMIT ' . $min ;
if ( $nRec > - 1 ) {
$sql .= ', ' . $nRec ;
}
} elseif ( $nRec > - 1 ) {
$sql .= ' LIMIT ' . $nRec ;
}
return mysql_query ( $sql );
2003-07-18 21:38:51 +00:00
}
/* specific database functions */
2003-08-13 22:05:36 +00:00
function getCategory ( $categoryID ) {
2003-11-30 21:05:57 +00:00
$sql = " SELECT * FROM category WHERE id = " . $categoryID ;
$result = mysql_query ( $sql );
if ( $result ) {
return mysql_fetch_object ( $result );
} else {
return NULL ;
}
2003-08-13 22:05:36 +00:00
}
2003-07-18 21:38:51 +00:00
function getForum ( $forumID ) {
2003-08-14 00:56:20 +00:00
$sql = " SELECT * FROM forum WHERE id = " . $forumID ;
$result = mysql_query ( $sql );
if ( $result ) {
2003-11-30 21:05:57 +00:00
return mysql_fetch_object ( $result );
} else {
return NULL ;
}
2003-07-18 21:38:51 +00:00
}
function getThread ( $threadID ) {
2003-07-24 22:58:38 +00:00
$sql = " SELECT * FROM thread WHERE id = " . $threadID ;
2003-08-14 00:56:20 +00:00
$result = mysql_query ( $sql );
if ( $result ) {
2003-11-30 21:05:57 +00:00
return mysql_fetch_object ( $result );
} else {
return NULL ;
}
2003-07-18 21:38:51 +00:00
}
2003-07-18 22:44:17 +00:00
function getPost ( $postID ) {
2003-07-24 22:58:38 +00:00
$sql = " SELECT * FROM post WHERE id = " . $postID ;
2003-08-14 00:56:20 +00:00
$result = mysql_query ( $sql );
if ( $result ) {
2003-11-30 21:05:57 +00:00
return mysql_fetch_object ( $result );
} else {
return NULL ;
}
2003-07-18 22:44:17 +00:00
}
2003-07-24 22:58:38 +00:00
2003-08-01 20:27:53 +00:00
// Returns the post that started the thread with id = $threadId
function getFirstPost ( $threadID ) {
2003-11-30 21:05:57 +00:00
$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 ;
}
2003-08-14 00:56:20 +00:00
}
function incThreadViews ( $threadID ) {
2003-11-30 21:05:57 +00:00
$sql = " UPDATE thread SET views = views + 1 WHERE id = " . $threadID . " LIMIT 1 " ;
mysql_query ( $sql );
2003-08-14 00:56:20 +00:00
}
/* Forum modifying functions. */
function createThread ( $forumID , $ownerID , $title , $content ) {
2003-08-22 19:08:14 +00:00
2003-12-04 22:42:51 +00:00
$title = addslashes ( sanitize_html ( $title ));
$content = addslashes ( sanitize_html ( $content ));
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
$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 ();
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
$postID = addPost ( $threadID , $ownerID , NULL , $content );
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
$sql = " UPDATE user SET posts = posts + 1 WHERE id = " . $ownerID . " LIMIT 1 " ;
mysql_query ( $sql );
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
$sql = " UPDATE forum SET threads = threads + 1, posts = posts + 1, timestamp = UNIX_TIMESTAMP() WHERE id = " . $forumID . " LIMIT 1 " ;
mysql_query ( $sql );
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
return $threadID ;
2003-08-14 00:56:20 +00:00
}
function replyToThread ( $threadID , $userID , $content , $parent_post = NULL ) {
2003-11-30 21:05:57 +00:00
$thread = getThread ( $threadID );
2003-08-22 19:08:14 +00:00
2003-12-04 22:42:51 +00:00
$content = addslashes ( sanitize_html ( $content ));
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
addPost ( $threadID , $userID , $parent_post , $content );
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
$sql = " UPDATE user SET posts = posts + 1 WHERE id = " . $userID . " LIMIT 1 " ;
mysql_query ( $sql );
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
$sql = " UPDATE thread SET replies = replies + 1, timestamp = UNIX_TIMESTAMP() WHERE id = " . $threadID . " LIMIT 1 " ;
mysql_query ( $sql );
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
$sql = " UPDATE forum SET posts = posts + 1, timestamp = UNIX_TIMESTAMP() WHERE id = " . $thread -> forum . " LIMIT 1 " ;
mysql_query ( $sql );
}
2003-08-14 00:56:20 +00:00
function addPost ( $threadID , $userID , $parentID , $content ) {
2003-11-30 21:05:57 +00:00
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 ;
2003-08-14 00:56:20 +00:00
}
function updatePost ( $postID , $content ) {
2003-12-04 22:42:51 +00:00
$x = addslashes ( sanitize_html ( stripslashes ( $content )));
$sql = " UPDATE post SET content = \" $x\ " , modified = UNIX_TIMESTAMP () WHERE id = " . $postID ;
2003-11-30 21:05:57 +00:00
$result = mysql_query ( $sql );
if ( ! $result ) return false ;
return true ;
2003-08-01 20:27:53 +00:00
}
2003-07-26 00:20:05 +00:00
/* display functions */
2003-08-01 20:27:53 +00:00
function show_posts ( $thread , $sort_style , $filter , $show_controls = true , $do_coloring = true , $is_helpdesk = false ) {
2003-07-26 00:20:05 +00:00
global $logged_in_user ;
2003-08-22 19:08:14 +00:00
$n = 1 ;
2003-08-01 20:27:53 +00:00
if ( $show_controls && ! $is_helpdesk ) {
2003-08-08 00:55:36 +00:00
$controls = FORUM_CONTROLS ;
2003-08-01 20:27:53 +00:00
} else if ( $show_controls && $is_helpdesk ) {
2003-08-08 00:55:36 +00:00
$controls = HELPDESK_CONTROLS ;
2003-08-01 20:27:53 +00:00
} else {
2003-08-08 00:55:36 +00:00
$controls = NO_CONTROLS ;
2003-08-01 20:27:53 +00:00
}
2003-08-22 19:08:14 +00:00
2003-08-14 00:56:20 +00:00
$posts = getPosts ( $thread -> id , - 1 , - 1 , $sort_style );
2003-08-01 20:27:53 +00:00
$firstPost = getFirstPost ( $thread -> id );
2003-08-22 19:08:14 +00:00
2003-08-01 20:27:53 +00:00
if ( $is_helpdesk ) {
2003-08-08 00:55:36 +00:00
if ( $firstPost ) {
show_post ( $firstPost , $thread , $logged_in_user , $n , $controls , true );
2003-11-30 21:05:57 +00:00
}
2003-08-01 20:27:53 +00:00
}
2003-08-22 19:08:14 +00:00
2003-08-14 00:56:20 +00:00
while ( $post = mysql_fetch_object ( $posts )) {
2003-07-30 00:50:02 +00:00
if ( $post -> score >= $filter ) {
2003-08-08 00:55:36 +00:00
if ( ! $is_helpdesk || ( $is_helpdesk && $post -> id != $firstPost -> id )) {
2003-11-30 21:05:57 +00:00
show_post ( $post , $thread , $logged_in_user , $n , $controls , false );
if ( $do_coloring ) $n = ( $n + 1 ) % 2 ;
2003-08-08 00:55:36 +00:00
}
2003-07-30 00:50:02 +00:00
}
2003-08-22 19:08:14 +00:00
}
2003-07-26 00:20:05 +00:00
}
2003-08-01 20:27:53 +00:00
function show_post ( $post , $thread , $logged_in_user , $n , $controls = FORUM_CONTROLS , $separate = false ) {
2003-11-30 21:05:57 +00:00
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 "
< tr class = \ " row $n\ " style = \ " vertical-align:top \" >
< td >
< a name = $post -> id ></ a >
< p style = \ " font-weight:bold \" >
" ;
if ( $user -> has_profile ) {
echo " <a href= \" ../view_profile.php?userid= $post->user\ " > " , $user->name , " </ a > " ;
} else {
echo $user -> name ;
}
echo "
</ p >
< p style = \ " font-size:8pt \" >
Joined : " , gmdate('M j, Y', $user->create_time ), " < br > Posts : " , $user->posts , "
</ p >
</ td >
< td >
" ;
if ( $controls == FORUM_CONTROLS || $controls == HELPDESK_CONTROLS ) {
echo " <form action= \" rate.php?post= " , $post -> id , " \" method= \" post \" > " ;
}
echo "
< table width = 100 % cellspacing = 0 cellpadding = 0 >
< tr valign = \ " top \" >
< td align = \ " left \" style= \" border:0px \" ><p style= \" font-size:8pt \" >
Posted : " , pretty_time_str( $post->timestamp );
;
if ( $post -> parent_post ) echo " in response to <a href=# $post->parent_post >Message ID $post->parent_post </a>. " ;
if ( $can_edit && $controls != NO_CONTROLS ) echo " <a href= \" edit.php?id= $post->id\ " > [ Edit this post ] </ a > " ;
if ( $post -> modified ) echo " <br>Last modified: " , pretty_time_Str ( $post -> modified );
echo " </p> \n </td> \n " ;
if ( $controls == FORUM_CONTROLS ) {
echo " <td align= \" right \" style= \" border:0px \" >Rate this post: " ;
show_combo_from_array ( " rating " , $post_ratings , " 0 " );
echo " <input type= \" submit \" value= \" Rate \" ></td> " ;
} else if ( $controls == HELPDESK_CONTROLS && $separate ) {
echo "
< td align = \ " right \" style= \" border:0px \" >
< input type = \ " submit \" name= \" submit \" value= \" " , SUFFERER , " \" >
</ td >
" ;
} else if ( $controls == HELPDESK_CONTROLS && ! $separate ) {
echo "
< td align = \ " right \" style= \" border:0px \" >
< input type = \ " submit \" name= \" submit \" value= \" " , SOLUTION , " \" >
< input type = \ " submit \" name= \" submit \" value= \" " , OFF_TOPIC , " \" >
</ td >
" ;
}
echo " </tr> \n </table> \n " ;
if ( $controls == FORUM_CONTROLS || $controls == HELPDESK_CONTROLS ) {
echo " </form> " ;
}
echo " <p> " , nl2br ( stripslashes ( $post -> content )), " </p> " ;
echo " <table width=100% cellspacing=0 cellpadding=0>
< tr valign = \ " bottom \" >
< td align = \ " left \" style= \" border:0px; font-size:7pt \" ><i>ID: " , $post -> id ;
if ( $controls == HELPDESK_CONTROLS && $separate ) {
echo " </i></td> " ;
} else if ( $controls == HELPDESK_CONTROLS && ! $separate ) {
echo " / Score: " , ( $post -> score * $post -> votes ), " </i></td> " ;
} else {
echo " / Rating: " , $post -> score , " </i></td> " ;
}
if ( $controls == FORUM_CONTROLS ) {
echo " <td align= \" right \" style= \" border:0px \" >[<a href= \" reply.php?thread= " . $thread -> id . " &post= " . $post -> id . " #input \" >Reply to this post</a>]</td> " ;
} else if ( $controls == HELPDESK_CONTROLS && ! $separate ) {
echo " <td align= \" right \" style= \" border:0px \" >[<a href= \" reply.php?thread= " . $thread -> id . " &post= " . $post -> id . " &helpdesk=1#input \" >Reply to this answer</a>]</td> " ;
}
echo " </tr></table></td></tr> " ;
if ( $separate ) {
echo "
</ table >
< br >< br >
< table class = \ " content \" border=0 cellpadding=5 cellspacing=0 width=100%>
< tr >
< th style = \ " width: 150px \" >Author</th>
< th > Answers </ th >
" ;
}
2003-07-26 00:20:05 +00:00
}
2003-07-24 22:58:38 +00:00
2003-08-14 00:56:20 +00:00
/* utility functions */
2003-11-30 21:05:57 +00:00
function start_forum_table ( $headings , $span = NULL ) {
echo "
< p style = \ " text-align:center \" >
< table order = 0 cellpadding = 5 cellspacing = 0 width = 100 %>
< tr >
" ;
for ( $i = 0 ; $i < count ( $headings ); $i ++ ) {
$cell = " <th " ;
if ( $span ) {
$cell = $cell . " colspan= $span " ;
}
$cell = $cell . " > " ;
echo $cell , $headings [ $i ], " </th> \n " ;
}
echo " </tr> \n " ;
2003-08-13 22:05:36 +00:00
}
function end_forum_table () {
2003-11-30 21:05:57 +00:00
echo " </table></p> \n " ;
2003-08-13 22:05:36 +00:00
}
2003-08-22 19:08:14 +00:00
function show_combo_from_array ( $name , $array , $selection ) {
2003-11-30 21:05:57 +00:00
echo " <select name= \" $name\ " > " ;
foreach ( $array as $key => $value ) {
echo " <option " ;
if ( $key == $selection ) {
echo " selected " ;
}
echo " value= \" " , $key , " \" > " , $value , " </option> " ;
}
echo " </select> " ;
2003-08-13 22:05:36 +00:00
}
function show_forum_title ( $forum = NULL , $thread = NULL , $helpdesk = false ) {
2003-11-30 21:05:57 +00:00
echo " <p> \n " ;
if ( ! $forum && ! $thread ) {
echo " <p class= \" title \" > " ;
if ( $helpdesk ) {
2003-12-01 06:08:22 +00:00
echo " Questions and problems</p> " ;
2003-11-30 21:05:57 +00:00
} else {
echo " Message boards</p> " ;
}
} else if ( $forum && ! $thread ) {
echo " <span class=title> " ;
if ( $helpdesk ) {
2003-12-01 06:08:22 +00:00
echo " <a href=help_desk.php> " , " Questions and problems</a> : " ;
2003-11-30 21:05:57 +00:00
} else {
echo " <a href=index.php> " , " Message boards</a> : " ;
}
echo $forum -> title ;
echo " </span><br> " ;
} else if ( $forum && $thread ) {
echo " <span class=title> " ;
if ( $helpdesk ) {
2003-12-01 06:08:22 +00:00
echo " <a href=help_desk.php> " , " Questions and problems</a> : " ;
2003-11-30 21:05:57 +00:00
} else {
2003-12-03 06:39:43 +00:00
echo " <a href=index.php> " , " Message boards</a> : " ;
2003-11-30 21:05:57 +00:00
}
echo " <a href= \" forum.php?id= " , $forum -> id , " \" > " , $forum -> title , " </a> : " ;
echo $thread -> title ;
echo " </span><br> " ;
} else {
echo " Invalid input to show_forum_title<br> " ;
}
echo " </p> \n " ;
2003-08-13 22:05:36 +00:00
}
2003-08-22 19:08:14 +00:00
2003-11-30 21:05:57 +00:00
?>