Arief Yudhawarman

Masih belajar buat blog

SMS Web

with one comment

Pada artikel yang lalu penulis sudah membahas aplikasi SMS Gateway dengan Perl. Di aplikasi itu untuk mengirim sms dilakukan melalui init script atau buat file txt yang disimpan di folder tertentu agar bisa diproses oleh aplikasi. Di kesempatan ini penulis membahas aplikasi kirim sms dengan script cgi melalui web. Kirim sms bisa dilakukan dengan lebih mudah baik melalui browser atau wget.

Contoh link untuk kirim sms melalui browser:
http://smsserver/cgi-bin/sms.cgi?user=user&pass=password&to=085236001234&txt=Tes%20kirim%20sms

Contoh kirim sms melalui wget:
wget -O hasil.txt “http://smsserver/cgi-bin/sms.cgi?user=user&pass=password&to=085236001234&txt=Tes kirim sms”

Kita lihat bahwa kalau kirim sms menggunakan browser maka user harus melakukan karakter encoding seperti karakter spasi harus diencode menjadi %20.

Aplikasi ini mendukung kirim sms ke lebih dari satu nomor tujuan. Lihat contoh pada akhir tulisan ini.

Untuk keperluan pembuatan aplikasi SMS Web maka server web perlu mengaktifkan modul cgi.

Jika menggunakan apache sebagai server web:


cd /etc/apache2/mods-enabled
ln -s ../mods-available/cgi.load .
/etc/init.d/apache2 reload


Jika menggunakan nginx lakukan konfigurasi mengikuti manual di How to Configure nginx.

Untuk keperluan autentikasi user maka buat database smscgi di mysql. Create table users sbb:

CREATE TABLE `users` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `user` varchar(20) NOT NULL UNIQUE,
  `pass` varchar(20) NOT NULL,
  `status` enum('active','closed') DEFAULT 'active',
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

Kemudian login ke mysql dan buat user baru:

mysql> use smscgi;
mysql> insert into users (user,pass) values ('user','password');
Query OK, 1 row affected (0.00 sec)

mysql> select * from users;
+----+------+----------+--------+---------------------+
| id | user | pass     | status | created             |
+----+------+----------+--------+---------------------+
|  1 | user | password | active | 2017-01-01 08:00:00 |
+----+------+----------+--------+---------------------+
1 row in set (0.00 sec)

mysql> 


Instal modul DBI untuk perl.
Untuk OS Debian based.


apt-get install libdbd-mysql-perl
apt-get install libdbi-perl

Untuk OS CentOS based.


yum install perl-DBD-MySQL
yum install libdbi-dbd-mysql

Edit file sms.cgi di folder /var/www/cgi-bin/ (jika menggunakan apache server) atau /usr/share/nginx/www/cgi-bin (jika menggunakan nginx server)

#!/usr/bin/perl -wT

#
# sms.cgi
# 

use strict;
use DBI;
use CGI;
use POSIX qw(strftime time);

$CGI::DISABLE_UPLOADS   = 0;
$CGI::POST_MAX          = 1024; # 1KB
 
my $DB   = 'smscgi';
my $HOST = 'localhost';
my $PORT = '3306';
my $USER = 'root';
my $PASS = 'password';
my $SMSDIR = '/var/tmp/sms';
my $TMPFILE;
my ($i, @arr);

my $cgi = new CGI;

if ( $cgi->param('user') && $cgi->param('pass') && $cgi->param('to') && $cgi->param('txt') ) {
  
  my $user = $cgi->param('user');
  my $pass = $cgi->param('pass');
  my $to   = $cgi->param('to');
  my $txt  = $cgi->param('txt');

  my $dbh = DBI->connect("DBI:mysql:database=$DB;host=$HOST;port=$PORT", "$USER", "$PASS")
               || error($cgi, "Can not connect to database.");

  # cek if user exist and is active
  my $sth = $dbh->prepare("select status from users where user=? and pass=?") 
               || error($cgi, "Database error.");
  $sth->execute($user,$pass);

  my ($status);

  if ($status = $sth->fetchrow_array) {
    if ($status == 'active') {
      # write sms
      $TMPFILE = "$SMSDIR/sms.".POSIX::time();
      open (OUT, "> $TMPFILE") || error ($cgi, "Error open file for writing.");

      @arr = split (/,/, $to);
      for ($i=0; $i < @arr; $i++) { 
        print OUT "$arr[$i] $txt\n";
      }
      close OUT;
      success ($cgi);
    } else {
      error ($cgi,"Username $user is not active.");
    }
  } else {
    error ($cgi,"Username $user or password not found.");
  }  
} else {
  error ($cgi,"Query is not complete.");
}

sub success {
  my ($cgi ) = @_;
  print $cgi->header( "text/html" ),
        $cgi->start_html( "SMS sent" ),
        $cgi->h1( "SMS sent"),
        $cgi->end_html;
  exit;
}

sub error {
  my ($cgi, $reason ) = @_;
  print $cgi->header( "text/html" ),
        $cgi->start_html( "Error" ),
        $cgi->h1( "Error" ),
        $cgi->p( "Your request was not procesed because the following error ",
               "occured: " ),
        $cgi->p($cgi->i($reason) ),
        $cgi->end_html;
  exit;
}

Kemudian beri mode executable:


chmod 755 /var/www/cgi-bin/sms.cgi


Restart apache atau nginx. Buka browser dan tes kirim sms.

http://smsserver/cgi-bin/sms.cgi?to=085236001234&txt=Tes%20kirim%20sms

Error

Your request was not procesed because the following error occured:

Query is not complete.

http://smsserver/cgi-bin/sms.cgi?user=user&pass=pass&to=085236001234&txt=Tes%20kirim%20sms

Error

Your request was not procesed because the following error occured:

Username user or password not found.

http://smsserver/cgi-bin/sms.cgi?user=user&pass=password&to=085236001234&txt=Tes%20kirim%20sms

SMS sent

Kirim sms ke lebih dari satu nomor tujuan.
http://smsserver/cgi-bin/sms.cgi?user=user&pass=password&to=085236001234,085236002345,085236003456&txt=Tes%20kirim%20sms

SMS sent


Advertisements

Written by awarmanf

January 4, 2017 at 9:12 am

Posted in Linux, perl

One Response

Subscribe to comments with RSS.

  1. gmn kabarnya mas,saya wowok mantan mega plasma

    Soulmate Sata

    January 30, 2017 at 7:35 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: