-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
171 lines (167 loc) · 6.68 KB
/
externallib.php
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
167
168
169
170
171
<?php
require_once("$CFG->libdir/externallib.php");
require_once("$CFG->dirroot/auth/twiliootp/twilio/vendor/autoload.php");
require_once("$CFG->dirroot/auth/twiliootp/lib.php");
require_once("$CFG->dirroot/auth/twiliootp/twiliootperrorcodes.php");
use Twilio\Rest\Client;
class moodle_auth_twiliootp_external extends external_api {
public static function twiliootp_js_method_parameters() {
return new external_function_parameters(
array(
'flag' => new external_value(PARAM_RAW, 'see flag'),
'phone' => new external_value(PARAM_RAW, 'see flag'),
'username' => new external_value(PARAM_RAW, 'see flag'),
'useremail' => new external_value(PARAM_RAW, 'see flag'),
'usercountry' => new external_value(PARAM_RAW, 'see flag')
)
);
}
public static function twiliootp_js_method_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_INT, 'Status code: see TwilioOTPErrorCodes'),
'errors' => new external_multiple_structure(
new external_value(PARAM_INT, 'Error code')
)
)
);
}
public static function twiliootp_js_method($flag,$phone,$username,$useremail,$usercountry) {
global $DB,$SITE;
$plugin = get_auth_plugin('twiliootp');
$sid = $plugin->config->twilio_ssid;
$token = $plugin->config->twilio_token;
$otp_validity = $plugin->config->validityperiod;
$otp_validity_sec = $otp_validity * 60;
$country_code = country_code_twiliootp($usercountry);
$sitename = $SITE->fullname;
$twilio = new Client($sid, $token);
$otp = mt_rand(1000, 9999);
$otp_expiry = time() + $otp_validity_sec;
$tonumber = $country_code.''.$phone;
$fromnumber = $plugin->config->twilio_number;
$error = false;
$errors = array();
$isusername = $DB->get_record('user',array('username' => $username));
$isuseremail = $DB->get_record('user',array('email' => $useremail));
$isuserphone = $DB->get_record('user',array('phone2' => $phone));
if(isset($isusername) && !empty($isusername)){
$errors[] = TwilioOTPErrorCodes::USERNAME_TAKEN;
}
if(isset($isuseremail) && !empty($isuseremail)){
$errors[] = TwilioOTPErrorCodes::EMAIL_TAKEN;
}
/*if(isset($isuserphone) && !empty($isuserphone)){
$status = 6;
//$error = true;
$errors[] = TwilioOTPErrorCodes::PHONE_TAKEN;
}*/
$str = array("otp" => $otp, "sitename" => $sitename, "otp_validity" => $otp_validity);
if(empty($error)){
try {
$lookup = $twilio->lookups->v1->phoneNumbers($phone)->fetch(array("countryCode" => $usercountry));
try {
$message = $twilio->messages
->create("whatsapp:".$lookup->phoneNumber, // to
array(
"from" => "whatsapp:".$fromnumber,
"body" => get_string('otpmessage', 'auth_twiliootp', $str)
)
);
//$status = 1;
} catch (\Twilio\Exceptions\TwilioException $e) {
$errors[] = TwilioOTPErrorCodes::TWILIO_ERROR;
} catch (Exception $e) {
$errors[] = TwilioOTPErrorCodes::TWILIO_ERROR;
}
} catch (Exception $e) {
$errors[] = TwilioOTPErrorCodes::INVALID_PHONE;
}
}
$result = array();
if (empty($errors)) {
$data = new stdClass();
$data->username = $username;
$data->email = $useremail;
$data->phone = $phone;
$data->countrycode = $usercountry;
$data->otp_code = $otp;
$data->otp_expiry = $otp_expiry;
$data->verification_status = '';
$data->otpcreated = time();
$data->attempts_count = '';
$DB->insert_record('auth_twiliootp_create', $data);
$result['status'] = TwilioOTPErrorCodes::SUCCESS;
$result['errors'] = $errors;
} else {
$result['status'] = TwilioOTPErrorCodes::GENERAL_ERROR; // You might define a general error code for unspecified issues
$result['errors'] = $errors;
}
return $result;
}
public static function success_twiliootp_url_parameters() {
return new external_function_parameters(
array(
'flag' => new external_value(PARAM_RAW, 'see flag'),
'phone' => new external_value(PARAM_RAW, 'see flag'),
'username' => new external_value(PARAM_RAW, 'see flag'),
'useremail' => new external_value(PARAM_RAW, 'see flag'),
'otp' => new external_value(PARAM_RAW, 'see flag')
)
);
}
public static function success_twiliootp_url_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_RAW, 'status: true if success'),
'errors' => new external_multiple_structure(
new external_value(PARAM_INT, 'Error code')
)
)
);
}
public static function success_twiliootp_url($flag,$phone,$username,$useremail,$otp) {
global $DB;
$plugin = get_auth_plugin('twiliootp');
$otp_record = $DB->get_record_sql('SELECT * FROM {auth_twiliootp_create} WHERE username = ? AND email = ? AND phone = ? ORDER BY id DESC LIMIT 1',[$username,$useremail,$phone]);
$errors = array();
$verification_status = false;
if ($otp_record) {
if ($otp_record->otp_code == $otp) {
$current_time = time();
$otp_expiry_time = $otp_record->otp_expiry;
if ($current_time <= $otp_expiry_time) {
$verification_status = true;
} else {
$verification_status = false;
$errors[] = TwilioOTPErrorCodes::OTP_EXPIRED;
}
} else {
$verification_status = false;
$errors[] = TwilioOTPErrorCodes::INVALID_OTP;
}
} else {
$verification_status = false;
$errors[] = TwilioOTPErrorCodes::OTP_RECORD_NOT_FOUND;
}
$previousattempt = $otp_record->attempts_count;
if (empty($errors)) {
$updateobj = new stdClass();
$updateobj->id = $otp_record->id;
$updateobj->verification_status = 1;//1=verified 0=not verfied
$updateobj->attempts_count = $previousattempt + 1;
$DB->update_record('auth_twiliootp_create', $updateobj, true);
$result['status'] = TwilioOTPErrorCodes::SUCCESS;
$result['errors'] = $errors;
} else {
$updateobj = new stdClass();
$updateobj->id = $otp_record->id;
$updateobj->attempts_count = $previousattempt + 1;
$DB->update_record('auth_twiliootp_create', $updateobj, true);
$result['status'] = TwilioOTPErrorCodes::GENERAL_ERROR; // You might define a general error code for unspecified issues
$result['errors'] = $errors;
}
return $result;
die;
}
}