Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: Eliúdy Tomás
- * Date: 25-06-2016
- * Time: 23:24
- * <> <> <> <> <>
- */
- if(isset($_GET)) {
- require_once('BD.class.php');
- BD::pdo();
- $userOnline = (int)$_GET['user'];
- $timestamp = ($_GET['timestamp'] == 0) ? time() : strip_tags(trim($_GET['timestamp']));
- $lastid = (isset($_GET['lastid']) && !empty($_GET['lastid'])) ? $_GET['lastid'] : 0;
- if(empty($timestamp)){
- die(json_encode(array('status' => 'erro')));
- }
- $tempoGasto = 0;
- $lastidQuery = '';
- if(!empty($lastid)){
- $lastidQuery = ' AND `id` > '.$lastid;
- }
- if($_GET['timestamp'] == 0){
- $verifica = BD::pdo()-> prepare("SELECT *FROM msg WHERE lido = 0 ORDER BY id DESC ");
- }else{
- $verifica = BD::pdo()-> prepare("SELECT *FROM msg WHERE time >= $timestamp".$lastidQuery." AND lido = 0 ORDER BY id DESC");
- }
- $verifica -> execute();
- $resultados = $verifica -> rowCount();
- if($resultados <= 0){
- while($resultados <= 0){
- if($resultados <= 0){
- // Durar 30 Segundos verificando!
- if($tempoGasto >= 30){
- die(json_encode(array('status' => 'vazio','lastid' => 0, 'timestamp' => time())));
- exit;
- }
- //Descansar o script por um segundo!
- sleep(1);
- $verifica = BD::pdo()->prepare("SELECT *FROM msg WHERE time >= $timestamp".$lastidQuery." AND lido = 0 ORDER BY id DESC");
- $verifica -> execute();
- $resultados = $verifica -> rowCount();
- $tempoGasto += 1;
- }
- }
- }
- $novasMensagens = array();
- if($resultados >= 1){
- while($row = $verifica -> fetch()){
- $fotoUser = '';
- $janela_de = 0;
- if($userOnline == $row['id_de']){
- $janela_de = $row['id_para'];
- }elseif($userOnline == $row['id_para']){
- $janela_de = $row['id_de'];
- $pegaUsr = BD::pdo()-> prepare("SELECT foto FROM user WHERE id='$row->id_de'");
- $pegaUsr -> execute();
- while($usr = $pegaUsr -> fetch()){
- $fotoUser = ($usr['foto'] == '') ? 'default.php' : $usr['foto'];
- }
- }
- $emotions = array(':)',':D',':(',':O',';)','(:','(;',':M');
- $imgs = array(
- '<img src="default/x1.png" width="15" >',
- '<img src="default/x2.png" width="15" >',
- '<img src="default/x3.png" width="15" >',
- '<img src="default/x4.png" width="15" >',
- '<img src="default/x5.png" width="15" >',
- '<img src="default/x6.png" width="15" >',
- '<img src="default/x7.png" width="15" >',
- '<img src="default/x8.png" width="15" >'
- );
- $msg = str_replace($emotions,$imgs,$row->mensagem);
- $novasMensagens[] = array(
- 'id' => $row['id'],
- 'mensagem' => utf8_encode($msg),
- 'fotoUser' => $fotoUser,
- 'id_de' => $row['id_de'],
- 'id_para' => $row['id_para'],
- 'janela_de' => $janela_de
- );
- }
- }
- $ultimaMsg = end($novasMensagens);
- $ultimoId = $ultimaMsg['id'];
- die(json_encode(array('status' => 'resultados','timestamp' => time(), 'lastid' => $ultimoId, 'dados' => $novasMensagens)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement