<?php
/**
* (c) 2000-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.
*/

//---------------------------------------------------------------------- INCLUDES

@include_once("./builder-config.inc.php");
if (!defined('PHPLENS_DIR')) define('PHPLENS_DIR','..');
include_once(PHPLENS_DIR."/phplens.inc.php");

//---------------------------------------------------------------------- INIT
ob_start();
session_start();
error_reporting(E_ALL);
define('PHPLENSGRIDBUILDER',1);


if (!isset($PHP_SELF)) $PHP_SELF = $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'];

if (!defined('PHPLENS_MENU_COLOR')) {
	define('PHPLENS_MENU_COLOR','#FF9933');
	define('PHPLENS_MENUFONT_COLOR','white');
	define('PHPLENS_BKGD_COLOR','white');
	define('PHPLENS_SIDE_COLOR','#FFFFCC');
	define('PHPLENS_MENU2_COLOR','#FFCC99');
}

//-------------------------------------------
// Set the following to false to prevent the 
// 'X' delete applet icon from showing
$ALLOW_DELETES = true;

$dir = APPLETS_DIR."/objs";
ProcessGET($dir);

//---------------------------------------------------------------------- FUNCTIONS
function ProcessGET($dir)
{
global $gApplet_name,$HTTP_SESSION_VARS,$ALLOW_DELETES;

	if ($ALLOW_DELETES && isset($_GET['delete'])) {
		$applet = $_GET['delete'];
		if ($HTTP_SESSION_VARS['gApplet_name'] == $applet) { 
			$gApplet_name = false;
			$HTTP_SESSION_VARS['gApplet_name'] = false;
		}
		$HTTP_SESSION_VARS['lens_qb_id'] = false; // make sure that grid builder has no problems too
		
		if (!preg_match('/^[a-z0-9_]*$/i',$applet)) {
			echo "<p>Bad Applet</p>";
			return;
		}
		
		$ddir = $dir.'/'.$applet;
		if (strncmp(PHP_OS,'WIN',3) === 0)  {
			$ddir = str_replace('/','\\',$ddir);
			$cmd = 'cmd /c rmdir /s /q "'.$ddir.'"';
			system($cmd);
		} else {
			$cmd = "rm -rf $ddir";
			system($cmd);
		}
		
	}
}

//---------------------------------------------------------------------- START HTML
?>
<html>

<head>
<title>PhpLens Builder</title>
</head>

<body bgcolor=#CCCCCC topmargin=0>
<table width=100% cellpadding="3" cellspacing="0"><tr bgcolor=<?php print PHPLENS_MENU_COLOR;?>>
<td align> <img src=phplens.gif><td align=right>
 &nbsp; &nbsp; <a><b>Applets</b></a> 
 &nbsp; &nbsp; <a href=grid/index.php target=_top><b>Builder</b></a> 
 &nbsp; &nbsp; <a href=grid/sql.php target=_top><b>SQL Tool</b></a>
  &nbsp; &nbsp; <a href=ide/index.php target=_top><b>Editor</b></a>
 </td></tr></table>
<?php
//---------------------------------------------------------------------- GENERATE APPLET LIST
	if (ini_get('safe_mode')) {
		#print "<p>Warning: Applets will not work properly in <a href=http://www.php.net/manual/en/features.safe-mode.php>safe mode</a>. Please set safe_mode=off in your php.ini file.</p>";
	}
	
	$d = @dir($dir);
	if (!$d) {
		@mkdir($dir,0777);
		print "Created directory";
	} else {
		$arr = array();
		while($entry=$d->read()) {
			if ($entry[0] == '.' || !is_dir($dir."/".$entry)) continue;
			if ($entry == 'CVS') continue;
			$arr[] = $entry;
		}
		$d->close(); 
	}
	
?>
<table border=0 width=100%>
<tr><td width=15% valign=top bgcolor=<?php print PHPLENS_SIDE_COLOR;?>>

<table>
<?php
//---------------------------------------------------------------------- DISPLAY APPLET LIST

	if (isset($arr) && is_array($arr)) {
		asort($arr);
		foreach ($arr as $entry) {
			if (isset($HTTP_GET_VARS['applet']) && $entry == $HTTP_GET_VARS['applet']) {
				$entry2 = "<b><font class=A>$entry</font></b>";
				$HTTP_SESSION_VARS['gApplet_name'] = $entry;
			} else if (!isset($HTTP_GET_VARS['applet']) && 
				isset($HTTP_SESSION_VARS['gApplet_name']) && 
				$entry == $HTTP_SESSION_VARS['gApplet_name']) $entry2 = "<b><font class=A>$entry</font></b>";
			else $entry2 = "<font class=B>$entry</font>";
			echo "<tr><td nowrap>\n";
			if ($ALLOW_DELETES)
				echo "	<a href=$PHP_SELF?delete=$entry onclick=\"javascript:return confirm('Delete $entry?')\"><img border=0 src=$PHPLENS_PATH/img/del.gif alt=Delete></a> ";
	 	  	echo "	<a href=$PHP_SELF?applet=$entry>$entry2</a><br>\n";
			echo "</td></tr>\n";
		}
	}

?>
</table>
</td><td bgcolor=<?php print PHPLENS_BKGD_COLOR;?> valign=top><br>
<?php
//---------------------------------------------------------------------- RUN SELECTED APPLET
	include_once('run.php');
?>
</td></tr>
</table>
<?php 
//---------------------------------------------------------------------- DISPLAY FOOTER
print "<table border=1 bgcolor=white align=center><tr><td>".
	($gApplet_name ? "View <a href=$PHP_SELF?v=inc>Include File</a> <a href=$PHP_SELF?v=imp>Implementation File</a>" : '').
" &nbsp; <font size=1>APPLETS are stored in <b>".APPLETS_DIR."/objs</b></font>".
"</td></tr></table>";
?>
</body>
</html>
<?php
	if (isset($DB) && is_object($DB)) $DB->Close(); 
	die();
?>
