Skip to content

Commit

Permalink
Implement database functions for Alternate Gateways
Browse files Browse the repository at this point in the history
Added nongammu_model.php as a superclass for all Alternative Gateways.
Implemented:
 - a stub function for sending message (really_send_massages())
 - saving a message to sentitems after sending
 - saving a message into outbox for to-be-postponed messages
 - processing of postponed messages from outbox
 - crontab script for periodic executing
  • Loading branch information
jbubik committed Jun 2, 2014
1 parent 881038e commit ea70be7
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 34 deletions.
17 changes: 16 additions & 1 deletion application/controllers/daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down
8 changes: 4 additions & 4 deletions application/models/gateway/clickatell_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'].
Expand All @@ -58,4 +58,4 @@ function send_messages($data)
}

/* End of file clikatell_model.php */
/* Location: ./application/models/gateway/clikatell_model.php */
/* Location: ./application/models/gateway/clikatell_model.php */
11 changes: 9 additions & 2 deletions application/models/gateway/gammu_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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 */
Expand Down
8 changes: 4 additions & 4 deletions application/models/gateway/kannel_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'].
Expand All @@ -52,4 +52,4 @@ function send_messages($data)
}

/* End of file kannel_model.php */
/* Location: ./application/models/gateway/kannel_model.php */
/* Location: ./application/models/gateway/kannel_model.php */
260 changes: 260 additions & 0 deletions application/models/gateway/nongammu_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
<?php
/**
* Kalkun
* An open source web based SMS Management
*
* @package Kalkun
* @author Kalkun Dev Team
* @license http://kalkun.sourceforge.net/license.php
* @link http://kalkun.sourceforge.net
*/

// ------------------------------------------------------------------------

/**
* nongammu_model Class
*
* Handle all database activity for sending messages
* for alternative gateways (all non-gammu based).
* To be used as an ancestor class for all alternative
* gateways classes. Inherits all other methods from Gammu_model.
*
* @package Kalkun
* @subpackage Messages
* @category Models
*/
require_once('gammu_model'.EXT);

class nongammu_model extends Gammu_model {

/**
* Constructor
*
* @access public
*/
function __construct()
{
parent::__construct();
log_message('debug',"NonGammu Class Initialized");
}

// --------------------------------------------------------------------

/**
* Send Messages (process SMS,
* enqueue for later sending (table outbox) or send immediatelly and save to sentitems
*
*
* @access public
* @param mixed $options
* Option: Values
* --------------
* dest string, phone number destination
* date datetime
* message string
* coding default, unicode
* class -1, 0, 1
* delivery_report default, yes, no
* uid int
* url string, WAP url
* type string, waplink
* validity string
* @return object
*/
function send_messages($data)
{
//default values
$data = $this->_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 */
Loading

0 comments on commit ea70be7

Please sign in to comment.