function chatHeartbeat($slug, $pageId) { //Add check for open chat boxes in order to keep track of multiple windows if (empty($_SESSION['openChatBoxes']["{$pageId}"])) { $_SESSION['openChatBoxes']["{$pageId}"] = 0; $lastRecord = 0; } else { $lastRecord = $_SESSION['openChatBoxes']["{$pageId}"]; } //**JAG check that SLUG is not null $project = Project::getProjectFromSlug($slug); $projectName = $project->getTitle(); //This line is used to track whether we are switching between projects and need to close down chat rooms $_SESSION['lastProjectID'] = $project->getID(); //signed in user $userId = Session::getUserID(); $chatBoxes = array(); $chats = Chat::getChats($slug, $lastRecord); $numRows = count($chats); $rowIndex = 0; $items = ''; if (is_array($chats)) { foreach ($chats as $row => $chat) { $rowIndex++; if (!isset($_SESSION['openChatBoxes'][$chat['recipient']]) && isset($_SESSION['chatHistory'][$chat['recipient']])) { $items = $_SESSION['chatHistory'][$chat['recipient']]; } //Grab username if available (should always be available) $chatFrom = User::load($chat['sender'])->getUsername(); $chat['message'] = sanitize($chat['message']); $message = str_replace('"', '\\"', formatParagraphs($chat['message'], true)); //Since chatHeartbeat always returns records greater than the stored last id, the only time that // the returned id of a search will match the stored last id will be on the first post in an empty // chat room if ($chat['id'] !== $lastRecord) { $items .= <<<EOD \t\t\t\t\t { \t\t\t"s": "0", \t\t\t"f": "{$chatFrom}", \t\t\t"m": "{$message}", "r": "{$chat['id']}", "t": "{$projectName}" \t }, EOD; } if (!isset($_SESSION['chatHistory'][$chat['recipient']])) { $_SESSION['chatHistory'][$chat['recipient']] = ''; } $_SESSION['chatHistory'][$chat['recipient']] .= <<<EOD \t\t\t\t\t\t { \t\t\t"s": "0", \t\t\t"f": "{$chatFrom}", \t\t\t"m": "{$message}", "r": "{$chat['id']}", "t": "{$projectName}" \t }, EOD; $_SESSION['openChatBoxes'][$chat['recipient']] = $chat['sent']; unset($_SESSION['tsChatBoxes'][$chat['recipient']]); if ($numRows == $rowIndex && $numRows > 0) { $_SESSION['openChatBoxes']["{$pageId}"] = $chat['id']; } } } //Update user record with heart beat (used to tell logged in members) Chat::updateUserLocation($userId, $project->getID()); if ($items != '') { $items = substr($items, 0, -1); } header('Content-type: application/json'); ?> { "items": [ <?php echo $items; ?> ] } <?php exit(0); }
$response = array(); // Handling the supported actions: switch ($_GET['action']) { case 'login': $response = Chat::login($_POST['name'], $_POST['email']); break; case 'checkLogged': $response = Chat::checkLogged(); break; case 'logout': $response = Chat::logout(); break; case 'submitChat': $response = Chat::submitChat($_POST['chatText']); break; case 'getUsers': $response = Chat::getUsers(); break; case 'getChats': $response = Chat::getChats($_GET['lastID']); break; default: throw new Exception('Wrong action'); } echo json_encode($response); } catch (Exception $e) { die(json_encode(array('error' => $e->getMessage()))); } ?>
// Handling the supported actions: switch ($_GET['action']) { case 'login': $response = Chat::login($_POST['name'], $_POST['email'], $_POST['user']); break; case 'checkLogged': $response = Chat::checkLogged(); break; case 'logout': $response = Chat::logout(); break; case 'submitChat': $response = Chat::submitChat($_POST['chatText'], $_POST['room'], $_POST['room_od']); break; case 'getUsers': $response = Chat::getUsers(); break; case 'getChats': $response = Chat::getChats($_GET['lastID'], $_GET['room'], $_GET['room_od']); break; default: throw new Exception('Error'); } if ($_GET['action'] == "getChats") { echo emot(json_encode($response)); } else { echo json_encode($response); } } catch (Exception $e) { die(json_encode(array('error' => $e->getMessage()))); }