-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosms.pm
executable file
·166 lines (117 loc) · 4.38 KB
/
autosms.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package VCS::Site::autosms;
use strict;
use utf8;
use Math::Random::Secure qw( irand );
use Digest::MD5 qw( md5_hex );
use Digest::SHA1 qw( sha1_hex );
use HTTP::Request::Common;
use Data::Dumper;
sub new
# //////////////////////////////////////////////////
{
my ( $class, $pclass, $vars ) = @_;
my $self = bless {}, $pclass;
$self->{ 'VCS::Vars' } = $vars;
return $self;
}
sub get_phone_for_sms
# //////////////////////////////////////////////////
{
my ( $self, $without_app ) = @_;
my $phone = undef;
if ( $without_app ) {
my ( $app_phone, $appdata_phone ) = $self->{ af }->query( 'sel1', __LINE__, "
SELECT AutoAppointments.Phone, AutoAppData.AppPhone
FROM AutoToken
JOIN AutoAppointments ON AutoToken.AutoAppID = AutoAppointments.ID
LEFT JOIN AutoAppData ON AutoToken.AutoAppDataID = AutoAppData.ID
WHERE Token = ?", $self->{ token }
);
$phone = ( !$appdata_phone ? $app_phone : $appdata_phone );
}
else {
$phone = $self->{ af }->query( 'sel1', __LINE__, "
SELECT Phone FROM Appointments JOIN AutoToken ON Appointments.ID = AutoToken.CreatedApp
WHERE AutoToken.Token = ?", $self->{ token }
);
}
return $phone;
}
sub get_code_from_db
# //////////////////////////////////////////////////
{
my ( $self, $without_app ) = @_;
my ( $remote_id, $sms_code ) = ( 0, 0 );
if ( $without_app ) {
( $remote_id, $sms_code ) = $self->{ af }->query( 'sel1', __LINE__, "
SELECT DocUploadedAgreements.ID, SMScode FROM DocUploadedAgreements
JOIN AutoToken ON AutoToken.ID = DocUploadedAgreements.Token
WHERE AutoToken.Token = ?", $self->{ token }
);
}
else {
( $remote_id, $sms_code ) = $self->{ af }->query( 'sel1', __LINE__, "
SELECT AutoRemote.ID, SMScode FROM AutoRemote
JOIN AutoToken ON AutoToken.CreatedApp = AutoRemote.AppID
WHERE Token = ?", $self->{ token }
);
}
return ( $remote_id, $sms_code );
}
sub get_code_for_sms
# //////////////////////////////////////////////////
{
my ( $self, $phone, $without_app ) = @_;
my $config = VCS::Site::autodata::get_settings()->{ sms };
my ( $remote_id, $sms_code ) = get_code_from_db( $self, $without_app );
return md5_hex( $sms_code ) if $sms_code;
my $new_code = int( irand( 9000 ) ) + 1000;
my $sms_id = 0;
$sms_id = sending_sms( $self, $phone, "Vash kod dogovora $new_code" ) unless $config->{ do_not_send_sms };
my $table = ( $without_app ? "DocUploadedAgreements" : "AutoRemote" );
$self->{ af }->query( 'query', __LINE__, "
UPDATE $table SET SMScode = ?, SMSmsgID = ? WHERE ID = ?", {}, $new_code, $sms_id, $remote_id
);
return md5_hex( $new_code );
}
sub get_signed
# //////////////////////////////////////////////////
{
my $self = shift;
my $sign_date = $self->query( 'sel1', __LINE__, "
SELECT SMSsigned FROM DocUploadedAgreements
JOIN AutoToken ON AutoToken.ID = DocUploadedAgreements.Token
WHERE AutoToken.Token = ?", $self->{ token }
);
return $sign_date;
}
sub code_from_sms_is_ok
# //////////////////////////////////////////////////
{
my ( $self, $code, $without_app ) = @_;
my ( $remote_id, $sms_code ) = get_code_from_db( $self, $without_app );
return 0 unless $sms_code eq $code;
my $table = ( $without_app ? "DocUploadedAgreements" : "AutoRemote" );
$self->{ af }->query( 'query', __LINE__, "
UPDATE $table SET SMSsigned = now() WHERE ID = ?", {}, $remote_id
);
return 1;
}
sub sending_sms
# //////////////////////////////////////////////////
{
my ( $self, $phone, $message ) = @_;
my $sms = VCS::Site::autodata::get_settings()->{ sms };
my $sms_sign = join( ';', sort ( $sms->{ project }, $sms->{ sender }, $message, $phone ) ) . ';' . $sms->{ key };
$sms_sign = md5_hex( sha1_hex( $sms_sign ) );
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; X11; Linux i686; en) Opera 7.60');
my $request_url = $sms->{ send_url } . '?project=' . $sms->{ project } . '&sender=' . $sms->{ sender } .
'&message=' . $message . '&recipients=' . $phone . '&sign=' . $sms_sign;
my $response = LWP::UserAgent->new( timeout => 30 )->get( $request_url );
return 0 unless $response->is_success;
return 0 unless $response->content =~ /\"status\"\:\"success\"/;
return 0 unless $response->content =~ /\"messages\_id\"\:\[([0-9]+)\]/;
return $1;
}
1;