mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
MDL-14679 var_export now used instead of addsingleslashes in installer; refactored the database setting initialisation
This commit is contained in:
parent
98e1919368
commit
16a5642c69
@ -465,7 +465,7 @@ if (!file_exists(dirname(dirname(__FILE__)) . '/config.php')) {
|
||||
error_reporting(0); // Hide errors
|
||||
|
||||
if (! $dbconnected = $DB->connect($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'], false, $INSTALL['prefix'])) {
|
||||
if (!$DB->create_database($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'])) {
|
||||
if (!$DB->create_database($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'])) {
|
||||
$errormsg = get_string('dbcreationerror', 'install');
|
||||
$nextstage = DATABASE;
|
||||
} else {
|
||||
@ -599,28 +599,18 @@ if (!file_exists(dirname(dirname(__FILE__)) . '/config.php')) {
|
||||
$str .= "\r\n";
|
||||
|
||||
$database = $databases[$CONFFILE['dbtype']];
|
||||
$database->connect($CONFFILE['dbhost'], $CONFFILE['dbuser'], $CONFFILE['dbpass'], $CONFFILE['dbname'], false, $CONFFILE['prefix']);
|
||||
$dbconfig = $database->export_dbconfig();
|
||||
$dbconfig->persistent = false;
|
||||
$dbconfig = $database->export_dbconfig($CONFFILE['dbhost'], $CONFFILE['dbuser'], $CONFFILE['dbpass'], $CONFFILE['dbname'], false, $CONFFILE['prefix']);
|
||||
|
||||
foreach ($dbconfig as $key=>$value) {
|
||||
$key = str_pad($key, 9);
|
||||
if (is_bool($value)) {
|
||||
if ($value) {
|
||||
$str .= '$CFG->'.$key.' = true;'."\r\n";
|
||||
} else {
|
||||
$str .= '$CFG->'.$key.' = false;'."\r\n";
|
||||
}
|
||||
} else {
|
||||
$str .= '$CFG->'.$key.' = \''.addsingleslashes($value)."';\r\n";
|
||||
}
|
||||
$str .= '$CFG->'.$key.' = '.var_export($value, true).";\r\n";
|
||||
}
|
||||
$str .= "\r\n";
|
||||
|
||||
$str .= '$CFG->wwwroot = \''.addsingleslashes($CONFFILE['wwwrootform'])."';\r\n";
|
||||
$str .= '$CFG->dirroot = \''.addsingleslashes($CONFFILE['dirrootform'])."';\r\n";
|
||||
$str .= '$CFG->dataroot = \''.addsingleslashes($CONFFILE['dataroot'])."';\r\n";
|
||||
$str .= '$CFG->admin = \''.addsingleslashes($CONFFILE['admindirname'])."';\r\n";
|
||||
$str .= '$CFG->wwwroot = '.var_export($CONFFILE['wwwrootform'], true).";\r\n";
|
||||
$str .= '$CFG->dirroot = '.var_export($CONFFILE['dirrootform'], true).";\r\n";
|
||||
$str .= '$CFG->dataroot = '.var_export($CONFFILE['dataroot'], true).";\r\n";
|
||||
$str .= '$CFG->admin = '.var_export($CONFFILE['admindirname'], true).";\r\n";
|
||||
$str .= "\r\n";
|
||||
|
||||
$str .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\r\n";
|
||||
|
29
install.php
29
install.php
@ -461,35 +461,18 @@ if ($nextstage == SAVE) {
|
||||
$str .= "\r\n";
|
||||
|
||||
$DB = $databases[$INSTALL['dbtype']];
|
||||
$DB->connect($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'], false, $INSTALL['prefix']);
|
||||
$dbconfig = $DB->export_dbconfig();
|
||||
$dbconfig->persistent = false;
|
||||
$dbconfig = $DB->export_dbconfig($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'], false, $INSTALL['prefix']);
|
||||
|
||||
foreach ($dbconfig as $key=>$value) {
|
||||
$key = str_pad($key, 9);
|
||||
if (is_bool($value)) {
|
||||
if ($value) {
|
||||
$str .= '$CFG->'.$key.' = true;'."\r\n";
|
||||
} else {
|
||||
$str .= '$CFG->'.$key.' = false;'."\r\n";
|
||||
}
|
||||
} else if (is_array($value)) {
|
||||
if (empty($value)) {
|
||||
$value = 'array()';
|
||||
} else {
|
||||
$value = 'unserialize(\'' . addsingleslashes(serialize($value)) . '\')';
|
||||
}
|
||||
$str .= '$CFG->'.$key.' = '. $value . ";\r\n";
|
||||
} else {
|
||||
$str .= '$CFG->'.$key.' = \''.addsingleslashes($value)."';\r\n";
|
||||
}
|
||||
$str .= '$CFG->'.$key.' = '.var_export($value, true).";\r\n";
|
||||
}
|
||||
$str .= "\r\n";
|
||||
|
||||
$str .= '$CFG->wwwroot = \''.addsingleslashes($INSTALL['wwwrootform'])."';\r\n";
|
||||
$str .= '$CFG->dirroot = \''.addsingleslashes($INSTALL['dirrootform'])."';\r\n";
|
||||
$str .= '$CFG->dataroot = \''.addsingleslashes($INSTALL['dataroot'])."';\r\n";
|
||||
$str .= '$CFG->admin = \''.addsingleslashes($INSTALL['admindirname'])."';\r\n";
|
||||
$str .= '$CFG->wwwroot = '.var_export($INSTALL['wwwrootform'], true).";\r\n";
|
||||
$str .= '$CFG->dirroot = '.var_export($INSTALL['dirrootform'], true).";\r\n";
|
||||
$str .= '$CFG->dataroot = '.var_export($INSTALL['dataroot'], true).";\r\n";
|
||||
$str .= '$CFG->admin = '.var_export($INSTALL['admindirname'], true).";\r\n";
|
||||
$str .= "\r\n";
|
||||
|
||||
$str .= '$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode'."\r\n";
|
||||
|
@ -11,6 +11,15 @@ abstract class adodb_moodle_database extends moodle_database {
|
||||
|
||||
protected $adodb;
|
||||
|
||||
/**
|
||||
* Returns general database library name
|
||||
* Note: can be used before connect()
|
||||
* @return string db type adodb, pdo, native
|
||||
*/
|
||||
protected function get_dblibrary() {
|
||||
return 'adodb';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns localised database type name
|
||||
* Note: can be used before connect()
|
||||
@ -21,40 +30,16 @@ abstract class adodb_moodle_database extends moodle_database {
|
||||
return get_string($dbtype, 'install');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns db related part of config.php
|
||||
* Note: can be used before connect()
|
||||
* @return string
|
||||
*/
|
||||
public function export_dbconfig() {
|
||||
$cfg = new stdClass();
|
||||
$cfg->dbtype = $this->get_dbtype();
|
||||
$cfg->dblibrary = 'adodb';
|
||||
$cfg->dbhost = $this->dbhost;
|
||||
$cfg->dbname = $this->dbname;
|
||||
$cfg->dbuser = $this->dbuser;
|
||||
$cfg->dbpass = $this->dbpass;
|
||||
$cfg->prefix = $this->prefix;
|
||||
|
||||
return $cfg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adodb preconnection routines, ususally sets up needed defines;
|
||||
*/
|
||||
protected abstract function preconfigure_dbconnection();
|
||||
|
||||
public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
|
||||
$this->dbhost = $dbhost;
|
||||
$this->dbuser = $dbuser;
|
||||
$this->dbpass = $dbpass;
|
||||
$this->dbname = $dbname;
|
||||
$this->dbpersist = $dbpersist;
|
||||
$this->prefix = $prefix;
|
||||
$this->dboptions = (array)$dboptions;
|
||||
|
||||
global $CFG;
|
||||
|
||||
$this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
|
||||
|
||||
$this->preconfigure_dbconnection();
|
||||
|
||||
require_once($CFG->libdir.'/adodb/adodb.inc.php');
|
||||
|
@ -99,6 +99,13 @@ abstract class moodle_database {
|
||||
*/
|
||||
protected abstract function get_dbtype();
|
||||
|
||||
/**
|
||||
* Returns general database library name
|
||||
* Note: can be used before connect()
|
||||
* @return string db type adodb, pdo, native
|
||||
*/
|
||||
protected abstract function get_dblibrary();
|
||||
|
||||
/**
|
||||
* Returns localised database type name
|
||||
* Note: can be used before connect()
|
||||
@ -118,7 +125,23 @@ abstract class moodle_database {
|
||||
* Note: can be used before connect()
|
||||
* @return string
|
||||
*/
|
||||
public abstract function export_dbconfig();
|
||||
public function export_dbconfig($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
|
||||
$this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
|
||||
|
||||
$cfg = new stdClass();
|
||||
$cfg->dbtype = $this->get_dbtype();
|
||||
$cfg->dblibrary = $this->get_dblibrary();
|
||||
$cfg->dbhost = $this->dbhost;
|
||||
$cfg->dbname = $this->dbname;
|
||||
$cfg->dbuser = $this->dbuser;
|
||||
$cfg->dbpass = $this->dbpass;
|
||||
$cfg->prefix = $this->prefix;
|
||||
if ($this->dboptions) {
|
||||
$cfg->dboptions = $this->dboptions;
|
||||
}
|
||||
|
||||
return $cfg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to db
|
||||
@ -134,6 +157,27 @@ abstract class moodle_database {
|
||||
*/
|
||||
public abstract function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null);
|
||||
|
||||
/**
|
||||
* Store various database settings
|
||||
* @param string $dbhost
|
||||
* @param string $dbuser
|
||||
* @param string $dbpass
|
||||
* @param string $dbname
|
||||
* @param bool $dbpersist
|
||||
* @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
|
||||
* @param array $dboptions driver specific options
|
||||
* @return void
|
||||
*/
|
||||
protected function store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
|
||||
$this->dbhost = $dbhost;
|
||||
$this->dbuser = $dbuser;
|
||||
$this->dbpass = $dbpass;
|
||||
$this->dbname = $dbname;
|
||||
$this->dbpersist = $dbpersist;
|
||||
$this->prefix = $prefix;
|
||||
$this->dboptions = (array)$dboptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to create the database
|
||||
* @param string $dbhost
|
||||
|
@ -35,13 +35,7 @@ abstract class pdo_moodle_database extends moodle_database {
|
||||
* @return bool success
|
||||
*/
|
||||
public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
|
||||
$this->dbhost = $dbhost;
|
||||
$this->dbuser = $dbuser;
|
||||
$this->dbpass = $dbpass;
|
||||
$this->dbname = $dbname;
|
||||
$this->dbpersist = $dbpersist;
|
||||
$this->prefix = $prefix;
|
||||
$this->dboptions = (array)$dboptions;
|
||||
$this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
|
||||
|
||||
try {
|
||||
$this->pdb = new PDO($this->get_dsn(), $this->dbuser, $this->dbpass, $this->get_pdooptions());
|
||||
@ -77,6 +71,15 @@ abstract class pdo_moodle_database extends moodle_database {
|
||||
///TODO: not needed preconfigure_dbconnection() stuff for PDO drivers?
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns general database library name
|
||||
* Note: can be used before connect()
|
||||
* @return string db type adodb, pdo, native
|
||||
*/
|
||||
protected function get_dblibrary() {
|
||||
return 'pdo';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns localised database type name
|
||||
* Note: can be used before connect()
|
||||
@ -95,24 +98,6 @@ abstract class pdo_moodle_database extends moodle_database {
|
||||
return get_string('databasesettingssub_' . $this->get_dbtype() . '_pdo', 'install');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns db related part of config.php
|
||||
* Note: can be used before connect()
|
||||
* @return string
|
||||
*/
|
||||
public function export_dbconfig() {
|
||||
$cfg = new stdClass();
|
||||
$cfg->dbtype = $this->get_dbtype();
|
||||
$cfg->dblibrary = 'pdo';
|
||||
$cfg->dbhost = $this->dbhost;
|
||||
$cfg->dbname = $this->dbname;
|
||||
$cfg->dbuser = $this->dbuser;
|
||||
$cfg->dbpass = $this->dbpass;
|
||||
$cfg->prefix = $this->prefix;
|
||||
$cfg->dboptions = $this->dboptions;
|
||||
return $cfg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns database server info array
|
||||
* @return array
|
||||
|
@ -72,10 +72,12 @@ class postgres7_adodb_moodle_database extends adodb_moodle_database {
|
||||
* Note: can be used before connect()
|
||||
* @return string
|
||||
*/
|
||||
public function export_dbconfig() {
|
||||
public function export_dbconfig($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
|
||||
$this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
|
||||
|
||||
$cfg = new stdClass();
|
||||
$cfg->dbtype = $this->get_dbtype();
|
||||
$cfg->dblibrary = 'adodb';
|
||||
$cfg->dblibrary = $this->get_dblibrary();
|
||||
if ($this->dbhost == 'localhost' or $this->dbhost == '127.0.0.1') {
|
||||
$cfg->dbhost = "user='{$this->dbuser}' password='{$this->dbpass}' dbname='{$this->dbname}'";
|
||||
$cfg->dbname = '';
|
||||
@ -88,6 +90,9 @@ class postgres7_adodb_moodle_database extends adodb_moodle_database {
|
||||
$cfg->dbpass = $this->dbpass;
|
||||
}
|
||||
$cfg->prefix = $this->prefix;
|
||||
if ($this->dboptions) {
|
||||
$cfg->dboptions = $this->dboptions;
|
||||
}
|
||||
|
||||
return $cfg;
|
||||
}
|
||||
|
@ -1121,9 +1121,9 @@ class moodle_database_for_testing extends moodle_database {
|
||||
public function driver_installed(){}
|
||||
public function get_dbfamily(){}
|
||||
protected function get_dbtype(){}
|
||||
protected function get_dblibrary(){}
|
||||
public function get_name(){}
|
||||
public function get_configuration_hints(){}
|
||||
public function export_dbconfig(){}
|
||||
public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null){}
|
||||
public function get_server_info(){}
|
||||
protected function allowed_param_types(){}
|
||||
|
@ -79,12 +79,3 @@ function check_memory_limit() {
|
||||
function inst_check_php_version() {
|
||||
return check_php_version("5.2.0");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add slashes for single quotes and backslashes
|
||||
* so they can be included in single quoted string
|
||||
* (for config.php)
|
||||
*/
|
||||
function addsingleslashes($input){
|
||||
return preg_replace("/(['\\\])/", "\\\\$1", $input);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user