123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?
- ##############################################################################################################
- #
- # QWnet
- #
- # Developer(s): swiNg
- #
- # This script is running as a daemon on the server, parsing messages from QWnet through SSH.
- #
- ##############################################################################################################
- ##############################################################################################################
- # INCLUDES
- ##############################################################################################################
- require "../includes/database.php";
- require "../includes/ssh.php";
- ##############################################################################################################
- # PROCEDURES
- ##############################################################################################################
- #*************************************************************************************************************
- # FUNCTION: SaveMessage() // Save QWnet message to database.
- #*************************************************************************************************************
- function SaveMessage($timestamp, $ident, $network, $frequency, $user_id, $nick, $source, $message) {
- // open database connection
- $qwnu_dbconn = gOpenDB();
- // check if message already exist in the database (posted from website)
- // in that case update it with the fetched ident
- $id = GetMessageID("", $network, $frequency, $nick, $source, $message);
- // construct sql query
- if ($id == 0) {
- $sql = "
- INSERT INTO qwnu_qwnet (create_time, ident, network, frequency, user_id, nick, source, message, counter) VALUES (
- '".mysql_real_escape_string($timestamp)."',
- '".mysql_real_escape_string($ident)."',
- '".mysql_real_escape_string($network)."',
- '".mysql_real_escape_string($frequency)."',
- '".mysql_real_escape_string($user_id)."',
- '".mysql_real_escape_string($nick)."',
- '".mysql_real_escape_string($source)."',
- '".mysql_real_escape_string($message)."',
- '0'
- )";
- }
- else {
- $sql = "UPDATE qwnu_qwnet SET ident = '".mysql_real_escape_string($ident)."' WHERE id = ".$id;
- }
- // execute sql query
- if (!mysql_query($sql)) {
- die(mysql_error());
- }
- // close database connection
- gCloseDB($qwnu_dbconn);
- }
- #*************************************************************************************************************
- # FUNCTION: GetMessageID() // Get ID of already existing message.
- #*************************************************************************************************************
- function GetMessageID($ident, $network, $frequency, $nick, $source, $message) {
- // query sql for previous approved posts
- $sql = "
- SELECT id
- FROM qwnu_qwnet WHERE
- ident = '".mysql_real_escape_string($ident)."' AND
- network = '".mysql_real_escape_string($network)."' AND
- frequency = '".mysql_real_escape_string($frequency)."' AND
- nick = '".mysql_real_escape_string($nick)."' AND
- source = '".mysql_real_escape_string($source)."' AND
- message = '".mysql_real_escape_string($message)."'
- ORDER BY id DESC
- LIMIT 0, 1";
- $recordset = mysql_query($sql);
- $row = mysql_fetch_assoc($recordset);
- // return result
- return (mysql_num_rows($recordset) > 0 ? $row["id"] : 0);
- }
- #*************************************************************************************************************
- # FUNCTION: MainLoop() // Parses the stream and saves messages.
- #*************************************************************************************************************
- function MainLoop(&$ssh) {
- // set variables
- $timeout = 300;
- $time_last_activity = time();
- // loop
- while (true) {
- $data = "";
- // read stream
- if ($ssh->authed) {
- // fetch data
- while ($buf = $ssh->ReadStream()) {
- $data .= $buf;
- }
- // parse data
- if ($data) {
- $time_last_activity = time();
- $data_array = explode("\r\n", $data);
- // echo any data to console
- foreach ($data_array as $data) {
- if ($data != "") {
- // get timestamp and action
- $array = explode(" ", substr($data, 27));
- $timestamp = time(); #strtotime(substr($data, 0, 19)); // use local timestamp time()
- $action = $array[0];
- // get action and ident
- if ($action == "BC") {
- // get ident
- $ident = (array_key_exists(1, $array) ? $array[1] : "");
- // get network, frequency and source
- $i = 0;
- $array = explode(",", substr($data, strpos($data, $ident) + strlen($ident) + 1));
- $network = $array[0];
- $frequency = (array_key_exists(1, $array) ? $array[1] : "");
- $source = (array_key_exists(2, $array) ? $array[2] : "");
- $message = "";
- // get nick and message
- $array = explode("','", substr($data, strpos($data, $source) + strlen($source) + 2));
- foreach($array as $item) {
- if ($i == 0) {
- $nick = $item;
- }
- else {
- $message .= $item;
- }
- $i++;
- }
- if (strlen($message) > 0) {
- $message = substr($message, 0, strrpos($message, "'"));
- }
- // save message to database
- if ($network == "QWalt" && $frequency == "-qw-") {
- SaveMessage($timestamp, $ident, $network, $frequency, 0, $nick, $source, $message);
- }
- }
- // print anything but ping messages
- if (substr($data, 27) != "PING alive?") {
- echo $data."\n";
- }
- }
- }
- }
- }
- // break on exceeded time limit or sleep 2 seconds before itteration
- if ($timeout >= 0 && time() - $time_last_activity >= $timeout) {
- echo "Connection timeout. Reconnecting...\n";
- $ssh->Connect(QWNET_HOST, QWNET_PORT, QWNET_FP, QWNET_USR, "", QWNET_KEY_PATH_PUB, QWNET_KEY_PATH_PRIV);
- echo $ssh->Shell("vt102", 1);
- $time_last_activity = time();
- }
- else {
- sleep(3);
- }
- }
- }
- ##############################################################################################################
- # INLINE CODE
- ##############################################################################################################
- // disable script timeout
- set_time_limit(0);
- // ssh settings
- define("QWNET_HOST", "b4r.org");
- define("QWNET_PORT", "22");
- define("QWNET_FP", "90C300C5853F3679C87240DAAF5F32A1");
- define("QWNET_USR", "stomp");
- define("QWNET_KEY_PATH_PUB", "");
- define("QWNET_KEY_PATH_PRIV", "");
- // open ssh connection and shell, then jump into main loop
- $ssh = new SSH;
- try {
- echo "Connecting to QWnet central...\n";
- $ssh->Connect(QWNET_HOST, QWNET_PORT, QWNET_FP, QWNET_USR, "", QWNET_KEY_PATH_PUB, QWNET_KEY_PATH_PRIV);
- $ssh->cmd_exit = "PART";
- echo $ssh->Shell("vt102", 1);
- MainLoop($ssh);
- }
- catch (Exception $e) {
- die("SSH error: ".$e->getMessage());
- }
|