Whoops! Just realised I never fully finished the deleteunconfirmed
[moodle.git] / admin / lang.php
blob2f73fe75e24370599c93e8e6e8622c55da8c59a3
1 <?PHP // $Id$
3 require_once("../config.php");
5 optional_variable($mode, "");
6 optional_variable($currentfile, "moodle.php");
8 require_login();
10 if (!isadmin()) {
11 error("You need to be admin to edit this page");
14 if (! $site = get_site()) {
15 error("Site not defined!");
18 $stradministration = get_string("administration");
19 $strconfiguration = get_string("configuration");
20 $strlanguage = get_string("language");
21 $strcurrentlanguage = get_string("currentlanguage");
22 $strmissingstrings = get_string("missingstrings");
23 $strcomparelanguage = get_string("comparelanguage");
24 $strthislanguage = get_string("thislanguage");
26 switch ($mode) {
27 case "missing":
28 $navigation = "<A HREF=\"lang.php\">$strlanguage</A> -> $strmissingstrings";
29 $title = $strmissingstrings;
30 break;
31 case "compare":
32 $navigation = "<A HREF=\"lang.php\">$strlanguage</A> -> $strcomparelanguage";
33 $title = $strcomparelanguage;
34 break;
35 default:
36 $title = $strlanguage;
37 $navigation = $strlanguage;
38 break;
41 $currentlang = current_language();
43 print_header("$site->shortname: $title", "$site->fullname",
44 "<a href=\"index.php\">$stradministration</a> -> ".
45 "<a href=\"configure.php\">$strconfiguration</a> -> $navigation");
47 if (!$mode) {
48 $currlang = current_language();
49 $langs = get_list_of_languages();
50 echo "<table align=center><tr><td align=\"right\">";
51 echo "<b>$strcurrentlanguage:</b>";
52 echo "</td><td>";
53 echo popup_form ("$CFG->wwwroot/admin/lang.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
54 echo "</td></tr></table>";
55 print_heading("<a href=\"lang.php?mode=missing\">$strmissingstrings</a>");
56 print_heading("<a href=\"lang.php?mode=compare\">$strcomparelanguage</a>");
57 echo "<center><hr noshade size=1>";
58 $options["lang"] = $currentlang;
59 print_single_button("http://moodle.org/download/lang/", $options, get_string("latestlanguagepack"));
60 echo "</center>";
61 print_footer();
62 exit;
65 // Get a list of all the root files in the English directory
67 $langdir = "$CFG->dirroot/lang/$currentlang";
68 $enlangdir = "$CFG->dirroot/lang/en";
70 if (! $stringfiles = get_directory_list($enlangdir, "", false)) {
71 error("Could not find English language pack!");
74 foreach ($stringfiles as $key => $file) {
75 if (substr($file, -4) != ".php") {
76 unset($stringfiles[$key]);
80 if ($mode == "missing") {
81 // For each file, check that a counterpart exists, then check all the strings
83 foreach ($stringfiles as $file) {
84 if (!file_exists("$langdir/$file")) {
85 if (!touch("$langdir/$file")) {
86 echo "<p><font color=red>".get_string("filemissing", "", "$langdir/$file")."</font></p>";
87 continue;
91 unset($string);
92 include("$enlangdir/$file");
93 $enstring = $string;
95 unset($string);
96 include("$langdir/$file");
98 $first = true;
99 foreach ($enstring as $key => $value) {
100 if (!isset($string[$key]) or $string[$key] == "") {
101 $value = htmlspecialchars($value);
102 $value = str_replace("$"."a", "\\$"."a", $value);
103 $value = str_replace("%%","%",$value);
104 if ($first) {
105 echo "</PRE><HR><P><B>".get_string("stringsnotset","","$langdir/$file")."</B></P><PRE>";
106 $first = false;
107 $somethingfound = true;
109 echo "$"."string['$key'] = \"$value\";<BR>";
114 if (! $files = get_directory_list("$CFG->dirroot/lang/en/help", "CVS")) {
115 error("Could not find English language help files!");
118 foreach ($files as $filekey => $file) { // check all the help files.
119 if (!file_exists("$langdir/help/$file")) {
120 echo "<p><font color=red>".get_string("filemissing", "", "$langdir/help/$file")."</font></p>";
121 $somethingfound = true;
122 continue;
126 if (! $files = get_directory_list("$CFG->dirroot/lang/en/docs", "CVS")) {
127 error("Could not find English language docs files!");
129 foreach ($files as $filekey => $file) { // check all the docs files.
130 if (!file_exists("$langdir/docs/$file")) {
131 echo "<P><FONT COLOR=red>".get_string("filemissing", "", "$langdir/docs/$file")."</FONT></P>";
132 $somethingfound = true;
133 continue;
137 if (!empty($somethingfound)) {
138 print_continue("lang.php");
139 } else {
140 notice(get_string("languagegood"), "lang.php");
143 } else if ($mode == "compare") {
145 if (isset($_POST['currentfile'])){ // Save a file
146 $newstrings = $_POST;
147 unset($newstrings['currentfile']);
148 if (lang_save_file($langdir, $currentfile, $newstrings)) {
149 notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
150 } else {
151 error("Could not save the file '$currentfile'!", "lang.php?mode=compare&currentfile=$currentfile");
155 print_heading_with_help($strcomparelanguage, "langedit");
157 print_simple_box_start("center", "80%");
158 echo '<center><font size="2">';
159 foreach ($stringfiles as $file) {
160 if ($file == $currentfile) {
161 echo "<b>$file</b> &nbsp; ";
162 } else {
163 echo "<a href=\"lang.php?mode=compare&currentfile=$file\">$file</a> &nbsp; ";
166 echo '</font></center>';
167 print_simple_box_end();
170 print_heading("$currentfile", "center", 4);
172 if (!file_exists("$langdir/$currentfile")) {
173 if (!touch("$langdir/$currentfile")) {
174 echo "<p><font color=red>".get_string("filemissing", "", "$langdir/$currentfile")."</font></p>";
175 continue;
179 error_reporting(0);
180 if ($f = fopen("$langdir/$currentfile","r+")) {
181 $editable = true;
182 fclose($f);
183 } else {
184 $editable = false;
185 echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")."</font></p>";
187 error_reporting(7);
190 unset($string);
191 include("$enlangdir/$currentfile");
192 $enstring = $string;
193 ksort($enstring);
195 unset($string);
196 include("$langdir/$currentfile");
198 if ($editable) {
199 echo "<form name=\"$currentfile\" action=\"lang.php\" method=\"post\">";
201 echo "<table width=\"100%\" cellpadding=2 cellspacing=3 border=0>";
202 foreach ($enstring as $key => $envalue) {
203 $envalue = nl2br(htmlspecialchars($envalue));
204 $envalue = preg_replace('/(\$a\-\&gt;[a-zA-Z0-9]*|\$a)/', '<b>$0</b>', $envalue); // Make variables bold.
205 $envalue = str_replace("%%","%",$envalue);
206 $envalue = str_replace("\\","",$envalue); // Delete all slashes
208 echo "<tr>";
209 echo "<td width=20% bgcolor=\"$THEME->cellheading\" nowrap valign=top>$key</td>";
210 echo "<td width=40% bgcolor=\"$THEME->cellheading\" valign=top>$envalue</td>";
212 $value = $string[$key];
213 $value = str_replace("\r","",$value); // Bad character caused by Windows
214 $value = str_replace("\n\n\n\n\n\n","\n",$value); // Collapse runs of blank lines
215 $value = str_replace("\n\n\n\n\n","\n",$value);
216 $value = str_replace("\n\n\n\n","\n",$value);
217 $value = str_replace("\n\n\n","\n",$value);
218 $value = str_replace("\n\n\n","\n",$value);
219 $value = str_replace("\\","",$value); // Delete all slashes
220 $value = str_replace("%%","%",$value);
221 $value = htmlspecialchars($value);
223 $cellcolour = $value ? $THEME->cellcontent: $THEME->highlight;
225 if ($editable) {
226 echo "<td width=40% bgcolor=\"$cellcolour\" valign=top>";
227 if (isset($string[$key])) {
228 $valuelen = strlen($value);
229 } else {
230 $valuelen = strlen($envalue);
232 $cols=50;
233 if (strstr($value, "\r") or strstr($value, "\n") or $valuelen > $cols) {
234 $rows = ceil($valuelen / $cols);
235 echo "<textarea name=\"string-$key\" cols=\"$cols\" rows=\"$rows\">$value</textarea>";
236 } else {
237 if ($valuelen) {
238 $cols = $valuelen + 2;
240 echo "<input type=\"text\" name=\"string-$key\" value=\"$value\" size=\"$cols\"></td>";
242 echo "</TD>";
244 } else {
245 echo "<td width=40% bgcolor=\"$cellcolour\" valign=top>$value</td>";
248 if ($editable) {
249 echo "<tr><td colspan=2>&nbsp;<td><br />";
250 echo " <input type=\"hidden\" name=\"currentfile\" value=\"$currentfile\">";
251 echo " <input type=\"hidden\" name=\"mode\" value=\"compare\">";
252 echo " <input type=\"submit\" name=\"update\" value=\"".get_string("savechanges").": $currentfile\">";
253 echo "</td></tr>";
255 echo "</table>";
256 echo "</form>";
260 print_footer();
262 //////////////////////////////////////////////////////////////////////
264 function lang_save_file($path, $file, $strings) {
265 // Thanks to Petri Asikainen for the original version of code
266 // used to save language files.
268 // $path is a full pathname to the file
269 // $file is the file to overwrite.
270 // $strings is an array of strings to write
272 global $CFG, $USER;
274 error_reporting(0);
275 if (!$f = fopen("$path/$file","w")) {
276 return false;
278 error_reporting(7);
281 fwrite($f, "<?PHP // \$Id\$ \n");
282 fwrite($f, " // $file - created with Moodle $CFG->release ($CFG->version)\n\n\n");
284 ksort($strings);
286 foreach ($strings as $key => $value) {
287 list($id, $stringname) = explode("-",$key);
288 if ($CFG->lang != "zh_hk" and $CFG->lang != "zh_tw") { // Some MB languages include backslash bytes
289 $value = str_replace("\\","",$value); // Delete all slashes
291 $value = str_replace("'", "\\'", $value); // Add slashes for '
292 $value = str_replace('"', "\\\"", $value); // Add slashes for "
293 $value = str_replace("%","%%",$value); // Escape % characters
294 $value = str_replace("\r", "",$value); // Remove linefeed characters
295 if ($id == "string" and $value != ""){
296 fwrite($f,"\$string['$stringname'] = '$value';\n");
299 fwrite($f,"\n?>\n");
300 fclose($f);
302 return true;