Skip to content
installer_base.lib.php 50.1 KiB
Newer Older
<?php

/*
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class installer_base {
	
	var $wb = array();
	var $language = 'en';
	var $db;
oliver's avatar
oliver committed
	public $conf;
tbrehm's avatar
tbrehm committed
	public $install_ispconfig_interface = true;
tbrehm's avatar
tbrehm committed
	public $is_update = false; // true if it is an update, falsi if it is a new install
pedro_morgan's avatar
pedro_morgan committed
    public function __construct()
        global $conf; //TODO: maybe $conf  should be passed to constructor
    //: TODO  Implement the translation function and language files for the installer.
pedro_morgan's avatar
pedro_morgan committed
	public function lng($text)
    {
tbrehm's avatar
tbrehm committed
		return $text;
pedro_morgan's avatar
pedro_morgan committed
	public function error($msg)
    {
pedro_morgan's avatar
pedro_morgan committed
	public function simple_query($query, $answers, $default)
    {		
tbrehm's avatar
tbrehm committed
		$finished = false;
		do {
pedro_morgan's avatar
pedro_morgan committed
			$answers_str = implode(',', $answers);
tbrehm's avatar
tbrehm committed
			swrite($this->lng($query).' ('.$answers_str.') ['.$default.']: ');
tbrehm's avatar
tbrehm committed
			$input = sread();
			
pedro_morgan's avatar
pedro_morgan committed
			//* Stop the installation
tbrehm's avatar
tbrehm committed
			if($input == 'quit') {
pedro_morgan's avatar
pedro_morgan committed
				swriteln($this->lng("Installation terminated by user.\n"));
tbrehm's avatar
tbrehm committed
				die();
tbrehm's avatar
tbrehm committed
			}
			
pedro_morgan's avatar
pedro_morgan committed
			//* Select the default
tbrehm's avatar
tbrehm committed
			if($input == '') {
				$answer = $default;
				$finished = true;
			}
			
pedro_morgan's avatar
pedro_morgan committed
            //* Set answer id valid
			if(in_array($input, $answers)) {
tbrehm's avatar
tbrehm committed
				$answer = $input;
				$finished = true;
			}
			
		} while ($finished == false);
tbrehm's avatar
tbrehm committed
		swriteln();
tbrehm's avatar
tbrehm committed
		return $answer;
	}
	
pedro_morgan's avatar
pedro_morgan committed
	public function free_query($query,$default)
    {		
tbrehm's avatar
tbrehm committed
		swrite($this->lng($query).' ['.$default.']: ');
tbrehm's avatar
tbrehm committed
		$input = sread();
			
pedro_morgan's avatar
pedro_morgan committed
		//* Stop the installation
tbrehm's avatar
tbrehm committed
		if($input == 'quit') {
pedro_morgan's avatar
pedro_morgan committed
            swriteln($this->lng("Installation terminated by user.\n"));
            die();
tbrehm's avatar
tbrehm committed
		}
			
pedro_morgan's avatar
pedro_morgan committed
        $answer =  ($input == '') ? $default : $input;
tbrehm's avatar
tbrehm committed
		swriteln();
tbrehm's avatar
tbrehm committed
		return $answer;
	}
	
pedro_morgan's avatar
pedro_morgan committed
	// TODO: this function is not used atmo I think - pedro
	function request_language(){
		
		swriteln(lng('Enter your language'));
		swriteln(lng('de, en'));
		
	}
tbrehm's avatar
tbrehm committed
	//** Detect installed applications
	public function find_installed_apps() {
		global $conf;
		
		if(is_installed('mysql') || is_installed('mysqld')) $conf['mysql']['installed'] = true;
		if(is_installed('postfix')) $conf['postfix']['installed'] = true;
		if(is_installed('apache') || is_installed('apache2') || is_installed('httpd')) $conf['apache']['installed'] = true;
		if(is_installed('getmail')) $conf['getmail']['installed'] = true;
		if(is_installed('couriertcpd')) $conf['courier']['installed'] = true;
		if(is_installed('saslsauthd')) $conf['saslauthd']['installed'] = true;
		if(is_installed('amavisd-new')) $conf['amavis']['installed'] = true;
		if(is_installed('clamdscan')) $conf['clamav']['installed'] = true;
		if(is_installed('pure-ftpd') || is_installed('pure-ftpd-wrapper')) $conf['pureftpd']['installed'] = true;
		if(is_installed('mydns') || is_installed('mydns-ng')) $conf['mydns']['installed'] = true;
		if(is_installed('jk_chrootsh')) $conf['jailkit']['installed'] = true;
		
		
	}
	
	/** Create the database for ISPConfig */ 
