Fixed bug #4536 : error in optional_param
[moodle.git] / admin / langimport.php
CommitLineData
f962f6bf 1<?php\r
2///This file only manages the installation of 1.6 lang packs.\r
1a71d807 3///in downloads.moodle.org, they are store in separate directory /lang16\r
f962f6bf 4///in local server, they are stored in $CFG->dataroot/lang\r
5///This helps to avoid confusion.\r
6\r
f962f6bf 7 include('../config.php');\r
8 $mode = optional_param('mode',0,PARAM_INT); //phase\r
9db82283 9 $pack = optional_param('pack','',PARAM_NOTAGS); //pack to install\r
1a71d807 10 $displaylang = $pack;\r
9db82283 11 $uninstalllang = optional_param('uninstalllang','',PARAM_NOTAGS);\r
f962f6bf 12 require_login();\r
13\r
14 if (!isadmin()) {\r
15 error('You must be an admin');\r
16 }\r
17 \r
1a71d807 18 if (!$site = get_site()) {\r
19 error("Site not defined!");\r
20 }\r
21 \r
bb5c34a1 22 $strlang = get_string('langimport','admin');\r
f962f6bf 23 \r
1a71d807 24 $stradministration = get_string("administration");\r
25 $strconfiguration = get_string("configuration");\r
26 $strlanguage = get_string("language");\r
27 $strthislanguage = get_string("thislanguage");\r
28 $title = $strlang;\r
29 \r
30 print_header("$site->shortname: $title", "$site->fullname",\r
31 "<a href=\"index.php\">$stradministration</a> -> ".\r
32 "<a href=\"configure.php\">$strconfiguration</a> -> ".\r
33 "<a href=\"lang.php\">$strlanguage</a> -> $strlang",\r
34 '', '', true, '');\r
f962f6bf 35 \r
36 print_heading('');\r
37\r
38 switch ($mode){\r
39 \r
1a71d807 40 case 2: ///installation of selected language pack\r
f962f6bf 41 \r
bb5c34a1 42 if (confirm_sesskey()){\r
1a71d807 43 if (optional_param('confirm')){\r
44 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there\r
45 @mkdir ($CFG->dataroot.'/lang/');\r
46 $source = 'http://download.moodle.org/lang16/'.$pack.'.zip';\r
47 $langpack = $CFG->dataroot.'/temp/'.$pack.'.zip';\r
48 $destination = $CFG->dataroot.'/lang/';\r
49 if ($contents = file_get_contents($source)) { // Grab whole page\r
50 if ($file = fopen($langpack, 'w')) { // Make local copy\r
51 if (!fwrite($file, $contents)){ //copy zip to temp folder..\r
52 error ('could not copy file');\r
53 }\r
54 fclose($file);\r
bb5c34a1 55\r
1a71d807 56 ///recursively remove the whole directory since unzip does not overwrites anything\r
57 if (file_exists($destination.$pack)){\r
58 @remove_dir($destination.$pack.'/');\r
59 }\r
60 //unpack the zip\r
61 if (unzip_file($langpack, $destination, false)){\r
62 print_heading(get_string('langimportsuccess','admin'));\r
63 }\r
64 else {\r
65 error('language installation failed');\r
66 }\r
67 //now, we update the md5key of the lang pack, this is used to check version\r
68 $md5file = $CFG->dataroot.'/lang/'.$pack.'/'.$pack.'.md5';\r
69 if ($file = fopen($md5file, 'w')){\r
70 fwrite($file, md5($contents)); //manually generate md5 from zip\r
71 }\r
72 fclose($file);\r
73 @unlink ($langpack); //bye bye zip file, come back when you get a little better\r
74 echo '<div align="center"><form action="langimport.php" method="POST">';\r
75 echo '<input type="submit" value="'.get_string('ok').'" />';\r
76 echo '</form></div>';\r
bb5c34a1 77 }\r
1a71d807 78 }\r
79 }\r
80 else { //print confirm box, no confirmation yet\r
81 if (confirm_sesskey()){\r
82 print_simple_box_start('center','100%');\r
83 echo '<div align="center">';\r
84 echo '<form name="langform" action="langimport.php?mode=2" method="POST">';\r
85 echo '<input name="pack" type="hidden" value="'.$pack.'" />';\r
86 echo '<input name="displaylang" type="hidden" value="'.$displaylang.'" />';\r
87 echo '<input name="confirm" type="hidden" value="1" />';\r
88 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';\r
89 print_heading(get_string('confirminstall','admin',$displaylang),2);\r
90 echo '<input type="submit" value="'.get_string('ok').'"/>';\r
91 echo '&nbsp;<input type="button" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';\r
92 echo '</form>';\r
93 echo '</div>';\r
94 print_simple_box_end();\r
f962f6bf 95 }\r
f962f6bf 96 }\r
97 }\r
f962f6bf 98 break;\r
1a71d807 99\r
100 case 3: //change site language\r
101\r
102 if (confirm_sesskey){\r
103 $langconfig = get_record('config','name','lang');\r
104 $sitelang = required_param('sitelangconfig',PARAM_NOTAGS);\r
105 $langconfig->value = $sitelang;\r
106 if (update_record('config',$langconfig)){\r
107 echo '<div align="center">';\r
108 notify (get_string('sitelangchanged','admin'));\r
109 echo '<form action="langimport.php" method="POST">';\r
110 echo '<input type="submit" value="'.get_string('ok').'" />';\r
111 echo '</form></div>';\r
112 }\r
113 else {\r
114 error ('can not update site language');\r
115 }\r
116 }\r
117\r
118 break;\r
119 case 4: //delete a directory(ies) containing a lang pack completely\r
436c8dff 120\r
bb5c34a1 121 if (!optional_param('confirm') && confirm_sesskey()){\r
436c8dff 122 print_simple_box_start('center','100%');\r
123 echo '<div align="center">';\r
124 echo '<form name="langform" action="langimport.php?mode=4" method="POST">';\r
125 echo '<input name="uninstalllang" type="hidden" value="'.$uninstalllang.'" />';\r
126 echo '<input name="confirm" type="hidden" value="1" />';\r
bb5c34a1 127 print_heading(get_string('uninstallconfirm','admin',$uninstalllang),2);\r
128 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';\r
129 echo '<input type="submit" value="'.get_string('uninstall','admin').'"/>';\r
436c8dff 130 echo '&nbsp;<input type="button" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';\r
131 echo '</form>';\r
132 echo '</div>';\r
133 print_simple_box_end();\r
134 }\r
bb5c34a1 135 else if (confirm_sesskey()){\r
436c8dff 136 $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;\r
137 $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;\r
bb5c34a1 138 $rm1 = false;\r
139 $rm2 = false;\r
436c8dff 140 if (file_exists($dest1)){\r
bb5c34a1 141 $rm1 = remove_dir($dest1);\r
436c8dff 142 }\r
143 if (file_exists($dest2)){\r
bb5c34a1 144 $rm2 = remove_dir($dest2);\r
436c8dff 145 }\r
146 //delete the direcotries\r
bb5c34a1 147 if ($rm1 or $rm2){\r
148 echo '<div align="center">';\r
149 print_string('langpackremoved','admin');\r
150 echo '<form action="langimport.php" method="POST">';\r
151 echo '<input type="submit" value="'.get_string('ok').'" />';\r
152 echo '</form></div>';\r
153 }\r
1a71d807 154 else { //nothing deleted, possibly due to permission error\r
155 error ('An error has occurred, language pack is not completely uninstalled, please check file permission');\r
bb5c34a1 156 }\r
436c8dff 157 }\r
158 break;\r
159 \r
1a71d807 160 case 5: //1 click update for all updatable language packs\r
161\r
162 //0th pull a list from download.moodle.org, \r
163 //key = langname, value = md5\r
164 $source = 'http://download.moodle.org/lang16/languages.md5';\r
165 $md5array = array();\r
166 $update = 0; //any packs updated?\r
167 $alllangs = array_keys(get_list_of_languages());\r
168 $lang16 = array(); //all the Moodle 1.6 unicode lang packs (updated and not updated)\r
169 $packs = array(); //all the packs that needs updating\r
170 \r
171 if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.\r
172 while(!feof ($fp)) {\r
173 $availablelangs[] = split(',', fgets($fp,1024));\r
174 }\r
175 }\r
176 else {\r
177 error('can not fopen!');\r
178 }\r
179 //and build an associative array\r
180 foreach ($availablelangs as $alang){\r
181 $md5array[$alang[0]] = $alang[1];\r
182 }\r
183\r
184 //first build a list of all the language packs,\r
185\r
186 //filtering out non-16 packs\r
187 foreach ($alllangs as $clang){\r
188 $dest1 = $CFG->dataroot.'/lang/'.$clang;\r
189 $dest2 = $CFG->dirroot.'/lang/'.$clang;\r
190 \r
191 if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){\r
192 $lang16[] = $clang;\r
193 }\r
194 }\r
195\r
196 //then filter out packs that have the same md5 key\r
197 foreach ($lang16 as $clang){\r
198 if (!is_installed_lang($clang, $md5array[$clang])){\r
199 $packs[] = $clang;\r
200 }\r
201 }\r
202 \r
203 \r
204 @mkdir ($CFG->dataroot.'/temp/');\r
205 @mkdir ($CFG->dataroot.'/lang/');\r
206 foreach ($packs as $pack){ //for each of the remaining in the list, we\r
207\r
208 //1. delete old director(ies)\r
209\r
210 $dest1 = $CFG->dataroot.'/lang/'.$pack;\r
211 $dest2 = $CFG->dirroot.'/lang/'.$pack;\r
212 $rm1 = false;\r
213 $rm2 = false;\r
214 if (file_exists($dest1)){\r
215 $rm1 = remove_dir($dest1);\r
216 }\r
217 if (file_exists($dest2)){\r
218 $rm2 = remove_dir($dest2);\r
219 }\r
220 if (!($rm1 || $rm2)){\r
221 error ('could not delete old directory, update failed');\r
222 }\r
223\r
224 //2. copy & unzip into new\r
225 \r
226 $source = 'http://download.moodle.org/lang16/'.$pack.'.zip';\r
227 $langpack = $CFG->dataroot.'/temp/'.$pack.'.zip';\r
228 $destination = $CFG->dataroot.'/lang/';\r
229 if ($contents = file_get_contents($source)) { // Grab whole page\r
230 if ($file = fopen($langpack, 'w')) { // Make local copy\r
231 if (!fwrite($file, $contents)){ //copy zip to temp folder..\r
232 error ('could not copy file');\r
233 }\r
234 fclose($file);\r
235\r
236 //unpack the zip\r
237 if (!unzip_file($langpack, $destination, false)){\r
238 error('language installation failed - can not unzip language pack');\r
239 }\r
240 //now, we update the md5key of the lang pack, this is used to check version\r
241 $md5file = $CFG->dataroot.'/lang/'.$pack.'/'.$pack.'.md5';\r
242 if ($file = fopen($md5file, 'w')){\r
243 fwrite($file, md5($contents));\r
244 }\r
245 fclose($file);\r
246 @unlink ($langpack); //remove the zip file\r
247 echo '<div align="center">';\r
248 notify(print_string('langpackupdated','admin',$pack));\r
249 $updated = 1;\r
250 echo '</div>';\r
251 }\r
252 }\r
253 }\r
254 \r
255 echo '<div align="center"><form action="langimport.php" method="POST">';\r
256 if ($updated){\r
257 notify(print_string('langupdatecomplete','admin'));\r
258 }\r
259 else {\r
260 notify(print_string('nolangupdateneeded','admin'));\r
261 }\r
262 echo '<input type="submit" value="'.get_string('ok').'" />';\r
263 echo '</form></div>';\r
264\r
265 break;\r
266 \r
f962f6bf 267 default: //display choice mode\r
268\r
9db82283 269 $source = 'http://download.moodle.org/lang16/languages.md5';\r
f962f6bf 270 $remote = 0; //flag for reading from remote or local\r
1a71d807 271\r
f962f6bf 272 if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.\r
273 while(!feof ($fp)) {\r
274 $availablelangs[] = split(',', fgets($fp,1024));\r
275 }\r
1a71d807 276 $remote = 1; //can read from download.moodle.org\r
f962f6bf 277 }\r
278 else { /// fopen failed, we find local copy of list.\r
279 $availablelangs = get_local_list_of_languages();\r
280 }\r
281 \r
282 if (!$remote){\r
283 print_simple_box_start('center','60%');\r
284 echo '<div align="center">';\r
1a71d807 285 print_string('remotelangnotavailable','admin',$CFG->dataroot.'/lang/');\r
f962f6bf 286 echo '</div>';\r
287 print_simple_box_end();\r
288 }\r
289 \r
290 print_simple_box_start('center','60%');\r
291 echo '<table width="100%"><tr><td align="center">';\r
bb5c34a1 292 echo get_string('installedlangs','admin');\r
f962f6bf 293 echo '</td><td align="center">';\r
bb5c34a1 294 echo get_string('availablelangs','admin');\r
f962f6bf 295 echo '</td></tr>';\r
1a71d807 296 echo '<tr><td align="center" valign="top">';\r
9db82283 297 echo '<form name="uninstallform" action="langimport.php?mode=4" method="POST">';\r
bb5c34a1 298 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';\r
1a71d807 299 $installedlangs = get_list_of_languages();\r
300\r
301 /// display installed langs here\r
302\r
436c8dff 303 echo '<select name="uninstalllang" size="15">';\r
436c8dff 304 foreach ($installedlangs as $clang =>$ilang){\r
305 echo '<option value="'.$clang.'">'.$ilang.'</option>';\r
f962f6bf 306 }\r
307 echo '</select>';\r
1a71d807 308 echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';\r
309 echo '</form>';\r
310 echo '<form name="updateform" action="langimport.php?mode=5" method="POST">';\r
311 echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';\r
312 echo '</form>';\r
313 echo '<p />';\r
314 \r
315 /// Display option to change site language\r
316 \r
317 print_string('changesitelang','admin');\r
318 $sitelanguage = get_record('config','name','lang');\r
319 echo '<form name="changelangform" action="langimport.php?mode=3" method="POST">';\r
320 echo '<select name="sitelangconfig">';\r
321 \r
322 foreach ($installedlangs as $clang =>$ilang){\r
323 if ($clang == $sitelanguage->value){\r
324 echo '<option value="'.$clang.'" selected="selected">'.$ilang.'</option>';\r
325 }\r
326 else {\r
327 echo '<option value="'.$clang.'">'.$ilang.'</option>';\r
328 }\r
329 }\r
330 echo '</select>';\r
331 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';\r
332 echo '<input type="submit" value="'.get_string('change','admin').'" />';\r
333 echo '</form>';\r
f962f6bf 334\r
1a71d807 335 /// display to be installed langs here\r
f962f6bf 336\r
1a71d807 337 echo '</td><td align="center" valign="top">';\r
338 echo '<table><tr><td valign="top" align="center">'; //availabe langs table\r
f962f6bf 339 $empty = 1; //something to pring\r
1a71d807 340\r
f962f6bf 341 /// if this language pack is not already installed, then we allow installation\r
342\r
1a71d807 343 echo '<form name="installform" method="POST" action="langimport.php?mode=2">';\r
344 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';\r
345 if ($remote){\r
346 echo '<select name="pack" size="15">';\r
347 }\r
f962f6bf 348\r
349 foreach ($availablelangs as $alang){\r
1a71d807 350 if ($remote){\r
351 if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed\r
352 echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$alang[0].')</option>';\r
f962f6bf 353 }\r
f962f6bf 354 }\r
1a71d807 355 else { //print list in local format, and instruction to install\r
356 echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a>';\r
357 }\r
358 $empty = 0;\r
359 }\r
360 if ($remote){\r
361 echo '</select>';\r
362 echo '<br/ ><input type="submit" value="'.get_string('install','admin').'">';\r
f962f6bf 363 }\r
1a71d807 364 echo '</form>';\r
365\r
f962f6bf 366 if ($empty){\r
367 echo '<tr><td align="center">';\r
368 print_string('nolanguagetodownload');\r
369 echo '</td></tr>';\r
370 }\r
371\r
1a71d807 372 echo '</td><tr></table>'; //close available langs table\r
f962f6bf 373 echo '<form>';\r
f962f6bf 374 echo '</td></tr></table>';\r
375 print_simple_box_end();\r
376\r
377 break;\r
378\r
379 } //close of main switch\r
380\r
381 print_footer();\r
382 \r
383 //returns a list of available language packs from a local copy shipped with standard moodle distro\r
384 //this is for site that can't perform fopen\r
385 function get_local_list_of_languages(){\r
386 global $CFG;\r
1a71d807 387 $source = $CFG->wwwroot.'/lib/languages.md5';\r
f962f6bf 388 $availablelangs = array();\r
389 if ($fp = fopen($source, 'r')){\r
390 while(!feof ($fp)) {\r
391 $availablelangs[] = split(',', fgets($fp,1024));\r
392 }\r
393 }\r
394 return $availablelangs;\r
395 }\r
396 \r
397 //checks the md5 of the zip file, grabbed from download.moodle.org, and\r
398 function is_installed_lang($lang, $md5check){\r
399 global $CFG;\r
9db82283 400 $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';\r
f962f6bf 401 if (file_exists($md5file)){\r
402 return (file_get_contents($md5file) == $md5check);\r
403 }\r
9db82283 404\r
f962f6bf 405 return false;\r
406 }\r
407 \r
408?>\r