function HandleM2MPublish ($pagename, $auth='read') {  // @@ separate publish permissions
	session_start();
	$targetformat = $_SESSION['targetformat'] = $_REQUEST['targetformat'];
	$converter = WikiConverterFactory::createConverter($pagename, $targetformat);
	if (isset($_REQUEST['m2m-options'])) 
		HandleM2MOptions($pagename, $auth);
	else {
		global $M2MDir, $M2MUrl, $M2MDataDir;
		require "$M2MDir/wikixml_markup.inc.php";  // load WikiXML markup...
		setlocale(LC_NUMERIC, "C");                // force decimal dots for number->string conversions
		if (is_object($converter)) {
			global $LinkPageCreateFmt, $M2MDataUrl;
			echo "<html><head><title>media2mult</title></head><body>";
			$mode = 'page';
			if (isset($_POST['publishtrail']))
				$mode = 'trail';
			elseif (isset($_POST['publishcollection']))
				$mode = 'collection';

			$datadir = "$M2MDataDir/$pagename";
			$options = new StylesheetOptions("$M2MDir/options/$targetformat.xml");
			if (file_exists("$datadir/{$targetformat}opt.xml"))
				$options->readXML("$datadir/{$targetformat}opt.xml");
			$options->setValue("processing.date", date('j.n.Y'));
			$options->setValue("processing.mode", $mode);
			$options->writeStylesheet("$datadir/$targetformat.xsl", "$M2MDir/xsl/{$targetformat}.xsl");

			$result = $converter->convert($mode);
			if ($result) {
				global $SingleResultOnly, $M2MUrl, $M2MDataUrl;
				$filetype = strtoupper($converter->getFileExtension());
				echo "<h2><font color='green'>conversion was successful</font></h2>";
				if ($SingleResultOnly)
					echo "<a href='$M2MDataUrl/$pagename/".basename($result)."'>download $filetype-document here</a>";
				else {
					echo "<img src='$M2MUrl/images/pdf.png' valign='middle'><a href='$M2MDataUrl/$pagename/".basename($result)."'>$filetype file</a><br>";
					echo "<img src='$M2MUrl/images/xml.png' valign='middle'><a href='$M2MDataUrl/$pagename/source.xml'>XML file</a>"; 
				}
			}
			else
				echo "<h2><font color='red'>error occured during conversion</font></h2>";
			echo "</body></html>";
		}
		exit;
	}
}
function saveOptions ($optarray, $format, $outputdir) {
	global $M2MDir;
	$optfile = "$outputdir/{$format}opt.xml";
	$sso = new StylesheetOptions();
	if(file_exists($optfile))
		$sso->readXML($optfile);
	else 
		$sso->readXML("$M2MDir/{$format}opt.xml"); 
	$options =& $sso->getOptions();
	foreach ($options as $o) {
		$value = $optarray[strtr($o['name'], ".", "_")]; 
		if ($o['type'] == 'file' && $o['value'] != '' && $value == '') {
			continue;
		}

		if ($o['type'] == 'bool')          // unchecked checkboxes are not submitted by POST
			$value = $value == "" ? 0 : 1;  // only checked checkboxes submit a (former) value "false"
		$sso->setValue($o['name'], $value);
	}
	$sso->writeXML($optfile);
}