<?php
/*
	s_cli.php - client for stream manager
	Copyright 2006 by Stephen D. Teter
	All rights reserved.
*/

// error_reporting (E_ALL);

/* The following definitions must not contain $explode_char!! */
$maintag = "FifDasuk0";
$stopcmd = "GecyecWen6";

/* Edit these to suit */
$addr = '127.0.0.1';
$port = 29406;
$explode_char = chr(1);
$space_char = chr(2);
$mainkey = "vegTudPewgCxcMajFis:ek^bDigh#UkF:ved8v9";
$ssh_loc="/usr/bin";

/* Do not change anything below this line unless you know what you are doing */

function ec( $buf ) {
    global $mainkey;
    $c = $buf;
    $td = mcrypt_module_open ('des', '', 'ecb', '');
    $key = substr ($mainkey, 0, mcrypt_enc_get_key_size ($td));
    $iv_size = mcrypt_enc_get_iv_size ($td);
    $iv = mcrypt_create_iv ($iv_size, MCRYPT_RAND);
    if (mcrypt_generic_init ($td, $key, $iv) != -1) {
        $c = mcrypt_generic ($td, $buf);
        mcrypt_generic_deinit ($td);
        mcrypt_module_close ($td);
    }
    return( $c );
}

function sendCmd($cmd) {
    global $maintag, $port, $addr, $explode_char;
    $out = "Unknown error";
    $buf = $maintag.$explode_char.$cmd;
    $ebuf = ec($buf);
    $socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket < 0) {
        $out = "socket_create() failed: reason: " . socket_strerror ($socket) . "\n";
    } else {
        $result = socket_connect ($socket, $addr, $port);
        if ($result < 0) {
            $out = "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
        } else {
//            echo "Writing [$socket] with [$ebuf]\n";
            socket_write ($socket, $ebuf);
            $out = socket_read ($socket, 100000);
            socket_close ($socket);
        }
    }
    return( $out );
}

function send_ssh($address, $cmd) {
    global $space_char, $explode_char;
    $str = $address.$space_char.$cmd;
    $buf = "send_ssh".$explode_char.$str;
	$res = sendCmd($buf);
    return( $res );
}

function do_upload($newdir, $userfile, $userfile_name) {
    global $explode_char, $space_char;
	$buf = "do_upload".$explode_char.$newdir.$space_char.$userfile.$space_char.$userfile_name;
	$res = sendCmd($buf);
    return( $res );
}

?>
