<?php
if (!isset($PHP_AUTH_USER) && !isset($PHP_AUTH_PW))
     {
     header("WWW-Authenticate: Basic realm=\"Shockwave Password Protected Directory\"");
     Header("HTTP/1.0 401 Unauthorized");
    echo "<HTML><BODY BGCOLOR=000066 LINK=ffcc00 VLINK=ffcc00 ALINK=ffcc00>
    <DIV ALIGN=center>
    <FONT FACE=arial,verdana SIZE=3 COLOR=ffffff>
    <B>You must have a username and password to enter this page in
    <BR><BR><font size=6><A HREF=\"http://$SERVER_NAME\">$SERVER_NAME</a><B></FONT>
    <BR><BR>Your ip was logged as 
    <BR><FONT COLOR=ffcc00>$REMOTE_ADDR</FONT>
    <BR>Date of attempted entry 
    <BR><FONT COLOR=ffcc00>".$strDate."</FONT>
    <BR>If you feel this is an error please contact 
    <BR><A HREF=\"mailto:$SERVER_ADMIN\">$SERVER_ADMIN</A>
    <BR><BR>Back to <A HREF=\"$HTTP_REFERER\">$HTTP_REFERER</A>";
     exit;
     }    
  else if(($PHP_AUTH_USER=="adm") && ($PHP_AUTH_PW=="adm"))
  {

  

include ("../functions.inc.php");
db_login();

echo "<HTML><head><title>Content Management</title>";
printf("<STYLE>
BODY {COLOR: #000000; FONT-FAMILY: Arial,sans-serif; FONT-SIZE: 10pt; FONT-WEIGHT: bold; TEXT-DECORATION: none}

A:hover {
    COLOR: #000099; FONT-WEIGHT: bold; TEXT-DECORATION: none
}
A {
    COLOR: #000099; TEXT-DECORATION: none
}
A.2:hover {
    COLOR: #000099; FONT-WEIGHT: normal; TEXT-DECORATION: underline
}
A.2 {
    COLOR: #000099; TEXT-DECORATION: underline
}
A:link {
    COLOR: #000080; FONT-FAMILY: Arial,sans-serif; FONT-SIZE: 10pt; FONT-WEIGHT: bold; TEXT-DECORATION: none
}
A:visited {
    COLOR: #000080; FONT-FAMILY: Arial,sans-serif; FONT-SIZE: 10pt; FONT-WEIGHT: bold; TEXT-DECORATION: none
}
A:active {
    COLOR: #000080; FONT-FAMILY: Arial,sans-serif; FONT-SIZE: 10pt; FONT-WEIGHT: bold; TEXT-DECORATION: none
}
A:hover {
    COLOR: #cc0000; FONT-FAMILY: Arial,sans-serif; FONT-SIZE: 10pt; FONT-WEIGHT: bold; TEXT-DECORATION: none
}
</STYLE>");
echo "</head><body>";

echo "<h1>Content management</h1>";
switch ($area){
case "add_temp":
	if (!isset($step)) {
		echo "<h3>Add new template</h3>";
		echo "<form action=$PHP_SELF method=post>";
		echo "Template name: <input type=text name=name size=30 maxlength=30>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Template type: ";
		echo "<SELECT  name=type> <OPTION selected value=0>HTML</OPTION> <OPTION value=1>PHP</OPTION></SELECT><br><BR>";
		echo "Show link to template in menu links listbox:&nbsp;";
		echo "<SELECT  name=show_t> <OPTION selected value=1>show</OPTION> <OPTION value=0>hide</OPTION></SELECT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "Template contents:<br><br>";
		echo "<TEXTAREA name=body cols=60 rows=25></textarea><br><br>";
		echo "<input type=hidden name=area value=add_temp>";
		echo "<input type=hidden name=step value=2>";
		echo "<input type=submit>";
	} else {
		
		
		if ($name==""||$body=="") {
			echo "<h3>All fields must be filled</h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

		} else  {
		
		if (mysql_num_rows(mysql_query("SELECT id FROM $temp_table WHERE name='$name'"))) {
			echo "<h3>Template with name '$name' already exist</h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

		
		} else  {
		
		if (!mysql_query("INSERT INTO $temp_table VALUES (NULL, '$name', '$body', '$type', '$show_t')")) {echo mysql_error(); die();	}
		else {
			echo "<h3>Template added successfully</h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

		}}}
			
	}
break;

case "lst_temp":
	$res=mysql_query("SELECT id, name, type, show_t FROM $temp_table");
	if (!isset($act)){
		echo "<table border=0><tr><td bgcolor=\"#eaeaea\"><B>Id</B></td><td bgcolor=\"#eaeaea\"><B>Name</B></td><td bgcolor=\"#eaeaea\"><B>Type</B></td><td bgcolor=\"#eaeaea\"><B>Show</B></td><td bgcolor=\"#eaeaea\">&nbsp;</td><td bgcolor=\"#eaeaea\">&nbsp;</td></tr>";
		for ($i=0; $i<mysql_num_rows($res) ;$i++) {
			$id=mysql_result($res, $i, 'id');
			$name=mysql_result($res, $i, 'name');
			$type=mysql_result($res, $i, 'type');
			$show_t=mysql_result($res, $i, 'show_t');
			if ($type==1) {$type='PHP';} else {$type='HTML';}
			echo "<tr>";
			echo "<td bgcolor=\"#eaeaea\">$id</td><td bgcolor=\"#eaeaea\">$name</td bgcolor=\"#eaeaea\"><td bgcolor=\"#eaeaea\">$type</td><td bgcolor=\"#eaeaea\">$show_t</td>";
			echo "<td bgcolor=\"#eaeaea\"><a href=$PHP_SELF?area=lst_temp&id=$id&act=edit>Edit</a></td>";
			echo "<td bgcolor=\"#eaeaea\"><a href=$PHP_SELF?area=lst_temp&id=$id&act=del>Delete</a></td>";
			echo "</tr>";
	}
	echo "</table>";
	} elseif ($act=='edit') {
		if (!isset($step)) {
			$name=mysql_result(mysql_query("SELECT name FROM $temp_table WHERE id='$id'"), 0, 'name');
			$body=mysql_result(mysql_query("SELECT body FROM $temp_table WHERE id='$id'"), 0, 'body');
			$type=mysql_result(mysql_query("SELECT type FROM $temp_table WHERE id='$id'"), 0, 'type');
			$show_t=mysql_result(mysql_query("SELECT show_t FROM $temp_table WHERE id='$id'"), 0, 'show_t');
			//$id=mysql_result(mysql_query("SELECT id FROM $temp_table WHERE id='$id'"), 0, 'id');
		echo "<h3>Edit template</h3>";
		echo "<form action=$PHP_SELF method=post>";
		echo "Template name: <input type=text name=name value=\"$name\" size=30 maxlength=30>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Template type: ";
		if ($type!='0') { $sel1=''; $sel2='selected';	} else {$sel2=''; $sel1='selected';}
		echo "<SELECT  name=type> <OPTION $sel1 value=0>HTML</OPTION> <OPTION $sel2 value=1>PHP</OPTION></SELECT><br><BR>";
		if ($show_t=='0') { $sel1=''; $sel2='selected';	} else {$sel2=''; $sel1='selected';}
		echo "Show link to template in menu links listbox:&nbsp;";
		echo "<SELECT  name=show_t> <OPTION $sel1 value=1>show</OPTION> <OPTION $sel2 value=0>hide</OPTION></SELECT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "Template contents:<br><br>";
		echo "<TEXTAREA name=body cols=60 rows=25>$body</textarea><br><br>";
		echo "<input type=hidden name=area value=lst_temp>";
		echo "<input type=hidden name=act value=edit>";
		echo "<input type=hidden name=step value=2>";
		echo "<input type=hidden name=id value=$id>";
		echo "<input type=submit></form>";
		} else {
			if ($name==""||$body=="") {
			echo "<h3>All fields must be filled</h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

		} else  {
		
		/*if (mysql_num_rows(mysql_query("SELECT id FROM $temp_table WHERE name='$name' AND id<>'id'"))) {
			echo "<h3>Cannot rename template to '$name' </h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

		
		} else  {*/
		if (!mysql_query("UPDATE $temp_table SET name='$name', body='$body', type='$type', show_t='$show_t' WHERE id='$id'")) {echo mysql_error(); die();	}
		else {
			echo "<h3>Template updated successfully</h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

		}}
		}

	} elseif ($act=='del') {
			if (!mysql_query("DELETE FROM $temp_table WHERE id='$id'")) {echo mysql_error(); die();	}
			else {
			echo "<h3>Template deleted successfully</h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

			}
	}

break;

case "add_menu":

		if (!isset($step)) {
			echo "<h3>Add menu section</h3>";
			echo "<form action=$PHP_SELF method=get>";
			echo "Section name: <input type=text name=name size=30 maxlength=100>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Parent section: ";
			$res=mysql_query("SELECT * FROM $menu_table");
			echo "<SELECT  name=pid><OPTION selected value=0>root</OPTION>";
			for ($i=0;$i<mysql_num_rows($res) ;$i++) {
			$id=mysql_result($res, $i, 'id');
			$name=mysql_result($res, $i, 'name');
			echo "<OPTION value=$id>$name</OPTION>";	
			}
			echo "</SELECT>";
			$res=mysql_query("SELECT name FROM $temp_table WHERE show_t='1'");
			echo "&nbsp;&nbsp;Linked pattern: <SELECT  name=link>";
			for ($i=0;$i<mysql_num_rows($res) ;$i++) {
			$name=mysql_result($res, $i, 'name');
			echo "<OPTION value=$name>$name</OPTION>";	
			}
			echo "</SELECT><br><BR>";
			echo "<input type=hidden name=area value=add_menu>";
			echo "<input type=hidden name=step value=2>";
			echo "<input type=submit></form>";}
			else {

			if ($name=="") {
				echo "<h3>Input name for new section</h3>";
				echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
				echo "<a href=$PHP_SELF>Main<a><br>";

			} else  {
				if (!mysql_query("INSERT INTO $menu_table  VALUES (NULL, '$pid', '$name', '$link')")) {echo mysql_error(); die();	}
					else {
					echo "<h3>Section added successfully</h3>";
					echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
					echo "<a href=$PHP_SELF>Main<a><br>";

				}}
					

			}
break;


case "lst_menu":
	
	if (!isset($act)&&!isset($act2)) {

		echo "<form action=$PHP_SELF>";
		echo "<select name=did>";
		$space = ""; 
		tree_op(0,$space);
		echo "</select>";
		echo "<input type=hidden name=area value=lst_menu>";
		echo "<input type=hidden name=step value=2>";
		echo "<input type=submit name=act value=edit>";
		echo "&nbsp;&nbsp;&nbsp;<input type=submit name=act2 value=delete>";
	
	} elseif (!isset($act)) {
		$delquery[0]= $did;
		$todel=deltree($did);
		
		for ($i=0; $i<sizeof($todel); $i++) {
			$query="DELETE FROM $menu_table WHERE id='$todel[$i]'";
			$delname = mysql_result(mysql_query("select name from $menu_table where id=$todel[$i]"), 0, 'name');
			if (mysql_query($query)) {
				echo"Successfully deleted section \"$delname\"";
				echo "<br>";
			} else {
				echo "<b>Error while delete section \"$delname\"</b>";
				echo mysql_error();
			}
			
		}
		echo "<br><br><br><a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";
	} else  {
			if (!isset($fin)) {
			$res = mysql_query ("SELECT * FROM $menu_table WHERE id=$did");
			$pid = mysql_result ($res, 0, 'pid');
			$name = mysql_result ($res, 0, 'name');
			$link = mysql_result ($res, 0, 'link');
			echo "<h3>Edit menu section</h3>";
			echo "<form action=$PHP_SELF method=get>";
			echo "Section name: <input type=text name=name value=\"$name\" size=30 maxlength=100>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";//Parent section: ";
			
			/*$res=mysql_query("SELECT * FROM $menu_table");
			if ($pid==0) {
				echo "<SELECT  name=pid><OPTION selected value=0>root</OPTION>";
			} else {echo "<SELECT  name=pid><OPTION value=0>root</OPTION>";}
			
			for ($i=0;$i<mysql_num_rows($res) ;$i++) {
			$id=mysql_result($res, $i, 'id');
			$name=mysql_result($res, $i, 'name');
			if  ($pid==$id){
			echo "<OPTION selected value=$id>$name</OPTION>";	
			} else {echo "<OPTION value=$id>$name</OPTION>";	}
			}
			echo "</SELECT>";
			*/
			$res=mysql_query("SELECT name FROM $temp_table WHERE show_t = '1'");
			echo "&nbsp;&nbsp;Linked pattern: <SELECT  name=link>";
			
			for ($i=0;$i<mysql_num_rows($res) ;$i++) {
			$name=mysql_result($res, $i, 'name');
			if ($name==$link) {
			echo "<OPTION selected value=$name>$name</OPTION>";	}
			else {echo "<OPTION value=$name>$name</OPTION>";	}
			}
			echo "</SELECT><br><BR>";
			echo "<input type=hidden name=area value=lst_menu>";
			echo "<input type=hidden name=step value=2>";
			echo "<input type=hidden name=id value=$did>";
			echo "<input type=hidden name=fin value=1>";
			echo "<input type=submit name=act value=save></form>";} 
		
		else{
			
			if ($name=="") {
			echo "<h3>Please fill 'name' field</h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

		} else  {
		
		if (!mysql_query("UPDATE $menu_table SET name='$name', link='$link'  WHERE id=$id")) {echo mysql_error(); die();	}
		else {
			echo "<h3>Menu section altered successfully</h3>";
			echo "<a href=\"javascript:history.back()\">Go Back<a><br>";
			echo "<a href=$PHP_SELF>Main<a><br>";

		}}
			
			
				}
		}
break;

default:
echo "<h3>Templates</h3><ul>";
echo "<li><a href=$PHP_SELF?area=add_temp>Add template</a>";
echo "<li><a href=$PHP_SELF?area=lst_temp>View/Edit/Delete templates</a></ul>";
echo "<h3>Menus</h3><ul>";
echo "<li><a href=$PHP_SELF?area=add_menu>Add menu section</a>";
echo "<li><a href=$PHP_SELF?area=lst_menu>View/Delete menu sections</a></ul>";
echo "<br><br><br>";
echo "<a href=index.php>Back to Shopping cart management system</a>";

}

echo "</body></html>";






}

else
{
header("WWW-Authenticate: Basic realm=\"Shockwave User Authentucation Required\"");
Header("HTTP/1.0 401 Unauthorized");
    echo "<HTML><BODY BGCOLOR=000066 LINK=ffcc00 VLINK=ffcc00 ALINK=ffcc00>
    <DIV ALIGN=center>
        <DIV ALIGN=center>
    <FONT FACE=arial,verdana SIZE=3 COLOR=ffffff>
    <B>You must have access to view this page in
    <BR><BR><font size=6><A HREF=\"http://$SERVER_NAME\">$SERVER_NAME</a><B></FONT>
    <BR><BR>Your ip was logged as 
    <BR><FONT COLOR=ffcc00>$REMOTE_ADDR</FONT>
    <BR>Date of attempted entry 
    <BR><FONT COLOR=ffcc00>".$strDate."</FONT>
    <BR>If you feel this is an error please contact 
    <BR><A HREF=\"mailto:$SERVER_ADMIN\">$SERVER_ADMIN</A>
    <BR><BR>Back to <A HREF=\"$HTTP_REFERER\">$HTTP_REFERER</A>";

}

?>