Example #1
0
function send_to_some_stream_server($type, $size, $raddress, $var, $fd)
{
    global $gbl, $sgbl, $login, $ghtml;
    $exitchar = $sgbl->__var_exit_char;
    $remotechar = $sgbl->__var_remote_char;
    $con = $sgbl->__var_connection_type;
    if ($raddress === "localhost") {
        $con = "tcp";
        $port = $sgbl->__var_local_port;
    } else {
        $con = "ssl";
        $port = $sgbl->__var_remote_port;
    }
    print_time('server');
    print_time('serverstart');
    $rraddress = $raddress;
    if (isLocalhost($raddress)) {
        $rraddress = "127.0.0.1";
    }
    $socket = stream_socket_client("{$con}://{$rraddress}:{$port}");
    //$socket =  fsockopen("$con://$raddress", $port);
    print_time('serverstart', "Fsockopen");
    if ($socket <= 0) {
        if ($raddress === 'localhost' && !WindowsOs() && !$sgbl->isDebug()) {
            //20140131 OA: dont reenable this, Ive just removed.
            //lxshell_background("/usr/sbin/lxrestart", $sgbl->__var_program_name);
            throw new lxException('no_socket_connect_to_server', '', $raddress);
            throw new lxException('restarting_backend', '', $raddress);
        } else {
            throw new lxException('no_socket_connect_to_server', '', $raddress);
        }
    }
    stream_set_timeout($socket, 30000000000);
    //stream_context_set_option($socket, 'ssl', 'allow_self_signed', true);
    //stream_context_set_option($socket, 'ssl', 'verify_peer', false);
    $in = $var;
    fwrite($socket, $in);
    $in = "\n";
    fwrite($socket, $in);
    $in = $exitchar;
    fwrite($socket, $in);
    $in = "\n";
    fwrite($socket, $in);
    $totalout = null;
    $totalsize = 0;
    while (true) {
        $out = fgets($socket, 8092);
        if (!$out) {
            if (!$totalout) {
                dprint("Got Nothing\n");
            }
            break;
        }
        if ($type === 'fileprint' || $type === 'file') {
            // The stream comes with a first and last character appended to it.
            if ($totalsize === 0 && $out[0] === 'f') {
                log_log("servfile", "Got failure from the servfile {$out}");
                break;
            }
            if ($totalsize === 0) {
                $out = substr($out, 1);
            }
            $totalsize += strlen($out);
            if ($totalsize >= $size + 1) {
                $out = substr($out, 0, strlen($out) - 1);
            }
            print_or_write($fd, $out);
            if ($totalsize >= $size + 1) {
                break;
            }
        } else {
            //$out = trim($out);
            $totalout .= $out;
            if (csa($totalout, $exitchar)) {
                break;
            }
        }
    }
    fclose($socket);
    if ($type === 'file' || $type === 'fileprint') {
        return $totalsize;
    }
    if (!$totalout) {
        return null;
    }
    dprint("Got this much:" . strlen($totalout));
    //dprint($totalout);
    $totalout = trim($totalout);
    $size = round(strlen($totalout) / 1024, 4);
    //dprint($totalout);
    //$ee = unserialize(base64_decode($totalout));
    return $totalout;
}
Example #2
0
function do_serve_file($fd, $rem)
{
    $file = $rem->filename;
    $file = basename($file);
    $file = "__path_serverfile/{$file}";
    if (!lxfile_exists($file)) {
        log_log("servfile", "datafile {$file} dosn't exist, exiting");
        print_or_write($fd, "fFile Doesn't {$file} Exist...\n\n\n\n");
        return false;
    }
    $array = lfile_get_unserialize($file);
    lunlink($file);
    $realfile = $array['filename'];
    $pass = $array['password'];
    if ($fd) {
        dprint("Got request for {$file}, realfile: {$realfile}\n");
    }
    log_log("servfile", "Got request for {$file} realfile {$realfile}");
    if (!($pass && $pass === $rem->password)) {
        print_or_write($fd, "fPassword doesn't match\n\n");
        return false;
    }
    if (is_dir($realfile)) {
        // This should neverhappen. The directories are zipped at cp-fileserv and tar_to_filserved then itself.
        $b = basename($realfile);
        lxfile_mkdir("__path_serverfile/tmp/");
        $tfile = tempnam("__path_serverfile/tmp/", "{$b}.tar");
        $list = lscandir_without_dot($realfile);
        lxshell_tar($realfile, $tfile, $list);
        $realfile = $tfile;
    }
    $fpr = lfopen($realfile, "rb");
    if (!$fpr) {
        print_or_write($fd, "fCouldn't open {$realfile}\n\n");
        return false;
    }
    print_or_write($fd, "s");
    while (!feof($fpr)) {
        $written = print_or_write($fd, fread($fpr, 8092));
        if ($written <= 0) {
            break;
        }
    }
    // Just send a newline so that the fgets will break after reading. This has to be removed after the file is read.
    print_or_write($fd, "\n");
    fclose($fpr);
    fileserv_unlink_if_tmp($realfile);
    return true;
}