Advertisement
Corey

Untitled

Mar 17th, 2011
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php
  2.     /**
  3.     /*           OSPHPChat Alpha 1 - 2011
  4.     /*
  5.     /*           File: /modules/chat/ajax.php
  6.     /*
  7.     /*       Website: www.OSPHPChat.com
  8.     /*
  9.     /*           Author: Daniel Hood <danny@osphpchat.com>
  10.     /*
  11.     /*
  12.     /*
  13.     **/
  14.  
  15.     if ($mode == 'send')
  16.     {
  17.         $message = $OSPHPChat->Purify->get('message', 'POST', 'string');
  18.         if (isset($message) && !empty($message))
  19.         {
  20.             if ($OSPHPChat->User->check_auth('main_talk'))
  21.             {
  22.                 $data = array(
  23.                     'user_id' => $OSPHPChat->User->user_info['id'],
  24.                     'to' => 'room:' . $OSPHPChat->Messages->room . ';',
  25.                     'message' => $message,
  26.                     'date' => $OSPHPChat->data,
  27.                     'time' => $OSPHPChat->time,
  28.                     'color' => $OSPHPChat->User->user_info['message_color'],
  29.                     'user_color' => $OSPHPChat->User->user_info['user_color'],
  30.                     'username' => $OSPHPChat->User->user_info['username']
  31.                 );
  32.                 $OSPHPChat->Messages->send_message($data);
  33.             }
  34.             else
  35.             {
  36.                 $error = "You are not permitted to talk.";
  37.             }
  38.         }
  39.         else
  40.         {
  41.             $error = "You cannot send blank messages.";
  42.         }
  43.         if ($error)
  44.         {
  45.             echo json_encode($error);
  46.         }
  47.     }
  48.     elseif ($mode == 'load')
  49.     {
  50.         $OSPHPChat->Messages->starting_id = $_SESSION['last_message'];
  51.         $OSPHPChat->Messages->get_messages();
  52.         $Messages = array();
  53.         foreach ($OSPHPChat->Messages->get_messages() as $message)
  54.         {
  55.             // Establishes User Color \\
  56.             if (isset($message['user_color']))
  57.             {
  58.                 $message['sender'] = "<a href='#' style='color:#" . $message['user_color'] . ";'>" . $message['username'] . "</a>";
  59.             }
  60.             else
  61.             {
  62.                 $message['sender'] = "<a href='#'>" . $message['username'] . "</a>";
  63.             }
  64.  
  65.             // Establishes Message Color \\
  66.             if (isset($message['color']))
  67.             {
  68.                 $message['text'] = "<span style='color:#" . $message['color'] . ";'>" . $message['message'] . "</span>";
  69.             }
  70.             else
  71.             {
  72.                 $message['text'] = "<span>" . $message['message'] . "</span>";
  73.             }
  74.             $_SESSION['last_message'] = $message['id'];
  75.             $Smarty->assign('message', $message);
  76.             $Messages[] = $Smarty->fetch('_message.tpl');
  77.         }
  78.         echo json_encode($Messages);
  79.     }
  80.     elseif ($mode == 'users')
  81.     {
  82.         $offline_time = time() - 15;
  83.  
  84.         // Getting [new] online users \\
  85.         $online_users = $OSPHPChat->db->Execute("SELECT * FROM `" . $OSPHPChat->config['database']['tables']['users'] . "` WHERE `online_time`> '" . $offline_time . "'");
  86.         while ($online_user = $online_users->FetchRow())
  87.         {
  88.             $All_users[$online_user['id']] = array(
  89.                 'id' => $online_user['id'],
  90.                 'username' => $online_user['username']
  91.             );
  92.             if (!isset($_SESSION['online_users'][$online_user['id']]))
  93.             {
  94.                 $Smarty->assign('user', $online_user);
  95.                 $Users[online][$online_user['id']] = array(
  96.                     'html' => $Smarty->fetch('_user_list.tpl'),
  97.                     'id' => $online_user['id']
  98.                 );
  99.             }
  100.             $Userids_online[$online_user['id']] = $online_user['username'];
  101.         }
  102.  
  103.         // Removing offline users \\
  104.         if (is_array($_SESSION['online_users']))
  105.         {
  106.             foreach ($_SESSION['online_users'] as $online_user)
  107.             {
  108.                 if (!isset($Userids_online[$online_user['id']]))
  109.                 {
  110.                     $Users['offline'][] = $online_user;
  111.                 }
  112.             }
  113.         }
  114.  
  115.         $_SESSION['online_users'] = $All_users;
  116.         echo json_encode($Users);
  117.     }
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement