<?php
/* $Id: step_2.inc.php,v 1.19 2006/04/27 11:42:55 k-fish Exp $ */
if(!defined("SETUP_RUNNING")) die('Use index.php for running setup!');

/***************************************************************************
* what to check
***************************************************************************/

// just the minimum version needed
$neededPHPVersion = '4.2.0'; // jpgraph says it needs at least 4.3.1, but...

$neededMemory = 16; //memory in Mb

// list all required extensions with their PHP name
// 0-must, 1-optional
$neededExtensions = array('xml' => 0,
'gd' => 0,
'pcre' => 0,
'session' => 0,
//'curl' => 1 //was needed only for check rooturl at install : now not needed
);

$dbDrivers = array(	'mysql' => array('mysql', 'MySQL'),
		       		'pgsql' => array('postgres7', 'PostgreSQL')
		       	);
    // at least for these we need the name of the PHP extensions
    //'' => array('', 'IBM DB2'), // loge, go for it!
    //'' => array('', 'Generic ODBC'),
    //'' => array('mssql', 'MS-SQL'),
    //'' => array('oci8', 'Oracle 8'),
    //'' => array('msql', 'mSQL'),
    //'' => array('ibase', 'interbase'),
    //'' => array('sybase', 'Sybase'),
    //'' => array('fbsql', 'Frontbase')


// the first number in the array is the value we expect,
// the second is the type of check 0-must, 1-should, 2-info
$neededSettings = array('session.auto_start' => array(0,0),
'safe_mode' => array(0,1),
'magic_quotes_runtime' => array(0,0),
'file_uploads' => array(1,1),
'display_errors' => array(0,1),
'allow_call_time_pass_reference' => array(1,1),
'register_globals' => array(0,2),
'magic_quotes_gpc' => array(0,2)
);

/***************************************************************************
***************************************************************************/

/* save posted data */
if (isset($_POST["lang_charset"])) {
  $lc = explode(";", $_POST["lang_charset"]);
  $_SESSION["SETUP"]["lang"] = $lc[0];
  $_SESSION["SETUP"]["charset"] = $lc[1];
}

$_SESSION["SETUP"]["system"] = true;

