Example #1
0
require_once $CFG->libdir . '/eventslib.php';
// Events functions
require_once $CFG->libdir . '/grouplib.php';
// Groups functions
require_once $CFG->libdir . '/sessionlib.php';
// All session and cookie related stuff
require_once $CFG->libdir . '/editorlib.php';
// All text editor related functions and classes
require_once $CFG->libdir . '/messagelib.php';
// Messagelib functions
require_once $CFG->libdir . '/modinfolib.php';
// Cached information on course-module instances
// make sure PHP is not severly misconfigured
setup_validate_php_configuration();
// Connect to the database
setup_DB();
// Disable errors for now - needed for installation when debug enabled in config.php
if (isset($CFG->debug)) {
    $originalconfigdebug = $CFG->debug;
    unset($CFG->debug);
} else {
    $originalconfigdebug = -1;
}
// Load up any configuration from the config table
initialise_cfg();
// Verify upgrade is not running unless we are in a script that needs to execute in any case
if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) {
    if ($CFG->upgraderunning < time()) {
        unset_config('upgraderunning');
    } else {
        print_error('upgraderunning');
 /**
  * Sets up global $DB moodle_database instance
  *
  * @global stdClass $CFG The global configuration instance.
  * @see config.php
  * @see config-dist.php
  * @global stdClass $DB The global moodle_database instance.
  * @return void|bool Returns true when finished setting up $DB. Returns void when $DB has already been set.
  */
 function setup_ExternalDB()
 {
     global $CFG, $DB, $remotedb;
     // Use a custom $remotedb (and not current system's $DB) if set - code sourced from configurable
     // Reports plugin.
     $remotedbhost = get_config('report_myfeedback', 'dbhost');
     $remotedbname = get_config('report_myfeedback', 'dbname');
     $remotedbuser = get_config('report_myfeedback', 'dbuser');
     $remotedbpass = get_config('report_myfeedback', 'dbpass');
     if (empty($remotedbhost) or empty($remotedbname) or empty($remotedbuser)) {
         $remotedb = $DB;
         setup_DB();
     } else {
         //
         if (!isset($CFG->dblibrary)) {
             $CFG->dblibrary = 'native';
             // use new drivers instead of the old adodb driver names
             switch ($CFG->dbtype) {
                 case 'postgres7':
                     $CFG->dbtype = 'pgsql';
                     break;
                 case 'mssql_n':
                     $CFG->dbtype = 'mssql';
                     break;
                 case 'oci8po':
                     $CFG->dbtype = 'oci';
                     break;
                 case 'mysql':
                     $CFG->dbtype = 'mysqli';
                     break;
             }
         }
         if (!isset($CFG->dboptions)) {
             $CFG->dboptions = array();
         }
         if (isset($CFG->dbpersist)) {
             $CFG->dboptions['dbpersist'] = $CFG->dbpersist;
         }
         if (!($remotedb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary))) {
             throw new dml_exception('dbdriverproblem', "Unknown driver {$CFG->dblibrary}/{$CFG->dbtype}");
         }
         try {
             $remotedb->connect($remotedbhost, $remotedbuser, $remotedbpass, $remotedbname, $CFG->prefix, $CFG->dboptions);
         } catch (moodle_exception $e) {
             if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
                 $body = "Connection error: " . $CFG->wwwroot . "\n\nInfo:" . "\n\tError code: " . $e->errorcode . "\n\tDebug info: " . $e->debuginfo . "\n\tServer: " . $_SERVER['SERVER_NAME'] . " (" . $_SERVER['SERVER_ADDR'] . ")";
                 if (file_exists($CFG->dataroot . '/emailcount')) {
                     $fp = @fopen($CFG->dataroot . '/emailcount', 'r');
                     $content = @fread($fp, 24);
                     @fclose($fp);
                     if (time() - (int) $content > 600) {
                         //email directly rather than using messaging
                         @mail($CFG->emailconnectionerrorsto, 'WARNING: Database connection error: ' . $CFG->wwwroot, $body);
                         $fp = @fopen($CFG->dataroot . '/emailcount', 'w');
                         @fwrite($fp, time());
                     }
                 } else {
                     //email directly rather than using messaging
                     @mail($CFG->emailconnectionerrorsto, 'WARNING: Database connection error: ' . $CFG->wwwroot, $body);
                     $fp = @fopen($CFG->dataroot . '/emailcount', 'w');
                     @fwrite($fp, time());
                 }
             }
             // rethrow the exception
             throw $e;
         }
         $CFG->dbfamily = $remotedb->get_dbfamily();
         // TODO: BC only for now
         return true;
     }
     return false;
 }