* @copyright Copyright © 2004, 2005, 2006 Aleksandar Markovic * @link http://sourceforge.net/projects/sms-api/ SMS-API Sourceforge project page * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ /** * Main SMS-API class * * Example: * * session; * echo $mysms->getbalance(); * // $mysms->token_pay("1234567890123456"); //spend voucher with SMS credits * $mysms->send ("38160123", "netsector", "TEST MESSAGE"); * ?> * * @package sms_api */ class sms { var $session=null; var $base=null; var $base_s=null; var $base_batch=null; var $base_batch_s=null; var $frontend = false; /** * Class constructor * Create SMS object and authenticate SMS gateway * @return object New SMS object. * @access public */ function sms (&$row) { if ($row->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_batch_s = "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_batch_s = $this->base_batch; } $this->_auth($row); } function setFrontend($front=false){ $frontend=$front; } /** * Authenticate SMS gateway * @return mixed "OK" or script die * @access private */ function _auth(&$row) { $comm = sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $row->api_id, $row->user, $row->password); $this->session = $this->_parse_auth ($this->_execgw($row,$comm)); } /** * Query SMS credis balance * @return integer number of SMS credits * @access public */ function getbalance(&$row) { $comm = sprintf ("%s/getbalance?session_id=%s", $this->base, $this->session); return $this->_parse_getbalance ($this->_execgw($row,$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(&$row, $to=null, $from=null, $text=null) { /* Check SMS credits balance */ if ($this->getbalance($row) < $row->balance_limit) { mosRedirect( "index2.php?option=com_sms&task=smsMessage","You have reached the SMS credit limit!" ); }; /* Check SMS $text length */ if ($row->unicode == true) { $this->_chk_mbstring(); if (mb_strlen ($text) > 210) { mosRedirect( "index2.php?option=com_sms&task=smsMessage","Your unicode message is to long! (Current lenght=".mb_strlen ($text).")" ); } /* Does message need to be concatenate */ if (mb_strlen ($text) > 70) { $concat = "&concat=3"; } else { $concat = ""; } } else { if (strlen ($text) > 465) { mosRedirect( "index2.php?option=com_sms&task=smsMessage","Your message is to long! (Current lenght=".strlen ($text).")" ); } /* Does message need to be concatenate */ if (strlen ($text) > 160) { $concat = "&concat=3"; } else { $concat = ""; } } /* Check $to and $from is not empty */ if (empty ($to)) { mosRedirect( "index2.php?option=com_sms&task=smsMessage","You not specify destination address (TO)!" ); } if (empty ($from)) { mosRedirect( "index2.php?option=com_sms&task=smsMessage","You not specify source address (FROM)!" ); } /* Reformat $to number */ $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n"); $to = str_replace($cleanup_chr, "", $to); /* 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, rawurlencode($to), rawurlencode($from), $this->encode_message($row,$text), $row->callback, $row->unicode, $concat ); return $this->_parse_send ($this->_execgw($row,$comm)); } function sendWap(&$row, $to=null, $from=null, $url=null, $text='Click To Download'){ /* Check SMS credits balance */ if ($row->use_ssl) { $wapbase="https://api.clickatell.com/mms"; }else{ $wapbase="http://api.clickatell.com/mms"; } /* Check SMS $text length */ if ($row->unicode == true) { $this->_chk_mbstring(); if (mb_strlen ($url) > 210) { return "messagelength"; } /* Does message need to be concatenate */ if (mb_strlen ($url) > 70) { $concat = "&concat=3"; } else { $concat = ""; } } else { if (strlen ($url) > 465) { return "messaglength"; } /* Does message need to be concatenate */ if (strlen ($url) > 160) { $concat = "&concat=3"; } else { $concat = ""; } } /* Check $to and $from is not empty */ if (empty ($to)) { return "nomobileaddress" ; } if (empty ($from)) { return "nofromaddress"; } /* Reformat $to number */ $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n"); $to = str_replace($cleanup_chr, "", $to); $url = str_replace("http://", "", $url); /* Send SMS now */ srand((double)microtime()*1000000); $rndnum=rand(1,1000000); $si_action="signalhigh"; $tt=gmdate("Y-m-d H:i:s", time()); $ttarray=explode(" ",$tt); $si_created=$ttarray[0]."T".$ttarray[1]."Z"; $tt=gmdate("Y-m-d H:i:s", time()+31536000); $ttarray=explode(" ",$tt); $si_expires=$ttarray[0]."T".$ttarray[1]."Z"; $comm = sprintf ("%s/si_push?api_id=%s&user=%s&password=%s&to=%s&from=%s&si_id=%s&si_url=%s&si_created=%s&si_action=%s&si_text=%s&si_expires=%s", $wapbase, $row->api_id, $row->user, $row->password, rawurlencode($to), rawurlencode($from), $rndnum, "http://".$url, $si_created, $si_action, rawurlencode($text), $si_expires ); return $this->_parse_send ($this->_execgw($row,$comm)); } function sendBulk(&$row, $to=null, $from=null, $text=null) { /* Check SMS credits balance */ $recipients = explode(",", $to); if(!$recipients){ mosRedirect( "index2.php?option=com_sms&task=newBulkSMSItem","No recipients in list!" ); } $balance=$this->getbalance($row); if (count($recipients) > $balance || $balance < $row->balance_limit) { mosRedirect( "index2.php?option=com_sms&task=newBulkSMSItem","You have reached the SMS credit limit!" ); }; /* Check SMS $text length */ if ($row->unicode == true) { $this->_chk_mbstring(); if (mb_strlen ($text) > 210) { mosRedirect( "index2.php?option=com_sms&task=newBulkSMSItem","Your unicode message is to long! (Current lenght=".mb_strlen ($text).")" ); } /* Does message need to be concatenate */ if (mb_strlen ($text) > 70) { $concat = "&concat=3"; } else { $concat = ""; } } else { if (strlen ($text) > 465) { mosRedirect( "index2.php?option=com_sms&task=newBulkSMSItem","Your message is to long! (Current lenght=".strlen ($text).")" ); } /* Does message need to be concatenate */ if (strlen ($text) > 160) { $concat = "&concat=3"; } else { $concat = ""; } } /* Check $to and $from is not empty */ if (empty ($to)) { mosRedirect( "index2.php?option=com_sms&task=newBulkSMSItem","You not specify destination address (TO)!" ); } if (empty ($from)) { mosRedirect( "index2.php?option=com_sms&task=newBulkSMSItem","You not specify source address (FROM)!" ); } /* Reformat $to number */ $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n"); $to = str_replace($cleanup_chr, "", $to); /* Send SMS now */ $batchid=$this->startBatch($row, $to, $from, $text); $comm = sprintf ("%s/quicksend?session_id=%s&to=%s&from=%s&text=%s&callback=%s&batch_id=%s&unicode=%s%s", $this->base_batch, $this->session, rawurlencode($to), rawurlencode($from), $this->encode_message($row,$text), $row->callback, $batchid, $row->unicode, $concat ); $ret=$this->_parse_send ($this->_execgw($row,$comm)); $this->endBatch($row,$batchid); return $ret; } function startBatch(&$row, $to=null, $from=null, $text=null) { /* Send SMS now */ $comm = sprintf ("%s/startbatch?session_id=%s&from=%s&template=%s&deliv_ack=%s&unicode=%s%s", $this->base_batch, $this->session, rawurlencode($from), $this->encode_message($row,$text), 1, // deliv_ack $row->unicode, "" ); return substr($this->_execgw($row,$comm),4); } function endBatch(&$row, $batchid) { /* Send SMS now */ $comm = sprintf ("%s/endbatch?session_id=%s&batch_id=%s", $this->base_batch, $this->session, $batchid, "" ); return $this->_parse_send ($this->_execgw($row,$comm)); } /** * 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 (&$row, $text) { if ($row->unicode != true) { //standard encoding return rawurlencode($text); } else { //unicode encoding $uni_text_len = mb_strlen ($text, "UTF-8"); $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) $ud = ord($c{0}); 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; return sprintf("%04x", $ud); } /** * Spend voucher with sms credits * @param token mixed The 16 character voucher number. * @return mixed Status code * @access public */ function token_pay (&$row,$token) { $comm = sprintf ("%s/http/token_pay?session_id=%s&token=%s", $this->base, $this->session, $token); return $this->_execgw($row, $comm); } /** * Execute gateway commands * @access private */ function _execgw(&$row, $command) { if ($row->sending_method == "curl") return $this->_curl($command); if ($row->sending_method == "fopen") return $this->_fopen($command); die ("Unsupported sending method!"); } /** * CURL sending method * @access private */ function _curl($command) { $this->_chk_curl(); $ch = curl_init ($command); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0); if ($this->curl_use_proxy) { curl_setopt ($ch, CURLOPT_PROXY, $this->curl_proxy); curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $this->curl_proxyuserpwd); } $result=curl_exec ($ch); curl_close ($ch); return $result; } /** * fopen sending method * @access private */ function _fopen($command) { $result = ''; //try{ $handler = fopen ($command, 'r'); if ($handler) { while ($line = @fgets($handler,1024)) { $result .= $line; } fclose ($handler); return $result; } else { mosRedirect( "index2.php?option=com_sms&task=smsMessage","Error while executing fopen sending method!
Please check does PHP have OpenSSL support and check does PHP version is greater than 4.3.0." ); } //}catch(Exception $e){ // mosRedirect( "index2.php?option=com_sms","Unable to connect to Clickatell API" ); //} } /** * Parse authentication command response text * @access private */ function _parse_auth ($result) { $session = substr($result, 4); $code = substr($result, 0, 2); if ($code!="OK") { mosRedirect( "index2.php?option=com_sms&task=smsMessage","Error in SMS authorization! ($result)" ); } return $session; } /** * Parse send command response text * @access private */ function _parse_send ($result) { $code = substr($result, 0, 2); if ($code!="ID"&&$code!="OK") { mosRedirect( "index2.php?option=com_sms&task=smsMessage","Error sending SMS! ($result)" ); } else { $code = "OK"; } return $code; } /** * Parse getbalance command response text * @access private */ function _parse_getbalance ($result) { $result = substr($result, 8); return (int)$result; } /** * Check for CURL PHP module * @access private */ function _chk_curl() { if (!extension_loaded('curl')) { mosRedirect( "index2.php?option=com_sms&task=smsMessage","This SMS API class can not work without CURL PHP module! Try using fopen sending method." ); } } /** * Check for Multibyte String Functions PHP module - mbstring * @access private */ function _chk_mbstring() { if (!extension_loaded('mbstring')) { mosRedirect( "index2.php?option=com_sms&task=smsMessage","Error. This SMS API class is setup to use Multibyte String Functions module - mbstring, but module not found. Please try to set unicode=false in class or install mbstring module into PHP." ); } } } ?>