Advertisement
gpgautier

RedNet Chat Remote 0.5 - server

Nov 10th, 2012
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.27 KB | None | 0 0
  1. <?php
  2.  
  3. $db = mysql_connect('localhost', '********', '*********');
  4. mysql_select_db('*********', $db);
  5.  
  6. class RedNet_Message
  7. {
  8.     public $ID;
  9.     public $protocol = 'rednet';
  10.     public $session = null;
  11.     public $channel = 1;
  12.     public $origin = -1;
  13.     public $nickname = null;
  14.     public $header = null;
  15.     public $body = null;
  16.     public $status = null;
  17.     public $online = null;
  18.     public $sender = null;
  19.  
  20.     public $bodySerialized = false;
  21.  
  22.     public function fromArray($data)
  23.     {  
  24.         if (isset($data['ID'])) {
  25.             $this->ID = $data['ID'];
  26.         }
  27.  
  28.         if (isset($data['protocol'])) {
  29.             $this->protocol = $data['protocol'];
  30.         }
  31.  
  32.         if (isset($data['session']) && is_numeric($data['session'])) {
  33.             $this->session = $data['session'];
  34.         }
  35.  
  36.         if (isset($data['channel'])) {
  37.             $this->channel = (int) $data['channel'];
  38.         }
  39.  
  40.         if (isset($data['origin'])) {
  41.             $this->origin = (int) $data['origin'];
  42.         }
  43.  
  44.         if (isset($data['nickname'])) {
  45.             $this->nickname = $data['nickname'];
  46.         }
  47.  
  48.         if (isset($data['header'])) {
  49.             $this->header = $data['header'];
  50.         }
  51.  
  52.         if (isset($data['body'])) {
  53.             $this->body = $data['body'];
  54.         }
  55.     }
  56.  
  57.     public function toSerializedLua()
  58.     {
  59.         $serialized = array();
  60.  
  61.         $id = $this->ID;
  62.         if (!is_numeric($id)) {
  63.             $id = 'nil';
  64.         }
  65.  
  66.         if (!is_string($this->body)) {
  67.             $body = 'nil';
  68.         } else {
  69.             if (!$this->bodySerialized) {
  70.                 $body = '"' . $this->body . '"';
  71.             } else {
  72.                 $body = $this->body;
  73.             }
  74.         }
  75.  
  76.         $session = $this->session;
  77.         if (!is_numeric($session)) {
  78.             $session = 'nil';
  79.         }
  80.        
  81.         $nickname = 'nil';
  82.         if (is_string($this->nickname)) {
  83.             $nickname = '"' . $this->nickname . '"';
  84.         }
  85.  
  86.         $online = 'nil';
  87.         if (is_string($this->online)) {
  88.             $online = '"' . $this->online . '"';
  89.         }
  90.  
  91.         $sender = 'nil';
  92.         if (is_string($this->sender)) {
  93.             $sender = '"' . $this->sender . '"';
  94.         }
  95.  
  96.         $serialized[] = '["ID"]=' . $id;
  97.         $serialized[] = '["protocol"]="' . $this->protocol . '"';
  98.         $serialized[] = '["session"]=' . $session;
  99.         $serialized[] = '["origin"]=' . $this->origin;
  100.         $serialized[] = '["nickname"]=' . $nickname;
  101.         $serialized[] = '["header"]="' . $this->header . '"';
  102.         $serialized[] = '["body"]=' . $body;
  103.         $serialized[] = '["status"]="' . $this->status . '"';
  104.         $serialized[] = '["online"]=' . $online . '';
  105.         $serialized[] = '["sender"]=' . $sender . '';
  106.         $serialized[] = '["channel"]=' . $this->channel;
  107.  
  108.         return '{' . implode(',', $serialized) . ',}';
  109.     }
  110. }
  111.  
  112. $request = new Rednet_Message();
  113. $request->fromArray($_POST);
  114.  
  115. $response = new Rednet_Message();
  116. $response->fromArray($_POST);
  117.  
  118. switch ($request->header) {
  119.     case 'join' :
  120.  
  121.         if (strlen($request->nickname) > 1) {
  122.            
  123.             $result = mysql_query("SELECT ID, nickname, origin, active, channel
  124.                 FROM sessions
  125.                 WHERE nickname = '" . $request->nickname . "'
  126.             ", $db);
  127.            
  128.             if (mysql_num_rows($result) === 0) {
  129.                 $time = getTime();
  130.  
  131.                 mysql_query("INSERT INTO sessions (
  132.                     origin, started, nickname, lastupdate, channel
  133.                 ) VALUES (
  134.                     " . $request->origin . ", " . $time . ", '" . $request->nickname . "', " . $time . ", " . $request->body . "
  135.                 )", $db);
  136.                
  137.                 $response->session = mysql_insert_id($db);
  138.  
  139.                 mysql_query("INSERT INTO messages
  140.                     (session, header, body, datetime, channel)
  141.                     VALUES ('" . $response->session . "', 'status', '" . $request->nickname . " joined channel " . $request->body . "', " . getTime() . ", " . $request->body . ")
  142.                 ");
  143.                
  144.                 $response->channel = $request->body;
  145.                 $response->status = 'ok';
  146.             } else {
  147.                 $row = mysql_fetch_assoc($result);
  148.  
  149.                 $response->session = $row['ID'];
  150.  
  151.                 if ((int) $row['origin'] === $request->origin) {
  152.                     $time = getTime();
  153.                     if ((int) $row['active'] === 0) {
  154.                         mysql_query("UPDATE sessions SET started = " . $time . ", lastupdate = " . $time . ", active  = 1 WHERE ID = " . $row['ID'], $db);
  155.  
  156.                         mysql_query("INSERT INTO messages
  157.                             (session, header, body, datetime, channel)
  158.                             VALUES ('" . $response->session . "', 'status', '" . $request->nickname . " is now online ', " . getTime() . ", " . $row['channel'] . ")
  159.                         ");
  160.  
  161.                         $response->channel = $row['channel'];
  162.                     } else {
  163.                         if ($request->body != $row['channel']) {
  164.                             mysql_query("UPDATE sessions SET channel = " . $request->body . " WHERE ID = " . $row['ID']);
  165.                            
  166.                             mysql_query("INSERT INTO messages
  167.                                 (session, header, body, datetime, channel)
  168.                                 VALUES ('" . $response->session . "', 'status', '" . $request->nickname . " joined channel " . $request->body . "', " . getTime() . ", " . $request->body . ")
  169.                             ");
  170.                            
  171.                             $response->channel = $request->body;
  172.                         } else {
  173.                             $response->channel = $row['channel'];
  174.                         }
  175.                     }
  176.                    
  177.                     $response->status = 'ok';
  178.                 } else {
  179.                     $response->status = 'error';
  180.                     $response->body = 'Username invalid or already in use';
  181.                 }
  182.             }
  183.            
  184.            
  185.         }
  186.  
  187.         break;
  188.    
  189.     case 'leave' :
  190.         $result = mysql_query("SELECT active, nickname
  191.             FROM sessions
  192.             WHERE ID = " . $request->session . " AND active = 1
  193.         ", $db);
  194.  
  195.         if (mysql_num_rows($result) > 0) {
  196.             $row = mysql_fetch_assoc($result);
  197.  
  198.             mysql_query("UPDATE sessions SET active = 0 WHERE ID = " . $request->session, $db);
  199.  
  200.             mysql_query("INSERT INTO messages
  201.                 (session, header, body, datetime, channel)
  202.                 VALUES ('" . $request->session . "', 'status', '" . $row['nickname'] . " is now offline ', " . getTime() . ", " . $request->channel . ")
  203.             ");
  204.         }
  205.        
  206.         $response->session = null;
  207.         $response->status = 'ok';
  208.  
  209.         break;
  210.    
  211.     case 'update' :
  212.         $result = mysql_query("SELECT started, lastupdate FROM sessions WHERE ID = " . $request->session, $db);
  213.         $row = mysql_fetch_assoc($result);
  214.  
  215.         $result = mysql_query("SELECT M.ID, M.session, M.target, M.header, M.body, S.origin, S.nickname from messages M LEFT JOIN sessions S ON M.session = S.ID WHERE (M.target IS NULL OR M.target = " . $request->session . ") AND M.datetime >= " . $row['lastupdate'] . " AND M.datetime >= " . $row['started'] . " AND ((M.header = 'msg' AND M.channel IS NULL OR M.channel = " . $request->channel . ")) ORDER BY M.datetime");
  216.  
  217.         mysql_query("UPDATE sessions SET lastupdate = " . getTime() . " WHERE ID = " . $request->session);
  218.        
  219.         $i = 1;
  220.         if (mysql_num_rows($result) > 0) {
  221.             while ($row = mysql_fetch_assoc($result)) {
  222.                 $message = new Rednet_Message();
  223.                 $message->fromArray($row);
  224.  
  225.                 if ($row['header'] == 'msg' && $row['nickname'] != $request->nickname) {
  226.                     $message->sender = $row['nickname'];
  227.                 }
  228.  
  229.                 $response->body .= '[' . $i . ']=' . $message->toSerializedLua() . ',';
  230.                 $i++;
  231.             }
  232.  
  233.             $response->body = '{' . $response->body . '}';
  234.             $response->bodySerialized = true;
  235.         }
  236.  
  237.         $response->status = 'ok';
  238.  
  239.         $result = mysql_query("SELECT ID, nickname, lastupdate, started, channel FROM sessions WHERE active = 1 AND channel = " . $request->channel . " ORDER BY started");
  240.        
  241.         if (mysql_num_rows($result) > 0) {
  242.             $online = array();
  243.             while ($row = mysql_fetch_assoc($result)) {
  244.                 if (getTime() - (int) $row['lastupdate'] > 10000) {
  245.                     mysql_query("UPDATE sessions SET active = 0 WHERE ID = " . $row['ID']);
  246.                     mysql_query("INSERT INTO messages
  247.                         (session, header, body, datetime, channel)
  248.                         VALUES ('" . $row['ID'] . "', 'status', '" . $row['nickname'] . " timed out.', " . getTime() . ", " . $row['channel'] . ")
  249.                     ");
  250.                 } else {
  251.                     if ($row['nickname'] != '') {
  252.                         $online[] = $row['nickname'];
  253.                     }
  254.                 }
  255.             }
  256.  
  257.             $online = implode(', ', $online);
  258.             $response->online = substr($online, 0, strlen($online));
  259.         }
  260.        
  261.  
  262.         break;
  263.    
  264.     case 'say' :
  265.         $body = $request->body . '';
  266.         $body = ucfirst($body);
  267.  
  268.         $lead = substr($body, strlen($body) - 1);
  269.  
  270.         if ($lead != '.' && $lead != '?' && $lead != '!') {
  271.             $body .= '.';
  272.         }
  273.        
  274.  
  275.         mysql_query("INSERT INTO messages
  276.             (session, header, body, datetime, channel)
  277.             VALUES ('" . $request->session . "', 'say', '" . $request->nickname . ": " . $body . "', " . getTime() . ", " . $request->channel . ")
  278.         ");
  279.  
  280.         break;
  281.    
  282.     case 'msg' :
  283.         $body = $request->body . '';
  284.         $body = explode(' ', $body);
  285.         $nickname = $body[0];
  286.         array_shift($body);
  287.         $message = implode(' ', $body);
  288.  
  289.         $result = mysql_query("SELECT ID, nickname FROM sessions WHERE nickname = '" . $nickname . "' AND active = 1");
  290.         if (mysql_num_rows($result) == 0) {
  291.             $response->status = 'error';
  292.             $response->body = $nickname . ' is not available';
  293.         } else {
  294.             $row = mysql_fetch_assoc($result);
  295.  
  296.             mysql_query("INSERT INTO messages
  297.                 (session, target, header, body, datetime)
  298.                 VALUES ('" . $request->session . "', '" . $row['ID'] . "', 'msg', '[" . $request->nickname . " > " . $row['nickname'] . "] " . $message . "', " . getTime() . ")
  299.             ");
  300.  
  301.             mysql_query("INSERT INTO messages
  302.                 (session, target, header, body, datetime)
  303.                 VALUES ('" . $request->session . "', '" . $request->session . "', 'msg', '[" . $request->nickname . " > " . $row['nickname'] . "] " . $message . "', " . getTime() . ")
  304.             ");
  305.  
  306.             $response->status = 'ok';
  307.             $response->body = $row['nickname'];
  308.         }
  309.  
  310.         break;
  311.  
  312.     default :
  313.         $response->status = 'error';
  314.         $response->body = 'Unknown method';
  315.         break;
  316. }
  317.  
  318. mysql_close($db);
  319.  
  320. echo $response->toSerializedLua();
  321.  
  322. function getTime()
  323. {
  324.     return microtime(true) * 1000;
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement