<?php

#############################################################################
#           			                   Site@School
# Site@School is a website management system designed for primary schools.
#
# About Site@School:
# Comissioned by: City District 'de Baarsjes', Amsterdam, Netherlands
# Original idea, coding (version 1.0) and design : Mark de Haan (mark@starwave.nl)
# Ideas, improvements, expansions (version 1.1 - 2.3): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# pagemanager.php: last changed 30/01/2006  , version: 2.4
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: pagemanager.inc.php,v 1.2 2006/10/15 19:08:53 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2005 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}
?>
<script language="JavaScript" type="text/javascript">
function checkinput ( form )
{
    if (form.title.value == "") 
    {
      alert( "<?php echo $sas_lang['input_error'] ?>" );
      form.title.focus();
      return false ;
    }
  return true ;
}
</script>
<?php


if (IsSet ($_GET['suboption']))
{
	$suboption = $_GET['suboption'];
}
else
{
	$suboption = "";
}
if (IsSet ($_GET['site']))
{
	$site = $_GET['site'];
}
else
{
	$site = "";
}
if (IsSet ($_GET['page']))
{
	$page = intval($_GET['page']);
}
if (IsSet ($_GET['section']))
{
	$section = intval($_GET['section']);
}
$d = date("Y-m-d");
## section protection get the sections field for the logged on user
## field is blank or has something like 1,2,4,P1,P3
$query = "SELECT sections FROM $table_users WHERE id = '$userid'";
$result = mysql_query($query) or die_script($query, mysql_error());

if ($site == "protected")
{
	$_SESSION['site'] = $site;
	$table_sections = $dbprefix . "prot_sections";
	$table_pages = $dbprefix . "prot_pages";
	$startsection = $sas_config['protected_startsection'];
	$user_sections = substr(mysql_result($result, 0), strpos(mysql_result($result, 0), "P")); //strip of public sections
	$user_sections = str_replace("P", "", $user_sections); //replace P with blanks
	$user_sections = explode(",", $user_sections); //create an array
}
else
{
	$startsection = $sas_config['startsection'];
	$user_sections = explode(",", mysql_result($result, 0)); //create an array
}
unset ($query);
unset ($result);

