2007-12-09 09:03:15 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGgroupWare setup - test or create the database
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @package setup
|
2016-05-01 17:56:49 +02:00
|
|
|
* @copyright (c) 2007-16 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
2007-12-09 09:03:15 +01:00
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
2008-10-02 17:27:39 +02:00
|
|
|
* @version $Id$
|
2007-12-09 09:03:15 +01:00
|
|
|
*/
|
|
|
|
|
2016-05-01 17:56:49 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
/**
|
|
|
|
* setup command: test or create the database
|
|
|
|
*/
|
2008-10-02 17:27:39 +02:00
|
|
|
class setup_cmd_database extends setup_cmd
|
2007-12-09 09:03:15 +01:00
|
|
|
{
|
2008-01-14 06:44:32 +01:00
|
|
|
/**
|
|
|
|
* Allow to run this command via setup-cli
|
|
|
|
*/
|
|
|
|
const SETUP_CLI_CALLABLE = true;
|
2013-05-26 11:43:10 +02:00
|
|
|
|
2010-07-31 11:23:41 +02:00
|
|
|
/**
|
|
|
|
* Maximum length of database name (at least for MySQL this is the limit)
|
2013-05-26 11:43:10 +02:00
|
|
|
*
|
2010-07-31 11:23:41 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const MAX_DB_NAME_LEN = 16;
|
2008-01-14 06:44:32 +01:00
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
/**
|
2016-05-01 17:56:49 +02:00
|
|
|
* Instance of Api\Db to connect or create the db
|
2007-12-09 09:03:15 +01:00
|
|
|
*
|
2016-05-01 17:56:49 +02:00
|
|
|
* @var Api\Db
|
2007-12-09 09:03:15 +01:00
|
|
|
*/
|
|
|
|
private $test_db;
|
2008-10-02 17:27:39 +02:00
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
/**
|
2007-12-13 03:41:55 +01:00
|
|
|
* Constructor
|
2007-12-09 09:03:15 +01:00
|
|
|
*
|
|
|
|
* @param string/array $domain domain-name to customize the defaults or array with all parameters
|
|
|
|
* @param string $db_type db-type (mysql, pgsql, ...)
|
2016-05-01 17:56:49 +02:00
|
|
|
* @param string $db_host =null
|
|
|
|
* @param string $db_port =null
|
|
|
|
* @param string $db_name =null
|
|
|
|
* @param string $db_user =null
|
|
|
|
* @param string $db_pass =null
|
|
|
|
* @param string $db_root =null
|
|
|
|
* @param string $db_root_pw =null
|
|
|
|
* @param string $sub_command ='create_db' 'create_db', 'test_db', 'test_db_root'
|
|
|
|
* @param string $db_grant_host ='localhost' host/ip of webserver for grant
|
|
|
|
* @param boolean $make_db_name_unique =false true: if create fails because db exists,
|
2010-07-31 11:23:41 +02:00
|
|
|
* try creating a unique name by shortening the name and adding a number to it
|
2007-12-09 09:03:15 +01:00
|
|
|
*/
|
|
|
|
function __construct($domain,$db_type=null,$db_host=null,$db_port=null,$db_name=null,$db_user=null,$db_pass=null,
|
2010-07-31 11:23:41 +02:00
|
|
|
$db_root=null,$db_root_pw=null,$sub_command='create_db',$db_grant_host='localhost',$make_db_name_unique=false)
|
2007-12-09 09:03:15 +01:00
|
|
|
{
|
|
|
|
if (!is_array($domain))
|
|
|
|
{
|
2016-08-06 10:35:48 +02:00
|
|
|
$data = array(
|
2007-12-09 09:03:15 +01:00
|
|
|
'domain' => $domain,
|
|
|
|
'db_type' => $db_type,
|
|
|
|
'db_host' => $db_host,
|
|
|
|
'db_port' => $db_port,
|
|
|
|
'db_name' => $db_name,
|
|
|
|
'db_user' => $db_user,
|
|
|
|
'db_pass' => $db_pass,
|
|
|
|
'db_root' => $db_root,
|
|
|
|
'db_root_pw' => $db_root_pw,
|
2008-11-18 20:58:11 +01:00
|
|
|
'sub_command' => $sub_command,
|
|
|
|
'db_grant_host' => $db_grant_host,
|
2013-05-26 11:43:10 +02:00
|
|
|
'make_db_name_unique' => $make_db_name_unique,
|
2007-12-09 09:03:15 +01:00
|
|
|
);
|
|
|
|
}
|
2016-08-06 10:35:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$data = $domain;
|
|
|
|
}
|
|
|
|
// need to incorporate correct defaults for given database type
|
|
|
|
admin_cmd::__construct(array_merge(self::defaults($data['db_type']), array_diff($data, array(null))));
|
2007-12-09 09:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-12-13 03:41:55 +01:00
|
|
|
* run the command: test or create database
|
2008-10-02 17:27:39 +02:00
|
|
|
*
|
2016-05-01 17:56:49 +02:00
|
|
|
* @param boolean $check_only =false only run the checks (and throw the exceptions), but not the command itself
|
2007-12-13 03:41:55 +01:00
|
|
|
* @return string success message
|
2007-12-09 09:03:15 +01:00
|
|
|
* @throws Exception(lang('Wrong credentials to access the header.inc.php file!'),2);
|
|
|
|
* @throws Exception('header.inc.php not found!');
|
|
|
|
*/
|
|
|
|
protected function exec($check_only=false)
|
|
|
|
{
|
|
|
|
if (!empty($this->domain) && !preg_match('/^([a-z0-9_-]+\.)*[a-z0-9]+/i',$this->domain))
|
|
|
|
{
|
2016-05-01 17:56:49 +02:00
|
|
|
throw new Api\Exception\WrongUserinput(lang("'%1' is no valid domain name!",$this->domain));
|
2007-12-09 09:03:15 +01:00
|
|
|
}
|
|
|
|
if ($this->remote_id && $check_only) return true; // further checks can only done locally
|
2008-10-02 17:27:39 +02:00
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
$this->_merge_defaults();
|
|
|
|
//_debug_array($this->as_array());
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch($this->sub_command)
|
|
|
|
{
|
|
|
|
case 'test_db_root':
|
|
|
|
$msg = $this->connect($this->db_root,$this->db_root_pw,$this->db_meta);
|
|
|
|
break;
|
|
|
|
case 'test_db':
|
|
|
|
$msg = $this->connect();
|
|
|
|
break;
|
2009-05-22 00:16:09 +02:00
|
|
|
case 'drop':
|
|
|
|
$msg = $this->drop();
|
|
|
|
break;
|
2007-12-09 09:03:15 +01:00
|
|
|
case 'create_db':
|
|
|
|
default:
|
|
|
|
$msg = $this->create();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
// we catch the exception to properly restore the db
|
|
|
|
}
|
|
|
|
$this->restore_db();
|
2008-10-02 17:27:39 +02:00
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
if ($e)
|
|
|
|
{
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
return $msg;
|
|
|
|
}
|
2008-10-02 17:27:39 +02:00
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
/**
|
|
|
|
* Connect to database
|
|
|
|
*
|
2016-05-01 17:56:49 +02:00
|
|
|
* @param string $user =null default $this->db_user
|
|
|
|
* @param string $pass =null default $this->db_pass
|
|
|
|
* @param string $name =null default $this->db_name
|
|
|
|
* @throws Api\Exception\WrongUserinput Can not connect to database ...
|
2007-12-09 09:03:15 +01:00
|
|
|
*/
|
|
|
|
private function connect($user=null,$pass=null,$name=null)
|
|
|
|
{
|
2017-03-09 12:07:14 +01:00
|
|
|
// propagate all db_* vars
|
|
|
|
$this->test_db = new Api\Db(get_object_vars($this));
|
2008-10-02 17:27:39 +02:00
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
$error_rep = error_reporting();
|
2017-03-09 12:07:14 +01:00
|
|
|
error_reporting($error_rep & ~E_WARNING); // switch warnings off, in case they are on
|
2007-12-09 09:03:15 +01:00
|
|
|
try {
|
2017-03-09 12:07:14 +01:00
|
|
|
$this->test_db->connect($name, null, null, $user, $pass);
|
2007-12-09 09:03:15 +01:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
// just give a nicer error, after switching error_reporting on again
|
|
|
|
}
|
|
|
|
error_reporting($error_rep);
|
2008-10-02 17:27:39 +02:00
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
if ($e)
|
|
|
|
{
|
2016-05-01 17:56:49 +02:00
|
|
|
throw new Api\Exception\WrongUserinput(lang('Can not connect to %1 database %2 on host %3 using user %4!',
|
2007-12-09 09:03:15 +01:00
|
|
|
$this->db_type,$name,$this->db_host.($this->db_port?':'.$this->db_port:''),$user).' ('.$e->getMessage().')');
|
|
|
|
}
|
|
|
|
return lang('Successful connected to %1 database %2 on %3 using user %4.',
|
|
|
|
$this->db_type,$name,$this->db_host.($this->db_port?':'.$this->db_port:''),$user);
|
|
|
|
}
|
2008-10-02 17:27:39 +02:00
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
/**
|
|
|
|
* Check and if does not yet exist create the new database and user
|
|
|
|
*
|
|
|
|
* The check will fail if the database exists, but already contains tables
|
2013-05-26 11:43:10 +02:00
|
|
|
*
|
2010-07-31 11:23:41 +02:00
|
|
|
* if $this->make_db_name_unique is set, a decrementing nummeric prefix gets
|
|
|
|
* added to $this->db_name AND $this->db_user, if db already exists.
|
2008-10-02 17:27:39 +02:00
|
|
|
*
|
2007-12-09 09:03:15 +01:00
|
|
|
* @return string with success message
|
2016-05-01 17:56:49 +02:00
|
|
|
* @throws Api\Exception\WrongUserinput
|
2007-12-09 09:03:15 +01:00
|
|
|
*/
|
|
|
|
private function create()
|
|
|
|
{
|
2010-07-31 11:23:41 +02:00
|
|
|
static $try_make_unique = 0; // to limit trials to create a unique name
|
|
|
|
|
2010-08-25 14:24:11 +02:00
|
|
|
// shorten db-name/-user to self::MAX_DB_NAME_LEN chars
|
|
|
|
if ($this->make_db_name_unique && strlen($this->db_name) > self::MAX_DB_NAME_LEN)
|
|
|
|
{
|
2013-05-26 11:43:10 +02:00
|
|
|
$this->set_defaults['db_name'] = $this->db_name =
|
2010-08-25 14:24:11 +02:00
|
|
|
$this->set_defaults['db_user'] = $this->db_user = // change user too (otherwise existing user/db could not connect any more!)
|
2014-04-16 22:14:19 +02:00
|
|
|
substr(str_replace(array('.', '-'), '_', $this->db_name),0,self::MAX_DB_NAME_LEN);
|
2010-08-25 14:24:11 +02:00
|
|
|
}
|
2007-12-09 09:03:15 +01:00
|
|
|
try {
|
2016-06-05 16:59:55 +02:00
|
|
|
$msg = $this->connect();
|
2007-12-09 09:03:15 +01:00
|
|
|
}
|
2016-05-01 17:56:49 +02:00
|
|
|
catch (Api\Exception\WrongUserinput $e) {
|
2007-12-09 09:03:15 +01:00
|
|
|
// db or user not working --> connect as root and create it
|
2007-12-11 02:43:18 +01:00
|
|
|
try {
|
2008-11-18 20:58:11 +01:00
|
|
|
$this->test_db->create_database($this->db_root,$this->db_root_pw,$this->db_charset,$this->db_grant_host);
|
2007-12-11 02:43:18 +01:00
|
|
|
$this->connect();
|
|
|
|
}
|
2016-05-01 17:56:49 +02:00
|
|
|
catch(Api\Db\Exception $e) { // catches failed to create database
|
2010-07-31 11:23:41 +02:00
|
|
|
// try connect as root to check if wrong root/root_pw is the problem
|
2007-12-09 09:03:15 +01:00
|
|
|
$this->connect($this->db_root,$this->db_root_pw,$this->db_meta);
|
2013-05-26 11:43:10 +02:00
|
|
|
|
2010-07-31 11:23:41 +02:00
|
|
|
// if we should create a db with a unique name (try it only N times, not endless!)
|
|
|
|
if ($this->make_db_name_unique && $try_make_unique++ < 20)
|
|
|
|
{
|
|
|
|
// check if we can connect as root to the db to create --> db exists already
|
|
|
|
try {
|
|
|
|
$this->connect($this->db_root,$this->db_root_pw);
|
|
|
|
// create new db_name by incrementing an existing numeric postfix
|
2016-05-01 17:56:49 +02:00
|
|
|
$matches = null;
|
2010-07-31 11:23:41 +02:00
|
|
|
if (preg_match('/([0-9]+)$/',$this->db_name,$matches))
|
|
|
|
{
|
|
|
|
$num = (string)(++$matches[1]);
|
|
|
|
}
|
|
|
|
else // or adding one starting with 2
|
|
|
|
{
|
|
|
|
$num = '2';
|
|
|
|
}
|
2013-05-26 11:43:10 +02:00
|
|
|
$this->set_defaults['db_name'] = $this->db_name =
|
2010-07-31 11:23:41 +02:00
|
|
|
$this->set_defaults['db_user'] = $this->db_user = // change user too (otherwise existing user/db could not connect any more!)
|
|
|
|
substr($this->db_name,0,self::MAX_DB_NAME_LEN-strlen($num)).$num;
|
2013-05-26 11:43:10 +02:00
|
|
|
|
2010-08-25 14:24:11 +02:00
|
|
|
return $this->create();
|
2010-07-31 11:23:41 +02:00
|
|
|
}
|
2016-05-01 17:56:49 +02:00
|
|
|
catch (Api\Exception\WrongUserinput $e2)
|
2010-07-31 11:23:41 +02:00
|
|
|
{
|
|
|
|
// we can NOT connect to db as root --> ignore exception to give general error
|
|
|
|
}
|
|
|
|
}
|
2007-12-09 09:03:15 +01:00
|
|
|
// if not give general error
|
2016-05-01 17:56:49 +02:00
|
|
|
throw new Api\Exception\WrongUserinput(lang('Can not create %1 database %2 on %3 for user %4!',
|
2007-12-09 09:03:15 +01:00
|
|
|
$this->db_type,$this->db_name,$this->db_host.($this->db_port?':'.$this->db_port:''),$this->db_user));
|
|
|
|
}
|
|
|
|
$msg = lang('Successful connected to %1 on %3 and created database %2 for user %4.',
|
|
|
|
$this->db_type,$this->db_name,$this->db_host.($this->db_port?':'.$this->db_port:''),$this->db_user);
|
|
|
|
}
|
|
|
|
// check if it already contains tables
|
|
|
|
if (($tables = $this->test_db->table_names()))
|
|
|
|
{
|
|
|
|
foreach($tables as &$table)
|
|
|
|
{
|
|
|
|
$table = $table['table_name'];
|
|
|
|
}
|
2016-05-01 17:56:49 +02:00
|
|
|
throw new Api\Exception\WrongUserinput(lang('%1 database %2 on %3 already contains the following tables:',
|
2007-12-09 09:03:15 +01:00
|
|
|
$this->db_type,$this->db_name,$this->db_host.($this->db_port?':'.$this->db_port:'')).' '.
|
|
|
|
implode(', ',$tables));
|
|
|
|
}
|
|
|
|
return $msg;
|
|
|
|
}
|
2008-10-02 17:27:39 +02:00
|
|
|
|
2009-05-22 00:16:09 +02:00
|
|
|
/**
|
|
|
|
* Drop database and user
|
|
|
|
*
|
|
|
|
* @return string with success message
|
2016-05-01 17:56:49 +02:00
|
|
|
* @throws Api\Exception\WrongUserinput
|
|
|
|
* @throws Api\Db\Exception if database not exist
|
2009-05-22 00:16:09 +02:00
|
|
|
*/
|
|
|
|
private function drop()
|
|
|
|
{
|
|
|
|
$this->connect($this->db_root,$this->db_root_pw,$this->db_meta);
|
|
|
|
$this->test_db->query('DROP DATABASE '.$this->test_db->name_quote($this->db_name),__LINE__,__FILE__);
|
|
|
|
$msg = lang('Datebase %1 droped.',$this->db_name);
|
|
|
|
try {
|
|
|
|
$this->test_db->query('DROP USER '.$this->test_db->quote($this->db_user).'@'.
|
|
|
|
$this->test_db->quote($this->db_grant_host?$this->db_grant_host:'%'),__LINE__,__FILE__);
|
|
|
|
}
|
2016-05-01 17:56:49 +02:00
|
|
|
catch (Api\Db\Exception $e) {
|
|
|
|
unset($e);
|
2009-05-22 00:16:09 +02:00
|
|
|
// we make this no fatal error, as the granthost might be something else ...
|
|
|
|
$msg .= ' '.lang('Error dropping User!');
|
|
|
|
}
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2007-12-09 09:03:15 +01:00
|
|
|
/**
|
|
|
|
* Return default database settings for a given domain
|
|
|
|
*
|
2016-05-01 17:56:49 +02:00
|
|
|
* @param string $db_type ='mysqli'
|
2007-12-09 09:03:15 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-05-26 11:43:10 +02:00
|
|
|
static function defaults($db_type='mysqli')
|
2007-12-09 09:03:15 +01:00
|
|
|
{
|
|
|
|
switch($db_type)
|
|
|
|
{
|
|
|
|
case 'mysql':
|
|
|
|
default:
|
|
|
|
$db_type = $meta_db = 'mysql';
|
|
|
|
break;
|
|
|
|
case 'pgsql':
|
|
|
|
$meta_db = 'template1';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'db_type' => $db_type,
|
|
|
|
'db_host' => 'localhost',
|
|
|
|
'db_port' => 3306,
|
|
|
|
'db_name' => 'egw_$domain',
|
|
|
|
'db_user' => 'egw_$domain',
|
2007-12-11 02:28:07 +01:00
|
|
|
'db_pass' => self::randomstring(),
|
2007-12-09 09:03:15 +01:00
|
|
|
'db_root' => 'root',
|
|
|
|
'db_root_pw' => '', // not really a default
|
|
|
|
'db_meta' => $meta_db,
|
|
|
|
'db_charset' => 'utf-8',
|
2008-11-18 20:58:11 +01:00
|
|
|
'db_grant_host' => 'localhost',
|
2007-12-09 09:03:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges the default into the current properties, if they are empty or contain placeholders
|
|
|
|
*/
|
|
|
|
private function _merge_defaults()
|
|
|
|
{
|
2007-12-11 02:28:07 +01:00
|
|
|
foreach(self::defaults() as $name => $default)
|
2007-12-09 09:03:15 +01:00
|
|
|
{
|
|
|
|
if (!$this->$name)
|
|
|
|
{
|
|
|
|
//echo "<p>setting $name='{$this->$name}' to it's default='$default'</p>\n";
|
|
|
|
$this->set_defaults[$name] = $this->$name = $default;
|
|
|
|
}
|
2008-10-02 17:27:39 +02:00
|
|
|
if (strpos($this->$name,'$domain') !== false)
|
2007-12-09 09:03:15 +01:00
|
|
|
{
|
2010-07-26 10:20:14 +02:00
|
|
|
// limit names to 16 chars (16 char is user-name limit in MySQL)
|
2013-05-26 11:43:10 +02:00
|
|
|
$this->set_defaults[$name] = $this->$name =
|
2010-07-31 11:23:41 +02:00
|
|
|
substr(str_replace(array('$domain','.','-'),array($this->domain,'_','_'),$this->$name),
|
|
|
|
0,self::MAX_DB_NAME_LEN);
|
2007-12-09 09:03:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|