636c2c08 |
1 | <?php //$Id$ |
6e113a15 |
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. |
6 | |
636c2c08 |
7 | require_once('../config.php'); |
6e4dc10f |
8 | require_once($CFG->libdir.'/adminlib.php'); |
1ae083e4 |
9 | |
10 | admin_externalpage_setup('langimport'); |
636c2c08 |
11 | |
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); |
18 | |
6e113a15 |
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); |
d6d04971 |
23 | |
6e113a15 |
24 | $strlang = get_string('langimport','admin'); |
d6d04971 |
25 | |
6e113a15 |
26 | $strlanguage = get_string("language"); |
27 | $strthislanguage = get_string("thislanguage"); |
28 | $title = $strlang; |
d6d04971 |
29 | |
1ae083e4 |
30 | admin_externalpage_print_header(); |
d6d04971 |
31 | |
0a5ce9dd |
32 | //reset and diagnose lang cache permissions |
33 | @unlink($CFG->dataroot.'/cache/languages'); |
34 | if (file_exists($CFG->dataroot.'/cache/languages')) { |
35 | notify('Language cache can not be deleted, please check permissions in dataroot.'); |
36 | } |
37 | get_list_of_languages(true); //refresh lang cache |
6e113a15 |
38 | |
39 | switch ($mode){ |
d6d04971 |
40 | |
6e113a15 |
41 | case INSTALLATION_OF_SELECTED_LANG: ///installation of selected language pack |
d6d04971 |
42 | |
6e113a15 |
43 | if (confirm_sesskey()) { |
004bd0cc |
44 | @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there |
45 | @mkdir ($CFG->dataroot.'/lang/'); |
d6d04971 |
46 | |
004bd0cc |
47 | require_once($CFG->libdir.'/componentlib.class.php'); |
48 | if ($cd = new component_installer('http://download.moodle.org', 'lang16', |
49 | $pack.'.zip', 'languages.md5', 'lang')) { |
50 | $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED |
51 | switch ($status) { |
1c6f450c |
52 | |
a66fe103 |
53 | case ERROR: |
1c6f450c |
54 | if ($cd->get_error() == 'remotedownloadnotallowed') { |
55 | $a = new stdClass(); |
a66fe103 |
56 | $a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip'; |
1c6f450c |
57 | $a->dest= $CFG->dataroot.'/lang'; |
58 | error(get_string($cd->get_error(), 'error', $a)); |
6e113a15 |
59 | } else { |
1c6f450c |
60 | error(get_string($cd->get_error(), 'error')); |
6e113a15 |
61 | } |
1c6f450c |
62 | break; |
004bd0cc |
63 | |
1c6f450c |
64 | case INSTALLED: |
0a5ce9dd |
65 | get_list_of_languages(true); //refresh lang cache |
1ae083e4 |
66 | redirect('langimport.php', get_string('langpackupdated','admin',$pack), -1); |
004bd0cc |
67 | break; |
68 | |
69 | case UPTODATE: |
1c6f450c |
70 | break; |
1c6f450c |
71 | |
6e113a15 |
72 | } |
004bd0cc |
73 | } else { |
74 | notify('Had an unspecified error with the component installer, sorry.'); |
6e113a15 |
75 | } |
76 | } |
77 | break; |
78 | |
79 | case CHANGE_SITE_LANG: //change site language |
80 | |
5872b4cc |
81 | if (confirm_sesskey()) { |
6e113a15 |
82 | $langconfig = get_record('config','name','lang'); |
6e113a15 |
83 | $langconfig->value = $sitelang; |
636c2c08 |
84 | if (!empty($sitelang) && update_record('config',$langconfig)){ |
004bd0cc |
85 | redirect('langimport.php', get_string('sitelangchanged','admin')); |
6e113a15 |
86 | } else { |
004bd0cc |
87 | error('Could not update the default site language!'); |
6e113a15 |
88 | } |
89 | } |
90 | |
91 | break; |
92 | case DELETION_OF_SELECTED_LANG: //delete a directory(ies) containing a lang pack completely |
93 | |
636c2c08 |
94 | if (!$confirm && confirm_sesskey()) { |
d6d04971 |
95 | notice_yesno(get_string('uninstallconfirm', 'admin', $uninstalllang), |
004bd0cc |
96 | 'langimport.php?mode=4&uninstalllang='.$uninstalllang.'&confirm=1&sesskey='.sesskey(), |
97 | 'langimport.php'); |
98 | } else if (confirm_sesskey()) { |
2786ff70 |
99 | if ($uninstalllang == 'en_utf8') { |
100 | error ('en_utf8 can not be uninstalled!'); |
101 | } |
6e113a15 |
102 | $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang; |
103 | $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang; |
104 | $rm1 = false; |
105 | $rm2 = false; |
106 | if (file_exists($dest1)){ |
107 | $rm1 = remove_dir($dest1); |
108 | } |
109 | if (file_exists($dest2)){ |
110 | $rm2 = remove_dir($dest2); |
111 | } |
0a5ce9dd |
112 | get_list_of_languages(true); //refresh lang cache |
6e113a15 |
113 | //delete the direcotries |
114 | if ($rm1 or $rm2) { |
1ae083e4 |
115 | redirect('langimport.php', get_string('langpackremoved','admin'), 3); |
6e113a15 |
116 | } else { //nothing deleted, possibly due to permission error |
004bd0cc |
117 | error('An error has occurred, language pack is not completely uninstalled, please check file permissions'); |
6e113a15 |
118 | } |
119 | } |
120 | break; |
d6d04971 |
121 | |
6e113a15 |
122 | case UPDATE_ALL_LANG: //1 click update for all updatable language packs |
123 | |
124 | //0th pull a list from download.moodle.org, |
125 | //key = langname, value = md5 |
126 | $source = 'http://download.moodle.org/lang16/languages.md5'; |
127 | $md5array = array(); |
ecb454fe |
128 | $updated = 0; //any packs updated? |
0a5ce9dd |
129 | $alllangs = array_keys(get_list_of_languages(false, true)); //get all available langs |
6e113a15 |
130 | $lang16 = array(); //all the Moodle 1.6 unicode lang packs (updated and not updated) |
131 | $packs = array(); //all the packs that needs updating |
132 | |
28dad8b9 |
133 | |
134 | if (!$availablelangs = proxy_url($source)) { |
6e1cc99d |
135 | print_error('cannotdownloadlanguageupdatelist'); |
6e113a15 |
136 | } |
28dad8b9 |
137 | |
6e113a15 |
138 | //and build an associative array |
139 | foreach ($availablelangs as $alang) { |
140 | $md5array[$alang[0]] = $alang[1]; |
141 | } |
142 | |
6e113a15 |
143 | //filtering out non-16 packs |
144 | foreach ($alllangs as $clang) { |
145 | $dest1 = $CFG->dataroot.'/lang/'.$clang; |
146 | $dest2 = $CFG->dirroot.'/lang/'.$clang; |
147 | |
148 | if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){ |
149 | $lang16[] = $clang; |
150 | } |
151 | } |
152 | |
153 | //then filter out packs that have the same md5 key |
154 | foreach ($lang16 as $clang) { |
155 | if (!is_installed_lang($clang, $md5array[$clang])){ |
156 | $packs[] = $clang; |
157 | } |
158 | } |
159 | |
6e113a15 |
160 | @mkdir ($CFG->dataroot.'/temp/'); |
161 | @mkdir ($CFG->dataroot.'/lang/'); |
162 | foreach ($packs as $pack){ //for each of the remaining in the list, we |
ee14d736 |
163 | if ($pack == 'en_utf8') { // no update for en_utf8 |
164 | continue; |
165 | } |
6e113a15 |
166 | //1. delete old director(ies) |
167 | |
168 | $dest1 = $CFG->dataroot.'/lang/'.$pack; |
169 | $dest2 = $CFG->dirroot.'/lang/'.$pack; |
170 | $rm1 = false; |
171 | $rm2 = false; |
172 | if (file_exists($dest1)) { |
173 | $rm1 = remove_dir($dest1); |
174 | } |
175 | if (file_exists($dest2)) { |
176 | $rm2 = remove_dir($dest2); |
177 | } |
178 | if (!($rm1 || $rm2)) { |
179 | error ('could not delete old directory, update failed'); |
180 | } |
181 | |
182 | //2. copy & unzip into new |
183 | |
1c6f450c |
184 | require_once($CFG->libdir.'/componentlib.class.php'); |
185 | if ($cd = new component_installer('http://download.moodle.org', 'lang16', |
186 | $pack.'.zip', 'languages.md5', 'lang')) { |
187 | $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED |
188 | switch ($status) { |
189 | |
190 | case ERROR: |
191 | if ($cd->get_error() == 'remotedownloadnotallowed') { |
192 | $a = new stdClass(); |
a66fe103 |
193 | $a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip'; |
1c6f450c |
194 | $a->dest= $CFG->dataroot.'/lang'; |
195 | error(get_string($cd->get_error(), 'error', $a)); |
196 | } else { |
197 | error(get_string($cd->get_error(), 'error')); |
6e113a15 |
198 | } |
1c6f450c |
199 | break; |
200 | case UPTODATE: |
201 | //Print error string or whatever you want to do |
202 | break; |
203 | case INSTALLED: |
004bd0cc |
204 | notify(get_string('langpackupdated','admin',$pack), 'notifysuccess'); |
1c6f450c |
205 | $updated = true; |
206 | //Print/do whatever you want |
207 | break; |
208 | default: |
6e113a15 |
209 | } |
1c6f450c |
210 | } else { |
211 | |
6e113a15 |
212 | } |
213 | } |
214 | |
6e113a15 |
215 | if ($updated) { |
1ae083e4 |
216 | notice(get_string('langupdatecomplete','admin'), 'langimport.php'); |
6e113a15 |
217 | } else { |
1ae083e4 |
218 | notice(get_string('nolangupdateneeded','admin'), 'langimport.php'); |
6e113a15 |
219 | } |
6e113a15 |
220 | |
221 | break; |
d6d04971 |
222 | |
6e113a15 |
223 | default: //display choice mode |
224 | |
225 | $source = 'http://download.moodle.org/lang16/languages.md5'; |
226 | $remote = 0; //flag for reading from remote or local |
227 | |
28dad8b9 |
228 | if ($availablelangs = proxy_url($source)) { |
229 | $remote = 1; |
230 | } else { |
231 | $availablelangs = get_local_list_of_languages(); |
232 | } |
233 | /* |
6e113a15 |
234 | if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org. |
235 | while(!feof ($fp)) { |
236 | $availablelangs[] = split(',', fgets($fp,1024)); |
237 | } |
238 | $remote = 1; //can read from download.moodle.org |
239 | } else { /// fopen failed, we find local copy of list. |
240 | $availablelangs = get_local_list_of_languages(); |
241 | } |
28dad8b9 |
242 | */ |
6e113a15 |
243 | if (!$remote) { |
0a8d5d77 |
244 | print_box_start(); |
6e113a15 |
245 | print_string('remotelangnotavailable','admin',$CFG->dataroot.'/lang/'); |
0a8d5d77 |
246 | print_box_end(); |
6e113a15 |
247 | } |
d6d04971 |
248 | |
0a8d5d77 |
249 | print_box_start(); |
250 | echo '<table summary="">'; |
6e113a15 |
251 | echo '<tr><td align="center" valign="top">'; |
b7dc2256 |
252 | echo '<form id="uninstallform" action="langimport.php?mode=4" method="post">'; |
0a8d5d77 |
253 | echo '<fieldset class="invisiblefieldset">'; |
6e113a15 |
254 | echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />'; |
0a5ce9dd |
255 | $installedlangs = get_list_of_languages(false, true); |
6e113a15 |
256 | |
257 | /// display installed langs here |
258 | |
d6d04971 |
259 | echo '<label for="uninstalllang">'.get_string('installedlangs','admin')."</label><br />\n"; |
2493fc10 |
260 | echo '<select name="uninstalllang" id="uninstalllang" size="15">'; |
6e113a15 |
261 | foreach ($installedlangs as $clang =>$ilang){ |
2786ff70 |
262 | echo '<option value="'.$clang.'">'.$ilang.'</option>'; |
6e113a15 |
263 | } |
264 | echo '</select>'; |
265 | echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />'; |
0a8d5d77 |
266 | echo '</fieldset>'; |
6e113a15 |
267 | echo '</form>'; |
b7dc2256 |
268 | echo '<form id="updateform" action="langimport.php?mode=5" method="post">'; |
44a825c2 |
269 | echo '<div>'; |
6e113a15 |
270 | echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />'; |
44a825c2 |
271 | echo '</div>'; |
6e113a15 |
272 | echo '</form>'; |
d6d04971 |
273 | |
6e113a15 |
274 | /// Display option to change site language |
6e113a15 |
275 | |
276 | /// display to be installed langs here |
277 | |
278 | echo '</td><td align="center" valign="top">'; |
d6d04971 |
279 | //availabe langs table |
6e113a15 |
280 | $empty = 1; //something to pring |
281 | |
282 | /// if this language pack is not already installed, then we allow installation |
283 | |
b7dc2256 |
284 | echo '<form id="installform" method="post" action="langimport.php?mode=2">'; |
0a8d5d77 |
285 | echo '<fieldset class="invisiblefieldset">'; |
6e113a15 |
286 | echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />'; |
d6d04971 |
287 | echo '<label for="pack">'.get_string('availablelangs','admin')."</label><br />\n"; |
6e113a15 |
288 | if ($remote) { |
2493fc10 |
289 | echo '<select name="pack" id="pack" size="15">'; |
6e113a15 |
290 | } |
291 | |
292 | foreach ($availablelangs as $alang) { |
28dad8b9 |
293 | if (trim($alang[0]) != "en_utf8") { |
e5952c5a |
294 | if ($remote){ |
8e6184dc |
295 | if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show |
296 | $shortlang = substr($alang[0], 0, -5); |
297 | } else { |
298 | $shortlang = $alang[0]; |
299 | } |
e5952c5a |
300 | if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed |
8e6184dc |
301 | echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$shortlang.')</option>'; |
e5952c5a |
302 | } |
303 | } else { //print list in local format, and instruction to install |
5c8b250f |
304 | echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a></td></tr>'; |
6e113a15 |
305 | } |
e5952c5a |
306 | $empty = 0; |
6e113a15 |
307 | } |
6e113a15 |
308 | } |
309 | if ($remote) { |
310 | echo '</select>'; |
04f1efd4 |
311 | echo '<br /><input type="submit" value="'.$THEME->larrow.' '.get_string('install','admin').'" />'; |
6e113a15 |
312 | } |
0a8d5d77 |
313 | echo '</fieldset>'; |
6e113a15 |
314 | echo '</form>'; |
315 | |
316 | if ($empty) { |
d6d04971 |
317 | echo '<br />'; |
1c6f450c |
318 | print_string('nolanguagetodownload','admin'); |
6e113a15 |
319 | } |
320 | |
d6d04971 |
321 | //close available langs table |
6e113a15 |
322 | echo '</td></tr></table>'; |
c1aa6ad2 |
323 | print_box_end(); |
6e113a15 |
324 | break; |
325 | |
326 | } //close of main switch |
327 | |
1ae083e4 |
328 | admin_externalpage_print_footer(); |
d6d04971 |
329 | |
6e113a15 |
330 | /* returns a list of available language packs from a |
331 | * local copy shipped with standard moodle distro |
332 | * this is for site that can't perform fopen |
333 | * @return array |
334 | */ |
335 | function get_local_list_of_languages() { |
336 | global $CFG; |
337 | $source = $CFG->wwwroot.'/lib/languages.md5'; |
338 | $availablelangs = array(); |
339 | if ($fp = fopen($source, 'r')){ |
340 | while(!feof ($fp)) { |
341 | $availablelangs[] = split(',', fgets($fp,1024)); |
342 | } |
343 | } |
344 | return $availablelangs; |
345 | } |
d6d04971 |
346 | |
347 | /* checks the md5 of the zip file, grabbed from download.moodle.org, |
6e113a15 |
348 | * against the md5 of the local language file from last update |
349 | * @param string $lang |
350 | * @param string $md5check |
351 | * @return bool |
352 | */ |
353 | function is_installed_lang($lang, $md5check) { |
354 | global $CFG; |
355 | $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5'; |
356 | if (file_exists($md5file)){ |
357 | return (file_get_contents($md5file) == $md5check); |
358 | } |
359 | return false; |
360 | } |
d6d04971 |
361 | |
28dad8b9 |
362 | //returns an array of languages, or false if can not read from source |
28dad8b9 |
363 | function proxy_url($url) { |
364 | global $CFG; |
365 | |
6e1cc99d |
366 | $availablelangs = array(); |
28dad8b9 |
367 | |
6e1cc99d |
368 | if( $content = download_file_content($url) ){ |
28dad8b9 |
369 | |
6e1cc99d |
370 | $alllines = split("\n", $content); |
371 | foreach($alllines as $line){ |
372 | if(!empty($line)){ |
373 | $availablelangs[] = split(',', $line); |
28dad8b9 |
374 | } |
28dad8b9 |
375 | } |
6e1cc99d |
376 | |
377 | return $availablelangs; |
378 | }else{ |
379 | return false; |
28dad8b9 |
380 | } |
28dad8b9 |
381 | } |
6e113a15 |
382 | ?> |