<?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
	
*/

function IsODBC($driver)
{
	switch($driver) {
	case 'db2':
	case 'access':
	case 'odbc':
		return true;
	}
	
	return false;
}

class Lens_Wizard extends Lens_Wizard_Base {

	function Title()
	{
		global $LENS_PROD;
		echo "<tr><td bgcolor=darkred><br><h1><font color=white> &nbsp; $LENS_PROD<white></h1><tr>
		<td><table cellpadding=8><tr><td>";
	}
	
	function Footer()
	{
		echo " &nbsp; <font size=1 color=#CCCCCC>(c)2003 Remistech(M) S/B</table></font>";
	}
	
	function _Next($step,&$wz,$request)
	{
		$next = $step;
		switch($step) {
			case 0: 
				$next = 1; 
				break;
			
			case 1:
				if (empty($request['driver'])) {
					$this->Error($wz['ERR_DRIVER']);
					break;
				}
				$wz['DRIVER'] = $request['driver'];
				$next = 2;
				break;
				
			case 2:
				$wz['HOST'] = $request['host'];
				$wz['USERID'] = $request['userid'];
				if ($request['pwd'] != $request['pwd2']) {
					$this->Error($wz['ERR_PWD']);
					break;
				}
				$wz['PWD'] = $request['pwd'];
			//	$wz['OLDDB'] = $request['olddb'];
				$next = 3;
				break;
				
			case 3:
				if (empty($request['db']) && !IsODBC($wz['DRIVER'])) {
					$this->Error($wz['ERR_SCHEMA']);
					break;
				}
				$wz['DB'] = @$request['db'];
				$next = 4;
				break;
				
			case 4:
				$next =5;
				break;
				
			default:
				$next += 1;
				if ($next > $wz['LASTSTEP']) $next = $wz['LASTSTEP'];
				
		}
		return $next;
	}
	
	function Render0(&$wz)
	{
		if (!$this->noNext) echo $wz['SetupDB'];
	}
	
	
	function Render1(&$wz)
	{
	global $LENS_DRIVERS;
	
		include_once('../license.php');
		$lic = new phplens_license();
		$lic->p;
		
		$s = $wz['DriverCaption']." <select name=driver><option>";
		foreach($LENS_DRIVERS as $key => $value) {
			
			if (@$wz['DRIVER'] == $key) $sel = ' selected';
			else $sel = '';
			$s .= "<option$sel value=$key>$value";
		}
		$s .= "</select>";
		
		echo $s;
	}
	
	function Render2(&$wz)
	{
	$driver = $wz['DRIVER'];
		
		$s = '<table border=1>';
		$s .= "<tr><td>{$wz['DriverCaption']}<td>{$wz['DRIVER']}";
		switch ($driver) {
		case 'oci8':
			 $s .= "<tr><td>{$wz['TNS']}<td><input type=text name=host size=20 value={$wz['HOST']}>";
			 break;
		case 'access':
		case 'db2':
		case 'odbc':
			$s .= "<tr><td>{$wz['DNSCaption']}<td><input type=text name=host size=20 value={$wz['HOST']}>"; 
			break;
		default:
			if (empty($wz['HOST'])) $wz['HOST'] = 'localhost';
			$s .= "<tr><td>{$wz['HostCaption']}<td><input type=text name=host size=20 value={$wz['HOST']}>";
			break;
		}
		$s .= "<tr><td>{$wz['UserCaption']}<td><input type=text name=userid size=24 value={$wz['USERID']}>";
		$s .= "<tr><td>{$wz['PwdCaption']}<td><input type=password name=pwd size=16 value={$wz['PWD']}>";
		$s .= "<tr><td>{$wz['Pwd2Caption']}<td><input type=password name=pwd2 size=16 value={$wz['PWD']}>";
		if ($wz['OLDDB'] == 1) {
			$c1 = 'checked';
			$c2 = '';
		} else {
			$c1 = '';
			$c2 = 'checked';
		}
		//$s .= "<tr><td>{$wz['ExistingDB']}<td><input type=radio $c1 name=olddb value=1>{$wz['Yes']} &nbsp; <input type=radio name=olddb $c2 value=0>{$wz['No']}";
		switch($wz['DRIVER']) {
		case 'oci8':
			break;
		case 'postgres':
			break;
		}
		$s .= '</table>';
		$s .= $wz['TRY_CONNECT'];
		echo $s;
	}
	
	function Render3(&$wz)
	{
		$s = '';
		$driver = $wz['DRIVER'];
		$db = NewADOConnection($driver);
		//$db->debug=1;
		switch ($driver) {
		case 'oci8':
			if ($wz['HOST'] == 'localhost') $wz['HOST'] = '';
			$ok = @$db->Connect(false,$wz['USERID'],$wz['PWD'],$wz['HOST']);
			break;
		default:
			$ok = @$db->Connect($wz['HOST'],$wz['USERID'],$wz['PWD']);
		}
		if (!$ok) {
			$this->Error($wz['ERR_CONNECT']);
			$this->noNext = true;
			return;
		} //else echo "<p>".($wz['OK_CONNECT'])."</p>";
		
		//$wz['OLDDB'] = 1;
		
		if (IsODBC($driver)) {
			$wz['OLDDB'] = true;
			echo $wz['ODBC_DB'];
		} else if ($wz['OLDDB']) {
			$arr = $db->MetaDatabases();
			//print_r($arr);
			if ($arr) {
				$s .= "<p>".$wz['OldDB']." <select name=db>";
				foreach($arr as $v) {
					$s .= '<option>'.($v);
				}
				$s .= '</select>';
			} else
				$s .= "<p>".$wz['OldDB']." <input type=text name=db value=\"{$wz['DB']}\">";
		} else {
			$s .= "<p>".$wz['NewDB']." <input type=text name=db value=\"{$wz['DB']}\">";
		}
		
		echo $s;
	}
	
