Example #1
0
/**
 * This function will introspect inside DB to detect it it's a UTF-8 DB or no
 * Used from setup.php to set correctly "set names" when the installation
 * process is performed without the initial and beautiful installer
 */
function setup_is_unicodedb()
{
    global $CFG, $db, $INSTALL;
    $unicodedb = false;
    // Calculate $CFG->dbfamily
    $dbfamily = set_dbfamily();
    switch ($dbfamily) {
        case 'mysql':
            $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'");
            if ($rs && $rs->RecordCount() > 0) {
                $records = $rs->GetAssoc(true);
                $encoding = $records['character_set_database']['Value'];
                if (strtoupper($encoding) == 'UTF8') {
                    $unicodedb = true;
                }
            }
            break;
        case 'postgres':
            /// Get PostgreSQL server_encoding value
            $rs = $db->Execute("SHOW server_encoding");
            if ($rs && $rs->RecordCount() > 0) {
                $encoding = $rs->fields['server_encoding'];
                if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') {
                    $unicodedb = true;
                }
            }
            break;
        case 'mssql':
            /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
            $unicodedb = true;
            break;
        case 'oracle':
            /// Get Oracle DB character set value
            $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'");
            if ($rs && $rs->RecordCount() > 0) {
                $encoding = $rs->fields['value'];
                if (strtoupper($encoding) == 'AL32UTF8') {
                    $unicodedb = true;
                }
            }
            break;
    }
    return $unicodedb;
}
Example #2
0
/**
 * This internal function, called from setup.php BEFORE stabilishing the DB
 * connection, defines the $CFG->dbfamily global -by calling set_dbfamily()-
 * and predefines some constants needed by ADOdb to switch some default
 * behaviours.
 *
 * This function must contain all the pre-connection code needed for each
 * dbtype supported.
 */
function preconfigure_dbconnection()
{
    global $CFG;
    /// Define dbfamily
    set_dbfamily();
    /// Based on $CFG->dbfamily, set some ADOdb settings
    switch ($CFG->dbfamily) {
        /// list here family types where we know
        /// the fieldnames will come in lowercase
        /// so we can avoid expensive tolower()
        case 'postgres':
        case 'mysql':
        case 'mssql':
            define('ADODB_ASSOC_CASE', 2);
            break;
        case 'oracle':
            define('ADODB_ASSOC_CASE', 0);
            /// Use lowercase fieldnames for ADODB_FETCH_ASSOC
            /// (only meaningful for oci8po, it's the default
            /// for other DB drivers so this won't affect them)
            /// Row prefetching uses a bit of memory but saves a ton
            /// of network latency. With current AdoDB and PHP, only
            /// Oracle uses this setting.
            define('ADODB_PREFETCH_ROWS', 1000);
            break;
        default:
            /// if we have to lowercase it, set to 0
            /// - note that the lowercasing is very expensive
            define('ADODB_ASSOC_CASE', 0);
            //Use lowercase fieldnames for ADODB_FETCH_ASSOC
    }
}
Example #3
0
/**
 * This internal function, called from setup.php, sets all the configuration
 * needed to work properly against any DB. It setups connection encoding
 * and some other variables. Also, ir defines the $CFG->dbfamily variable
 * to handle conditional code better than using $CFG->dbtype directly.
 *
 * This function must contain the init code needed for each dbtype supported.
 */
function configure_dbconnection()
{
    global $CFG, $db;
    switch ($CFG->dbtype) {
        case 'mysql':
        case 'mysqli':
            $db->Execute("SET NAMES 'utf8'");
            break;
        case 'postgres7':
            $db->Execute("SET NAMES 'utf8'");
            break;
        case 'mssql':
        case 'mssql_n':
        case 'odbc_mssql':
            /// No need to set charset. It must be specified in the driver conf
            /// Allow quoted identifiers
            $db->Execute('SET QUOTED_IDENTIFIER ON');
            /// Force ANSI nulls so the NULL check was done by IS NULL and NOT IS NULL
            /// instead of equal(=) and distinct(<>) simbols
            $db->Execute('SET ANSI_NULLS ON');
            /// Enable sybase quotes, so addslashes and stripslashes will use "'"
            ini_set('magic_quotes_sybase', '1');
            /// NOTE: Not 100% useful because GPC has been addslashed with the setting off
            ///       so IT'S MANDATORY TO CHANGE THIS UNDER php.ini or .htaccess for this DB
            ///       or to turn off magic_quotes to allow Moodle to do it properly
            break;
        case 'oci8po':
            /// No need to set charset. It must be specified by the NLS_LANG env. variable
            /// Enable sybase quotes, so addslashes and stripslashes will use "'"
            ini_set('magic_quotes_sybase', '1');
            /// NOTE: Not 100% useful because GPC has been addslashed with the setting off
            ///       so IT'S MANDATORY TO ENABLE THIS UNDER php.ini or .htaccess for this DB
            ///       or to turn off magic_quotes to allow Moodle to do it properly
            break;
    }
    /// Finally define dbfamily
    set_dbfamily();
}
/**
 * This function will check if database requirements are satisfied
 * @param string $version xml version we are going to use to test this server
 * @return object results encapsulated in one environment_result object
 */
