<?php
Error_Reporting(4);

function get_document_root() {
	global $_SERVER;
	$SCRIPT_FILENAME=str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']);
	$SCRIPT_NAME=str_replace('\\', '/', $_SERVER['SCRIPT_NAME']);
	return substr($SCRIPT_FILENAME, 0, strpos($SCRIPT_FILENAME, $SCRIPT_NAME));
	//$st=(strrpos($_SERVER['SCRIPT_FILENAME'], '\\') ? str_replace('/', '\\', $_SERVER['SCRIPT_NAME']) : $_SERVER['SCRIPT_NAME']);
	//return substr($_SERVER['SCRIPT_FILENAME'], 0, strpos($_SERVER['SCRIPT_FILENAME'], $st));
}

if (is_file("../tools/inc.php")) {
	include_once("../tools/inc.php");
	$PATH=$config["path"];
} else {
	$step=0;
	$PATH=preg_replace("/(.*)install\/.*$/", "$1", $_SERVER['REQUEST_URI']);
}
$DOCUMENT_ROOT=get_document_root();
$HTTP_HOST=$_SERVER['HTTP_HOST'];
chdir($DOCUMENT_ROOT.$PATH);

include "tools/template.php";
include "tools/db.php";
include "tools/tools.php";
include "tools/function.sql.php";
include "tools/function.string.php";
include "tools/class.link.php";
include "tools/class.message.php";
get_all_vars();

$array=pathinfo($_SERVER['SCRIPT_NAME']);
$link=new Link();

