<script language="JavaScript" type="text/javascript">
function checkinput ( form )
{
     if (form.new_language.value == "") 
     {
      alert( "<?php echo $sas_lang['input_error'] ?>" );
      form.new_language.focus();
      return false ;
     }
  return true ;
}
</script>
<?php

#############################################################################
#           			                   Site@School
# Site@School is a website management system designed for primary schools.
#
# About Site@School:
# Comissioned by: City District 'de Baarsjes', Amsterdam, Netherlands
# Original idea, coding (version 1.0) and design : Mark de Haan (mark@starwave.nl)
# Ideas, improvements, expansions (version 1.1, - 2.4): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# translate.php: last changed 08/03/2006  , version: 2.4
#            by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: translate.inc.php,v 1.1 2006/10/03 08:33:55 fstuurman Exp $
#
# 2003-06-22/HPA Fokker <peter@berestijn.nl>
#  - added HTML-tag higlighting via function colour_html()
#  - added light background colour in original text (improves readability IMHO)
#
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2005 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# 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.
##############################################################################
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}
if (IsSet ($_GET['suboption']))
{
	$suboption = $_GET['suboption'];
}
else
{
	$suboption = "";
}
if (IsSet ($_GET['go']))
{
	$go = $_GET['go'];
}
else
{
	$go = "";
}
if (IsSet ($_POST['new_language']))
{
	$new_language = $_POST['new_language'];
}
if (IsSet ($_GET['new_language']))
{
	$new_language = $_GET['new_language'];
}
if (IsSet ($_GET['module']))
{
	$module = $_GET['module'];
}
if (IsSet ($_POST['lang_new']))
{
	$lang_new = $_POST['lang_new'];
}
if (!IsSet ($new_language))
{
	$new_language = $language;
}
$new_language = strtoupper($new_language);

