Advertisement
jamboljack

chat.php

Dec 24th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.42 KB | None | 0 0
  1. <?php
  2. define ('DBPATH','localhost');
  3. define ('DBUSER','root');
  4. define ('DBPASS','jambolj4ck');
  5. define ('DBNAME','db_alumniteknik');
  6.  
  7. session_start();
  8.  
  9. global $dbh;
  10. $dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
  11. mysql_selectdb(DBNAME, $dbh);
  12. echo $this->db->last_query();
  13.  
  14. if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); }
  15. if ($_GET['action'] == "sendchat") { sendChat(); }
  16. if ($_GET['action'] == "closechat") { closeChat(); }
  17. if ($_GET['action'] == "startchatsession") { startChatSession(); }
  18.  
  19. if (!isset($_SESSION['chatHistory'])) {
  20.     $_SESSION['chatHistory'] = array();
  21. }
  22.  
  23. if (!isset($_SESSION['openChatBoxes'])) {
  24.     echo "Open Chat";
  25.     $_SESSION['openChatBoxes'] = array();  
  26. }
  27.  
  28. function chatHeartbeat() { 
  29.     $sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['username'])."' AND recd = 0) order by id ASC";
  30.     $query = mysql_query($sql);
  31.     $items = '';
  32.  
  33.     $chatBoxes = array();
  34.  
  35.     while ($chat = mysql_fetch_array($query)) {
  36.  
  37.         if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
  38.             $items = $_SESSION['chatHistory'][$chat['from']];
  39.         }
  40.  
  41.         $chat['message'] = sanitize($chat['message']);
  42.  
  43.         $items .= <<<EOD
  44.                        {
  45.             "s": "0",
  46.             "f": "{$chat['from']}",
  47.             "m": "{$chat['message']}"
  48.        },
  49. EOD;
  50.  
  51.     if (!isset($_SESSION['chatHistory'][$chat['from']])) {
  52.         $_SESSION['chatHistory'][$chat['from']] = '';
  53.     }
  54.  
  55.     $_SESSION['chatHistory'][$chat['from']] .= <<<EOD
  56.                            {
  57.             "s": "0",
  58.             "f": "{$chat['from']}",
  59.             "m": "{$chat['message']}"
  60.        },
  61. EOD;
  62.        
  63.         unset($_SESSION['tsChatBoxes'][$chat['from']]);
  64.         $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
  65.     }
  66.  
  67.     if (!empty($_SESSION['openChatBoxes'])) {
  68.     foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
  69.         if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
  70.             $now = time()-strtotime($time);
  71.             $time = date('g:iA M dS', strtotime($time));
  72.  
  73.             $message = "Sent at $time";
  74.             if ($now > 180) {
  75.                 $items .= <<<EOD
  76. {
  77. "s": "2",
  78. "f": "$chatbox",
  79. "m": "{$message}"
  80. },
  81. EOD;
  82.  
  83.     if (!isset($_SESSION['chatHistory'][$chatbox])) {
  84.         $_SESSION['chatHistory'][$chatbox] = '';
  85.     }
  86.  
  87.     $_SESSION['chatHistory'][$chatbox] .= <<<EOD
  88.         {
  89. "s": "2",
  90. "f": "$chatbox",
  91. "m": "{$message}"
  92. },
  93. EOD;
  94.             $_SESSION['tsChatBoxes'][$chatbox] = 1;
  95.         }
  96.         }
  97.     }
  98. }
  99.  
  100.     $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['username'])."' and recd = 0";
  101.     $query = mysql_query($sql);
  102.  
  103.     if ($items != '') {
  104.         $items = substr($items, 0, -1);
  105.     }
  106. header('Content-type: application/json');
  107. ?>
  108. {
  109.         "items": [
  110.             <?php echo $items;?>
  111.         ]
  112. }
  113.  
  114. <?php
  115.             exit(0);
  116. }
  117.  
  118. function chatBoxSession($chatbox) {
  119.    
  120.     $items = '';
  121.    
  122.     if (isset($_SESSION['chatHistory'][$chatbox])) {
  123.         $items = $_SESSION['chatHistory'][$chatbox];
  124.     }
  125.  
  126.     return $items;
  127. }
  128.  
  129. function startChatSession() {
  130.     $items = '';
  131.     if (!empty($_SESSION['openChatBoxes'])) {
  132.         foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
  133.             $items .= chatBoxSession($chatbox);
  134.         }
  135.     }
  136.  
  137.     if ($items != '') {
  138.         $items = substr($items, 0, -1);
  139.     }
  140.  
  141. header('Content-type: application/json');
  142. ?>
  143. {
  144.         "username": "<?php echo $_SESSION['username'];?>",
  145.         "items": [
  146.             <?php echo $items;?>
  147.         ]
  148. }
  149.  
  150. <?php
  151.  
  152.  
  153.     exit(0);
  154. }
  155.  
  156. function sendChat() {
  157.     echo "SendChat";
  158.     $from = $_SESSION['username'];
  159.     $to = $_POST['to'];
  160.     $message = $_POST['message'];
  161.  
  162.     $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());
  163.    
  164.     $messagesan = sanitize($message);
  165.  
  166.     if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
  167.         $_SESSION['chatHistory'][$_POST['to']] = '';
  168.     }
  169.  
  170.     $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
  171.                        {
  172.             "s": "1",
  173.             "f": "{$to}",
  174.             "m": "{$messagesan}"
  175.        },
  176. EOD;
  177.  
  178.  
  179.     unset($_SESSION['tsChatBoxes'][$_POST['to']]);
  180.  
  181.     $sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
  182.     $query = mysql_query($sql);
  183.     // echo $this->db->last_query();
  184.     echo "1";
  185.     exit(0);
  186. }
  187.  
  188. function closeChat() {
  189.  
  190.     unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  191.    
  192.     echo "1";
  193.     exit(0);
  194. }
  195.  
  196. function sanitize($text) {
  197.     $text = htmlspecialchars($text, ENT_QUOTES);
  198.     $text = str_replace("\n\r","\n",$text);
  199.     $text = str_replace("\r\n","\n",$text);
  200.     $text = str_replace("\n","<br>",$text);
  201.     return $text;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement