/[drupal]/contributions/modules/smsgateway/smsgateway.module
ViewVC logotype

Contents of /contributions/modules/smsgateway/smsgateway.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Mon Jan 29 03:21:29 2007 UTC (2 years, 9 months ago) by therave
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
Changes since 1.5: +88 -102 lines
File MIME type: text/x-php
Converted to support drupal 5.0
1 <?php
2 // $Id: smsgateway.module,v 1.5 2006/11/14 22:46:23 therave Exp $
3
4 /**
5 * @author David Hamilton, Jiffle.net
6 * @file
7 * This is a drupal module to provide an SMS gateway node type to allow the sending and receipt of SMS messages.
8 * Uses a gateway include file for the actual sending and receipt (parsing) of messages.
9 *
10 */
11
12 /**
13 * Return a list of all available gateway modules.
14 */
15 function _smsgateway_gateway_types() {
16 static $gateways;
17
18 if (!isset($gateways)) {
19 $gateways = array();
20 $path = drupal_get_path('module', 'smsgateway');
21 $files = file_scan_directory($path, '^gateway_.*\.inc$');
22 foreach ($files as $filename => $file) {
23 include_once($filename);
24 $gateways[] = drupal_substr($file->name, 8);
25 }
26 }
27 return $gateways;
28 }
29
30 /**
31 * Ensure that specified gateway is included.
32 */
33 function _smsgateway_ensuregateway( $gateway) {
34 $path = drupal_get_path('module', 'smsgateway');
35 $files = file_scan_directory($path, '^gateway_'. $gateway .'\.inc$');
36 foreach ($files as $filename => $file) {
37 include_once($filename);
38 }
39 }
40
41 /**
42 * Invoke a gateway hook.
43 */
44 function _smsgateway_invoke($hook, $gateway, $a1 = NULL, $a2 = NULL, $a3 = NULL) {
45 $function = 'smsgateway_gateway_'. $gateway .'_'. $hook;
46
47 if (function_exists($function)) {
48 return ($function($a1, $a2, $a3));
49 }
50 }
51
52 /**
53 * Menu Hook Implementation hook_menu()
54 * Sets up the definitions for the menuing system.
55 * @return array of menu definition objects
56 */
57 function smsgateway_menu() {
58 $items = array();
59 $items[] = array('path' => 'admin/smsgateway',
60 'title' => t('SMS Gateway'),
61 'callback' => '_smsgateway_logs',
62 'access' => user_access('administer smsgateway'));
63 $items[] = array('path' => 'admin/smsgateway/test',
64 'title' => t('Test'),
65 'callback' => '_smsgateway_test',
66 'access' => user_access('administer smsgateway'),
67 'type' => MENU_LOCAL_TASK);
68 $items[] = array('path' => 'admin/smsgateway/logs',
69 'title' => t('Logs'),
70 'callback' => '_smsgateway_logs',
71 'access' => user_access('administer smsgateway'),
72 'type' => MENU_DEFAULT_LOCAL_TASK);
73 $items[] = array('path' => 'admin/smsgateway/logs/clear', 'title' => t('Clear Logs'),
74 'access' => user_access('administer smsgateway'),
75 'callback' => '_smsgateway_clearlogs',
76 'type' => MENU_CALLBACK);
77 $items[] = array('path' => 'smsgateway/in', 'title' => t('SMS Update'),
78 'access' => user_access('receive sms message'),
79 'callback' => '_smsgateway_rxmessage',
80 'type' => MENU_CALLBACK);
81 $items[] = array('path' => 'admin/settings/smsgateway',
82 'title' => t('SMS Gateway'),
83 'description' => t('Configure SMS for a SMS Service Provider.'),
84 'callback' => 'drupal_get_form',
85 'callback arguments' => array('smsgateway_admin_settings'),
86 'access' => user_access('admin site configuration'),
87 'type' => MENU_NORMAL_ITEM);
88 return $items;
89 }
90
91 /**
92 * Log a sms message.
93 *
94 * @param $destination
95 * The destination telephone number.
96 * @param $message
97 * The message sent.
98 * @param $response
99 * the response received from the gateway.
100 */
101 function _smsgateway_smslog($destination, $message, $response, $incoming = false) {
102 db_query('INSERT INTO {smslog} (direction, number, message, response, timestamp) VALUES ( \'%s\', \'%s\', \'%s\', \'%s\', \'%d\')', $incoming ? 'IN' : 'OUT', $destination, $message, $response, time());
103 }
104
105 /**
106 * Callback from gateway server to allow to to receive messages. Alpha status.
107 *
108 * Test URL: {site URL}/smsgateway/in?api_id=12345&from=441234567890&to=440987654321&timestamp=2005-10-21+18:52:10&text=The%20message%20text&charset=ISO-8859-1&udh=
109 */
110 function _smsgateway_rxmessage() {
111 $output = '';
112 $smsgateway_gateway = variable_get('smsgateway_gateway', '');
113 if( $smsgateway_gateway == '') {
114 _smsgateway_smslog( 'N/A', 'Message Received but no gateway configured', 'N/A', true);
115 }
116 else {
117 _smsgateway_ensuregateway( $smsgateway_gateway);
118 $message = _smsgateway_invoke('parsereceived', $smsgateway_gateway);
119
120 _smsgateway_smslog( $message['sender_number'], $message['body'], $message['timestamp'], true);
121
122 module_invoke_all( 'smsgateway_newmessage', $message);
123
124 /* $output .= '<p>Api ID = '. $message['account_id'] .'</p>';
125 $output .= '<p>From = '. $message['sender_number'] .'</p>';
126 $output .= '<p>To = '. $message['destination_number'] .'</p>';
127 $output .= '<p>Timestamp = '. $message['timestamp'] .'</p>';
128 $output .= '<p>Charset = '. $message['charset'] .'</p>';
129 $output .= '<p>UDH = '. $message['header'] .'</p>';
130 $output .= '<p>Message = '. $message['body'] .'</p>';
131 print theme( 'page', $output);*/
132 }
133 }
134
135 /**
136 * Confirm clearing the logs
137 */
138 function _smsgateway_clearlogs() {
139 return drupal_get_form('smsgateway_clearlogs_confirm', null);
140 } // function _smsgateway_clearlogs
141
142 /** builds a confirmation form for clearing the logs
143 */
144 function smsgateway_clearlogs_confirm( $unused) {
145 return confirm_form( array(),
146 t('Are you sure you want to clear all the SMS logs.'),
147 'admin/smsgateway/logs', t('This action cannot be undone.'),
148 t('Clear Logs'));
149 } // function smsgateway_clearlogs_confirm
150
151 /** handles the submit for the clear logs form */
152 function smsgateway_clearlogs_confirm_submit() {
153 db_query('TRUNCATE {smslog}');
154 drupal_set_message(t('SMS logs cleared.'));
155 drupal_goto('admin/smsgateway/logs');
156 } // function smsgateway_clearlogs_confirm_submit
157
158
159 /**
160 * View message sending logs
161 */
162 function _smsgateway_logs() {
163
164 $op = $_POST['op'];
165 if ($op == t('Clear Logs')) {
166 return _smsgateway_clearlogs();
167 }
168
169 $header = array(
170 array('data' => t('Date'), 'field' => 's.sid', 'sort' => 'desc'),
171 array('data' => t('Type'), 'field' => 's.direction'),
172 array('data' => t('Number'), 'field' => 's.number'),
173 array('data' => t('Message'), 'field' => 's.message'),
174 array('data' => t('Response'), 'field' => 's.response'),
175 );
176
177 $sql = sprintf('SELECT s.* FROM {smslog} s %s', tablesort_sql($header));
178 $result = pager_query($sql, 50);
179 while ($smslog = db_fetch_object($result)) {
180 $rows[] = array('data' =>
181 array(
182 // Cells
183 format_date($smslog->timestamp, 'small'),
184 t($smslog->direction),
185 truncate_utf8($smslog->number, 14),
186 truncate_utf8($smslog->message, 80),
187 truncate_utf8($smslog->response, 32)
188 ),
189 // Attributes for tr
190
191 );
192 }
193
194 if (!$rows) {
195 $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '5'));
196 }
197
198 $pager = theme('pager', NULL, 50, 0);
199 if (!empty($pager)) {
200 $rows[] = array(array('data' => $pager, 'colspan' => '5'));
201 }
202
203 //$output = '<div class="container-inline">'. form_render($form) .'</div>';
204 $output .= '<div id="smsgateway-log">' . theme('table', $header, $rows) . '</div>';
205
206 $output .= drupal_get_form('smsgateway_clearlogs_form', null);
207 return $output;
208 } // function _smsgateway_logs
209
210 /** creates clear logs form */
211 function smsgateway_clearlogs_form( $record) {
212 $form[] = array(
213 '#type' => 'submit',
214 '#value' => t('Clear Logs'),
215 );
216 return $form;
217 } // function smsgateway_logs
218
219 /**
220 * Test message sending page
221 */
222 function _smsgateway_test() {
223 // content variable that will be returned for display
224 $page_content = '';
225 $page_content .= drupal_get_form('smsgateway_test_form');
226 return $page_content;
227 } // function smsgateway_all
228
229 /** Creates the test form
230 */
231 function smsgateway_test_form() {
232 $form["smsgatewaytest_destination"] = array(
233 '#type' => 'textfield',
234 '#title' => t("Test Mobile Number"),
235 '#default_value' => variable_get("smsgatewaytest_destination", ""),
236 '#size' => 25,
237 '#maxlength' => 40,
238 '#description' => t("The number to be used as the destination for the test.", NULL, TRUE),
239 );
240 $form['smsgatewaytest_usebulk'] = array(
241 '#type' => 'checkbox',
242 '#title' => t("Use Bulk Sending for test"),
243 '#return_value' => 1,
244 '#default_value' => '0',
245 '#description' => "Whether the multiple message send approach is used",
246 );
247 $form['submit'] = array(
248 '#type' => 'submit',
249 '#value' => t('Submit'),
250 );
251
252 return $form;
253 } // function smsgateway_test_form
254
255 /** Submits the test form data
256 */
257 function smsgateway_test_form_submit( $form_id, $form_values) {
258 // SECURITY - strip non-numerics from number
259 $destination = ereg_replace('[^0-9]', '', trim( $form_values['smsgatewaytest_destination']));
260 // SECURITY - use int conversion on numerics
261 $usebulk = intval( $form_values['smsgatewaytest_usebulk']);
262 $page_content = '<p>Test Message sent to '. $destination .' using ';
263 $page_content .= $usebulk == '1' ? 'bulk' : 'single';
264 $page_content .= ' send.</p>';
265 if ($usebulk == '1') {
266 $messageset = array();
267 $messageset['1']['destination_number'] = $destination;
268 $messageset['1']['message_body'] = 'First bulk test SMS message from Drupal installation at '. $_SERVER['SERVER_NAME'] .'.';
269 $messageset['2']['destination_number'] = $destination;
270 $messageset['2']['message_body'] = 'Second bulk test SMS message from Drupal installation at '. $_SERVER['SERVER_NAME'] .'.';
271 $resultcode = smsgateway_sendmessageset( $messageset);
272 $connectstring = $resultcode['connectstring'];
273 $initialbalance = $resultcode['initialbalance'];
274 $response = $resultcode['1'] ." / ". $resultcode['2'];
275 }
276 else {
277 $resultcode = smsgateway_sendmessage( $destination, "Test SMS message from Drupal installation at ". $_SERVER['SERVER_NAME'] .".");
278 $connectstring = $resultcode['connectstring'];
279 $initialbalance = $resultcode['initialbalance'];
280 $response = $resultcode['response'];
281 }
282 $page_content .= '<p>Gateway Connection String was '. $connectstring .'</p>';
283 $page_content .= '<p>Initial Balance was '. $initialbalance .'</p>';
284 $page_content .= '<p>Response from gateway was '. $response .'</p>';
285 drupal_set_message(t($page_content));
286 } // function smsgateway_test_form
287
288 /**
289 * Sends the defined SMS message to the specified number
290 * @param $destination_number the number to receive the message.
291 * @param $message_body the text to send.
292 * @return an array. 'connectstring' contains the connection string used.
293 'initialbalance' optionally contains the balance at the start of the sending procedure (provider specific).
294 'response' contains the success or failure code
295 */
296 function smsgateway_sendmessage( $destination_number, $message_body) {
297 $destination_number = trim($destination_number);
298 if ($destination_number == null || drupal_strlen($destination_number) == 0) {
299 return array('connectstring' => 'Not used', 'response' => 'No telephone number specified');
300 }
301
302 $smsgateway_gateway = variable_get("smsgateway_gateway", "");
303 $smssettings = array();
304 $smssettings['account_id'] = variable_get("smsgateway_apiid", "");
305 $smssettings['accountname'] = variable_get("smsgateway_accountname", "");
306 $smssettings['password'] = variable_get("smsgateway_password", "");
307 $smssettings['from'] = variable_get("smsgateway_from", "");
308 $smssettings['usessl'] = variable_get("smsgateway_usessl", "");
309
310 if($smsgateway_gateway != '') {
311 $message = array();
312 $message['destination_number'] = $destination_number;
313 $message['message_body'] = $message_body;
314 _smsgateway_ensuregateway( $smsgateway_gateway);
315 $result = _smsgateway_invoke('sendsingle', $smsgateway_gateway, $smssettings, $message);
316 _smsgateway_smslog($destination_number, $message_body, $result['response']);
317 }
318 else {
319 $result = array();
320 $result['response'] = 'No gateway configured to send message';
321 _smsgateway_smslog( 'N/A', 'No gateway configured to send message', 'N/A');
322 }
323
324 return $result;
325 } // function smsgateway_sendmessage
326
327 /**
328 * Sends an array of defined SMS messages to the specified numbers
329 * @param an array of SMS messages keyed on 'id'.
330 'destination_number' contains the target address,
331 'message_body' contains the test text.
332 * @return a keyed array against the same 'id'. In item
333 'connectstring' contains the connection string used
334 'initialbalance' optionally contains the balance at the start of the sending procedure (provider specific).
335 Other ids contain the success or failure codes.
336 */
337 function smsgateway_sendmessageset( $sms_messages) {
338 $smsgateway_gateway = variable_get("smsgateway_gateway", "");
339 $smssettings = array();
340 $smssettings['account_id'] = variable_get("smsgateway_apiid", "");
341 $smssettings['accountname'] = variable_get("smsgateway_accountname", "");
342 $smssettings['password'] = variable_get("smsgateway_password", "");
343 $smssettings['from'] = variable_get("smsgateway_from", "");
344 $smssettings['usessl'] = variable_get("smsgateway_usessl", "");
345
346 if($smsgateway_gateway != '') {
347 _smsgateway_ensuregateway( $smsgateway_gateway);
348 $result = _smsgateway_invoke('sendbatch', $smsgateway_gateway, $smssettings, $sms_messages);
349 foreach( $sms_messages as $msgid => $message) {
350 _smsgateway_smslog($message['destination_number'], $message['message_body'], $result[ $msgid]);
351 }
352 }
353 else {
354 $result = array();
355 $result['response'] = 'No gateway configured to send message';
356 _smsgateway_smslog( 'N/A', 'No gateway configured to send message', 'N/A');
357 }
358 return $result;
359 } // function smsgateway_sendmessage
360
361
362 /**
363 * Display help and module information hook_help()
364 * @param section which section of the site we're displaying help
365 * @return help text for section
366 */
367 function smsgateway_help( $section='') {
368 $output = '';
369 switch ($section) {
370 case "admin/modules#description":
371 $output = t("Allows SMS messages to be sent by other system modules");
372 break;
373
374 case "admin/help#smsgateway":
375 $output = t("<p>The SMS gateway module allows other modules to send SMS messages to the configured gateway. System can be tested independently and logs kept and reviewed.</p>");
376 break;
377
378 case 'admin/settings/smsgateway':
379 $output = t("<p>This page controls the configuration of the smsgateway module. If allows the gateway sending implementation to be chosen and the
380 gateway settings to be configured.</p>");
381 break;
382
383 case "admin/smsgateway/test":
384 $output = t("<p>Sends a test message to the specified destination number.</p>");
385 break;
386
387 case 'admin/smsgateway/logs':
388 $output = t("<p>Views the SMS sending logs (if configured in the settings) and allows them to be cleared down.</p>");
389 break;
390 }
391 return $output;
392 } // function smsgateway_help
393
394 /**
395 * Valid permissions for this module hook_perm()
396 * @return array An array of valid permissions for the smsgateway module
397 */
398 function smsgateway_perm() {
399 return array('send sms message', 'administer smsgateway', 'receive sms message');
400 } // function smsgateway_perm
401
402
403
404 /** system_listing($mask, $directory, $key = 'name', $min_depth = 1) gives file names!!!
405 * Module configuration settings hook_admin_settings()
406 * @return settings HTML or deny access
407 */
408 function smsgateway_admin_settings() {
409 // only administrators can access this module
410 if (!user_access('admin smsgateway')) {
411 drupal_access_denied();
412 return;
413 }
414
415 drupal_set_title(t('SMS Gateway Settings'));
416
417 $gateways = _smsgateway_gateway_types();
418 $gateway_options = array();
419 foreach( $gateways as $gateway) {
420 $obj = new stdClass();
421 $obj->option = array($gateway => _smsgateway_invoke( 'name', $gateway));
422 $gateway_options[] = $obj;
423 }
424 $current_gateway = variable_get('smsgateway_gateway', '');
425
426 if ($current_gateway == '') {
427 $current_gateway_text = t('<p>No gateway has been selected</p>');
428 }
429 else {
430 $current_gateway_text = t( '<p>'. $gateway_options[ $current_gateway] .' Gateway</p>');
431 $current_gateway_text .= t( '<p>'. _smsgateway_invoke( 'description', $current_gateway) .'</p>');
432 }
433
434 $form[ 'selected_gateway_title'] = array(
435 '#value' => t('<h3>Currently Selected Gateway</h3>'),
436 );
437 $form[ 'selected_gateway_text'] = array(
438 '#value' => $current_gateway_text,
439 );
440
441 $form['smsgateway_gateway'] = array(
442 '#type' => 'select',
443 '#title' => t('Service Provider'),
444 '#default_value' => $current_gateway,
445 '#options' => $gateway_options,
446 '#description' => t('The Name of the SMS Service Provider'),
447 '#multiple' => FALSE,
448 );
449
450 $form['smsgateway_usessl'] = array(
451 '#type' => 'checkbox',
452 '#title' => t('Use SSL Encryption'),
453 '#return_value' => 1,
454 '#default_value' => variable_get('smsgateway_usessl', '0'),
455 '#description' => t('Whether the connection to the gateway should be encrypted.'),
456 );
457
458 $form["smsgateway_apiid"] = array(
459 '#type' => 'textfield',
460 '#title' => t('API Identifier'),
461 '#default_value' => variable_get('smsgateway_apiid', ''),
462 '#size' => 10,
463 '#maxlength' => 10,
464 '#description' => t('The API version identifier that may be used by the SMS gateway.'),
465 );
466
467 $form['smsgateway_accountname'] = array(
468 '#type' => 'textfield',
469 '#title' => t('Account Name'),
470 '#default_value' => variable_get('smsgateway_accountname', ''),
471 '#size' => 25,
472 '#maxlength' => 40,
473 '#description' => t('The account or user name used to log into the SMS gateway.', NULL, TRUE),
474 );
475
476 $form['smsgateway_password'] = array(
477 '#type' => 'textfield',
478 '#title' => t('Password'),
479 '#default_value' => variable_get('smsgateway_password', ''),
480 '#size' => 25,
481 '#maxlength' => 40,
482 '#description' => t('The password used to log into the SMS gateway.', NULL, TRUE),
483 );
484
485 $form['smsgateway_from'] = array(
486 '#type' => 'textfield',
487 '#title' => t('From Number'),
488 '#default_value' => variable_get('smsgateway_from', ''),
489 '#size' => 25,
490 '#maxlength' => 40,
491 '#description' => t('The telephone number that the message is sent from.'),
492 );
493
494 return system_settings_form( $form);
495 } // function smsgateway_admin_settings
496
497 ?>

  ViewVC Help
Powered by ViewVC 1.1.2