Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.96 KB | None | 0 0
  1. #!/usr/bin/php -q
  2. <?php
  3. include "Debug.php";
  4. function error_handler($num,$str,$file,$line,$context) {
  5.     global $debug;
  6.     $debug .= "---------------------------ERROR---------------------------\n";
  7.     $debug .= "[$num]: $str in $file:$line\ncontext.print_r: \n".print_r($context,true)."\n";
  8.     $debug .= "---------------------------END ERROR---------------------------\n";
  9. }
  10. set_error_handler("error_handler");
  11.  
  12. $debug = "";
  13. function postDebug() {
  14.     global $debug;
  15.     mysql_query("INSERT INTO debug (time, info,project) VALUES (".time().",'".mysql_real_escape_string($debug)."','txt')");
  16. }
  17. function change($who,$to,$what) {
  18.     return mysql_query("UPDATE txt_main SET $what='".mysql_real_escape_string($to)."' WHERE email='$who'")?"$what chgd successfully ":"$what couldnt be chgd.";
  19. }
  20. function getHelp($what) {
  21.     $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)";
  22.     $h = explode("|",$h);
  23.     $ret = "";
  24.     for($i=0;$i<sizeof($h);$i++) {
  25.         $cur = $h[$i];
  26.         list($cmd,$help) = explode("-",$cur);
  27.         if($cmd == $what) {
  28.             $ret = $help;
  29.         }
  30.     }
  31.     if($ret == "") {
  32.         $ret = "The command you provided, $what, was not found.";
  33.     }
  34.     return $ret;
  35. }
  36. function getInfo($who) {
  37.     $q = mysql_query("SELECT name,info FROM txt_main WHERE id=".mysql_real_escape_string($who));
  38.     $ret = "";
  39.     if(!$q) {
  40.         $ret = "couldnt get info.";
  41.     } else {
  42.         $row = mysql_fetch_assoc($q);
  43.         $name=getName($who);
  44.         $ret = "$name's info: ".$row["info"];
  45.     }
  46.     return $ret;
  47. }
  48. function sendResponse($from,$response) {
  49.     global $debug;
  50.     if($response == "") { return; }
  51.     $debug .= "sending response: $response\nto: $from";
  52.     $response = preg_replace("/\n/"," ",$response);
  53.     $headers = "From: txt@sirmxe.info\r\nReply-to: txt@sirmxe.info";
  54.     if(strlen($response) > 150) {
  55.         $responses = explode("\n",wordwrap($response,150,"\n"));
  56.                 $num = sizeof($responses);
  57.                 $i = 1;
  58.         foreach($responses as $response) {
  59.                         $response .= " ($i/$num)";
  60.             mail($from,"",$response,$headers);
  61.                         $i++;
  62.         }
  63.     } else {
  64.         mail($from,"",$response,$headers);
  65.     }
  66. }
  67.  
  68. function lusers() {
  69.     global $debug;
  70.     $q = mysql_query("SELECT id,name,nick FROM txt_main");
  71.     $ret = "Userlist\n";
  72.     if(!$q) {
  73.         $ret="An error occurred; sorry :(";
  74.     } else {
  75.         while(($row = mysql_fetch_assoc($q)) !== false) {
  76.             $id = $row["id"];
  77.             $debug .= "luser print_r ".print_r($row,true)."\n";
  78.             $name = "";
  79.             if($row["nick"] != "") {
  80.                 $name = $row["nick"];
  81.             } else {
  82.                 $n = explode(" ",$row["name"]);
  83.                 $name = $n[0];
  84.             }
  85.             $ret .= "$id. $name ";
  86.         }
  87.     }
  88.     return $ret;
  89. }
  90. function ago($tm,$rcs = 0) {
  91.     $cur_tm = time(); $dif = $cur_tm-$tm;
  92.     $pds = array('second','minute','hour','day','week','month','year','decade');
  93.     $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
  94.     for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
  95.    
  96.     $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
  97.     if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
  98.     return $x;
  99. }
  100. function getStatus ($usr) {
  101.     global $debug;
  102.     $q = mysql_query("SELECT `status`,`when` FROM txt_main_status WHERE userid=$usr ORDER BY `status` DESC LIMIT 1");
  103.     $ret = "";
  104.     if(!$q) {
  105.         $ret = "Could not get status for userid $usr";
  106.         $debug .= "Couldn't get status $usr: " .mysql_error() . " (".mysql_errno().")\n";
  107.     } else {
  108.         $row = mysql_fetch_assoc($q);
  109.         $who = getName($usr);
  110.         $when = ago($row['when']) . "ago";
  111.         $status = $row["status"];
  112.         $ret = "$who's status: $status (posted $when)";
  113.     }
  114.     return $ret;
  115. }
  116. function getMostRecent() {
  117.     global $debug;
  118.     $q = mysql_query("SELECT `status`,`when`,`userid` FROM txt_main_status ORDER BY `when` DESC LIMIT 1");
  119.     $ret = "";
  120.     if(!$q) {
  121.         $ret = "Could not fetch most recent update.";
  122.         $debug .= "Couldn't fetch recent 1: " .mysql_error() . " (".mysql_errno().")\n";
  123.     } else {
  124.         $row = mysql_fetch_assoc($q);
  125.         $status = $row["status"];
  126.         $when = ago($row["when"]) . "ago";
  127.         $q2 = mysql_query("SELECT name,nick FROM txt_main WHERE id=".$row['userid']);
  128.         if(!$q2) {
  129.             $ret = "Could not fetch most recent update.";
  130.             $debug .= "Couldn't fetch recent 2: " .mysql_error() . " (".mysql_errno().")\n";
  131.         } else {
  132.             $name = getName($row['userid']);
  133.             $ret = "Most recent update: $status (from $name $when)";
  134.         }
  135.     }
  136.     return $ret;
  137. }
  138.        
  139.        
  140. function updateStatus ($from,$stat) {
  141.     global $debug;
  142.     $stat = preg_replace("/\n/","",$stat);
  143.     $q = mysql_query("SELECT id FROM txt_main WHERE email='$from'");
  144.     $debug .= "from = $from\n";
  145.     $ret = "";
  146.     if(!$q) {
  147.         $ret = "Could not update status.";
  148.         $debug .= "Couldn't update status 1: " .mysql_error() . " (".mysql_errno().")\n";
  149.     } else {
  150.         $r = mysql_fetch_assoc($q);
  151.         $id = $r['id'];
  152.         $q = mysql_query("INSERT INTO txt_status (`status`,`when`,`userid`) VALUES ('".mysql_real_escape_string($stat)."',".time().",$id)");
  153.         if(!$q) {
  154.             $ret = "Could not update status.";
  155.             $debug .= "Couldn't update status 2: " .mysql_error() . " (".mysql_errno().")\n";
  156.         } else {
  157.             $ret = "Status updated.";
  158.             $q = mysql_query("UPDATE txt_main SET lastupdate=".mysql_insert_id()." WHERE id=$id");
  159.             if(!$q) { $debug .= "Could not update last update for $id: " .mysql_error()." (".mysql_errno().")\n"; }
  160.             $q = mysql_query("UPDATE txt_main SET lastupdatetxt='".mysql_real_escape_string($stat)."' WHERE id=$id");
  161.             if(!$q) { $debug .= "Could not update last update txt for $id: " .mysql_error()." (".mysql_errno().")\n"; }
  162.             $q = mysql_query("SELECT followerid FROM txt_main_followers WHERE userid=$id");
  163.             while(($row = mysql_fetch_assoc($q)) !== false) {
  164.                 $f = $row['followerid'];
  165.                 $r = mysql_fetch_assoc(mysql_query("SELECT email FROM txt_main WHERE id=$f"));
  166.                 $n = getName($id);
  167.                 $msg = "New update from $n: $stat";
  168.                 sendResponse($r["email"],$msg);
  169.             }
  170.         }
  171.     }
  172. }
  173. function getName($id,$field="id") {
  174.     $q = mysql_query("SELECT username,name,nick FROM txt_main WHERE $field=".($field!="id"?"'$id'":"$id"));
  175.     $name = "";
  176.     if(!$q) { } else {
  177.         $row = mysql_fetch_assoc($q);
  178.         $name = "";
  179.         if($row["nick"] != "") {
  180.             $name = $row["nick"];
  181.         } else if($row["name"] != "") {
  182.             $n = explode(" ",$row["name"]);
  183.             $name = $n[0];
  184.         } else {
  185.             $name = $row['username'];
  186.         }
  187.     }
  188.     return $name;
  189. }
  190.        
  191. function follow($from,$who) {
  192.     global $debug;
  193.     $n = getName($who);
  194.     $q = mysql_query("SELECT id FROM txt_main WHERE email='$from'");
  195.     if(!$q) {
  196.         $ret = "could not follow $n";
  197.         $debug .= "follow err: ".mysql_error()."\n";
  198.     } else {
  199.         $row = mysql_fetch_assoc($q);
  200.         $follower = $row["id"];
  201.         if(mysql_num_rows(mysql_query("SELECT * FROM txt_main_followers WHERE userid=$who AND followerid=$follower")) > 0) {
  202.             $ret = "You are already following $n.";
  203.         } else if($who == $follower) {
  204.             $ret = "You can't follow yourself.";
  205.         } else  {
  206.             $time = time();
  207.             $follow = mysql_query("INSERT INTO txt_followers (userid,followerid,time) VALUES ($who,$follower,$time)");
  208.             if(!$follow) {
  209.                 $ret = "Could not follow $n";
  210.                 $debug .= "follow err2: ".mysql_error()."\n";
  211.             } else {
  212.                 $ret = "You are now following $n";
  213.             }
  214.         }
  215.     }
  216.     return $ret;
  217. }
  218.  
  219. function unfollow($from,$who) {
  220.     global $debug;
  221.     $n = getName($who);
  222.     $q = mysql_query("SELECT id FROM txt_main WHERE email='$from'");
  223.     if(!$q) {
  224.         $ret = "could not unfollow $n";
  225.         $debug .= "unfollow err: ".mysql_error()."\n";
  226.     } else {
  227.         $row = mysql_fetch_assoc($q);
  228.         $follower = $row["id"];
  229.         if(mysql_num_rows(mysql_query("SELECT * FROM txt_main_followers WHERE userid=$who AND followerid=$follower")) == 0) {
  230.             $ret = "You are not following $n.";
  231.         } else if($who == $follower) {
  232.             $ret = "You cannot unfollow yourself.";
  233.         } else {
  234.             $time = time();
  235.             $follow = mysql_query("DELETE FROM txt_main_followers WHERE followerid=$follower AND userid=$who");
  236.             if(!$follow) {
  237.                 $ret = "Could not unfollow $n";
  238.                 $debug .= "unfollow err2: ".mysql_error()."\n";
  239.             } else {
  240.                 $ret = "You are no longer following $n";
  241.             }
  242.         }
  243.     }
  244.     return $ret;
  245. }
  246.  
  247. function msg($from,$usr,$txt) {
  248.     $q = mysql_query("SELECT * from txt WHERE id=$usr");
  249.     $ret = "";
  250.     if(!$q) {
  251.         $ret = "Sorry, there was an error. :(";
  252.     } else if (mysql_num_rows($q) == 0) {
  253.         $ret = "That usr doesnt exist.";
  254.     } else {
  255.         $row = mysql_fetch_assoc($q);
  256.         $name = getName($usr);
  257.         $nm = getName($from,"email");  
  258.         $msg = "Hi $name; U have rcvd a msg from $nm: $txt 2 reply, pls msg back w/ msg $id: ur txt here";
  259.         sendResponse($row["email"],$msg);
  260.         $ret = "msg sent 2 $name :)";
  261.     }
  262.     return $ret;
  263. }
  264.    
  265.  
  266. mysql_connect('localhost','','');
  267. mysql_select_db('enigma_toolbus');
  268. $contents = "";
  269. $time = time();
  270. $stdin = fopen("php://stdin","r");
  271. while(!feof($stdin)) {
  272.     $contents .= fgets($stdin);
  273. }
  274. fclose($stdin);
  275. $db2 = mysql_connect('localhost','','');
  276. mysql_select_db('enigma_toolbus',$db2);
  277. $b = mysql_real_escape_string($contents);
  278. $time = time();
  279. $debug .= "contents\n-------------------------------------\n$contents\n-----------------------------\n";
  280. $d = explode("\n\n",$contents);
  281. $headers = explode("\n",$d[0]);
  282. $body = array_slice($d,1);
  283. $phone = "";
  284. $from ="";
  285. $subject = "";
  286. foreach($headers as $cur) {
  287.       if((preg_match("/From: .+ <(.+)>/i",$cur,$match) != 0) || (preg_match("/From: <(.+)>/i",$cur,$match) != 0) || (preg_match("/From: (.+)/i",$cur,$match) != 0)){
  288.          $phone = $from = $match[1];
  289.       }
  290.       else if(preg_match("/Subject: (.+)/",$cur,$match) != 0) {
  291.         $subject = $match[1];
  292.       }
  293. }
  294. $img = "";
  295. $noText = !preg_match("/Content-Type: text\/plain/i",implode("\n",$body));
  296. $noImg = !preg_match("/img src/i",implode("\n",$body));
  297. $debug .= "noText: $noText; noImg: $noImg\n";
  298. if(is_array($body)) {        
  299.     for($i=0;$i<sizeof($body);$i++) {                    
  300.         if(!$noText && $noImg) {                        
  301.             if(preg_match("/Content-Type: text\/plain/i",$body[$i])) {                         
  302.                 $body = implode("\n",array_splice($body,$i+1));
  303.                 $body = preg_replace("/--[\w\d\-\s\!\@\#\$\%\^\&\*\(\)\_\+\=\[\]\{\}\;\:\'\"\,\.\/\<\>\?]+/","",$body);
  304.                 break;
  305.             }
  306.         } else {              
  307.             if(preg_match("/Content-Type: text\/html/i",$body[$i])) {                          
  308.                 $body = $body[$i+1];
  309.                 $body = preg_replace("/<title>.+<\/title>/","",$body); 
  310.                 $body = preg_replace("/<\!\-\-.+?\-\->/is","",$body);  
  311.                 if($noImg) {
  312.                     $body = preg_replace("/\n\n/","",$body);                                           
  313.                     $body = preg_replace("/<.*?>/","$1",$body);            
  314.                     $body = preg_replace("/[\r\t\n ]{2,}/"," ",$body);             
  315.                 } else {
  316.                   if(preg_match("/sprint\.com",implode("\n",$headers))) {
  317.                         if(preg_match("/Message:.+?<pre.+?>(.+?)<\/pre>/i",$body,$match)) {
  318.                             $msg = $match[1];                          
  319.                             $debug .= "got msg(sprint): $msg\n";
  320.                         }
  321.                         if(preg_match("/<img src=\"(http:\/\/pictures\.sprintpcs\.com\/{1,2}mmps.+?)\"\/>\n/",$body,$match)) {
  322.                             $img = $match[1];
  323.                             $debug .= "got img(sprint): $img\n";
  324.                         }
  325.                     }
  326.                 }
  327.                 break;
  328.             }
  329.         }
  330.     }
  331. }
  332. if(is_array($body)) {
  333.     $body = implode("\n",$body);
  334. } else {
  335.     $body = $body;
  336. }
  337. $body = preg_replace("/On .+,.+ (wrote|said):/i","",$body);
  338. $body = preg_replace("/\n{2,}/"," ",$body);
  339. $body = preg_replace("/^\s+/","",$body);
  340. 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());
  341. $body = preg_replace("/>\s?.+\n/","",$body);
  342. if(preg_match("/(.+)@.+/",$phone,$match)) {
  343.  $phone = $match[1];
  344. }
  345. $email = $from;
  346. $from = mysql_real_escape_string($from);
  347. $response = "";
  348. $time=time();
  349. $debug .= "from: $from\n";
  350. if(mysql_num_rows(mysql_query("SELECT * FROM txt_main WHERE email='$from'")) == 0) {
  351.     if(preg_match("/^reg ([\w\d\.]+) (.+)/i",$body,$match) === 0) {
  352.         $response = "Hello $phone. You are not currently registered. To register, please reply back: 'reg username password'";
  353.     } else {
  354.         $username = $match[1];
  355.         $password = md5($match[2]);
  356.         $q = mysql_query("INSERT INTO txt (email,username,password,created) VALUES ('$from','".mysql_real_escape_string($username)."','$password',$time)");
  357.         if(!$q) {
  358.             $response = "I'm sorry, $username, but we couldn't register you. Please try again!";
  359.         } else {
  360.             $response = "Welcome to txtbus, $username! Use cname 2 chg ur name, cnick 2 chg ur nick, cinfo to chg ur info, help for commands";
  361.         }
  362.     }
  363. } else {
  364.     $body = htmlentities($body);
  365.     $debug .= "body: $body\n";
  366.     $row = mysql_fetch_assoc(mysql_query("SELECT name FROM txt_main WHERE email='$from'"));
  367.     $name = $row['name'];
  368.     $d = explode(" ",$name);
  369.     $body = preg_replace("/^\s*/","",$body);
  370.     if(sizeof($d) == 2) { $name = $d[0]; }
  371.     if(preg_match("/^cinfo (.+)/i",$body,$match) !== 0) {
  372.         $response = $name.": ".change($from,$match[1],"info");
  373.     }
  374.     else if(preg_match("/^cname (.+)/i",$body,$match) !== 0) {
  375.         $response = change($from,$match[1],"name");
  376.     } else if(preg_match("/^cnick (.+)/i",$body,$match) !== 0) {
  377.         $response = change($from,$match[1],"nick");
  378.     }
  379.     else if(preg_match("/^fortune/i",$body) !== 0) {
  380.         $db2 = mysql_connect("localhost","","");
  381.         mysql_select_db("enigma_malformedxmlelement",$db2);
  382.         $query = mysql_query("SELECT * FROM fortunes",$db2);
  383.         if(!$query) { $response = "Could not get a fortune."; }
  384.         $id = mt_rand(0,mysql_num_rows($query));       
  385.         $query = mysql_query("SELECT * FROM fortunes WHERE id=$id",$db2);
  386.         if(!$query) { $response = "Could not get a fortune."; }
  387.         $row = mysql_fetch_assoc($query);
  388.         $response = $row["fortune"];               
  389.     } else if(preg_match("/^info (\d+)/i",$body,$match) !== 0) {
  390.         $response = getInfo($match[1]);
  391.     }
  392.     else if(preg_match("/^lusers/i",$body,$match) !== 0) {
  393.         $response = lusers();
  394.     } else if(preg_match("/^follow (\d+)/i",$body,$match) !== 0) {
  395.         $response = follow($from,$match[1]);
  396.     } else if(preg_match("/^unfollow (\d+)/i",$body,$match) !== 0) {
  397.         $response = unfollow($from,$match[1]);
  398.     } else if(preg_match("/^help ([a-z]+)/i",$body,$match) !== 0) {
  399.         $response = getHelp($match[1]);
  400.     } else if(preg_match("/^(help|menu)/i",$body,$match) !== 0) {
  401.         $response = "cinfo|cname|cnick|fortune|info|lusers|msg|status|recent|menu|no cmd 2 update status|help cmd|follow|unfollow";
  402.     } else if(preg_match("/^msg (\d+) (.+)/i",$body,$match) !== 0) {
  403.         $response = msg($from,$match[1],$match[2]);
  404.     } else if(preg_match("/^recent/",$body,$match) !== 0) {
  405.         $response = getMostRecent();   
  406.     } else if(preg_match("/^status (\d+)/i",$body,$match) !== 0) {
  407.         $response = getStatus($match[1]);
  408.     } else {
  409.         $response = updateStatus($from,$body);
  410.     }
  411. }
  412. sendResponse($email,$response);
  413. postDebug();
  414. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement