require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
+ require_once($CFG->libdir.'/filelib.php');
+ require_once($CFG->libdir.'/componentlib.class.php');
admin_externalpage_setup('langimport');
$sitelang = optional_param('sitelangconfig', '', PARAM_FILE);
define('INSTALLATION_OF_SELECTED_LANG', 2);
- define('CHANGE_SITE_LANG', 3);
define('DELETION_OF_SELECTED_LANG', 4);
define('UPDATE_ALL_LANG', 5);
- $strlang = get_string('langimport','admin');
-
- $strlanguage = get_string("language");
- $strthislanguage = get_string("thislanguage");
- $title = $strlang;
-
- admin_externalpage_print_header();
+ $strlang = get_string('langimport','admin');
+ $strlanguage = get_string('language');
+ $strthislanguage = get_string('thislanguage');
+ $title = $strlang;
//reset and diagnose lang cache permissions
@unlink($CFG->dataroot.'/cache/languages');
if (file_exists($CFG->dataroot.'/cache/languages')) {
- notify('Language cache can not be deleted, please check permissions in dataroot.');
+ error('Language cache can not be deleted, please fix permissions in dataroot/cache/languages!');
}
get_list_of_languages(true); //refresh lang cache
+ $notice_ok = array();
+ $notice_error = array();
+
switch ($mode){
case INSTALLATION_OF_SELECTED_LANG: ///installation of selected language pack
@mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
@mkdir ($CFG->dataroot.'/lang/');
- require_once($CFG->libdir.'/componentlib.class.php');
if ($cd = new component_installer('http://download.moodle.org', 'lang16',
$pack.'.zip', 'languages.md5', 'lang')) {
$status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
switch ($status) {
- case ERROR:
- if ($cd->get_error() == 'remotedownloadnotallowed') {
- $a = new stdClass();
+ case ERROR:
+ if ($cd->get_error() == 'remotedownloaderror') {
+ $a = new object();
$a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
$a->dest= $CFG->dataroot.'/lang';
- error(get_string($cd->get_error(), 'error', $a));
+ error(get_string($cd->get_error(), 'error', $a), 'langimport.php');
} else {
- error(get_string($cd->get_error(), 'error'));
+ error(get_string($cd->get_error(), 'error'), 'langimport.php');
}
break;
case INSTALLED:
- get_list_of_languages(true); //refresh lang cache
- redirect('langimport.php', get_string('langpackupdated','admin',$pack), -1);
+ $notice_ok[] = get_string('langpackinstalled','admin',$pack);
break;
case UPTODATE:
}
break;
- case CHANGE_SITE_LANG: //change site language
-
- if (confirm_sesskey()) {
- $langconfig = get_record('config','name','lang');
- $langconfig->value = $sitelang;
- if (!empty($sitelang) && update_record('config',$langconfig)){
- redirect('langimport.php', get_string('sitelangchanged','admin'));
- } else {
- error('Could not update the default site language!');
- }
- }
-
- break;
case DELETION_OF_SELECTED_LANG: //delete a directory(ies) containing a lang pack completely
- if (!$confirm && confirm_sesskey()) {
+ if ($uninstalllang == 'en_utf8') {
+ $notice_error[] = 'en_utf8 can not be uninstalled!';
+
+ } else if (!$confirm && confirm_sesskey()) {
+ admin_externalpage_print_header();
notice_yesno(get_string('uninstallconfirm', 'admin', $uninstalllang),
- 'langimport.php?mode=4&uninstalllang='.$uninstalllang.'&confirm=1&sesskey='.sesskey(),
+ 'langimport.php?mode='.DELETION_OF_SELECTED_LANG.'&uninstalllang='.$uninstalllang.'&confirm=1&sesskey='.sesskey(),
'langimport.php');
+ print_footer();
+ die;
+
} else if (confirm_sesskey()) {
- if ($uninstalllang == 'en_utf8') {
- error ('en_utf8 can not be uninstalled!');
- }
$dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
$dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
$rm1 = false;
get_list_of_languages(true); //refresh lang cache
//delete the direcotries
if ($rm1 or $rm2) {
- redirect('langimport.php', get_string('langpackremoved','admin'), 3);
+ $notice_ok[] = get_string('langpackremoved','admin');
} else { //nothing deleted, possibly due to permission error
- error('An error has occurred, language pack is not completely uninstalled, please check file permissions');
+ $notice_error[] = 'An error has occurred, language pack is not completely uninstalled, please check file permissions';
}
}
break;
//0th pull a list from download.moodle.org,
//key = langname, value = md5
- $source = 'http://download.moodle.org/lang16/languages.md5';
$md5array = array();
$updated = 0; //any packs updated?
$alllangs = array_keys(get_list_of_languages(false, true)); //get all available langs
$packs = array(); //all the packs that needs updating
- if (!$availablelangs = proxy_url($source)) {
+ if (!$availablelangs = get_remote_list_of_languages()) {
print_error('cannotdownloadlanguageupdatelist');
}
$md5array[$alang[0]] = $alang[1];
}
- //filtering out non-16 packs
+ //filtering out non-16 and unofficial packs
foreach ($alllangs as $clang) {
+ if (!array_key_exists($clang, $md5array)) {
+ $notice_ok[] = get_string('langpackupdateskipped', 'admin', $clang);
+ continue;
+ }
$dest1 = $CFG->dataroot.'/lang/'.$clang;
$dest2 = $CFG->dirroot.'/lang/'.$clang;
if ($pack == 'en_utf8') { // no update for en_utf8
continue;
}
+
//1. delete old director(ies)
$dest1 = $CFG->dataroot.'/lang/'.$pack;
$rm1 = false;
$rm2 = false;
if (file_exists($dest1)) {
- $rm1 = remove_dir($dest1);
+ if (!remove_dir($dest1)) {
+ $notice_error[] = 'Could not delete old directory '.$dest1.', update of '.$pack.' failed, please check permissions.';
+ continue;
+ }
}
if (file_exists($dest2)) {
- $rm2 = remove_dir($dest2);
- }
- if (!($rm1 || $rm2)) {
- error ('could not delete old directory, update failed');
+ if (!remove_dir($dest2)) {
+ $notice_error[] = 'Could not delete old directory '.$dest2.', update of '.$pack.' failed, please check permissions.';
+ continue;
+ }
}
//2. copy & unzip into new
- require_once($CFG->libdir.'/componentlib.class.php');
if ($cd = new component_installer('http://download.moodle.org', 'lang16',
$pack.'.zip', 'languages.md5', 'lang')) {
$status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
switch ($status) {
case ERROR:
- if ($cd->get_error() == 'remotedownloadnotallowed') {
+ if ($cd->get_error() == 'remotedownloaderror') {
$a = new stdClass();
$a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
$a->dest= $CFG->dataroot.'/lang';
- error(get_string($cd->get_error(), 'error', $a));
+ error(get_string($cd->get_error(), 'error', $a)); // not probable
} else {
- error(get_string($cd->get_error(), 'error'));
+ error(get_string($cd->get_error(), 'error')); // not probable
}
break;
case UPTODATE:
//Print error string or whatever you want to do
break;
case INSTALLED:
- notify(get_string('langpackupdated','admin',$pack), 'notifysuccess');
+ $notice_ok[] = get_string('langpackupdated', 'admin', $pack);
$updated = true;
//Print/do whatever you want
break;
}
if ($updated) {
- notice(get_string('langupdatecomplete','admin'), 'langimport.php');
+ $notice_ok[] = get_string('langupdatecomplete','admin');
} else {
- notice(get_string('nolangupdateneeded','admin'), 'langimport.php');
+ $notice_ok[] = get_string('nolangupdateneeded','admin');
}
break;
+ } //close of main switch
- default: //display choice mode
- $source = 'http://download.moodle.org/lang16/languages.md5';
- $remote = 0; //flag for reading from remote or local
+ admin_externalpage_print_header();
- if ($availablelangs = proxy_url($source)) {
- $remote = 1;
- } else {
- $availablelangs = get_local_list_of_languages();
- }
-/*
- if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.
- while(!feof ($fp)) {
- $availablelangs[] = split(',', fgets($fp,1024));
- }
- $remote = 1; //can read from download.moodle.org
- } else { /// fopen failed, we find local copy of list.
- $availablelangs = get_local_list_of_languages();
- }
-*/
- if (!$remote) {
- print_box_start();
- print_string('remotelangnotavailable','admin',$CFG->dataroot.'/lang/');
- print_box_end();
- }
+ $installedlangs = get_list_of_languages(true, true);
- print_box_start();
- echo '<table summary="">';
- echo '<tr><td align="center" valign="top">';
- echo '<form id="uninstallform" action="langimport.php?mode=4" method="post">';
- echo '<fieldset class="invisiblefieldset">';
- echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
- $installedlangs = get_list_of_languages(false, true);
+ if ($availablelangs = get_remote_list_of_languages()) {
+ $remote = 1;
+ } else {
+ $remote = 0; //flag for reading from remote or local
+ $availablelangs = get_local_list_of_languages();
+ }
- /// display installed langs here
+ if (!$remote) {
+ print_box_start();
+ print_string('remotelangnotavailable', 'admin', $CFG->dataroot.'/lang/');
+ print_box_end();
+ }
- echo '<label for="uninstalllang">'.get_string('installedlangs','admin')."</label><br />\n";
- echo '<select name="uninstalllang" id="uninstalllang" size="15">';
- foreach ($installedlangs as $clang =>$ilang){
- echo '<option value="'.$clang.'">'.$ilang.'</option>';
- }
- echo '</select>';
- echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
- echo '</fieldset>';
- echo '</form>';
- echo '<form id="updateform" action="langimport.php?mode=5" method="post">';
- echo '<div>';
- echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
- echo '</div>';
- echo '</form>';
-
- /// Display option to change site language
-
- /// display to be installed langs here
-
- echo '</td><td align="center" valign="top">';
- //availabe langs table
- $empty = 1; //something to pring
-
- /// if this language pack is not already installed, then we allow installation
-
- echo '<form id="installform" method="post" action="langimport.php?mode=2">';
- echo '<fieldset class="invisiblefieldset">';
- echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
- echo '<label for="pack">'.get_string('availablelangs','admin')."</label><br />\n";
- if ($remote) {
- echo '<select name="pack" id="pack" size="15">';
- }
+ if ($notice_ok) {
+ $info = implode('<br />', $notice_ok);
+ notify($info, 'notifysuccess');
+ }
- foreach ($availablelangs as $alang) {
- if (trim($alang[0]) != "en_utf8") {
- if ($remote){
- if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
- $shortlang = substr($alang[0], 0, -5);
- } else {
- $shortlang = $alang[0];
- }
- if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed
- echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$shortlang.')</option>';
- }
- } else { //print list in local format, and instruction to install
- echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a></td></tr>';
- }
- $empty = 0;
- }
- }
- if ($remote) {
- echo '</select>';
- echo '<br /><input type="submit" value="'.$THEME->larrow.' '.get_string('install','admin').'" />';
- }
- echo '</fieldset>';
- echo '</form>';
+ if ($notice_error) {
+ $info = implode('<br />', $notice_error);
+ notify($info, 'notifyproblem');
+ }
+
+ print_box_start();
+ echo '<table summary="">';
+ echo '<tr><td align="center" valign="top">';
+ echo '<form id="uninstallform" action="langimport.php?mode='.DELETION_OF_SELECTED_LANG.'" method="post">';
+ echo '<fieldset class="invisiblefieldset">';
+ echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
+
+ /// display installed langs here
+
+ echo '<label for="uninstalllang">'.get_string('installedlangs','admin')."</label><br />\n";
+ echo '<select name="uninstalllang" id="uninstalllang" size="15">';
+ foreach ($installedlangs as $clang =>$ilang){
+ echo '<option value="'.$clang.'">'.$ilang.'</option>';
+ }
+ echo '</select>';
+ echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
+ echo '</fieldset>';
+ echo '</form>';
+
+ if ($remote) {
+ echo '<form id="updateform" action="langimport.php?mode='.UPDATE_ALL_LANG.'" method="post">';
+ echo '<div>';
+ echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
+ echo '</div>';
+ echo '</form>';
+ }
+
+ /// Display option to change site language
+
+ /// display to be installed langs here
+
+ echo '</td><td align="center" valign="top">';
+ //availabe langs table
+ $empty = 1; //something to pring
+
+ /// if this language pack is not already installed, then we allow installation
+
+ echo '<form id="installform" method="post" action="langimport.php?mode='.INSTALLATION_OF_SELECTED_LANG.'">';
+ echo '<fieldset class="invisiblefieldset">';
+ echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
+ echo '<label for="pack">'.get_string('availablelangs','admin')."</label><br />\n";
+ if ($remote) {
+ echo '<select name="pack" id="pack" size="15">';
+ }
- if ($empty) {
- echo '<br />';
- print_string('nolanguagetodownload','admin');
+ foreach ($availablelangs as $alang) {
+ if ($alang[0] == '') {
+ continue;
+ }
+ if (trim($alang[0]) != "en_utf8") {
+ if ($remote) {
+ if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
+ $shortlang = substr($alang[0], 0, -5);
+ } else {
+ $shortlang = $alang[0];
+ }
+ if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed
+ echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$shortlang.')</option>';
+ }
+ } else { //print list in local format, and instruction to install
+ echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a></td></tr>';
}
+ $empty = 0;
+ }
+ }
+ if ($remote) {
+ echo '</select>';
+ echo '<br /><input type="submit" value="'.$THEME->larrow.' '.get_string('install','admin').'" />';
+ }
+ echo '</fieldset>';
+ echo '</form>';
- //close available langs table
- echo '</td></tr></table>';
- print_box_end();
- break;
+ if ($empty) {
+ echo '<br />';
+ print_string('nolanguagetodownload','admin');
+ }
- } //close of main switch
+ //close available langs table
+ echo '</td></tr></table>';
+ print_box_end();
admin_externalpage_print_footer();
- /* returns a list of available language packs from a
+ /**
+ * 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
* @return array
*/
function get_local_list_of_languages() {
global $CFG;
- $source = $CFG->wwwroot.'/lib/languages.md5';
+ $source = $CFG->dirroot.'/lib/languages.md5';
$availablelangs = array();
- if ($fp = fopen($source, 'r')){
+ 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,
+ /**
+ * checks the md5 of the zip file, grabbed from download.moodle.org,
* against the md5 of the local language file from last update
* @param string $lang
* @param string $md5check
return false;
}
- //returns an array of languages, or false if can not read from source
- function proxy_url($url) {
- global $CFG;
-
+ /**
+ * Returns the latest list of available language packs from
+ * moodle.org
+ * @return array or false if can not download
+ */
+ function get_remote_list_of_languages() {
+ $source = 'http://download.moodle.org/lang16/languages.md5';
$availablelangs = array();
- if( $content = download_file_content($url) ){
-
+ if ($content = download_file_content($source)) {
$alllines = split("\n", $content);
- foreach($alllines as $line){
- if(!empty($line)){
+ foreach($alllines as $line) {
+ if (!empty($line)){
$availablelangs[] = split(',', $line);
}
}
-
return $availablelangs;
- }else{
+
+ } else {
return false;
}
}
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
-// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
+// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
// //
// This program is free software; you can redistribute it and/or modify //
// This library includes all the necessary stuff to use the one-click
// download and install feature of Moodle, used to keep updated some
// items like languages, pear, enviroment... i.e, components.
-//
-// It has been developed harcoding some important limits that are
+//
+// It has been developed harcoding some important limits that are
// explained below:
// - It only can check, download and install items under moodledata.
// - Every downloadeable item must be one zip file.
// - The zip file root content must be 1 directory, i.e, everything
// is stored under 1 directory.
-// - Zip file name and root directory must have the same name (but
+// - Zip file name and root directory must have the same name (but
// the .zip extension, of course).
// - Every .zip file must be defined in one .md5 file that will be
// stored in the same remote directory than the .zip file.
// - The name of such .md5 file is free, although it's recommended
-// to use the same name than the .zip (that's the default
+// to use the same name than the .zip (that's the default
// assumption if no specified).
// - Every remote .md5 file will be a comma separated (CVS) file where each
// line will follow this format:
// - Will contain the md5 od the latest installed component
// With all these details present, the process will perform this tasks:
// - Perform security checks. Only admins are allowed to use this for now.
-// - Perform server checks. fopen must allow to open remote URLs.
// - Read the .md5 file from source (1).
// - Extract the correct line for the .zip being requested.
// - Compare it with the local .md5 file (2).
// - If different:
// - ERROR. Old package won't be modified. We shouldn't
// reach here ever.
-// - If fopen is not available, a message text about how to do
-// the process manually (remotedownloadnotallowed) must be
-// built to explain it.
+// - If component download is not possible, a message text about how to do
+// the process manually (remotedownloaderror) must be displayed to explain it.
//
// General Usage:
//
// To install one component:
//
// require_once($CFG->libdir.'/componentlib.class.php');
-// if ($cd = new component_installer('http://download.moodle.org', 'lang16',
+// if ($cd = new component_installer('http://download.moodle.org', 'lang16',
// 'es_utf8.zip', 'languages.md5', 'lang')) {
// $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
// switch ($status) {
-// case ERROR:
-// if ($cd->get_error() == 'remotedownloadnotallowed') {
+// case ERROR:
+// if ($cd->get_error() == 'remotedownloaderror') {
// $a = new stdClass();
// $a->url = 'http://download.moodle.org/lang16/es_utf8.zip';
// $a->dest= $CFG->dataroot.'/lang';
class component_installer {
var $sourcebase; /// Full http URL, base for downloadable items
- var $zippath; /// Relative path (from sourcebase) where the
+ var $zippath; /// Relative path (from sourcebase) where the
/// downloadeable item resides.
var $zipfilename; /// Name of the .zip file to be downloaded
var $md5filename; /// Name of the .md5 file to be read
* without performing any check at all.
*
* @param string Full http URL, base for downloadeable items
- * @param string Relative path (from sourcebase) where the
+ * @param string Relative path (from sourcebase) where the
* downloadeable item resides
* @param string Name of the .zip file to be downloaded
- * @param string Name of the .md5 file to be read (default '' = same
+ * @param string Name of the .md5 file to be read (default '' = same
* than zipfilename)
- * @param string Relative path (from moodledata) where the .zip file will
+ * @param string Relative path (from moodledata) where the .zip file will
* be expanded (default='' = moodledataitself)
* @return object
*/
/**
* This function will check if everything is properly set to begin
- * one installation. It'll check for fopen wrappers enabled and
- * admin privileges. Also, it will check for required settings
+ * one installation. Also, it will check for required settings
* and will fill everything as needed.
*
* @return boolean true/false (plus detailed error in errorstring)
$this->requisitesok = false;
- /// Check for fopen remote enabled
- if (!ini_get('allow_url_fopen')) {
- $this->errorstring='remotedownloadnotallowed';
- return false;
- }
/// Check that everything we need is present
if (empty($this->sourcebase) || empty($this->zippath) || empty($this->zipfilename)) {
$this->errorstring='missingrequiredfield';
return false;
}
}
- /// Calculate the componentnamea
+ /// Calculate the componentname
$pos = stripos($this->zipfilename, '.zip');
$this->componentname = substr($this->zipfilename, 0, $pos);
/// Calculate md5filename if it's empty
$destinationdir = $CFG->dataroot.'/'.$this->destpath;
$destinationcomponent = $destinationdir.'/'.$this->componentname;
@remove_dir($destinationcomponent.'_old'); //Deleting possible old components before
- @rename ($destinationcomponent, $destinationcomponent.'_old'); //Moving to a safe place
+ @rename ($destinationcomponent, $destinationcomponent.'_old'); //Moving to a safe place
/// Unzip new version
if (!unzip_file($zipfile, $destinationdir, false)) {
/// Error so, go back to the older
}
}
- /**
+ /**
* This function will change the zip file to install on the fly
* to allow the class to process different components of the
* same md5 file without intantiating more objects.
}
/// Get all components of md5 file
if (!$comp_arr = $this->get_all_components_md5()) {
- $this->errorstring='cannotdownloadcomponents';
+ if (empty($this->errorstring)) {
+ $this->errorstring='cannotdownloadcomponents';
+ }
return false;
}
/// Search for the componentname component
return $component[1];
}
- /**
- * This function allows you to retrieve the complete array of components found in
+ /**
+ * This function allows you to retrieve the complete array of components found in
* the md5filename
*
* @return array array of components in md5 file or false if error
$this->cachedmd5components[$source] = $comp_arr;
} else {
/// Return error
- $this->errorstring='cannotdownloadcomponents';
+ $this->errorstring='remotedownloaderror';
return false;
}
}
- /// If there is no commponents, error
- if (empty($comp_arr)) {
+ /// If there is no commponents or erros found, error
+ if (!empty($this->errorstring)) {
+ return false;
+
+ } else if (empty($comp_arr)) {
$this->errorstring='cannotdownloadcomponents';
return false;
}
}
/** This function returns the extramd5 field (optional in md5 file)
- *
+ *
* @return string the extramd5 field
*/
function get_extra_md5_field() {