<?php


  // contents of function MailUsers

/*

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 $control, $subjectrow; 
 
  // don't do it if they don't permit it
  if (!$control ['allow_notification'])
    return;
 
  $forum_name = $control ['forum_name'];
  $forum_url = $control ['forum_url'];

  // see if people want notification about it
  // select all users that want notification about that topic/subject (except ourselves)
  
  if ($new)
    $query = "SELECT username, email, notify_add, notify_new_thread, bbuser.bbuser_id "
           . "FROM bbuser LEFT JOIN bbnotify_topic "
           . "ON bbnotify_topic.bbuser_id = bbuser.bbuser_id "  // match on the relevant user
           . "WHERE bbuser.bbuser_id <> $bbuser_id "  // don't send to ourselves
           . "  AND  (bbuser.bounced IS NULL OR bbuser.bounced = 0) "  // can we send it? yes we can!
           . "  AND (  bbnotify_topic.bbtopic_id = $bbtopic_id "       // match in bbnotify_topic
           . "     OR  bbuser.notify_new_thread <> 0 "   // OR they want all new subjects
           . "      ) "
           . "GROUP BY bbuser.bbuser_id ";    // only want each user once
                                        
  else
    $query = "SELECT username, email, notify_add "
           . "FROM bbuser, bbnotify_subject "
           . "WHERE bbsubject_id = $bbsubject_id "
           . "  AND   bbuser.bbuser_id = bbnotify_subject.bbuser_id "
           . "  AND   bbuser.bbuser_id <> $bbuser_id "                         
           . "  AND  (bbuser.bounced IS NULL OR bbuser.bounced = 0)";
             
  $result = mysql_query ($query) 
      or Problem ("Select of notification failed: " . mysql_error ());

  while ($row = mysql_fetch_array ($result))
    {
    $notifyname = $row ['username'];
    $notifyemail = $row ['email'];

    // skip anyone who will already have got notification
    if ($row ['notify_add'])
      continue;

    $removal = "To stop receiving further notifications, please click on the link below:\n\n";
          
    if ($row ['notify_new_thread'] && $new)
      $removal .= "  $forum_url/bbuseredit.php?action=amend&bbuser_id=" . $row ['bbuser_id'];
    else
      {
      $removal .= "  $forum_url/bbshowpost.php?action=unsubscribe&";
      if ($new)
        $removal .= "bbtopic_id=$bbtopic_id";
      else
        $removal .= "bbsubject_id=$bbsubject_id";      
      } // end of ordinary subscription

    $removal .= "\n\n";
      
    // send mail message
    
    $mailresult = mail ($notifyname . " <" . $notifyemail . ">", 
          "$subject",
          "Hi $notifyname,\n\n" 
        . "$username has posted a new message about: $subject.\n\n"
        . "This was in the section: " . 
           $subjectrow ['section_name'] .           
           " -> " .
           $subjectrow ['topic_name'] .
           "\n\n"
        . "You can view this at:\n\n  $forum_url$link\n\n\n\n"
        . $removal    // how to stop getting them
        . $control ['email_signature'],
        // mail header
        "From: " . $control ['email_from'] . "\r\n"
      . "Content-type: text/plain\r\n"
      . "X-mailer: PHP/" . phpversion()
        );
    
    } // end of mail loop
      
  mysql_free_result ($result);
  
  /*
  
  I decided not to use this, because I think you need to subscribe to a *topic* first
   which will notify you of the *first* post to that topic, and then if you like it
   you subscribe to that *subject* as a separate operation.
   
  // this slightly trickier select finds all users that want notification about this
  // *subject* except those that also want notification about the *topic* - after all,
  // you don't want *two* notifications
  
  $query = "SELECT * FROM bbuser, bbnotify_subject "
         . "LEFT JOIN bbnotify_topic "
         . "  ON  bbnotify_topic.bbtopic_id = $bbtopic_id "
         . "  AND bbnotify_topic.bbuser_id = bbnotify_subject.bbuser_id "
         . "WHERE bbsubject_id = $bbsubject_id "
         . "  AND   bbnotify_topic.bbuser_id IS NULL "
         . "  AND   bbuser.bbuser_id = bbnotify_subject.bbuser_id  "                        
         . "  AND   bbuser.bbuser_id <> $bbuser_id "                         
         . "  AND  (bbuser.bounced IS NULL OR bbuser.bounced = 0)";
         
   */
  
?>
