Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / admin / langdoc.php
blob660ddeb947d9a197a9516405aee338cad170942c
1 <?php // $Id$
3 /**
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');
13 require_once($CFG->libdir.'/adminlib.php');
14 admin_externalpage_setup('langedit');
17 // Some local configuration
19 $fileeditorrows = 12; // number of textareas' rows
20 $fileeditorcols = 100; // dtto cols
21 $fileeditorinline = 1; // shall be textareas put in one row?
22 $filemissingmark = ' (***)'; // mark to add to non-existing filenames in selection form
23 $fileoldmark = ' (old?)'; // mark to add to filenames in selection form id english version is newer
24 // or to filenames with filesize() == 0
25 $filetemplate = ''; // template for new files, i.e. '$Id$';
27 $currentfile = optional_param('currentfile', 'docs/README.txt', PARAM_PATH);
29 $strlanguage = get_string("language");
30 $strcurrentlanguage = get_string("currentlanguage");
31 $strthislanguage = get_string("thislanguage");
32 $stredithelpdocs = get_string('edithelpdocs', 'admin');
34 admin_externalpage_print_header();
36 notify('NOTICE: This interface is obsolete now and will be removed. You should use
37 improved <a href="lang.php?mode=helpfiles">lang.php</a> interface.');
39 $currentlang = current_language();
40 $langdir = "$CFG->dataroot/lang/$currentlang";
41 $enlangdir = "$CFG->dirroot/lang/en_utf8";
44 if (!file_exists($langdir)) {
45 error ('to edit this language pack, you need to put it in '.$CFG->dataroot.'/lang');
47 // Shall I save POSTed data?
49 if (isset($_POST['currentfile'])) {
50 if (confirm_sesskey()) {
51 if (langdoc_save_file($langdir, $currentfile, $_POST['filedata'])) {
52 notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
53 } else {
54 error("Could not save the file '$currentfile'!", "langdoc.php?currentfile=$currentfile&sesskey=$USER->sesskey");
59 error_reporting(0); // Error reporting turned off due to non-existing files
61 // Generate selection for all help and documentation files
63 // Get all files from /docs directory
65 if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/docs", "CVS")) {
66 error("Could not find English language docs files!");
69 $options = array();
71 foreach ($files as $filekey => $file) { // check all the docs files.
72 $options["docs/$file"] = "docs/$file";
73 // add mark if file doesn't exist or is empty
74 if (( !file_exists("$langdir/docs/$file")) || (filesize("$langdir/docs/$file") == 0)) {
75 $options["docs/$file"] .= "$filemissingmark";
76 } else {
77 if (filemtime("$langdir/docs/$file") < filemtime("$CFG->dirroot/lang/en_utf8/docs/$file")) {
78 $options["docs/$file"] .= "$fileoldmark";
83 // Get all files from /help directory
85 if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/help", "CVS")) {
86 error("Could not find English language help files!");
89 foreach ($files as $filekey => $file) { // check all the help files.
90 $options["help/$file"] = "help/$file";
91 if (( !file_exists("$langdir/help/$file")) || (filesize("$CFG->dirroot/lang/en_utf8/help/$file") == 0)) {
92 $options["help/$file"] .= "$filemissingmark";
93 } else {
94 if (filemtime("$langdir/help/$file") < filemtime("$langdir/help/$file")) {
95 $options["help/$file"] .= "$fileoldmark";
100 echo "<table align=\"center\"><tr><td align=\"center\">";
101 echo popup_form ("$CFG->wwwroot/$CFG->admin/langdoc.php?sesskey=$USER->sesskey&amp;currentfile=", $options, "choosefile", $currentfile, "", "", "", true);
102 echo "</td></tr></table>";
104 // Generate textareas
106 if (!empty($currentfile)) {
108 if (!file_exists("$langdir/$currentfile")) {
109 //check if directory exist
110 $pathparts = explode('/',$currentfile);
111 $checkpath = $langdir;
112 for ($a=0; $a < count($pathparts)-1 ; $a++) {
113 $checkpath .= "/".$pathparts[$a];
114 if(!file_exists($checkpath)){
115 if(!mkdir($checkpath, $CFG->directorypermissions)){
116 echo ("Cannot create directory: $checkpath");
121 // file doesn't exist - let's check webserver's permission to create it
123 if (!touch("$langdir/$currentfile")) {
125 // webserver is unable to create new file
127 echo "<p align=\"center\"><font color=red>".get_string("filemissing", "", "
128 $langdir/$currentfile")."</font></p>";
129 $editable = false;
130 } else {
132 // webserver can create new file - we can delete it now and let
133 // the langdoc_save_file() create it again if its filesize() > 0
135 $editable = true;
136 unlink("$langdir/$currentfile");
138 } elseif ($f = fopen("$langdir/$currentfile","r+")) {
140 // file exists and is writeable - good for you, translator ;-)
142 $editable = true;
143 fclose($f);
144 } else {
146 // file exists but it is not writeable by web server process :-(
148 $editable = false;
149 echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")
150 ."</font></p>";
153 //en_utf8 in dataroot is not editable
154 if ($currentlang == 'en_utf8') {
155 $editable = false;
158 echo "<table align=\"center\"><tr valign=\"center\"><td align=\"center\">\n";
159 echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"\">\n";
160 echo htmlspecialchars(file_get_contents("$enlangdir/$currentfile"));
161 echo "</textarea>\n";
162 //link_to_popup_window("/lang/en_utf8/$currentfile", "popup", get_string("preview"));
163 $preview_url = langdoc_preview_url($currentfile);
164 if ($preview_url) {
165 link_to_popup_window($preview_url.'&amp;forcelang=en_utf8', 'popup', get_string('preview'));
167 echo "</td>\n";
168 if ($fileeditorinline == 1) {
169 echo "</tr>\n<tr valign=\"center\">\n";
171 echo "<td align=\"center\">\n";
173 if ($editable) {
174 echo "<form id=\"$currentfile\" action=\"langdoc.php\" method=\"post\">";
175 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
176 echo '<input type="hidden" name="currentfile" value="'.$currentfile.'" />';
178 echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"filedata\">\n";
179 if (file_exists("$langdir/$currentfile")) {
180 echo htmlspecialchars(file_get_contents("$langdir/$currentfile"));
181 } else {
182 echo ($filetemplate);
184 echo "</textarea>\n";
185 $preview_url = langdoc_preview_url($currentfile);
186 if ($preview_url) {
187 link_to_popup_window($preview_url, 'popup', get_string('preview'));
189 echo '<div class="mdl-align"><input type="submit" value="'.get_string('savechanges').': lang/'.$currentlang.'/'.$currentfile.'" /></div>';
190 echo '</form>';
193 echo "</td>\n</tr>\n</table>";
196 error_reporting($CFG->debug);
199 admin_externalpage_print_footer();
201 //////////////////////////////////////////////////////////////////////
203 function langdoc_save_file($path, $file, $content) {
205 // $path is a full pathname to the file
206 // $file is the file to overwrite.
207 // $content are data to write
209 global $CFG, $USER;
211 error_reporting(0);
213 if (!$f = fopen("$path/$file","w")) {
214 error_reporting($CFG->debug);
215 return false;
218 error_reporting($CFG->debug);
220 $content = str_replace("\r", "",$content); // Remove linefeed characters
221 $content = preg_replace("/\n{3,}/", "\n\n", $content); // Collapse runs of blank lines
222 $content = trim($content); // Delete leading/trailing whitespace
224 fwrite($f, stripslashes($content));
226 fclose($f);
228 // Remove file if its empty
230 if (filesize("$path/$file") == 0) {
231 unlink("$path/$file");
234 return true;
238 * Return a preview URL for the file, if available.
240 * Documentation will be moved into moodle.org wiki and current version 1.6 does not
241 * seem to be able to display local documentation. Thus, return empty URL for doc files.
242 * See lib/moodlelib.php document_file() - it still relies on old pre-UTF8 lang/ location.
244 function langdoc_preview_url($currentfile) {
245 if (substr($currentfile, 0, 5) == 'help/') {
246 $currentfile = substr($currentfile, 5);
247 $currentpathexp = explode('/', $currentfile);
248 if (count($currentpathexp) > 1) {
249 $url = '/help.php?module='.implode('/',array_slice($currentpathexp,0,count($currentpathexp)-1)).'&amp;file='.end($currentpathexp);
250 } else {
251 $url = '/help.php?module=moodle&amp;file='.$currentfile;
253 } else {
254 $url = '';
256 return $url;