if ($go == "save") //save the language file
{
	$language_output = "<?php
	#############################################################################
	#                              Site@School
	# Site@School is a website management system designed for schools.
	#
	# language file: $new_language.php 
	############################################################################\n";

	if (get_magic_quotes_gpc()) // code to use when magic quotes is set
	{
		while ($i < count($lang_new)) //loop through the language entries
		{
			$language_output = $language_output . "$" . "sas_lang['" . key($lang_new) . "'] = \"" . preg_replace("/\\\'/", "'", current($lang_new)) . "\" ;\n";
			next($lang_new);
			$i++;
		}
	}
	else
	{ // code to use when magic quotes is not set
		while ($i < count($lang_new)) //loop through the language entries
		{
			$language_output = $language_output . "$" . "sas_lang['" . key($lang_new) . "'] = \"" . preg_replace("/\"/", "\\\"", current($lang_new)) . "\" ;\n";
			next($lang_new);
			$i++;
		}
	}

	$language_output = $language_output . "?>";

	include ("interface/header.inc.php");
	include ("interface/menubar.inc.php");
	include ("interface/menustart.inc.php");

	print "<span class=heading>Menu</span><br><br>";
	print "<span class=link><a href=\"index.php?option=tools&suboption=translation\">" . $sas_lang['startcenter'] . "</a></span><br>";
	$query = "SELECT name,location FROM $table_modules"; //get all the module names and locations                           
	$result = mysql_query($query) or die_script($query, mysql_error());
	while ($row = mysql_fetch_array($result))
	{
		$module_language_file = "languages/EN/" . $row['location'] . "/EN.php";
		if (file_exists($module_language_file)) // If english exists we can translate into other languages
		{
			print "<span class=link><a href=\"index.php?option=tools&suboption=translation&module=$row[name]\">" . $row['name'] . "</a></span><br>";
		}
	}
	include ("interface/menuend.inc.php");
	include ("interface/pagestart.inc.php");

	if (IsSet ($module)) //module translation
	{
		$query = "SELECT location FROM $table_modules WHERE name='$module'";
		$result = mysql_query($query) or die_script($query, mysql_error());
		$module_location = mysql_result($result, 0);
		unset ($query);
		unset ($result);
		@ chmod("languages/$new_language/$module_location/$new_language.php", 0777);
		$language_file = fopen("languages/$new_language/$module_location/" . $new_language . "_user.php", "w");
		fwrite($language_file, "$language_output");
		print $sas_lang['module_name'] . " <b>$module</b><br>";
	}
	else
	{
		@ chmod("languages/$new_language/$new_language.php", 0777);
		$language_file = fopen("languages/$new_language/" . $new_language . "_user.php", "w");
		fwrite($language_file, "$language_output");
	}

	echo "$new_language : " . $sas_lang['language_file_saved'];
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

include ("interface/header.inc.php");
include ("interface/menubar.inc.php");
include ("interface/menustart.inc.php");
print "<span class=heading>Menu</span><br><br>";
print "<span class=link><a href=\"index.php?option=tools&suboption=translation\">" . $sas_lang['startcenter'] . "</a></span><p>";
$query = "SELECT name,location FROM $table_modules"; //get all the module names and locations                           
$result = mysql_query($query) or die_script($query, mysql_error());
while ($row = mysql_fetch_array($result))
{
	$module_language_file = "languages/EN/" . $row['location'] . "/EN.php";
	if (file_exists($module_language_file)) // If english exists we can translate into other languages
	{
		print "<span class=link><a href=\"index.php?option=tools&suboption=translation&module=$row[name]\">" . $row['name'] . "</a></span><p>";
	}
}

include ("interface/menuend.inc.php");
include ("interface/pagestart.inc.php");

if (substr("$access", 11, 1) == "0") //bit 12 off the accessrights
{
	print "<span class=\"heading\">" . $sas_lang['noaccess'] . "<br></span>";
	print "<span class=\"text\">" . $sas_lang['noaccess_introduction'] . "</span><br><br><br>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

print "<span class=\"heading\">" . $sas_lang['translation'] . "<br></span>";
print "<span class=\"text\">" . $sas_lang['translation_introduction'] . "<br><br></span>";

if (IsSet ($module)) //module translation
{
	$query = "SELECT location FROM $table_modules WHERE name='$module'";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$module_location = mysql_result($result, 0);
	unset ($query);
	unset ($result);

	$language_file = "languages/" . $new_language . "/" . $module_location . "/" . $new_language . ".php";
	$language_file_user = "languages/" . $new_language . "/" . $module_location . "/" . $new_language . "_user.php";

	if (!file_exists($language_file) and file_exists("languages/$new_language/$new_language.php")) //if module language file not exists create one
	{
		mkdir("languages/$new_language/$module_location", 0755);
		copy("languages/EN/$module_location/EN.php", "$language_file");
	}
	unset ($sas_lang); //clear the main language file array

	if (file_exists($language_file)) //load the default language file
	{
		include ("$language_file");
	}

	if (file_exists($language_file_user)) //load the user language file if it exists
	{
		include ("$language_file_user");
	}

	$lang_mod_new = $sas_lang;

	include ("languages/EN/$module_location/EN.php");
	$lang_mod_en = $sas_lang;
	include ("languages/" . $language . "/" . $language . ".php"); //read main language file again

	print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\"><tr><td>
	         <form method=\"POST\" action=\"index.php?option=tools&suboption=translation&module=$module\" onsubmit=\"return checkinput(this);\"></td>
	         <td>" . $sas_lang['language_file'] . " <b>$new_language</b><br>" . $sas_lang['language_file_new'] . " <input type=\"text\" name=\"new_language\" size=\"2\">&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['send'] . "\" class=\"savebutton\" name=\"B1\"></form>
	         <form method='POST' action='index.php?option=tools&suboption=translation&go=save&new_language=$new_language&module=$module'>
	         </td></tr><tr><td>&nbsp;</td><td>" . $sas_lang['module_name'] . " <b>$module</b></td></tr><tr><td>&nbsp;</td></tr>";
	$i = 1;
	while ($i < count($lang_mod_en) + 1)
	{
		$key_en = key($lang_mod_en);
		$value_new = $lang_mod_new[$key_en];
		$value_en = stripslashes(current($lang_mod_en));
		$value_en = colour_html($value_en);
		$length = strlen($value_en) + 20;

		if ($length > 100)
		{
			$rows = ceil($length / 100);
			print "<tr><td valign=\"top\">$i.</i></td><td bgcolor=\"#CCCCCC\" class=\"text\"><b>$value_en</b></td></tr><tr><td>&nbsp;</td><td><textarea class=\"text\" cols=\"90\" rows=\"$rows\" name=\"lang_new[$key_en]\">$value_new</textarea></td></tr>";
		}
		else
		{
			print "<tr><td valign=\"top\">$i.</td><td bgcolor=\"#CCCCCC\" class=\"text\"><b>$value_en</b></td></tr><tr><td>&nbsp;</td><td><textarea style=\"HEIGHT:20px;\" class=\"text\" cols=\"$length\" style=\"overflow: auto\" rows=\"1\" name=\"lang_new[$key_en]\">$value_new</textarea></td></tr>";
		}

		next($lang_mod_en);
		$i++;
	}
	print "<tr><td colspan=\"2\" align=\"center\"><input type='submit' value='&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "' class=\"savebutton\" name='B1'></td></tr>
	         </table></form>";
}
else //main translation
	{
	$language_file = "languages/" . $new_language . "/" . $new_language . ".php";
	$language_file_user = "languages/" . $new_language . "/" . $new_language . "_user.php";

	if (!file_exists($language_file)) //if language file not exists create one
	{
		mkdir("languages/$new_language", 0755);
		copy("languages/EN/EN.php", "languages/$new_language/$new_language.php");
	}
	include ("$language_file"); //load the default language file

	if (file_exists($language_file_user)) //load the user language file if it exists
	{
		include ("$language_file_user");
	}

	$lang_new = $sas_lang;

	include ("languages/EN/EN.php");
	$lang_en = $sas_lang;
	include ("languages/" . $language . "/" . $language . ".php");

	print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\"><tr><td>
	       <form method=\"POST\" action=\"index.php?option=tools&suboption=translation\" onsubmit=\"return checkinput(this);\"></td>
	       <td>" . $sas_lang['language_file'] . " <b>$new_language</b><br>" . $sas_lang['language_file_new'] . " <input type=\"text\" name=\"new_language\" size=\"2\">&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['send'] . "\" class=\"savebutton\" name=\"B1\"></form>
	       <form method='POST' action='index.php?option=tools&suboption=translation&go=save&new_language=$new_language'>
	       </td></tr><tr><td>&nbsp;</td></tr>";
	$i = 1;
	while ($i < count($lang_en) + 1)
	{
		$key_en = key($lang_en);

		$value_new = $lang_new[$key_en];
		$value_en = stripslashes(current($lang_en));
		$value_en = colour_html($value_en);
		$length = strlen($value_en) + 20;

		if ($length > 100)
		{
			$rows = ceil($length / 100);
			print "<tr><td valign=\"top\">$i.</td><td bgcolor=\"#CCCCCC\" class=\"text\"><b>$value_en</b></td></tr><tr><td>&nbsp;</td><td><textarea class=\"text\" cols=\"90\" rows=\"$rows\" name=\"lang_new[$key_en]\">$value_new</textarea></td></tr>";
		}
		else
		{
			print "<tr><td valign=\"top\">$i.</td><td bgcolor=\"#CCCCCC\" class=\"text\"><b>$value_en</b></td></tr><tr><td>&nbsp;</td><td><textarea style=\"HEIGHT:22px;\" class=\"text\" cols=\"$length\" style=\"overflow: auto\" rows=\"1\" name=\"lang_new[$key_en]\">$value_new</textarea></td></tr>";
		}

		next($lang_en);
		$i++;
	}
	print "<tr><td colspan=\"2\" align=\"center\"><input type='submit' value='&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "' class=\"savebutton\" name='B1'></td></tr>
	       </table></form>";
}

include ("interface/pageend.inc.php");
include ("interface/footer.inc.php");

function colour_html($s, $f1 = "<font color=\"red\">", $f0 = "</font>")
{
	/*------------------------------------------------------------------
	 * 2003-06-22/HPA Fokker <peter@berestijn.nl>
	 *
	 * colour_html() - show HTML-tags in string $s by wrapping strings
	 *                 in $f1 and $f0 in output. 
	 * $s            = string to convert
	 * $f1           = (optional) string which switches highlighting on
	 * $f0           = (optional) string which switches highlighting off
	 *
	 * A quick and dirty hack to make HTML-tags visible in the S@S
	 * translation module. By default we color the tags with a RED
	 * foreground colour.
	 */

	/*1 -- are there any tags at all? */

	$i = intval(strpos(" $s", "<")); // note the leading space, use that to
	// work around the difference between
	// $1 = 0 (character occurred at start of string)
	// $1 = FALSE (character was not found at all)
	// Here $i can never be 0 because "<" is not " "
	if (!$i)
		return $s; // none found, return original string

	/* 2 -- we have at least 1 opening bracket, process string */

	$t = "";
	while ($i)
	{
		$t .= substr($s, 0, $i -1); // add part of source upto the first bracket to target
		$s = substr($s, $i -1); // remainder of source including the 1st bracket
		$j = intval(strpos(" $s", ">"));
		if (!$j)
		{ // no closing bracket? just use rest of $s
			$h = $s; // $h contains the (invalid) HTML tag
			$s = ""; // and we're through
		}
		else
		{
			$h = substr($s, 0, $j); // got the HTML-tag including ">"
			$s = substr($s, $j); // and remainder of source without the ">"
		}
		$t .= $f1 . htmlspecialchars($h) . $f0 . $h; // highlight the escaped tag + add the tag itself
		$i = intval(strpos(" $s", "<")); // look for the next tag
	}
	$t .= $s; // add remainder of $s if any

	/* 3 -- now also show S@S variables in the different colour */
	$s = $t; // $s is once again our source
	$t = ""; // and $t our target
	while (ereg("(@[A-Z0-9]+@)", $s, $regs))
	{ // while the source contains @VARNAME@...
		$i = intval(strpos(" $s", $regs[0])); // locate the @VARNAME@
		$t .= substr($s, 0, $i -1) . $f1 . $regs[0] . $f0; // put @VARNAME@ between colourcodes $f1 and $f0
		$s = substr($s, $i +strlen($regs[0]) - 1); // an chomp the processed part off $s; rinse & repeat.
	}
	$t .= $s;
	return $t;
}
?>
