diff options
author | Patrick Davison <snapwilliam@gmail.com> | 2013-01-15 01:22:15 -0800 |
---|---|---|
committer | Patrick Davison <snapwilliam@gmail.com> | 2013-01-15 01:22:15 -0800 |
commit | 43067b7e18c0bd44f703a8bee8770bcdab674f20 (patch) | |
tree | 4f492083a9675bb6cefec53d6e1c1f0da1870643 | |
parent | cb9620d7917cc7fa4aeee54b21d32593a50b081c (diff) | |
download | pathery-43067b7e18c0bd44f703a8bee8770bcdab674f20.tar.xz |
This is the chat app* whoops.
-rw-r--r-- | ajax_chat.php | 78 | ||||
-rw-r--r-- | includes/chats.php | 74 | ||||
-rw-r--r-- | pages/chat.php | 220 |
3 files changed, 372 insertions, 0 deletions
diff --git a/ajax_chat.php b/ajax_chat.php new file mode 100644 index 0000000..37521de --- /dev/null +++ b/ajax_chat.php @@ -0,0 +1,78 @@ +<?
+session_start();
+$userID = $_SESSION['userID'];
+
+include('includes/chats.php');
+
+$sent = false;
+
+if ($_REQUEST['messages']) {
+ $messages = $_REQUEST['messages'];
+ $messages = explode("|:|", $messages);
+ foreach ($messages as $message) {
+ $message = chatFilter(stripslashes($message));
+ $insertID = addchat($userID, $message);
+ }
+
+ $sent = true;
+ //echo "IID:$insertID:";
+}
+
+
+if ($_REQUEST['getChatFromID']) {
+ if ($sent) {
+ //Wait .2 seconds
+ usleep(200000);
+ //echo "yesSent";
+ for( $i = 1; $i < 3; $i++) {
+ //echo "loop";
+ $data = getChat($_REQUEST['getChatFromID']);
+ if ($data !== false) {
+ //$data['debug'][] = "Count times: $i";
+ //$data['debug'][] = "Count value:".count($data);
+ echo prepareChatData($data);
+ exit;
+ }
+ //Wait .1 seconds
+ usleep(100000);
+ }
+ }
+ //echo "reachedEnd";
+ $json = prepareChatData(getChat($_REQUEST['getChatFromID']));
+ echo $json;
+ exit;
+}
+
+function prepareChatData($data) {
+ foreach($data as &$user) {
+ //$user['message'] = $user['message'].'write-append';
+ $user['secondsSince'] = strtotime($user['dateSent']) - strtotime("now");
+ }
+ return json_encode($data);
+}
+
+
+
+function chatFilter($chat) {
+ //cuss words
+ $chat = str_replace("fuck you", "i am moron", $chat);
+ $chat = str_replace("fuck", "dumb", $chat);
+
+ $chat = str_replace("asshole", "dumb", $chat);
+ $chat = str_replace("damnit", "dumb", $chat);
+ $chat = str_replace("damn", "dumb", $chat);
+ $chat = str_replace("cunt", "dumb", $chat);
+ $chat = str_replace("shit", "dumb", $chat);
+ $chat = str_replace("bitch", "dumb", $chat);
+ $chat = str_replace("sucks ass", "is dumb", $chat);
+ $chat = str_replace("suck ass", "is dumb", $chat);
+ $chat = str_replace("stfu", "shutup", $chat);
+ $chat = str_replace("omfg", "omg", $chat);
+ $chat = str_replace("fuk", "dumb", $chat);
+ return $chat;
+}
+
+
+
+
+?>
\ No newline at end of file diff --git a/includes/chats.php b/includes/chats.php new file mode 100644 index 0000000..56c88d1 --- /dev/null +++ b/includes/chats.php @@ -0,0 +1,74 @@ +<?
+/*
+ * addChat, getChat functions
+*/
+include_once('sqli.php');
+
+define('CHAT_ROWS_TO_KEEP', 30);
+
+
+function addChat($userID, $message) {
+ global $mysqli;
+
+ $stmt = $mysqli->prepare("INSERT INTO `chat`
+ (`userID`, `message`)
+ VALUES (?, ?)");
+ $stmt->bind_param('is', $userID, $message);
+ $stmt->execute();
+
+ $ID = $stmt->insert_id;
+ $stmt->close();
+ // $mysqli->close;
+ return $ID;
+}
+
+function getChat($startID = 0) {
+ global $mysqli;
+ $array = array();
+
+ if (!is_numeric($startID))
+ return false;
+
+ //Not sure why this happens; but just return nothing..
+ if (!is_object($mysqli))
+ die("Error creating object");
+
+ $res = $mysqli->query("
+ SELECT
+ chat.ID, chat.userID, chat.message, chat.dateSent,
+ users.displayName,
+ userData.displayColor,
+ userData.wallColor,
+ userData.wallEmblem
+ FROM `chat`
+ LEFT JOIN `userData`
+ ON chat.userID = userData.userID
+ LEFT JOIN `users`
+ ON chat.userID = users.ID
+ WHERE chat.ID > '$startID'
+ ORDER BY chat.ID ASC
+ ");
+ $array = array();
+ if ($res->num_rows == 0) {
+ $res->close();
+ return false;
+ }
+ while ($response = $res->fetch_assoc()) {
+ $array[] = $response;
+ $lastID = $response['ID'];
+ }
+ $res->close();
+
+ if (count($array) < 1)
+ return false;
+
+ //Turnicate messages.
+ $deleteFromID = $lastID - CHAT_ROWS_TO_KEEP;
+ $mysqli->query("DELETE FROM `chat` WHERE `ID` < $deleteFromID");
+ $mysqli->close();
+
+ return $array;
+}
+
+
+?>
\ No newline at end of file diff --git a/pages/chat.php b/pages/chat.php new file mode 100644 index 0000000..11bb935 --- /dev/null +++ b/pages/chat.php @@ -0,0 +1,220 @@ +<?php
+htmlHeader(
+ array('stats'), 'Chat',
+ 'BetaChat for Pathery.com',
+ array('scores', 'dateformat')
+);
+?>
+<body>
+<?php
+topbar($Links);
+
+?>
+
+<style>
+.chatMessage {
+ width:99%;
+ height:40px;
+ margin:5px;
+ background-color:#222;
+ display:block;
+ white-space:nowrap;
+ overflow-x:hidden;
+}
+.chatTimestamp {
+ font-family:"Times New Roman", Times, serif;
+
+
+ float:left;
+ padding: 5px 3px 3px 3px;
+ min-width:70px;
+ overflow:hidden;
+ text-overflow: ellipsis;
+ white-space:nowrap;
+}
+.chatUsername {
+ font-family:Comic Sans MS, Comic Sans MS5, cursive;
+ float:left;
+ padding: 5px 0px 3px 3px;
+ min-width:60px;
+ overflow:hidden;
+ text-overflow: ellipsis;
+ white-space:nowrap;
+}
+.chatText {
+ font-family:Comic Sans MS, Comic Sans MS5, cursive;
+ width:99%;
+ float:left;
+ padding: 5px 0px 3px 3px;
+ width:330px;
+
+}
+
+#chatContainer {
+ border:1px solid red;
+ width:600px;
+ margin:0 auto;
+ height:400px;
+ overflow:auto;
+}
+.chatContainer2 {
+ border:1px solid yellow;
+ width:610px;
+ margin:0 auto;
+ height:450px;
+ overflow:auto;
+}
+
+
+
+</style>
+
+<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
+
+<script>
+var lastID = 1;
+var chatTimerDelay = 5000;
+
+var skipNextGetChat = false;
+
+var chatBuffer = new Array();
+
+getChatTimer();
+function getChatTimer() {
+ setTimeout("getChatTimer()", chatTimerDelay);
+ if (skipNextGetChat) {
+ skipNextGetChat = false;
+ return;
+ }
+ getChat();
+}
+
+
+getChat();
+function getChatDone(data) {
+ var items = [];
+ var p; //our prep string
+ var newChats = false;
+
+ //console.log('datalength', data.length);
+
+ if (data.length < 3 || data == 'false')
+ return;
+
+ console.log("START BUILD");
+
+ console.log("JSON TO PARSE", data);
+
+ json = jQuery.parseJSON(data);
+
+ console.log("JSON PREPED");
+
+ $.each(json, function(key, user) {
+
+ var postDate = new Date();
+ postDate.setTime(postDate.getTime() + user.secondsSince * 1000);
+ //var timestamp = postDate.format("ddd h:MM TT");
+ var timestamp = postDate.format("h:MM:ss");
+
+ console.log("INSIDE BUILD START");
+ p = '';
+ p = p+ " <span class='chatTimestamp'>["+timestamp+"]</span>";
+ p = p+ " <div class='grid_td' style='float:left; width:35px; height:35px; background:"+user.wallColor+" url(images/marks/"+user.wallEmblem+");'>";
+ p = p+ " <div style='background-color:transparent;' class='grid_td_inner grid_td_rocks'>";
+ p = p+ " </div>";
+ p = p+ " </div>";
+
+ p = p+ "<span class='chatUsername'><a href='achievements?id="+user.userID+"' style='color:"+user.displayColor+"'>";
+ //p = p+ user.dateSent+": ";
+ p = p+ user.displayName+"</a>:</span>";
+
+ p = p+ " <span class='chatText'>";
+ p = p+ htmlEncode(user.message);
+ p = p+ " </span>";
+
+ lastID = user.ID;
+
+ items.unshift('<div class="chatMessage" id="' + key + '">' + p + '</div>');
+ newChats = true;
+ });
+
+ console.log("BUILD DONE");
+
+ if (newChats) {
+ var new_chatDiv = $('<div/>', {
+ 'class': 'my-new-list',
+ 'style': 'display: none',
+ html: items.join('')
+ });
+ new_chatDiv.prependTo('#chatContainer').slideDown('fast');
+ }
+ console.log('(END) lastID', lastID);
+}
+
+function getChat(message) {
+ console.log('LASTID:', lastID);
+ var dataString = 'getChatFromID='+lastID;
+ if (chatBuffer.length > 0) {
+ dataString += '&send=true&messages='+chatBuffer.join('|:|');
+ chatBuffer.length = 0;
+ }
+ //$.getJSON('ajax_chat.php?getChatFromID='+lastID++message, );
+
+ $.ajax({
+ type: "GET",
+ url: "ajax_chat.php",
+ data: dataString,
+ success: function(data) {getChatDone(data);}
+ });
+
+}
+
+function sendChat() {
+ var message = $("input#message").val().replace("|:|", "||");
+ chatBuffer.push(message);
+ $("input#message").val('');
+ if (skipNextGetChat == false) {
+ skipNextGetChat = true;
+ getChat();
+ }
+ return false;
+}
+
+$(document).ready(function() {
+
+ $('#sendChat').live("submit", function() {
+ sendChat()
+ });
+
+});
+
+function htmlEncode(value){
+ if (value) {
+ return jQuery('<div />').text(value).html();
+ } else {
+ return '';
+ }
+}
+
+</script>
+
+<h3>this page will overflow...</h3>
+
+<div class='chatContainer2'>
+ <form id='sendChat' onsubmit="return false">
+ <input type="hidden" name="stuff" value="1724">
+ <input type="text" name="message" id="message" value="" maxlength="255" autocomplete="off" >
+ <input type="button" class="send" id='chatSendBtn' value='Send' onClick="sendChat();">
+ </form>
+ <div id='chatContainer'>
+
+ </div>
+
+</div>
+
+
+<?
+
+
+htmlFooter();
+?>
\ No newline at end of file |