<?php
/**
* (c) 2000, 2001, 2002 Remistech Sdn Bhd. All Rights Reserved.
*  Developed by John Lim
*  Licensed to Natsoft (M) Sdn Bhd to distribute and support. Refer to LICENSE document.
*/
/* 
	there is a bug in PHP -- given a page frame, 
	the session id is not propagated if js open window under IIS - PWS ok
	
	so we restore all session variables we can if they are in the GET vars
*/

error_reporting(63);
if (isset($HTTP_GET_VARS['clear'])) die();

//--------------
// Declarations
$new_id = false;
$gLens_Table = '';
$gLens_Prefix = 't_';
$iderr= '';

include_once('./init.php');
include_once(ADODB_DIR.'/tohtml.inc.php');

function CheckIDWidth($gDB)
{
global $PHPLENS_TABLE;

	$arr = $gDB->MetaColumns($PHPLENS_TABLE);
	if (isset($arr['id'])) $ocol = $arr['id'];
	else if (isset($arr['ID'])) $ocol = $arr['ID'];
	
	if (isset($ocol) && isset($ocol->max_length))
		if (0 < $ocol->max_length && $ocol->max_length < PHPLENS_MAX_ID_LEN+2) {
			print "<table border=1 bgcolor=yellow><tr><td><p><b>Since phpLens 2.1, the <i>phplens</i> table <i>id</i> field must be at least ".(PHPLENS_MAX_ID_LEN+2).
				" characters wide. Please do an <i>ALTER TABLE phplens MODIFY id CHAR(".(PHPLENS_MAX_ID_LEN+2).
				")</i>. Note that the ALTER TABLE syntax might need to be changed depending on your database. </b></p></h3></td></tr></table>";
		}
}

//---------------------
// Session definition
session_register('gLens_Cols');
session_register('gLens_Table');
session_register('gLens_LastSave');


$gLens_Cols = &$HTTP_SESSION_VARS['gLens_Cols'];
$gLens_Table = &$HTTP_SESSION_VARS['gLens_Table'];
$gLens_LastSave = &$HTTP_SESSION_VARS['gLens_LastSave'];

function SetLensApplet(&$lens)
{
	SetLensAppletGlobal($lens);
}

if (empty($gLens_LastSave)) $gLens_LastSave = '';

//----------------------
// GET variable handling
if (isset($HTTP_GET_VARS['lensnewid'])) {
	$new_id = $HTTP_GET_VARS['lensnewid'];

	if (!empty($new_id) && $new_id != $lens_qb_id) {
		$dir = APPLETS_DIR."/objs/$new_id";
		if (file_exists($dir)) {
			$iderr = '<p>ID is in use already</p>';
		} else if (!eregi('^[A-Z0-9_]{1,'.PHPLENS_MAX_ID_LEN.'}$',$new_id)) {
			$iderr = '<p>ID is invalid (should be a-z A-Z 0-9 or _ and no spaces)</p>';
			if (isset($HTTP_GET_VARS['Save'])) unset($HTTP_GET_VARS['Save']);
		}
	}
	
}


if (!empty($HTTP_GET_VARS['table'])) {
	if ($gLens_Table != $HTTP_GET_VARS['table']) {
		$lens_qb_id=lens_qb_gen_id();
		$gLens_Table = $HTTP_GET_VARS['table'];	
		$gLens_LastSave = '';
		
		if (strpos($gLens_Table,' ') !== false) $gLens_Table = '"'.$gLens_Table.'"';
	}
}

$lensid = $lens_qb_id;

if (isset($HTTP_GET_VARS['qb_id'])) {
	$lens_qb_id = $HTTP_GET_VARS['qb_id'];
}

$tab = $gLens_Table;

?>
<html>
<head>
<title>PHPLens</title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo isset($PHPLENS_CHARSET) ? $PHPLENS_CHARSET : 'iso-8859-1'?>">
</head>

<body bgcolor="#FFFFFF">
<SCRIPT language=javascript>
var phplens_upgrade='Configuring phpLens requires Internet Explorer 4.0+ or Netscape 4.0+.'
var phplens_win1
if (parseInt(navigator.appVersion) < 4) alert(phplens_upgrade);

