Blog Emka

Portal linux, pemrograman sistem informasi, computer security & santri

Tutorial PHP

Tutorial PHP: Format SMS Panjang Pada Gammu

Dua minggu terakhir ini saya intens belajar lagi tentang Gammu, mempersiapkan project *gelap* untuk dunia pendidikan Indonesia (mengusir penat dan pendulum kerja yang tak tentu masa depannya). Pada artikel tentang PHP ini, kita akan mencoba menginjeksikan format SMS panjang ke Inbox Gammu, kemudian biarkan Gammu yang melanjutkan.

Class PHP untuk melakukan hal ini sudah diposting oleh om Ikhsan dari FTIK USM di PHPClasses. Berikut sourcecode class-nya:

<?php
/**
 * sms_inject
 * 
 * @package   gammu smsd class
 * @author    ikhsan agustian <ikhsan017@gmail.com>
 * @license   Distributed under GNU/GPL
 * @version   0.1
 * @access    public
 */

class sms_inject
{
    private $error, $res, $msg, $dest, $udh, $msg_part; //msg_part array of couple udh + msg
    
    /**
     * sms_inject::__construct()
     * @usage object constructor 
     * @param mysql link resource $res
     * @return void
     */
    function __construct($res) //throw mysql resource as argument
    {
        $this->udh=array(
        'udh_length'=>'05', //sms udh lenth 05 for 8bit udh, 06 for 16 bit udh
        'identifier'=>'00', //use 00 for 8bit udh, use 08 for 16bit udh
        'header_length'=>'03', //length of header including udh_length & identifier
        'reference'=>'00', //use 2bit 00-ff if 8bit udh, use 4bit 0000-ffff if 16bit udh
        'msg_count'=>1, //sms count
        'msg_part'=>1 //sms part number
        );
        $this->msg_part=array();
        $this->res=$res;
        $this->error=array();
    }
    
    
    /**
     * sms_inject::mass_sms()
     * @usage tell gammu-smsd to send one sms to many recipient
     * @param string $msg
     * @param array $dest
     * @param string $sender
     * @return void
     */
    function mass_sms($msg,$dest,$sender='')
    {
        $this->msg=$msg;
        $this->create_msg();
        if(!is_array($dest))
        {
            $this->send_sms($msg,$dest,$sender);
        }
        else
        {
            foreach($dest as $dst)
            {
                $this->send_sms($msg,$dst,$sender);
            }
        }
    }
    
    
    /**
     * sms_inject::send_sms()
     * @usage tell gammu-smsd to send sms to sepcified phone number
     * @param string $msg
     * @param string $dest
     * @param string $sender
     * @return false if error
     */
    function send_sms($msg,$dest,$sender='')
    {
        if(!$dest)
        {
            $this->error[]='No destination number defined';
            return false;
        }
        $this->msg=$msg;
        $this->dest=$dest;
        $this->create_msg();
        //uncomment to get preview
        //echo "<pre>Destination : $this->dest\nSender : $sender\nMessage :\n";print_r($this->msg_part);
        $this->inject($sender);
    }
    
    
    /**
     * sms_inject::inject()
     * @usage insert previously created sms part to database
     * @param string $sender
     * @return void
     */
    private function inject($sender='')
    {
        $multipart=(count($this->msg_part) > 1)?'true':'false';
        $id='';
        foreach($this->msg_part as $number => $sms)
        {
            if($number==1)
            {
                $query="insert into outbox (`UDH`,`DestinationNumber`,`TextDecoded`,`MultiPart`,`SenderID`) values ('{$sms['udh']}','{$this->dest}','{$sms['msg']}','{$multipart}','$sender')";
                mysql_query($query,$this->res);
                $id=mysql_fetch_assoc(mysql_query("select last_insert_id() as id",$this->res));
                $id=$id['id'];
            }
            else
            {
                $query="insert into outbox_multipart (`UDH`,`SequencePosition`,`TextDecoded`,`ID`) values ('{$sms['udh']}','{$number}','{$sms['msg']}','{$id}')";
                mysql_query($query,$this->res);
            }
        }
    }
    
    
    /**
     * sms_inject::create_msg()
     * @usage create sms message (and create udh if sms is multipart)
     * @return void
     */
    private function create_msg()
    {
        $x=1;
        if(strlen($this->msg)<=160) //if single sms, send without udh
        {
            $this->msg_part[$x]['udh']='';
            $this->msg_part[$x]['msg']=$this->msg;
        }
        else //if multipart sms, split into 153 character each part
        {
            $msg=str_split($this->msg,153);
            $ref=mt_rand(1,255);
            $this->udh['msg_count']=$this->dechex_str(count($msg));
            $this->udh['reference']=$this->dechex_str($ref);
            foreach($msg as $part)
            {
                $this->udh['msg_part']=$this->dechex_str($x);
                $this->msg_part[$x]['udh']=implode('',$this->udh);
                $this->msg_part[$x]['msg']=$part;
                $x++;
            }
        }
    }
    
    
    /**
     * sms_inject::dechex_str()
     * @usage convert decimal to zerofilled hexadecimal
     * @param integer $ref
     * @return 2 digit hexa-decimal in string format
     */
    private function dechex_str($ref)
    {
        return ($ref <= 15 )?'0'.dechex($ref):dechex($ref);
    }
}
?> 

Nah untuk contoh penggunaannya:

include_once 'sms_inject.class.php';

$res=mysql_connect('localhost','root','');
mysql_select_db('sms',$res);

$sms=new sms_inject($res);
$msg="In the cellular phone industry, mobile phones and their networks sometimes support concatenated short message service (or concatenated SMS) to overcome the limitation on the number of characters that can be sent in a single SMS text message transmission (which is usually 160).";
$sms->send_sms($msg,'085747078078','send_sms');
$sms->mass_sms($msg,array('085747079079','085225307799','085225099883'),'mass_sms'); 

Tinggal kreativitas anda bermain dengan class php ini!

Comments are Closed

Theme by Anders Norén

%d bloggers like this: