Archive

Archive for the ‘Drupal’ Category

SMS to Drupal

August 8th, 2009 admin No comments

We required a method of using sms to activate users on our new site so that we can be sure that people aren’t creating duplicate accounts and are from the UK. We have now come up with two methods for achieving this, both of which are documented below. It took a long time to get this working, not because it is difficult, but because like anything in Drupal there are so many different ways of doing things. Finding methods which work the best can prove difficult.

Our first implementation was to send sms messages to users with a code to enter. This was easy to setup using our favourite sms gateway redsms. We used the smsframework module and sms_user, forcing the user to enter a mobile phone number on registration and adding some code in the code confirmation function to add the user to a new role. We never got as far as adding in a form to set which role the user gets promoted to because we spotted a problem, it was going to cost us up to 7.5 pence per new user and we’d have to keep an eye on our balance to keep it in credit. This was clearly not the best way of doing things.

We then spent quite a long time, many days, coming up with ideas on how to do it the other way round. We had code which would receive sms messages from redsms but wouldn’t integrate nicely with Drupal. We came up with all sorts of weird and wonderful ways of interacting with Drupal’s user apis, trauled through existing modules on drupal.org to come up with ideas, and still didn’t get anywhere. Then one day it hit me, switch to a gateway which can send us sms messages by HTTP POST (so we don’t have to poll their api or parse emails) and use the user_import module to automatically import users from a csv file.

The following code (which is protected through .htaccess/.htpasswd) will receive messages from txtlocal.co.uk, remove our keyword (which is 8 characters long), check that the sender has a uk mobile number, check that the number is unique, and finally append to a csv file.


<?php
$sender = $_REQUEST['sender'];
$content = $_REQUEST['content'];
$content = substr($content, 8);
if(substr_compare($sender, '447', 0, 3)!=0) {
die('non-uk number');
}
$existing = system("grep $sender sites/votecast.co.uk/modules/user_import/data.txt | wc -l");
if ($existing!='0') {
die('duplicate number');
}
$fp = fopen('sites/votecast.co.uk/modules/user_import/data.txt', 'a');
fwrite($fp, $sender);
fwrite($fp, ', ');
fwrite($fp, $content);
fwrite($fp, "\n");
fclose($fp);
?>

user_import can then use this csv file, and on every cron job will add the new users and email them a random username and password. Only it won’t. user_import isn’t designed to handle a csv file which is constantly being appended to. Luckily it’s a quick fix for now, which we can improve later on.

Categories: Drupal Tags: , ,