function sendsmsd($single_queue = '', $sendsmsd_limit = 0, $sendsmsd_offset = 0) { global $core_config; if ($single_queue) { $queue_sql = "AND queue_code='" . $single_queue . "'"; // logger_print("single queue queue_code:".$single_queue, 2, "sendsmsd"); } $sendsmsd_limit = (int) $sendsmsd_limit; if ($sendsmsd_limit > 0) { $sql_limit = "LIMIT " . $sendsmsd_limit; } $sendsmsd_offset = (int) $sendsmsd_offset; if ($sendsmsd_offset > 0) { $sql_offset = "OFFSET " . $sendsmsd_offset; } $db_query = "SELECT * FROM " . _DB_PREF_ . "_tblSMSOutgoing_queue WHERE flag='0' " . $queue_sql . " " . $sql_limit . " " . $sql_offset; // logger_print("q: ".$db_query, 3, "sendsmsd"); $db_result = dba_query($db_query); while ($db_row = dba_fetch_array($db_result)) { $c_queue_id = $db_row['id']; $c_queue_code = $db_row['queue_code']; $c_sender_id = addslashes(trim($db_row['sender_id'])); $c_footer = addslashes(trim($db_row['footer'])); $c_message = addslashes(trim($db_row['message'])); $c_uid = $db_row['uid']; $c_gpid = $db_row['gpid']; $c_sms_type = $db_row['sms_type']; $c_unicode = $db_row['unicode']; $c_sms_count = $db_row['sms_count']; $c_schedule = $db_row['datetime_scheduled']; $c_smsc = $db_row['smsc']; $c_current = core_get_datetime(); // logger_print("delivery datetime qeueue:".$c_queue_code." scheduled:".$c_schedule." current:".$c_current, 3, "sendsmsd"); if (strtotime($c_current) >= strtotime($c_schedule)) { logger_print("start processing queue_code:" . $c_queue_code . " sms_count:" . $c_sms_count . " uid:" . $c_uid . " gpid:" . $c_gpid . " sender_id:" . $c_sender_id, 2, "sendsmsd"); $counter = 0; $db_query2 = "SELECT * FROM " . _DB_PREF_ . "_tblSMSOutgoing_queue_dst WHERE queue_id='{$c_queue_id}' AND flag='0'"; $db_result2 = dba_query($db_query2); while ($db_row2 = dba_fetch_array($db_result2)) { $counter++; // queue_dst ID is SMS Log ID $c_smslog_id = $db_row2['id']; $c_dst = $db_row2['dst']; $c_flag = 2; $c_ok = false; logger_print("sending queue_code:" . $c_queue_code . " smslog_id:" . $c_smslog_id . " to:" . $c_dst . " sms_count:" . $c_sms_count . " counter:" . $counter, 2, "sendsmsd"); $ret = sendsms_process($c_smslog_id, $c_sender_id, $c_footer, $c_dst, $c_message, $c_uid, $c_gpid, $c_sms_type, $c_unicode, $c_queue_code, $c_smsc); $c_dst = $ret['to']; if ($ret['status']) { $c_ok = true; $c_flag = 1; } logger_print("result queue_code:" . $c_queue_code . " to:" . $c_dst . " flag:" . $c_flag . " smslog_id:" . $c_smslog_id, 2, "sendsmsd"); $db_query3 = "UPDATE " . _DB_PREF_ . "_tblSMSOutgoing_queue_dst SET flag='{$c_flag}' WHERE id='{$c_smslog_id}'"; $db_result3 = dba_query($db_query3); $ok[] = $c_ok; $to[] = $c_dst; $smslog_id[] = $c_smslog_id; $queue[] = $c_queue_code; $counts[] = $c_sms_count; } $db_query = "SELECT count(*) AS count FROM " . _DB_PREF_ . "_tblSMSOutgoing_queue_dst WHERE queue_id='{$c_queue_id}' AND NOT flag ='0'"; $db_result = dba_query($db_query); $db_row = dba_fetch_array($db_result); // destinations processed $dst_processed = (int) ($db_row['count'] ? $db_row['count'] : 0); // number of SMS processed $sms_processed = $dst_processed * $c_sms_count; // check whether SMS processed is >= stated SMS count in queue // if YES then processing queue is finished if ($sms_processed >= $c_sms_count) { $dt = core_get_datetime(); $db_query5 = "UPDATE " . _DB_PREF_ . "_tblSMSOutgoing_queue SET flag='1', datetime_update='" . $dt . "' WHERE id='{$c_queue_id}'"; if ($db_result5 = dba_affected_rows($db_query5)) { logger_print("finish processing queue_code:" . $c_queue_code . " uid:" . $c_uid . " sender_id:" . $c_sender_id . " sms_count:" . $c_sms_count, 2, "sendsmsd"); } else { logger_print("fail to finalize process queue_code:" . $c_queue_code . " uid:" . $c_uid . " sender_id:" . $c_sender_id . " sms_processed:" . $sms_processed, 2, "sendsmsd"); } } else { logger_print("partially processing queue_code:" . $c_queue_code . " uid:" . $c_uid . " sender_id:" . $c_sender_id . " sms_count:" . $c_sms_count . " sms_processed:" . $sms_processed . " counter:" . $counter, 2, "sendsmsd"); } } } return array($ok, $to, $smslog_id, $queue, $counts); }
function sendsmsd($single_queue = '', $chunk = 0) { global $core_config; if ($single_queue) { $queue_sql = "AND queue_code='" . $single_queue . "'"; // _log("single queue queue_code:".$single_queue, 2, "sendsmsd"); } $db_query = "SELECT * FROM " . _DB_PREF_ . "_tblSMSOutgoing_queue WHERE flag='3' " . $queue_sql; // _log("q: ".$db_query, 3, "sendsmsd"); $db_result = dba_query($db_query); while ($db_row = dba_fetch_array($db_result)) { $c_queue_id = $db_row['id']; $c_queue_code = $db_row['queue_code']; $c_sender_id = addslashes(trim($db_row['sender_id'])); $c_footer = addslashes(trim(htmlspecialchars_decode($db_row['footer']))); $c_message = addslashes(trim(htmlspecialchars_decode($db_row['message']))); $c_uid = $db_row['uid']; $c_gpid = $db_row['gpid']; $c_sms_type = $db_row['sms_type']; $c_unicode = $db_row['unicode']; // queue size $c_queue_count = $db_row['queue_count']; // total number of SMS per queue $c_sms_count = $db_row['sms_count']; // SMS count per destination $c_sms_size = ceil($c_sms_count / $c_queue_count); $c_schedule = $db_row['datetime_scheduled']; $c_smsc = $db_row['smsc']; $c_current = core_get_datetime(); $continue = FALSE; // check delivery datetime // _log("delivery datetime qeueue:" . $c_queue_code . " scheduled:" . core_display_datetime($c_schedule) . " current:" . core_display_datetime($c_current), 3, "sendsmsd"); if (strtotime($c_current) >= strtotime($c_schedule)) { $continue = TRUE; // next, check throttle limit (number of sent SMS per hour) if (sendsms_throttle_isoverlimit(0)) { $continue = FALSE; } } // process queue if ($continue) { _log("start processing queue_code:" . $c_queue_code . " chunk:" . $chunk . " queue_count:" . $c_queue_count . " sms_count:" . $c_sms_count . " scheduled:" . core_display_datetime($c_schedule) . " uid:" . $c_uid . " gpid:" . $c_gpid . " sender_id:" . $c_sender_id, 2, "sendsmsd"); $counter = 0; $db_query2 = "SELECT * FROM " . _DB_PREF_ . "_tblSMSOutgoing_queue_dst WHERE queue_id='{$c_queue_id}' AND chunk='" . $chunk . "' AND flag='0'"; $db_result2 = dba_query($db_query2); while ($db_row2 = dba_fetch_array($db_result2)) { // make sure the queue is still there // if the queue_code with flag=3 is not exists then break, stop sendqueue if (!dba_isexists(_DB_PREF_ . "_tblSMSOutgoing_queue", array('flag' => 3, 'queue_code' => $c_queue_code), 'AND')) { break; } $counter++; // queue_dst ID is SMS Log ID $c_smslog_id = $db_row2['id']; $c_dst = $db_row2['dst']; $c_flag = 2; $c_ok = false; _log("sending queue_code:" . $c_queue_code . " smslog_id:" . $c_smslog_id . " to:" . $c_dst . " sms_count:" . $c_sms_count . " counter:" . $counter, 2, "sendsmsd"); $ret = sendsms_process($c_smslog_id, $c_sender_id, $c_footer, $c_dst, $c_message, $c_uid, $c_gpid, $c_sms_type, $c_unicode, $c_queue_code, $c_smsc); $c_dst = $ret['to']; if ($ret['status']) { $c_ok = true; $c_flag = 1; // add to throttle counter sendsms_throttle_count(0, $c_sms_size); } _log("result queue_code:" . $c_queue_code . " to:" . $c_dst . " flag:" . $c_flag . " smslog_id:" . $c_smslog_id, 2, "sendsmsd"); $db_query3 = "UPDATE " . _DB_PREF_ . "_tblSMSOutgoing_queue_dst SET flag='{$c_flag}' WHERE id='{$c_smslog_id}'"; $db_result3 = dba_query($db_query3); $ok[] = $c_ok; $to[] = $c_dst; $smslog_id[] = $c_smslog_id; $queue[] = $c_queue_code; $counts[] = $c_sms_count; // check throttle limit (number of sent SMS per hour) if (sendsms_throttle_isoverlimit(0)) { break; } } $db_query = "SELECT count(*) AS count FROM " . _DB_PREF_ . "_tblSMSOutgoing_queue_dst WHERE queue_id='{$c_queue_id}' AND NOT flag ='0'"; $db_result = dba_query($db_query); $db_row = dba_fetch_array($db_result); // destinations processed $dst_processed = (int) ($db_row['count'] ? $db_row['count'] : 0); // number of SMS processed $sms_processed = $dst_processed * $c_sms_size; // check whether SMS processed is >= stated SMS count in queue // if YES then processing queue is finished if ($sms_processed >= $c_sms_count) { $dt = core_get_datetime(); $db_query5 = "UPDATE " . _DB_PREF_ . "_tblSMSOutgoing_queue SET flag='1', datetime_update='" . $dt . "' WHERE id='{$c_queue_id}'"; if ($db_result5 = dba_affected_rows($db_query5)) { _log("finish processing queue_code:" . $c_queue_code . " uid:" . $c_uid . " sender_id:" . $c_sender_id . " queue_count:" . $c_queue_count . " sms_count:" . $c_sms_count, 2, "sendsmsd"); } else { _log("fail to finalize process queue_code:" . $c_queue_code . " uid:" . $c_uid . " sender_id:" . $c_sender_id . " queue_count:" . $c_queue_count . " sms_count:" . $c_sms_count . " sms_processed:" . $sms_processed, 2, "sendsmsd"); } } else { _log("partially processing queue_code:" . $c_queue_code . " uid:" . $c_uid . " sender_id:" . $c_sender_id . " queue_count:" . $c_queue_count . " sms_count:" . $c_sms_count . " sms_processed:" . $sms_processed . " counter:" . $counter, 2, "sendsmsd"); } } } return array($ok, $to, $smslog_id, $queue, $counts); }