// uninstall?
if($_SESSION["SETUP"]['uninstall'] === true){
  $smarty->assign('uninstall', true);
} // end uninstall
else{
  $smarty->assign('uninstall', false);
  $report = array();

  // check PHP version
  if (PHP_VERSION < $neededPHPVersion){
    $_SESSION["SETUP"]["system"] = false;
    $report[] = array('status' => 'error',
    'title' => Lang::getParmLang(Lang::getLanguageString('phpreq'), array($neededPHPVersion)),
    'desc' => Lang::getParmLang(Lang::getLanguageString('phpreqdesc'), array($neededPHPVersion)));
  }
  else{
    $report[] = array('status' => 'ok',
    'title' => Lang::getParmLang(Lang::getLanguageString('phpreq'), array($neededPHPVersion)),
    'desc' => '');
  }

  /* we must be able to write the configfile */
  clearstatcache();
  if(!is_writeable(ROOTPATH)){
    $_SESSION["SETUP"]["system"] = false;
    $report[] = array('status' => 'error',
    'title' => Lang::getParmLang(Lang::getLanguageString('writereq'), array(ROOTPATH)),
    'desc' => Lang::getParmLang(Lang::getLanguageString('writereqdesc'), array(ROOTPATH)));
  }
  else{
    $report[] = array('status' => 'ok',
    'title' => Lang::getParmLang(Lang::getLanguageString('writereq'), array(ROOTPATH)),
    'desc' => '');
  }

  // check needed PHP extensions
  $extensions = get_loaded_extensions();

//databases drivers
  $str_db_needed="";
  $icpt = 0;
  $dbs = array();
  foreach($dbDrivers as $dbext => $dbd){
  	if ($icpt>0) {
  		$str_db_needed.=", ";
  	}
  	$icpt++;
  	$str_db_needed .=$dbd[1];
	if(in_array($dbext, $extensions)){
	    $dbs[$dbd[0]] = $dbd[1];
	}
  }//foreach
  if (count($dbs)==0) {

	$_SESSION["SETUP"]["system"] = false;
    $report[] = array(	'status' => 'error',
				    	'title' => Lang::getLanguageString('dbneeded'),
				    	'desc' => Lang::getLanguageString('dbneeded_msg')." : ".$str_db_needed
					);
  }
  else{
  	$report[] = array('status' => 'ok',
				      'title' => Lang::getLanguageString('dbneeded'),
				      'desc' => ''
					  );
  }
//-----------

  foreach($neededExtensions as $ext => $v){
    if(!in_array($ext, $extensions)){
      if($v == 0){
        $_SESSION["SETUP"]["system"] = false;
        $report[] = array('status' => 'error',
        'title' => Lang::getParmLang(Lang::getLanguageString('extreq0'), array($ext)),
        'desc' => Lang::getParmLang(Lang::getLanguageString('extreqdesc0'), array($ext)));
      }
      else{
        $report[] = array('status' => 'note',
        'title' => Lang::getParmLang(Lang::getLanguageString('extreq1'), array($ext)),
        'desc' => Lang::getParmLang(Lang::getLanguageString('extreqdesc1'), array($ext)));
      }
    }
    else{
      $report[] = array('status' => 'ok',
      'title' => Lang::getParmLang(Lang::getLanguageString('extreq'), array($ext)),
      'desc' => '');
    }
  }

  // check include_path
  $incPaths = explode((PHP_OS == 'WIN32' || PHP_OS == 'WINNT') ? ";" : ":", ini_get("include_path"));
  if (! (in_array(".",$incPaths) || in_array("./",$incPaths) || in_array(".\\",$incPaths))){
    $_SESSION["SETUP"]["system"] = false;
    $report[] = array('status' => 'error',
    'title' => Lang::getLanguageString('incpathreq'),
    'desc' => Lang::getLanguageString('incpathreqdesc'));
  }
  else{
    $report[] = array('status' => 'ok',
    'title' => Lang::getLanguageString('incpathreq'),
    'desc' => '');
  }

  // check memory_limit
  $mlimit = ini_get('memory_limit');
  if (stristr($mlimit,"m"))        {
    $mlimit=doubleval($mlimit)*1024*1024;
  } elseif (stristr($mlimit,"k"))  {
    $mlimit=doubleval($mlimit)*1024;
  }
  if ($mlimit&&$mlimit<$neededMemory*1024*1024) {
    $_SESSION["SETUP"]["system"] = false;
    $report[] = array('status' => 'error',
    'title' => Lang::getParmLang(Lang::getLanguageString('memoryreq'), array($neededMemory)),
    'desc' => Lang::getParmLang(Lang::getLanguageString('memoryreqdesc'), array($neededMemory))
	);
  }
  else{
    $report[] = array('status' => 'ok',
    'title' => Lang::getParmLang(Lang::getLanguageString('memoryreq'), array($neededMemory)),
    'desc' => '');
  }

  // check PHP settings requirements
  foreach($neededSettings as $set => $v){
    if(!ini_get($set) == $v[0]){
      if($v[1] == 0){
        $_SESSION["SETUP"]["system"] = false;
        $report[] = array('status' => 'error',
        'title' => Lang::getParmLang(Lang::getLanguageString('setreq0'), array($set, $v[0])),
        'desc' => Lang::getParmLang(Lang::getLanguageString('setreqdesc0'), array($set, $v[0])));
      }
      elseif($v[1] == 1){
        $report[] = array('status' => 'warning',
        'title' => Lang::getParmLang(Lang::getLanguageString('setreq1'), array($set, $v[0])),
        'desc' => Lang::getParmLang(Lang::getLanguageString('setreqdesc1'), array($set, $v[0])));
      }
      elseif($v[1] == 2){
        $report[] = array('status' => 'note',
        'title' => Lang::getParmLang(Lang::getLanguageString('setreq2'), array($set, (int)ini_get($set))),
        'desc' => Lang::getParmLang(Lang::getLanguageString('setreqdesc2'), array($set, $v[0])));
      }
    }
    else{
      $report[] = array('status' => 'ok',
      'title' => Lang::getParmLang(Lang::getLanguageString('setreq2'), array($set, $v[0])),
      'desc' => '');
    }
  }

  // assign results to Smarty
  $smarty->assign('report', $report);
  $smarty->assign('status', $_SESSION["SETUP"]['system']);
} // end install

/* append navigation */
$nav = true;
$smarty->append("nav", array(0=>"prev",1=>Lang::getLanguageString("prev"),2=>1));
if($_SESSION["SETUP"]['uninstall'] !== true)
$smarty->append("subnav", array(0=>"refresh",1=>Lang::getLanguageString("refresh"),2=>1));
if ($_SESSION["SETUP"]["system"] === false)
$smarty->append("nav", array(0=>"next",1=>Lang::getLanguageString("next"),2=>0));
else
$smarty->append("nav", array(0=>"next",1=>Lang::getLanguageString("next"),2=>1));

?>