Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Copyright (c) 2016, Thomas Jones, TomTec Solutions.
- define(VERSION, "1.1");
- //// BEGIN: CONFIG ////
- $chatlogFileName = "chatlog.txt";
- $debug = true;
- $emoji = true;
- $allowHistoryDelete = true;
- $doJoinMessages = true;
- $doLeaveMessages = true;
- //// END: CONFIG ////
- //// BEGIN: DEBUG ////
- if ($debug) {
- ini_set("display_errors", 1);
- ini_set("display_startup_errors", 1);
- ini_set("html_errors", 1);
- error_reporting(E_ALL);
- //header("Content-Type: text/plain");
- }
- //// END: DEBUG ////
- //// BEGIN: EXECUTION ////
- if (!doesSessionExist()) {
- session_start();
- }
- if (wasRequestPost()) {
- if (getPostVar("message")) {
- $message = getPostVar("message");
- if ($emoji) {
- $message = emojiReplace($message);
- }
- $date = date("h:i:s a");
- $buffer = '<span class="badge">' . $date . '</span> <span class="badge" style="background-color: deepskyblue;">' . $_SESSION["name"] . '</span> <strong>' . $message . '</strong>';
- die(writeToChatLog($buffer, true));
- } elseif (getPostVar("delete")) {
- if ($allowHistoryDelete) {
- die(deleteChatLog());
- }
- } elseif (getPostVar("name")) {
- if ($doJoinMessages) {
- $name = getPostVar("name");
- $buffer = '<span class="badge" style="background-color: purple;">' . $name . ' joined the chat</span>';
- writeToChatLog($buffer, true);
- }
- die($_SESSION["name"] = $name);
- } elseif (getPostVar("delete")) {
- if (doesSessionExist()) {
- die(deleteChatLog());
- }
- } elseif (getPostVar("closed")) {
- if ($doLeaveMessages && isset($_SESSION["name"])) {
- $name = $_SESSION["name"];
- $buffer = '<span class="badge" style="background-color: purple;">' . $name . ' left the chat</span>';
- die(writeToChatLog($buffer, true));
- }
- }
- } else {
- if (isset($_GET["read"])) {
- $log = readBackChatLog();
- if (empty($log)) {
- $brs = "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";
- die($brs . "<h4 style=\"color: blue; text-align: center; -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none;\">This is a new chat, start it off by sending your first message!</h4>");
- } else {
- $formatted = nl2br($log);
- die($formatted);
- }
- } elseif (isset($_GET["clrsession"])) {
- if ($debug) {
- session_unset();
- session_destroy();
- }
- }
- }
- //// END: EXECUTION ////
- //// BEGIN: FUNCTIONS ////
- function getPostVar($var) {
- if (wasRequestPost()) {
- return htmlspecialchars($_POST[$var]);
- } return false;
- }
- function emojiReplace($message) {
- $message = str_replace(array(":)", ":-)"), "😊", $message); // smile
- $message = str_replace(array(":P", ":-P"), "😛", $message); // smile with tongue out
- $message = str_replace(array(":D", ":-D"), "😄", $message); // happy smile
- $message = str_replace(array(":*", ":-*", ";*", ";-*"), "😘", $message); // kiss
- $message = str_replace(array(":'(", ":-'("), "😢", $message); // crying
- $message = str_replace(array(":O", ":-O"), "😲", $message); // shocked
- $message = str_replace(array(":|", ":-|"), "ðŸ˜", $message); // unamused
- $message = str_replace(array(":\\", ":-\\"), "😕", $message); // unsure/worried
- $message = str_replace(array(">:(", ">:-("), "😡", $message); // angry
- $message = str_replace(array(":(", ":-("), "😔", $message); // sad
- $message = str_replace(array(htmlspecialchars("<3"), htmlspecialchars("<#")), "💙", $message); // heart (blue)
- return $message;
- }
- function readBackChatLog() {
- global $chatlogFileName;
- createChatLogFileIfNotExist();
- try {
- $file = fopen($chatlogFileName, "r");
- $log = @fread($file, filesize($chatlogFileName));
- fclose($file);
- return $log;
- } catch (Exception $e) {
- return $e->getMessage();
- }
- }
- function writeToChatLog($message, $doNewLine = false) {
- global $chatlogFileName;
- createChatLogFileIfNotExist();
- if ($doNewLine) {
- $message = ($message . "\n");
- }
- try {
- $file = fopen($chatlogFileName, "a");
- fwrite($file, $message);
- return fclose($file);
- } catch (Exception $e) {
- return $e->getMessage();
- }
- }
- function deleteChatLog() {
- global $chatlogFileName;
- if (doesChatLogExist()) {
- unlink($chatlogFileName);
- }
- }
- function doesChatLogExist() {
- global $chatlogFileName;
- return file_exists($chatlogFileName);
- }
- function createChatLogFileIfNotExist() {
- global $chatlogFileName;
- if (!doesChatLogExist()) {
- touch($chatlogFileName);
- }
- }
- function createChatLogFile() {
- global $chatlogFileName;
- touch($chatlogFileName);
- }
- function wasRequestPost() {
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- return true;
- } return false;
- }
- function doesSessionExist() {
- if (session_id() == "" || !isset($_SESSION)) {
- return false;
- } return true;
- }
- //// END: FUNCTIONS ////
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
- <script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
- <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- <title>TomTec Phobos</title>
- <style>
- div.chatlog {
- width: 100%;
- height: 256px;
- border: 1px solid black;
- padding: 5px;
- overflow-y: scroll;
- white-space: pre-wrap;
- word-wrap: break-word;
- line-height: 0.4;
- -moz-user-select: none;
- -webkit-user-select: none;
- -ms-user-select: none;
- }
- .header img {
- float: left;
- width: 75px;
- height: 75px;
- }
- .header h1 {
- position: relative;
- top: 18px;
- left: 10px;
- }
- .footer {
- padding-top: 20px;
- }
- .noscript-warning {
- background-color: #FF0000;
- color: white;
- text-align: center;
- padding: 10%;
- }
- .centered {
- text-align: center;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header page-header">
- <img src="//tomtecsolutions.com.au/assets/images/tomtecsolutions.png" draggable="false">
- <h1>TomTec Phobos Chat System v<?php echo VERSION ?></h1><br /><br />
- </div>
- <div id="alert-box"></div>
- <noscript>
- <div class="noscript-warning">
- <h2>This chat system requires JavaScript to function.</h2>
- </div>
- </noscript>
- <div class="form-horizontal" id="chatform" style="display: none;">
- <div class="form-group">
- <div class="row">
- <div class="col-md-12">
- <div class="chatlog form-control" id="chatlog-frame">
- </div>
- </div>
- </div>
- </div>
- <div class="form-group">
- <label for="sendmessagebox">Message:</label>
- <input type="text" autofocus class="form-control" id="send_message_box">
- </div>
- <div class="form-group" style="text-align: center;">
- <p>Emojis are <?php if ($emoji) { ?>ON<?php } else { ?>OFF<?php } ?></p>
- <?php if ($allowHistoryDelete) { ?>
- <div style="text-align: left; float: left;">
- <button style="text-align: left;" id="delete_scrollback" data-loading-text="Deleting..." class="btn btn-danger">Delete History</button>
- </div>
- <?php } ?>
- <div style="text-align: right; float: right;">
- <button style="text-align: right;" id="send_message" data-loading-text="Sending..." class="btn btn-primary">Send Message</button>
- </div>
- </div>
- </div>
- </div>
- <div class="modal fade" id="identModal" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- <h3 class="modal-title">Phobos Identification</h3>
- </div>
- <div class="modal-body">
- <div id="alert-box-modal">
- <p>Welcome to Phobos, please provide identification so other chatters know who you are.</p>
- <br />
- </div>
- <div class="form-horizontal">
- <div class="form-group">
- <label class="col-sm-2 control-label" for="name">Name:</label>
- <div class="col-sm-9">
- <input type="text" autofocus class="form-control" id="name" placeholder="John Doe">
- </div>
- </div>
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" id="set_name" class="btn btn-primary">Join Chat</button>
- </div>
- </div>
- </div>
- </div>
- <script>
- $(function() {
- $("#chatform").show();
- function resetFrame() {
- $.get("<?php echo(basename(__FILE__)); ?>?read=true&_=" + new Date().getTime(), // we send the timestamp as a hacky way of stopping the browser from caching the result
- function (data, status) {
- if (status != "success") {
- alert(status);
- } else {
- $("#chatlog-frame").html(data);
- }
- });
- $("#chatlog-frame").scrollTop($("#chatlog-frame")[0].scrollHeight - $("#chatlog-frame")[0].clientHeight);
- }
- var repeater = setInterval(resetFrame, 250);
- <?php if (!isset($_SESSION["name"])) { ?>
- $("#identModal").modal({
- backdrop: "static",
- keyboard: false
- });
- $("#identModal").on("shown.bs.modal", function() {
- $("#name").focus();
- });
- $("#set_name").click(function() {
- var nameBoxValue = $("#name").val();
- if (!nameBoxValue.trim()) {
- var alert_message = '<div class="alert alert-danger alert-dismissable"><strong>Warning:</strong> You can\'t use an empty or space-filled name.</div>';
- $("#alert-box-modal").html(alert_message);
- return;
- } else {
- $("#identModal").modal("hide");
- }
- $.post("<?php echo(basename(__FILE__)); ?>",
- {
- name: nameBoxValue
- },
- function(data, status) {
- if (status != "success") {
- alert(status);
- }
- });
- });
- <?php } ?>
- });
- // make the enter/return key auto-press the send message button
- $("#send_message_box").keypress(function(e) {
- if (e.which == 13) {
- $("#send_message").click();
- }
- });
- $("#delete_scrollback").click(function() {
- $.post("<?php echo(basename(__FILE__)); ?>",
- {
- delete: true
- },
- function(data, status) {
- if (status != "success") {
- alert(status);
- }
- });
- });
- // send the message data to the server and clear the message box
- $("#send_message").click(function() {
- var messageBoxValue = $("#send_message_box").val();
- $("#send_message_box").val("");
- if (!messageBoxValue.trim()) {
- var alert_message = '<div class="alert alert-danger alert-dismissable"><strong>Warning:</strong> You can\'t send an empty or space-filled message.<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
- $("#alert-box").html(alert_message);
- return;
- }
- $.post("<?php echo(basename(__FILE__)); ?>",
- {
- message: messageBoxValue
- },
- function(data, status) {
- if (status != "success") {
- alert(status);
- }
- });
- });
- $(window).on("unload", function() {
- $.post("<?php echo(basename(__FILE__)); ?>",
- {
- closed: "true"
- },
- function(data, status) {
- return;
- }
- );
- });
- $(window).on("beforeunload", function() {
- $.post("<?php echo(basename(__FILE__)); ?>",
- {
- closed: "true"
- },
- function(data, status) {
- return;
- }
- );
- });
- </script>
- <div class="centered footer" style="opacity: 0.6;">
- © 2016 Thomas Jones, <a href="https://tomtecsolutions.com.au" target="_blank">TomTec Solutions</a>.
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement