<?php
require ("sms_gateway.ini");
$service_port = $SENDER_SERVER_PORT;
$service_addr = $SENDER_SERVER_IP;
$conn_checker = pg_connect ("host=$SQL_SERVER_HOST port=$SQL_SERVER_PORT dbname=$SQL_SERVER_TABLE user=$SQL_SERVER_USER password=$SQL_SERVER_PASSWORD");
if ($conn_checker == false)
{
pg_close ($conn_checker);
continue;
}
while (1)
{
sleep (5);
// $nowis = gettimeofday();
// $thesec = $nowis['sec'];
$thesec = time ();
$sql_sendout = "SELECT account, reserve, target, content, series, mobile, fileidx FROM reserve WHERE (reserve < $thesec);";
$result_sendout = pg_exec ($conn_checker, $sql_sendout);
if ($result_sendout == false)
{
@pg_freeresult ($result_sendout);
$conn_checker = pg_connect ("host=$SQL_SERVER_HOST port=$SQL_SERVER_PORT dbname=$SQL_SERVER_TABLE user=$SQL_SERVER_USER password=$SQL_SERVER_PASSWORD");
if ($conn_checker == false)
{
print "connect fail..\n";
pg_close ($conn_checker);
}
continue;
}
$rows_sendout = pg_numrows ($result_sendout);
if ($rows_sendout <= 0)
{
pg_freeresult ($result_sendout);
continue;
}
$fp_sender = fsockopen ($SENDER_SERVER_IP, $SENDER_SERVER_PORT, $errno_sender, $errstr_sender, 30);
if (!$fp_sender)
{
echo "CONNECT ERROR: $errstr_sender ($errno_sender)<br>\n";
continue;
}
fputs ($fp_sender, "LOGIN:S:$GATEWAY_ACCOUNT:$GATEWAY_PASSWORD\n");
$ack_reply = fgets ($fp_sender, 128);
list ($ack_content, $ack_number) = split (":", $ack_reply);
if ($ack_number == 200)
{
// fail
fclose ($fp_sender);
continue;
}
if ($result_sendout != false)
{
for ($i = 0; $i < $rows_sendout; $i++)
{
$sendout_account = pg_result ($result_sendout, $i, 0);
$account_tail = $sendout_account % 100;
if ($account_tail < 10)
$account_tail = '0' . $account_tail;
$sendout_reserve = pg_result ($result_sendout, $i, 1);
$sendout_target = pg_result ($result_sendout, $i, 2);
$sendout_target = rtrim ($sendout_target);
$sendout_content = pg_result ($result_sendout, $i, 3);
$sendout_series = pg_result ($result_sendout, $i, 4);
$sendout_mobile = pg_result ($result_sendout, $i, 5);
$sendout_fileidx = pg_result ($result_sendout, $i, 6);
$wordlen = strlen ($sendout_content);
unset ($newword);
for ($wordptr = 0; $wordptr < $wordlen; $wordptr++)
{
if ($sendout_content[$wordptr] == ':')
$newword .= "\xa1G";
else
$newword .= $sendout_content[$wordptr];
}
$sendout_info = sprintf ("%.124s", $newword);
$series_number = sprintf ("%9s%01d%012d%2s", $GATEWAY_LIMITKEY, $GATEWAY_TYPE, $sendout_series, $account_tail);
$outgoing = "SEND:" . $series_number . ":" . $sendout_target . ":(" . $sendout_mobile . ")" . $sendout_info . ":BG:Now\n";
$dayis = getdate ();
print "[ " . $dayis['year'] . "/" . $dayis['month'] . "/" . $dayis['mday'] . " ";
print $dayis['hours'] . ":" . $dayis['minutes'] . ":" . $dayis['seconds'] . " ] ";
print $outgoing;
fputs ($fp_sender, $outgoing);
$send_reply = fgets ($fp_sender);
list ($srp_content, $srp_number) = split (":", $send_reply);
if ($srp_number == 200)
continue;
$sql_delive = "INSERT INTO delive$account_tail (account, sendout, target, content, result, series, fileidx, finaldate) VALUES ('$sendout_account', $thesec, '$sendout_target', '$sendout_content', 0, $sendout_series, '$sendout_fileidx', $thesec);";
pg_exec ($conn_checker, $sql_delive);
$sql_drop = "DELETE FROM reserve WHERE (series = $sendout_series);";
pg_exec ($conn_checker, $sql_drop);
}
}
fclose ($fp_sender);
pg_freeresult ($result_sendout);
}
pg_close ($conn_checker);
?>