<commit_before><?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Functions to support installation process
 *
 * @package    core
 * @subpackage install
 * @copyright  2009 <NAME> (http://skodak.org)
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

/** INSTALL_WELCOME = 0 */
define('INSTALL_WELCOME',       0);
/** INSTALL_ENVIRONMENT = 1 */
define('INSTALL_ENVIRONMENT',   1);
/** INSTALL_PATHS = 2 */
define('INSTALL_PATHS',         2);
/** INSTALL_DOWNLOADLANG = 3 */
define('INSTALL_DOWNLOADLANG',  3);
/** INSTALL_DATABASETYPE = 4 */
define('INSTALL_DATABASETYPE',  4);
/** INSTALL_DATABASE = 5 */
define('INSTALL_DATABASE',      5);
/** INSTALL_SAVE = 6 */
define('INSTALL_SAVE',          6);

/**
 * Tries to detect the right www root setting.
 * @return string detected www root
 */
function install_guess_wwwroot() {
    $wwwroot = '';
    if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
        $wwwroot .= 'http://';
    } else {
        $wwwroot .= 'https://';
    }
    $hostport = explode(':', $_SERVER['HTTP_HOST']);
    $wwwroot .= reset($hostport);
    if ($_SERVER['SERVER_PORT'] != 80 and $_SERVER['SERVER_PORT'] != '443') {
        $wwwroot .= ':'.$_SERVER['SERVER_PORT'];
    }
    $wwwroot .= $_SERVER['SCRIPT_NAME'];

    list($wwwroot, $xtra) = explode('/install.php', $wwwroot);

    return $wwwroot;
}

/**
 * Copy of @see{ini_get_bool()}
 * @param string $ini_get_arg
 * @return bool
 */
function install_ini_get_bool($ini_get_arg) {
    $temp = ini_get($ini_get_arg);

    if ($temp == '1' or strtolower($temp) == 'on') {
        return true;
    }
    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
 * @param string $titel
 * @return void
 */
function install_helpbutton($url, $title='') {<commit_msg>MDL-30381 install_init_dataroot() now creates temp and cache<commit_after><?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Functions to support installation process
 *
 * @package    core
 * @subpackage install
 * @copyright  2009 <NAME> (http://skodak.org)
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

/** INSTALL_WELCOME = 0 */
define('INSTALL_WELCOME',       0);
/** INSTALL_ENVIRONMENT = 1 */
define('INSTALL_ENVIRONMENT',   1);
/** INSTALL_PATHS = 2 */
define('INSTALL_PATHS',         2);
/** INSTALL_DOWNLOADLANG = 3 */
define('INSTALL_DOWNLOADLANG',  3);
/** INSTALL_DATABASETYPE = 4 */
define('INSTALL_DATABASETYPE',  4);
/** INSTALL_DATABASE = 5 */
define('INSTALL_DATABASE',      5);
/** INSTALL_SAVE = 6 */
define('INSTALL_SAVE',          6);

/**
 * Tries to detect the right www root setting.
 * @return string detected www root
 */
function install_guess_wwwroot() {
    $wwwroot = '';
    if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
        $wwwroot .= 'http://';
    } else {
        $wwwroot .= 'https://';
    }
    $hostport = explode(':', $_SERVER['HTTP_HOST']);
    $wwwroot .= reset($hostport);
    if ($_SERVER['SERVER_PORT'] != 80 and $_SERVER['SERVER_PORT'] != '443') {
        $wwwroot .= ':'.$_SERVER['SERVER_PORT'];
    }
    $wwwroot .= $_SERVER['SCRIPT_NAME'];

    list($wwwroot, $xtra) = explode('/install.php', $wwwroot);

    return $wwwroot;
}

/**
 * Copy of @see{ini_get_bool()}
 * @param string $ini_get_arg
 * @return bool
 */
function install_ini_get_bool($ini_get_arg) {
    $temp = ini_get($ini_get_arg);

    if ($temp == '1' or strtolower($temp) == 'on') {
        return true;
    }
    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
    }

    // create the directory for $CFG->tempdir
    if (!is_dir("$dataroot/temp")) {
        if (!mkdir("$dataroot/temp", $dirpermissions, true)) {
            return false;
        }
    }
    if (!is_writable("$dataroot/temp")) {
        return false; // we can not continue
    }

    // create the directory for $CFG->cachedir
    if (!is_dir("$dataroot/cache")) {
        if (!mkdir("$dataroot/cache", $dirpermissions, true)) {
            return false;
        }
    }
    if (!is_writable("$dataroot/cache")) {
        return false; // we can not continue
    }

    // create the directory for $CFG->langotherroot
    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
 * @param string $titel
 * @return void
 */
function install_helpbutton($url, $title='') {
{'fraction_non_alphanumeric': 0.1434322521279043, 'fraction_numerical': 0.00747642051989878, 'mean_word_length': 2.820298769771529, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 28, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2051554', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2724, 'n_words': 827}