(Posting from a Starbucks in Seattle, because it seemed like the right thing to do)

I made a SMS service for people in my local church group to send each other anonymous compliments (nice notes) and receive responses. The process is simple:

Send the following to the Twilio number: <First name> <Last name> <message>

This has a second feature: If there are less than three words in the text message, it checks the directory of people for names that are similar, and returns the name and addresses of those.

[php title="sms.php"]
<?php
$AccountSid = "";
$AuthToken = "";

// Connect to the database
$con = mysql_connect("localhost","root","");
mysql_select_db("nicenotes", $con);

$message = parseBody($_GET['Body']);
$From = phoneUS($_GET['From']);

if($message['to'] && $message['text']){
// Log the message
if($message['to'] != $From){
$sql = "INSERT INTO log SET log.`To` = '$message[to]', log.`Re` = '$From', log.`Body` = '$message[text]'";
mysql_query($sql);
}
echo "<Response>";
echo "<Sms to="$message[to]">$message[text]</Sms>";
echo "</Response>";
}
else{
echo "<Response>";
echo "<Sms>Could not figure out who you were trying to message. Try again, or tell Alex Swan.</Sms>";
echo "</Response>";
}

function parseBody($Body){
// Load the csv
$names = array();
$fp = fopen('menu.csv', 'r');
while($line = fgets($fp)){
$parts = explode(",", $line);
$names[strtolower($parts[0])] = $parts[1];
}
fclose($fp);
// Get the first two words of the Body

$arrayMatches = explode(" ", $Body);
if($arrayMatches && count($arrayMatches) >= 3){ // It's a nice note
$name = strtolower($arrayMatches[0] . " " . $arrayMatches[1]);
if(isset($names["$name"])){ // It's a new one
$to = $names["$name"];
$message = substr($Body, strlen($name)+1);
return array("to"=>phoneUS($to), "text"=>"Provo 174th e-note: " . htmlspecialchars($message));
}
else{ // It's possibly a reply
// Check old messages to respond
// Find the name based on the sender
$From = phoneUS($_GET['From']);
$sql = "SELECT * FROM log WHERE log.`To` = '$From' ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql);
if($log = mysql_fetch_array($result)){
return array("to"=>phoneUS($log['Re']), "text"=>"e-note reply: " . htmlspecialchars($Body));
}
}
}
else{ // It's guessing the name
$message = array();
$Body = strtolower($Body);
foreach($names as $key=>$value){
$nameArray = explode(" ", $key);
if(levenshtein($nameArray[0], $Body) <= 2 || levenshtein($nameArray[1], $Body) <= 2 || levenshtein($key, $Body) <= 4){
array_push($message, ucwords($key) . ": $value");
}
}
return array("to"=>phoneUS($_GET['From']), "text"=>htmlspecialchars(implode($message)));
}
}

function phoneUS($number){
$pattern = "/^D*[1]?D*(d{3})D*(d{3})D*(d{4})(?:D*)$/";
preg_match($pattern, $number, $numberArray);

if(!$numberArray){
return 0;
}
return "+1".$numberArray[1].$numberArray[2].$numberArray[3];
}

?>
[/php]

The directory of members is a csv file in this fashion:
[text]
Al Deans,8884446633
Alex Foy,5557770000
[/text]

And the mysql database it checks looks like this:
[sql]
CREATE TABLE `log` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`To` varchar(14) DEFAULT NULL,
`Re` varchar(14) DEFAULT NULL,
`Body` varchar(320) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=84 DEFAULT CHARSET=latin1;
[/sql]