PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/inc_chat.php

https://gitlab.com/robinphillips/qags-web
PHP | 92 lines | 53 code | 10 blank | 29 comment | 6 complexity | 06d3d8b4dbeb1ab8a5f2a39ae5f1f156 MD5 | raw file
  1. <?php
  2. /*
  3. QAGS Web. Copyright (c) Robin Phillips
  4. QAGS Second Edition is copyright (c) Steve Johnson and Leighton Connor
  5. */
  6. require ("inc_config.php");
  7. function prepareMessage ($msg) {
  8. // Remove paragraph tags from log string
  9. $msg = str_ireplace(array("<p>","</p>"), "", $msg);
  10. // Turn <BR> tags into newlines
  11. $msg = str_ireplace(array('<br>', '<br />'), "\n", $msg);
  12. // Replace double newlines with single ones
  13. do
  14. $msg = str_ireplace("\n\n", "\n", $msg, $c);
  15. while ($c > 0);
  16. // Remove any remaining HTML tags and convert entities into characters
  17. $msg = html_entity_decode(strip_tags($msg), ENT_QUOTES);
  18. // Return prepared message
  19. return $msg;
  20. }
  21. function sendIrcMessage ($msg) {
  22. // Prepare the message
  23. $msg = prepareMessage($msg);
  24. // IRC can't handle newlines, so $irc_msgs is an array of lines to be sent to IRC
  25. // Convert newlines into elements of an array
  26. $ma = explode ("\n", $msg);
  27. // Make sure that no line is more than 200 characters long
  28. $irc_msgs = array ();
  29. foreach ($ma as $s)
  30. if (strlen($s) > 200) {
  31. $ss = str_split ($s, 200);
  32. foreach ($ss as $sss)
  33. $irc_msgs[] = $sss;
  34. }
  35. else
  36. $irc_msgs[] = $s;
  37. $irc_server = array();
  38. //Open the socket connection to the IRC server
  39. $irc_server['SOCKET'] = @fsockopen(IRC_HOST, IRC_PORT, $errno, $errstr, 2);
  40. if($irc_server['SOCKET']) {
  41. // Login. Note that spaces in IRC_Nick are replaced with underscores
  42. sendIrcCommand('PASS ' . IRC_PASS . "\n\r", $irc_server);
  43. sendIrcCommand('NICK ' . str_replace(' ', '_', IRC_NICK) . "\n\r", $irc_server);
  44. // Must have four parameters
  45. sendIrcCommand('USER ' . str_replace(' ', '_', IRC_NICK) . ' QAGS WEB IRC' . "\n\r", $irc_server);
  46. while(!feof($irc_server['SOCKET'])) {
  47. $irc_server['READ_BUFFER'] = fgets($irc_server['SOCKET'], 1024); //get a line of data from the server
  48. /*
  49. IRC Sends a "PING" command to the client which must be anwsered with a "PONG"
  50. Or the client gets Disconnected
  51. Now lets check to see if we have joined the server
  52. 422 is the message number of the MOTD for the server (The last thing displayed after a successful connection)
  53. */
  54. if(strpos($irc_server['READ_BUFFER'], "422")) {
  55. //If we have joined the server
  56. sendIrcCommand('JOIN ' . IRC_CHAN . "\n\r", $irc_server); //Join the chanel
  57. }
  58. if(substr($irc_server['READ_BUFFER'], 0, 6) == "PING :") {
  59. //If the server has sent the ping command
  60. sendIrcCommand("PONG :".substr($irc_server['READ_BUFFER'], 6)."\n\r", $irc_server); //Reply with pong
  61. }
  62. sendIrcCommand('JOIN ' . IRC_CHAN . "\n\r", $irc_server); //Join the chanel
  63. // Send messages
  64. foreach ($irc_msgs as $msg)
  65. sendIrcCommand('PRIVMSG ' . IRC_CHAN . " :$msg\n\r", $irc_server);
  66. flush(); //This flushes the output buffer
  67. // Disconnect
  68. sendIrcCommand ("QUIT\n\r", $irc_server);
  69. }
  70. }
  71. }
  72. function sendIrcCommand ($cmd, $irc_server) {
  73. @fwrite($irc_server['SOCKET'], $cmd, strlen($cmd));
  74. }
  75. function sendXmppMessage ($msg) {
  76. // Prepare the message
  77. $msg = prepareMessage($msg);
  78. // Escape message for use as a shell argument
  79. $arg = escapeshellarg($msg);
  80. // Run sendxmpp
  81. exec ("echo $arg | ".XMPP_EXE." --jserver '".XMPP_SERVER."' --username '".XMPP_USER."' --password '".XMPP_PASSWORD."' --tls --resource '".XMPP_RESOURCE."' --chatroom '".XMPP_CHATROOM."'");
  82. }