Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php -q
- <?php
- include "Debug.php";
- function error_handler($num,$str,$file,$line,$context) {
- global $debug;
- $debug .= "---------------------------ERROR---------------------------\n";
- $debug .= "[$num]: $str in $file:$line\ncontext.print_r: \n".print_r($context,true)."\n";
- $debug .= "---------------------------END ERROR---------------------------\n";
- }
- set_error_handler("error_handler");
- $debug = "";
- function postDebug() {
- global $debug;
- mysql_query("INSERT INTO debug (time, info,project) VALUES (".time().",'".mysql_real_escape_string($debug)."','txt')");
- }
- function change($who,$to,$what) {
- return mysql_query("UPDATE txt_main SET $what='".mysql_real_escape_string($to)."' WHERE email='$who'")?"$what chgd successfully ":"$what couldnt be chgd.";
- }
- function getHelp($what) {
- $h = "cinfo-change your info.|cname-change name on account|cnick-change your nickname|fortune-get a fortune|info-get usrs info (use like info userid)|lusers-list usrs and id|msg-msg usr (use like msg userid text)|status-get status (use like status id)|recent-get most recent update|help-alone lists cmds, w/ cmd name gives help (i.e. help help)|menu-lists cmds|follow-follow user (use like follow id)|unfollow-unfollow user (use like unfollow id)";
- $h = explode("|",$h);
- $ret = "";
- for($i=0;$i<sizeof($h);$i++) {
- $cur = $h[$i];
- list($cmd,$help) = explode("-",$cur);
- if($cmd == $what) {
- $ret = $help;
- }
- }
- if($ret == "") {
- $ret = "The command you provided, $what, was not found.";
- }
- return $ret;
- }
- function getInfo($who) {
- $q = mysql_query("SELECT name,info FROM txt_main WHERE id=".mysql_real_escape_string($who));
- $ret = "";
- if(!$q) {
- $ret = "couldnt get info.";
- } else {
- $row = mysql_fetch_assoc($q);
- $name=getName($who);
- $ret = "$name's info: ".$row["info"];
- }
- return $ret;
- }
- function sendResponse($from,$response) {
- global $debug;
- if($response == "") { return; }
- $debug .= "sending response: $response\nto: $from";
- $response = preg_replace("/\n/"," ",$response);
- $headers = "From: txt@sirmxe.info\r\nReply-to: txt@sirmxe.info";
- if(strlen($response) > 150) {
- $responses = explode("\n",wordwrap($response,150,"\n"));
- $num = sizeof($responses);
- $i = 1;
- foreach($responses as $response) {
- $response .= " ($i/$num)";
- mail($from,"",$response,$headers);
- $i++;
- }
- } else {
- mail($from,"",$response,$headers);
- }
- }
- function lusers() {
- global $debug;
- $q = mysql_query("SELECT id,name,nick FROM txt_main");
- $ret = "Userlist\n";
- if(!$q) {
- $ret="An error occurred; sorry :(";
- } else {
- while(($row = mysql_fetch_assoc($q)) !== false) {
- $id = $row["id"];
- $debug .= "luser print_r ".print_r($row,true)."\n";
- $name = "";
- if($row["nick"] != "") {
- $name = $row["nick"];
- } else {
- $n = explode(" ",$row["name"]);
- $name = $n[0];
- }
- $ret .= "$id. $name ";
- }
- }
- return $ret;
- }
- function ago($tm,$rcs = 0) {
- $cur_tm = time(); $dif = $cur_tm-$tm;
- $pds = array('second','minute','hour','day','week','month','year','decade');
- $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
- for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
- $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
- if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
- return $x;
- }
- function getStatus ($usr) {
- global $debug;
- $q = mysql_query("SELECT `status`,`when` FROM txt_main_status WHERE userid=$usr ORDER BY `status` DESC LIMIT 1");
- $ret = "";
- if(!$q) {
- $ret = "Could not get status for userid $usr";
- $debug .= "Couldn't get status $usr: " .mysql_error() . " (".mysql_errno().")\n";
- } else {
- $row = mysql_fetch_assoc($q);
- $who = getName($usr);
- $when = ago($row['when']) . "ago";
- $status = $row["status"];
- $ret = "$who's status: $status (posted $when)";
- }
- return $ret;
- }
- function getMostRecent() {
- global $debug;
- $q = mysql_query("SELECT `status`,`when`,`userid` FROM txt_main_status ORDER BY `when` DESC LIMIT 1");
- $ret = "";
- if(!$q) {
- $ret = "Could not fetch most recent update.";
- $debug .= "Couldn't fetch recent 1: " .mysql_error() . " (".mysql_errno().")\n";
- } else {
- $row = mysql_fetch_assoc($q);
- $status = $row["status"];
- $when = ago($row["when"]) . "ago";
- $q2 = mysql_query("SELECT name,nick FROM txt_main WHERE id=".$row['userid']);
- if(!$q2) {
- $ret = "Could not fetch most recent update.";
- $debug .= "Couldn't fetch recent 2: " .mysql_error() . " (".mysql_errno().")\n";
- } else {
- $name = getName($row['userid']);
- $ret = "Most recent update: $status (from $name $when)";
- }
- }
- return $ret;
- }
- function updateStatus ($from,$stat) {
- global $debug;
- $stat = preg_replace("/\n/","",$stat);
- $q = mysql_query("SELECT id FROM txt_main WHERE email='$from'");
- $debug .= "from = $from\n";
- $ret = "";
- if(!$q) {
- $ret = "Could not update status.";
- $debug .= "Couldn't update status 1: " .mysql_error() . " (".mysql_errno().")\n";
- } else {
- $r = mysql_fetch_assoc($q);
- $id = $r['id'];
- $q = mysql_query("INSERT INTO txt_status (`status`,`when`,`userid`) VALUES ('".mysql_real_escape_string($stat)."',".time().",$id)");
- if(!$q) {
- $ret = "Could not update status.";
- $debug .= "Couldn't update status 2: " .mysql_error() . " (".mysql_errno().")\n";
- } else {
- $ret = "Status updated.";
- $q = mysql_query("UPDATE txt_main SET lastupdate=".mysql_insert_id()." WHERE id=$id");
- if(!$q) { $debug .= "Could not update last update for $id: " .mysql_error()." (".mysql_errno().")\n"; }
- $q = mysql_query("UPDATE txt_main SET lastupdatetxt='".mysql_real_escape_string($stat)."' WHERE id=$id");
- if(!$q) { $debug .= "Could not update last update txt for $id: " .mysql_error()." (".mysql_errno().")\n"; }
- $q = mysql_query("SELECT followerid FROM txt_main_followers WHERE userid=$id");
- while(($row = mysql_fetch_assoc($q)) !== false) {
- $f = $row['followerid'];
- $r = mysql_fetch_assoc(mysql_query("SELECT email FROM txt_main WHERE id=$f"));
- $n = getName($id);
- $msg = "New update from $n: $stat";
- sendResponse($r["email"],$msg);
- }
- }
- }
- }
- function getName($id,$field="id") {
- $q = mysql_query("SELECT username,name,nick FROM txt_main WHERE $field=".($field!="id"?"'$id'":"$id"));
- $name = "";
- if(!$q) { } else {
- $row = mysql_fetch_assoc($q);
- $name = "";
- if($row["nick"] != "") {
- $name = $row["nick"];
- } else if($row["name"] != "") {
- $n = explode(" ",$row["name"]);
- $name = $n[0];
- } else {
- $name = $row['username'];
- }
- }
- return $name;
- }
- function follow($from,$who) {
- global $debug;
- $n = getName($who);
- $q = mysql_query("SELECT id FROM txt_main WHERE email='$from'");
- if(!$q) {
- $ret = "could not follow $n";
- $debug .= "follow err: ".mysql_error()."\n";
- } else {
- $row = mysql_fetch_assoc($q);
- $follower = $row["id"];
- if(mysql_num_rows(mysql_query("SELECT * FROM txt_main_followers WHERE userid=$who AND followerid=$follower")) > 0) {
- $ret = "You are already following $n.";
- } else if($who == $follower) {
- $ret = "You can't follow yourself.";
- } else {
- $time = time();
- $follow = mysql_query("INSERT INTO txt_followers (userid,followerid,time) VALUES ($who,$follower,$time)");
- if(!$follow) {
- $ret = "Could not follow $n";
- $debug .= "follow err2: ".mysql_error()."\n";
- } else {
- $ret = "You are now following $n";
- }
- }
- }
- return $ret;
- }
- function unfollow($from,$who) {
- global $debug;
- $n = getName($who);
- $q = mysql_query("SELECT id FROM txt_main WHERE email='$from'");
- if(!$q) {
- $ret = "could not unfollow $n";
- $debug .= "unfollow err: ".mysql_error()."\n";
- } else {
- $row = mysql_fetch_assoc($q);
- $follower = $row["id"];
- if(mysql_num_rows(mysql_query("SELECT * FROM txt_main_followers WHERE userid=$who AND followerid=$follower")) == 0) {
- $ret = "You are not following $n.";
- } else if($who == $follower) {
- $ret = "You cannot unfollow yourself.";
- } else {
- $time = time();
- $follow = mysql_query("DELETE FROM txt_main_followers WHERE followerid=$follower AND userid=$who");
- if(!$follow) {
- $ret = "Could not unfollow $n";
- $debug .= "unfollow err2: ".mysql_error()."\n";
- } else {
- $ret = "You are no longer following $n";
- }
- }
- }
- return $ret;
- }
- function msg($from,$usr,$txt) {
- $q = mysql_query("SELECT * from txt WHERE id=$usr");
- $ret = "";
- if(!$q) {
- $ret = "Sorry, there was an error. :(";
- } else if (mysql_num_rows($q) == 0) {
- $ret = "That usr doesnt exist.";
- } else {
- $row = mysql_fetch_assoc($q);
- $name = getName($usr);
- $nm = getName($from,"email");
- $msg = "Hi $name; U have rcvd a msg from $nm: $txt 2 reply, pls msg back w/ msg $id: ur txt here";
- sendResponse($row["email"],$msg);
- $ret = "msg sent 2 $name :)";
- }
- return $ret;
- }
- mysql_connect('localhost','','');
- mysql_select_db('enigma_toolbus');
- $contents = "";
- $time = time();
- $stdin = fopen("php://stdin","r");
- while(!feof($stdin)) {
- $contents .= fgets($stdin);
- }
- fclose($stdin);
- $db2 = mysql_connect('localhost','','');
- mysql_select_db('enigma_toolbus',$db2);
- $b = mysql_real_escape_string($contents);
- $time = time();
- $debug .= "contents\n-------------------------------------\n$contents\n-----------------------------\n";
- $d = explode("\n\n",$contents);
- $headers = explode("\n",$d[0]);
- $body = array_slice($d,1);
- $phone = "";
- $from ="";
- $subject = "";
- foreach($headers as $cur) {
- if((preg_match("/From: .+ <(.+)>/i",$cur,$match) != 0) || (preg_match("/From: <(.+)>/i",$cur,$match) != 0) || (preg_match("/From: (.+)/i",$cur,$match) != 0)){
- $phone = $from = $match[1];
- }
- else if(preg_match("/Subject: (.+)/",$cur,$match) != 0) {
- $subject = $match[1];
- }
- }
- $img = "";
- $noText = !preg_match("/Content-Type: text\/plain/i",implode("\n",$body));
- $noImg = !preg_match("/img src/i",implode("\n",$body));
- $debug .= "noText: $noText; noImg: $noImg\n";
- if(is_array($body)) {
- for($i=0;$i<sizeof($body);$i++) {
- if(!$noText && $noImg) {
- if(preg_match("/Content-Type: text\/plain/i",$body[$i])) {
- $body = implode("\n",array_splice($body,$i+1));
- $body = preg_replace("/--[\w\d\-\s\!\@\#\$\%\^\&\*\(\)\_\+\=\[\]\{\}\;\:\'\"\,\.\/\<\>\?]+/","",$body);
- break;
- }
- } else {
- if(preg_match("/Content-Type: text\/html/i",$body[$i])) {
- $body = $body[$i+1];
- $body = preg_replace("/<title>.+<\/title>/","",$body);
- $body = preg_replace("/<\!\-\-.+?\-\->/is","",$body);
- if($noImg) {
- $body = preg_replace("/\n\n/","",$body);
- $body = preg_replace("/<.*?>/","$1",$body);
- $body = preg_replace("/[\r\t\n ]{2,}/"," ",$body);
- } else {
- if(preg_match("/sprint\.com",implode("\n",$headers))) {
- if(preg_match("/Message:.+?<pre.+?>(.+?)<\/pre>/i",$body,$match)) {
- $msg = $match[1];
- $debug .= "got msg(sprint): $msg\n";
- }
- if(preg_match("/<img src=\"(http:\/\/pictures\.sprintpcs\.com\/{1,2}mmps.+?)\"\/>\n/",$body,$match)) {
- $img = $match[1];
- $debug .= "got img(sprint): $img\n";
- }
- }
- }
- break;
- }
- }
- }
- }
- if(is_array($body)) {
- $body = implode("\n",$body);
- } else {
- $body = $body;
- }
- $body = preg_replace("/On .+,.+ (wrote|said):/i","",$body);
- $body = preg_replace("/\n{2,}/"," ",$body);
- $body = preg_replace("/^\s+/","",$body);
- mysql_query("INSERT INTO incoming (body, time) VALUES ('".mysql_real_escape_string("TXT_Debug\n$b\nparsed body content: $body\n\ndebug\n$debug")."','$time')",$db2) or die("Err".mysql_errno().": ".mysql_error());
- $body = preg_replace("/>\s?.+\n/","",$body);
- if(preg_match("/(.+)@.+/",$phone,$match)) {
- $phone = $match[1];
- }
- $email = $from;
- $from = mysql_real_escape_string($from);
- $response = "";
- $time=time();
- $debug .= "from: $from\n";
- if(mysql_num_rows(mysql_query("SELECT * FROM txt_main WHERE email='$from'")) == 0) {
- if(preg_match("/^reg ([\w\d\.]+) (.+)/i",$body,$match) === 0) {
- $response = "Hello $phone. You are not currently registered. To register, please reply back: 'reg username password'";
- } else {
- $username = $match[1];
- $password = md5($match[2]);
- $q = mysql_query("INSERT INTO txt (email,username,password,created) VALUES ('$from','".mysql_real_escape_string($username)."','$password',$time)");
- if(!$q) {
- $response = "I'm sorry, $username, but we couldn't register you. Please try again!";
- } else {
- $response = "Welcome to txtbus, $username! Use cname 2 chg ur name, cnick 2 chg ur nick, cinfo to chg ur info, help for commands";
- }
- }
- } else {
- $body = htmlentities($body);
- $debug .= "body: $body\n";
- $row = mysql_fetch_assoc(mysql_query("SELECT name FROM txt_main WHERE email='$from'"));
- $name = $row['name'];
- $d = explode(" ",$name);
- $body = preg_replace("/^\s*/","",$body);
- if(sizeof($d) == 2) { $name = $d[0]; }
- if(preg_match("/^cinfo (.+)/i",$body,$match) !== 0) {
- $response = $name.": ".change($from,$match[1],"info");
- }
- else if(preg_match("/^cname (.+)/i",$body,$match) !== 0) {
- $response = change($from,$match[1],"name");
- } else if(preg_match("/^cnick (.+)/i",$body,$match) !== 0) {
- $response = change($from,$match[1],"nick");
- }
- else if(preg_match("/^fortune/i",$body) !== 0) {
- $db2 = mysql_connect("localhost","","");
- mysql_select_db("enigma_malformedxmlelement",$db2);
- $query = mysql_query("SELECT * FROM fortunes",$db2);
- if(!$query) { $response = "Could not get a fortune."; }
- $id = mt_rand(0,mysql_num_rows($query));
- $query = mysql_query("SELECT * FROM fortunes WHERE id=$id",$db2);
- if(!$query) { $response = "Could not get a fortune."; }
- $row = mysql_fetch_assoc($query);
- $response = $row["fortune"];
- } else if(preg_match("/^info (\d+)/i",$body,$match) !== 0) {
- $response = getInfo($match[1]);
- }
- else if(preg_match("/^lusers/i",$body,$match) !== 0) {
- $response = lusers();
- } else if(preg_match("/^follow (\d+)/i",$body,$match) !== 0) {
- $response = follow($from,$match[1]);
- } else if(preg_match("/^unfollow (\d+)/i",$body,$match) !== 0) {
- $response = unfollow($from,$match[1]);
- } else if(preg_match("/^help ([a-z]+)/i",$body,$match) !== 0) {
- $response = getHelp($match[1]);
- } else if(preg_match("/^(help|menu)/i",$body,$match) !== 0) {
- $response = "cinfo|cname|cnick|fortune|info|lusers|msg|status|recent|menu|no cmd 2 update status|help cmd|follow|unfollow";
- } else if(preg_match("/^msg (\d+) (.+)/i",$body,$match) !== 0) {
- $response = msg($from,$match[1],$match[2]);
- } else if(preg_match("/^recent/",$body,$match) !== 0) {
- $response = getMostRecent();
- } else if(preg_match("/^status (\d+)/i",$body,$match) !== 0) {
- $response = getStatus($match[1]);
- } else {
- $response = updateStatus($from,$body);
- }
- }
- sendResponse($email,$response);
- postDebug();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement