qwnet.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?
  2. ##############################################################################################################
  3. #
  4. # QWnet
  5. #
  6. # Developer(s): swiNg
  7. #
  8. # This script is running as a daemon on the server, parsing messages from QWnet through SSH.
  9. #
  10. ##############################################################################################################
  11. ##############################################################################################################
  12. # INCLUDES
  13. ##############################################################################################################
  14. require "../includes/database.php";
  15. require "../includes/ssh.php";
  16. ##############################################################################################################
  17. # PROCEDURES
  18. ##############################################################################################################
  19. #*************************************************************************************************************
  20. # FUNCTION: SaveMessage() // Save QWnet message to database.
  21. #*************************************************************************************************************
  22. function SaveMessage($timestamp, $ident, $network, $frequency, $user_id, $nick, $source, $message) {
  23. // open database connection
  24. $qwnu_dbconn = gOpenDB();
  25. // check if message already exist in the database (posted from website)
  26. // in that case update it with the fetched ident
  27. $id = GetMessageID("", $network, $frequency, $nick, $source, $message);
  28. // construct sql query
  29. if ($id == 0) {
  30. $sql = "
  31. INSERT INTO qwnu_qwnet (create_time, ident, network, frequency, user_id, nick, source, message, counter) VALUES (
  32. '".mysql_real_escape_string($timestamp)."',
  33. '".mysql_real_escape_string($ident)."',
  34. '".mysql_real_escape_string($network)."',
  35. '".mysql_real_escape_string($frequency)."',
  36. '".mysql_real_escape_string($user_id)."',
  37. '".mysql_real_escape_string($nick)."',
  38. '".mysql_real_escape_string($source)."',
  39. '".mysql_real_escape_string($message)."',
  40. '0'
  41. )";
  42. }
  43. else {
  44. $sql = "UPDATE qwnu_qwnet SET ident = '".mysql_real_escape_string($ident)."' WHERE id = ".$id;
  45. }
  46. // execute sql query
  47. if (!mysql_query($sql)) {
  48. die(mysql_error());
  49. }
  50. // close database connection
  51. gCloseDB($qwnu_dbconn);
  52. }
  53. #*************************************************************************************************************
  54. # FUNCTION: GetMessageID() // Get ID of already existing message.
  55. #*************************************************************************************************************
  56. function GetMessageID($ident, $network, $frequency, $nick, $source, $message) {
  57. // query sql for previous approved posts
  58. $sql = "
  59. SELECT id
  60. FROM qwnu_qwnet WHERE
  61. ident = '".mysql_real_escape_string($ident)."' AND
  62. network = '".mysql_real_escape_string($network)."' AND
  63. frequency = '".mysql_real_escape_string($frequency)."' AND
  64. nick = '".mysql_real_escape_string($nick)."' AND
  65. source = '".mysql_real_escape_string($source)."' AND
  66. message = '".mysql_real_escape_string($message)."'
  67. ORDER BY id DESC
  68. LIMIT 0, 1";
  69. $recordset = mysql_query($sql);
  70. $row = mysql_fetch_assoc($recordset);
  71. // return result
  72. return (mysql_num_rows($recordset) > 0 ? $row["id"] : 0);
  73. }
  74. #*************************************************************************************************************
  75. # FUNCTION: MainLoop() // Parses the stream and saves messages.
  76. #*************************************************************************************************************
  77. function MainLoop(&$ssh) {
  78. // set variables
  79. $timeout = 300;
  80. $time_last_activity = time();
  81. // loop
  82. while (true) {
  83. $data = "";
  84. // read stream
  85. if ($ssh->authed) {
  86. // fetch data
  87. while ($buf = $ssh->ReadStream()) {
  88. $data .= $buf;
  89. }
  90. // parse data
  91. if ($data) {
  92. $time_last_activity = time();
  93. $data_array = explode("\r\n", $data);
  94. // echo any data to console
  95. foreach ($data_array as $data) {
  96. if ($data != "") {
  97. // get timestamp and action
  98. $array = explode(" ", substr($data, 27));
  99. $timestamp = time(); #strtotime(substr($data, 0, 19)); // use local timestamp time()
  100. $action = $array[0];
  101. // get action and ident
  102. if ($action == "BC") {
  103. // get ident
  104. $ident = (array_key_exists(1, $array) ? $array[1] : "");
  105. // get network, frequency and source
  106. $i = 0;
  107. $array = explode(",", substr($data, strpos($data, $ident) + strlen($ident) + 1));
  108. $network = $array[0];
  109. $frequency = (array_key_exists(1, $array) ? $array[1] : "");
  110. $source = (array_key_exists(2, $array) ? $array[2] : "");
  111. $message = "";
  112. // get nick and message
  113. $array = explode("','", substr($data, strpos($data, $source) + strlen($source) + 2));
  114. foreach($array as $item) {
  115. if ($i == 0) {
  116. $nick = $item;
  117. }
  118. else {
  119. $message .= $item;
  120. }
  121. $i++;
  122. }
  123. if (strlen($message) > 0) {
  124. $message = substr($message, 0, strrpos($message, "'"));
  125. }
  126. // save message to database
  127. if ($network == "QWalt" && $frequency == "-qw-") {
  128. SaveMessage($timestamp, $ident, $network, $frequency, 0, $nick, $source, $message);
  129. }
  130. }
  131. // print anything but ping messages
  132. if (substr($data, 27) != "PING alive?") {
  133. echo $data."\n";
  134. }
  135. }
  136. }
  137. }
  138. }
  139. // break on exceeded time limit or sleep 2 seconds before itteration
  140. if ($timeout >= 0 && time() - $time_last_activity >= $timeout) {
  141. echo "Connection timeout. Reconnecting...\n";
  142. $ssh->Connect(QWNET_HOST, QWNET_PORT, QWNET_FP, QWNET_USR, "", QWNET_KEY_PATH_PUB, QWNET_KEY_PATH_PRIV);
  143. echo $ssh->Shell("vt102", 1);
  144. $time_last_activity = time();
  145. }
  146. else {
  147. sleep(3);
  148. }
  149. }
  150. }
  151. ##############################################################################################################
  152. # INLINE CODE
  153. ##############################################################################################################
  154. // disable script timeout
  155. set_time_limit(0);
  156. // ssh settings
  157. define("QWNET_HOST", "b4r.org");
  158. define("QWNET_PORT", "22");
  159. define("QWNET_FP", "90C300C5853F3679C87240DAAF5F32A1");
  160. define("QWNET_USR", "stomp");
  161. define("QWNET_KEY_PATH_PUB", "");
  162. define("QWNET_KEY_PATH_PRIV", "");
  163. // open ssh connection and shell, then jump into main loop
  164. $ssh = new SSH;
  165. try {
  166. echo "Connecting to QWnet central...\n";
  167. $ssh->Connect(QWNET_HOST, QWNET_PORT, QWNET_FP, QWNET_USR, "", QWNET_KEY_PATH_PUB, QWNET_KEY_PATH_PRIV);
  168. $ssh->cmd_exit = "PART";
  169. echo $ssh->Shell("vt102", 1);
  170. MainLoop($ssh);
  171. }
  172. catch (Exception $e) {
  173. die("SSH error: ".$e->getMessage());
  174. }