<?php
//Mysql connection made here

mysql_connect ("dbserver", "dbuser", "dbpassword") or die(mysql_error());

mysql_select_db("dbname") or die(mysql_error());

//Useful functions comes here

function filter ($text) {
	return mysql_escape_string($text);

}

function setMessageProcessed($id) {
	mysql_query ("UPDATE `inbox` SET `processed` = '1' WHERE `id` = '".filter($id)."'");  //Set processed as 1

	if (mysql_error()) return false; 
	return true;

}

function sendmessage($number, $text) {

	$footer = "\nPowered by Spider.";  //Footer text
	$text = $text.$footer; 						//Remove this line to not add footer text.

	mysql_query("INSERT INTO `outbox`  (`id` ,`number` ,`processed_date` ,`insertdate` ,`text` ,`phone` ,`processed` ,`error` ,`dreport`) VALUES (NULL , '".filter($number)."', NOW( ) , '0000-00-00 00:00:00', '".filter($text)."', NULL , '0', '-1', '0');");

}

while (1) {
	sleep(1);  //Check messages every 1 sec

	$details = mysql_fetch_array(mysql_query ("SELECT * FROM `inbox` WHERE `processed` = 0 ORDER BY `id` LIMIT 0, 1"));

	if ($details == false) continue;
	setMessageProcessed($details['id']);

	$number = $details['number'];
	$reply = "Thank you :)";

	sendmessage($number, $reply);
}
?>