More work on circulation dialog. Adding files and styles necessary to do drop-shadow...
[koha.git] / C4 / SMS.pm
blob319ac33ff4525f923c4f3680847cca4d7833f576
1 package C4::SMS;
2 #Written by tgarip@neu.edu.tr for SMS message sending and other SMS related services
4 use strict;
5 require Exporter;
6 use LWP::UserAgent;
7 use C4::Context;
8 use vars qw($VERSION @ISA @EXPORT);
9 $VERSION = 0.01;
10 my $user=C4::Context->config('smsuser');
11 my $pwd=C4::Context->config('smspass');
12 my $uri ="https://spgw.kktcell.com/smshttpproxy/SmsHttpProxyServlet";
16 @ISA = qw(Exporter);
18 @EXPORT = qw(
19 &get_sms_auth
20 &send_sms
21 &read_sms
22 &error_codes
23 &parse_phone
24 &parse_message
25 &write_sms
26 &mod_sms
27 &kill_sms
30 sub get_sms_auth {
31 my $ua = LWP::UserAgent->new;
32 my $commands;
33 my $res=$ua->post($uri,[cmd=>'REGISTER',pUser=>$user,pPwd=>$pwd]);
34 if ($res->is_success){
35 $commands=parse_content($res->content);
37 return($commands,$ua);
40 sub send_sms{
41 my $ua=shift;
42 my $phone=shift;
43 my $message=shift;
44 my $session=shift;
45 my $res=$ua->post($uri,[cmd=>'SENDSMS',pUser=>$user,pPwd=>$pwd,pSessionId=>$session,pService_Code=>4130,pMsisdn=>$phone,
46 pContent=>$message]);
47 return parse_content($res->content);
49 sub read_sms{
50 my $ua=shift;
51 my $session=shift;
52 my $res=$ua->post($uri,[cmd=>'GETSMS',pUser=>$user,pPwd=>$pwd,pSessionId=>$session,pService_Code=>4130]);
53 return parse_content($res->content);
55 sub parse_content{
56 my $content=shift;
57 my %commands;
58 my @attributes=split /&/,$content;
59 foreach my $params(@attributes){
60 my (@param)=split /=/,$params;
61 $commands{$param[0]}=$param[1];
63 return(\%commands);
66 sub error_codes{
67 my $error=shift;
68 if ($error==-1){
69 return "Closed session - Retry ";
70 }elsif($error==-2){
71 return "Invalid session - Retry ";
72 }elsif($error==-3){
73 return "Invalid password" ;
74 }elsif($error==-103){
75 return "Invalid user";
76 }elsif($error==-422){
77 return "Invalid Parameter";
78 }elsif($error==-426){
79 return "User doesn’t have permission to send message";
80 }elsif($error==-700){
81 return "No permission";
82 }elsif($error==-801){
83 return " Msdisn count differs-warn administartor";
84 }elsif($error==-803){
85 return "Content count differs from XSER count";
86 }elsif($error==-1101){
87 return " Insufficient Credit Do not retry" ;
88 }elsif($error==-1104){
89 return "Invalid Phone number";
90 }elsif($error==-10001){
91 return " Internal system error- Tell Turkcell/Telsim";
92 }elsif($error==-9005){
93 return " No messages to read";
94 }elsif ($error){
95 return "Unknow error no $error occured - tell Turkcell/Telsim";
99 sub parse_phone{
100 ## checks acceptable phone numbers
101 ## Fix to accept Telsim when available (542 numbers)
102 my $phone=shift;
103 $phone=~s/^0//g;
104 $phone=~s/ //g;
105 my $length=length($phone);
106 if ($length==10 || $length==12){
107 my $code=substr($phone,0,3) if $length==10;
108 $code=substr($phone,0,5) if $length==12;
109 if ($code=~/533/){
110 return $phone;
111 }else{
112 return 0;
114 }else{
115 return 0;
119 sub parse_message{
120 my $message=shift;
121 $message=~s/ / /g;
122 my @parsed=split / /,$message;
123 return (@parsed);
126 sub write_sms{
127 my ($userid,$message,$phone)=@_;
128 my $dbh=C4::Context->dbh;
129 my $sth=$dbh->prepare("INSERT into sms_messages(userid,message,user_phone,date_received) values(?,?,?,now())");
130 $sth->execute($userid,$message,$phone);
131 $sth->finish;
132 return $dbh->{'mysql_insertid'};
135 sub mod_sms{
136 my ($smsid,$message)=@_;
137 my $dbh=C4::Context->dbh;
138 my $sth=$dbh->prepare("UPDATE sms_messages set reply=? ,date_replied=now() where smsid=?");
139 $sth->execute($message,$smsid);
140 $sth->finish;
142 sub kill_sms{
143 #end a session
144 my $ua=shift;
145 my $session=shift;
146 my $res=$ua->post($uri,[cmd=>'KILLSESSION',pSessionId=>$session]);
149 __END__