Added new strings for question grade matching
[moodle-linuxchix.git] / admin / langimport.php
blobd2039975406a3daa6f1a3c14bdf06b684daf5f14
1 <?php //$Id$
2 ///This file only manages the installation of 1.6 lang packs.
3 ///in downloads.moodle.org, they are store in separate directory /lang16
4 ///in local server, they are stored in $CFG->dataroot/lang
5 ///This helps to avoid confusion.
7 require_once('../config.php');
9 $mode = optional_param('mode', 0, PARAM_INT); //phase
10 $pack = optional_param('pack', '', PARAM_FILE); //pack to install
11 $displaylang = $pack;
12 $uninstalllang = optional_param('uninstalllang', '', PARAM_FILE);
13 $confirm = optional_param('confirm', 0, PARAM_BOOL);
14 $sitelang = optional_param('sitelangconfig', '', PARAM_FILE);
16 define('INSTALLATION_OF_SELECTED_LANG', 2);
17 define('CHANGE_SITE_LANG', 3);
18 define('DELETION_OF_SELECTED_LANG', 4);
19 define('UPDATE_ALL_LANG', 5);
21 require_login();
23 if (!isadmin()) {
24 error('You must be an admin');
27 if (!$site = get_site()) {
28 error("Site not defined!");
31 $strlang = get_string('langimport','admin');
33 $stradministration = get_string("administration");
34 $strconfiguration = get_string("configuration");
35 $strlanguage = get_string("language");
36 $strthislanguage = get_string("thislanguage");
37 $title = $strlang;
39 print_header("$site->shortname: $title", "$site->fullname",
40 "<a href=\"index.php\">$stradministration</a> -> ".
41 "<a href=\"configure.php\">$strconfiguration</a> -> ".
42 "<a href=\"lang.php\">$strlanguage</a> -> $strlang",
43 '', '', true, '');
45 print_heading('');
47 switch ($mode){
49 case INSTALLATION_OF_SELECTED_LANG: ///installation of selected language pack
51 if (confirm_sesskey()) {
52 if ($confirm) {
53 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
54 @mkdir ($CFG->dataroot.'/lang/');
56 require_once($CFG->libdir.'/componentlib.class.php');
57 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
58 $pack.'.zip', 'languages.md5', 'lang')) {
59 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
60 switch ($status) {
62 case ERROR:
63 if ($cd->get_error() == 'remotedownloadnotallowed') {
64 $a = new stdClass();
65 $a->url = 'http://download.moodle.org/lang16/'.$pack.'zip';
66 $a->dest= $CFG->dataroot.'/lang';
67 error(get_string($cd->get_error(), 'error', $a));
68 } else {
69 error(get_string($cd->get_error(), 'error'));
71 break;
72 case UPTODATE:
74 break;
75 case INSTALLED:
76 print_string('langpackupdated','admin',$pack);
77 @unlink($CFG->dataroot.'/cache/languages');
78 print_continue('langimport.php');
79 break;
80 default:
81 //We shouldn't reach this point
83 } else {
84 //We shouldn't reach this point
87 } else { //print confirm box, no confirmation yet
88 if (confirm_sesskey()) {
89 print_simple_box_start('center','100%');
90 echo '<div align="center">';
91 echo '<form name="langform" action="langimport.php?mode=2" method="POST">';
92 echo '<input name="pack" type="hidden" value="'.$pack.'" />';
93 echo '<input name="displaylang" type="hidden" value="'.$displaylang.'" />';
94 echo '<input name="confirm" type="hidden" value="1" />';
95 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
96 print_heading(get_string('confirminstall','admin',$displaylang),2);
97 echo '<input type="submit" value="'.get_string('ok').'"/>';
98 echo '&nbsp;<input type="button" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
99 echo '</form>';
100 echo '</div>';
101 print_simple_box_end();
105 break;
107 case CHANGE_SITE_LANG: //change site language
109 if (confirm_sesskey()) {
110 $langconfig = get_record('config','name','lang');
111 $langconfig->value = $sitelang;
112 if (!empty($sitelang) && update_record('config',$langconfig)){
113 echo '<div align="center">';
114 notify (get_string('sitelangchanged','admin'));
115 echo '<form action="langimport.php" method="POST">';
116 echo '<input type="submit" value="'.get_string('ok').'" />';
117 echo '</form></div>';
118 } else {
119 error ('can not update site language');
123 break;
124 case DELETION_OF_SELECTED_LANG: //delete a directory(ies) containing a lang pack completely
126 if (!$confirm && confirm_sesskey()) {
127 print_simple_box_start('center','100%');
128 echo '<div align="center">';
129 echo '<form name="langform" action="langimport.php?mode=4" method="POST">';
130 echo '<input name="uninstalllang" type="hidden" value="'.$uninstalllang.'" />';
131 echo '<input name="confirm" type="hidden" value="1" />';
132 print_heading(get_string('uninstallconfirm','admin',$uninstalllang),2);
133 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
134 echo '<input type="submit" value="'.get_string('uninstall','admin').'"/>';
135 echo '&nbsp;<input type="button" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
136 echo '</form>';
137 echo '</div>';
138 print_simple_box_end();
140 else if (confirm_sesskey()) {
141 if ($uninstalllang == 'en_utf8') {
142 error ('en_utf8 can not be uninstalled!');
144 $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
145 $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
146 $rm1 = false;
147 $rm2 = false;
148 if (file_exists($dest1)){
149 $rm1 = remove_dir($dest1);
151 if (file_exists($dest2)){
152 $rm2 = remove_dir($dest2);
154 //delete the direcotries
155 if ($rm1 or $rm2) {
156 echo '<div align="center">';
157 print_string('langpackremoved','admin');
158 echo '<form action="langimport.php" method="POST">';
159 echo '<input type="submit" value="'.get_string('ok').'" />';
160 echo '</form></div>';
161 } else { //nothing deleted, possibly due to permission error
162 error ('An error has occurred, language pack is not completely uninstalled, please check file permission');
165 @unlink($CFG->dataroot.'/cache/languages');
166 break;
168 case UPDATE_ALL_LANG: //1 click update for all updatable language packs
170 //0th pull a list from download.moodle.org,
171 //key = langname, value = md5
172 $source = 'http://download.moodle.org/lang16/languages.md5';
173 $md5array = array();
174 $updated = 0; //any packs updated?
175 $alllangs = array_keys(get_list_of_languages());
176 $lang16 = array(); //all the Moodle 1.6 unicode lang packs (updated and not updated)
177 $packs = array(); //all the packs that needs updating
180 if (!$availablelangs = proxy_url($source)) {
181 error ('can not read from course');
184 //and build an associative array
185 foreach ($availablelangs as $alang) {
186 $md5array[$alang[0]] = $alang[1];
189 //filtering out non-16 packs
190 foreach ($alllangs as $clang) {
191 $dest1 = $CFG->dataroot.'/lang/'.$clang;
192 $dest2 = $CFG->dirroot.'/lang/'.$clang;
194 if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){
195 $lang16[] = $clang;
199 //then filter out packs that have the same md5 key
200 foreach ($lang16 as $clang) {
201 if (!is_installed_lang($clang, $md5array[$clang])){
202 $packs[] = $clang;
206 @mkdir ($CFG->dataroot.'/temp/');
207 @mkdir ($CFG->dataroot.'/lang/');
208 foreach ($packs as $pack){ //for each of the remaining in the list, we
209 if ($pack == 'en_utf8') { // no update for en_utf8
210 continue;
212 //1. delete old director(ies)
214 $dest1 = $CFG->dataroot.'/lang/'.$pack;
215 $dest2 = $CFG->dirroot.'/lang/'.$pack;
216 $rm1 = false;
217 $rm2 = false;
218 if (file_exists($dest1)) {
219 $rm1 = remove_dir($dest1);
221 if (file_exists($dest2)) {
222 $rm2 = remove_dir($dest2);
224 if (!($rm1 || $rm2)) {
225 error ('could not delete old directory, update failed');
228 //2. copy & unzip into new
230 require_once($CFG->libdir.'/componentlib.class.php');
231 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
232 $pack.'.zip', 'languages.md5', 'lang')) {
233 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
234 switch ($status) {
236 case ERROR:
237 if ($cd->get_error() == 'remotedownloadnotallowed') {
238 $a = new stdClass();
239 $a->url = 'http://download.moodle.org/lang16/'.$pack.'zip';
240 $a->dest= $CFG->dataroot.'/lang';
241 error(get_string($cd->get_error(), 'error', $a));
242 } else {
243 error(get_string($cd->get_error(), 'error'));
245 break;
246 case UPTODATE:
247 //Print error string or whatever you want to do
248 break;
249 case INSTALLED:
250 print_string('langpackupdated','admin',$pack);
251 $updated = true;
252 //Print/do whatever you want
253 break;
254 default:
256 } else {
261 echo '<div align="center"><form action="langimport.php" method="POST">';
262 if ($updated) {
263 echo '<br />';
264 notify(get_string('langupdatecomplete','admin'));
265 } else {
266 notify(get_string('nolangupdateneeded','admin'));
268 echo '<input type="submit" value="'.get_string('ok').'" />';
269 echo '</form></div>';
271 break;
273 default: //display choice mode
275 $source = 'http://download.moodle.org/lang16/languages.md5';
276 $remote = 0; //flag for reading from remote or local
278 if ($availablelangs = proxy_url($source)) {
279 $remote = 1;
280 } else {
281 $availablelangs = get_local_list_of_languages();
284 if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.
285 while(!feof ($fp)) {
286 $availablelangs[] = split(',', fgets($fp,1024));
288 $remote = 1; //can read from download.moodle.org
289 } else { /// fopen failed, we find local copy of list.
290 $availablelangs = get_local_list_of_languages();
293 if (!$remote) {
294 print_simple_box_start('center','60%');
295 echo '<div align="center">';
296 print_string('remotelangnotavailable','admin',$CFG->dataroot.'/lang/');
297 echo '</div>';
298 print_simple_box_end();
301 print_simple_box_start('center','60%');
302 echo '<table width="100%"><tr><td align="center">';
303 echo get_string('installedlangs','admin');
304 echo '</td><td align="center">';
305 echo get_string('availablelangs','admin');
306 echo '</td></tr>';
307 echo '<tr><td align="center" valign="top">';
308 echo '<form name="uninstallform" action="langimport.php?mode=4" method="POST">';
309 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
310 $installedlangs = get_list_of_languages();
312 /// display installed langs here
314 echo '<select name="uninstalllang" size="15">';
315 foreach ($installedlangs as $clang =>$ilang){
316 echo '<option value="'.$clang.'">'.$ilang.'</option>';
318 echo '</select>';
319 echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
320 echo '</form>';
321 echo '<form name="updateform" action="langimport.php?mode=5" method="POST">';
322 echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
323 echo '</form>';
324 echo '<p />';
326 /// Display option to change site language
328 print_string('changesitelang','admin');
329 $sitelanguage = get_record('config','name','lang');
330 echo '<form name="changelangform" action="langimport.php?mode=3" method="POST">';
331 echo '<select name="sitelangconfig">';
333 foreach ($installedlangs as $clang =>$ilang) {
334 if ($clang == $sitelanguage->value){
335 echo '<option value="'.$clang.'" selected="selected">'.$ilang.'</option>';
336 } else {
337 echo '<option value="'.$clang.'">'.$ilang.'</option>';
340 echo '</select>';
341 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
342 echo '<input type="submit" value="'.get_string('change','admin').'" />';
343 echo '</form>';
345 /// display to be installed langs here
347 echo '</td><td align="center" valign="top">';
348 echo '<table><tr><td valign="top" align="center">'; //availabe langs table
349 $empty = 1; //something to pring
351 /// if this language pack is not already installed, then we allow installation
353 echo '<form name="installform" method="POST" action="langimport.php?mode=2">';
354 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
355 if ($remote) {
356 echo '<select name="pack" size="15">';
359 foreach ($availablelangs as $alang) {
360 if (trim($alang[0]) != "en_utf8") {
361 if ($remote){
362 if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed
363 echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$alang[0].')</option>';
365 } else { //print list in local format, and instruction to install
366 echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a>';
368 $empty = 0;
371 if ($remote) {
372 echo '</select>';
373 echo '<br/ ><input type="submit" value="'.get_string('install','admin').'">';
375 echo '</form>';
377 if ($empty) {
378 echo '<tr><td align="center">';
379 print_string('nolanguagetodownload','admin');
380 echo '</td></tr>';
383 echo '</td><tr></table>'; //close available langs table
384 echo '<form>';
385 echo '</td></tr></table>';
386 print_simple_box_end();
388 break;
390 } //close of main switch
392 print_footer();
394 /* returns a list of available language packs from a
395 * local copy shipped with standard moodle distro
396 * this is for site that can't perform fopen
397 * @return array
399 function get_local_list_of_languages() {
400 global $CFG;
401 $source = $CFG->wwwroot.'/lib/languages.md5';
402 $availablelangs = array();
403 if ($fp = fopen($source, 'r')){
404 while(!feof ($fp)) {
405 $availablelangs[] = split(',', fgets($fp,1024));
408 return $availablelangs;
411 /* checks the md5 of the zip file, grabbed from download.moodle.org,
412 * against the md5 of the local language file from last update
413 * @param string $lang
414 * @param string $md5check
415 * @return bool
417 function is_installed_lang($lang, $md5check) {
418 global $CFG;
419 $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
420 if (file_exists($md5file)){
421 return (file_get_contents($md5file) == $md5check);
423 return false;
426 //returns an array of languages, or false if can not read from source
427 //uses a socket if proxy is set as a config variable
428 function proxy_url($url) {
429 global $CFG;
431 if ($CFG->proxyhost && $CFG->proxyport) {
433 $proxy_fp = fsockopen($CFG->proxyhost, $CFG->proxyport);
434 if (!$proxy_fp) {
435 return false; //failed
437 fputs($proxy_fp, "GET $url HTTP/1.0\r\nHost: $CFG->proxyhost\r\n\r\n");
438 $i = 0;
439 while(!feof($proxy_fp)) {
440 $string = fgets($proxy_fp, 1024);
441 if ($i > 11) { //12 lines of info skipped
442 $availablelangs[] = split(',', $string);
444 $i++;
446 fclose($proxy_fp);
448 } else { //proxy not in use
449 if ($fp = fopen($url, 'r')){ /// attempt to get the list from Moodle.org.
450 while(!feof ($fp)) {
451 $availablelangs[] = split(',', fgets($fp,1024));
453 } else { /// fopen failed, return false.
454 return false;
457 return $availablelangs;