<?php 
$action = $_GET['action'] ;
if ($action == "installnow") {
	$file = urldecode($_GET['file']) ;
	if ($file != "") {
		include "includes/admin/features/".$file."_ext.inc" ;
	}
}
if (isset($_POST['submit'])) {
	$featureid = $_POST['featureid'] ;
	$featurekey = $_POST['featurekey'] ;
	if ($_POST['featureenabled'] == 'on') {
		$featureenabled = 1 ;
	}
	else {
		$featureenabled = 0 ;
	}
	$featuredata1 = $_POST['featuredata1'] ;
	$featuredata2 = $_POST['featuredata2'] ;
	$featuredata3 = $_POST['featuredata3'] ;
	$featuredata4 = $_POST['featuredata4'] ;
	$featuredata5 = $_POST['featuredata5'] ;
	if ($featuredata1 == "on") {
		$featuredata1 = 1 ;
	}
	if ($featuredata1 == "") {
		$featuredata1 = 0 ;
	}
	if ($featuredata2 == "on") {
		$featuredata2 = 1 ;
	}
	if ($featuredata2 == "") {
		$featuredata2 = 0 ;
	}
	if ($featuredata3 == "on") {
		$featuredata3 = 1 ;
	}
	if ($featuredata3 == "") {
		$featuredata3 = 0 ;
	}
	if ($featuredata4 == "on") {
		$featuredata4 = 1 ;
	}
	if ($featuredata4 == "") {
		$featuredata4 = 0 ;
	}
	if ($featuredata5 == "on") {
		$featuredata5 = 1 ;
	}
	if ($featuredata5 == "") {
		$featuredata5 = 0 ;
	}
	if ($_POST['edittype'] == 'mainedit') {
		$db->query("UPDATE feature SET `key` = '".$featurekey."' WHERE id=".$featureid) ;
		$db->query("UPDATE feature SET `enabled`=".$featureenabled." WHERE id=".$featureid) ;
		$db->query("UPDATE feature SET `data_1`='".$featuredata1."' WHERE id=".$featureid) ;
		$db->query("UPDATE feature SET `data_2`='".$featuredata2."' WHERE id=".$featureid) ;
		$db->query("UPDATE feature SET `data_3`='".$featuredata3."' WHERE id=".$featureid) ;
		$db->query("UPDATE feature SET `data_4`='".$featuredata4."' WHERE id=".$featureid) ;
		$db->query("UPDATE feature SET `data_5`='".$featuredata5."' WHERE id=".$featureid) ;
	}
}
$featuresLink = '' ;
$db->query("SELECT * FROM feature ORDER BY id") ;
$res = $db->fetchAll() ;
$featuresForm = "<table width=650px border=0>" ;
foreach ($res as $row) {
	$featuresForm .= '<tr><form method="post" action=""><input type="hidden" value="'.$row['id'].'" name="featureid"/><input type="hidden" name="edittype" value="mainedit">' ;
	$featuresForm .= '<td width=200px><h3>'.$row['name'].'</h3></td>' ;
	//$featuresForm .= '<td width=300px>Key&nbsp;<input size="35" type="text" value="'.$row['key'].'" name="featurekey"/><br><b>'.$active.'</b></td>' ;
	$featuresForm .= '<td width=70px style="font-size:12px">Enabled&nbsp;&nbsp;<input type="checkbox" name="featureenabled"' ;
	$enabled = (($row['enabled'] == true)) ;
	if ($enabled == true)
		$featuresForm .= ' checked' ;
	$featuresForm .= '/></td>' ;
	$featuresForm .= '<td><div align=left><input type="submit" class="biggerbutton" name="submit" value="Save"></div></td>' ;
	$featuresForm .= '</tr>' ;
	if ($row['var_count'] > 0) {
		$featuresForm .= '<tr><td colspan=4><table><tr><td width="250px">'.$row['data_name_1'].'</td><input type="hidden" value="'.$row['id'].'" name="featureid"/><td>' ;
		if ($row['data_type1'] == 'bool') {
			if ($row['data_1'] > 0) {
				$featuresForm .= '<input type="checkbox" name="featuredata1" CHECKED>' ;
			}
			else {
				$featuresForm .= '<input type="checkbox" name="featuredata1">' ;
			}
		}
		else {
			$featuresForm .= '<textarea rows="5" cols="65" name="featuredata1">'.htmlentities($row['data_1'], ENT_QUOTES).'</textarea>' ;
		}
		$featuresForm .= '</td></tr></table></td></tr>' ;
	}
	if ($row['var_count'] > 1) {
		$featuresForm .= '<tr><td colspan=4><table><tr><td width="250px">'.$row['data_name_2'].'</td><input type="hidden" value="'.$row['id'].'" name="featureid"/><td>' ;
		if ($row['data_type2'] == 'bool') {
			if ($row['data_2'] > 0) {
				$featuresForm .= '<input type="checkbox" name="featuredata2" CHECKED>' ;
			}
			else {
				$featuresForm .= '<input type="checkbox" name="featuredata2">' ;
			}
		}
		else {
			$featuresForm .= '<textarea rows="5" cols="65" name="featuredata2">'.htmlentities($row['data_2'], ENT_QUOTES).'</textarea>' ;
		}
		$featuresForm .= '</td></tr></table></td></tr>' ;
	}
	if ($row['var_count'] > 2) {
		$featuresForm .= '<tr><td colspan=4><table><tr><td width="250px">'.$row['data_name_3'].'</td><input type="hidden" value="'.$row['id'].'" name="featureid"/><td>' ;
		if ($row['data_type3'] == 'bool') {
			if ($row['data_3'] > 0) {
				$featuresForm .= '<input type="checkbox" name="featuredata3" CHECKED>' ;
			}
			else {
				$featuresForm .= '<input type="checkbox" name="featuredata3">' ;
			}
		}
		else {
			$featuresForm .= '<textarea rows="5" cols="65" name="featuredata3">'.htmlentities($row['data_3'], ENT_QUOTES).'</textarea>' ;
		}
		$featuresForm .= '</td></tr></table></td></tr>' ;
	}
	if ($row['var_count'] > 3) {
		$featuresForm .= '<tr><td colspan=4><table><tr><td width="250px">'.$row['data_name_4'].'</td><input type="hidden" value="'.$row['id'].'" name="featureid"/><td>' ;
		if ($row['data_type4'] == 'bool') {
			if ($row['data_4'] > 0) {
				$featuresForm .= '<input type="checkbox" name="featuredata4" CHECKED>' ;
			}
			else {
				$featuresForm .= '<input type="checkbox" name="featuredata4">' ;
			}
		}
		else {
			$featuresForm .= '<textarea rows="5" cols="65" name="featuredata4">'.htmlentities($row['data_4'], ENT_QUOTES).'</textarea>' ;
		}
		$featuresForm .= '</td></tr></table></td></tr>' ;
		if ($row['var_count'] > 4) {
			$featuresForm .= '<tr><td colspan=4><table><tr><td width="250px">'.$row['data_name_5'].'</td><input type="hidden" value="'.$row['id'].'" name="featureid"/><td>' ;
			if ($row['data_type5'] == 'bool') {
				if ($row['data_5'] > 0) {
					$featuresForm .= '<input type="checkbox" name="featuredata5" CHECKED>' ;
				}
				else {
					$featuresForm .= '<input type="checkbox" name="featuredata5">' ;
				}
			}
			else {
				$featuresForm .= '<textarea rows="5" cols="65" name="featuredata5">'.htmlentities($row['data_5'], ENT_QUOTES).'</textarea>' ;
			}
			$featuresForm .= '</td></tr></table></td></tr>' ;
		}
	}
	$featuresForm .= '</form><tr><td colspan=4><hr></td></tr>' ;
}
$featuresForm .= '</table>' ;
$installForm = "<h3><div align=right><a href='".$sitepath."index.php?id=150'>>>back to manage features</a></div></h3><br><table width=650px border=0>" ;
$installForm .= "<br><h2>Features installed</h2><br>" ;
$db->query("SELECT * from feature WHERE installable = 1") ;
if ($db->numRows() > 0) {
	$eres = $db->fetchAll() ;
	foreach ($eres as $key=>$value) {
		$installForm .= "<b>".$value['name']."</b><br>" ;
	}
}
else {
	$installForm .= "<b>You have not installed any features yet</b><br>" ;
}
$anythere = 0 ;
$installForm .= "<br><br><h2>Features uploaded and ready to install</h2><br>" ;
if ($handle = opendir($rootpath.'includes/admin/features/')) {
	while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != "..") {
			$db->query("SELECT filename FROM feature WHERE filename='".str_replace("_ext.inc", "", $file)."'") ;
			if ($db->numRows() < 1) {
				$feature_name = str_replace("_", " ", str_replace("_ext.inc", "", $file)) ;
				$installForm .= "<b>".$feature_name." <a href='".$sitepath."index.php?id=150&action=installnow&file=".urlencode(str_replace("_ext.inc", "", $file))."'>install</a></b><br>" ;
				$anythere = 1 ;
			}
		}
	}
}
closedir($handle) ;
$installForm .= ($anythere < 1) ? "<b>There are no features available to install, please check that you have uploaded all files correctly</b><br>" : "<br><b><font color='red'>(Features can't be uninstalled but can be disabled)</font></b>" ;
$installForm .= '</table>' ;
?>