<?
/*
#############################################################################

 Copyright (c) 2002, 2006 Nexedi SARL and Contributors. All Rights Reserved.
                    Bartlomiej Gorny <bartek@erp5.pl>

 WARNING: This program as such is intended to be used by professional
 programmers who take the whole responsability of assessing all potential
 consequences resulting from its eventual inadequacies and bugs
 End users who are looking for a ready-to-use solution with commercial
 garantees and support are strongly adviced to contract a Free Software
 Service Company

 This program is Free Software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#############################################################################
*/

// this is an example use of oood from php
// requires phpxmlrpc extension
// available at
// https://sourceforge.net/projects/phpxmlrpc/

include 'xmlrpc.inc';

function getDataItem($response, $item){
    return $response['data']->structMem($item)->scalarval();
}


function analyzeResponse($res){
    $val = $res->value();
    $list = $val->scalarval();
    $status_code = $list[0]->scalarval();
    $data = $list[1];
    $msg = $list[2]->scalarval();
    return array('code'=>$status_code, 'data'=>$data, 'message'=>$msg);
}

function makeRequest($method, $params){
    $newparams = array();
    foreach($params as $k=>$v){
        if($k == 'data'){
            $v = base64_encode($v);
        }
        $newparams[$k] = new xmlrpcval($v, 'string');
    }
    $newparams = new xmlrpcval($newparams, 'struct');
    return new xmlrpcmsg($method, array($newparams));
}

 
if($_FILES){
    $tmpname = $_FILES['a_file']['tmp_name'];
    $filedata = file_get_contents($tmpname);
    $params = array('data'=>$filedata, 'extension'=>'pdf');
    $msg = makeRequest('generate', $params);

    $client = new xmlrpc_client('http://192.168.20.192:8008');
    $res = $client->send($msg);
    $response = analyzeResponse($res);

    if($response['code'] == 200){
        $f = fopen('test.pdf', 'w');
        $data = getDataItem($response, 'data');
        $mimetype = getDataItem($response, 'mime');
        header('Content-type: '.$mimetype);
        header('Content-Disposition: attachment; filename="test.pdf"');
        echo base64_decode($data);
    }else{
        echo 'Failed with code '.$response['code'].', message "'.$response['message'];
    }
}

?>

<html>
     <head>
        <title>Oood Pdf Generator</title>
    </head>
    <body>
        <form action="index.php" method="POST" enctype="multipart/form-data">
            <input type="file" name="a_file"/>
            <input type=submit value="Submit">
        </form>
    </body>
</html>