-
class sendsmsComponent extends Object
-
{
-
-
var $api_id = "your_clickatell_api_id";
-
var $user = "your_clickatell_username";
-
var $password = "you_clickatell_password";
-
var $use_ssl = false;
-
-
/**
-
* Define SMS balance limit below class will not work
-
* @var integer
-
*/
-
var $balace_limit = 0;
-
-
/**
-
* Gateway command sending method (curl,fopen)
-
* @var mixed
-
*/
-
var $sending_method = "fopen";
-
var $unicode = false;
-
-
/**
-
* Optional CURL Proxy
-
* @var bool
-
*/
-
var $curl_use_proxy = false;
-
var $curl_proxy = "http://127.0.0.1:8080";
-
var $curl_proxyuserpwd = "login:secretpass";
-
-
/**
-
* Callback
-
* 0 - Off
-
* 1 - Returns only intermediate statuses
-
* 2 - Returns only final statuses
-
* 3 - Returns both intermediate and final statuses
-
* @var integer
-
*/
-
var $callback = 0;
-
var $session;
-
var $batch;
-
-
/**
-
* Class constructor
-
* Create SMS object and authenticate SMS gateway
-
* @return object New SMS object.
-
* @access public
-
*/
-
-
function startup(&$controller)
-
{
-
// This method takes a reference to the controller which is loading it.
-
// Perform controller initialization here.
-
-
if ($this->use_ssl) {
-
$this->base = "http://api.clickatell.com/http";
-
$this->base_s = "https://api.clickatell.com/http";
-
$this->base_batch = "http://api.clickatell.com/http_batch";
-
$this->base_s_batch = "https://api.clickatell.com/http_batch";
-
-
} else {
-
$this->base = "http://api.clickatell.com/http";
-
$this->base_s = $this->base;
-
$this->base_batch = "http://api.clickatell.com/http_batch";
-
$this->base_s_batch = $this->base_batch;
-
-
}
-
$this->_auth();
-
}
-
-
/*
-
function sms () {
-
if ($this->use_ssl) {
-
$this->base = "http://api.clickatell.com/http";
-
$this->base_s = "https://api.clickatell.com/http";
-
} else {
-
$this->base = "http://api.clickatell.com/http";
-
$this->base_s = $this->base;
-
}
-
$this->_auth();
-
}
-
*/
-
-
/**
-
* Authenticate SMS gateway
-
* @return mixed "OK" or script die
-
* @access private
-
*/
-
function _auth() {
-
$comm =
sprintf ("%s/auth?api_id=%s&user=%s&password=%s",
$this->
base_s,
$this->
api_id,
$this->
user,
$this->
password);
-
$this->session = $this->_parse_auth ($this->_execgw($comm));
-
}
-
-
/**
-
* Query SMS credis balance
-
* @return integer number of SMS credits
-
* @access public
-
*/
-
function getbalance() {
-
$comm =
sprintf ("%s/getbalance?session_id=%s",
$this->
base,
$this->
session);
-
return $this->_parse_getbalance ($this->_execgw($comm));
-
}
-
-
/**
-
* Send SMS message
-
* @param to mixed The destination address.
-
* @param from mixed The source/sender address
-
* @param text mixed The text content of the message
-
* @return mixed "OK" or script die
-
* @access public
-
*/
-
function send($to=null, $from=null, $text=null) {
-
-
/* Check SMS credits balance */
-
if ($this->getbalance() <$this->balace_limit) {
-
die ("You have reach the SMS credit limit!");
-
};
-
-
/* Check SMS $text length */
-
if ($this->unicode == true) {
-
$this->_chk_mbstring();
-
-
die ("Your unicode message is to long! (Current lenght=".
mb_strlen ($text).
")");
-
}
-
/* Does message need to be concatenate */
-
-
$concat = "&concat=3";
-
} else {
-
$concat = "";
-
}
-
} else {
-
-
die ("Your message is to long! (Current lenght=".
strlen ($text).
")");
-
}
-
/* Does message need to be concatenate */
-
-
$concat = "&concat=3";
-
} else {
-
$concat = "";
-
}
-
}
-
-
/* Check $to and $from is not empty */
-
-
die ("You not specify destination address (TO)!");
-
}
-
-
die ("You not specify source address (FROM)!");
-
}
-
$from = $this->_custom_encode($from);
-
-
/* Reformat $to number */
-
$cleanup_chr =
array ("+",
" ",
"(",
")",
"\r",
"\n",
"\r\n");
-
-
-
/* Send SMS now */
-
$comm =
sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s&unicode=%s%s",
-
$this->base,
-
$this->session,
-
-
$from,
-
$this->encode_message($text),
-
$this->callback,
-
$this->unicode,
-
$concat
-
);
-
$myvar = $this->_parse_send ($this->_execgw($comm));
-
return $myvar;
-
}
-
-
function sendbatch($to=null, $from=null, $text=null){
-
/* Check SMS credits balance */
-
if ($this->getbalance() <$this->balace_limit) {
-
die ("You have reach the SMS credit limit!");
-
};
-
-
/* Check SMS $text length */
-
if ($this->unicode == true) {
-
$this->_chk_mbstring();
-
-
die ("Your unicode message is to long! (Current lenght=".
mb_strlen ($text).
")");
-
}
-
/* Does message need to be concatenate */
-
-
$concat = "&concat=3";
-
} else {
-
$concat = "";
-
}
-
} else {
-
-
die ("Your message is to long! (Current lenght=".
strlen ($text).
")");
-
}
-
/* Does message need to be concatenate */
-
-
$concat = "&concat=3";
-
} else {
-
$concat = "";
-
}
-
}
-
-
/* Check $to and $from is not empty */
-
-
die ("You not specify destination address (TO)!");
-
}
-
-
die ("You not specify source address (FROM)!");
-
}
-
$from = $this->_custom_encode($from);
-
-
/* get batchID now */
-
$comm =
sprintf ("%s/startbatch?session_id=%s&template=%s&callback=%s&unicode=%s%s",
-
$this->base_batch,
-
$this->session,
-
$this->encode_message($text),
-
$this->callback,
-
$this->unicode,
-
$concat
-
);
-
$this->batch = $this->_batchID ($this->_execgw($comm));
-
-
$comm =
sprintf ("%s/quicksend?session_id=%s&batch_id=%s&to=$to&from=$from",
-
$this->base_batch,
-
$this->session,
-
$this->batch
-
);
-
$myvar = $this->_parse_sendbatch ($this->_execgw($comm));
-
return $myvar;
-
}
-
-
/**
-
* Encode message text according to required standard
-
* @param text mixed Input text of message.
-
* @return mixed Return encoded text of message.
-
* @access public
-
*/
-
function encode_message ($text) {
-
if ($this->unicode != true) {
-
//standard encoding
-
//return rawurlencode($text);
-
return $this->_custom_encode($text);
-
} else {
-
//unicode encoding
-
-
$out_text = "";
-
-
//encode each character in text
-
for ($i=0; $i<$uni_text_len; $i++) {
-
$out_text .=
$this->
uniord(mb_substr ($text,
$i,
1,
"UTF-8"));
-
}
-
-
return $out_text;
-
}
-
}
-
-
/**
-
* Unicode function replacement for ord()
-
* @param c mixed Unicode character.
-
* @return mixed Return HEX value (with leading zero) of unicode character.
-
* @access public
-
*/
-
function uniord($c) {
-
$ud = 0;
-
if (ord($c{0})>=
0 &&
ord($c{0})<=
127)
-
-
if (ord($c{0})>=
192 &&
ord($c{0})<=
223)
-
$ud =
(ord($c{0})-
192)*
64 +
(ord($c{1})-
128);
-
if (ord($c{0})>=
224 &&
ord($c{0})<=
239)
-
$ud =
(ord($c{0})-
224)*
4096 +
(ord($c{1})-
128)*
64 +
(ord($c{2})-
128);
-
if (ord($c{0})>=
240 &&
ord($c{0})<=
247)
-
$ud =
(ord($c{0})-
240)*
262144 +
(ord($c{1})-
128)*
4096 +
(ord($c{2})-
128)*
64 +
(ord($c{3})-
128);
-
if (ord($c{0})>=
248 &&
ord($c{0})<=
251)
-
$ud =
(ord($c{0})-
248)*
16777216 +
(ord($c{1})-
128)*
262144 +
(ord($c{2})-
128)*
4096 +
(ord($c{3})-
128)*
64 +
(ord($c{4})-
128);
-
if (ord($c{0})>=
252 &&
ord($c{0})<=
253)
-
$ud =
(ord($c{0})-
252)*
1073741824 +
(ord($c{1})-
128)*
16777216 +
(ord($c{2})-
128)*
262144 +
(ord($c{3})-
128)*
4096 +
(ord($c{4})-
128)*
64 +
(ord($c{5})-
128);
-
if (ord($c{0})>=
254 &&
ord($c{0})<=
255) //error
-
$ud = false;
-
-
}
-
-
/**
-
* Spend voucher with sms credits
-
* @param token mixed The 16 character voucher number.
-
* @return mixed Status code
-
* @access public
-
*/
-
function token_pay ($token) {
-
$comm =
sprintf ("%s/http/token_pay?session_id=%s&token=%s",
-
$this->base,
-
$this->session,
-
$token);
-
-
return $this->_execgw($comm);
-
}
-
-
/**
-
* Execute gateway commands
-
* @access private
-
*/
-
function _execgw($command) {
-
if ($this->sending_method == "curl")
-
return $this->_curl($command);
-
if ($this->sending_method == "fopen")
-
return $this->_fopen($command);
-