Output was missing utf8 meta tag.
[moodle-linuxchix.git] / admin / langimport.php
blob7ae5dd38bfc5afcfacecb91c8549c2203606a56a
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');
8 require_once($CFG->libdir.'/adminlib.php');
9 $adminroot = admin_get_root();
10 admin_externalpage_setup('langimport', $adminroot);
12 $mode = optional_param('mode', 0, PARAM_INT); //phase
13 $pack = optional_param('pack', '', PARAM_FILE); //pack to install
14 $displaylang = $pack;
15 $uninstalllang = optional_param('uninstalllang', '', PARAM_FILE);
16 $confirm = optional_param('confirm', 0, PARAM_BOOL);
17 $sitelang = optional_param('sitelangconfig', '', PARAM_FILE);
19 define('INSTALLATION_OF_SELECTED_LANG', 2);
20 define('CHANGE_SITE_LANG', 3);
21 define('DELETION_OF_SELECTED_LANG', 4);
22 define('UPDATE_ALL_LANG', 5);
24 $strlang = get_string('langimport','admin');
26 $strlanguage = get_string("language");
27 $strthislanguage = get_string("thislanguage");
28 $title = $strlang;
30 admin_externalpage_print_header($adminroot);
33 switch ($mode){
35 case INSTALLATION_OF_SELECTED_LANG: ///installation of selected language pack
37 if (confirm_sesskey()) {
38 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
39 @mkdir ($CFG->dataroot.'/lang/');
41 require_once($CFG->libdir.'/componentlib.class.php');
42 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
43 $pack.'.zip', 'languages.md5', 'lang')) {
44 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
45 switch ($status) {
47 case ERROR:
48 if ($cd->get_error() == 'remotedownloadnotallowed') {
49 $a = new stdClass();
50 $a->url = 'http://download.moodle.org/lang16/'.$pack.'zip';
51 $a->dest= $CFG->dataroot.'/lang';
52 error(get_string($cd->get_error(), 'error', $a));
53 } else {
54 error(get_string($cd->get_error(), 'error'));
56 break;
58 case INSTALLED:
59 @unlink($CFG->dataroot.'/cache/languages');
60 redirect('langimport.php', get_string('langpackupdated','admin',$pack));
61 break;
63 case UPTODATE:
64 break;
67 } else {
68 notify('Had an unspecified error with the component installer, sorry.');
71 break;
73 case CHANGE_SITE_LANG: //change site language
75 if (confirm_sesskey()) {
76 $langconfig = get_record('config','name','lang');
77 $langconfig->value = $sitelang;
78 if (!empty($sitelang) && update_record('config',$langconfig)){
79 redirect('langimport.php', get_string('sitelangchanged','admin'));
80 } else {
81 error('Could not update the default site language!');
85 break;
86 case DELETION_OF_SELECTED_LANG: //delete a directory(ies) containing a lang pack completely
88 if (!$confirm && confirm_sesskey()) {
89 notice_yesno(get_string('uninstallconfirm', 'admin', $uninstalllang),
90 'langimport.php?mode=4&amp;uninstalllang='.$uninstalllang.'&amp;confirm=1&amp;sesskey='.sesskey(),
91 'langimport.php');
92 } else if (confirm_sesskey()) {
93 if ($uninstalllang == 'en_utf8') {
94 error ('en_utf8 can not be uninstalled!');
96 $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
97 $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
98 $rm1 = false;
99 $rm2 = false;
100 if (file_exists($dest1)){
101 $rm1 = remove_dir($dest1);
103 if (file_exists($dest2)){
104 $rm2 = remove_dir($dest2);
106 //delete the direcotries
107 if ($rm1 or $rm2) {
108 redirect('langimport.php', get_string('langpackremoved','admin'));
109 } else { //nothing deleted, possibly due to permission error
110 error('An error has occurred, language pack is not completely uninstalled, please check file permissions');
113 @unlink($CFG->dataroot.'/cache/languages');
114 break;
116 case UPDATE_ALL_LANG: //1 click update for all updatable language packs
118 //0th pull a list from download.moodle.org,
119 //key = langname, value = md5
120 $source = 'http://download.moodle.org/lang16/languages.md5';
121 $md5array = array();
122 $updated = 0; //any packs updated?
123 unset($CFG->langlist); // ignore admin's langlist
124 $alllangs = array_keys(get_list_of_languages());
125 $lang16 = array(); //all the Moodle 1.6 unicode lang packs (updated and not updated)
126 $packs = array(); //all the packs that needs updating
129 if (!$availablelangs = proxy_url($source)) {
130 error ('can not read from course');
133 //and build an associative array
134 foreach ($availablelangs as $alang) {
135 $md5array[$alang[0]] = $alang[1];
138 //filtering out non-16 packs
139 foreach ($alllangs as $clang) {
140 $dest1 = $CFG->dataroot.'/lang/'.$clang;
141 $dest2 = $CFG->dirroot.'/lang/'.$clang;
143 if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){
144 $lang16[] = $clang;
148 //then filter out packs that have the same md5 key
149 foreach ($lang16 as $clang) {
150 if (!is_installed_lang($clang, $md5array[$clang])){
151 $packs[] = $clang;
155 @mkdir ($CFG->dataroot.'/temp/');
156 @mkdir ($CFG->dataroot.'/lang/');
157 foreach ($packs as $pack){ //for each of the remaining in the list, we
158 if ($pack == 'en_utf8') { // no update for en_utf8
159 continue;
161 //1. delete old director(ies)
163 $dest1 = $CFG->dataroot.'/lang/'.$pack;
164 $dest2 = $CFG->dirroot.'/lang/'.$pack;
165 $rm1 = false;
166 $rm2 = false;
167 if (file_exists($dest1)) {
168 $rm1 = remove_dir($dest1);
170 if (file_exists($dest2)) {
171 $rm2 = remove_dir($dest2);
173 if (!($rm1 || $rm2)) {
174 error ('could not delete old directory, update failed');
177 //2. copy & unzip into new
179 require_once($CFG->libdir.'/componentlib.class.php');
180 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
181 $pack.'.zip', 'languages.md5', 'lang')) {
182 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
183 switch ($status) {
185 case ERROR:
186 if ($cd->get_error() == 'remotedownloadnotallowed') {
187 $a = new stdClass();
188 $a->url = 'http://download.moodle.org/lang16/'.$pack.'zip';
189 $a->dest= $CFG->dataroot.'/lang';
190 error(get_string($cd->get_error(), 'error', $a));
191 } else {
192 error(get_string($cd->get_error(), 'error'));
194 break;
195 case UPTODATE:
196 //Print error string or whatever you want to do
197 break;
198 case INSTALLED:
199 notify(get_string('langpackupdated','admin',$pack), 'notifysuccess');
200 $updated = true;
201 //Print/do whatever you want
202 break;
203 default:
205 } else {
210 if ($updated) {
211 notice(get_string('langupdatecomplete','admin'), 'langimport.php');
212 } else {
213 notice(get_string('nolangupdateneeded','admin'), 'langimport.php');
216 break;
218 default: //display choice mode
220 $source = 'http://download.moodle.org/lang16/languages.md5';
221 $remote = 0; //flag for reading from remote or local
223 if ($availablelangs = proxy_url($source)) {
224 $remote = 1;
225 } else {
226 $availablelangs = get_local_list_of_languages();
229 if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.
230 while(!feof ($fp)) {
231 $availablelangs[] = split(',', fgets($fp,1024));
233 $remote = 1; //can read from download.moodle.org
234 } else { /// fopen failed, we find local copy of list.
235 $availablelangs = get_local_list_of_languages();
238 if (!$remote) {
239 print_box_start();
240 print_string('remotelangnotavailable','admin',$CFG->dataroot.'/lang/');
241 print_box_end();
244 print_box_start();
245 echo '<table summary="">';
246 echo '<tr><td align="center" valign="top">';
247 echo '<form id="uninstallform" action="langimport.php?mode=4" method="post">';
248 echo '<fieldset class="invisiblefieldset">';
249 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
250 unset($CFG->langlist); // ignore admin's langlist
251 $installedlangs = get_list_of_languages();
253 /// display installed langs here
255 echo '<label for="uninstalllang">'.get_string('installedlangs','admin')."</label><br />\n";
256 echo '<select name="uninstalllang" id="uninstalllang" size="15">';
257 foreach ($installedlangs as $clang =>$ilang){
258 echo '<option value="'.$clang.'">'.$ilang.'</option>';
260 echo '</select>';
261 echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
262 echo '</fieldset>';
263 echo '</form>';
264 echo '<form id="updateform" action="langimport.php?mode=5" method="post">';
265 echo '<fieldset class="invisiblefieldset">';
266 echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
267 echo '</fieldset>';
268 echo '</form>';
270 /// Display option to change site language
272 /// display to be installed langs here
274 echo '</td><td align="center" valign="top">';
275 //availabe langs table
276 $empty = 1; //something to pring
278 /// if this language pack is not already installed, then we allow installation
280 echo '<form id="installform" method="post" action="langimport.php?mode=2">';
281 echo '<fieldset class="invisiblefieldset">';
282 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
283 echo '<label for="pack">'.get_string('availablelangs','admin')."</label><br />\n";
284 if ($remote) {
285 echo '<select name="pack" id="pack" size="15">';
288 foreach ($availablelangs as $alang) {
289 if (trim($alang[0]) != "en_utf8") {
290 if ($remote){
291 if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
292 $shortlang = substr($alang[0], 0, -5);
293 } else {
294 $shortlang = $alang[0];
296 if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed
297 echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$shortlang.')</option>';
299 } else { //print list in local format, and instruction to install
300 echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a></td></tr>';
302 $empty = 0;
305 if ($remote) {
306 echo '</select>';
307 echo '<br /><input type="submit" value="&larr; '.get_string('install','admin').'" />';
309 echo '</fieldset>';
310 echo '</form>';
312 if ($empty) {
313 echo '<br />';
314 print_string('nolanguagetodownload','admin');
317 //close available langs table
318 echo '</td></tr></table>';
319 print_simple_box_end();
321 break;
323 } //close of main switch
325 admin_externalpage_print_footer($adminroot);
327 /* returns a list of available language packs from a
328 * local copy shipped with standard moodle distro
329 * this is for site that can't perform fopen
330 * @return array
332 function get_local_list_of_languages() {
333 global $CFG;
334 $source = $CFG->wwwroot.'/lib/languages.md5';
335 $availablelangs = array();
336 if ($fp = fopen($source, 'r')){
337 while(!feof ($fp)) {
338 $availablelangs[] = split(',', fgets($fp,1024));
341 return $availablelangs;
344 /* checks the md5 of the zip file, grabbed from download.moodle.org,
345 * against the md5 of the local language file from last update
346 * @param string $lang
347 * @param string $md5check
348 * @return bool
350 function is_installed_lang($lang, $md5check) {
351 global $CFG;
352 $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
353 if (file_exists($md5file)){
354 return (file_get_contents($md5file) == $md5check);
356 return false;
359 //returns an array of languages, or false if can not read from source
360 //uses a socket if proxy is set as a config variable
361 function proxy_url($url) {
362 global $CFG;
364 if ($CFG->proxyhost && $CFG->proxyport) {
366 $proxy_fp = fsockopen($CFG->proxyhost, $CFG->proxyport);
367 if (!$proxy_fp) {
368 return false; //failed
370 fputs($proxy_fp, "GET $url HTTP/1.0\r\nHost: $CFG->proxyhost\r\n\r\n");
371 $i = 0;
372 while(!feof($proxy_fp)) {
373 $string = fgets($proxy_fp, 1024);
374 if ($i > 11) { //12 lines of info skipped
375 $availablelangs[] = split(',', $string);
377 $i++;
379 fclose($proxy_fp);
381 } else { //proxy not in use
382 if ($fp = fopen($url, 'r')){ /// attempt to get the list from Moodle.org.
383 while(!feof ($fp)) {
384 $availablelangs[] = split(',', fgets($fp,1024));
386 } else { /// fopen failed, return false.
387 return false;
390 return $availablelangs;