Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Send events via external webpage?

If you have a question or need help, this is the place to be.
Post Reply
thug
Plugin Developer
Posts: 64
Joined: Sat Jul 22, 2006 2:37 pm
Location: Australia

Send events via external webpage?

Post by thug »

Hi,

I want to see if I can get a home automation web page up and running, you know, click a button on a web page, from work, and my mediabox responds. :)

I have a server (PC1) and a mediabox (PC2) both inside my firewall.
PC1 has a web server (visible on the internet port 80) but not EG.
PC2 has EG running on it but no web server (or maybe the webserver plugin).

I know how to surf my web server, from work, click a button (on the web page) and PC1 processes the result (via a form using cgi, php or asp etc...)
That works....
Now how do i get the web server (on PC1) to send something to EG on my PC2?

I'm thinking some of my options are:-

a) Get the web server (PC1) to execute a stub proggie (using psexec) that sends events to EG on PC2 (does a stub proggie exist like girder has?).
b) Write a php or asp network event sender to interact with PC2's network event receiver (ouch, has this been done already?).
c) Dump events in a file on PC2's HDD and use directory watcher plugin and python script to process them.
d) Get php (on PC1) to "surf" the webserver plugin on PC2.

Is there another / easier / quicker / non-complex way? :?:

Has anyone done anything similar?

I hope I haven't confused anyone. ;)

Cheers!
User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Post by Bitmonster »

Options c) and d) are bad ideas.

Option a) might work, but there is no stub proggie currently.

Option b) has been done. I use it all day with Apache. My PHP script looks like this:

Code: Select all

<?
function sendMsg($args)
{
    $fp = fsockopen($args["host"],$args["port"], $errno, $errstr, 10);
    
    if (!$fp)
    { //Something didn't work....
        echo "ERROR: $errno - $errstr\n";
        return FALSE;

    } else {

        // First wake up the server, for security reasons it does not
        // respond by it self it needs this string, why this odd word ?
        // well if someone is scanning ports "connect" would be very obvious
        // this one you'd never guess :-)
        
        fputs($fp, "quintessence\n\r");
        
        // The server now returns a cookie, the protocol works like the
        // APOP protocol. The server gives you a cookie you add :<password>
        // calculate the md5 digest out of this and send it back
        // if the digests match you are in.
        // We do this so that noone can listen in on our password exchange
        // much safer then plain text.
        
        $cookie = fgets($fp, 400);		
        
        // Trim all enters and whitespaces off
        $cookie = trim($cookie);
        
        // Combine the token <cookie>:$args[pword]
        $token = $cookie . ":" . $args["password"];
        
        // Calculate the digest
        $digest = md5($token);
        
        // add the enters
        $digest = $digest . "\n";
                
        // Send it to the server		
        fputs($fp, $digest );
        
        // Get the answer
        $res = fgets($fp, 400);
        
        // If the password was correct and you are allowed to connect
        // to the server, you'll get "accept"
        if ( trim($res) != "accept" )
        {
             fclose($fp);
             return FALSE;
        }
        
        if (isset($args['payload']))
        {
            if (is_array($args['payload']))
            {
                foreach($args['payload'] as $key => $value)
                    fputs($fp, "payload " . $value . "\n");
            }else
            {
                if ( $args["payload"] <> "" )
                    fputs($fp, "payload ".$args["payload"]."\n");		
            }		
        }
        // now just pipe those commands to the server
        fputs($fp, $args["eventstring"]."\n");
        
        // tell the server that we are done nicely.
        fputs($fp, "close\n");
        
        fclose($fp);
            
        return TRUE;
    }
}


if (isset($_GET))
{	
    if(isset($_GET['host']))
        $args['host'] = urldecode($_GET['host']);
    else
        $args['host'] = '127.0.0.1';
    $args['port'] = 32333;
    $args['password'] = 'password';
    $args['eventstring'] =  urldecode($_GET['event']);
    foreach($_GET as $key => $value)
        if(strcasecmp(substr($key, 0, 3), 'pld') == 0)
            $args['payload'][] = urldecode($value);
    sendMsg($args);
    if(!isset($_GET['REFERER']))
        $_GET['REFERER'] = "index.html";
    header("Location: http://".$_SERVER['HTTP_HOST']
           . dirname($_SERVER['PHP_SELF'])
           ."/".$_GET["REFERER"]);
}
?>

Could be written better but was only a quick hack that works now for years. Just edit the $args['host'], $args['port'] and $args['password'] to your setup.
You call the script with a URL like:
http://MyHost/MyScript.php?event=MyEven ... =next.html

REFERER is the page that should be displayed after the link.
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!
User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Post by Bitmonster »

And option a) should now also work:
http://www.eventghost.org/forum/viewtopic.php?t=287
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!
Post Reply