$page = new Template("templates/index.tpl");
$page->dictionary("common/dict.ru");
$message=new Message();
$error=new Message("error");
$db=new DB(true);
if (isset($step) and $step==2) {
	include "install/sql.php";
	$db->execute_all($sql);
	$page->section("INSTALL_2");
	for ($i=0; $i<count($actions); $i++) {
		$page->addrow("LINE", $actions[$i]);
	}
	$message->add("{rs:MSG_INSTALLATION_COMPLETED}");
	$page->assign($r);
} elseif (isset($step) and $step==1) {
	if (isset($save)) {
		$table="users".$config["table_prefix"];
		$r=check_array($r);
		$r["date_created"]=time();
		$r["modify_user"]=0;
		$r["modify_time"]=time();
		if (!$r["login"]) $error->add("{rs:ERROR_LOGIN}");
		if (!$r["password"]) $error->add("{rs:ERROR_PASSWORD}");
		if ($r["password"]!=$r["password2"]) $error->add("{rs:ERROR_PASSWORD_CONFIRMATION}");
		if (!check_email($r["mail"])) $error->add("{rs:ERROR_MAIL}");
		if (!$error->count) {
			$r["password"]=($r["password"] ? $r["password"] : $data["password"]);
			$r["system_status"]=1;
			$r["status"]=1;
			$fields="date_created, system_status, login, password, mail, name, gender, phone_h, phone_w, phone_c, status, modify_user, modify_time";
			$sql=array();
			$sql[]="create table $table (
				id int not null auto_increment,
				date_created int,
				system_status int default 0,
				login varchar(30),
				password varchar(30),
				mail varchar(255),
				name varchar(255),
				gender int default 0,
				phone_h varchar(255),
				phone_w varchar(255),
				phone_c varchar(255),
				status int default 0,
				modify_user int,
				modify_time int,
				primary key(id))";
			$sql[]=get_sql_insert($table, $fields, $r);
	                $db->execute_all($sql);
	                $db->close();
			// Cron Start
			$filename=$DOCUMENT_ROOT.$PATH.'tools/cron.txt';
			$f=fopen($filename, 'w');
			if ($f) {
				fwrite($f, "MAILTO=\"{r[mail]}\"\n");
				fwrite($f, '0 0 * * * '.$DOCUMENT_ROOT.$PATH.'tools/stat_daily_delete.php'."\n");
				fwrite($f, '* * * * * '.$DOCUMENT_ROOT.$PATH.'tools/auto_create.php'."\n");
				fwrite($f, '* * * * * '.$DOCUMENT_ROOT.$PATH.'tools/msg_autoresponder.php'."\n");
				fwrite($f, '* * * * * '.$DOCUMENT_ROOT.$PATH.'tools/msg_campaign_schedule.php'."\n");
				fwrite($f, '*/10 * * * * '.$DOCUMENT_ROOT.$PATH.'tools/msg_check.php'."\n");
				fclose($f);
				//exec('crontab '.$filename);
				@unlink($filename);
			}
			// Cron End
	                $link->add("step", "2");
	                header("Location: ".$link->get());
	                exit;
		} else {
			$r=html_string($r, true);
		}
        } else {
		$r=array(
			"gender"	=> 1,
		);
	}
	$page->section("INSTALL_1");
	$r["BACK_LINK"]=($link->get() ? $link->get().'&' : '?')."step=0";
	$page->assign($r);
} else {
	if (isset($save)) {
		$r=check_array($r);
		$config=array(
			"db"	=> array(
				"server"	=> $r["server"],
				"username"	=> $r["username"],
				"password"	=> $r["password"],
				"database"	=> $r["database"],
		));
		$db=new DB(true);
		chmod("tools/", 0777);
		if ($db->error_code) $error->add("{rs:ERROR_DB_CONNECT}");
		elseif (!$fl=fopen("tools/inc.php", "w")) $error->add("{rs:ERROR_SAVE_CONFIG_FILE}");
		if (!$error->count) {
			$cfg="<?php
\$DOCUMENT_ROOT=\"%DOCUMENT_ROOT%\";
\$config=array(
	\"db\"		=> array (
				\"server\"	=> \"%server%\",
				\"username\"	=> \"%username%\",
				\"password\"	=> \"%password%\",
				\"port\"		=> \"%port%\",
				\"database\"	=> \"%database%\"),
	\"table_prefix\"	=> \"%table_prefix%\",
	\"path\"		=> \"%path%\",
	\"host\"		=> \"%host%\");
?>";
			$f=array("%DOCUMENT_ROOT%", "%server%", "%username%", "%password%", "%port%", "%database%", "%table_prefix%", "%path%", "%host%", "\r");
			$v=array($DOCUMENT_ROOT, $r["server"], $r["username"], $r["password"], $r["port"], $r["database"], $r["table_prefix"], $PATH, $HTTP_HOST, "");
			$cfg=str_replace($f, $v, $cfg);
			fwrite($fl, $cfg);
			fclose($fl);

			$table_list=array(
				"access".$r["table_prefix"],
				"autoresponder".$r["table_prefix"],
				"black_list".$r["table_prefix"],
				"campaign_schedule".$r["table_prefix"],
				"custom_fields".$r["table_prefix"],
				"encodes".$r["table_prefix"],
				"mailing_groups".$r["table_prefix"],
				"modules".$r["table_prefix"],
				"msg_autoresponder".$r["table_prefix"],
				"msg_campaign_schedule".$r["table_prefix"],
				"msg_confirm_subscribe".$r["table_prefix"],
				"msg_confirm_unsubscribe".$r["table_prefix"],
				"msg_newsletters".$r["table_prefix"],
				"newsletters".$r["table_prefix"],
				"pages".$r["table_prefix"],
				"pop3_accounts".$r["table_prefix"],
				"scf".$r["table_prefix"],
				"settings".$r["table_prefix"],
				"smtp_accounts".$r["table_prefix"],
				"stat".$r["table_prefix"],
				"stat_daily".$r["table_prefix"],
				"stat_links".$r["table_prefix"],
				"stat_opened_newsletters".$r["table_prefix"],
				"stat_bounced".$r["table_prefix"],
				"stat_unsubscribes".$r["table_prefix"],
				"subscribers".$r["table_prefix"],
				"subscribers_group".$r["table_prefix"],
				"templates".$r["table_prefix"],
				"user_access".$r["table_prefix"],
				"users".$r["table_prefix"],
				"users_newsletter".$r["table_prefix"],
				"users_mailing_groups".$r["table_prefix"],
			);
			$sql="drop table if exists ".implode(", ", $table_list);
			$db->execute($sql);
			$db->close();
			$link->add("step", "1");
			header("Location: ".$link->get());
			exit;
		} else $r=html_string($r, true);
	} else {
		chmod("files/", 0777);
		chmod("files/export/", 0777);
		chmod("files/import/", 0777);
		chmod("files/stat/", 0777);
		$r["server"]='localhost';
		$r["table_prefix"]='_mailing';
	}
	$page->section("INSTALL_0");
	$page->assign($r);
}

$page->assign(array(
        "PATH"  	=> $PATH,
	"MESSAGE"	=> ($message->count ? $message->get() : ""),
	"ERROR"		=> ($error->count ? $error->get() : ""),
	"IMG"		=> "installation.gif",
	"WINDOW_TITLE"	=> "{rs:TITLE_INSTALL}",
));
print $page->evaluate();
exit;
?>