Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Eliúdy Tomás
  5. * Date: 25-06-2016
  6. * Time: 23:24
  7. * <> <> <> <> <>
  8. */
  9. if(isset($_GET)) {
  10. require_once('BD.class.php');
  11. BD::pdo();
  12.  
  13. $userOnline = (int)$_GET['user'];
  14. $timestamp = ($_GET['timestamp'] == 0) ? time() : strip_tags(trim($_GET['timestamp']));
  15. $lastid = (isset($_GET['lastid']) && !empty($_GET['lastid'])) ? $_GET['lastid'] : 0;
  16.  
  17. if(empty($timestamp)){
  18. die(json_encode(array('status' => 'erro')));
  19. }
  20.  
  21. $tempoGasto = 0;
  22. $lastidQuery = '';
  23.  
  24. if(!empty($lastid)){
  25. $lastidQuery = ' AND `id` > '.$lastid;
  26. }
  27.  
  28. if($_GET['timestamp'] == 0){
  29. $verifica = BD::pdo()-> prepare("SELECT *FROM msg WHERE lido = 0 ORDER BY id DESC ");
  30. }else{
  31. $verifica = BD::pdo()-> prepare("SELECT *FROM msg WHERE time >= $timestamp".$lastidQuery." AND lido = 0 ORDER BY id DESC");
  32. }
  33. $verifica -> execute();
  34. $resultados = $verifica -> rowCount();
  35.  
  36. if($resultados <= 0){
  37. while($resultados <= 0){
  38. if($resultados <= 0){
  39. // Durar 30 Segundos verificando!
  40. if($tempoGasto >= 30){
  41. die(json_encode(array('status' => 'vazio','lastid' => 0, 'timestamp' => time())));
  42. exit;
  43. }
  44.  
  45. //Descansar o script por um segundo!
  46. sleep(1);
  47.  
  48. $verifica = BD::pdo()->prepare("SELECT *FROM msg WHERE time >= $timestamp".$lastidQuery." AND lido = 0 ORDER BY id DESC");
  49.  
  50. $verifica -> execute();
  51. $resultados = $verifica -> rowCount();
  52. $tempoGasto += 1;
  53. }
  54. }
  55. }
  56. $novasMensagens = array();
  57.  
  58. if($resultados >= 1){
  59. while($row = $verifica -> fetch()){
  60. $fotoUser = '';
  61. $janela_de = 0;
  62.  
  63. if($userOnline == $row['id_de']){
  64. $janela_de = $row['id_para'];
  65. }elseif($userOnline == $row['id_para']){
  66. $janela_de = $row['id_de'];
  67. $pegaUsr = BD::pdo()-> prepare("SELECT foto FROM user WHERE id='$row->id_de'");
  68. $pegaUsr -> execute();
  69.  
  70. while($usr = $pegaUsr -> fetch()){
  71. $fotoUser = ($usr['foto'] == '') ? 'default.php' : $usr['foto'];
  72. }
  73. }
  74.  
  75. $emotions = array(':)',':D',':(',':O',';)','(:','(;',':M');
  76. $imgs = array(
  77. '<img src="default/x1.png" width="15" >',
  78. '<img src="default/x2.png" width="15" >',
  79. '<img src="default/x3.png" width="15" >',
  80. '<img src="default/x4.png" width="15" >',
  81. '<img src="default/x5.png" width="15" >',
  82. '<img src="default/x6.png" width="15" >',
  83. '<img src="default/x7.png" width="15" >',
  84. '<img src="default/x8.png" width="15" >'
  85. );
  86.  
  87. $msg = str_replace($emotions,$imgs,$row->mensagem);
  88. $novasMensagens[] = array(
  89. 'id' => $row['id'],
  90. 'mensagem' => utf8_encode($msg),
  91. 'fotoUser' => $fotoUser,
  92. 'id_de' => $row['id_de'],
  93. 'id_para' => $row['id_para'],
  94. 'janela_de' => $janela_de
  95. );
  96. }
  97. }
  98.  
  99. $ultimaMsg = end($novasMensagens);
  100. $ultimoId = $ultimaMsg['id'];
  101. die(json_encode(array('status' => 'resultados','timestamp' => time(), 'lastid' => $ultimoId, 'dados' => $novasMensagens)));
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement