Automatic installer.php lang files by installer_builder (20070202)
[moodle.git] / admin / langimport.php
blob4ff88921939a4eee2cfeed9b3d17fc5eb736bf60
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 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
53 @mkdir ($CFG->dataroot.'/lang/');
55 require_once($CFG->libdir.'/componentlib.class.php');
56 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
57 $pack.'.zip', 'languages.md5', 'lang')) {
58 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
59 switch ($status) {
61 case ERROR:
62 if ($cd->get_error() == 'remotedownloadnotallowed') {
63 $a = new stdClass();
64 $a->url = 'http://download.moodle.org/lang16/'.$pack.'zip';
65 $a->dest= $CFG->dataroot.'/lang';
66 error(get_string($cd->get_error(), 'error', $a));
67 } else {
68 error(get_string($cd->get_error(), 'error'));
70 break;
72 case INSTALLED:
73 @unlink($CFG->dataroot.'/cache/languages');
74 redirect('langimport.php', get_string('langpackupdated','admin',$pack));
75 break;
77 case UPTODATE:
78 break;
81 } else {
82 notify('Had an unspecified error with the component installer, sorry.');
85 break;
87 case CHANGE_SITE_LANG: //change site language
89 if (confirm_sesskey()) {
90 $langconfig = get_record('config','name','lang');
91 $langconfig->value = $sitelang;
92 if (!empty($sitelang) && update_record('config',$langconfig)){
93 redirect('langimport.php', get_string('sitelangchanged','admin'));
94 } else {
95 error('Could not update the default site language!');
99 break;
100 case DELETION_OF_SELECTED_LANG: //delete a directory(ies) containing a lang pack completely
102 if (!$confirm && confirm_sesskey()) {
103 notice_yesno(get_string('uninstallconfirm', 'admin', $uninstalllang),
104 'langimport.php?mode=4&amp;uninstalllang='.$uninstalllang.'&amp;confirm=1&amp;sesskey='.sesskey(),
105 'langimport.php');
106 } else if (confirm_sesskey()) {
107 if ($uninstalllang == 'en_utf8') {
108 error ('en_utf8 can not be uninstalled!');
110 $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
111 $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
112 $rm1 = false;
113 $rm2 = false;
114 if (file_exists($dest1)){
115 $rm1 = remove_dir($dest1);
117 if (file_exists($dest2)){
118 $rm2 = remove_dir($dest2);
120 //delete the direcotries
121 if ($rm1 or $rm2) {
122 redirect('langimport.php', get_string('langpackremoved','admin'));
123 } else { //nothing deleted, possibly due to permission error
124 error('An error has occurred, language pack is not completely uninstalled, please check file permissions');
127 @unlink($CFG->dataroot.'/cache/languages');
128 break;
130 case UPDATE_ALL_LANG: //1 click update for all updatable language packs
132 //0th pull a list from download.moodle.org,
133 //key = langname, value = md5
134 $source = 'http://download.moodle.org/lang16/languages.md5';
135 $md5array = array();
136 $updated = 0; //any packs updated?
137 unset($CFG->langlist); // ignore admin's langlist
138 $alllangs = array_keys(get_list_of_languages());
139 $lang16 = array(); //all the Moodle 1.6 unicode lang packs (updated and not updated)
140 $packs = array(); //all the packs that needs updating
143 if (!$availablelangs = proxy_url($source)) {
144 error ('can not read from course');
147 //and build an associative array
148 foreach ($availablelangs as $alang) {
149 $md5array[$alang[0]] = $alang[1];
152 //filtering out non-16 packs
153 foreach ($alllangs as $clang) {
154 $dest1 = $CFG->dataroot.'/lang/'.$clang;
155 $dest2 = $CFG->dirroot.'/lang/'.$clang;
157 if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){
158 $lang16[] = $clang;
162 //then filter out packs that have the same md5 key
163 foreach ($lang16 as $clang) {
164 if (!is_installed_lang($clang, $md5array[$clang])){
165 $packs[] = $clang;
169 @mkdir ($CFG->dataroot.'/temp/');
170 @mkdir ($CFG->dataroot.'/lang/');
171 foreach ($packs as $pack){ //for each of the remaining in the list, we
172 if ($pack == 'en_utf8') { // no update for en_utf8
173 continue;
175 //1. delete old director(ies)
177 $dest1 = $CFG->dataroot.'/lang/'.$pack;
178 $dest2 = $CFG->dirroot.'/lang/'.$pack;
179 $rm1 = false;
180 $rm2 = false;
181 if (file_exists($dest1)) {
182 $rm1 = remove_dir($dest1);
184 if (file_exists($dest2)) {
185 $rm2 = remove_dir($dest2);
187 if (!($rm1 || $rm2)) {
188 error ('could not delete old directory, update failed');
191 //2. copy & unzip into new
193 require_once($CFG->libdir.'/componentlib.class.php');
194 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
195 $pack.'.zip', 'languages.md5', 'lang')) {
196 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
197 switch ($status) {
199 case ERROR:
200 if ($cd->get_error() == 'remotedownloadnotallowed') {
201 $a = new stdClass();
202 $a->url = 'http://download.moodle.org/lang16/'.$pack.'zip';
203 $a->dest= $CFG->dataroot.'/lang';
204 error(get_string($cd->get_error(), 'error', $a));
205 } else {
206 error(get_string($cd->get_error(), 'error'));
208 break;
209 case UPTODATE:
210 //Print error string or whatever you want to do
211 break;
212 case INSTALLED:
213 notify(get_string('langpackupdated','admin',$pack), 'notifysuccess');
214 $updated = true;
215 //Print/do whatever you want
216 break;
217 default:
219 } else {
224 if ($updated) {
225 notice(get_string('langupdatecomplete','admin'), 'langimport.php');
226 } else {
227 notice(get_string('nolangupdateneeded','admin'), 'langimport.php');
230 break;
232 default: //display choice mode
234 $source = 'http://download.moodle.org/lang16/languages.md5';
235 $remote = 0; //flag for reading from remote or local
237 if ($availablelangs = proxy_url($source)) {
238 $remote = 1;
239 } else {
240 $availablelangs = get_local_list_of_languages();
243 if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.
244 while(!feof ($fp)) {
245 $availablelangs[] = split(',', fgets($fp,1024));
247 $remote = 1; //can read from download.moodle.org
248 } else { /// fopen failed, we find local copy of list.
249 $availablelangs = get_local_list_of_languages();
252 if (!$remote) {
253 print_simple_box_start('center','60%');
254 echo '<div align="center">';
255 print_string('remotelangnotavailable','admin',$CFG->dataroot.'/lang/');
256 echo '</div>';
257 print_simple_box_end();
260 print_simple_box_start('center','60%');
261 echo '<table width="100%"><tr><td align="center">';
262 echo get_string('installedlangs','admin');
263 echo '</td><td align="center">';
264 echo get_string('availablelangs','admin');
265 echo '</td></tr>';
266 echo '<tr><td align="center" valign="top">';
267 echo '<form name="uninstallform" action="langimport.php?mode=4" method="POST">';
268 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
269 unset($CFG->langlist); // ignore admin's langlist
270 $installedlangs = get_list_of_languages();
272 /// display installed langs here
274 echo '<select name="uninstalllang" size="15">';
275 foreach ($installedlangs as $clang =>$ilang){
276 echo '<option value="'.$clang.'">'.$ilang.'</option>';
278 echo '</select>';
279 echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
280 echo '</form>';
281 echo '<form name="updateform" action="langimport.php?mode=5" method="POST">';
282 echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
283 echo '</form>';
284 echo '<p />';
286 /// Display option to change site language
288 print_string('changesitelang','admin');
289 $sitelanguage = get_record('config','name','lang');
290 echo '<form name="changelangform" action="langimport.php?mode=3" method="POST">';
291 echo '<select name="sitelangconfig">';
293 foreach ($installedlangs as $clang =>$ilang) {
294 if ($clang == $sitelanguage->value){
295 echo '<option value="'.$clang.'" selected="selected">'.$ilang.'</option>';
296 } else {
297 echo '<option value="'.$clang.'">'.$ilang.'</option>';
300 echo '</select>';
301 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
302 echo '<input type="submit" value="'.get_string('change','admin').'" />';
303 echo '</form>';
305 /// display to be installed langs here
307 echo '</td><td align="center" valign="top">';
308 echo '<table><tr><td valign="top" align="center">'; //availabe langs table
309 $empty = 1; //something to pring
311 /// if this language pack is not already installed, then we allow installation
313 echo '<form name="installform" method="POST" action="langimport.php?mode=2">';
314 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
315 if ($remote) {
316 echo '<select name="pack" size="15">';
319 foreach ($availablelangs as $alang) {
320 if (trim($alang[0]) != "en_utf8") {
321 if ($remote){
322 if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
323 $shortlang = substr($alang[0], 0, -5);
324 } else {
325 $shortlang = $alang[0];
327 if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed
328 echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$shortlang.')</option>';
330 } else { //print list in local format, and instruction to install
331 echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a>';
333 $empty = 0;
336 if ($remote) {
337 echo '</select>';
338 echo '<br/ ><input type="submit" value="<-- '.get_string('install','admin').'">';
340 echo '</form>';
342 if ($empty) {
343 echo '<tr><td align="center">';
344 print_string('nolanguagetodownload','admin');
345 echo '</td></tr>';
348 echo '</td><tr></table>'; //close available langs table
349 echo '<form>';
350 echo '</td></tr></table>';
351 print_simple_box_end();
353 break;
355 } //close of main switch
357 print_footer();
359 /* returns a list of available language packs from a
360 * local copy shipped with standard moodle distro
361 * this is for site that can't perform fopen
362 * @return array
364 function get_local_list_of_languages() {
365 global $CFG;
366 $source = $CFG->wwwroot.'/lib/languages.md5';
367 $availablelangs = array();
368 if ($fp = fopen($source, 'r')){
369 while(!feof ($fp)) {
370 $availablelangs[] = split(',', fgets($fp,1024));
373 return $availablelangs;
376 /* checks the md5 of the zip file, grabbed from download.moodle.org,
377 * against the md5 of the local language file from last update
378 * @param string $lang
379 * @param string $md5check
380 * @return bool
382 function is_installed_lang($lang, $md5check) {
383 global $CFG;
384 $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
385 if (file_exists($md5file)){
386 return (file_get_contents($md5file) == $md5check);
388 return false;
391 //returns an array of languages, or false if can not read from source
392 //uses a socket if proxy is set as a config variable
393 function proxy_url($url) {
394 global $CFG;
396 if ($CFG->proxyhost && $CFG->proxyport) {
398 $proxy_fp = fsockopen($CFG->proxyhost, $CFG->proxyport);
399 if (!$proxy_fp) {
400 return false; //failed
402 fputs($proxy_fp, "GET $url HTTP/1.0\r\nHost: $CFG->proxyhost\r\n\r\n");
403 $i = 0;
404 while(!feof($proxy_fp)) {
405 $string = fgets($proxy_fp, 1024);
406 if ($i > 11) { //12 lines of info skipped
407 $availablelangs[] = split(',', $string);
409 $i++;
411 fclose($proxy_fp);
413 } else { //proxy not in use
414 if ($fp = fopen($url, 'r')){ /// attempt to get the list from Moodle.org.
415 while(!feof ($fp)) {
416 $availablelangs[] = split(',', fgets($fp,1024));
418 } else { /// fopen failed, return false.
419 return false;
422 return $availablelangs;