<?php


// contents of function DeleteMessage

/*

Copyright © 2001 Nick Gammon.

  This program is free software; you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation; either version 2 of the License, 
  or (at your option) any later version. 

  This program is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  See the GNU General Public License for more details. 

*/


  global $bbpost_id, $bbsubject_id, $foruminfo, $control, $username, $subjectrow;
    
  if (!$bbpost_id)
    Problem ("No message ID specified");

  if (!$control ['allow_deletes'])
    Problem ("Deleting of messages is not permitted at present");
        
  $query = "SELECT * FROM bbpost, bbsubject, bbtopic "
         . "WHERE bbpost_id = $bbpost_id "
         . "  AND   bbsubject.bbsubject_id = bbpost.bbsubject_id "
         . "  AND   bbtopic.bbtopic_id = bbsubject.bbtopic_id ";
    
//  echo $query;
                        
  $result = mysql_query ($query) 
      or Problem ("Select of message failed: " . mysql_error ());
    
  if (!($row = mysql_fetch_array ($result)))
    Problem ("Message $bbpost_id is not on file");
  
  if ($row ['bbuser_id'] != $foruminfo ['bbuser_id'] && 
                          !($foruminfo ['admin'] ||
                            $foruminfo ['moderator_topic'] == $row ['bbtopic_id'] ||
                            $foruminfo ['moderator_section'] == $row ['bbsection_id']))
    Problem ("You did not post this message");

  $username = $foruminfo ['username'];  // for mail message
  
  // save subject so we can redisplay topic
  $bbsubject_id = $row ['bbsubject_id'];
  
  mysql_free_result ($result);

  $query = "DELETE FROM bbpost "
         . "WHERE bbpost_id = $bbpost_id";
  $result = mysql_query ($query)
    or Problem ("Delete of message $bbpost_id failed: " . mysql_error ());
    
  $bbpost_id = "";    // message is gone now

// see if whole subject has gone

  $query = "SELECT count(*) FROM bbpost "
         . "WHERE bbsubject_id = $bbsubject_id ";
    
//  echo $query;
                        
  $result = mysql_query ($query) 
      or Problem ("Select of count failed: " . mysql_error ());
    
  if (!($row = mysql_fetch_row ($result)))
    Problem ("Could not get count of messages");
        
  mysql_free_result ($result);

  // get subject name and topicid for mail message
  LookupSubject ();
  $subject = $subjectrow ['subject_name'];
  $bbtopic_id = $subjectrow ['bbtopic_id'];
    
// no messages left? delete subject itself

  if ($row [0] == 0)
    {
    $query = "DELETE FROM bbsubject "
           . "WHERE bbsubject_id = $bbsubject_id";
    $result = mysql_query ($query)
      or Problem ("Delete of subject $bbsubject_id failed: " . mysql_error ()); 

    // now we don't have this subject
    $bbsubject_id = "";   

    MailAdmins ("Deleted subject: $subject", "deleted the subject: $subject", 
                "/bbshowpost.php?bbtopic_id=$bbtopic_id",
                "notify_delete");
    
    } // end of no messages left
  else
    {
    // update record for that subject, to make sorting easier
    UpdateSubject ($bbsubject_id);
    
    MailAdmins ("Deleted message: $subject", "deleted a message about: $subject", 
                "/bbshowpost.php?bbsubject_id=$bbsubject_id",
                "notify_delete");
    }    
    
?>
