From 0bda70315bdebe47ac57fab68e0478af6381b51a Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 29 Aug 2010 11:33:04 +0000 Subject: [PATCH] MDL-23984 improved dataroot handling in installer --- install.php | 10 +++------ lib/installlib.php | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/install.php b/install.php index 10558641c12..2ef2f761244 100644 --- a/install.php +++ b/install.php @@ -304,7 +304,7 @@ if ($config->stage == INSTALL_DOWNLOADLANG) { $hint_dataroot = get_string('pathsroparentdataroot', 'install', $a); $config->stage = INSTALL_PATHS; } else { - if (!make_upload_directory('lang', false)) { + if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) { $hint_dataroot = get_string('pathserrcreatedataroot', 'install', $a); $config->stage = INSTALL_PATHS; } @@ -344,12 +344,8 @@ if ($config->stage == INSTALL_DATABASETYPE) { if ($config->stage == INSTALL_DOWNLOADLANG) { $downloaderror = ''; -// Create necessary lang dir - if (!make_upload_directory('lang', false)) { - $downloaderror = get_string('cannotcreatelangdir', 'error'); - -// Download and install lang component - } else if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) { +// Download and install lang component, lang dir was already created in install_init_dataroot + if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) { if ($cd->install() == COMPONENT_ERROR) { if ($cd->get_error() == 'remotedownloaderror') { $a = new stdClass(); diff --git a/lib/installlib.php b/lib/installlib.php index e7cacf9190b..1867b9e6213 100644 --- a/lib/installlib.php +++ b/lib/installlib.php @@ -78,6 +78,57 @@ function install_ini_get_bool($ini_get_arg) { return false; } +/** + * Creates dataroot if not exists yet, + * makes sure it is writable, add lang directory + * and add .htaccess just in case it works. + * + * @param string $dataroot full path to dataroot + * @param int $dirpermissions + * @return bool success + */ +function install_init_dataroot($dataroot, $dirpermissions) { + if (file_exists($dataroot) and !is_dir($dataroot)) { + // file with the same name exists + return false; + } + + umask(0000); + if (!file_exists($dataroot)) { + if (!mkdir($dataroot, $dirpermissions, true)) { + // most probably this does not work, but anyway + return false; + } + } + @chmod($dataroot, $dirpermissions); + + if (!is_writable($dataroot)) { + return false; // we can not continue + } + + // now create the lang folder - we need it and it makes sure we can really write in dataroot + if (!is_dir("$dataroot/lang")) { + if (!mkdir("$dataroot/lang", $dirpermissions, true)) { + return false; + } + } + if (!is_writable("$dataroot/lang")) { + return false; // we can not continue + } + + // finally just in case some broken .htaccess that prevents access just in case it is allowed + if (!file_exists("$dataroot/.htaccess")) { + if ($handle = fopen("$dataroot/.htaccess", 'w')) { + fwrite($handle, "deny from all\r\nAllowOverride None\r\nNote: this file is broken intentionally, we do not want anybody to undo it in subdirectory!\r\n"); + fclose($handle); + } else { + return false; + } + } + + return true; +} + /** * Print help button * @param string $url