-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsms_ru.module
432 lines (399 loc) · 12.7 KB
/
sms_ru.module
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
define('SMS_RU_ACCESS_ADMINISTER', 'administer sms ru');
define('SMS_RU_ACCESS_SEND_SMS', 'send sms ru');
/**
* Implements hook_menu().
*/
function sms_ru_menu() {
$items = array();
$items['admin/config/sms-ru'] = array(
'title' => 'SMS.RU',
'page callback' => 'drupal_get_form',
'page arguments' => array('sms_ru_admin_form'),
'access arguments' => array('administer sms ru'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
*
*
* @param
*
* @return
*
*/
function sms_ru_admin_form($form, &$form_state) {
if (isset($form_state['values'])) {
$values = $form_state['values'];
} else {
$values = sms_ru_settings_get();
}
$values+=array(
'account' => array(),
'send_test' => array(),
);
$values['service']+=array('url' => 'http://sms.ru/sms/send');
$values['account']+=array('api_id' => '', 'admin_phone' => '');
$values['send_test']+=array('text' => '');
if (!isset($values['debug_mode'])) {
$values['debug_mode'] = FALSE;
}
$form['service'] = array(
'#type' => 'fieldset',
'#title' => t('Service sms.ru settings'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
$form['service']['url'] = array(
'#type' => 'textfield',
'#title' => t('Query URL'),
'#description' => t(''),
'#default_value' => $values['service']['url'],
'#required' => TRUE,
'#size' => 60,
'#maxlength' => 128,
);
$form['account'] = array(
'#type' => 'fieldset',
'#title' => t('Account sms.ru'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
$form['account']['api_id'] = array(
'#type' => 'textfield',
'#title' => t('API ID'),
'#description' => t(''),
'#default_value' => $values['account']['api_id'],
'#required' => TRUE,
'#size' => 60,
'#maxlength' => 128,
);
$form['account']['admin_phone'] = array(
'#type' => 'textfield',
'#title' => t('Admin phone number'),
'#description' => t(''),
'#default_value' => $values['account']['admin_phone'],
'#size' => 32,
'#maxlength' => 32,
);
$form['send_test'] = array(
'#type' => 'fieldset',
'#title' => t('Test send sms'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
$form['send_test']['text'] = array(
'#type' => 'textarea',
'#title' => t('Text sms'),
'#description' => t(''),
'#default_value' => $values['send_test']['text'],
'#rows' => 2,
);
$form['send_test']['message'] = array(
'#type' => 'markup',
'#markup' => '<div id="send-test-message" class="message"></div>',
);
$form['send_test']['send'] = array(
'#type' => 'submit',
'#value' => t('Send'),
'#ajax' => array(
'callback' => 'sms_ru_admin_form_send_sms_ajax_callback',
)
);
$form['debug_mode'] = array(
'#type' => 'checkbox',
'#title' => t('Debug mode'),
'#default_value' => $values['debug_mode'],
'#description' => t('Enable logging service answers'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
return $form;
}
/**
*
*
* @param
*
* @return
*
*/
function sms_ru_admin_form_send_sms_ajax_callback($form, &$form_state) {
if (isset($form_state['clicked_button']['#parents'][1]) && $form_state['clicked_button']['#parents'][1] == 'send') {
$text = $form_state['values']['send_test']['text'];
$phone = $form_state['values']['account']['admin_phone'];
$result = sms_ru_sms_send($text, $phone);
drupal_set_message($result['message'], $result['status']);
$commands = array();
$commands[] = ajax_command_replace('#send-test-message', '<div id="send-test-message">' . theme('status_messages') . '</div>');
return array('#type' => 'ajax', '#commands' => $commands);
}
}
function sms_ru_admin_form_submit($form, &$form_state) {
$fields = array('service', 'account', 'debug_mode');
$values = array_intersect_key($form_state['values'], array_combine($fields, $fields));
variable_set('sms_ru_admin_form', $values);
}
/**
*
*
* @param
*
* @return
*
*/
function sms_ru_access_check($access_permission) {
switch ($access_permission) {
case SMS_RU_ACCESS_ADMINISTER:
if (user_access(SMS_RU_ACCESS_ADMINISTER)) {
return TRUE;
} else {
drupal_set_message(t('You have no rights to a administer sms send settings'), 'error');
return FALSE;
}
break;
case SMS_RU_ACCESS_SEND_SMS:
if (user_access(SMS_RU_ACCESS_SEND_SMS)) {
return TRUE;
} else {
drupal_set_message(t('You have no rights to a Send sms from site'), 'error');
return FALSE;
}
break;
}
}
/**
*
*
* @param
*
* @return
*
*/
function sms_ru_settings_get() {
$info = variable_get('sms_ru_admin_form', FALSE);
if (!$info) {
drupal_set_message('SMS RU settings - missing!</br> See : @link', array('@link' => sms_ru_link('settings')));
}
return $info;
}
/**
*
*
* @param
*
* @return
*
*/
function sms_ru_link($op = 'settings') {
switch ($op) {
case 'settings':
$link = l(t('Sms RU settings'), 'admin/config/sms-ru');
break;
default:
$link = FALSE;
}
return $link;
}
/**
*
*
* @param
*
* @return
*
*/
function sms_ru_sms_send($text, $phone_numbers) {
if (!sms_ru_access_check(SMS_RU_ACCESS_SEND_SMS)) {
return FALSE;
}
$info = sms_ru_settings_get();
if (!$info) {
return FALSE;
}
if (!is_array($phone_numbers)) {
$phone_numbers = array($phone_numbers);
}
$ch = curl_init($info['service']['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"api_id" => $info['account']['api_id'],
"to" => implode(',', $phone_numbers),
"text" => $text
));
$result = curl_exec($ch);
curl_close($ch);
$code = substr($result, 0, 3);
$message = sms_ru_service_messages($code);
$return = array('message' => $message['text'], 'status' => $message['status'], 'data' => $result);
if ($info['debug_mode']) {
watchdog('sms_ru', print_r($return, TRUE));
}
return $return;
}
/**
*
*
* @param
*
* @return
*
*/
function sms_ru_service_messages($code) {
$messages = &drupal_static(__FUNCTION__);
if ($messages == NULL) {
$messages = array(
// 100 Сообщение принято к отправке. На следующих строчках вы найдете идентификаторы отправленных сообщений в том же порядке, в котором вы указали номера, на которых совершалась отправка.
'100' => array(
'text' => t('(Code: 100) message is accepted for delivery. In the following lines you will find the identifiers of messages sent in the same order in which you entered the room , which was accomplished shipping.'),
'status' => 'status'
),
// 200 Неправильный api_id
'200' => array(
'text' => t('(Code: 200) Wrong api_id'),
'status' => 'error'
),
//201 Не хватает средств на лицевом счету
'201' => array(
'text' => t('(Code: 201) Not enough money to personal account'),
'status' => 'error'
),
//202 Неправильно указан получатель
'202' => array(
'text' => t('(Code: 202) Incorrect recipient'),
'status' => 'error'
),
//203 Нет текста сообщения
'203' => array(
'text' => t('(Code: 203) No text messages'),
'status' => 'error'
),
//204 Имя отправителя не согласовано с администрацией
'204' => array(
'text' => t('(Code: 204) Name of the sender is not in accord with the administration'),
'status' => 'error'
),
//205 Сообщение слишком длинное (превышает 8 СМС)
'205' => array(
'text' => t('(Code: 205) Message too long (more than 8 SMS)'),
'status' => 'error'
),
//206 Будет превышен или уже превышен дневной лимит на отправку сообщений
'206' => array(
'text' => t('(Code: 206) Is exceeded or already exceeded the daily limit for sending messages'),
'status' => 'error'
),
//207 На этот номер (или один из номеров) нельзя отправлять сообщения, либо указано более 100 номеров в списке получателей
'207' => array(
'text' => t('(Code: 207) This number (or one of the rooms) can not send messages , or specified more than 100 rooms in the recipient list'),
'status' => ''
),
//208 Параметр time указан неправильно
'208' => array(
'text' => t('(Code: 208) The time parameter is incorrect'),
'status' => 'error'
),
//209 Вы добавили этот номер (или один из номеров) в стоп-лист
'209' => array(
'text' => t('(Code: 209) You have added the number (or one of the rooms) to the stop list'),
'status' => 'error'
),
//210 Используется GET, где необходимо использовать POST
'210' => array(
'text' => t('(Code: 210) Use GET, where you must use POST'),
'status' => 'error'
),
//211 Метод не найден
'211' => array(
'text' => t('(Code: 211) Method not found'),
'status' => 'error'
),
//212 Текст сообщения необходимо передать в кодировке UTF-8 (вы передали в другой кодировке)
'212' => array(
'text' => t('(Code: 212) Message to be transmitted in UTF- 8 (you passed to the other encoding)'),
'status' => 'error'
),
//220 Сервис временно недоступен, попробуйте чуть позже.
'220' => array(
'text' => t('(Code: 220) Service temporarily unavailable , please try later.'),
'status' => 'error'
),
//230 Сообщение не принято к отправке, так как на один номер в день нельзя отправлять более 250 сообщений.
'230' => array(
'text' => t('(Code: 230) Message is not accepted for delivery, as there is one room on the day you can not send more than 250 messages.'),
'status' => 'error'
),
//300 Неправильный token (возможно истек срок действия, либо ваш IP изменился)
'300' => array(
'text' => t('(Code: 300) Invalid token (possibly expired or your IP has changed)'),
'status' => 'error'
),
//301 Неправильный пароль, либо пользователь не найден
'301' => array(
'text' => t('(Code: 301) Wrong password or user not found'),
'status' => 'error'
),
//302 Пользователь авторизован, но аккаунт не подтвержден (пользователь не ввел код, присланный в регистрационной смс)
'302' => array(
'text' => t('(Code: 302) Is authorized user , but the account is not confirmed (the user enters a code sent in the registration sms)'),
'status' => 'error'
),
);
}
return isset($messages[$code]) ? $messages[$code] : FALSE;
}
/**
* Implements hook_rules_action_info().
*/
function sms_ru_rules_action_info() {
return array(
'sms_ru_rules_action_sms_send' => array(
'label' => t('Send sms over sms.ru'),
'group' => t('SMS'),
'parameter' => array(
'sms_text' => array(
'type' => 'text',
'label' => t('SMS text'),
'description' => t("The sms message body."),
),
'phone_number' => array(
'type' => 'text',
'label' => t('Phone number'),
'description' => t('The destination SMS number'),
),
),
// 'access callback' => 'sms_rules_access_callback',
),
);
}
/**
*
*
* @param
*
* @return
*
*/
function sms_ru_rules_action_sms_send($sms_text, $phone_number) {
sms_ru_sms_send($sms_text, $phone_number);
}
function sms_ru_permission() {
return array(
'administer sms ru' => array(
'title' => t('Administer sms.ru module'),
'description' => t('Administer sms.ru module.'),
),
'send sms ru' => array(
'title' => t('Send sms over sms.ru'),
'description' => t('Send sms over sms.ru'),
),
);
}