4 This script enables Moodle translators to edit /docs and /help language
5 files directly via WWW interface.
7 Author: mudrd8mz@uxit.pedf.cuni.cz (http://moodle.cz)
8 Based on: lang.php in 1.4.3+ release
9 Thanks: Jaime Villate for important bug fixing, koen roggemans for his job and all moodlers
10 for intensive testing of this my first contribution
12 require_once('../config.php');
15 // Some local configuration
17 $fileeditorrows = 12; // number of textareas' rows
18 $fileeditorcols = 100; // dtto cols
19 $fileeditorinline = 1; // shall be textareas put in one row?
20 $filemissingmark = ' (***)'; // mark to add to non-existing filenames in selection form
21 $fileoldmark = ' (old?)'; // mark to add to filenames in selection form id english version is newer
22 // or to filenames with filesize() == 0
23 $filetemplate = ''; // template for new files, i.e. '$Id$';
25 $currentfile = optional_param('currentfile', 'docs/README.txt', PARAM_PATH
);
30 error("You need to be admin to edit this page");
33 if (! $site = get_site()) {
34 error("Site not defined!");
37 $stradministration = get_string("administration");
38 $strconfiguration = get_string("configuration");
39 $strlanguage = get_string("language");
40 $strcurrentlanguage = get_string("currentlanguage");
41 $strthislanguage = get_string("thislanguage");
42 $stredithelpdocs = get_string('edithelpdocs', 'admin');
44 print_header("$site->shortname: $stredithelpdocs: $currentfile", "$site->fullname",
45 "<a href=\"index.php\">$stradministration</a> -> ".
46 "<a href=\"configure.php\">$strconfiguration</a> ->
47 <a href=\"lang.php\">$strlanguage</a> -> $stredithelpdocs",
48 'choosefile.popup', '', true);
50 $currentlang = current_language();
51 $langdir = "$CFG->dataroot/lang/$currentlang";
52 $enlangdir = "$CFG->dirroot/lang/en_utf8";
55 if (!file_exists($langdir)) {
56 error ('to edit this language pack, you need to put it in '.$CFG->dataroot
.'/lang');
58 // Shall I save POSTed data?
60 if (isset($_POST['currentfile'])) {
61 if (confirm_sesskey()) {
62 if (langdoc_save_file($langdir, $currentfile, $_POST['filedata'])) {
63 notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
65 error("Could not save the file '$currentfile'!", "langdoc.php?currentfile=$currentfile&sesskey=$USER->sesskey");
70 error_reporting(0); // Error reporting turned off due to non-existing files
72 // Generate selection for all help and documentation files
74 // Get all files from /docs directory
76 if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/docs", "CVS")) {
77 error("Could not find English language docs files!");
82 foreach ($files as $filekey => $file) { // check all the docs files.
83 $options["docs/$file"] = "docs/$file";
84 // add mark if file doesn't exist or is empty
85 if (( !file_exists("$langdir/docs/$file")) ||
(filesize("$langdir/docs/$file") == 0)) {
86 $options["docs/$file"] .= "$filemissingmark";
88 if (filemtime("$langdir/docs/$file") < filemtime("$CFG->dirroot/lang/en_utf8/docs/$file")) {
89 $options["docs/$file"] .= "$fileoldmark";
94 // Get all files from /help directory
96 if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/help", "CVS")) {
97 error("Could not find English language help files!");
100 foreach ($files as $filekey => $file) { // check all the help files.
101 $options["help/$file"] = "help/$file";
102 if (( !file_exists("$langdir/help/$file")) ||
(filesize("$CFG->dirroot/lang/en_utf8/help/$file") == 0)) {
103 $options["help/$file"] .= "$filemissingmark";
105 if (filemtime("$langdir/help/$file") < filemtime("$langdir/help/$file")) {
106 $options["help/$file"] .= "$fileoldmark";
111 echo "<table align=\"center\"><tr><td align=\"center\">";
112 echo popup_form ("$CFG->wwwroot/$CFG->admin/langdoc.php?sesskey=$USER->sesskey&currentfile=", $options, "choosefile", $currentfile, "", "", "", true);
113 echo "</td></tr></table>";
115 // Generate textareas
117 if (!empty($currentfile)) {
119 if (!file_exists("$langdir/$currentfile")) {
120 //check if directory exist
121 $pathparts = explode('/',$currentfile);
122 $checkpath = $langdir;
123 for ($a=0; $a < count($pathparts)-1 ; $a++
) {
124 $checkpath .= "/".$pathparts[$a];
125 if(!file_exists($checkpath)){
126 if(!mkdir($checkpath)){
127 echo ("Cannot create directory: $checkpath");
132 // file doesn't exist - let's check webserver's permission to create it
134 if (!touch("$langdir/$currentfile")) {
136 // webserver is unable to create new file
138 echo "<p align=\"center\"><font color=red>".get_string("filemissing", "", "
139 $langdir/$currentfile")."</font></p>";
143 // webserver can create new file - we can delete it now and let
144 // the langdoc_save_file() create it again if its filesize() > 0
147 unlink("$langdir/$currentfile");
149 } elseif ($f = fopen("$langdir/$currentfile","r+")) {
151 // file exists and is writeable - good for you, translator ;-)
157 // file exists but it is not writeable by web server process :-(
160 echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")
164 //en_utf8 in dataroot is not editable
165 if ($currentlang == 'en_utf8') {
169 echo "<table align=\"center\"><tr valign=\"center\"><td align=\"center\">\n";
170 echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"\">\n";
171 echo htmlspecialchars(file_get_contents("$enlangdir/$currentfile"));
172 echo "</textarea>\n";
173 //link_to_popup_window("/lang/en_utf8/$currentfile", "popup", get_string("preview"));
174 link_to_popup_window(help_preview_url($currentfile).'&forcelang=en_utf8', 'popup', get_string('preview'));
176 if ($fileeditorinline == 1) {
177 echo "</tr>\n<tr valign=\"center\">\n";
179 echo "<td align=\"center\">\n";
182 echo "<form name=\"$currentfile\" action=\"langdoc.php\" method=\"post\">";
183 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
184 echo '<input type="hidden" name="currentfile" value="'.$currentfile.'" />';
187 echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"filedata\">\n";
188 if (file_exists("$langdir/$currentfile")) {
189 echo htmlspecialchars(file_get_contents("$langdir/$currentfile"));
191 echo ($filetemplate);
193 echo "</textarea>\n";
194 link_to_popup_window(help_preview_url($currentfile), 'popup', get_string('preview'));
195 echo "</td>\n</tr>\n</table>";
198 echo '<div align="center"><input type="submit" value="'.get_string('savechanges').': lang/'.$currentlang.'/'.$currentfile.'" /></div>';
202 error_reporting($CFG->debug
);
207 //////////////////////////////////////////////////////////////////////
209 function langdoc_save_file($path, $file, $content) {
211 // $path is a full pathname to the file
212 // $file is the file to overwrite.
213 // $content are data to write
219 if (!$f = fopen("$path/$file","w")) {
220 error_reporting($CFG->debug
);
224 error_reporting($CFG->debug
);
226 $content = str_replace("\r", "",$content); // Remove linefeed characters
227 $content = preg_replace("/\n{3,}/", "\n\n", $content); // Collapse runs of blank lines
228 $content = trim($content); // Delete leading/trailing whitespace
230 fwrite($f, stripslashes($content));
234 // Remove file if its empty
236 if (filesize("$path/$file") == 0) {
237 unlink("$path/$file");
244 function help_preview_url($currentfile) {
245 if (substr($currentfile, 0, 5) == 'help/') {
246 $currentfile = substr($currentfile, 5);
248 $currentpathexp = explode('/', $currentfile);
249 if (count($currentpathexp) > 1) {
250 $url = '/help.php?module='.$currentpathexp[0].'&file='.$currentpathexp[1];
252 $url = '/help.php?module=moodle&file='.$currentfile;