<?php
/*

	File     : install.php
	Version  : 0.2.1 Beta
	Copyright: Attila Soki 2001
	e-mail   : atiware@gmx.net
	Date     : 2001-05-15
*/

	//this script tries to create the db and tables as specified in constants.inc.php,
	//and creates a new admin account (and with this operation tests the mysql user and
	//password settings in constants.inc.php)
	//!! this script does not drop databases or tables !!
	//!! if database or table exists then occurs no error in script,
	//and your tables stays unchanged.
	//(will create only a new admin account if your admins table is empty)

	//!! this script runs only if no admininstrator saved in admins table

	include_once("MySql.class.php");
	include_once("Admin.class.php");

	$error=0;
	$saved=false;
	$aerrors=null;
	$aerrorcount=0;
	if (DEMOMODE) {
		$aerrors[$aerrorcount++]="Please edit your 'config' file and set DEMOMODE to false!";
	}
	if (strlen(MYSQL_HOST)==0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set MYSQL_HOST!";
	}
	if (strlen(MYSQL_PORT)==0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set MYSQL_PORT!";
	}
	if (strlen(MYSQL_USER)==0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set MYSQL_USER!";
	}
	if (strlen(MYSQL_PASSWORD)==0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set MYSQL_PASSWORD!";
	}
	if (strlen(MYSQL_DB)==0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set MYSQL_DB!";
	}
	if (strlen(MMCADMINSTABLE)==0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set MMCADMINSTABLE!";
	}
	if (strlen(MMCRIGHTSTABLE)==0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set MMCRIGHTSTABLE!";
	}
	if (strlen(MMCDOMAINTABLE)==0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set MMCDOMAINTABLE!";
	}
	if (PAGESIZE<=0) {
		$aerrors[$aerrorcount++]="Please edit constants.inc.php and set PAGESIZE!";
	}
	
	if (!$aerrorcount) {
		$dbo=new MySql();
		$dbo->query("select * from ".MMCADMINSTABLE);
		if ($dbo->numRows()) {
			$aerrors[$aerrorcount++]="Sorry. You can't run this script, because in your 'MMCADMINSTABLE' table is already one or more admin accounts.";
			$aerrors[$aerrorcount++]="Delete all admins or drop your 'MMCADMINSTABLE' table to continue.";
			$aerrors[$aerrorcount++]="For your 'MMCADMINSTABLE' proper name see your settings in 'config' file,";
			$aerrors[$aerrorcount++]="or for more info read the readme...";
			$dbo->freeResult();
		}
		$dbo->close();


		if (strlen($installButton)>0) {
			if (strlen($username)==0)
				$error|=1;
			if (strlen($pwd1)>0) {
				if (strcmp($pwd1,$pwd2)!=0)
					$error|=2;
			}
			else
				$error|=2;

			if (strlen($email)==0) {
				$error|=4;
			}
			if (strlen($myuser)==0) {
				$error|=8;
			}
			if (strlen($mypwd)==0) {
				$error|=16;
			}
			if (!$error) {
				//create adminrights
				$qar="CREATE TABLE ".MYSQL_DB.".".MMCRIGHTSTABLE." (";
				$qar.="Id int(11) NOT NULL auto_increment,";
				$qar.="AdminId int(11) NOT NULL default '0',";
				$qar.="DomainId int(11) NOT NULL default '0',";
				$qar.="PRIMARY KEY (Id),";
				$qar.="KEY AdminId(AdminId),";
				$qar.="KEY DomainId(DomainId))";

				//create admins
				$qa="CREATE TABLE ".MYSQL_DB.".".MMCADMINSTABLE." (";
				$qa.="Id int(11) NOT NULL auto_increment,";
				$qa.="Username varchar(64) NOT NULL default '',";
				$qa.="Pwd varchar(16) NOT NULL default '',";
				$qa.="Email varchar(255) NOT NULL default '',";
				$qa.="Root smallint(6) NOT NULL default '0',";
				$qa.="PRIMARY KEY (Id),";
				$qa.="KEY Username(Username))";

				//create domains
				$qd="CREATE TABLE ".MYSQL_DB.".".MMCDOMAINTABLE." (";
				$qd.="Id int(11) NOT NULL auto_increment,";
				$qd.="Domain varchar(255) NOT NULL default '',";
				$qd.="MServerIp varchar(255) NOT NULL default '',";
				$qd.="MServerPort int(11) NOT NULL default '0',";
				$qd.="MServerDb varchar(255) NOT NULL default '',";
				$qd.="MServerUser varchar(64) NOT NULL default '',";
				$qd.="MServerPwd varchar(64) NOT NULL default '',";
				$qd.="MailTable varchar(255) NOT NULL default '',";
				$qd.="FieldUser varchar(127) NOT NULL default '',";
				$qd.="FieldDomain varchar(127) NOT NULL default '',";
				$qd.="FieldAlias varchar(127) NOT NULL default '',";
				$qd.="FieldIsAlias varchar(127) NOT NULL default '',";
				$qd.="FieldPassword varchar(127) NOT NULL default '',";
				$qd.="PwdCrypt smallint(6) NOT NULL default '0',";
				$qd.="PRIMARY KEY (Id),";
				$qd.="KEY Domain(Domain))";

				$dbo=new MySql(false); //dont connect
				$dbo->setUser(trim($myuser));
				$dbo->setPassword(trim($mypwd));
				$dbo->connect();
				$dbo->query("select 1+1 as TWO");
				if (!$dbo->numRows()) {
					$aerrors[$aerrorcount++]="Can't connect to database. Check MySql username and password fields,";
					$aerrors[$aerrorcount++]="and check MYSQL_HOST and MYSQL_PORT settings in constants.inc.php";
					$error|=8;
					$error|=16;
				}
				if (!$error) {
					$dbo->query("create database ".MYSQL_DB);
					$dbo->query($qar);
					$dbo->query($qa);
					$dbo->query($qd);

					//some check
					$dbo->query("explain ".MYSQL_DB.".".MMCRIGHTSTABLE);
					if (!$dbo->numRows()) {
						$aerrors[$aerrorcount++]="Error creating ".MYSQL_DB.".".MMCRIGHTSTABLE;
					}

					$dbo->query("explain ".MYSQL_DB.".".MMCADMINSTABLE);
					if (!$dbo->numRows()) {
						$aerrors[$aerrorcount++]="Error creating ".MYSQL_DB.".".MMCADMINSTABLE;
					}

					$dbo->query("explain ".MYSQL_DB.".".MMCDOMAINTABLE);
					if (!$dbo->numRows()) {
						$aerrors[$aerrorcount++]="Error creating ".MYSQL_DB.".".MMCDOMAINTABLE;
					}
					$dbo->close();
				}

				if (!$aerrorcount) {
					$admin=new Admin();
					$admin->create(trim($username),trim($pwd1),trim($email),1,null);
					$saved=$admin->isRoot();
					if (!$saved) {
						$aerrors[$aerrorcount++]="Error creating the new admin account.";
						$aerrors[$aerrorcount++]="Check MYSQL_USER and MYSQL_PASSWORD settings in constants.inc.php";
					}
				}
			}
		}
	}
	require("head.inc.php");
