diff --git a/application/controllers/daemon.php b/application/controllers/daemon.php index ff328bc9f..9fb259a1e 100644 --- a/application/controllers/daemon.php +++ b/application/controllers/daemon.php @@ -36,7 +36,7 @@ function __construct() // -------------------------------------------------------------------- /** - * Message routine + * Incoming Message routine * * Process the new/unprocessed incoming sms * Called by shell/batch script on Gammu RunOnReceive directive @@ -219,6 +219,21 @@ function server_alert_daemon() } } + + /** + * Outbox Message routine for Alternate Gateways + * + * Process outgoing sms waiting in outbox + * Called by shell/batch script via Crontab + * + * @access public + */ + function outbox_routine() + { + $this->load->model(array('Message_model')); + // send waiting messages + $this->Message_model->process_outbox_queue(); + } } /* End of file daemon.php */ diff --git a/application/models/gateway/clickatell_model.php b/application/models/gateway/clickatell_model.php index bc62cbcc8..74660299e 100644 --- a/application/models/gateway/clickatell_model.php +++ b/application/models/gateway/clickatell_model.php @@ -21,9 +21,9 @@ * @subpackage Messages * @category Models */ -require_once('gammu_model'.EXT); +require_once('nongammu_model'.EXT); -class Clickatell_model extends Gammu_model { +class Clickatell_model extends nongammu_model { /** * Constructor @@ -49,7 +49,7 @@ function __construct() * * @return void */ - function send_messages($data) + function really_send_messages($data) { $gateway = $this->gateway; file_get_contents($gateway['url'].'/http/sendmsg?user='.$gateway['username']. @@ -58,4 +58,4 @@ function send_messages($data) } /* End of file clikatell_model.php */ -/* Location: ./application/models/gateway/clikatell_model.php */ \ No newline at end of file +/* Location: ./application/models/gateway/clikatell_model.php */ diff --git a/application/models/gateway/gammu_model.php b/application/models/gateway/gammu_model.php index 32c4d56e9..b3c8257d2 100644 --- a/application/models/gateway/gammu_model.php +++ b/application/models/gateway/gammu_model.php @@ -29,9 +29,9 @@ class Gammu_model extends Model { * * @access public */ - function Message_model() + function __construct() { - parent::Model(); + parent::__construct(); // Set mb encoding mb_internal_encoding($this->config->item('charset')); @@ -1346,6 +1346,13 @@ function canned_response($name,$message, $action) else die("Invalid Option"); } + + // hook function for Alternate Gateways + // do nothing for GAMMU (gammu-smsd moves from outbox to sentitems) + function process_outbox_queue() + { + log_message('info',"Nothing to do in outbox queue for GAMMU."); + } } /* End of file gammu_model.php */ diff --git a/application/models/gateway/kannel_model.php b/application/models/gateway/kannel_model.php index e9ee877a8..82982e893 100644 --- a/application/models/gateway/kannel_model.php +++ b/application/models/gateway/kannel_model.php @@ -21,9 +21,9 @@ * @subpackage Messages * @category Models */ -require_once('gammu_model'.EXT); +require_once('nongammu_model'.EXT); -class Kannel_model extends Gammu_model { +class Kannel_model extends nongammu_model { /** * Constructor @@ -43,7 +43,7 @@ function __construct() * * @return void */ - function send_messages($data) + function really_send_messages($data) { $gateway = $this->config->item('gateway'); file_get_contents($gateway['url'].'/cgi-bin/sendsms?username='.$gateway['username']. @@ -52,4 +52,4 @@ function send_messages($data) } /* End of file kannel_model.php */ -/* Location: ./application/models/gateway/kannel_model.php */ \ No newline at end of file +/* Location: ./application/models/gateway/kannel_model.php */ diff --git a/application/models/gateway/nongammu_model.php b/application/models/gateway/nongammu_model.php new file mode 100644 index 000000000..1b72614a1 --- /dev/null +++ b/application/models/gateway/nongammu_model.php @@ -0,0 +1,260 @@ +_default(array('SenderID' => NULL, 'CreatorID' => '', 'validity' => '-1'), $data); + + // check if wap msg + if (isset($data['type']) AND $data['type']=='waplink') { + log_message('error',"Non-gammu alternate gateways DO NOT support WAP link sending!"); + return ; + } + + //check empty message + if (trim($data['message']) == "") { + log_message('error',"Cannot send empty message!"); + return; + }; + + if (strtotime($data['date'])>time()){ //to be sent in the future + $this->enqueue_messages($data); + return; + } + + $gateway = substr(get_class($this),0,-6); + log_message('debug',"SMS via gateway \"$gateway\" to ".$data['dest']. + " length ".strlen($data['message'])." chars"); + + $ret=$this->really_send_messages($data); + if(is_string($ret)){ + log_message('error',"Message failed via gateway \"$gateway\" to ".$data['dest']." Reason: ".$ret); + $this->save_sent_messages($data,$ret); + return; + }; + $this->save_sent_messages($data); + $this->Kalkun_model->add_sms_used($data['uid']); + } + + /** + * really_send_messages + * A template function to send message "your way" + * Redefine in descendant Classes + * + * @author jbubik + * @category SMS + * @param array $data + * @return null/String + * Return of string value indicates Error sending the message. Otherwise success. + **/ + + function really_send_messages($data) + { + return "Error. Method really_send_messages not redefined in Class ".get_class($this)."!!!"; + } + + /** + * enqueue_messages + * Save a message to outbox for sending later + * + * @author jbubik + * @category SMS + * @param array $data + * @return void + **/ + + function enqueue_messages($tmp_data) + { + // remove spaces and dashes if any + $tmp_data['dest'] = str_replace(" ", "", $tmp_data['dest']); + $tmp_data['dest'] = str_replace("-", "", $tmp_data['dest']); + + $data = array ( + 'InsertIntoDB' => date('Y-m-d H:i:s'), + 'SendingDateTime' => $tmp_data['date'], + 'DestinationNumber' => $tmp_data['dest'], + 'Coding' => ($tmp_data['coding']=='default'?"Default_No_Compression":"Unicode_No_Compression"), + 'Class' => $tmp_data['class'], + 'CreatorID' => $tmp_data['CreatorID'], + 'SenderID' => $tmp_data['SenderID'], + 'TextDecoded' => $tmp_data['message'], + 'RelativeValidity' => $tmp_data['validity'], + 'DeliveryReport' => $tmp_data['delivery_report'], + 'MultiPart' => 'false' + ); + $this->db->insert('outbox', $data); + $this->db->insert('user_outbox', array('id_outbox'=>$this->db->insert_id(), + 'id_user'=>$tmp_data['uid'])); + log_message('debug',"Message saved to outbox dest:".$tmp_data['dest']); + } + + /** + * save_sent_massages + * Save a message to sentitems + * Optionally delete from outbox ($data['outbox_id']!=null) + * Optionally mark that sending has failed ($err_desc!="") + * + * @author jbubik + * @category SMS + * @param array $data + * @return void + **/ + function save_sent_messages($tmp_data,$err_desc="") + { + $data = array ( + 'InsertIntoDB' => date('Y-m-d H:i:s'), + 'SendingDateTime' => $tmp_data['date'], + 'DestinationNumber' => $tmp_data['dest'], + 'Coding' => ($tmp_data['coding']=='default'?"Default_No_Compression":"Unicode_No_Compression"), + 'Class' => $tmp_data['class'], + 'CreatorID' => $tmp_data['CreatorID'], + 'SenderID' => strval($tmp_data['SenderID']), + 'TextDecoded' => $tmp_data['message'].($err_desc==""?'':' / '.$err_desc), + 'RelativeValidity' => $tmp_data['validity'], + 'Status' => ($err_desc==""?'SendingOK':'SendingError'), + 'SequencePosition' => 1, + 'id_folder' => 3 + ); + $this->db->trans_begin(); + $this->db->from('sentitems'); + $this->db->select('ID'); + $this->db->limit(1); + $this->db->order_by('ID', 'DESC'); + $lstmsg=$this->db->get(); + if($lstmsg->num_rows==0){ + $data['ID']=1; + }else{ + $data['ID']=1+$lstmsg->row('ID'); + }; + $this->db->insert('sentitems', $data); + $this->db->insert('user_sentitems', array('id_sentitems'=>$data['ID'], + 'id_user'=>$tmp_data['uid'])); + if(array_key_exists('id_outbox',$tmp_data)){ + log_message('debug',"Deleting from outbox message ID=".$tmp_data['id_outbox']); + $this->db->where('ID', $tmp_data['id_outbox']); + $this->db->delete('outbox'); + $this->db->where('id_outbox', $tmp_data['id_outbox']); + $this->db->delete('user_outbox'); + }; + $this->db->trans_commit(); + log_message('debug',"Message saved to sentitems dest:".$tmp_data['dest']); + } + + // hook function for Alternate Gateways + // for NON-GAMMU check outbox queue, send and move to sentitems + function process_outbox_queue() + { + $gateway = substr(get_class($this),0,-6); + log_message('debug',"Processing outbox queue in gateway ".$gateway); + $this->db->from('outbox'); + $this->db->where('SendingDateTime <=',date('Y-m-d H:i:s')); + $this->db->order_by('SendingDateTime', 'ASC'); + $res=$this->db->get(); + if($res->num_rows==0){ + log_message('debug',"Nothing to process in outbox queue."); + return; + }; + log_message('debug',"Processing ".$res->num_rows." messages in outbox queue."); + foreach ($res->result_array() as $row) + { + $data = array ( + 'date' => $row['SendingDateTime'], + 'dest' => $row['DestinationNumber'], + 'coding' => ($row['Coding']=="Default_No_Compression"?'default':'unicode'), + 'class' => $row['Class'], + 'CreatorID' => $row['CreatorID'], + 'SenderID' => $row['SenderID'], + 'message' => $row['TextDecoded'], + 'validity' => $row['RelativeValidity'], + 'delivery_report' => $row['DeliveryReport'], + 'id_outbox' => $row['ID'] + ); + $this->db->from('user_outbox'); + $this->db->where('id_outbox',$row['ID']); + $res2=$this->db->get(); + if ($res2->num_rows!=1){ + log_message('error',"outbox ID=".$row['ID']." not found in user_outbox. Sending as user 1."); + $data['uid']=1; + }else{ + $data['uid']=$res2->row('id_user'); + }; + + log_message('debug',"SMS via gateway \"$gateway\" to ".$data['dest']. + " length ".strlen($data['message'])." chars"); + $ret=$this->really_send_messages($data); + if(is_string($ret)){ + log_message('error',"Message failed via gateway \"$gateway\" to ".$data['dest']." Reason: ".$ret); + $this->save_sent_messages($data,$ret); + return; + }; + $this->save_sent_messages($data); + $this->Kalkun_model->add_sms_used($data['uid']); + } + } + + +} + +/* End of file nongammu_model.php */ +/* Location: ./application/models/gateway/nongammu_model.php */ diff --git a/application/models/gateway/nowsms_model.php b/application/models/gateway/nowsms_model.php index b1c7a14f1..af5cad9c1 100644 --- a/application/models/gateway/nowsms_model.php +++ b/application/models/gateway/nowsms_model.php @@ -21,9 +21,9 @@ * @subpackage Messages * @category Models */ -require_once('gammu_model'.EXT); +require_once('nongammu_model'.EXT); -class Nowsms_model extends Gammu_model { +class Nowsms_model extends nongammu_model { /** * Constructor @@ -43,7 +43,7 @@ function __construct() * * @return void */ - function send_messages($data) + function really_send_messages($data) { $gateway = $this->config->item('gateway'); file_get_contents($gateway['url'].'/?User='.$gateway['username']. @@ -52,4 +52,4 @@ function send_messages($data) } /* End of file nowsms_model.php */ -/* Location: ./application/models/gateway/nowsms_model.php */ \ No newline at end of file +/* Location: ./application/models/gateway/nowsms_model.php */ diff --git a/application/models/gateway/ozeking_model.php b/application/models/gateway/ozeking_model.php index 370a419ac..9304f4d91 100644 --- a/application/models/gateway/ozeking_model.php +++ b/application/models/gateway/ozeking_model.php @@ -21,9 +21,9 @@ * @subpackage Messages * @category Models */ -require_once('gammu_model'.EXT); +require_once('nongammu_model'.EXT); -class Ozeking_model extends Gammu_model { +class Ozeking_model extends nongammu_model { /** * Constructor @@ -43,7 +43,7 @@ function __construct() * * @return void */ - function send_messages($data) + function really_send_messages($data) { $gateway = $this->config->item('gateway'); file_get_contents($gateway['url'].'/api?action=sendmessage&username='.$gateway['username']. @@ -52,4 +52,4 @@ function send_messages($data) } /* End of file ozeking_model.php */ -/* Location: ./application/models/gateway/ozeking_model.php */ \ No newline at end of file +/* Location: ./application/models/gateway/ozeking_model.php */ diff --git a/application/models/gateway/panacea_model.php b/application/models/gateway/panacea_model.php index ead5a9854..d1effb20a 100644 --- a/application/models/gateway/panacea_model.php +++ b/application/models/gateway/panacea_model.php @@ -22,9 +22,9 @@ * @subpackage Messages * @category Models */ -require_once('gammu_model'.EXT); +require_once('nongammu_model'.EXT); -class panacea_model extends Gammu_model { +class panacea_model extends nongammu_model { /** * Constructor @@ -46,7 +46,7 @@ function __construct() * * replace xxx from &from=xxx with your sender id */ - function send_messages($data) + function really_send_messages($data) { $gateway = $this->config->item('gateway'); file_get_contents($gateway['url'].'/json?action=message_send&username='.$gateway['username']. diff --git a/application/models/gateway/tmobilecz_model.php b/application/models/gateway/tmobilecz_model.php index 1d2b4ebdf..7054cd165 100644 --- a/application/models/gateway/tmobilecz_model.php +++ b/application/models/gateway/tmobilecz_model.php @@ -14,16 +14,17 @@ /** * Tmobilecz_model Class * - * Handle all messages database activity + * Handle the real sending of SMS * for T-Mobile CZ + * Database handling inherited from nongammu_model * * @package Kalkun * @subpackage Messages * @category Models */ -require_once('gammu_model'.EXT); +require_once('nongammu_model'.EXT); -class Tmobilecz_model extends Gammu_model { +class Tmobilecz_model extends nongammu_model { /** * Constructor @@ -39,7 +40,7 @@ function __construct() // -------------------------------------------------------------------- /** - * Send Messages (Still POC) + * Real Send Messages * * @access public * @param mixed $options @@ -52,18 +53,14 @@ function __construct() * class -1, 0, 1 * delivery_report default, yes, no * uid int - * @return object + * @return null/string */ - function send_messages($data) + function really_send_messages($data) { - if (trim($data['message']) == "") { - log_message('error',"TMCZ> Aborting empty SMS via ".__CLASS__." to ".$data['dest']); - return; - }; $gateway = $this->config->item('gateway'); if(!is_array($gateway['tmobileczauth'])) { log_message('error',"TMCZ> Authentication not configured in kalkun_settings.php. SMS aborted."); - return; + return "Authentication not configured in kalkun_settings.php."; }; $auth=$gateway['tmobileczauth']; if (($user=$auth[$data['uid']]['user'])&&($pass=$auth[$data['uid']]['pass'])){ @@ -77,7 +74,7 @@ function send_messages($data) }else{ log_message('error',"TMCZ> Aborting SMS. No credentials to send SMS via ". __CLASS__." to ".$data['dest']." for user ID ".$data['uid']); - return; + return "No credentials to send SMS."; }; log_message('debug',"TMCZ> SMS via ".__CLASS__." user ".$user." to ".$data['dest']. " length ".strlen($data['message'])." chars"); @@ -85,6 +82,7 @@ function send_messages($data) $data['class']=="0",$data['delivery_report']=="yes",$hist,$eml); if(is_string($ret)){ log_message('error',"TMCZ> SMS via ".__CLASS__." to ".$data['dest']." failed: ".$ret); + return $ret; }; } diff --git a/scripts/outbox_queue.bat b/scripts/outbox_queue.bat new file mode 100644 index 000000000..aee1c7141 --- /dev/null +++ b/scripts/outbox_queue.bat @@ -0,0 +1,10 @@ + + +:: Configure this (use absolute path) +:: php cli path +set PHP=C:\php\php.exe +:: daemon.php path +set DAEMON=C:\kalkun\scripts\outbox_queue.php + +:: Execute +%PHP% %DAEMON% diff --git a/scripts/outbox_queue.php b/scripts/outbox_queue.php new file mode 100755 index 000000000..bebba9c7d --- /dev/null +++ b/scripts/outbox_queue.php @@ -0,0 +1,10 @@ + diff --git a/scripts/outbox_queue.sh b/scripts/outbox_queue.sh new file mode 100755 index 000000000..b4bc782c0 --- /dev/null +++ b/scripts/outbox_queue.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +# Configure this (use absolute path) +PHP=/usr/bin/php # php cli path +DAEMON=/path/to/kalkun/scripts/outbox_queue.php # daemon.php path + +# Execute +$PHP $DAEMON