<?php
include('xmlrpc.inc');
class zip_client {
private $host;
private $port;
private $username;
private $password;
private $output_directory;
function __construct($host = 'localhost', $port = 6543, $username = '', $password = '') {
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->output_directory = getcwd();
}
function setOutputDirectory($output_directory) {
if (!is_dir($output_directory)) {
mkdir($output_directory);
}
$this->output_directory = $output_directory;
}
/**
* Generate a ZIP file from a directory containing all its contents.
*
* @param string $directory: Directory containing the files to ZIP
* @return string $zip_filename: Filename of the generated ZIP file
*/
function _makeZipFromDirectory($directory) {
$directory = realpath($directory);
$zip_filename = tempnam('./', 'tmpfile');
$handle = fopen($zip_filename, 'w');
$ZF = new ZipArchive();
if ($ZF->open($zip_filename, ZIPARCHIVE::CREATE) !== TRUE) {
die('ERROR: Cannot open ' . $zip_filename);
}
if (is_dir($directory)) {
if ($dh = opendir($directory)) {
while (($file = readdir($dh)) !== false) {
if (filetype($directory . '/' . $file) == 'file') {
$ZF->addFile($directory . '/' . $file, $file);
}
}
closedir($dh);
}
}
$ZF->close();
return $zip_filename;
}
/**
* Send an XMl-RPC-request
*
* @param string $method: Name of method
* @param array $parameters: Parameters as an array of objects
* @return string: Response of XML-RPC-server
*/
private function xmlrpcCall($method, $parameters = array()) {
$f = new xmlrpcmsg($method, $parameters);
$c = new xmlrpc_client('/' . $method, $this->host, $this->port);
$r = $c->send($f);
if (!$r->faultCode()) {
$v = $r->value();
return $v->scalarval();
} else {
die('An error occurred (Code ' . htmlspecialchars($r->faultCode()) . '): ' . ($r->faultString()));
}
}
function _authenticate() {
return $this->xmlrpcCall(
'authenticate',
array(
new xmlrpcval($this->username, 'string'),
new xmlrpcval($this->password, 'string')
)
);
}
function ping() {
return $this->xmlrpcCall('ping');
}
/**
* Return available converters
*
* @return array
*/
function availableConverters() {
$converters = array();
foreach ($this->xmlrpcCall('availableConverters') as $converter) {
$converters[] = $converter->scalarval();
}
return $converters;
}
/**
* XMLRPC client to SmartPrintNG server
*
* @param string $dirname: Name of directory with files
* @param string $converter_name: (optional) Name of converter
* @return string: Name of output file
*/
function convertZIP($dirname, $converter_name = 'pdf-prince') {
// Authenticate first
$auth_token = $this->_authenticate();
$zip_filename = $this->_makeZipFromDirectory($dirname);
$handle = fopen($zip_filename, 'r');
$zip_data = $this->xmlrpcCall(
'convertZIP',
array(
new xmlrpcval($auth_token, 'boolean'),
new xmlrpcval(base64_encode(fread($handle, filesize($zip_filename))), 'string'),
new xmlrpcval($converter_name, 'string')
)
);
fclose($handle);
// and receive the conversion result as base64 encoded ZIP archive
//(it will contain only *one* file)
$zip_temp = tempnam('./', 'tmpfile');
$handle = fopen($zip_temp, 'w');
fwrite($handle, base64_decode($zip_data));
fclose($handle);
$ZF = new ZipArchive();
$ZF->open($zip_temp);
$ZF->extractTo($this->output_directory);
$filename = $ZF->getNameIndex(0);
$ZF->close();
unlink($zip_temp);
unlink($zip_filename);
return $filename;
}
/**
* XMLRPC client to SmartPrintNG server
*
* @param string $dirname: Name of directory with files
* @param string $converter_name: (optional) Name of converter
* @param string $prefix: Name of output file
* @return string: Location (url) of output file
*/
function convertZIPandRedirect($dirname, $converter_name = 'pdf-prince', $prefix = NULL) {
// Authenticate first
$auth_token = $this->_authenticate();
$zip_filename = $this->_makeZipFromDirectory($dirname);
$handle = fopen($zip_filename, 'r');
$location = $this->xmlrpcCall(
'convertZIPandRedirect',
array(
new xmlrpcval($auth_token, 'boolean'),
new xmlrpcval(base64_encode(fread($handle, filesize($zip_filename))), 'string'),
new xmlrpcval($converter_name, 'string'),
new xmlrpcval($prefix, 'string')
)
);
fclose($handle);
return $location;
}
/**
* XMLRPC client to SmartPrintNG server
*
* @param string $dirname: Name of directory with files
* @param string $converter_name: (optional) Name of converter
* @param string $sender
* @param string $recipients
* @param string $subject
* @param string $body
* @return
*/
function convertZIPEmail($dirname, $converter_name = 'pdf-prince', $sender = NULL, $recipients = NULL, $subject = NULL, $body = NULL) {
// Authenticate first
$auth_token = $this->_authenticate();
$zip_filename = $this->_makeZipFromDirectory($dirname);
$handle = fopen($zip_filename, 'r');
$result = $this->xmlrpcCall(
'convertZIPEmail',
array(
new xmlrpcval($auth_token, 'boolean'),
new xmlrpcval(base64_encode(fread($handle, filesize($zip_filename))), 'string'),
new xmlrpcval($converter_name, 'string'),
new xmlrpcval($sender, 'string'),
new xmlrpcval($recipients, 'string'),
new xmlrpcval($subject, 'string'),
new xmlrpcval($body, 'string')
)
);
fclose($handle);
return $result;
}
}
$zipClient = new zip_client('zopyx.com', 6543);
#$zipClient->setOutputDirectory('outputdir');
#var_dump($zipClient->_makeZipFromDirectory('ziptestdateien2'));
#var_dump($zipClient->ping());
#var_dump($zipClient->_authenticate());
#var_dump($zipClient->availableConverters());
#var_dump($zipClient->convertZIP('ziptestdateien2'));
#var_dump($zipClient->convertZIPandRedirect('ziptestdateien2', 'pdf-prince'));
#var_dump($zipClient->convertZIPEmail('ziptestdateien2', 'pdf-prince', 'hide@address.com', 'hide@address.com', 'zip client test', 'hier body'));
?>