File: langdoc.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (257 lines) | stat: -rwxr-xr-x 9,626 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php // $Id: langdoc.php,v 1.12.2.1 2006/05/17 16:37:15 tjhunt Exp $

/**
This script enables Moodle translators to edit /docs and /help language
files directly via WWW interface.

Author:     mudrd8mz@uxit.pedf.cuni.cz (http://moodle.cz)
Based on:   lang.php in 1.4.3+ release
Thanks:     Jaime Villate for important bug fixing, koen roggemans for his job and all moodlers
            for intensive testing of this my first contribution
*/
    require_once('../config.php');

    //
    // Some local configuration
    //
    $fileeditorrows = 12;           // number of textareas' rows
    $fileeditorcols = 100;          // dtto cols
    $fileeditorinline = 1;          // shall be textareas put in one row?
    $filemissingmark = ' (***)';    // mark to add to non-existing filenames in selection form
    $fileoldmark = ' (old?)';       // mark to add to filenames in selection form id english version is newer
                                    // or to filenames with filesize() == 0
    $filetemplate = '';             // template for new files, i.e. '$Id: langdoc.php,v 1.12.2.1 2006/05/17 16:37:15 tjhunt Exp $';

    $currentfile = optional_param('currentfile', 'docs/README.txt', PARAM_PATH); 

    require_login();

    if (!isadmin()) {
        error("You need to be admin to edit this page");
    }     

    if (! $site = get_site()) {
        error("Site not defined!");
    }

    $stradministration = get_string("administration");
    $strconfiguration = get_string("configuration");
    $strlanguage = get_string("language");
    $strcurrentlanguage = get_string("currentlanguage");
    $strthislanguage = get_string("thislanguage");
    $stredithelpdocs = get_string('edithelpdocs', 'admin');

    print_header("$site->shortname: $stredithelpdocs: $currentfile", "$site->fullname",
                 "<a href=\"index.php\">$stradministration</a> -> ".
                 "<a href=\"configure.php\">$strconfiguration</a> -> 
                  <a href=\"lang.php\">$strlanguage</a> -> $stredithelpdocs",
                 'choosefile.popup', '', true);

    $currentlang = current_language();
    $langdir = "$CFG->dataroot/lang/$currentlang";
    $enlangdir = "$CFG->dirroot/lang/en_utf8";


    if (!file_exists($langdir)) {
        error ('to edit this language pack, you need to put it in '.$CFG->dataroot.'/lang');
    }
    // Shall I save POSTed data?

    if (isset($_POST['currentfile'])) {
        if (confirm_sesskey()) {
            if (langdoc_save_file($langdir, $currentfile, $_POST['filedata'])) {
                notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
            } else {
                error("Could not save the file '$currentfile'!", "langdoc.php?currentfile=$currentfile&sesskey=$USER->sesskey");
            }
        }
    }

    error_reporting(0); // Error reporting turned off due to non-existing files

    // Generate selection for all help and documentation files

    // Get all files from /docs directory

    if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/docs", "CVS")) {
        error("Could not find English language docs files!");
    }

    $options = array();

    foreach ($files as $filekey => $file) {    // check all the docs files.
        $options["docs/$file"] = "docs/$file";
        // add mark if file doesn't exist or is empty
        if (( !file_exists("$langdir/docs/$file")) || (filesize("$langdir/docs/$file") == 0)) {
            $options["docs/$file"] .= "$filemissingmark";
        } else {       
            if (filemtime("$langdir/docs/$file") < filemtime("$CFG->dirroot/lang/en_utf8/docs/$file")) {
                $options["docs/$file"] .= "$fileoldmark";
            }
        }    
    }

    // Get all files from /help directory

    if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/help", "CVS")) {
        error("Could not find English language help files!");
    }

    foreach ($files as $filekey => $file) {    // check all the help files.
        $options["help/$file"] = "help/$file";
        if (( !file_exists("$langdir/help/$file")) || (filesize("$CFG->dirroot/lang/en_utf8/help/$file") == 0)) {
            $options["help/$file"] .= "$filemissingmark";
        } else {
            if (filemtime("$langdir/help/$file") < filemtime("$langdir/help/$file")) {
                $options["help/$file"] .= "$fileoldmark";
            }
        }    
    }

    echo "<table align=\"center\"><tr><td align=\"center\">";
    echo popup_form ("$CFG->wwwroot/$CFG->admin/langdoc.php?sesskey=$USER->sesskey&amp;currentfile=", $options, "choosefile", $currentfile, "", "", "", true);
    echo "</td></tr></table>";

    // Generate textareas

    if (!empty($currentfile)) {

        if (!file_exists("$langdir/$currentfile")) {
            //check if directory exist
            $pathparts = explode('/',$currentfile);
            $checkpath = $langdir;
            for ($a=0; $a < count($pathparts)-1 ; $a++) {
                $checkpath .= "/".$pathparts[$a];
                if(!file_exists($checkpath)){
                     if(!mkdir($checkpath)){
                         echo ("Cannot create directory: $checkpath");
                     }    
                }    
            }
            //
            // file doesn't exist - let's check webserver's permission to create it
            //
            if (!touch("$langdir/$currentfile")) {
                //
                // webserver is unable to create new file
                //
                echo "<p align=\"center\"><font color=red>".get_string("filemissing", "", "
$langdir/$currentfile")."</font></p>";
                $editable = false;
            } else {
                //
                // webserver can create new file - we can delete it now and let
                // the langdoc_save_file() create it again if its filesize() > 0
                //
                $editable = true;
                unlink("$langdir/$currentfile");
            }
        } elseif ($f = fopen("$langdir/$currentfile","r+")) {
            //
            // file exists and is writeable - good for you, translator ;-)
            //
            $editable = true;
            fclose($f);
        } else {
            //
            // file exists but it is not writeable by web server process :-(
            //
            $editable = false;
            echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")
."</font></p>";
        }

        //en_utf8 in dataroot is not editable
        if ($currentlang == 'en_utf8') {
            $editable = false;
        }

        echo "<table align=\"center\"><tr valign=\"center\"><td align=\"center\">\n";
        echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"\">\n";
        echo htmlspecialchars(file_get_contents("$enlangdir/$currentfile"));
        echo "</textarea>\n";
        //link_to_popup_window("/lang/en_utf8/$currentfile", "popup", get_string("preview"));
        link_to_popup_window(help_preview_url($currentfile).'&amp;forcelang=en_utf8', 'popup', get_string('preview'));
        echo "</td>\n";
        if ($fileeditorinline == 1) {
            echo "</tr>\n<tr valign=\"center\">\n";
        }
        echo "<td align=\"center\">\n";

        if ($editable) {
            echo "<form name=\"$currentfile\" action=\"langdoc.php\" method=\"post\">";
            echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
            echo '<input type="hidden" name="currentfile" value="'.$currentfile.'" />';
        }

        echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"filedata\">\n";
        if (file_exists("$langdir/$currentfile")) {
            echo htmlspecialchars(file_get_contents("$langdir/$currentfile"));
        } else {
            echo ($filetemplate);
        }
        echo "</textarea>\n";
        link_to_popup_window(help_preview_url($currentfile), 'popup', get_string('preview'));
        echo "</td>\n</tr>\n</table>";

        if ($editable) {
            echo '<div align="center"><input type="submit" value="'.get_string('savechanges').': lang/'.$currentlang.'/'.$currentfile.'" /></div>';
            echo '</form>';
        }

        error_reporting($CFG->debug);
    }

    print_footer();

//////////////////////////////////////////////////////////////////////

function langdoc_save_file($path, $file, $content) {

// $path is a full pathname to the file
// $file is the file to overwrite.
// $content are data to write

    global $CFG, $USER;

    error_reporting(0);
    
    if (!$f = fopen("$path/$file","w")) {
        error_reporting($CFG->debug);
        return false;
    }
    
    error_reporting($CFG->debug);

    $content = str_replace("\r", "",$content);              // Remove linefeed characters
    $content = preg_replace("/\n{3,}/", "\n\n", $content);  // Collapse runs of blank lines
    $content = trim($content);                              // Delete leading/trailing whitespace
        
    fwrite($f, stripslashes($content));

    fclose($f);

    // Remove file if its empty

    if (filesize("$path/$file") == 0) {
        unlink("$path/$file");
    }

    return true;
}


function help_preview_url($currentfile) {
    if (substr($currentfile, 0, 5) == 'help/') {
        $currentfile = substr($currentfile, 5);
    }
    $currentpathexp = explode('/', $currentfile);
    if (count($currentpathexp) > 1) {
        $url = '/help.php?module='.$currentpathexp[0].'&amp;file='.$currentpathexp[1];
    } else {
        $url = '/help.php?module=moodle&amp;file='.$currentfile;
    }
    return $url;
}

?>