package SB::Defaults;

use strict;
use warnings;
require SB::Utils;
require SB::OS;

use subs qw(getPHPPaths);
use vars qw($SB_PREFIX $SB_CONFIG $SB_HTDOCS);

BEGIN {
	use Exporter;
	our (@ISA, $VERSION, @EXPORT, @EXPORT_OK);
	$VERSION        =  4.00;
	@ISA = qw(Exporter);
    @EXPORT    = qw();
    @EXPORT_OK = qw($CONFIG $SB_HTDOCS $SB_CONFIG $SB_PREFIX %SQL_ADAPTER);
}

INIT { init(); }

our $SB_PREFIX;
our $SB_CONFIG;
our $SB_HTDOCS;

our $CONFIG = {
	'general' => {
		'preloader'					=> 1,
		'db_sessions'               => 1,
		'session_life_time'         => 86400,
		'use_rewrite'               => 1,
		'support_link'              => undef,
		'ftp_connect_timeout'       => undef,
		'application_url'           => undef,
		'vz'						=> undef,
		'remote_admin'				=> 1,
	},
	
	'database' => {
		'adapter'    => 'PDO_MYSQL',
		'host'       => 'localhost',
		'username'   => 'root',
		'password'   => '', 
		'dbname'     => 'sitebuilder3',
		'emulate_prepares' => 0,
	},
	
	'help' => {
		'admin_help_url'		=> undef,
		'user_help_url'			=> undef,
		'site_owner_help_url'	=> undef,
		'reseller_help_url'		=> undef,
	},

	'log' => {
		'system_log_level'   => 2,
		'security_log_level' => 2,
	},

	'path' => {
		'fonts' => 'resources/fonts',
		'htdocs' => 'htdocs',
		'skins'  => 'htdocs/skins',
		'include' => 'include',
		'resources' => 'resources',
		'styles' => 'resources/styles',
		'locale' => 'resources/locale',
		'wsdl' => 'resources/wsdl',
		'tmp' => 'tmp',
		'tmp_htdocs' => 'htdocs/tmp',
		'sql' => 'resources/sql',
		'templates' => 'htdocs/templates',
		'logos' => 'htdocs/images/logos',
		'utils' => 'utils',
		'sites' => 'htdocs/sites',
		'publish_modules' => 'resources/modules',
		'publish_modules_include' => 'resources/modules/include',
		'publish_modules_resources' => 'resources/modules/resources',
		'modules_htdocs' => 'htdocs/modules',
	},

	'url' => {
		'templates' => '/templates',
		'logos' => '/images/logos', 
		'tmp_htdocs' => '/tmp',
		'sites' => '/sites',
		'modules_htdocs' => '/modules',
	},

	'sso' => {
		'enabled'  => 0,
		'server'   => undef,
		'relay'    => undef,
	},

	'modulesPath' => {
		'publish_modules_config' => 'config.php',
		'publish_modules_data_folder' => 'data',
		'publish_modules_storage_folder' => 'storage',
		'publish_modules_storage_db' => 'sb_modules.db',
		'publish_modules_storage_attachments' => 'attachments',
		'publish_modules_settings' => 'settings',
		'publish_modules_settings_attachments' => 'attachments',
		'publish_modules_tmp_path' => 'tmp',
		'publish_site_password' => 'pwd.php',
		'publish_site_api' => 'api.php',
		'attachments_folder' => 'attachments',
	},

	'internal' => {
		'httpd_port'	=> 80,
		'phpcli_path'	=> "",
		'phpcgi_path'	=> "",
		'httpd_owner'	=> "",
		'db_port'		=> 3306,
		'db_admin_name'	=> "root",
		'db_admin_passwd'	=> "admin",

		'sb_admin_passwd' => "",
		#sb_admin_name	=> "admin",
		'sb_hostname'	=> "",
		'sb_host_ip'	=> "",
		'ext_ip'		=> "localhost",
		'enable_ssl'	=> 0
	}
};

#
our %SQL_ADAPTER = (
    pdoMysql  => "mysql",
    PDO_MYSQL => "mysql"
);

sub getPHPPaths() {
    my @phpcli_paths = (
				"/opt/php52/bin/php5",
				"/usr/local/sitebuilder/bin/php",
				"/opt/php51/bin/php5",
                "/usr/bin/php5",
                "/usr/bin/php",
                "/usr/local/bin/php");

    foreach my $path (@phpcli_paths) {
		next unless SB::Utils::checkPHPSanity($path, 51, "cli");
		$CONFIG->{'internal'}->{'phpcli_path'} = $path;
		last;
    }

    my @phpcgi_paths = (
				"/opt/php52/cgi-bin/php5",
				"/usr/local/sitebuilder/cgi-bin/php",
				"/opt/php51/cgi-bin/php5",
                "/usr/local/bin/php-cgi",
                "/srv/www/cgi-bin/php5",
                "/usr/bin/php5-cgi",
                "/usr/bin/php-cgi");

    foreach my $path (@phpcgi_paths) {
		next unless SB::Utils::checkPHPSanity($path, 51, "cgi");
		$CONFIG->{'internal'}->{'phpcgi_path'} = $path;
		last;
    }
}

sub getOpSysParams() {
    if (SB::OS::uname() eq "Linux") {
        $CONFIG->{'internal'}->{'sb_hostname'} = "sitebuilder." . `hostname -f`;
    } else {
        $CONFIG->{'internal'}->{'sb_hostname'} = "sitebuilder." . `hostname`;
    }
    chomp($CONFIG->{'internal'}->{'sb_hostname'});

    $CONFIG->{'internal'}->{'sb_host_ip'} =  SB::Utils::getHostAddr();
	$CONFIG->{'internal'}->{'httpd_owner'} = SB::OS::getApacheUser();

	my $applicationUrl = "http://"
		. $CONFIG->{'internal'}->{'sb_hostname'} . ":"
		. $CONFIG->{"internal"}->{'httpd_port'};

	substr($applicationUrl, 0, 4) = 'https' if  $CONFIG->{'internal'}->{'enable_ssl'};
	$CONFIG->{'general'}->{'application_url'} = $applicationUrl;
}

sub makeConfigCopy($) {
	my $config = shift;
	my $res = { };
	while (my($key,$value) = each(%$config)) {
		$res->{$key} = { %$value };
	}
	return $res;
}

sub init {
	getPHPPaths();
	getOpSysParams();
	$SB_PREFIX = SB::OS::getInstallPrefix();
	$SB_HTDOCS = $SB_PREFIX . "/htdocs";
	$SB_CONFIG = $SB_PREFIX . "/config";
}

1;

