dataroot/lang
///This helps to avoid confusion.
/*********************************************
* Problem with directory permisssion *
* /temp, /lang *
*********************************************/
include('../config.php');
$mode = optional_param('mode',0,PARAM_INT); //phase
$pack = optional_param('pack','',PARAM_NOTAGS); //pack to install
$displaylang = optional_param('displaylang','',PARAM_ALPHA); //display language
$uninstalllang = optional_param('uninstalllang','',PARAM_NOTAGS);
require_login();
if (!isadmin()) {
error('You must be an admin');
}
$strlang = get_string('langimport','admin');
print_header($strlang, $strlang, $strlang);
print_heading('');
switch ($mode){
case 2: //mode 2 confirmation
if (confirm_sesskey()){
print_simple_box_start('center','100%');
echo '
';
echo '';
echo '
';
print_simple_box_end();
}
break;
case 3: //mode 3 process (copy, unzip, write md5 file, cleanup)
if (confirm_sesskey()){
@mkdir ($CFG->dataroot.'/temp/');
@mkdir ($CFG->dataroot.'/lang/');
$source = 'http://download.moodle.org/lang16/'.$pack.'.zip';
$langpack = $CFG->dataroot.'/temp/'.$pack.'.zip';
$destination = $CFG->dataroot.'/lang/';
if ($contents = file_get_contents($source)) { // Grab whole page
if ($file = fopen($langpack, 'w')) { // Make local copy
if (!fwrite($file, $contents)){ //copy zip to temp folder..
error ('could not copy file');
}
fclose($file);
///recursively remove the whole directory since unzip does not overwrites anything
if (file_exists($destination.$pack)){
@remove_dir($destination.$pack.'/');
}
//unpack the zip
if (unzip_file($langpack, $destination, false)){
print_heading(get_string('langimportsuccess','admin'));
}
else {
error('language installation failed');
}
//now, we update the md5key of the lang pack, this is used to check version
$md5file = $CFG->dataroot.'/lang/'.$pack.'/'.$pack.'.md5';
if ($file = fopen($md5file, 'w')){
fwrite($file, md5($contents)); //we should not pass md5 value from moodle.org, because some sites can't fopen,, and the value will not be obtainable
}
fclose($file);
@unlink ($langpack); //remove the zip file
echo '';
}
}
}
break;
case 4:
if (!optional_param('confirm') && confirm_sesskey()){
print_simple_box_start('center','100%');
echo '';
echo '';
echo '
';
print_simple_box_end();
}
else if (confirm_sesskey()){
$dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
$dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
$rm1 = false;
$rm2 = false;
if (file_exists($dest1)){
$rm1 = remove_dir($dest1);
}
if (file_exists($dest2)){
$rm2 = remove_dir($dest2);
}
//delete the direcotries
if ($rm1 or $rm2){
echo '';
print_string('langpackremoved','admin');
echo '
';
}
else {
error ('An error has occured, language pack is not completely uninstalled');
}
}
break;
default: //display choice mode
$source = 'http://download.moodle.org/lang16/languages.md5';
$remote = 0; //flag for reading from remote or local
if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.
while(!feof ($fp)) {
$availablelangs[] = split(',', fgets($fp,1024));
}
$remote = 1;
}
else { /// fopen failed, we find local copy of list.
$availablelangs = get_local_list_of_languages();
}
if (!$remote){
print_simple_box_start('center','60%');
echo '';
print_string('remotelangnotavailable');
echo '
';
print_simple_box_end();
}
print_simple_box_start('center','60%');
echo '| ';
echo get_string('installedlangs','admin');
echo ' | ';
echo get_string('availablelangs','admin');
echo ' |
';
echo '| ';
echo ' | ';
/// display to be installed Components here
//echo ' |
';
print_simple_box_end();
break;
} //close of main switch
print_footer();
//returns a list of available language packs from a local copy shipped with standard moodle distro
//this is for site that can't perform fopen
function get_local_list_of_languages(){
global $CFG;
$source = $CFG->wwwroot.'/lib/languages.txt';
$availablelangs = array();
if ($fp = fopen($source, 'r')){
while(!feof ($fp)) {
$availablelangs[] = split(',', fgets($fp,1024));
}
}
return $availablelangs;
}
//checks the md5 of the zip file, grabbed from download.moodle.org, and
function is_installed_lang($lang, $md5check){
global $CFG;
$md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
if (file_exists($md5file)){
return (file_get_contents($md5file) == $md5check);
}
return false;
}
?>