switch ($suboption) // start of switch
{

	case "editpage" :
		$query = "SELECT content, section, sort, name, id, invisible, category, locked_by 
		          FROM $table_pages WHERE id = $page"; // getting the page content, pagename
		$result = mysql_query($query) or die_script($query, mysql_error());
		$content = mysql_result($result, 0, 0);
		$section = mysql_result($result, 0, 1);
		$sort = mysql_result($result, 0, 2);
		$title = mysql_result($result, 0, 3);
		$pageid = mysql_result($result, 0, 4);
		$invisible = mysql_result($result, 0, 5);
		$category = mysql_result($result, 0, 6);
		$userid_in_use = mysql_result($result, 0, 7);
		unset ($query);
		unset ($result);

		$ses_table = $dbprefix . "sessions";
		$str_len = strlen($userid_in_use);
		$ses_value = "%userid|s:$str_len:\"" . $userid_in_use . "\";%";
		$result1 = mysql_query("SELECT ses_id FROM $ses_table WHERE ses_value like '$ses_value'");

		//check to see if userid holding the page still has a session
		if (mysql_num_rows($result1) == 0)
		{
			mysql_query("UPDATE $table_pages SET locked_by = '' WHERE id='$page'");
			$userid_in_use = ''; //no session , reset locked_by field.
		}

		if (($userid_in_use == '') OR ($userid_in_use == $_SESSION['userid'])) //nobody is holding the page
		{
			//set the newsletter in use by the userid in the locked_by field
			mysql_query("UPDATE $table_pages SET locked_by = '$_SESSION[userid]' WHERE id='$page'");
			$query = "SELECT location FROM $table_editors WHERE id = '$editor'"; // getting the editors location
			$result = mysql_query($query) or die_script($query, mysql_error());
			$editor_loc = mysql_result($result, 0);
			unset ($query);
			unset ($result);
			// include the editor
			include ("editors/$editor_loc");
		}
		else
		{
			$query = "SELECT name FROM $table_users WHERE id = '$userid_in_use'";
			$result = mysql_query($query);
			$user_name = mysql_result($result, 0);
			$msg = $sas_lang['page_in_use2'] . $user_name;
			print "<script language=\"Javascript\">
						   <!--
						   alert (\"" . $msg . "\")
						   window.location = 'index.php?option=pagemanager&module=" . $module . "&section=" . $section . "&site=" . $site . "';
						   //-->
						   </script>";
		}

		break;

	case "editmodpage" :
		$query = "SELECT content, name, sort, module, invisible, category FROM $table_pages WHERE id = $page"; // getting the page content, pagename
		$result = mysql_query($query) or die_script($query, mysql_error()); // sort order, modulenam, invisible bit and category
		$content = mysql_result($result, 0, 0);
		$title = mysql_result($result, 0, 1);
		$sort = mysql_result($result, 0, 2);
		$module = mysql_result($result, 0, 3);
		$invisible = mysql_result($result, 0, 4);
		$category = mysql_result($result, 0, 5);
		unset ($query);
		unset ($result);

		include ("interface/header.inc.php");
		include ("interface/menubar.inc.php");
		include ("interface/menustart.inc.php");
		include ("interface/menuend.inc.php");
		include ("interface/pagestart.inc.php");
		print "<span class=\"heading\">" . $sas_lang['editmodpage'] . "<br></span>\n";
		print "<span class=\"text\">" . $sas_lang['editmodpage_introduction'] . "</span><br>\n";

		$query = "SELECT name FROM $table_sections WHERE id = $section"; // getting the name
		$result = mysql_query($query) or die_script($query, mysql_error());
		$sectionname = mysql_result($result, 0);
		unset ($query);
		unset ($result);

		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=pagemanager&section=$section&savepage=$page&site=$site\" onsubmit=\"return checkinput(this);\">";
		//print "<span class=\"text\">".$sas_lang['section'].": $sectionname";

		print "<p>" . $sas_lang['section'] . "<br><select class=text size=\"1\" name=\"newsection\">\n";
		$query = "SELECT id, name FROM $table_sections";
		$result = mysql_query($query) or die_script($query, mysql_error());
		while ($section_list = mysql_fetch_array($result))
		{
			if ($section_list[0] == $section)
			{
				print "<option selected value=\"$section_list[0]\">$section_list[1]</option>\n";
			}
			else
			{
				print "<option value=\"$section_list[0]\">$section_list[1]</option>\n";
			}
		}
		print "</select>";

		print "<p>" .
		$sas_lang['title'] . "<br><input type=text size=60 name=\"title\" value=\"$title\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['title_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print $sas_lang['order'] . "<br><input type=\"text\" name=\"sort\" size=\"10\" value=\"$sort\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['order_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print $sas_lang['category'] . "<br><input type=text size=20 name=\"category\" value=\"$category\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['category2_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print $sas_lang['module_optional'] . "<br><input type=text size=60 name=\"module\" value=\"$module\" class=\"textfield\">&nbsp;";
		print "&nbsp;<input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['browse'] . "\" name=\"modulebutton\" 
				onClick=\"MyWindow=window.open('core/pag_modbrowser.inc.php?sessioncode=" . $_SESSION['sessioncode'] . "','MyWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=400,left=20,top=20'); return false;\" class=\"modulebutton\">
				&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['module_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		if ($invisible == "yes")
		{
			print "<input type=\"checkbox\" name=\"invisible\" value=\"yes\" checked>" . $sas_lang['invisible'] . "&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['invisible_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br>";
		}
		else
		{
			print "<input type=\"checkbox\" name=\"invisible\" value=\"yes\">" . $sas_lang['invisible'] . "&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['invisible_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br>";
		}
		print "<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['cancel'] . "\" name=\"backbutton\" 
				onClick=\"javascript:parent.location='index.php?option=pagemanager&section=$section&site=$site';\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";

		include ("interface/pageend.inc.php");
		include ("interface/footer.inc.php");

		break;

	case "newsection" : // new section

		include ("interface/header.inc.php");
		include ("interface/menubar.inc.php");
		include ("interface/menustart.inc.php");
		include ("interface/menuend.inc.php");
		include ("interface/pagestart.inc.php");
		print "<span class=\"heading\">" . $sas_lang['newsection'] . "<br></span>";
		print "<span class=\"text\">" . $sas_lang['newsection_introduction'] . "</span><br>";
		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=pagemanager&savesection=new&site=$site\">
		<span class=\"text\">";
		if (IsSet ($section))
		{
			$sectionname = sectionname($section);
			print $sas_lang['section'] . ": $sectionname<br>";
		}

		print "<br>" . $sas_lang['title'] . "<br><input type=text size=60 name=\"title\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['title_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print $sas_lang['order'] . "<br><input type=text size=10 name=\"order\" value=\"$sort\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['order_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print "<input type=\"checkbox\" name=\"invisible\" value=\"yes\">" . $sas_lang['invisible'] . "&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['invisible_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br>";
		print "<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['cancel'] . "\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";

		include ("interface/pageend.inc.php");
		include ("interface/footer.inc.php");

		break;

	case "editsection" : // edit section

		include ("interface/header.inc.php");
		include ("interface/menubar.inc.php");
		include ("interface/menustart.inc.php");
		include ("interface/menuend.inc.php");
		include ("interface/pagestart.inc.php");
		print "<span class=\"heading\">" . $sas_lang['editsection'] . "<br></span>";
		print "<span class=\"text\">" . $sas_lang['editsection_introduction'] . "</span><br>";
		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=pagemanager&savesection=$section&site=$site\">
		<span class=\"text\">";
		if (IsSet ($section))
		{
			$query = "SELECT sort FROM $table_sections WHERE id='$section'"; // getting the sort order
			$result = mysql_query($query) or die_script($query, mysql_error());
			$sort = mysql_result($result, 0);
			unset ($query);
			unset ($result);
			$query = "SELECT name FROM $table_sections WHERE id='$section'"; // getting the pagename
			$result = mysql_query($query) or die_script($query, mysql_error());
			$sectionname = mysql_result($result, 0);
			unset ($query);
			unset ($result);
			$query = "SELECT invisible FROM $table_sections WHERE id='$section'"; // getting the invisible bit
			$result = mysql_query($query) or die_script($query, mysql_error());
			$invisible = mysql_result($result, 0);
			unset ($query);
			unset ($result);

			print $sas_lang['section'] . ": $sectionname<br>";
		}
		print "<br>" . $sas_lang['title'] . "<br><input type=text size=60 name=\"title\" value=\"$sectionname\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['title_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print $sas_lang['order'] . "<br><input type=text size=10 name=\"order\" value=\"$sort\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('$sas_langorder_tip]','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		if ($invisible == "yes")
		{
			print "<input type=\"checkbox\" name=\"invisible\" value=\"yes\" checked>" . $sas_lang['invisible'] . "&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['invisible_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br>";
		}
		else
		{
			print "<input type=\"checkbox\" name=\"invisible\" value=\"yes\">" . $sas_lang['invisible'] . "&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['invisible_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br>";
		}
		print "<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['cancel'] . "\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=pagemanager&site=$site';\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";

		include ("interface/pageend.inc.php");
		include ("interface/footer.inc.php");

		break;

	case "newpage" : // new page

		include ("interface/header.inc.php");
		include ("interface/menubar.inc.php");
		include ("interface/menustart.inc.php");
		include ("interface/menuend.inc.php");
		include ("interface/pagestart.inc.php");
		print "<span class=\"heading\">" . $sas_lang['newpage'] . "<br></span>";
		print "<span class=\"text\">" . $sas_lang['newpage_introduction'] . "</span><br>";

		$query = "SELECT name FROM $table_sections WHERE id='$section'"; // getting the pagename
		$result = mysql_query($query) or die_script($query, mysql_error());
		$sectionname = mysql_result($result, 0);
		unset ($query);
		unset ($result);

		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=pagemanager&section=$section&savepage=new&site=$site\" onsubmit=\"return checkinput(this);\">
		<span class=\"text\">" . $sas_lang['section'] . ": $sectionname<br><br>" .
		$sas_lang['title'] . " (*)<br><input type=text size=60 name=\"title\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['title_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print $sas_lang['order'] . "<br><input type=\"text\" name=\"sort\" size=\"10\" value=\"\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['order_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print $sas_lang['category'] . "<br><input type=text size=20 name=\"category\" value=\"$category\" class=\"textfield\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['category2_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print $sas_lang['module_optional'] . "<br><input type=text size=60 name=\"module\" class=\"textfield\">&nbsp;";
		print "&nbsp;<input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['browse'] . "\" onClick=\"MyWindow=window.open('core/pag_modbrowser.inc.php?sessioncode=" . $_SESSION['sessioncode'] . "','MyWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=400,left=20,top=20'); return false;\" name=\"modulebutton\" onClick=\"javascript:history.go(-1)\" class=\"modulebutton\">&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['module_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><br><br>";
		print "<input type=\"checkbox\" name=\"invisible\" value=\"yes\">" . $sas_lang['invisible'] . "&nbsp;<img src=graphics/help.gif ONMOUSEOVER=\"popup('" . $sas_lang['invisible_tip'] . "','CCDAEC')\"; ONMOUSEOUT=\"kill()\"><p>";
		print $sas_lang['required_fields'] . "<br><br>";
		print "<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['cancel'] . "\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=pagemanager&section=$section&site=$site';\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";

		include ("interface/pageend.inc.php");
		include ("interface/footer.inc.php");
		break;

	default : // page manager
		include ("core/pag_start.inc.php");
		break;
}
?>
