<?
/***
* pLiMa - php List Manager
* Copyright (C) 2003 Jinn Koriech (hide@address.com)
*
* This file is part of pLiMa.
*
* pLiMa 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
* any later version.
*
* pLiMa 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 pLiMa; if not, visit http://www.gnu.org or write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
*/
// Lets create an SQL query for the criteria
$fields = getFields($_SESSION['MALI']);
$field_count = count($fields);
for ( $idx = 0; $idx < $field_count; $idx++ ) {
$field = $fields[$idx];
$fieldSelector = $field."Selector";
if ( $_SESSION[$fieldSelector] == 'term' ) {
$fieldTerm = $field."Term";
$criteria[$idx] = "$field LIKE '%".$_SESSION[$fieldTerm]."%'";
} elseif ( $_SESSION[$fieldSelector] == 'enum' ) {
$fieldArray = $field."Array";
$enum_count = count($_SESSION[$fieldArray]);
for ( $adx=0; $adx < $enum_count; $adx++ ) {
$enum[] = $_SESSION[$fieldArray][$adx];
}
$enum_IN = implode("', '", $enum);
$criteria[] = "$field IN ('" . $enum_IN . "')";
}
}
$criteria_sql = "SELECT * FROM ".$_SESSION['MALI']." WHERE 1";
for ( $idx=0; $idx < count($criteria); $idx++ ) {
$criteria_sql .= " AND " . $criteria[$idx];
}
$recipients = db_query($criteria_sql, "SND10");
$num_recipients = db_num_rows($recipients);
// Check the send time
function checkSendTime($sendTime, $sendTimeAt) {
if ( $sendTime == 'now' ) {
$send_time = date("G:i", time()+60);
$sendAt = date("Y-m-d G:i", time()+60);
} else {
$send_time = $sendTimeAt;
$sendAt = date("Y-m-d", time()+60);
$sendAt .= " " . $send_time;
}
// Check that there isn't another item scheduled for the same time too...
$sql = "SELECT * FROM lima_queue WHERE sendAt = '".$sendAt."';";
$result = db_query($sql,"SND20");
if ( db_num_rows($result) > 0 ) {
$row = db_fetch_array($result,0);
$sql = "SELECT DATE_ADD('".$row['sendAt']."', INTERVAL 15 SECOND) AS sendAt";
$result = db_query($sql,"SND30");
$row = db_fetch_array($result,0);
$sendAt = $row['sendAt'];
$sendTimeAt = substr($sendAt,strrpos($sendAt, " "), 9);
list($sendAt, $sendTimeAt) = checkSendTime($sendAt, $sendTimeAt);
}
return array($sendAt, $sendTimeAt);
}
list($sendAt, $sendTimeAt) = checkSendTime($sendTime, $sendTimeAt);
// Put the broadcast into the queue
$sql = "INSERT INTO lima_queue (list_name, sendAt, recipients, criteria, status, subject, message, salutation, usename, usenum, usesig)
VALUES (
'".$_SESSION['MALI']."',
'".$sendAt."',
'".$num_recipients."',
'".mysql_escape_string($criteria_sql)."',
'W',
'".$_POST['subject']."',
'".$_POST['body']."',
'".$_POST['beforename']."',
'".$_POST['usename']."',
'".$_POST['usenum']."',
'".$_POST['usesig']."'
)";
$result = mysql_query($sql); // This particular command needs to be direct so we can get the insert id.
$insert_id = mysql_insert_id() or die("MySQL said:".mysql_error());
// Give the system the command to send the broadcast, and get the job_id
$at_cmd = "at $send_time -f ".APP_ROOT."/restricted/send.sh";
$at_return = `${at_cmd}`;
?>
<h1>Message queued</h1>
<p>Your message has been queued to be sent at <? echo $send_time; ?>. There are <? echo $num_recipients; ?> addressses that will recieve this message.</p>
<p>This will be carried out in the background. You can view the progress by viewing the <a href='sendq.phtml'>Send Queue</a>.</p>
<?
require(APP_ROOT."/inc/foot.inc.php");
?>