Location: PHPKode > projects > Slashster > slashster/cron/slashster_mailschedule.php
#!/usr/local/bin/php
<?

# Slashster: Open Source Friend of a Friend Software
# Copyright 2004 Mark El-Wakil
# Contact: hide@address.com
# http://seventhcycle.net
#
# This file is part of Slashster.
#
# Slashster 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.
#
# Slashster 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 Slashster; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   set_time_limit(0);

   require("cronpth.php");       
   $mpage = new PageClass("sqlonly");

   $x = 0;


   $fdir = $mpage->sys[siteroot] . "cron";

   $pid =  posix_getpid();
   $fd = fopen("$fdir/schedule1.pid", "w");
   fwrite($fd, $pid);
   fclose($fd);

   $touched = filemtime($mpage->sys[siteroot] . "cron/tmp/ismail.txt");
   $ntouch  = "";


   while(1)
   {
     echo "$x";

     if ($ntouch != $touched)
     {
        Scheduler();
        echo "mail!";
        $touched = $ntouch;
     }

     echo "\n";
     $x++;
     sleep(5);

     clearstatcache();
     $ntouch = filemtime($mpage->sys[siteroot] . "cron/tmp/ismail.txt");
   }


function Scheduler()
{
   global $mpage;

   $sql = "select Scheduler.*, UserTable.DefMailInbox from Scheduler
           left join UserTable on UserTable.Id = Scheduler.MailTo
          ";
   $r = $mpage->DoQuery($sql,1);

   $rr = $r;
   for ($x = 0; $x < sizeof($rr); $x++) $rr[$x] = $rr[$x][Id];
   
   $rr = (sizeof($r)) ? join(", ", $rr) : "0";
   
   $sql = "delete from Scheduler where Id IN ($rr)";
   $mpage->DoQuery($sql);


   foreach ($r as $s)
   {
       $body = addslashes($s[Body]);
       $subj = addslashes($s[Subject]);

       if ($s[Pbox])
       {

          $sql = "insert into PrivateMessage set
                    Start     = $s[MailFrom],
                    End       = $s[MailTo],
                    Owner     = $s[MailTo],
                    Folder    = $s[DefMailInbox],
                    Subject   = \"$subj\",
                    Body      = \"$body\",
                    DateSent  = NOW(),
                    Expires   = $s[Expire],
                    Encrypted = $s[Encrypted]
                 ";

          $mpage->DoQuery($sql);
      }


       $sql = "update UserTable set NewMsgs = NewMsgs + 1 where Id = $s[MailTo]";
       $mpage->DoQuery($sql);

       $sql = "lock tables UserTable write";
       $mpage->DoQuery($sql);

       $sql = "select * from UserTable where Id = $s[MailTo]";
       $gg = $mpage->DoQuery($sql,1);
       $gg = $gg[0];

       if ($s[DeliverNow])
       {
            slashmail($gg[Email], $s[Subject], strip_tags($s[Body]), "",
            $mpage->sys[sitename] . " Bot <robot@".$mpage->sys[sitedomain]
            . ">", "robot@".$mpage->sys[sitedomain].""); 

            $sql = "update UserTable set LastNotify = NOW() where Id = $s[MailTo]";
            $mpage->DoQuery($sql);
       }
       elseif ($gg[MailOften] == 1)
       {
            MailPerson($gg[Fname], $gg[Lname], $gg[Email]);
            $sql = "update UserTable set LastNotify = NOW() where Id = $s[MailTo]";
            $mpage->DoQuery($sql);
       }

       $sql = "update UserTable set NewestMessage = NOW() where Id = $s[MailTo]";
       $mpage->DoQuery($sql);
    
       $sql = "unlock tables";
       $mpage->DoQuery($sql);
   }

}

function MailPerson($fname, $lname, $email)
{
   global $mpage;

   $arr = array();
   $arr["fname"]   = $fname;
   $arr["lname"]   = $lname;
   $arr["email"]   = $email;

   $msg = $mpage->PrintSnippit("NewMsg", $arr);

   $eml = "$fname $lname <$email>";

   $headers= "X-Mailer: PHP/" . phpversion() . "\r\n";

   slashmail($eml, "New Message In Your ".$mpage->sys[sitename]." Mailbox", $msg, $headers,
            $mpage->sys[sitename]." Bot <robot@".$mpage->sys[sitedomain].">", "robot@".$mpage->sys[sitedomain]);

}


function slashmail($to, $subject, $txt, $headers, $from, $retpath)
{
   global $mpage;

   if ($mpage->sys[chretpath])
      $sendmail = "/usr/sbin/sendmail -t -f " . $mpage->sys[bounceadr];
   else
      $sendmail = "/usr/sbin/sendmail -t";

   $fd = popen($sendmail, "w");
   fputs($fd, "Reply-To: <$retpath>\r\n");
   fputs($fd, "From: $from\r\n");
   fputs($fd, "To: $to\r\n");
   fputs($fd, "Subject: $subject\r\n");
   fputs($fd, $headers);
   fputs($fd, $txt . "\r\n");
   pclose($fd);

}


?>
Return current item: Slashster