/*open a url in a new window */
function phplens_openwin(zurl)
{
var invalidwin,r,url

	r = Math.random()
	url = ''+zurl
	if (url.match(/\?/)) url += '&rnd='+r
	else url += '?rnd='+r
	
	if (phplens_win1 && phplens_win1.open && !phplens_win1.closed) {
		phplens_win1.document.open();
		if (phplens_win1.location.href != url) 
			phplens_win1.location.href = url;
	}else {
		phplens_win1 = window.open(url,'phplensmsgwindow',"height=400,width=700,scrollbars=yes,resizable=yes") 
	}
	phplens_win1.focus()
	
}

</SCRIPT>
<?php
//print_r($HTTP_GET_VARS);

if (!$gDB) {
	print "<h3>Cannot connect to ($lens_qb_driver): $lens_qb_server $lens_qb_database</h3>";
}

if (!empty($tab) && $gDB) {

	if ( $gLens_LastSave  == $lens_qb_id) {
		$DB = $gDB;
		include_once(APPLETS_DIR."/objs/$lens_qb_id/$lens_qb_id.inc.php");
		print "</body></html>";
		die();
	} 	
	
	include_once(APPLETS_DIR.'/init/setlensappletglobal.inc.php');
	CheckIDWidth($gDB);
		
	$arr = $gDB->MetaPrimaryKeys($tab);
	if ($arr) {
		if (sizeof($arr)>1) {
			$keyCompound = implode(';',$arr);
		} else $keyCol = $arr[0];
	}
	
	$sep = '^';
	$gLens_Cols = array();
	$thecols = $gDB->MetaColumns($tab);
	if ($thecols)
		foreach($thecols as $o) {
			$gLens_Cols[] = trim($o->name);
		} 
	$gLens_Cols = implode(',',$gLens_Cols);
	if (strlen($gLens_Cols) == 0) $gLens_Cols = '*';
	
	$lens = new PHPLens($lensid,$gDB,"select $gLens_Cols from $tab");
	
	$lens->_idprefix = $gLens_Prefix;
	if (isset($keyCol))$lens->keyCol = $keyCol;
	if (isset($keyCompound)) $lens->keyCompound = $keyCompound;
	$lens->overrideFunction = 'SetLensApplet';
	if (!empty($HTTP_GET_VARS['columns'])) {
$help=<<<EOD
<h4>
To control which columns are visible, click on $lens->dynEditTabIcon on the far left.<br>
To edit column titles and other column stuff, click on $lens->dynEditColIcon next to the column titles.<br> 
For global changes, click on $lens->dynEditIcon in the navigation bar on the right.
</h4>
EOD;
		print $help;
		$lens->Reset();
	}
	
	ob_start();
		$cols = urlencode($gLens_Cols);
		print "<div align=center><a href='javascript:phplens_openwin(\"$PHP_SELF?lens_e_$lens->id=code&table=$tab&qb_id=$lens_qb_id&cols=$cols\")'><b>View PHP Source Code</b></a> &nbsp; <a href=$PHPLENS_PATH/help/ target=lensgridhelp>Help</a></div>";
		
		$lens->applet = 1;
		$lens->dynEdit = 2;
		PHPLensLightFonts($lens);
		if (function_exists('LensModifyUI')) LensModifyUI(&$lens);
		$lens->Render();
		
		$rendered = ob_get_contents();
	ob_end_clean();
	$lens->Verify();
	print $rendered;
	$lens->Close();
	if (isset($PHPLENS_TIMER)) print "<p>Bytes generated = ".strlen($rendered)."</p>";

?><div align=center>
<?php print $iderr; 
	if (!empty($lens) && isset($HTTP_GET_VARS['Save'])) { // once only
	
		$dir = APPLETS_DIR."/objs/$new_id";
		$lens->id = $new_id;
		$e = PHPLENS_DynEdit($lens); 
		
		if (LensSaveApplet($e,$dir,$lens_qb_drivername)) $gLens_LastSave = $new_id;
		
	}
}
if (!empty($lens) && ($gLens_LastSave != $new_id || empty($new_id))) {
?>
<form name=id>
To save, set Applet ID: <input type=text size=<?php echo PHPLENS_MAX_ID_LEN;?>  name=lensnewid maxlength=<?php echo PHPLENS_MAX_ID_LEN;?> value=<?php print $lens_qb_id;?>>
 <input type=submit name=Save onclick="javascript:return confirm('Have you chosen an Applet ID before you save?')"  value="Save Applet">
</form>
<?php 
}

if (isset($lens)) {
	if (isset($PHPLENS_TIMER)) PHPLensTimer($lens->timer);
}
if ($gDB) $gDB->Close();
if (!empty($sessdb)) $sessdb->Close();
?>
</div>
</body>
</html>
