Ver Mensaje Individual
  #7 (permalink)  
Antiguo 23/05/2010, 15:01
Avatar de joska1993
joska1993
 
Fecha de Ingreso: septiembre-2009
Mensajes: 76
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Funcion no funciona

si, ya lo consegui, pero ahora ocurre otro problema, y es que no me deja desplazar la barra lateral de la pagina la barra de scroll, que puede ser?

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