-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabsMobile.php
232 lines (199 loc) · 7.36 KB
/
LabsMobile.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/*+**********************************************************************************
* LabsMobile Implementation on vtiger 7 plugin
*
* From: alphanumeric string (max 11 chars) or numeric sender
*
* IMPORTANT
*
* In order to make SMSNotifier Module works, you have to:
* 1. change line 35, in /modules/SMSNotifier/models/Provider.php
*
* Change this:
*
* if (!in_array($file, array('.', '..', 'MyProvider.php', 'CVS'))) {
*
* to this:
*
* if (!in_array($file, array('.', '..', 'MyProvider.php', 'SMSProvider.php'))) {
*
* 2. Copy this file into /modules/SMSNotifier/providers/
*
* 3. Configure your LabsMobile details (username/passsword).
* With you admin user go to SMSNotifier -> Server configuration -> New Configuration.
* Select LabsMobile as supplier, your username and token and the default sender.
************************************************************************************/
class SMSNotifier_LabsMobile_Provider implements SMSNotifier_ISMSProvider_Model {
private $userName;
private $password;
private $parameters = array();
const SERVICE_URI = 'https://api.labsmobile.com';
private static $REQUIRED_PARAMETERS = array(
array('name' => 'Username', 'label' => 'Username', 'type' => 'text'),
array('name' => 'Token', 'label' => 'Token', 'type' => 'password'),
array('name' => 'Sender', 'label' => 'Sender', 'type' => 'text'),
array('name' => 'Charset', 'label' => 'Charset GSM or Unicode', 'type' => 'picklist', 'picklistvalues' => array('1' => 'GSM', '2' => 'Unicode'))
);
/**
* Function to get provider name
* @return <String> provider name
*/
public function getName() {
return 'LabsMobile';
}
/**
* Function to get required parameters other than (userName, password)
* @return <array> required parameters list
*/
public function getRequiredParams() {
return self::$REQUIRED_PARAMETERS;
}
/**
* Function to get service URL to use for a given type
* @param <String> $type like SEND, PING, QUERY
*/
public function getServiceURL($type = false) {
if($type) {
switch(strtoupper($type)) {
case self::SERVICE_AUTH: return self::SERVICE_URI . '/get/auth.php';
case self::SERVICE_SEND: return self::SERVICE_URI . '/get/send.php?';
case self::SERVICE_QUERY: return self::SERVICE_URI . '/get/ack.php?';
}
}
return false;
}
/**
* Function to set authentication parameters
* @param <String> $userName
* @param <String> $password
*/
public function setAuthParameters($userName, $password) {
$this->userName = $userName;
$this->password = $password;
}
/**
* Function to set non-auth parameter.
* @param <String> $key
* @param <String> $value
*/
public function setParameter($key, $value) {
$this->parameters[$key] = $value;
}
/**
* Function to get parameter value
* @param <String> $key
* @param <String> $defaultValue
* @return <String> value/$default value
*/
public function getParameter($key, $defaultValue = false) {
if(isset($this->parameters[$key])) {
return $this->parameters[$key];
}
return $defaultValue;
}
/**
* Function to handle SMS Send operation
* @param <String> $message
* @param <Mixed> $toNumbers One or Array of numbers
*/
public function send($message, $toNumbers) {
if(!is_array($toNumbers)) {
$toNumbers = array($toNumbers);
}
$toNumbers = $this->cleanNumbers($toNumbers);
$clientMessageReference = $this->generateClientMessageReference();
$response = $this->sendMessage($clientMessageReference, $message, $toNumbers);
return $this->processSendMessageResult($response, $clientMessageReference, $toNumbers);
}
private function generateClientMessageReference() {
return uniqid();
}
private function cleanNumbers($numbers) {
$pattern = '/[^\d]/';
$replacement = '';
return preg_replace($pattern, $replacement, $numbers);
}
private function sendMessage($clientMessageReference, $message, $tonumbers) {
$sender = $this->getParameter('Sender', '');
$token = $this->getParameter('Token', '');
$username = $this->getParameter('Username', '');
$charset = $this->getParameter('Charset', '');
$serviceURL = $this->getServiceURL(self::SERVICE_SEND);
$serviceURL = $serviceURL . 'username=' . urlencode($username) . '&';
$serviceURL = $serviceURL . 'password=' . urlencode($token) . '&';
$serviceURL = $serviceURL . 'msisdn=' . urlencode(implode(',', $tonumbers)) . '&';
$serviceURL = $serviceURL . 'message=' . urlencode(html_entity_decode($message)) . '&';
$serviceURL = $serviceURL . 'sender=' . urlencode($sender) . '&';
if($charset === '2') {
$serviceURL = $serviceURL . 'ucs2=1&';
}
$serviceURL = $serviceURL . 'subid=' . urlencode($clientMessageReference);
$httpClient = new Vtiger_Net_Client($serviceURL);
return $serviceURL . $httpClient->doGet(false);
}
private function processSendMessageResult($response, $clientMessageReference, $tonumbers) {
$results = array();
foreach ($tonumbers as $number) {
$result = array();
$result['to'] = $number;
if(stripos($response, "successfully") !== FALSE) {
$result['id'] = $clientMessageReference . '--' . $number;
$result['status'] = self::MSG_STATUS_PROCESSING;
} else {
$result['error'] = true;
$result['statusmessage'] = $response;
}
$results[] = $result;
}
return $results;
}
/**
* Function to get query for status using messgae id
* @param <Number> $messageId
*/
public function query($messageid) {
$messageidSplit = split('--', $messageid);
$clientMessageReference = trim($messageidSplit[0]);
$number = trim($messageidSplit[1]);
$response = $this->queryMessage($clientMessageReference, $number);
return $this->processQueryMessageResult($response, $number);
}
private function queryMessage($clientMessageReference, $number) {
$token = $this->getParameter('Token', '');
$username = $this->getParameter('Username', '');
$serviceURL = $this->getServiceURL(self::SERVICE_QUERY);
$serviceURL = $serviceURL . 'username=' . urlencode($username) . '&';
$serviceURL = $serviceURL . 'password=' . urlencode($token) . '&';
$serviceURL = $serviceURL . 'subid=' . urlencode($clientMessageReference) . '&';
$serviceURL = $serviceURL . 'msisdn=' . urlencode($number);
$httpClient = new Vtiger_Net_Client($serviceURL);
return $httpClient->doPost(array());
}
private function processQueryMessageResult($response) {
$xmlNode = new SimpleXMLElement($response, LIBXML_NOCDATA);
$result = array();
$result['needlookup'] = 1;
switch($xmlNode->status) {
case 'error':
$result['error'] = true;
$result['statusmessage'] = $xmlNode->desc;
$result['status'] = self::MSG_STATUS_FAILED;
break;
case 'processed':
case 'operator':
case 'gateway':
$result['error'] = false;
$result['status'] = self::MSG_STATUS_PROCESSING;
break;
case 'handset':
$result['error'] = false;
$result['status'] = self::MSG_STATUS_DELIVERED;
break;
}
return $result;
}
function getProviderEditFieldTemplateName() {
return 'BaseProviderEditFields.tpl';
}
}
?>