?>
  <tr> 
    <td colspan="2" align="left" valign="top"> 
      <form method="post" action="install.php" name="form1" ENCTYPE="multipart/form-data">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
	if (((!$aerrorcount) && (!$saved)) || (($installButton) && ($error))) {
?>
          <tr> 
            <td colspan="2" bgcolor="#0099CC"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Install 
                databases and create first admin</b></font></div>
            </td>
          </tr>
<?php
		if ((int)$SERVER_PORT==80) { //http port (not https port)
?>
          <tr> 
            <td colspan="2"> 
              <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#CC0000"><b>
                Not error, WARNING only. You are using an unsecure (non SSL) connection!
            </td>
          </tr>
<?php
	}
?>
          <tr<?php echo ($error&1) ? ' bgcolor="'.ERRORCOLOR.'"':"" ?>> 
            <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">New 
              admin username:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="username" size="40" maxlength="64" value="<?php echo $username ?>">
              </font></td>
          </tr>
          <tr<?php echo ($error&2) ? ' bgcolor="'.ERRORCOLOR.'"':' bgcolor="#F2F2E0"' ?>> 
            <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Admin 
              password:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="password" name="pwd1" size="40" maxlength="255" value="<?php echo $pwd1 ?>">
              </font></td>
          </tr>
          <tr<?php echo ($error&2) ? ' bgcolor="'.ERRORCOLOR.'"':"" ?>> 
            <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Retype 
              password:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="password" name="pwd2" size="40" maxlength="255" value="<?php echo $pwd2 ?>">
              </font></td>
          </tr>
          <tr<?php echo ($error&4) ? ' bgcolor="'.ERRORCOLOR.'"':' bgcolor="#F2F2E0"' ?>> 
            <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your e-mail
              address:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="text" name="email" size="40" maxlength="255" value="<?php echo $email ?>">
              </font></td>
          </tr>
          <tr<?php echo ($error&8) ? ' bgcolor="'.ERRORCOLOR.'"':"" ?>> 
            <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">MySql username to create db and 
              tables:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="username" name="myuser" size="40" maxlength="255" value="<?php echo $myuser ?>">
              </font></td>
          </tr>
          <tr<?php echo ($error&16) ? ' bgcolor="'.ERRORCOLOR.'"':' bgcolor="#F2F2E0"' ?>> 
            <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">MySql password to create db and 
              tables:</font></td>
            <td> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
              <input type="password" name="mypwd" size="40" maxlength="255" value="<?php echo $mypwd ?>">
              </font></td>
          </tr>
          <tr> 
            <td colspan="2" bgcolor="#FFFFCC"> 
              <div align="center"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> 
                <input type="submit" name="installButton" value="Install">
                </font></div>
            </td>
          </tr>
<?php
	}
	if ($aerrorcount) {
?>
          <tr> 
            <td colspan="2" bgcolor="<?php echo ERRORCOLOR ?>"> 
              <div align="center"> <font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b>You have one or more errors!</b>
                </font></div>
            </td>
          </tr>
<?php
		for ($i=0;$i<$aerrorcount;$i++) {
?>
          <tr> 
            <td colspan="2" bgcolor="<?php echo ERRORCOLOR ?>"> 
              <div align="center"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo $aerrors[$i] ?>
                </font></div>
            </td>
          </tr>
<?php
		}
	}
	if ($saved) {
?>
          <tr> 
            <td colspan="2" bgcolor="<?php echo ERRORCOLOR ?>"> 
              <div align="center"> <font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b>Successfully installed. Please log in with your admin account now.</b>
                </font></div>
            </td>
          </tr>
          <tr> 
            <td colspan="2" bgcolor="<?php echo ERRORCOLOR ?>"> 
              <div align="center"> <font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b><a href="index.php">Click here to login.</a></b>
                </font></div>
            </td>
          </tr>
<?php
	}
?>
        </table>
      </form>
    </td>
  </tr>
<?php
	require("foot.inc.php");
?>