	function &Connect(&$wz)
	{
		$s = '';
		$driver = $wz['DRIVER'];
		$db = NewADOConnection($driver);
		switch ($driver) {
		case 'oci8':
			if ($wz['HOST'] == 'localhost') $wz['HOST'] = '';
			$ok = @$db->Connect(false,$wz['USERID'],$wz['PWD'],$wz['HOST']);
			break;
		default:
			$ok = @$db->Connect($wz['HOST'],$wz['USERID'],$wz['PWD'],$wz['DB']);
		}
		if (!$ok) {
			$this->noNext = true;
			return false;
		}
		return $db;
	}
	
	function Render4(&$wz)
	{
		$db = $this->Connect($wz);
		if (!$db) return;
		
		$s = '';
		$dd = NewDataDictionary($db);
		$sqla = $dd->CreateTableSQL('phplens',
			"ID char(40) notnull primary,
			LASTMOD T notnull,
			DATA X", 
			array() //array('REPLACE')
  			);
		/*
		create table lens_columns (
		tablename varchar(64) not null, 
		colname varchar(64) not null, 
		title varchar(128) default ' ',
		lookup varchar(4000) default ' ',
		primary key(tablename,colname)
		);
		*/
		$sqla2 = $dd->CreateTableSQL('lens_columns',
			"TABLENAME C 64 notnull primary,
			COLNAME    C 64  notnull primary,
			TITLE      C 250 ,
			DATA       X",
			array()
  			);
		$sqla = array_merge($sqla2,$sqla);
		$sql = '';
		foreach($sqla as $s) {
			$sql .= $s.";\n\n";
		}
		echo '<p>'.$wz['StoreDB'].'<i>'.$wz['DB'].'</i><p>';
		echo "<textarea name=sql cols=80 rows=10>".htmlspecialchars($sql)."</textarea>";
	}
	
	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 Render5(&$wz)
	{
	global $LENSWZ_TABLE_CHECK,$HTTP_GET_VARS,$HTTP_POST_VARS;
	
		$db = $this->Connect($wz);
		if (!$db) return;
		
		$t =& $db->MetaTables();
		
		// CHECK TO SEE IF TABLE ALREADY CREATED
		if ($LENSWZ_TABLE_CHECK)
		foreach($t as $v) {
			if (strtoupper($v) == strtoupper($LENSWZ_TABLE_CHECK)) {
				echo $wz['TABLE_EXISTS'];
				return;
			}
		}
		
		// OTHERWISE CREATE TABLES
		if (isset($HTTP_GET_VARS['sql'])) $sql = $HTTP_GET_VARS['sql'];
		else if (isset($HTTP_POST_VARS['sql'])) $sql = $HTTP_POST_VARS['sql'];
		
		$sql = $this->undomq($sql);
		
		$sqla = explode(';',$sql);
		$db->debug=1;
		foreach($sqla as $s) {
			$s = trim($s);
			if ($s)$db->Execute($s);
		}
	}
	
	function GenPath($wz)
	{
	global $HTTP_SERVER_VARS;

		
		$path = dirname($HTTP_SERVER_VARS['PHP_SELF']);
		$at = strrpos($path,'/');
		$path = substr($path,0,$at);
		if (empty($path)) $path = '/phplens';
		
		return $path;
	}
	
	function Render6(&$wz)
	{
	global $HTTP_SERVER_VARS;
		$db = $this->Connect($wz);
		if (!$db) return;
		
		$s = $wz['END'];
		$p = dirname(__FILE__);
		$p = str_replace('\\','/',$p);
		$at = strrpos($p,'/');
		$p = substr($p,0,$at);
		
		// $p holds the application directory path
		echo str_replace('{PATH}',$p.'/phplens/config/phplens.config.inc.php',$s);
		

		$array = array (
			'DRIVER' => addslashes($wz['DRIVER']),
			'HOST' => addslashes($wz['HOST']),
			'USER' => addslashes($wz['USERID']),
			'PWD' => addslashes($wz['PWD']),
			'DB' => addslashes($wz['DB']),
			'SERVERPATH' => $this->GenPath($wz),
			'RANDOM' => md5(rand().time())
		);
		global $LENS_FILE_TEMPLATE;
		$s = $this->ConfigText($LENS_FILE_TEMPLATE,$array);
		$s = htmlspecialchars($s);
		echo "
<textarea rows=20 cols=80>$s</textarea>";
	}
}
?>