<?php

ob_start();

$error="";

$success="";

if(isset($_POST['button'])){	

$dbname=$_POST['dbname'];

$dbusername=$_POST['dbusername'];

$password=$_POST['password'];

$siteusername=$_POST['siteusername'];

$sitepassword=md5($_POST['sitepassword']);

$dbhost=$_POST['dbhost'];




$myFile = "../web_school/inc/class/dbconnect.php";
$myFile2 = "../web_school/inc/class/config.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$fh2 = fopen($myFile2, 'w') or die("can't open file");
$stringData = '<?php 
  $this->host = "'.$dbhost.'";                 
  $this->password = "'.$password.'";           
  $this->user = "'.$dbusername.'";                
  $this->database = "'.$dbname.'";           
  $this->rows = 0; 
?>';
$stringData2 = '<?php 
$connection = mysql_connect("'.$dbhost.'","'.$dbusername.'","'.$password.'");
mysql_select_db("'.$dbname.'",$connection);
?>';
fwrite($fh, $stringData);

fclose($fh);
fwrite($fh2, $stringData2);

fclose($fh2);
$link = mysql_connect("$dbhost", "$dbusername", "$password");

mysql_select_db($dbname) or ("Couldnt Find the Database");

if (!$link) {

    $error='Could not connect to database';

}



$sql = "CREATE DATABASE IF NOT EXISTS $dbname";

if (mysql_query($sql, $link)) {

    $success="<div class='notice-four'>Installation Successfull.\n

						<span></span>

					</div>";

}else {

   $error=1;

}



/*Table creation*/

if($success){

mysql_query("CREATE TABLE IF NOT EXISTS $dbname.`admin_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(200) NOT NULL,
  `email` varchar(200) NOT NULL,
  `date_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `type` int(11) NOT NULL,
  `phone` int(11) NOT NULL,
  `user_key` varchar(500) NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1", $link);

    $today = date("Y-m-d, h:i:s");

	$ip=$_SERVER['REMOTE_ADDR'];


mysql_query("INSERT INTO $dbname.`admin_users` (`id`, `first_name`, `last_name`, `username`, `password`, `email`, `date_updated`, `type`, `phone`, `user_key`, `status`) VALUES
(1, 'Administrator', 'WebSchool', '$siteusername', '$sitepassword', '', '$today', 101, 0, '', 1)");



mysql_query("
CREATE TABLE IF NOT EXISTS $dbname.`photos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_type` varchar(50) NOT NULL,
  `path` varchar(200) NOT NULL,
  `userid` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");



mysql_query("CREATE TABLE IF NOT EXISTS $dbname.`client_org` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `admin_id` int(11) NOT NULL,
  `school_name` varchar(200) NOT NULL,
  `contact_person` varchar(100) NOT NULL,
  `contact_email` varchar(100) NOT NULL,
  `phone_number` varchar(100) NOT NULL,
  `address` varchar(100) NOT NULL,
  `pb_no` int(11) NOT NULL,
  `mobile` varchar(100) NOT NULL,
  `fax` varchar(100) NOT NULL,
  `sms_credict` int(11) NOT NULL,
  `email_credict`  int(11) NOT NULL,
  `created_date`  datetime NOT NULL,
  `updated_date` datetime NOT NULL,
  `package_start_date` datetime NOT NULL,
  `daily_agenda` int(11) NOT NULL,
  `start_date` datetime NOT NULL,
  `end_date`  datetime NOT NULL,
  `attendance_type` varchar(50) NOT NULL,
  `language` varchar(50) NOT NULL,
  `timezone` varchar(50) NOT NULL,
  `country` varchar(50) NOT NULL,
  `currency_type` varchar(50) NOT NULL,
  `logo` varchar(500) NOT NULL,
  `grading_type` varchar(50) NOT NULL,
  `user_key` varchar(100) NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
mysql_query("INSERT INTO $dbname.`client_org` (`id`, `admin_id`, `school_name`, `contact_person`, `contact_email`, `phone_number`, `address`, `pb_no`, `mobile`, `fax`, `sms_credict`, `email_credict`, `created_date`, `updated_date`, `package_start_date`, `daily_agenda`, `start_date`, `end_date`, `attendance_type`, `language`, `timezone`, `country`, `currency_type`, `logo`, `grading_type`, `user_key`, `status`) VALUES ('101', '', 'WebSchool', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '1')");


mysql_query("CREATE TABLE IF NOT EXISTS $dbname.`course` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `course_name` varchar(100) NOT NULL,
  `section_name` varchar(100) NOT NULL,
  `code` varchar(100) NOT NULL,
  `org_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");

mysql_query("CREATE TABLE IF NOT EXISTS $dbname.`batch` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `course_id` int(10) NOT NULL,
  `batch` varchar(100) NOT NULL,
  `start_date` varchar(20) NOT NULL,
  `end_date` varchar(20) NOT NULL,
  `org_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");

mysql_query("CREATE TABLE IF NOT EXISTS $dbname.`subjects` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `course_id` int(10) NOT NULL,
  `batch_id` int(10) NOT NULL,
  `subject` varchar(100) NOT NULL,
  `staff` int(10) NOT NULL,
  `org_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");

mysql_query("CREATE TABLE IF NOT EXISTS $dbname.`std_category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `std_category` varchar(200) NOT NULL,
  `org_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");

mysql_query("CREATE TABLE IF NOT EXISTS $dbname.`emp_leave` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `leavetype` varchar(11) NOT NULL,
  `code` int(11) NOT NULL,
  `leave_count` int(11) NOT NULL,
  `carry_forward` int(11) NOT NULL,
  `org_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");





if(!$error)

{header("location:../web_school/index.php?success");}

} 

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html dir="ltr" lang="en-US" xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

<title>Installation</title>

<!-- Styles -->
<link rel="stylesheet" href="../web_school/bootstrap/css/bootstrap.min.css">
	<link rel="stylesheet" href="../web_school/css/todc-bootstrap.min.css">
	<link rel="stylesheet" href="../web_school/css/style.css">
    <link rel="stylesheet" href="../web_school/css/custom_style.css">
	<link rel="stylesheet" href="../web_school/css/theme/color_1.css" id="theme">
<link rel='stylesheet' href='_layout/style.css' type='text/css' media='all' />

<!--[if IE]>

		

			<link rel='stylesheet' href='_layout/IE.css' type='text/css' media='all' />		

			

		<![endif]-->

<!-- Fonts -->

<link href='http://fonts.googleapis.com/css?family=Droid+Sans:regular,bold|PT+Sans+Narrow:regular,bold|Droid+Serif:i&amp;v1' rel='stylesheet' type='text/css' />

<!-- Scripts -->

<script type='text/javascript' src='../../../../ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min97e1.js?ver=1.7'></script>

<script type='text/javascript' src='_layout/custom.js'></script>

</head>

<body>

<div id="layout">

  <div class="page2 fixed" >

    <div id="content">

      <?php if($error){?>

      <div class='notice-three'>Error establishing a database connection <span></span> </div>

      <br />

      <p> This either means that the username and password information in you provided is incorrect or we can't contact the database server at localhost. This could mean your host's database server is down.<br />

      <ul>

        <li>Are you sure you have the correct username and password?</li>

        <li> Are you sure that you have typed the correct hostname?</li>

        <li> Are you sure that the database server is running?</li>

      </ul>

      <br />

      If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit our Support Forums(support@web-school.in).<br />

      <div align="center"><a href="index.php" class="button-blue">Try Again</a></div>

      </p>

      <?php }else{

					?>

      <div class='notice-one'>Install directory should be deleted after installation. <span></span> </div>

      <h1>Install </h1>

      <div class="hr"></div>

      <p><strong>Follow these steps for installing Web School</strong></p>

      <p>

        <?php if($success)echo $success;?>

      </p>

      <h1>Installation Instructions</h1>

      <div class="hr"></div>

      <p>You have simple steps to configure this <strong>Web School</strong>.</p>

      <div class="fixed"> </div>

      <ul class="list-style-arrow-right">

        <li>Create a database in PHPMyAdmin</li>

        <li>Set Mysql username</li>

        <li>Set Mysql Password</li>

        <li>Give the database name,username and password to the following coresponding fields.</li>

        <li>Delete install directory after installation is completed.</li>

      </ul>

      <div style="clear:both"></div>

      <div class="hr"></div>

      <div class="fixed">

        <form action="index.php" method="post" id="installation" name="installation">

          <table width="507" border="0">

            <tr>

              <td width="195">Database Name</td>

              <td width="302"><label>

                  <input type="text" name="dbname" id="dbname" data-required="true" />

                </label></td>

            </tr>

            <tr>

              <td>User Name</td>

              <td>&nbsp;

                <input type="text" name="dbusername" id="dbusername" data-required="true"/></td>

            </tr>

            <tr>

              <td>Password</td>

              <td>&nbsp;

                <input type="text" name="password" id="password" /></td>

            </tr>

            <tr>

              <td>Database Host</td>

              <td>&nbsp;

                <input type="text" name="dbhost" id="dbhost" value="localhost" data-required="true"/></td>

            </tr>

            <tr>

              <td colspan="2"><div class="hr"></div></td>

            </tr>

            <tr>

              <td colspan="2"><h2>Login Information for Admin Panel</h2></td>

            </tr>

            <tr>

              <td>User Name</td>

              <td>&nbsp;

                <input type="text" name="siteusername" id="siteusername" data-required="true" />

                <br />

                <span class="input-text">Username for your site admin</span></td>

            </tr>

            <tr>

              <td>Password</td>

              <td>&nbsp;

                <input type="text" name="sitepassword" id="sitepassword" data-required="true" data-minlength="6" data-minlength-message="Password should have at least 6 characters."/>

                <br />

                <span class="input-text">Password for your site admin</span></td>

            </tr>

            <tr>

              <td>&nbsp;</td>

              <td>&nbsp;

                <button class="button-blue" name="button">Install</button></td>

            </tr>

          </table>

        </form>

      </div>

    </div>

  </div>

</div>

<?php }?>
	<script src="../web_school/js/jquery.min.js"></script>
	<!-- jquery cookie -->
			<script src="../web_school/js/jquery_cookie.min.js"></script>
	<script src="../web_school/js/lib/parsley/parsley.min.js"></script>
	<script>
		$(function() {
			
			//* validation
			$('#installation').parsley({
				errors: {
					classHandler: function ( elem, isRadioOrCheckbox ) {
						if(isRadioOrCheckbox) {
							return $(elem).closest('.form_sep');
						}
					},
					container: function (element, isRadioOrCheckbox) {
						if(isRadioOrCheckbox) {
							return element.closest('.form_sep');
						}
					}
				}
			});});
			</script>
</body>

</html>