Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $db = mysql_connect('localhost', '********', '*********');
- mysql_select_db('*********', $db);
- class RedNet_Message
- {
- public $ID;
- public $protocol = 'rednet';
- public $session = null;
- public $channel = 1;
- public $origin = -1;
- public $nickname = null;
- public $header = null;
- public $body = null;
- public $status = null;
- public $online = null;
- public $sender = null;
- public $bodySerialized = false;
- public function fromArray($data)
- {
- if (isset($data['ID'])) {
- $this->ID = $data['ID'];
- }
- if (isset($data['protocol'])) {
- $this->protocol = $data['protocol'];
- }
- if (isset($data['session']) && is_numeric($data['session'])) {
- $this->session = $data['session'];
- }
- if (isset($data['channel'])) {
- $this->channel = (int) $data['channel'];
- }
- if (isset($data['origin'])) {
- $this->origin = (int) $data['origin'];
- }
- if (isset($data['nickname'])) {
- $this->nickname = $data['nickname'];
- }
- if (isset($data['header'])) {
- $this->header = $data['header'];
- }
- if (isset($data['body'])) {
- $this->body = $data['body'];
- }
- }
- public function toSerializedLua()
- {
- $serialized = array();
- $id = $this->ID;
- if (!is_numeric($id)) {
- $id = 'nil';
- }
- if (!is_string($this->body)) {
- $body = 'nil';
- } else {
- if (!$this->bodySerialized) {
- $body = '"' . $this->body . '"';
- } else {
- $body = $this->body;
- }
- }
- $session = $this->session;
- if (!is_numeric($session)) {
- $session = 'nil';
- }
- $nickname = 'nil';
- if (is_string($this->nickname)) {
- $nickname = '"' . $this->nickname . '"';
- }
- $online = 'nil';
- if (is_string($this->online)) {
- $online = '"' . $this->online . '"';
- }
- $sender = 'nil';
- if (is_string($this->sender)) {
- $sender = '"' . $this->sender . '"';
- }
- $serialized[] = '["ID"]=' . $id;
- $serialized[] = '["protocol"]="' . $this->protocol . '"';
- $serialized[] = '["session"]=' . $session;
- $serialized[] = '["origin"]=' . $this->origin;
- $serialized[] = '["nickname"]=' . $nickname;
- $serialized[] = '["header"]="' . $this->header . '"';
- $serialized[] = '["body"]=' . $body;
- $serialized[] = '["status"]="' . $this->status . '"';
- $serialized[] = '["online"]=' . $online . '';
- $serialized[] = '["sender"]=' . $sender . '';
- $serialized[] = '["channel"]=' . $this->channel;
- return '{' . implode(',', $serialized) . ',}';
- }
- }
- $request = new Rednet_Message();
- $request->fromArray($_POST);
- $response = new Rednet_Message();
- $response->fromArray($_POST);
- switch ($request->header) {
- case 'join' :
- if (strlen($request->nickname) > 1) {
- $result = mysql_query("SELECT ID, nickname, origin, active, channel
- FROM sessions
- WHERE nickname = '" . $request->nickname . "'
- ", $db);
- if (mysql_num_rows($result) === 0) {
- $time = getTime();
- mysql_query("INSERT INTO sessions (
- origin, started, nickname, lastupdate, channel
- ) VALUES (
- " . $request->origin . ", " . $time . ", '" . $request->nickname . "', " . $time . ", " . $request->body . "
- )", $db);
- $response->session = mysql_insert_id($db);
- mysql_query("INSERT INTO messages
- (session, header, body, datetime, channel)
- VALUES ('" . $response->session . "', 'status', '" . $request->nickname . " joined channel " . $request->body . "', " . getTime() . ", " . $request->body . ")
- ");
- $response->channel = $request->body;
- $response->status = 'ok';
- } else {
- $row = mysql_fetch_assoc($result);
- $response->session = $row['ID'];
- if ((int) $row['origin'] === $request->origin) {
- $time = getTime();
- if ((int) $row['active'] === 0) {
- mysql_query("UPDATE sessions SET started = " . $time . ", lastupdate = " . $time . ", active = 1 WHERE ID = " . $row['ID'], $db);
- mysql_query("INSERT INTO messages
- (session, header, body, datetime, channel)
- VALUES ('" . $response->session . "', 'status', '" . $request->nickname . " is now online ', " . getTime() . ", " . $row['channel'] . ")
- ");
- $response->channel = $row['channel'];
- } else {
- if ($request->body != $row['channel']) {
- mysql_query("UPDATE sessions SET channel = " . $request->body . " WHERE ID = " . $row['ID']);
- mysql_query("INSERT INTO messages
- (session, header, body, datetime, channel)
- VALUES ('" . $response->session . "', 'status', '" . $request->nickname . " joined channel " . $request->body . "', " . getTime() . ", " . $request->body . ")
- ");
- $response->channel = $request->body;
- } else {
- $response->channel = $row['channel'];
- }
- }
- $response->status = 'ok';
- } else {
- $response->status = 'error';
- $response->body = 'Username invalid or already in use';
- }
- }
- }
- break;
- case 'leave' :
- $result = mysql_query("SELECT active, nickname
- FROM sessions
- WHERE ID = " . $request->session . " AND active = 1
- ", $db);
- if (mysql_num_rows($result) > 0) {
- $row = mysql_fetch_assoc($result);
- mysql_query("UPDATE sessions SET active = 0 WHERE ID = " . $request->session, $db);
- mysql_query("INSERT INTO messages
- (session, header, body, datetime, channel)
- VALUES ('" . $request->session . "', 'status', '" . $row['nickname'] . " is now offline ', " . getTime() . ", " . $request->channel . ")
- ");
- }
- $response->session = null;
- $response->status = 'ok';
- break;
- case 'update' :
- $result = mysql_query("SELECT started, lastupdate FROM sessions WHERE ID = " . $request->session, $db);
- $row = mysql_fetch_assoc($result);
- $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");
- mysql_query("UPDATE sessions SET lastupdate = " . getTime() . " WHERE ID = " . $request->session);
- $i = 1;
- if (mysql_num_rows($result) > 0) {
- while ($row = mysql_fetch_assoc($result)) {
- $message = new Rednet_Message();
- $message->fromArray($row);
- if ($row['header'] == 'msg' && $row['nickname'] != $request->nickname) {
- $message->sender = $row['nickname'];
- }
- $response->body .= '[' . $i . ']=' . $message->toSerializedLua() . ',';
- $i++;
- }
- $response->body = '{' . $response->body . '}';
- $response->bodySerialized = true;
- }
- $response->status = 'ok';
- $result = mysql_query("SELECT ID, nickname, lastupdate, started, channel FROM sessions WHERE active = 1 AND channel = " . $request->channel . " ORDER BY started");
- if (mysql_num_rows($result) > 0) {
- $online = array();
- while ($row = mysql_fetch_assoc($result)) {
- if (getTime() - (int) $row['lastupdate'] > 10000) {
- mysql_query("UPDATE sessions SET active = 0 WHERE ID = " . $row['ID']);
- mysql_query("INSERT INTO messages
- (session, header, body, datetime, channel)
- VALUES ('" . $row['ID'] . "', 'status', '" . $row['nickname'] . " timed out.', " . getTime() . ", " . $row['channel'] . ")
- ");
- } else {
- if ($row['nickname'] != '') {
- $online[] = $row['nickname'];
- }
- }
- }
- $online = implode(', ', $online);
- $response->online = substr($online, 0, strlen($online));
- }
- break;
- case 'say' :
- $body = $request->body . '';
- $body = ucfirst($body);
- $lead = substr($body, strlen($body) - 1);
- if ($lead != '.' && $lead != '?' && $lead != '!') {
- $body .= '.';
- }
- mysql_query("INSERT INTO messages
- (session, header, body, datetime, channel)
- VALUES ('" . $request->session . "', 'say', '" . $request->nickname . ": " . $body . "', " . getTime() . ", " . $request->channel . ")
- ");
- break;
- case 'msg' :
- $body = $request->body . '';
- $body = explode(' ', $body);
- $nickname = $body[0];
- array_shift($body);
- $message = implode(' ', $body);
- $result = mysql_query("SELECT ID, nickname FROM sessions WHERE nickname = '" . $nickname . "' AND active = 1");
- if (mysql_num_rows($result) == 0) {
- $response->status = 'error';
- $response->body = $nickname . ' is not available';
- } else {
- $row = mysql_fetch_assoc($result);
- mysql_query("INSERT INTO messages
- (session, target, header, body, datetime)
- VALUES ('" . $request->session . "', '" . $row['ID'] . "', 'msg', '[" . $request->nickname . " > " . $row['nickname'] . "] " . $message . "', " . getTime() . ")
- ");
- mysql_query("INSERT INTO messages
- (session, target, header, body, datetime)
- VALUES ('" . $request->session . "', '" . $request->session . "', 'msg', '[" . $request->nickname . " > " . $row['nickname'] . "] " . $message . "', " . getTime() . ")
- ");
- $response->status = 'ok';
- $response->body = $row['nickname'];
- }
- break;
- default :
- $response->status = 'error';
- $response->body = 'Unknown method';
- break;
- }
- mysql_close($db);
- echo $response->toSerializedLua();
- function getTime()
- {
- return microtime(true) * 1000;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement