<?php
/**
  V3.50 7 April 2003  (c) 2000-2003 John Lim (jlim@natsoft.com.my). All rights reserved.
  Released under both BSD license and Lesser GPL library license. 
  Whenever there is any discrepancy between the two licenses, 
  the BSD license will take precedence.
	
  Set tabs to 4 for best viewing.
 
	config.inc.php:       Configuration settings
	steps.inc.php:        Code for specific steps in the install process
	installer.inc.php:    Base class for all installations.
	installer-*.inc.php:  Language files
	
*/



class Lens_Wizard_Base {

	var $noNext = false; // if true, disable the NEXT button
	
	function undomq($m) 
	{
		if (get_magic_quotes_gpc()) {
			// undo the damage
			$m = str_replace('\\\\','\\',$m);
			$m = str_replace('\"','"',$m);
			$m = str_replace('\\\'','\'',$m);
		}
		return $m;
	}

	function Title()
	{
		global $LENS_PROD;
		echo "<h1>$LENS_PROD</h1>";
	}
	
	function RootPath()
	{
	GLOBAL $HTTP_SERVER_VARS;
	
		if (isset($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) { // APACHE and PWS
			$_p_file = (str_replace('\\\\','/',$HTTP_SERVER_VARS['DOCUMENT_ROOT'])); // compat for php4.02 on windows
			return (str_replace('\\','/',$_p_file));
		}
		if (isset($HTTP_SERVER_VARS['APPL_PHYSICAL_PATH'])) { // IIS
			$_p_file = (str_replace('\\\\','/',$HTTP_SERVER_VARS['APPL_PHYSICAL_PATH'])); // compat for php4.02 on windows
			return (str_replace('\\','/',$_p_file));
		}
		global $PHP_SELF; // iis cgi
	 	$p = @$HTTP_SERVER_VARS['PATH_TRANSLATED'];
		if ($p) {
			$p = dirname(str_replace('\\\\','\\',$p));
			return  substr($p,0,strlen($p) - strlen(dirname($PHP_SELF)));
		}
				
		return '';
	} //function

		
	function Error($s)
	{
		echo "<table align=center border=1><tr><td><font color=darkred>$s</font></table>";
	}

	// 0 = start, 1 = normal (next/prev), 2 = next
	function Start(&$wz,$action='index.php') 
	{
		$this->Title();
		
		$step = &$wz['STEP'];
		if (isset($_REQUEST['next'])) $step = $this->Next($step,$wz);
		if (isset($_REQUEST['prev'])) $step = $this->Prev($step,$wz);
		echo "<form method=post action=$action name=lens_wiz>";
	}

	function End($wz) 
	{
	
		$step = $wz['STEP'];
	
		echo "<p>";
		if ($step > 0) {
			echo "<input type=submit name=prev value=\"{$wz['Prev']}\">";
		}
		if (!$this->noNext && $step < $wz['LASTSTEP']) {
			echo "<input type=submit name=next value=\"{$wz['Next']}\"";
		}
		echo "</form>";
		
		echo "<p><font size=1 color=#CCCCCC>Step: $step</font>";
		
		$this->Footer();
	}	
	
	function Footer()
	{
	}
	
	function Next($step,&$wz)
	{
	global $HTTP_POST_VARS, $HTTP_GET_VARS;
	
		foreach($HTTP_POST_VARS as $k => $v) {
			$REQUEST[$k] = $this->undomq($v);
		}
		foreach($HTTP_GET_VARS as $k => $v) {
			$REQUEST[$k] = $this->undomq($v);
		}
		
		return $this->_Next($step,$wz,$REQUEST);
	}
	
	function Prev($step,&$wz)
	{
		$prev = $step;
		
		switch($step) {
			case 0: break;
			default:
				$prev = $step - 1;
				break;
		}
		return $prev;
	}
	
	
	function Render(&$wz)
	{
		$this->Start($wz);
		
		if (empty($wz['STEP'])) {
			$this->TestVersion($wz);
			$this->TestZendOptimizer($wz);
			$this->TestMagicQuotes($wz);
		}
		if (isset($wz[$wz['STEP']])) echo $wz[$wz['STEP']];
		$fn = 'Render'.$wz['STEP'];
		$this->$fn($wz);
		$this->End($wz); 
		
	}			
	
	function ConfigText($file, $arr)
	{
		foreach($arr as $k => $v) {
			$src[] = '{'.$k.'}';
			$dst[] = $v;
		}
		$f = fopen($file,'r');
		if ($f) {
			$text = fread($f,199999);
			fclose($f);
		} else echo "<p>Missing $file</p>";
		
		return adodb_str_replace($src,$dst,$text);
	}
	
	function TestVersion($wz)
	{
	global $LENS_MIN_VERSION;
	
		//print "$LENS_MIN_VERSION ". PHP_VERSION;
		if (empty($LENS_MIN_VERSION)) return true;
		if (strnatcmp(PHP_VERSION, $LENS_MIN_VERSION)>=0) return true;
		
		$this->Error(sprintf($wz['ERR_VERSION'],$LENS_MIN_VERSION,PHP_VERSION));
		return false;
		
	}
	
	function TestZendOptimizer($wz)
	{
	global $LENS_TEST_ZEND_OPTIMIZER,$TEST_ZO;
	
	if (!$LENS_TEST_ZEND_OPTIMIZER) return true;
	ob_start();
		$TEST_ZO = false;
		include_once(dirname(__FILE__).'/testzo.enc.php');
	ob_end_clean();
		if ($TEST_ZO) return true;
		$this->noNext = true;
		$this->Error($wz['ERR_ZO']);
		return false;
	}
	
	function TestMagicQuotes($wz)
	{
	global $LENS_MAGIC_QUOTES_OFF;
	
		if (!$LENS_MAGIC_QUOTES_OFF) return true;
		if (get_magic_quotes_gpc()) {
			 $this->Error($wz['ERR_MAGIC_Q_OFF']);
			 $this->noNext = true;
			return false;
		}
		
		if (get_magic_quotes_runtime()) {
		 	echo $wz['ERR_MAGIC_Q_RUNTIME_OFF'];
			$this->noNext = true;
			return false;
		}
		return true;
	}
};
?>