<?php

/*

Copyright © 2001 Nick Gammon.

  Author: Nick Gammon <nick@gammon.com.au>
  Web:    http://www.gammon.com.au/
  Date:   February 2001

  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. 

  You should have received a copy of the GNU General Public License 
  along with this program; if not, write to 

  The Free Software Foundation, Inc., 
  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


  The Free Software Foundation maintains a web page at: http://www.fsf.org

  See the file gpl.txt for the full GNU General Public License.

*/

require ("config.php");
require ($CONFIG_PATH . "general_config.php");
require ($INCLUDE_DIRECTORY . "common.php");

Init ("Send mail message", "forum, mail, message"); 

$forum_name = $control ['forum_name'];

?>

<h1><?php echo $forum_name; ?></h1>

<?php

CheckField ("user identifier", $bbuser_id, false);

if (!$foruminfo)
  Problem ("You have not logged in");
   
$query = "SELECT * "
       . "FROM bbuser "
       . "WHERE bbuser_id = $bbuser_id";
                              
$result = mysql_query ($query) 
    or Problem ("Select of user failed: " . mysql_error ());

if (!($row = mysql_fetch_array ($result)))
  Problem ("User id $bbuser_id is not on file");
  
mysql_free_result ($result);

if (!$control ['allow_messages']) 
  Problem ("Sending messages is disabled at present");

if (!$row ['show_email'] && !$row ['allow_mail'] && !$foruminfo ['admin'])
  Problem ("That user does not want to receive messages");

if ($row ['bounced'])
  Problem ("Email to that user has bounced");

if (!$foruminfo ['show_email'] && !$foruminfo ['allow_mail'] && !$foruminfo ['admin'])
  Problem ("You cannot send messages if you do not permit replies");

if ($foruminfo ['bounced'])
  Problem ("Email to you has bounced - please correct your email address");
  
echo "<hr>\n";

echo "<h2>Send mail message</h2>\n";

if (!$action)
  {

  shLink ($biolink, htmlentities ($row ['username']), "bbshowbio.php", "bbuser_id=$bbuser_id", false);
  
?>

<p>Use this form to send a message to <b><?php echo $biolink; ?></b>.</p>

<p>Both subject and message body are required.<br> 
Press the &quot;Send&quot; button when finished, to send your message.</p>
<?php  

  // if they disclose their email address they probably don't mind a direct reply
  
  $allow_reply = $foruminfo ['show_email'];
  
  }   // end of no action 
     
$specials = array
  (
  'subject'     => array (
                'input' => 'subject',
                'description' => 'Subject:',
                'comment' => "Enter the subject of the message.",
                'size' => 80,
                'maxlength' => 80
                ),    // subject
  'message'     => array (
                'input' => 'message',
                'type' => 'multiline',                
                'description' => 'Message:',
                'comment' => 'Enter your message, maximum of 6,000 characters.',
                'rows' => 20,
                'cols' => 72
                ),    // message
  'allow_reply'     => array (
                'input' => 'allow_reply',
                'type' => 'bool',
                'description' => 'Allow reply:',
                'comment' => "If checked, your email address WILL be disclosed to the recipient "
                           . " so they can reply directly.\n"
                           . "If unchecked they can reply using this form.",
                ),    // allow_reply
  );  // end of specials


if ($action == "Send")
  {
  ValidateOneField ('subject', 'text', true, 80, $specials);
  ValidateOneField ('message', 'text', true, 6000, $specials);
  ValidateOneField ('allow_reply', 'bool', false, 1, $specials);
      
  } // end of Send requested

if ($have_error)
  {
?>
<hr>
<h2>Error in submission</h2>
<p>There were some errors in processing your submission.</p>
<p>Please try again. Thank you.</p> 
  
<?php  
  } // end of error in fields
else
  if ($action == "Send")
    {

    $forum_url = $control ['forum_url'];


    $bbuser_id = $row ['bbuser_id'];
    $username = $row ['username'];
    $email = $row ['email'];
    if ($allow_reply)
      {
      $email_from = $foruminfo ['username'] . " <" .$foruminfo ['email'] . ">";
      $howtoreply = "";
      }      
    else 
      {
      $email_from = $control ['email_from'];
      $howtoreply = "You can reply by visiting:\n\n  $forum_url/bbsendmail.php?bbuser_id="
                . $foruminfo ['bbuser_id']
                . "\n\n";
      }
    $from_name = $foruminfo ['username'];
         
    $today = strftime ("%Y-%m-%d", utctime());   // today's date
   
    // send mail message
        
    $mailresult = mail ($username . " <" . $email . ">", 
          "$subject",
          "Hi $username,\n\n" 
        . "This message is from $from_name at $forum_name.\n\n"
        . "--------------------------------------------------------\n"
        . "$message\n\n"
        . "--------------------------------------------------------\n"
        . $howtoreply
        . "You can visit the forum at:\n\n  $forum_url/bbshowpost.php\n\n"
        . $control ['email_signature'],
        // mail header
        "From: " . $email_from . "\r\n"
      . "Content-type: text/plain\r\n"
      . "X-mailer: PHP/" . phpversion()
        );
    
    if (!$mailresult)
      Problem ("An error occurred sending the email message");
    
    echo "<p>Your message is now being sent to <b>" 
          . htmlspecialchars ($username) . "</b></p>\n";
      
// update record for sending user (count of mails sent, date of last mail sent)

    $date_now = strftime ("%Y-%m-%d %H:%M:%S", utctime());
    
    $query = "UPDATE bbuser SET count_mail_sent = count_mail_sent + 1, "
           . "  date_mail_sent = '$date_now' "
           . "WHERE bbuser_id = " . $foruminfo ['bbuser_id'];
    $result = mysql_query ($query)
      or Problem ("Update of bbuser failed: " . mysql_error ());

// update record for receiving user (count of mails received, date of last mail received)

    $query = "UPDATE bbuser SET count_mail_received = count_mail_received + 1, "
           . "  date_mail_received = '$date_now' "
           . "WHERE bbuser_id = $bbuser_id";
    $result = mysql_query ($query)
      or Problem ("Update of bbuser failed: " . mysql_error ());
      
    echo "<hr><p>\n";
    hLink ("View list of registered users", "bbuserlist.php", "", false);
    echo "</p><p>";
    hLink ("Go to $forum_name", "bbshowpost.php");
    echo "</p>";
    MessageTail (false);
    die ();
      
    
  } // end of Send 

echo "<form METHOD=post ACTION=\"$PHP_SELF\">\n";

$table ['subject'] = $subject;
$table ['message'] = $message;
$table ['allow_reply'] = $allow_reply;
  
$params = array
  (
  'LH' => 'valign=top bgcolor="#ADD8E6" align=right',
  'RH' => 'bgcolor="#FAF0E6" align=left'
  );

if ($bbuser_id == $foruminfo ['bbuser_id'])
  echo "<p><b>WARNING - you are sending this message to yourself!</b></p>\n";
        
ShowTable ($table, $params, $specials);

echo "<p>";
echo "<input Type=submit Name=action Value=\"Send\">";
echo "<input type=hidden name=action Value=\"Send\">";
echo "</p>";
?>


<input type=hidden name=bbuser_id value=<?php echo ($bbuser_id); ?>>
<?php FormSession (); ?>

</form>
    
<?php

echo "<hr><p>\n";
hLink ("View list of registered users", "bbuserlist.php", "", false);
echo "</p><p>";
hLink ("Back to $forum_name", "bbshowpost.php");
echo "</p>";

MessageTail (false);
?>    

