Petr's patch for MDL-7327 to modify require_capability login a bit
[moodle.git] / admin / langdoc.php
blobd08476c2f91ae377bfbb595c95260232b9c11ae2
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 $adminroot = admin_get_root();
15 admin_externalpage_setup('langedit', $adminroot);
18 // Some local configuration
20 $fileeditorrows = 12; // number of textareas' rows
21 $fileeditorcols = 100; // dtto cols
22 $fileeditorinline = 1; // shall be textareas put in one row?
23 $filemissingmark = ' (***)'; // mark to add to non-existing filenames in selection form
24 $fileoldmark = ' (old?)'; // mark to add to filenames in selection form id english version is newer
25 // or to filenames with filesize() == 0
26 $filetemplate = ''; // template for new files, i.e. '$Id$';
28 $currentfile = optional_param('currentfile', 'docs/README.txt', PARAM_PATH);
30 $strlanguage = get_string("language");
31 $strcurrentlanguage = get_string("currentlanguage");
32 $strthislanguage = get_string("thislanguage");
33 $stredithelpdocs = get_string('edithelpdocs', 'admin');
35 admin_externalpage_print_header($adminroot);
37 $currentlang = current_language();
38 $langdir = "$CFG->dataroot/lang/$currentlang";
39 $enlangdir = "$CFG->dirroot/lang/en_utf8";
42 if (!file_exists($langdir)) {
43 error ('to edit this language pack, you need to put it in '.$CFG->dataroot.'/lang');
45 // Shall I save POSTed data?
47 if (isset($_POST['currentfile'])) {
48 if (confirm_sesskey()) {
49 if (langdoc_save_file($langdir, $currentfile, $_POST['filedata'])) {
50 notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
51 } else {
52 error("Could not save the file '$currentfile'!", "langdoc.php?currentfile=$currentfile&sesskey=$USER->sesskey");
57 error_reporting(0); // Error reporting turned off due to non-existing files
59 // Generate selection for all help and documentation files
61 // Get all files from /docs directory
63 if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/docs", "CVS")) {
64 error("Could not find English language docs files!");
67 $options = array();
69 foreach ($files as $filekey => $file) { // check all the docs files.
70 $options["docs/$file"] = "docs/$file";
71 // add mark if file doesn't exist or is empty
72 if (( !file_exists("$langdir/docs/$file")) || (filesize("$langdir/docs/$file") == 0)) {
73 $options["docs/$file"] .= "$filemissingmark";
74 } else {
75 if (filemtime("$langdir/docs/$file") < filemtime("$CFG->dirroot/lang/en_utf8/docs/$file")) {
76 $options["docs/$file"] .= "$fileoldmark";
81 // Get all files from /help directory
83 if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/help", "CVS")) {
84 error("Could not find English language help files!");
87 foreach ($files as $filekey => $file) { // check all the help files.
88 $options["help/$file"] = "help/$file";
89 if (( !file_exists("$langdir/help/$file")) || (filesize("$CFG->dirroot/lang/en_utf8/help/$file") == 0)) {
90 $options["help/$file"] .= "$filemissingmark";
91 } else {
92 if (filemtime("$langdir/help/$file") < filemtime("$langdir/help/$file")) {
93 $options["help/$file"] .= "$fileoldmark";
98 echo "<table align=\"center\"><tr><td align=\"center\">";
99 echo popup_form ("$CFG->wwwroot/$CFG->admin/langdoc.php?sesskey=$USER->sesskey&amp;currentfile=", $options, "choosefile", $currentfile, "", "", "", true);
100 echo "</td></tr></table>";
102 // Generate textareas
104 if (!empty($currentfile)) {
106 if (!file_exists("$langdir/$currentfile")) {
107 //check if directory exist
108 $pathparts = explode('/',$currentfile);
109 $checkpath = $langdir;
110 for ($a=0; $a < count($pathparts)-1 ; $a++) {
111 $checkpath .= "/".$pathparts[$a];
112 if(!file_exists($checkpath)){
113 if(!mkdir($checkpath)){
114 echo ("Cannot create directory: $checkpath");
119 // file doesn't exist - let's check webserver's permission to create it
121 if (!touch("$langdir/$currentfile")) {
123 // webserver is unable to create new file
125 echo "<p align=\"center\"><font color=red>".get_string("filemissing", "", "
126 $langdir/$currentfile")."</font></p>";
127 $editable = false;
128 } else {
130 // webserver can create new file - we can delete it now and let
131 // the langdoc_save_file() create it again if its filesize() > 0
133 $editable = true;
134 unlink("$langdir/$currentfile");
136 } elseif ($f = fopen("$langdir/$currentfile","r+")) {
138 // file exists and is writeable - good for you, translator ;-)
140 $editable = true;
141 fclose($f);
142 } else {
144 // file exists but it is not writeable by web server process :-(
146 $editable = false;
147 echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")
148 ."</font></p>";
151 //en_utf8 in dataroot is not editable
152 if ($currentlang == 'en_utf8') {
153 $editable = false;
156 echo "<table align=\"center\"><tr valign=\"center\"><td align=\"center\">\n";
157 echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"\">\n";
158 echo htmlspecialchars(file_get_contents("$enlangdir/$currentfile"));
159 echo "</textarea>\n";
160 //link_to_popup_window("/lang/en_utf8/$currentfile", "popup", get_string("preview"));
161 $preview_url = langdoc_preview_url($currentfile);
162 if ($preview_url) {
163 link_to_popup_window($preview_url.'&amp;forcelang=en_utf8', 'popup', get_string('preview'));
165 echo "</td>\n";
166 if ($fileeditorinline == 1) {
167 echo "</tr>\n<tr valign=\"center\">\n";
169 echo "<td align=\"center\">\n";
171 if ($editable) {
172 echo "<form name=\"$currentfile\" action=\"langdoc.php\" method=\"post\">";
173 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
174 echo '<input type="hidden" name="currentfile" value="'.$currentfile.'" />';
176 echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"filedata\">\n";
177 if (file_exists("$langdir/$currentfile")) {
178 echo htmlspecialchars(file_get_contents("$langdir/$currentfile"));
179 } else {
180 echo ($filetemplate);
182 echo "</textarea>\n";
183 $preview_url = langdoc_preview_url($currentfile);
184 if ($preview_url) {
185 link_to_popup_window($preview_url, 'popup', get_string('preview'));
187 echo '<div align="center"><input type="submit" value="'.get_string('savechanges').': lang/'.$currentlang.'/'.$currentfile.'" /></div>';
188 echo '</form>';
191 echo "</td>\n</tr>\n</table>";
194 error_reporting($CFG->debug);
197 admin_externalpage_print_footer($adminroot);
199 //////////////////////////////////////////////////////////////////////
201 function langdoc_save_file($path, $file, $content) {
203 // $path is a full pathname to the file
204 // $file is the file to overwrite.
205 // $content are data to write
207 global $CFG, $USER;
209 error_reporting(0);
211 if (!$f = fopen("$path/$file","w")) {
212 error_reporting($CFG->debug);
213 return false;
216 error_reporting($CFG->debug);
218 $content = str_replace("\r", "",$content); // Remove linefeed characters
219 $content = preg_replace("/\n{3,}/", "\n\n", $content); // Collapse runs of blank lines
220 $content = trim($content); // Delete leading/trailing whitespace
222 fwrite($f, stripslashes($content));
224 fclose($f);
226 // Remove file if its empty
228 if (filesize("$path/$file") == 0) {
229 unlink("$path/$file");
232 return true;
236 * Return a preview URL for the file, if available.
238 * Documentation will be moved into moodle.org wiki and current version 1.6 does not
239 * seem to be able to display local documentation. Thus, return empty URL for doc files.
240 * See lib/moodlelib.php document_file() - it still relies on old pre-UTF8 lang/ location.
242 function langdoc_preview_url($currentfile) {
243 if (substr($currentfile, 0, 5) == 'help/') {
244 $currentfile = substr($currentfile, 5);
245 $currentpathexp = explode('/', $currentfile);
246 if (count($currentpathexp) > 1) {
247 $url = '/help.php?module='.$currentpathexp[0].'&amp;file='.$currentpathexp[1];
248 } else {
249 $url = '/help.php?module=moodle&amp;file='.$currentfile;
251 } else {
252 $url = '';
254 return $url;