<?
/*
SVGTranslate 1.05 © 2008 Nikola Smolenski <smolensk@eunet.yu>

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.
*/

//error_reporting(E_ERROR);
include("include.php");

if($_POST['action'] == "translate") {
	if(isset($lang[$_POST['lang']])) {
		$language = $_POST['lang'];
	} else {
		error("Bad request.");
	}
	$svg=$_POST['svg'];
	$svg=(strpos($svg,"http://")===0)?$svg:"http://$svg";
	$in=fetch_file($svg);
	$in=file_split($in);

 	$i=0;
	foreach($in as $k=>$v) {
		if(strlen(trim($v)) && $v{0} != "<" && $v != "image/svg+xml") {
			if(trim($v) !=$_POST["original$i"]) {
				error("Strange error.");
			}
			if(strlen(trim($_POST["translation$i"]))) {
				$in[$k]=trim($_POST["translation$i"]);
			}
			$i++;
		}
	}

	$in=implode("",$in);
	$in.=(substr($in,-1)=="\n"?"":"\n")."<!-- Translated to ".$lang[$language]." by SVGTranslate 1.05 -->";

	header("Content-Type: image/svg+xml");
	header("Content-Length: ".strlen($in));
	header("Content-Disposition: attachment; filename=".preg_replace("/[.]([^.]+)$/","_$language.\\1",basename($svg)));

	echo $in;
	die();
}

head();
?><div style="float: right;"><a href="svgtranslate.php?view_source">view source</a> <a href="include.php?view_source">view include.php source</a></div>
<p><a href="http://tools.wikimedia.de/~nikola/svgtranslate.php">SVGTranslate</a> 1.05</p>
<?
if(isset($_REQUEST['svg'])) {
	$svg=trim($_REQUEST['svg']);

	if(strtolower(substr($svg,-4))!=".svg") {
		error("Not a SVG file!");
	}
	
	$svg=(strpos($svg,"http://")===0)?$svg:"http://$svg";
	$in=fetch_file($svg);
	
	if(stripos($in,"<svg")===false) {
		error("Not a SVG file!");
	}

	$string=fetch_strings(file_split($in));

	if(!count($string)) {
		error("Nothing to translate!");
	}

	echo "Translating...";
	?><form method="POST">
<input type="hidden" name="action" value="translate"/>
<input type="hidden" name="strings" value="<? echo count($string); ?>"/>
<input type="hidden" name="svg" value="<? echo htmlspecialchars($svg); ?>"/>
<table>
<tr><th>Original</th><th>Translation</th></tr><?
	$i=0;
	foreach($string as $v) {
		echo "\t<tr><td align=\"right\"><input type=\"hidden\" name=\"original$i\" value=\"".
			htmlspecialchars($v).
			"\"/>".
			htmlspecialchars($v).
			"</td><td><input type=\"text\" name=\"translation$i\" size=\"40\"/></td></tr>\n";
		$i++;
	}
	?>
	<tr><td colspan="2"><hr style="width: 50%"/></td></tr>
	<tr><td align="right">Language</td><td><? printselect("lang",$lang,""); ?></td></tr>
	<tr><td>&nbsp;</td><td><input type="submit"  value="Submit Translation"/>&nbsp;<input type="reset"/></td></tr>
</table>
</form><?
} else {
	inputform();
}

foot();

function fetch_file($svg) {
	$in=implode("",file($svg));
		
	if(!strlen($in)) {
		error("URL can not be retrieved!");
	}
	
	return $in;
}

function file_split($in) {
	return preg_split("/(<[^>]*>)/",$in,-1,PREG_SPLIT_DELIM_CAPTURE);
}

function fetch_strings($in) {
	$string=array();
	foreach($in as $v) {
		if(strlen(trim($v)) && $v{0} != "<" && $v != "image/svg+xml") {
			$string[]=trim($v);
		}
	}

	return $string;
}

function error($message) {
	global $svg;

	echo "<p style='color: red;'>$message</p>\n";
	inputform($svg);
	foot();
	die();
}

function inputform($svg = null) {
?><form method="GET"><table>
<tr>
	<td align="right">SVG file URL:</td>
	<td><input type="text" name="svg"<? echo isset($svg)?" value=\"".htmlspecialchars($svg)."\"":""; ?>/>
</tr><tr>
	<td>&nbsp;</td>
	<td><input type="submit" value="Submit"/>&nbsp;<input type="reset"/></td>
</tr>
</table></form>
<p>NOTE: the SVG file URL must point to the file itself, not to its description page. For example, <a href="http://upload.wikimedia.org/wikipedia/commons/8/8a/Planetary_transit.svg">http://upload.wikimedia.org/wikipedia/commons/8/8a/Planetary_transit.svg</a> is a valid URL. You may get this by going to the image description page, right clicking over the SVG and selecting "Copy Link Location" or similar in your browser's menu.</p>
<p>Questions, comments, suggestiong and bug reports are welcome at <a href="http://commons.wikimedia.org/wiki/User_talk:Nikola_Smolenski">my talk page</a>.</p><?
}
?>