tbrehm's avatar
tbrehm committed
	public function configure_database() {
		//** Create the database
		if(!$this->db->query('CREATE DATABASE IF NOT EXISTS '.$conf['mysql']['database'].' DEFAULT CHARACTER SET '.$conf['mysql']['charset'])) {
			$this->error('Unable to create MySQL database: '.$conf['mysql']['database'].'.');
		//* Set the database name in the DB library
		$this->db->dbName = $conf['mysql']['database'];
		//* Load the database dump into the database, if database contains no tables
		$db_tables = $this->db->getTables();
		if(count($db_tables) > 0) {
			$this->error('Stopped: Database already contains some tables.');
			if($conf['mysql']['admin_password'] == '') {
				caselog("mysql --default-character-set=".$conf['mysql']['charset']." -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' '".$conf['mysql']['database']."' < '".ISPC_INSTALL_ROOT."/install/sql/ispconfig3.sql' &> /dev/null", 
                        __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in ispconfig3.sql');
				caselog("mysql --default-character-set=".$conf['mysql']['charset']." -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' -p'".$conf['mysql']['admin_password']."' '".$conf['mysql']['database']."' < '".ISPC_INSTALL_ROOT."/install/sql/ispconfig3.sql' &> /dev/null", 
                        __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in ispconfig3.sql');
			}
			$db_tables = $this->db->getTables();
			if(count($db_tables) == 0) {
				$this->error('Unable to load SQL-Dump into database table.');
			}
			
			//* Load system.ini into the sys_ini table
			$system_ini = $this->db->quote(rf('tpl/system.ini.master'));
			$this->db->query("UPDATE sys_ini SET config = '$system_ini' WHERE sysini_id = 1");
			
	//** Create the server record in the database
	public function add_database_server_record() {
		
		if($conf['mysql']['host'] == 'localhost') {
tbrehm's avatar
tbrehm committed
			$from_host = 'localhost';
		} else {
		// Delete ISPConfig user in the local database, in case that it exists
		$this->db->query("DELETE FROM mysql.user WHERE User = '".$conf['mysql']['ispconfig_user']."' AND Host = '".$from_host."';");
		$this->db->query("DELETE FROM mysql.db WHERE Db = '".$conf['mysql']['database']."' AND Host = '".$from_host."';");
		$this->db->query('FLUSH PRIVILEGES;');
		
		//* Create the ISPConfig database user in the local database
        $query = 'GRANT SELECT, INSERT, UPDATE, DELETE ON '.$conf['mysql']['database'].".* "
                ."TO '".$conf['mysql']['ispconfig_user']."'@'".$from_host."' "
                ."IDENTIFIED BY '".$conf['mysql']['ispconfig_password']."';";
		if(!$this->db->query($query)) {
			$this->error('Unable to create database user: '.$conf['mysql']['ispconfig_user'].' Error: '.$this->db->errorMessage);
		}
		
		//* Reload database privelages
		$this->db->query('FLUSH PRIVILEGES;');
		
tbrehm's avatar
tbrehm committed
		//* Set the database name in the DB library
		$this->db->dbName = $conf['mysql']['database'];
tbrehm's avatar
tbrehm committed
		$tpl_ini_array = ini_to_array(rf('tpl/server.ini.master'));
Loading full blame...