<?php

/**-----------------------------------------------
*
* Rooty, 2018 <rooty@rooty.me>
*
*
* This software is protected by copyright, please
* read the file COPYRIGHT.
* This program is distributed in the hope that it
* will be useful, but without any warranty; without
* even the implied warranty of merchantability or
* fitness for a particular purpose. Please
* read the file LICENCE.
*
* Ce fichier contient la gestion de l'installation qware
*
* @author Initiance <www.initiance.com|martial@initiance.com>
* @copyright Copyright &copy; 2009-2018, Rooty
* @since 2004/06/07
* @version 0.3
* @link www.rooty.me
* @package install
* @docreview Tristan <tristan@initiance.com> | 30-10-2005
*/

include_once($PATH_INSTALL."lib_install.php");

/**
* Fonction d'exécution du processus passé en paramètre
*
* @author Initiance <www.initiance.com|martial@initiance.com>
*/
function main($step)
{
	global $message, $on, $off, $stepimg, $etat, $CONF_LIB_PATH;
	$chaineprocess = "<div id='infoetape'><h2 class='title'>".$message->getMessage("StepLabel")."</h2><ul class='etape'>";
	$chaineentete = "<h1 class='installtitre'>".$message->getMessage("TextInfo")."</h1>";
	
	$chaine = "";
	
	if (defined('QWARE_INSTALLED') && $step!=4)
	{
		$tableauprincipal = $chaineentete."<div class='main'>".$message->getMessage("installeffected")."<a href=\"../index.php\" class=\"installeffected\">".$message->getMessage("Clickhere")."</a></div>";
		return $tableauprincipal;
	}

	switch($step)
	{
		case 0 :
			$bool=true;
			$chainecontrol ="<table>";
			if (version_compare(phpversion(),'7.2','>=')==1)
			{
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("phpcorrect")."</td><td class='vgood'>".phpversion()."</td></tr>";
			} else {
				$bool=false;
				$chainecontrol .= "<tr><td class='alert'>".$message->getMessage("phpincorrect")."</td><td>".phpversion()."< 4.1</td></tr>";
			}

			if (in_array("mysql", PDO::getAvailableDrivers(), TRUE))
			{
				$mysqlversion = explode(" ", getModuleSetting('pdo_mysql','Client API version'));
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("mysqlon")."</td><td class='vgood'>".$mysqlversion[1]."</td></tr>";
			} else {
				$bool=false;
				$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("mysqloff")."</td></tr>";
			}

			if(in_array("pgsql", PDO::getAvailableDrivers(), TRUE))
			{
				$pgsqlversion = explode(" ", getModuleSetting('pdo_pgsql','PostgreSQL(libpq) Version'));
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("pgsqlon")."</td><td class='vgood'>".$pgsqlversion[0]."</td></tr>";
			} else {
				$bool=true;
				$chainecontrol .= "<tr><td colspan='2' class='vopt'>".$message->getMessage("pgsqloff")."</td></tr>";
			}

			if(getModuleSetting('ldap','LDAP Support'))
			{
				$ldapversion = getModuleSetting('ldap','API Version');
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("ldapon")."</td><td class='vgood'>".$ldapversion."</td></tr>";
			} else {
				$bool=true;
				$chainecontrol .= "<tr><td colspan='2' class='vopt'>".$message->getMessage("ldapoff")."</td></tr>";
			}

			if(getModuleSetting('odbc','ODBC Support'))
			{
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("odbcon")."</td><td class='vgood'>OK</td></tr>";
			} else {
				$bool=true;
				$chainecontrol .= "<tr><td colspan='2' class='vopt'>".$message->getMessage("odbcoff")."</td></tr>";
			}

			if(getModuleSetting('hash','hash support'))
			{
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("hashon")."</td><td class='vgood'>OK</td></tr>";
			} else {
				$bool=false;
				$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("hashoff")."</td></tr>";
			}
/*
			if(getModuleSetting('mcrypt','Version'))
			{
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("mcrypton")."</td><td class='vgood'>OK</td></tr>";
			} else {
				$bool=false;
				$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("mcryptoff")."</td></tr>";
			}*/

			if(getModuleSetting('ftp','FTP support'))
			{
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("ftpon")."</td><td class='vgood'>OK</td></tr>";
			} else {
				$bool=true;
				$chainecontrol .= "<tr><td colspan='2' class='vopt'>".$message->getMessage("ftpoff")."</td></tr>";
			}

			clearstatcache();

			if((!is_writable($CONF_LIB_PATH.'config')) || (!is_writable($CONF_LIB_PATH.'data')) || (!is_writable($CONF_LIB_PATH.'lib')) || (!is_writable($CONF_LIB_PATH.'log')))
			{
				$bool=false;
				$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("writeerrorreps");
				if(!is_writable($CONF_LIB_PATH.'config')) { $chainecontrol .= " config/"; }
				if(!is_writable($CONF_LIB_PATH.'data')) { $chainecontrol .= " data/"; }
				if(!is_writable($CONF_LIB_PATH.'lib')) { $chainecontrol .= " lib/"; }
				if(!is_writable($CONF_LIB_PATH.'log')) { $chainecontrol .= " log/"; }
				$chainecontrol .= $message->getMessage("writeerrorrepsend")."<td></tr>";
			} else {
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("writeonreps")."</td><td class='vgood'>OK</td></tr>";
			}

// 			if(!is_writable($CONF_LIB_PATH.'config'))
// 			{
// 				// Contrôle des droits d'écriture sur les fichiers de configuration
// // 				if(!is_writable($CONF_LIB_PATH.'config')
// // 				{
// 					$bool=false;
// 					$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("writeerrorconf")."</td></tr>";
// 				} else {
// 					$chainecontrol .= "<tr><td class='good'>".$message->getMessage("writeonconf")."</td><td class='vgood'>OK</td></tr>";
// // 				}
// 			}

			// Contrôle des droits d'écriture sur les répertoires data/ et lib/ et affichage d'un message d'erreur spécifique
			if(is_writable($CONF_LIB_PATH.'data'))
			{
				//$chainecontrol .= "<tr><td class='good'>".$message->getMessage("writeonrepdata")."</td><td class='vgood'>OK</td></tr>";
			}

			if(is_writable($CONF_LIB_PATH.'lib'))
			{
				//$chainecontrol .= "<tr><td class='good'>".$message->getMessage("writeonreplib")."</td><td class='vgood'>OK</td></tr>";
			}
	
			if (is_writable($CONF_LIB_PATH.'log'))
			{
				// Contrôle de l'existence des fichiers logs. Dans le cas échéant = création.
				if(!file_exists($CONF_LIB_PATH.'log/auth.alien.log'))
				{
					$bool=false;
					if (($isFileCreated = @fopen($CONF_LIB_PATH."log/auth.alien.log","w")) == true)
					{
						$bool=true;
						$boollog=true;
						//$chainecontrol .= "<tr><td class='good'>".$message->getMessage("createlogfile")."auth.alien.log</td><td class='vgood'>OK</td></tr>";
					} else {
						$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("createerrorlogauthalien")."</td></tr>";
					}
				} else { $boollog=true; }
				if(!file_exists($CONF_LIB_PATH.'log/auth.log'))
				{
					$bool=false;
					if (($isFileCreated = @fopen($CONF_LIB_PATH."log/auth.log","w")) == true)
					{
						$bool=true;
						$boollog=true;
						//$chainecontrol .= "<tr><td class='good'>".$message->getMessage("createlogfile")."auth.log</td><td class='vgood'>OK</td></tr>";
					} else {
						$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("createerrorlogauth")."</td></tr>";
					}
				} else { $boollog=true; }
				if(!file_exists($CONF_LIB_PATH.'log/error.log'))
				{
					$bool=false;
					if (($isFileCreated = @fopen($CONF_LIB_PATH."log/error.log","w")) == true)
					{
						$bool=true;
						$boollog=true;
						//$chainecontrol .= "<tr><td class='good'>".$message->getMessage("createlogfile")."error.log</td><td class='vgood'>OK</td></tr>";
					} else {
						$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("createerrorlogerror")."</td></tr>";
					}
				} else { $boollog=true; }
				if(!file_exists($CONF_LIB_PATH.'log/message.log'))
				{
					$bool=false;
					if (($isFileCreated = @fopen($CONF_LIB_PATH."log/message.log","w")) == true)
					{
						$bool=true;
						$boollog=true;
						//$chainecontrol .= "<tr><td class='good'>".$message->getMessage("createlogfile")."message.log</td><td class='vgood'>OK</td></tr>";
					} else {
						$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("createerrorlogmessage")."</td></tr>";
					}
				} else { $boollog=true; }
				if(!file_exists($CONF_LIB_PATH.'log/template.log'))
				{
					$bool=false;
					if (($isFileCreated = @fopen($CONF_LIB_PATH."log/template.log","w")) == true)
					{
						$bool=true;
						$boollog=true;
						//$chainecontrol .= "<tr><td class='good'>".$message->getMessage("createlogfile")."template.log</td><td class='vgood'>OK</td></tr>";
					} else {
						$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("createerrorlogtemplate")."</td></tr>";
					}
				} else { $boollog=true; }

				if($boollog == true)
				{
					$chainecontrol .= "<tr><td class='good'>".$message->getMessage("createlogfiles")."</td><td class='vgood'>OK</td></tr>";
				} else {
					$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("writeerrorlogs")."</td></tr>";
				}
			}

			// Contrôle des droits d'écriture sur les fichiers de logs et affichage d'un message d'erreur spécifique
			if(is_writable($CONF_LIB_PATH.'log/auth.alien.log') || is_writable($CONF_LIB_PATH.'log/auth.log') || is_writable($CONF_LIB_PATH.'log/error.log') || is_writable($CONF_LIB_PATH.'log/message.log') || is_writable($CONF_LIB_PATH.'log/template.log'))
			{
				$chainecontrol .= "<tr><td class='good'>".$message->getMessage("writeonlogs")."</td><td class='vgood'>OK</td></tr>";
			}

			// Contrôle de l'inexistence du fichier de cache (data/upload/install/cache/cache.dat) et affichage d'un message d'erreur spécifique
			if(file_exists($CONF_LIB_PATH.'data/upload/install/cache/cache.dat'))
			{
				$bool=false;
				$chainecontrol .= "<tr><td colspan='2' class='alert'>".$message->getMessage("cacheerror")."</td></tr>";
			}

			$chaine .= "<div id='verif'><h3 class='title'>".$message->getMessage("Step".$step)."</h3>";
			$chaine .=  "<div class='main'><form id='myform' action='index.php?step=1' method='post' >".$chainecontrol."</table>";

			if($bool)
			{
				$chaine .= "<p><input type=\"submit\" name=\"verif\" value=\"".$message->getMessage("Next")."\" class=\"button\"/></p>";
			}

			$chaine .= "</form></div>";
			$chaineprocess .= stepprocess($step,$etat);
			$chaineprocess .= stepprocess($step+1,0);
			$chaineprocess .= stepprocess($step+2,0);
			$chaineprocess .= stepprocess($step+3,0);
			break;

		case 1 :
			$chaine .= "<div id='verif'><h3 class='title'>".$message->getMessage("Step".$step)."</h3>";
		
			if(isset($_POST['verif']))
			{
				$chaine .= datasource();
			}
			else
			{
				$chaine .= datasource($_POST);
			}
			$chaineprocess .= stepprocess($step-1,1);
			$chaineprocess .= stepprocess($step,$etat);
			$chaineprocess .= stepprocess($step+1,0);
			$chaineprocess .= stepprocess($step+2,0);
			break;

		case 2 :
			$chaine .= "<div id='verif'><h3 class='title'>".$message->getMessage("Adapt")."</h3>";

			if(!$_POST)
			{
				$chaine .= adapt();
			}
			else
			{
				$chaine .= adapt($_POST);
			}
			$chaineprocess .= stepprocess($step-2,1);
			$chaineprocess .= stepprocess($step-1,1);
			$chaineprocess .= stepprocess($step,$etat);
			$chaineprocess .= stepprocess($step+1,0);
			break;

		case 3 :
			$chaine .= "<div id='verif'><h3 class='title'>".$message->getMessage("Rootuser")."</h3>";

			if(!$_POST)
			{
				$chaine .= admin();
			}
			else
			{
				$chaine .= admin($_POST);
			}
			$chaineprocess .= stepprocess($step-3,1);
			$chaineprocess .= stepprocess($step-2,1);
			$chaineprocess .= stepprocess($step-1,1);
			$chaineprocess .= stepprocess($step,0);
			break;

		default :
			$chaine .="<div id='verif'><h3 class='title'>".$message->getMessage("textinstalled")."</h3>";
			$chaine .= "<div class='main'><a href='../index.php' class='end'>".$message->getMessage("Clickhere")."</a></div>";
			$chaineprocess .= stepprocess($step-4,1);
			$chaineprocess .= stepprocess($step-3,1);
			$chaineprocess .= stepprocess($step-2,1);
			$chaineprocess .= stepprocess($step-1,1);
			$chaineprocess .= stepprocess($step,1);
			break;
	}
	$chaine .="</div>";
	$chaineprocess .="</ul></div>";
	$tableauprincipal =$chaineentete.$chaineprocess.$chaine;

	return $tableauprincipal;
}
?>