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
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");
30 admin_externalpage_print_header($adminroot);
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
48 if ($cd->get_error() == 'remotedownloadnotallowed') {
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));
54 error(get_string($cd->get_error(), 'error'));
59 @unlink
($CFG->dataroot
.'/cache/languages');
60 redirect('langimport.php', get_string('langpackupdated','admin',$pack));
68 notify('Had an unspecified error with the component installer, sorry.');
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'));
81 error('Could not update the default site language!');
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&uninstalllang='.$uninstalllang.'&confirm=1&sesskey='.sesskey(),
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;
100 if (file_exists($dest1)){
101 $rm1 = remove_dir($dest1);
103 if (file_exists($dest2)){
104 $rm2 = remove_dir($dest2);
106 //delete the direcotries
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');
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';
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')){
148 //then filter out packs that have the same md5 key
149 foreach ($lang16 as $clang) {
150 if (!is_installed_lang($clang, $md5array[$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
161 //1. delete old director(ies)
163 $dest1 = $CFG->dataroot
.'/lang/'.$pack;
164 $dest2 = $CFG->dirroot
.'/lang/'.$pack;
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
186 if ($cd->get_error() == 'remotedownloadnotallowed') {
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));
192 error(get_string($cd->get_error(), 'error'));
196 //Print error string or whatever you want to do
199 notify(get_string('langpackupdated','admin',$pack), 'notifysuccess');
201 //Print/do whatever you want
211 notice(get_string('langupdatecomplete','admin'), 'langimport.php');
213 notice(get_string('nolangupdateneeded','admin'), 'langimport.php');
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)) {
226 $availablelangs = get_local_list_of_languages();
229 if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.
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();
240 print_string('remotelangnotavailable','admin',$CFG->dataroot
.'/lang/');
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>';
261 echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
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').'" />';
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";
285 echo '<select name="pack" id="pack" size="15">';
288 foreach ($availablelangs as $alang) {
289 if (trim($alang[0]) != "en_utf8") {
291 if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
292 $shortlang = substr($alang[0], 0, -5);
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>';
307 echo '<br /><input type="submit" value="← '.get_string('install','admin').'" />';
314 print_string('nolanguagetodownload','admin');
317 //close available langs table
318 echo '</td></tr></table>';
319 print_simple_box_end();
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
332 function get_local_list_of_languages() {
334 $source = $CFG->wwwroot
.'/lib/languages.md5';
335 $availablelangs = array();
336 if ($fp = fopen($source, 'r')){
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
350 function is_installed_lang($lang, $md5check) {
352 $md5file = $CFG->dataroot
.'/lang/'.$lang.'/'.$lang.'.md5';
353 if (file_exists($md5file)){
354 return (file_get_contents($md5file) == $md5check);
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) {
364 if ($CFG->proxyhost
&& $CFG->proxyport
) {
366 $proxy_fp = fsockopen($CFG->proxyhost
, $CFG->proxyport
);
368 return false; //failed
370 fputs($proxy_fp, "GET $url HTTP/1.0\r\nHost: $CFG->proxyhost\r\n\r\n");
372 while(!feof($proxy_fp)) {
373 $string = fgets($proxy_fp, 1024);
374 if ($i > 11) { //12 lines of info skipped
375 $availablelangs[] = split(',', $string);
381 } else { //proxy not in use
382 if ($fp = fopen($url, 'r')){ /// attempt to get the list from Moodle.org.
384 $availablelangs[] = split(',', fgets($fp,1024));
386 } else { /// fopen failed, return false.
390 return $availablelangs;