<!DOCTYPE html>
<html>
<head>
	<title>Proja | نصب سیستم</title>
	<meta charset="utf-8">
	  	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	  	<link rel="stylesheet" type="text/css" href="../themes/default/css/bootstrap.min.css">
	  	<link rel="stylesheet" type="text/css" href="../themes/default/css/font-awesome.min.css">
	  	<link rel="stylesheet" type="text/css" href="../themes/default/css/style.css">
	  	<script src="../themes/default/js/jquery.min.js"></script>
	  	<script src="../themes/default/js/bootstrap.min.js"></script>
	  	<link rel="shortcut icon" href="../images/proja.png">
</head>
<body>
	<div class="container">

	  	<!-- Main component for a primary marketing message or call to action -->
	  	<div class="jumbotron col-lg-12 col-md-12 col-sm-12 col-xs-12 rtl" >
<?php 
require_once '../config.php';
include_once '../includes/class.database.php';
include_once '../includes/functions.php';
$error=$success="";
// $dbhost=$dbname=$dbusername=$dbpass=$prefix=$page_limit=$ausername=$apass="";
$db_connection = new dbConnection();

if($db_connection->connect()!==0)
{
	Success ("پروژا پیش از این با موفقیت نصب شده است");
	echo '
	<a href="../">ورود به بخش مدیریت</a><br>
	<small class="done">&nbsp;<i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp;لطفا جهت حفظ موارد امنیتی پوشه install را حذف نمایید!</small>
	';
}
else
{
$subject="به نظر می رسد پروژا هنوز نصب نشده است! برای راه اندازی سیستم، لطفا اطلاعات خواسته شده را با دقت و به طور کامل وارد کنید...";
	if (isset($_POST['install'])) 
	{
		$dbhost= $_POST['dbhost'];
		$dbname= $_POST['dbname'];
		$dbusername= $_POST['dbusername'];
		$dbpass=$_POST['dbpass'];
		$prefix = $_POST['prefix'].'_';
		$page_limit= 10;
		$ausername= $_POST['ausername'];
		$apass=$_POST['apass'];



		// Passwords Lenght validation
		if (strlen($apass) < 6)
			$error="رمز عبور انتخابی باید حداقل دارای 6 کاراکتر باشد";
		else
			$apass=md5($_POST['apass']);

		if (empty($dbhost) || empty($dbname) || empty($dbusername) || empty($dbpass) || empty($prefix) || empty($ausername) || empty($apass)) 
			$error="پرکردن تمامی فیلدها الزامی است";

		if ($error==="") {
			$dbConTest=$db_connection->connect();
			$type=gettype($dbConTest);
			if ($type==="integer") {
				if ($dbConTest==0)
					$error= "اطلاعات پایگاه داده غلط است. ارتباط با بانک میسر نشد";
			}
		}


		if ($error==="") //Now install Projecta
		{
			$handle=fopen('../config.php', 'w+');
			$data = "<?php\n\$dbhost = '".$dbhost."';\n\$dbname = '".$dbname."';\n\$dbusername = '".$dbusername."';\n\$dbpass = '".$dbpass."';\n\$prefix = '".$prefix."';\n\$page_limit = '".$page_limit."';\n?>";

		
			if (fwrite($handle, $data))
			{
				// echo '<small class="done">- فایل config.php با موفقیت ایجاد شد.<br></small>';
				try 
				{
				
					$dbh = $db_connection->connect();
					/*** echo a message saying we have connected ***/
					// echo '<small class="done">- اتصال به پایگاه داده برقرار شد.<br></small>';
				
					/*** set the PDO error mode to exception ***/
					$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
					/*** begin the transaction ***/
					$dbh->beginTransaction();
				
					/*** CREATE table statements ***/
					$dbh->exec("
						CREATE TABLE IF NOT EXISTS `".$prefix."admins` (
						  `aid` int(11) NOT NULL AUTO_INCREMENT,
						  `ausername` varchar(15) COLLATE utf8_persian_ci NOT NULL,
						  `apass` varchar(32) COLLATE utf8_persian_ci NOT NULL,
						  `aactive` tinyint(1) NOT NULL DEFAULT '1',
						  `aexpiration_date` text COLLATE utf8_persian_ci,
						  `afname` varchar(40) COLLATE utf8_persian_ci DEFAULT NULL,
						  `alname` varchar(40) COLLATE utf8_persian_ci DEFAULT NULL,
						  `agender` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0=M,1=F',
						  `atel` varchar(30) COLLATE utf8_persian_ci DEFAULT NULL,
						  `aemail` varchar(50) COLLATE utf8_persian_ci DEFAULT NULL,
						  `apic` varchar(30) COLLATE utf8_persian_ci DEFAULT NULL,
						  `acomments` text COLLATE utf8_persian_ci,
						  `allow_add_admin` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_delete_admin` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_edit_admin` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_list_admins` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_add_issue` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_edit_issue` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_delete_issue` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_list_issues` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_manage_issue_types` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_add_project` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_edit_project` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_delete_project` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_list_projects` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_add_task` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_edit_task` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_delete_task` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_list_tasks` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_access_1` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_access_2` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_access_3` tinyint(1) NOT NULL DEFAULT '0',
						  `allow_access_4` tinyint(1) NOT NULL DEFAULT '0',
						  `atimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
						  PRIMARY KEY (`aid`),
						  UNIQUE KEY `ausername` (`ausername`)
						) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
						");
					$dbh->exec("
						INSERT INTO `".$prefix."admins` (`aid`, `ausername`, `apass`, `aactive`, `aexpiration_date`, `afname`, `alname`, `agender`, `atel`, `aemail`, `apic`, `acomments`, `allow_add_admin`, `allow_delete_admin`, `allow_edit_admin`, `allow_list_admins`, `allow_add_issue`, `allow_edit_issue`, `allow_delete_issue`, `allow_list_issues`, `allow_manage_issue_types`, `allow_add_project`, `allow_edit_project`, `allow_delete_project`, `allow_list_projects`, `allow_add_task`, `allow_edit_task`, `allow_delete_task`, `allow_list_tasks`, `allow_access_1`, `allow_access_2`, `allow_access_3`, `allow_access_4`) VALUES
						(1, '$ausername', '$apass', 1, '', '', '', 0, '', '', '', '', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
						");
					$dbh->exec("
						CREATE TABLE IF NOT EXISTS `".$prefix."admins_tasks` (
						  `atid` int(11) NOT NULL AUTO_INCREMENT,
						  `aid` int(11) NOT NULL,
						  `tskid` int(11) NOT NULL,
						  PRIMARY KEY (`atid`),
						  KEY `aid` (`aid`),
						  KEY `tskid` (`tskid`)
						) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
						");
					$dbh->exec("
						CREATE TABLE IF NOT EXISTS `".$prefix."drafts` (
						  `drfid` int(11) NOT NULL AUTO_INCREMENT,
						  `aid` int(11) NOT NULL,
						  `drftitle` varchar(40) COLLATE utf8_persian_ci NOT NULL,
						  `drfdesc` text COLLATE utf8_persian_ci,
						  `drfpublished` tinyint(1) NOT NULL DEFAULT '0',
						  `drfdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
						  PRIMARY KEY (`drfid`),
						  KEY `aid` (`aid`)
						) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
						");
					$dbh->exec("
						CREATE TABLE IF NOT EXISTS `".$prefix."issues` (
						  `iid` int(11) NOT NULL AUTO_INCREMENT,
						  `aid` int(11) NOT NULL,
						  `tyid` int(11) DEFAULT NULL COMMENT 'Type Of Issue',
						  `prjid` int(11) DEFAULT NULL COMMENT 'Project ID',
						  `iversion` varchar(15) COLLATE utf8_persian_ci DEFAULT NULL,
						  `icode` varchar(50) COLLATE utf8_persian_ci NOT NULL,
						  `ititle` varchar(255) COLLATE utf8_persian_ci NOT NULL,
						  `idesc` text COLLATE utf8_persian_ci,
						  `iproirity` int(11) NOT NULL DEFAULT '0' COMMENT '0=None',
						  `icomplexity` tinyint(4) NOT NULL DEFAULT '0' COMMENT '0=Easy,1=Difficult,2=Hard,3=Very Hard',
						  `ineeded_time` varchar(11) COLLATE utf8_persian_ci DEFAULT NULL,
						  `ifile1` varchar(255) COLLATE utf8_persian_ci DEFAULT NULL,
						  `ifile2` varchar(255) COLLATE utf8_persian_ci DEFAULT NULL,
						  `ifile3` varchar(255) COLLATE utf8_persian_ci DEFAULT NULL,
						  `iarchive` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1=Archived',
						  `idate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
						  `iwho_fullname` varchar(100) COLLATE utf8_persian_ci DEFAULT NULL,
						  `iwho_email` varchar(50) COLLATE utf8_persian_ci DEFAULT NULL,
						  `iwho_tel` varchar(30) COLLATE utf8_persian_ci DEFAULT NULL,
						  `idone` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1=Done',
						  `idone_date` varchar(10) COLLATE utf8_persian_ci DEFAULT NULL,
						  `idone_version` varchar(30) COLLATE utf8_persian_ci DEFAULT NULL,
						  `icomments` text COLLATE utf8_persian_ci,
						  PRIMARY KEY (`iid`),
						  KEY `aid` (`aid`),
						  KEY `tyid` (`tyid`),
						  KEY `prjid` (`prjid`)
						) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
						");
					$dbh->exec("
						CREATE TABLE IF NOT EXISTS `".$prefix."issue_types` (
						  `tyid` int(11) NOT NULL AUTO_INCREMENT,
						  `aid` int(11) NOT NULL,
						  `tycode` varchar(50) COLLATE utf8_persian_ci NOT NULL,
						  `tytitle` varchar(255) COLLATE utf8_persian_ci NOT NULL,
						  `tycomments` text COLLATE utf8_persian_ci,
						  PRIMARY KEY (`tyid`),
						  KEY `aid` (`aid`)
						) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
						");
					$dbh->exec("
						INSERT INTO `".$prefix."issue_types` (`tyid`, `aid`, `tycode`, `tytitle`, `tycomments`) VALUES
						(1, 1, 'Features', 'امکانات', '');
						");
					$dbh->exec("
						INSERT INTO `".$prefix."issue_types` (`tyid`, `aid`, `tycode`, `tytitle`, `tycomments`) VALUES
						(2, 1, 'Bugs', 'ایرادات', '');
						");
					$dbh->exec("
						INSERT INTO `".$prefix."issue_types` (`tyid`, `aid`, `tycode`, `tytitle`, `tycomments`) VALUES
						(3, 1, 'Suggestions', 'پیشنهادات', '');
						");
					$dbh->exec("
						CREATE TABLE IF NOT EXISTS `".$prefix."projects` (
						  `prjid` int(11) NOT NULL AUTO_INCREMENT,
						  `aid` int(11) NOT NULL,
						  `prjcode` varchar(255) COLLATE utf8_persian_ci NOT NULL,
						  `prjtitle` varchar(255) COLLATE utf8_persian_ci NOT NULL,
						  `prjdesc` text COLLATE utf8_persian_ci,
						  `prjlogo` varchar(50) COLLATE utf8_persian_ci DEFAULT NULL,
						  `prjcomments` text COLLATE utf8_persian_ci,
						  `prjdate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
						  PRIMARY KEY (`prjid`),
						  KEY `aid` (`aid`)
						) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
						");
					$dbh->exec("
						CREATE TABLE IF NOT EXISTS `".$prefix."tasks` (
						  `tskid` int(11) NOT NULL AUTO_INCREMENT,
						  `aid` int(11) NOT NULL,
						  `prjid` int(11) NOT NULL,
						  `tskcode` varchar(255) COLLATE utf8_persian_ci NOT NULL,
						  `tsktitle` varchar(255) COLLATE utf8_persian_ci NOT NULL,
						  `tskdesc` text COLLATE utf8_persian_ci,
						  `tskdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
						  `tskdone` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1=Done',
						  `tskdone_date` varchar(10) COLLATE utf8_persian_ci DEFAULT NULL,
						  `tskdone_aid` varchar(50) COLLATE utf8_persian_ci DEFAULT NULL,
						  `tskcomments` text COLLATE utf8_persian_ci,
						  PRIMARY KEY (`tskid`),
						  KEY `prjid` (`prjid`),
						  KEY `aid` (`aid`)
						) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
						");
					$dbh->exec("
						CREATE TABLE IF NOT EXISTS `".$prefix."tasks_issues` (
						  `tiid` int(11) NOT NULL AUTO_INCREMENT,
						  `tskid` int(11) DEFAULT NULL,
						  `iid` int(11) DEFAULT NULL,
						  PRIMARY KEY (`tiid`),
						  KEY `tskid` (`tskid`),
						  KEY `iid` (`iid`)
						) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
						");
					$dbh->exec("
						ALTER TABLE `".$prefix."admins_tasks`
						  ADD CONSTRAINT `".$prefix."admins_tasks_aid_fk` FOREIGN KEY (`aid`) REFERENCES `".$prefix."admins` (`aid`) ON DELETE CASCADE ON UPDATE CASCADE,
						  ADD CONSTRAINT `".$prefix."admins_tasks_tskid_fk` FOREIGN KEY (`tskid`) REFERENCES `".$prefix."tasks` (`tskid`) ON DELETE CASCADE ON UPDATE CASCADE;
						");
					$dbh->exec("
						ALTER TABLE `".$prefix."drafts`
						  ADD CONSTRAINT `".$prefix."drafts_aid_fk` FOREIGN KEY (`aid`) REFERENCES `".$prefix."admins` (`aid`) ON DELETE CASCADE ON UPDATE CASCADE;
						");
					$dbh->exec("
						ALTER TABLE `".$prefix."issues`
						  ADD CONSTRAINT `".$prefix."issues_aid_fk` FOREIGN KEY (`aid`) REFERENCES `".$prefix."admins` (`aid`) ON UPDATE CASCADE,
						  ADD CONSTRAINT `".$prefix."issues_prjid_fk` FOREIGN KEY (`prjid`) REFERENCES `".$prefix."projects` (`prjid`) ON DELETE NO ACTION ON UPDATE CASCADE,
						  ADD CONSTRAINT `".$prefix."issues_tyid_fk` FOREIGN KEY (`tyid`) REFERENCES `".$prefix."issue_types` (`tyid`) ON DELETE CASCADE ON UPDATE CASCADE;
						");
					$dbh->exec("
						ALTER TABLE `".$prefix."issue_types`
						  ADD CONSTRAINT `".$prefix."issue_types_aid_fk` FOREIGN KEY (`aid`) REFERENCES `".$prefix."admins` (`aid`) ON DELETE NO ACTION ON UPDATE CASCADE;
						");
					$dbh->exec("
						ALTER TABLE `".$prefix."projects`
						  ADD CONSTRAINT `".$prefix."projects_aid_fk` FOREIGN KEY (`aid`) REFERENCES `".$prefix."admins` (`aid`) ON DELETE NO ACTION ON UPDATE CASCADE;
						");
					$dbh->exec("
						ALTER TABLE `".$prefix."tasks`
						  ADD CONSTRAINT `".$prefix."tasks_aid_fk` FOREIGN KEY (`aid`) REFERENCES `".$prefix."admins` (`aid`) ON DELETE NO ACTION ON UPDATE CASCADE,
						  ADD CONSTRAINT `".$prefix."tasks_prjid_fk` FOREIGN KEY (`prjid`) REFERENCES `".$prefix."projects` (`prjid`);
						");
					$dbh->exec("
						ALTER TABLE `".$prefix."tasks_issues`
						  ADD CONSTRAINT `".$prefix."tasks_issues_iid_fk` FOREIGN KEY (`iid`) REFERENCES `".$prefix."issues` (`iid`) ON UPDATE CASCADE,
						  ADD CONSTRAINT `".$prefix."tasks_issues_tskid_fk` FOREIGN KEY (`tskid`) REFERENCES `".$prefix."tasks` (`tskid`) ON DELETE CASCADE ON UPDATE CASCADE;
						");
					
							/*** commit the transaction ***/
							$dbh->commit();
							$subject="";
							
						
							/*** echo a message to say the database was created ***/
							$message="تبریک عرض می‌کنیم! پروژا با موفقیت نصب شد";
							echo '<div class="alert alert-success"><img src="../images/checked.png" width="25px">  '.$massage.'...</div>';
							echo'
							<a href="../"><strong>پنل مدیریت</strong></a><br>
							<small class="done">&nbsp;<i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp;لطفاً به بخش مدیریت سیستم وارد شوید و تنظیمات سیستم را کامل نمایید.</small><br>
							 
							<small class="done">&nbsp;<i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp;لطفا جهت حفظ موارد امنیتی پوشه install را حذف نمایید!</small>
							';
				}
				catch(PDOException $e)
				{
					/*** roll back the transaction if we fail ***/
					$dbh->rollback();
						
				}
			}
			else
			$error= "مشکلی در ساختن فایل config.php به وجود آمد. لطفاً سطح دسترسی این فایل را روی 777 تنظیم نمایید"; 
		}
	}

if (!empty($error)) 
	Failure($error);

if (!empty($success)) 
	Success($success);
echo '

<center><img src="../images/brand-logo.png" width="200"></center>
<div class="red form-group col-md-12 col-sm-12 col-xs-12">
	<center><span>'.$subject.'</span></center>
</div>
<div class="indexframe1ins form-group col-md-12 col-sm-12 col-xs-12">
	<center><h4>نصب پروژا</h4></center>
</div>
<div class="form-group col-md-12 col-sm-12 col-xs-12">
	<form action="" method="post" >
		<div class="form-group col-md-4 col-sm-12 col-xs-12">
			<label for="dbhost">آدرس هاست<span class="required"> *</span>:</label>
			<input type="text" class="form-control ltr " id="dbhost" name="dbhost" value="'.(isset($_POST['dbhost'])?$_POST['dbhost']:'localhost').'">
			<small class="done">&nbsp;<i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp;اگر نمی‌دانید این فیلد به چه معناست، همان localhost رها کنید. در اکثر مواقع این عبارت صحیح است.</small>
		</div>
		<div class="form-group col-md-4 col-sm-12 col-xs-12">
			<label for="dbname">نام دیتابیس<span class="required"> *</span>:</label>
			<input type="text" class="form-control ltr " id="dbname" name="dbname" value="'.(isset($_POST['dbname'])?''.$_POST['dbname'].'':'').'">
		</div>
		<div class="form-group col-md-4 col-sm-12 col-xs-12">
			<label for="dbusername">نام کاربری دیتابیس<span class="required"> *</span>:</label>
			<input type="text" class="form-control ltr " id="dbusername" name="dbusername" value="'.(isset($_POST['dbusername'])?''.$_POST['dbusername'].'':'').'">
		</div>
		<div class="form-group col-md-4 col-sm-12 col-xs-12">
			<label for="dbpass">رمز عبور دیتابیس<span class="required"> *</span>:</label>
			<input type="password" class="form-control ltr " id="dbpass" name="dbpass" autocomplete="off">
		</div>
		<div class="form-group col-md-4 col-sm-12 col-xs-12">
			<label for="prefix">پیشوند (دلخواه) برای جداول دیتابیس<span class="required"> *</span>:</label>
			<input type="text" class="form-control ltr " id="prefix" name="prefix" value="'.(isset($_POST['prefix'])?''.$_POST['prefix'].'':'').'">
		</div><hr>
		<div class="form-group col-md-4 col-sm-12 col-xs-12">
		</div>
		<div class="form-group col-md-4 col-sm-12 col-xs-12">
			<label for="ausername">نام کاربری مدیر کل<span class="required"> *</span>:</label>
			<input type="text" class="form-control ltr " id="ausername" name="ausername" value="'.(isset($_POST['ausername'])?''.$_POST['ausername'].'':'').'">
		</div>
		<div class="form-group col-md-4 col-sm-12 col-xs-12">
			<label for="apass">رمز عبور مدیرکل<span class="required"> *</span>:</label>
			<input type="password" class="form-control ltr " id="apass" name="apass" autocomplete="off">
		</div>
		<div class="form-group col-md-12 col-sm-12 col-xs-12">
			<center><input type="submit" name="install" value="نصب" class="btn btn-primary btn-large"></center>
		</div>
	</form>
</div>
';
}
 ?>
		</div>
	</div>
</body>
</html>




