#!/usr/bin/php -q
<?php

/* 
 * 
 * RTMPHP - by Espen Holm Nilsen (holm@blackedge.org) (www.gho.no / www.arpa.no)
 * You can use and modify this code as long as the above reference to me still exists.
 * 
 */

function communicate ($sock) {
    
$data socket_read($sock1537PHP_BINARY_READ);
    
parsePacket($data);
}

function 
createPacket ($intType) {

    switch (
$intType) {
        case 
'startHandshake':
            
$strHandshake generateHandshake(getUptimeMs());
            break;
    }

}

function 
generateHandshake ($uptime_ms) {
    
$handshake NULL;

    
$uptime_ms getUptimeMs();
    
$handshake pack('N'$uptime_ms);
    
$handshake .= "\x00\x00\x00\x00";
    
$magic $uptime_ms 256;

    
$bytes 8;
    while (
$bytes 1536) {

        
$magic = (1211121 $magic 1) % 256;

        if (
strlen($handshake) != 1535) {
            
$handshake .= sprintf("%c"$magic) . "\x00";
        } else {
            
$handshake .= $magic;
        }
        
$bytes += 2;
    }

    
$handshake "\x03" $handshake;
    return 
$handshake;
}

function 
parsePacket ($packet) {
    
$data NULL;
    global 
$handshake;
    global 
$socket;

    if (
strlen($packet)%2)
        
$packet .= "\x00";

    
$sum strlen($packet);

    for (
$i 0$i <= $sum$i += 2) {
#        print $packet[$i] . "\n";
    
}

    print 
"Received packet was " $sum "bytes long.\n";

    if (
$packet[0] == "\x03" && $packet[5] == "\x00" && $packet[6] == "\x00" && $packet[7] == "\x00") {
        
// This is most likely a handshake packet
        
print "... that was a handshake packet from the server.\n";

        print 
"Trying to just ship it back stripped of the 0x03 byte\n";
        
socket_write($socketsubstr($packet1strlen($packet) - 1));
    }

    if (
$sum == 0)
        die(
"Connection most likely died.\n");

    
getHeader($packet);

}

function 
getHeader ($packet) {
#    print "Parseheader: " . $packet[0] . "\n";

    
if ($packet[0] > "\x00" && $packet[0] < "\0x0f") {
        print 
"Nice data\n";
    }
}

$host $_SERVER['argv'][1];
$port $_SERVER['argv'][2];

if (empty(
$host) || empty($port))
    die(
"./script host port\n");

$socket gimmeSocket($host$port);


$arrReadSockets = array($socket);

$handshake generateHandshake(0);
socket_write($socket$handshake);

while (
1) {
    
$read $arrReadSockets;
    if (
socket_select($read$write NULL$except NULL0) > 0) {

        if (!
$socket) {
            die(
"ERROR: We got disconnected...\n");
        }

        die( 
"OK: We received data, wowza is responding!\n");
        
communicate($socket);
    }

    
    
// Det er sunt aa sove noen ganger
    
usleep(8000);
}

function 
getUptimeMs () {
    
$fd fopen("/proc/uptime"'r');

    if (
$fd) {
        
$strUptime fgets($fd1024);
    } else {
        return 
FALSE;
    }

    
$arrUptime explode(" "$strUptime);
    
$arrUptime[0] = str_replace("\n"""$arrUptime[0]);

    
$arrTmpUptime explode("."$arrUptime[0]);

    
$strUptime $arrTmpUptime[0] . $arrTmpUptime[1] * 100;

    if (
$fd) {
        
fclose($fd);
    }

    
// FIXME, THIS GOTTA BE BETTER!
    
return substr($strUptime010);

}

function 
byteToHex ($char) {
    return 
dechex(ord($char));
}

function 
hexbin ($strHex) {
    
$bin decbin(hexdec($strHex));
    
$count strlen($bin);

    for (
$i 0$i < ($count); $i++) {
        
$bin '0' $bin;
    }

    unset (
$i);
    return 
$bin;
}

function 
gimmeSocket ($strServer$intPort$strBind NULL) {
    if ((
$fdSocket socket_create(AF_INETSOCK_STREAM0)) == FALSE)
        die(
"Unable to create socket.\n");

    if (
$strBind != NULL) {
        if ((@
socket_bind($fdSocket$strBind)) == FALSE)
            die(
"Unable to bind to $strBind.\n");
    }

    if ((@
socket_connect($fdSocket$strServer$intPort)) == FALSE)
        die(
"Could not connect\n");

    return 
$fdSocket;

}

?>