PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/MoodleWebRole/admin/langimport.php

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