function environment_check_database($version)
{
    global $db;
    $result = new environment_results('database');
    $vendors = array();
    //Array of vendors in version
    /// Get the enviroment version we need
    if (!($data = get_environment_for_version($version))) {
        /// Error. No version data found
        $result->setStatus(false);
        $result->setErrorCode(NO_VERSION_DATA_FOUND);
        return $result;
    }
    /// Extract the database part
    if (!isset($data['#']['DATABASE'])) {
        /// Error. No DATABASE section found
        $result->setStatus(false);
        $result->setErrorCode(NO_DATABASE_SECTION_FOUND);
        return $result;
    } else {
        /// Extract level
        $level = get_level($data['#']['DATABASE']['0']);
    }
    /// Extract DB vendors. At least 2 are mandatory (mysql & postgres)
    if (!isset($data['#']['DATABASE']['0']['#']['VENDOR'])) {
        /// Error. No VENDORS found
        $result->setStatus(false);
        $result->setErrorCode(NO_DATABASE_VENDORS_FOUND);
        return $result;
    } else {
        /// Extract vendors
        foreach ($data['#']['DATABASE']['0']['#']['VENDOR'] as $vendor) {
            if (isset($vendor['@']['name']) && isset($vendor['@']['version'])) {
                $vendors[$vendor['@']['name']] = $vendor['@']['version'];
                $vendorsxml[$vendor['@']['name']] = $vendor;
            }
        }
    }
    /// Check we have the mysql vendor version
    if (empty($vendors['mysql'])) {
        $result->setStatus(false);
        $result->setErrorCode(NO_DATABASE_VENDOR_MYSQL_FOUND);
        return $result;
    }
    /// Check we have the postgres vendor version
    if (empty($vendors['postgres'])) {
        $result->setStatus(false);
        $result->setErrorCode(NO_DATABASE_VENDOR_POSTGRES_FOUND);
        return $result;
    }
    /// Now search the version we are using (depending of vendor)
    $current_vendor = set_dbfamily();
    $dbinfo = $db->ServerInfo();
    $current_version = normalize_version($dbinfo['version']);
    $needed_version = $vendors[$current_vendor];
    /// Check we have a needed version
    if (!$needed_version) {
        $result->setStatus(false);
        $result->setErrorCode(NO_DATABASE_VENDOR_VERSION_FOUND);
        return $result;
    }
    /// And finally compare them, saving results
    if (version_compare($current_version, $needed_version, '>=')) {
        $result->setStatus(true);
    } else {
        $result->setStatus(false);
    }
    $result->setLevel($level);
    $result->setCurrentVersion($current_version);
    $result->setNeededVersion($needed_version);
    $result->setInfo($current_vendor);
    /// Do any actions defined in the XML file.
    process_environment_result($vendorsxml[$current_vendor], $result);
    return $result;
}
Example #5
0
/**
 * This internal function, called from setup.php, sets all the configuration
 * needed to work properly against any DB. It setups connection encoding
 * and some other variables. Also, ir defines the $CFG->dbfamily variable
 * to handle conditional code better than using $CFG->dbtype directly.
 *
 * This function must contain the init code needed for each dbtype supported.
 */
function configure_dbconnection()
{
    global $CFG, $db;
    switch ($CFG->dbtype) {
        case 'mysql':
        case 'mysqli':
            $db->Execute("SET NAMES 'utf8'");
            break;
        case 'postgres7':
            $db->Execute("SET NAMES 'utf8'");
            break;
        case 'mssql':
        case 'mssql_n':
        case 'odbc_mssql':
            /// No need to set charset. It must be specified in the driver conf
            /// Allow quoted identifiers
            $db->Execute('SET QUOTED_IDENTIFIER ON');
            /// Force ANSI nulls so the NULL check was done by IS NULL and NOT IS NULL
            /// instead of equal(=) and distinct(<>) simbols
            $db->Execute('SET ANSI_NULLS ON');
            /// Enable sybase quotes, so addslashes and stripslashes will use "'"
            ini_set('magic_quotes_sybase', '1');
            /// NOTE: Not 100% useful because GPC has been addslashed with the setting off
            ///       so IT'S MANDATORY TO CHANGE THIS UNDER php.ini or .htaccess for this DB
            ///       or to turn off magic_quotes to allow Moodle to do it properly
            break;
        case 'oci8po':
            /// No need to set charset. It must be specified by the NLS_LANG env. variable
            /// Enable sybase quotes, so addslashes and stripslashes will use "'"
            ini_set('magic_quotes_sybase', '1');
            /// NOTE: Not 100% useful because GPC has been addslashed with the setting off
            ///       so IT'S MANDATORY TO ENABLE THIS UNDER php.ini or .htaccess for this DB
            ///       or to turn off magic_quotes to allow Moodle to do it properly
            /// Now set the decimal separator to DOT, Moodle & PHP will always send floats to
            /// DB using DOTS. Manually introduced floats (if using other characters) must be
            /// converted back to DOTs (like gradebook does)
            $db->Execute("ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,'");
            break;
    }
    /// Finally define dbfamily
    set_dbfamily();
}