public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {
global $CFG;
+ $driverstatus = $this->driver_installed();
+
+ if ($driverstatus !== true) {
+ throw new dml_exception('dbdriverproblem', $driverstatus);
+ }
+
+ ob_start();
+
$this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
$this->preconfigure_dbconnection();
// we probably want to change this value to ''.
$this->adodb->null2null = 'A long random string that will never, ever match something we want to insert into the database, I hope. \'';
+
if (!empty($this->dboptions['dbpersist'])) { // Use persistent connection
- if (!$this->adodb->PConnect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname)) {
- return false;
- }
+ $connected = $this->adodb->PConnect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
} else { // Use single connection
- if (!$this->adodb->Connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname)) {
- return false;
- }
+ $connected = $this->adodb->Connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
+ }
+
+ $dberr = ob_get_contents();
+ ob_end_clean();
+
+ if (!$connected) {
+ throw new dml_connection_exception($dberr);
}
+
$this->configure_dbconnection();
+
return true;
}
* @param string $dbname
* @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
* @param array $dboptions driver specific options
- * @return bool success
+ * @return bool true
+ * @throws dml_connection_exception if error
*/
public abstract function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null);
public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {
global $CFG;
+ $driverstatus = $this->driver_installed();
+
+ if ($driverstatus !== true) {
+ throw new dml_exception('dbdriverproblem', $driverstatus);
+ }
+
$this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
+ ob_start();
$this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
- if ($this->mysqli->connect_error) {
- return false;
+ $dberr = ob_get_contents();
+ ob_end_clean();
+ $errorno = @$this->mysqli->connect_errno;
+
+ if ($errorno !== 0) {
+ throw new dml_connection_exception($dberr);
}
+
$this->query_start("--set_charset()", null, SQL_QUERY_AUX);
$this->mysqli->set_charset('utf8');
$this->query_end(true);
* @param string $dbname
* @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
* @param array $dboptions driver specific options
- * @return bool success
+ * @return bool true
+ * @throws dml_connection_exception if error
*/
public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {
global $CFG;
+ $driverstatus = $this->driver_installed();
+
+ if ($driverstatus !== true) {
+ throw new dml_exception('dbdriverproblem', $driverstatus);
+ }
+
$this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
$pass = addcslashes($this->dbpass, "'\\");
$connection = "host='$this->dbhost' port='$port' user='$this->dbuser' password='$pass' dbname='$this->dbname'";
}
+ ob_start();
if (empty($this->dboptions['dbpersit'])) {
$this->pgsql = pg_connect($connection, PGSQL_CONNECT_FORCE_NEW);
} else {
$this->pgsql = pg_pconnect($connection, PGSQL_CONNECT_FORCE_NEW);
}
-
+ $dberr = ob_get_contents();
+ ob_end_clean();
+
$status = pg_connection_status($this->pgsql);
if ($status === false or $status === PGSQL_CONNECTION_BAD) {
$this->pgsql = null;
- return false;
+ throw new dml_connection_exception($dberr);
}
$this->query_start("--pg_set_client_encoding()", null, SQL_QUERY_AUX);
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = pg_query($this->pgsql, $sql);
$this->query_end($result);
- if ($result === false) {
- return false;
- }
+
$this->bytea_oid = pg_fetch_result($result, 0);
pg_free_result($result);
if ($this->bytea_oid === false) {
- return false;
+ $this->pgsql = null;
+ throw new dml_connection_exception('Can not read bytea type.');
}
return true;
}
}
+/**
+ * DML db connection exception - triggered if database not accessible.
+ */
+class dml_connection_exception extends dml_exception {
+ function __construct($error) {
+ $errorinfo = '<em>'.s($error).'</em>';
+ parent::__construct('dbconnectionfailed', NULL, $errorinfo);
+ }
+}
+
/**
* DML read exception - triggered by SQL syntax errors, missing tables, etc.
*/
$CFG->dboptions['dbpersist'] = $CFG->dbpersist;
}
-
if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
}
- $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
-
- $driverstatus = $DB->driver_installed();
-
- if ($driverstatus !== true) {
- throw new dml_exception('dbdriverproblem', $driverstatus);
- }
-
- if (debugging('', DEBUG_ALL)) {
- // catch errors
- ob_start();
- } else {
- $prevdebug = error_reporting(0);
- }
-
- $connected = false;
try {
- $connected = $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
- } Catch (Exception $e) {
- $connected = false;
- }
-
- if (!$connected) {
- $dberr = '';
- if (debugging('', DEBUG_ALL)) {
- if ($dberr = ob_get_contents()) {
- $dberr = '<p><em>'.$dberr.'</em></p>';
- }
- ob_end_clean();
- }
+ $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
+ } catch (moodle_exception $e) {
if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
if (file_exists($CFG->dataroot.'/emailcount')){
- $fp = fopen($CFG->dataroot.'/emailcount', 'r');
- $content = fread($fp, 24);
- fclose($fp);
+ $fp = @fopen($CFG->dataroot.'/emailcount', 'r');
+ $content = @fread($fp, 24);
+ @fclose($fp);
if((time() - (int)$content) > 600){
@mail($CFG->emailconnectionerrorsto,
'WARNING: Database connection error: '.$CFG->wwwroot,
'Connection error: '.$CFG->wwwroot);
- $fp = fopen($CFG->dataroot.'/emailcount', 'w');
- fwrite($fp, time());
+ $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
+ @fwrite($fp, time());
}
} else {
@mail($CFG->emailconnectionerrorsto,
'WARNING: Database connection error: '.$CFG->wwwroot,
'Connection error: '.$CFG->wwwroot);
- $fp = fopen($CFG->dataroot.'/emailcount', 'w');
- fwrite($fp, time());
+ $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
+ @fwrite($fp, time());
}
}
- throw new dml_exception('dbconnectionfailed', $dberr);
- }
- if (debugging('', DEBUG_ALL)) {
- ob_end_clean();
- } else {
- error_reporting($prevdebug);
+ // rethrow the exception
+ throw $e;
}
+ $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
+
return true;
}
if ($ex instanceof moodle_exception) {
if (!isset($CFG->theme) or !isset($CFG->stylesheets)) {
- _print_early_error($ex->errorcode, $ex->module, $ex->a);
+ _print_early_error($ex->errorcode, $ex->module, $ex->a, $backtrace, $ex->debuginfo);
} else {
_print_normal_error($ex->errorcode, $ex->module, $ex->a, $ex->link, $backtrace, $ex->debuginfo);
}
* Internal function - do not use directly!!
* This function is used if fatal error occures before the themes are fully initialised (eg. in lib/setup.php)
*/
-function _print_early_error($errorcode, $module, $a) {
+function _print_early_error($errorcode, $module, $a, $backtrace=null, $debuginfo=null) {
$message = get_string($errorcode, $module, $a);
if ($module === 'error' and strpos($message, '[[') === 0) {
//search in moodle file if error specified - needed for backwards compatibility
border-color:black; background-color:#ffffee; border-style:solid; border-radius: 20px; border-collapse: collapse;
width: 80%; -moz-border-radius: 20px; padding: 15px">
'.$message.'
-</div>
-</body></html>';
+</div>';
+ if (debugging('', DEBUG_DEVELOPER)) {
+ if ($debuginfo) {
+ debugging($debuginfo, DEBUG_DEVELOPER, $backtrace);
+ } else {
+ notify('Stack trace:'.print_backtrace($backtrace, true), 'notifytiny');
+ }
+ }
+
+ echo '</body></html>';
die;
}