Advertisement
tjone270

Chat system

Dec 12th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.52 KB | None | 0 0
  1. <?php
  2.   // Copyright (c) 2016, Thomas Jones, TomTec Solutions.
  3.   define(VERSION, "1.1");
  4.  
  5.   //// BEGIN: CONFIG ////
  6.   $chatlogFileName = "chatlog.txt";
  7.   $debug = true;
  8.   $emoji = true;
  9.   $allowHistoryDelete = true;
  10.   $doJoinMessages = true;
  11.   $doLeaveMessages = true;
  12.   //// END: CONFIG ////
  13.  
  14.   //// BEGIN: DEBUG ////
  15.   if ($debug) {
  16.     ini_set("display_errors", 1);
  17.     ini_set("display_startup_errors", 1);
  18.     ini_set("html_errors", 1);
  19.     error_reporting(E_ALL);
  20.     //header("Content-Type: text/plain");
  21.   }
  22.   //// END: DEBUG ////
  23.  
  24.   //// BEGIN: EXECUTION ////
  25.   if (!doesSessionExist()) {
  26.     session_start();
  27.   }
  28.   if (wasRequestPost()) {
  29.     if (getPostVar("message")) {
  30.       $message = getPostVar("message");
  31.       if ($emoji) {
  32.         $message = emojiReplace($message);
  33.       }
  34.       $date = date("h:i:s a");
  35.       $buffer = '<span class="badge">' . $date . '</span> <span class="badge" style="background-color: deepskyblue;">' . $_SESSION["name"] . '</span> <strong>' . $message . '</strong>';
  36.       die(writeToChatLog($buffer, true));
  37.     } elseif (getPostVar("delete")) {
  38.       if ($allowHistoryDelete) {
  39.         die(deleteChatLog());
  40.       }
  41.     } elseif (getPostVar("name")) {
  42.       if ($doJoinMessages) {
  43.         $name = getPostVar("name");
  44.         $buffer = '<span class="badge" style="background-color: purple;">' . $name  . ' joined the chat</span>';
  45.         writeToChatLog($buffer, true);
  46.       }
  47.       die($_SESSION["name"] = $name);
  48.     } elseif (getPostVar("delete")) {
  49.       if (doesSessionExist()) {
  50.         die(deleteChatLog());
  51.       }
  52.     } elseif (getPostVar("closed")) {
  53.       if ($doLeaveMessages && isset($_SESSION["name"])) {
  54.         $name = $_SESSION["name"];
  55.         $buffer = '<span class="badge" style="background-color: purple;">' . $name  . ' left the chat</span>';
  56.         die(writeToChatLog($buffer, true));
  57.       }
  58.     }
  59.   } else {
  60.     if (isset($_GET["read"])) {
  61.       $log = readBackChatLog();
  62.       if (empty($log)) {
  63.         $brs = "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";
  64.         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>");
  65.       } else {
  66.         $formatted = nl2br($log);
  67.         die($formatted);
  68.       }
  69.     } elseif (isset($_GET["clrsession"])) {
  70.       if ($debug) {
  71.         session_unset();
  72.         session_destroy();
  73.       }
  74.     }
  75.   }
  76.   //// END: EXECUTION ////
  77.  
  78.   //// BEGIN: FUNCTIONS ////
  79.   function getPostVar($var) {
  80.     if (wasRequestPost()) {
  81.       return htmlspecialchars($_POST[$var]);
  82.     } return false;
  83.   }
  84.  
  85.   function emojiReplace($message) {
  86.     $message = str_replace(array(":)", ":-)"), "😊",                                                  $message);     // smile
  87.     $message = str_replace(array(":P", ":-P"), "😛",                                                  $message);     // smile with tongue out
  88.     $message = str_replace(array(":D", ":-D"), "😄",                                                  $message);     // happy smile
  89.     $message = str_replace(array(":*", ":-*", ";*", ";-*"), "😘",                                     $message);     // kiss
  90.     $message = str_replace(array(":'(", ":-'("), "😢",                                                $message);     // crying
  91.     $message = str_replace(array(":O", ":-O"), "😲",                                                  $message);     // shocked
  92.     $message = str_replace(array(":|", ":-|"), "😐",                                                  $message);     // unamused
  93.     $message = str_replace(array(":\\", ":-\\"), "😕",                                                $message);     // unsure/worried
  94.     $message = str_replace(array(">:(", ">:-("), "😡",                                                $message);     // angry
  95.     $message = str_replace(array(":(", ":-("), "😔",                                                  $message);     // sad
  96.     $message = str_replace(array(htmlspecialchars("<3"), htmlspecialchars("<#")), "💙",               $message);     // heart (blue)
  97.     return $message;
  98.   }
  99.  
  100.   function readBackChatLog() {
  101.     global $chatlogFileName;
  102.     createChatLogFileIfNotExist();
  103.     try {
  104.       $file = fopen($chatlogFileName, "r");
  105.       $log = @fread($file, filesize($chatlogFileName));
  106.       fclose($file);
  107.       return $log;
  108.     } catch (Exception $e) {
  109.       return $e->getMessage();
  110.     }
  111.   }
  112.  
  113.   function writeToChatLog($message, $doNewLine = false) {
  114.     global $chatlogFileName;
  115.     createChatLogFileIfNotExist();
  116.     if ($doNewLine) {
  117.       $message = ($message . "\n");
  118.     }
  119.     try {
  120.       $file = fopen($chatlogFileName, "a");
  121.       fwrite($file, $message);
  122.       return fclose($file);
  123.     } catch (Exception $e) {
  124.       return $e->getMessage();
  125.     }
  126.   }
  127.  
  128.   function deleteChatLog() {
  129.     global $chatlogFileName;
  130.     if (doesChatLogExist()) {
  131.       unlink($chatlogFileName);
  132.     }
  133.   }
  134.  
  135.   function doesChatLogExist() {
  136.     global $chatlogFileName;
  137.     return file_exists($chatlogFileName);
  138.   }
  139.  
  140.   function createChatLogFileIfNotExist() {
  141.     global $chatlogFileName;
  142.     if (!doesChatLogExist()) {
  143.       touch($chatlogFileName);
  144.     }
  145.   }
  146.  
  147.   function createChatLogFile() {
  148.     global $chatlogFileName;
  149.     touch($chatlogFileName);
  150.   }
  151.  
  152.   function wasRequestPost() {
  153.     if ($_SERVER["REQUEST_METHOD"] == "POST") {
  154.       return true;
  155.     } return false;
  156.   }
  157.  
  158.   function doesSessionExist() {
  159.     if (session_id() == "" || !isset($_SESSION)) {
  160.       return false;
  161.     } return true;
  162.   }
  163.   //// END: FUNCTIONS ////
  164. ?>
  165. <!DOCTYPE html>
  166. <html lang="en">
  167.   <head>
  168.     <meta name="viewport" content="width=device-width, initial-scale=1">
  169.     <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  170.     <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
  171.     <script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
  172.     <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  173.     <title>TomTec Phobos</title>
  174.     <style>
  175.       div.chatlog {
  176.         width: 100%;
  177.         height: 256px;
  178.         border: 1px solid black;
  179.         padding: 5px;
  180.         overflow-y: scroll;
  181.         white-space: pre-wrap;
  182.         word-wrap: break-word;
  183.         line-height: 0.4;
  184.         -moz-user-select: none;
  185.         -webkit-user-select: none;
  186.         -ms-user-select: none;
  187.       }
  188.  
  189.       .header img {
  190.         float: left;
  191.         width: 75px;
  192.         height: 75px;
  193.       }
  194.  
  195.       .header h1 {
  196.         position: relative;
  197.         top: 18px;
  198.         left: 10px;
  199.       }
  200.  
  201.       .footer {
  202.         padding-top: 20px;
  203.       }
  204.  
  205.       .noscript-warning {
  206.         background-color: #FF0000;
  207.         color: white;
  208.         text-align: center;
  209.         padding: 10%;
  210.       }
  211.  
  212.       .centered {
  213.         text-align: center;
  214.       }
  215.     </style>
  216.   </head>
  217.   <body>
  218.     <div class="container">
  219.       <div class="header page-header">
  220.         <img src="//tomtecsolutions.com.au/assets/images/tomtecsolutions.png" draggable="false">
  221.         <h1>TomTec Phobos Chat System v<?php echo VERSION ?></h1><br /><br />
  222.       </div>
  223.       <div id="alert-box"></div>
  224.       <noscript>
  225.         <div class="noscript-warning">
  226.           <h2>This chat system requires JavaScript to function.</h2>
  227.         </div>
  228.       </noscript>
  229.       <div class="form-horizontal" id="chatform" style="display: none;">
  230.         <div class="form-group">
  231.           <div class="row">
  232.             <div class="col-md-12">
  233.               <div class="chatlog form-control" id="chatlog-frame">
  234.               </div>
  235.             </div>
  236.           </div>
  237.         </div>
  238.         <div class="form-group">
  239.           <label for="sendmessagebox">Message:</label>
  240.           <input type="text" autofocus class="form-control" id="send_message_box">
  241.         </div>
  242.         <div class="form-group" style="text-align: center;">
  243.           <p>Emojis are <?php if ($emoji) { ?>ON<?php } else { ?>OFF<?php } ?></p>
  244.           <?php if ($allowHistoryDelete) { ?>
  245.           <div style="text-align: left; float: left;">
  246.             <button style="text-align: left;" id="delete_scrollback" data-loading-text="Deleting..." class="btn btn-danger">Delete History</button>
  247.           </div>
  248.           <?php } ?>
  249.           <div style="text-align: right; float: right;">
  250.             <button style="text-align: right;" id="send_message" data-loading-text="Sending..." class="btn btn-primary">Send Message</button>
  251.           </div>
  252.         </div>
  253.       </div>
  254.     </div>
  255.     <div class="modal fade" id="identModal" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static">
  256.       <div class="modal-dialog" role="document">
  257.         <div class="modal-content">
  258.           <div class="modal-header">
  259.             <h3 class="modal-title">Phobos Identification</h3>
  260.           </div>
  261.           <div class="modal-body">
  262.             <div id="alert-box-modal">
  263.               <p>Welcome to Phobos, please provide identification so other chatters know who you are.</p>
  264.               <br />
  265.             </div>
  266.             <div class="form-horizontal">
  267.               <div class="form-group">
  268.                 <label class="col-sm-2 control-label" for="name">Name:</label>
  269.                 <div class="col-sm-9">
  270.                   <input type="text" autofocus class="form-control" id="name" placeholder="John Doe">
  271.                 </div>
  272.               </div>
  273.             </div>
  274.           </div>
  275.           <div class="modal-footer">
  276.             <button type="button" id="set_name" class="btn btn-primary">Join Chat</button>
  277.           </div>
  278.         </div>
  279.       </div>
  280.     </div>
  281.     <script>
  282.       $(function() {
  283.         $("#chatform").show();
  284.         function resetFrame() {
  285.           $.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
  286.           function (data, status) {
  287.             if (status != "success") {
  288.               alert(status);
  289.             } else {
  290.               $("#chatlog-frame").html(data);
  291.             }
  292.           });
  293.           $("#chatlog-frame").scrollTop($("#chatlog-frame")[0].scrollHeight - $("#chatlog-frame")[0].clientHeight);
  294.         }
  295.         var repeater = setInterval(resetFrame, 250);
  296.  
  297.         <?php if (!isset($_SESSION["name"])) { ?>
  298.           $("#identModal").modal({
  299.             backdrop: "static",
  300.             keyboard: false
  301.           });
  302.           $("#identModal").on("shown.bs.modal", function() {
  303.             $("#name").focus();
  304.           });
  305.           $("#set_name").click(function() {
  306.             var nameBoxValue = $("#name").val();
  307.             if (!nameBoxValue.trim()) {
  308.               var alert_message = '<div class="alert alert-danger alert-dismissable"><strong>Warning:</strong> You can\'t use an empty or space-filled name.</div>';
  309.               $("#alert-box-modal").html(alert_message);
  310.               return;
  311.             } else {
  312.               $("#identModal").modal("hide");
  313.             }
  314.             $.post("<?php echo(basename(__FILE__)); ?>",
  315.             {
  316.               name: nameBoxValue
  317.             },
  318.             function(data, status) {
  319.               if (status != "success") {
  320.                 alert(status);
  321.               }
  322.             });
  323.           });
  324.         <?php } ?>
  325.       });
  326.       // make the enter/return key auto-press the send message button
  327.       $("#send_message_box").keypress(function(e) {
  328.         if (e.which == 13) {
  329.           $("#send_message").click();
  330.         }
  331.       });
  332.  
  333.       $("#delete_scrollback").click(function() {
  334.         $.post("<?php echo(basename(__FILE__)); ?>",
  335.         {
  336.           delete: true
  337.         },
  338.         function(data, status) {
  339.           if (status != "success") {
  340.             alert(status);
  341.           }
  342.         });
  343.       });
  344.       // send the message data to the server and clear the message box
  345.       $("#send_message").click(function() {
  346.         var messageBoxValue = $("#send_message_box").val();
  347.         $("#send_message_box").val("");
  348.         if (!messageBoxValue.trim()) {
  349.           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">&times;</button></div>';
  350.           $("#alert-box").html(alert_message);
  351.           return;
  352.         }
  353.         $.post("<?php echo(basename(__FILE__)); ?>",
  354.         {
  355.           message: messageBoxValue
  356.         },
  357.         function(data, status) {
  358.           if (status != "success") {
  359.             alert(status);
  360.           }
  361.         });
  362.       });
  363.  
  364.       $(window).on("unload", function() {
  365.         $.post("<?php echo(basename(__FILE__)); ?>",
  366.           {
  367.             closed: "true"
  368.           },
  369.           function(data, status) {
  370.             return;
  371.           }
  372.         );
  373.       });
  374.  
  375.       $(window).on("beforeunload", function() {
  376.         $.post("<?php echo(basename(__FILE__)); ?>",
  377.           {
  378.             closed: "true"
  379.           },
  380.           function(data, status) {
  381.             return;
  382.           }
  383.         );
  384.       });
  385.     </script>
  386.     <div class="centered footer" style="opacity: 0.6;">
  387.       &copy; 2016 Thomas Jones, <a href="https://tomtecsolutions.com.au" target="_blank">TomTec Solutions</a>.
  388.     </div>
  389.   </body>
  390. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement