2000-07-06 09:38:42 +00:00
< ?
2000-09-26 07:34:33 +00:00
function discussion_score ( $comment ) {
2000-10-10 10:51:00 +00:00
$value = ( $comment -> votes ) ? ( $comment -> score / $comment -> votes ) : (( $comment -> score ) ? $comment -> score : 0 );
2000-09-26 07:34:33 +00:00
return ( strpos ( $value , " . " )) ? substr ( $value . " 00 " , 0 , 4 ) : $value . " .00 " ;
}
function discussion_moderate ( $moderate ) {
global $user , $comment_votes ;
2000-10-13 10:17:30 +00:00
if ( $user -> id && $moderate ) {
$na = $comment_votes [ key ( $comment_votes )];
2000-09-26 07:34:33 +00:00
2000-10-13 10:17:30 +00:00
foreach ( $moderate as $id => $vote ) {
if ( $vote != $comment_votes [ $na ] && ! user_getHistory ( $user -> history , " c $id " )) {
### Update the comment's score:
$result = db_query ( " UPDATE comments SET score = score $vote , votes = votes + 1 WHERE cid = $id " );
2000-09-26 07:34:33 +00:00
2000-10-13 10:17:30 +00:00
### Update the user's history:
user_setHistory ( $user , " c $id " , $vote );
}
2000-09-26 07:34:33 +00:00
}
}
}
2000-10-19 13:31:23 +00:00
function discussion_kids ( $cid , $mode , $threshold , $level = 0 , $dummy = 0 ) {
2000-07-06 09:38:42 +00:00
global $user , $theme ;
$comments = 0 ;
2000-10-19 13:31:23 +00:00
$result = db_query ( " SELECT c.*, u.* FROM comments c LEFT JOIN users u ON c.author = u.id WHERE c.pid = $cid AND (c.votes = 0 OR c.score / c.votes >= $threshold ) ORDER BY c.timestamp, c.cid " );
2000-07-06 09:38:42 +00:00
if ( $mode == " nested " ) {
while ( $comment = db_fetch_object ( $result )) {
2000-10-19 13:31:23 +00:00
if ( $comment -> score >= $threshold ) {
2000-07-06 09:38:42 +00:00
if ( $level && ! $comments ) print " <UL> " ;
$comments ++ ;
2000-10-02 07:32:17 +00:00
$link = " <A HREF= \" discussion.php?op=reply&sid= $comment->sid &pid= $comment->cid\ " >< FONT COLOR = \ " $theme->hlcolor2\ " > reply to this comment </ FONT ></ A > " ;
2000-10-19 13:31:23 +00:00
$theme -> comment ( $comment -> userid , stripslashes ( $comment -> subject ), stripslashes ( $comment -> comment ), $comment -> timestamp , stripslashes ( $comment -> url ), stripslashes ( $comment -> fake_email ), discussion_score ( $comment ), $comment -> votes , $comment -> cid , $link );
2000-07-06 09:38:42 +00:00
2000-10-19 13:31:23 +00:00
discussion_kids ( $comment -> cid , $mode , $threshold , $level + 1 , $dummy + 1 );
2000-07-06 09:38:42 +00:00
}
}
}
2000-10-13 10:17:30 +00:00
else { // mode == 'flat'
2000-07-06 09:38:42 +00:00
while ( $comment = db_fetch_object ( $result )) {
2000-10-19 13:31:23 +00:00
if ( $comment -> score >= $threshold ) {
2000-10-02 07:32:17 +00:00
$link = " <A HREF= \" discussion.php?op=reply&sid= $comment->sid &pid= $comment->cid\ " >< FONT COLOR = \ " $theme->hlcolor2\ " > reply to this comment </ FONT ></ A > " ;
2000-10-19 13:31:23 +00:00
$theme -> comment ( $comment -> userid , check_output ( $comment -> subject ), check_output ( $comment -> comment ), $comment -> timestamp , $comment -> url , $comment -> fake_email , discussion_score ( $comment ), $comment -> votes , $comment -> cid , $link );
2000-07-06 09:38:42 +00:00
}
2000-10-19 13:31:23 +00:00
discussion_kids ( $comment -> cid , $mode , $threshold );
2000-07-06 09:38:42 +00:00
}
}
if ( $level && $comments ) {
print " </UL> " ;
}
}
2000-10-19 13:31:23 +00:00
function discussion_childs ( $cid , $threshold , $level = 0 , $thread ) {
2000-10-02 07:32:17 +00:00
global $theme , $user ;
2000-07-06 09:38:42 +00:00
### Perform SQL query:
2000-10-19 13:31:23 +00:00
$result = db_query ( " SELECT c.*, u.* FROM comments c LEFT JOIN users u ON c.author = u.id WHERE c.pid = $cid AND (c.votes = 0 OR c.score / c.votes >= $threshold ) ORDER BY c.timestamp, c.cid " );
2000-07-06 09:38:42 +00:00
if ( $level == 0 ) $thread = " " ;
$comments = 0 ;
while ( $comment = db_fetch_object ( $result )) {
if ( $level && ! $comments ) {
$thread .= " <UL> " ;
}
$comments ++ ;
### Compose link:
2000-10-02 07:32:17 +00:00
$thread .= " <LI><A HREF= \" discussion.php?id= $comment->sid &cid= $comment->cid &pid= $comment->pid\ " > " . check_output( $comment->subject ) . " </ A > by " . format_username( $comment->userid ) . " < SMALL > ( " . discussion_score( $comment ) . " ) < SMALL ></ LI > " ;
2000-07-06 09:38:42 +00:00
### Recursive:
2000-10-19 13:31:23 +00:00
discussion_childs ( $comment -> cid , $threshold , $level + 1 , & $thread );
2000-07-06 09:38:42 +00:00
}
if ( $level && $comments ) {
$thread .= " </UL> " ;
}
return $thread ;
}
2000-10-19 13:31:23 +00:00
function discussion_settings ( $mode , $order , $threshold ) {
2000-10-02 07:32:17 +00:00
global $user ;
2000-10-12 06:44:11 +00:00
if ( $user -> id ) {
2000-10-19 13:31:23 +00:00
$data [ mode ] = $mode ;
$data [ sort ] = $order ;
$data [ threshold ] = $threshold ;
user_save ( $data , $user -> id );
2000-10-02 07:32:17 +00:00
}
}
function discussion_display ( $sid , $pid , $cid , $level = 0 ) {
2000-07-06 09:38:42 +00:00
global $user , $theme ;
### Pre-process variables:
$pid = ( empty ( $pid )) ? 0 : $pid ;
2000-10-02 07:32:17 +00:00
$cid = ( empty ( $cid )) ? 0 : $cid ;
2000-10-19 13:31:23 +00:00
$mode = ( $user -> id ) ? $user -> mode : " threaded " ;
$order = ( $user -> id ) ? $user -> sort : " 1 " ;
$threshold = ( $user -> id ) ? $user -> threshold : " 0 " ;
2000-07-06 09:38:42 +00:00
### Compose story-query:
2000-10-02 07:32:17 +00:00
$result = db_query ( " SELECT s.*, u.userid FROM stories s LEFT JOIN users u ON s.author = u.id WHERE s.status != 0 AND s.id = $sid " );
2000-07-06 09:38:42 +00:00
$story = db_fetch_object ( $result );
### Display story:
2000-10-13 10:17:30 +00:00
if ( $story -> status == 1 ) $theme -> article ( $story , " [ <A HREF= \" submission.php \" ><FONT COLOR= \" $theme->hlcolor2\ " > submission queue </ FONT ></ A > | < A HREF = \ " discussion.php?op=reply&sid= $story->id &pid=0 \" ><FONT COLOR= \" $theme->hlcolor2\ " > add a comment </ FONT ></ A > ] " );
else $theme -> article ( $story , " [ <A HREF= \" \" ><FONT COLOR= \" $theme->hlcolor2\ " > home </ FONT ></ A > | < A HREF = \ " discussion.php?op=reply&sid= $story->id &pid=0 \" ><FONT COLOR= \" $theme->hlcolor2\ " > add a comment </ FONT ></ A > ] " );
2000-07-06 09:38:42 +00:00
### Display `comment control'-box:
2000-10-19 13:31:23 +00:00
if ( $user -> id ) $theme -> commentControl ( $sid , $title , $threshold , $mode , $order );
2000-07-06 09:38:42 +00:00
### Compose query:
2000-10-19 13:31:23 +00:00
$query .= " SELECT c.*, u.* FROM comments c LEFT JOIN users u ON c.author = u.id WHERE c.sid = $sid AND c.pid = $pid AND (c.votes = 0 OR c.score / c.votes >= $threshold ) " ;
2000-07-06 09:38:42 +00:00
if ( $order == 1 ) $query .= " ORDER BY c.timestamp DESC " ;
if ( $order == 2 ) $query .= " ORDER BY c.score DESC " ;
2000-10-13 10:17:30 +00:00
$result = db_query ( $query );
2000-07-06 09:38:42 +00:00
2000-09-26 07:34:33 +00:00
print " <FORM METHOD= \" post \" ACTION= \" discussion.php \" > \n " ;
2000-07-06 09:38:42 +00:00
### Display the comments:
while ( $comment = db_fetch_object ( $result )) {
### Dynamically compose the `reply'-link:
if ( $pid != 0 ) {
2000-10-13 10:17:30 +00:00
list ( $pid ) = db_fetch_row ( db_query ( " SELECT pid FROM comments WHERE cid = $comment->pid " ));
2000-10-02 07:32:17 +00:00
$link = " <A HREF= \" discussion.php?id= $comment->sid &pid= $pid\ " >< FONT COLOR = \ " $theme->hlcolor2\ " > return to parent </ FONT ></ A > | < A HREF = \ " discussion.php?op=reply&sid= $comment->sid &pid= $comment->cid\ " >< FONT COLOR = \ " $theme->hlcolor2\ " > reply to this comment </ FONT ></ A > " ;
2000-07-06 09:38:42 +00:00
}
else {
2000-10-02 07:32:17 +00:00
$link = " <A HREF= \" discussion.php?op=reply&sid= $comment->sid &pid= $comment->cid\ " >< FONT COLOR = \ " $theme->hlcolor2\ " > reply to this comment </ FONT ></ A > " ;
2000-07-06 09:38:42 +00:00
}
### Display the comments:
if ( empty ( $mode ) || $mode == " threaded " ) {
2000-10-19 13:31:23 +00:00
$thread = discussion_childs ( $comment -> cid , $threshold );
$theme -> comment ( $comment -> userid , check_output ( $comment -> subject ), check_output ( $comment -> comment ), $comment -> timestamp , $comment -> url , $comment -> fake_email , discussion_score ( $comment ), $comment -> votes , $comment -> cid , $link , $thread );
2000-07-06 09:38:42 +00:00
}
else {
2000-10-19 13:31:23 +00:00
$theme -> comment ( $comment -> userid , check_output ( $comment -> subject ), check_output ( $comment -> comment ), $comment -> timestamp , $comment -> url , $comment -> fake_email , discussion_score ( $comment ), $comment -> votes , $comment -> cid , $link );
discussion_kids ( $comment -> cid , $mode , $threshold , $level );
2000-07-06 09:38:42 +00:00
}
}
2000-09-26 07:34:33 +00:00
print " <INPUT TYPE= \" hidden \" NAME= \" id \" VALUE= \" $sid\ " > \n " ;
print " <INPUT TYPE= \" submit \" NAME= \" op \" VALUE= \" Moderate comments \" > \n " ;
print " </FORM> \n " ;
2000-07-06 09:38:42 +00:00
}
2000-10-02 07:32:17 +00:00
function discussion_reply ( $pid , $sid ) {
2000-07-06 09:38:42 +00:00
global $anonymous , $user , $theme ;
### Extract parent-information/data:
if ( $pid ) {
$item = db_fetch_object ( db_query ( " SELECT comments.*, users.userid FROM comments LEFT JOIN users ON comments.author = users.id WHERE comments.cid = $pid " ));
2000-10-19 13:31:23 +00:00
$theme -> comment ( $item -> userid , check_output ( stripslashes ( $item -> subject )), check_output ( stripslashes ( $item -> comment )), $item -> timestamp , stripslashes ( $item -> url ), stripslashes ( $item -> fake_email ), discussion_score ( $comment ), $comment -> votes , $item -> cid , " reply to this comment " );
2000-07-06 09:38:42 +00:00
}
else {
2000-07-12 07:15:09 +00:00
$item = db_fetch_object ( db_query ( " SELECT stories.*, users.userid FROM stories LEFT JOIN users ON stories.author = users.id WHERE stories.status != 0 AND stories.id = $sid " ));
2000-07-06 09:38:42 +00:00
$theme -> article ( $item , " " );
}
### Build reply form:
$output .= " <FORM ACTION= \" discussion.php \" METHOD= \" post \" > \n " ;
### Name field:
2000-10-12 06:44:11 +00:00
if ( $user -> id ) {
2000-07-06 09:38:42 +00:00
$output .= " <P> \n " ;
$output .= " <B>Your name:</B><BR> \n " ;
$output .= " <A HREF= \" account.php \" > $user->userid </A> <FONT SIZE= \" 2 \" >[ <A HREF= \" account.php?op=logout \" >logout</A> ]</FONT> \n " ;
$output .= " </P> \n " ;
}
else {
$output .= " <P> \n " ;
$output .= " <B>Your name:</B><BR> \n " ;
$output .= " $anonymous\n " ;
$output .= " </P> \n " ;
}
### Subject field:
$output .= " <P> \n " ;
$output .= " <B>Subject:</B><BR> \n " ;
2000-10-02 07:32:17 +00:00
$output .= " <INPUT TYPE= \" text \" NAME= \" subject \" SIZE= \" 50 \" MAXLENGTH= \" 60 \" > \n " ;
2000-07-06 09:38:42 +00:00
$output .= " </P> \n " ;
### Comment field:
$output .= " <P> \n " ;
$output .= " <B>Comment:</B><BR> \n " ;
2000-10-02 07:32:17 +00:00
$output .= " <TEXTAREA WRAP= \" virtual \" COLS= \" 50 \" ROWS= \" 10 \" NAME= \" comment \" > " . check_input ( $user -> signature ) . " </TEXTAREA><BR> \n " ;
2000-07-06 09:38:42 +00:00
$output .= " </P> \n " ;
### Hidden fields:
$output .= " <INPUT TYPE= \" hidden \" NAME= \" pid \" VALUE= \" $pid\ " > \n " ;
$output .= " <INPUT TYPE= \" hidden \" NAME= \" sid \" VALUE= \" $sid\ " > \n " ;
### Preview button:
$output .= " <INPUT TYPE= \" submit \" NAME= \" op \" VALUE= \" Preview comment \" > (You must preview at least once before you can submit.) \n " ;
$output .= " </FORM> \n " ;
$theme -> box ( " Reply " , $output );
}
2000-10-02 07:32:17 +00:00
function comment_preview ( $pid , $sid , $subject , $comment ) {
2000-07-06 09:38:42 +00:00
global $anonymous , $user , $theme ;
### Preview comment:
2000-10-12 06:44:11 +00:00
if ( $user -> id ) $theme -> comment ( " " , check_output ( stripslashes ( $subject )), check_output ( stripslashes ( $comment )), time (), " " , " " , " " , " " , " " , " reply to this comment " );
2000-10-19 13:31:23 +00:00
else $theme -> comment ( $user -> userid , check_output ( stripslashes ( $subject )), check_output ( stripslashes ( $comment )), time (), stripslashes ( $user -> url ), stripslashes ( $user -> fake_email ), " " , " " , " " , " reply to this comment " );
2000-07-06 09:38:42 +00:00
### Build reply form:
$output .= " <FORM ACTION= \" discussion.php \" METHOD= \" post \" > \n " ;
### Name field:
2000-10-12 06:44:11 +00:00
if ( $user -> id ) {
2000-07-06 09:38:42 +00:00
$output .= " <P> \n " ;
$output .= " <B>Your name:</B><BR> \n " ;
$output .= " <A HREF= \" account.php \" > $user->userid </A> <FONT SIZE= \" 2 \" >[ <A HREF= \" account.php?op=logout \" >logout</A> ]</FONT> \n " ;
$output .= " </P> \n " ;
}
else {
$output .= " <P> \n " ;
$output .= " <B>Your name:</B><BR> \n " ;
$output .= " $anonymous\n " ;
$output .= " </P> \n " ;
}
### Subject field:
$output .= " <P> \n " ;
$output .= " <B>Subject:</B><BR> \n " ;
2000-10-02 07:32:17 +00:00
$output .= " <INPUT TYPE= \" text \" NAME= \" subject \" SIZE= \" 50 \" MAXLENGTH= \" 60 \" VALUE= \" " . check_input ( $subject ) . " \" > \n " ;
2000-07-06 09:38:42 +00:00
$output .= " </P> \n " ;
### Comment field:
$output .= " <P> \n " ;
$output .= " <B>Comment:</B><BR> \n " ;
2000-10-02 07:32:17 +00:00
$output .= " <TEXTAREA WRAP= \" virtual \" COLS= \" 50 \" ROWS= \" 10 \" NAME= \" comment \" > " . check_input ( $comment ) . " </TEXTAREA><BR> \n " ;
2000-07-06 09:38:42 +00:00
$output .= " </P> \n " ;
### Hidden fields:
$output .= " <INPUT TYPE= \" hidden \" NAME= \" pid \" VALUE= \" $pid\ " > \n " ;
$output .= " <INPUT TYPE= \" hidden \" NAME= \" sid \" VALUE= \" $sid\ " > \n " ;
if ( empty ( $subject )) {
$output .= " <P> \n " ;
$output .= " <FONT COLOR= \" red \" ><B>Warning:</B></FONT> you did not supply a <U>subject</U>. \n " ;
$outout .= " </P> \n " ;
}
2000-10-02 07:32:17 +00:00
### Preview and submit button:
$output .= " <P> \n " ;
$output .= " <INPUT TYPE= \" submit \" NAME= \" op \" VALUE= \" Preview comment \" > \n " ;
$output .= " <INPUT TYPE= \" submit \" NAME= \" op \" VALUE= \" Post comment \" > \n " ;
$output .= " </FORM> \n " ;
$output .= " </P> \n " ;
2000-07-06 09:38:42 +00:00
$theme -> box ( " Reply " , $output );
}
2000-10-02 07:32:17 +00:00
function comment_post ( $pid , $sid , $subject , $comment ) {
2000-07-06 09:38:42 +00:00
global $user , $theme ;
### Check for fake threads:
$fake = db_result ( db_query ( " SELECT COUNT(*) FROM stories WHERE id = $sid " ), 0 );
### Check for duplicate comments:
$duplicate = db_result ( db_query ( " SELECT COUNT(*) FROM comments WHERE pid = ' $pid ' AND sid = ' $sid ' AND subject = ' " . addslashes ( $subject ) . " ' AND comment = ' " . addslashes ( $comment ) . " ' " ), 0 );
if ( $fake != 1 ) {
2000-10-02 07:32:17 +00:00
watchdog ( 3 , " attemp to insert fake comment " );
2000-07-06 09:38:42 +00:00
$theme -> box ( " fake comment " , " fake comment: $fake " );
}
elseif ( $duplicate != 0 ) {
2000-10-02 07:32:17 +00:00
watchdog ( 3 , " attemp to insert duplicate comment " );
2000-07-06 09:38:42 +00:00
$theme -> box ( " duplicate comment " , " duplicate comment: $duplicate " );
}
else {
2000-10-02 07:32:17 +00:00
### Validate subject:
$subject = ( $subject ) ? $subject : substr ( $comment , 0 , 29 );
2000-07-06 09:38:42 +00:00
2000-10-02 07:32:17 +00:00
### Add comment to database:
2000-10-21 13:59:27 +00:00
db_insert ( " INSERT INTO comments (pid, sid, author, subject, comment, hostname, timestamp) VALUES ( $pid , $sid , ' $user->id ', ' " . addslashes ( $subject ) . " ', ' " . addslashes ( $comment ) . " ', ' " . getenv ( " REMOTE_ADDR " ) . " ', ' " . time () . " ') " );
2000-10-02 07:32:17 +00:00
### Compose header:
header ( " Location: discussion.php?id= $sid " );
2000-07-06 09:38:42 +00:00
}
}
2000-10-10 10:51:00 +00:00
include " includes/theme.inc " ;
2000-07-06 09:38:42 +00:00
2000-09-26 07:34:33 +00:00
switch ( $op ) {
2000-07-06 09:38:42 +00:00
case " Preview comment " :
$theme -> header ();
2000-10-02 07:32:17 +00:00
comment_preview ( $pid , $sid , $subject , $comment );
2000-07-06 09:38:42 +00:00
$theme -> footer ();
break ;
case " Post comment " :
2000-10-02 07:32:17 +00:00
comment_post ( $pid , $sid , $subject , $comment );
2000-07-06 09:38:42 +00:00
break ;
case " reply " :
$theme -> header ();
2000-10-02 07:32:17 +00:00
discussion_reply ( $pid , $sid );
$theme -> footer ();
break ;
case " Save " :
2000-10-19 13:31:23 +00:00
discussion_settings ( $mode , $order , $threshold );
2000-10-02 07:32:17 +00:00
$theme -> header ();
discussion_display ( $id , $pid , $sid );
2000-07-06 09:38:42 +00:00
$theme -> footer ();
break ;
2000-09-26 07:34:33 +00:00
case " Moderate comments " :
discussion_moderate ( $moderate );
2000-10-02 07:32:17 +00:00
$theme -> header ();
discussion_display ( $id , $pid , $sid );
$theme -> footer ();
break ;
2000-07-06 09:38:42 +00:00
default :
$theme -> header ();
2000-10-02 07:32:17 +00:00
discussion_display ( $id , $pid , $sid );
2000-07-06 09:38:42 +00:00
$theme -> footer ();
}
?>