<?

//
// Sample Web form demonstrating file upload to Siets server using HTTP API built-in
// conversion tool
//

header('content-type: text/html; charset=utf-8');

// stupid quotes
if (get_magic_quotes_gpc()) {

   function stripslashes_deep($value)
   {
       return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
   }

   $_GET = array_map('stripslashes_deep', $_GET);
   $_POST = array_map('stripslashes_deep', $_POST);
   $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}



function http_post($u, $headers, $data)
{
	if (is_string($u)) $u = parse_url($u);
	if (!$u['port']) $u['port'] = 80;
	if (!$u['path']) $u['path'] = '/';
	$request = "POST " . $u['path'] . '?' . $u['query'] . " HTTP/1.0\r\nHost: " . $u['host'] . "\r\n$headers\r\n$data\r\n\r\n";
	$response = FALSE;
	
	$f = fsockopen($u['host'], $u['port']);
	if ($f) {
		fputs($f, $request);
		while (!feof($f)) $response .= fgets($f, 4096);
		fclose($f);
	}
		
	return $response;
}



function xml_esc($str)
{
	return htmlspecialchars($str, ENT_QUOTES, 'utf-8');
}



if (!isset($_POST['b'])) {
	// form initialization
	$p = 'http://127.0.0.1/cgi-bin/siets/api.cgi';
	$s = 'test';
	$t = '';
	$display_reply = FALSE;
} else {
	// POST data received
	$p = $_POST['p'];
	$s = $_POST['s'];
	$t = $_POST['t'];
	$enc = 'utf-8';
	$f = $_FILES['f'];
	$display_reply = TRUE;

	if ($f['error'] === UPLOAD_ERR_OK) {
		// pass file extension as its type
		if (($extpos = strrpos($f['name'], '.')) === FALSE) {
			$ext = '';
		} else {
			$ext = substr($f['name'], $extpos + 1);
		}
		
		// format XML command (conversion works with update command too)
		$data = '<?xml version="1.0" encoding="' . xml_esc($enc) . '"?' . '>
<siets:request xmlns:siets="www.siets.net">
	<siets:storage>' . xml_esc($s) . '</siets:storage>
	<siets:command>insert</siets:command>
	<siets:requestid>1234</siets:requestid>
	<siets:application>console</siets:application>
	<siets:user>guest</siets:user>
	<siets:password>guest</siets:password>
	<siets:reply_charset>utf-8</siets:reply_charset>
	<siets:content>
		<document>
			<id>' . xml_esc($f['name']) . '</id>
			' . (1 ? '<title>' . xml_esc($t) . '</title>' : '') . '
			<rate>100</rate>
			<!--<text>$text</text>-->
			<file store="yes">
				<ext>' . xml_esc($ext) . '</ext>
				<data>' . base64_encode(file_get_contents($f['tmp_name'])) . '</data>
			</file>
			<info></info>
			<domain></domain>
		</document>
	</siets:content>
</siets:request>';
		
		// execute Siets command
		$reply = http_post($p, "Content-Type: text/xml\r\nContent-Length: " . strlen($data) . "\r\n", $data);
		if ($reply === FALSE) {
			$reply = 'Could not connect to Siets server!';
			$display_reply = FALSE;
		}
	} else {
		$reply = 'File upload failed!';
		$display_reply = FALSE;
	}
}

?>
<html>
<head><title>Siets HTTP API conversion demo</title></head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<body>
<h1>File upload to Siets Server using HTTP API built-in conversion tool</h1>
<form method="post" enctype="multipart/form-data">
<table width="100%" border="1" cellpadding="10">
<tr>
<td width="50%" valign="top">
	<div>
		<h4>Upload file</h4>
		<input type="text" name="p" value="<?= xml_esc($p) ?>" style="width: 100%" />
		<table style="width: 100%; border: 0px">
			<tr><td>Storage:</td><td><input type="text" name="s" value="<?= xml_esc($s) ?>" style="width: 100%" /></td></tr>
			<tr><td>Title:</td><td><input type="text" name="t" value="<?= xml_esc($t) ?>" style="width: 100%" /></td></tr>
			<tr><td>File:</td><td><input type="file" name="f" size="42" /></td></tr>
		</table>
		<div align="center">
			<input type="submit" name="b" value="Upload" />
		</div>
	</div>
</td>
<td width="50%" valign="top">
	<div>
		<h4>Response:</h4>
	</div>
	<? if ($display_reply) { ?>
  	<textarea style="width: 100%; height: 380" wrap="off"><?= xml_esc($reply) ?></textarea>
	<? } else { ?>
	<p><?= xml_esc($reply) ?></p>
	<? } ?>
</td>
</tr>
</table>
</form>
</body>
</html>