<?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.4): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# pag_start.inc.php: last changed 04/10/2006  , version: 2.4.10
#                    by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: pag_start.inc.php,v 1.2 2006/10/15 19:08:53 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2006 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");
}
if (IsSet ($_GET['setstartpage']))
{
	$setstartpage = $_GET['setstartpage'];
}
if (IsSet ($_GET['category']))
{
	$category = rawurldecode($_GET['category']);
}
else
{
	$category = "";
}
if (IsSet ($_GET['savepage']))
{
	$savepage = $_GET['savepage'];
}
if (IsSet ($_GET['deletepage']))
{
	$deletepage = $_GET['deletepage'];
}
if (IsSet ($_GET['setstartsection']))
{
	$setstartsection = $_GET['setstartsection'];
}
if (IsSet ($_GET['deletesection']))
{
	$deletesection = $_GET['deletesection'];
}
if (IsSet ($_POST['invisible']))
{
	$invisible = $_POST['invisible'];
}
if (IsSet ($_POST['module']))
{
	$module = $_POST['module'];
}
if (IsSet ($_POST['newsection']))
{
	$newsection = $_POST['newsection'];
}
else
{
	$newsection = "";
}
if (IsSet ($_POST['sort']))
{
	$sort = $_POST['sort'];
}
if (IsSet ($_GET['savesection']))
{
	$savesection = $_GET['savesection'];
}
if (IsSet ($_POST['order']))
{
	$order = $_POST['order'];
}
if (IsSet ($_POST['category']))
{
	$category = $_POST['category'];
}

$table_stats = $dbprefix . "stats";

if (IsSet ($_POST['title']))
{
	if (!get_magic_quotes_gpc())
	{
		$title = addslashes($_POST['title']);
	}
	else
	{
		$title = $_POST['title'];
	}
}
if (IsSet ($_POST['content']))
{
	if (!get_magic_quotes_gpc())
	{
		$content = addslashes($_POST['content']);
	}
	else
	{
		$content = $_POST['content'];
	}
}

if (IsSet ($savesection))
{
	if ($savesection == "new")
	{
		mysql_query("INSERT INTO $table_sections (id, name, sort, invisible) VALUES (NULL, '$title', '$order', '$invisible')");
	}
	else
	{
		mysql_query("UPDATE $table_sections SET name='$title' WHERE id='$savesection'");
		mysql_query("UPDATE $table_sections SET sort='$order' WHERE id='$savesection'");
		mysql_query("UPDATE $table_sections SET invisible='$invisible' WHERE id='$savesection'");
	}
}

if (IsSet ($setstartpage))
{
	mysql_query("UPDATE $table_sections SET frontpage='$setstartpage' WHERE id='$section'");
}

mysql_query("UPDATE $table_pages SET locked_by = NULL WHERE locked_by = $_SESSION[userid]");
//clear locked_by newsletterss which might be in use by the user

if (IsSet ($savepage) && ($title != ""))
{
	$history = "Uid: " . $userid . " - " . date("H:i:s"); //saved in the history field of the pages table

	if ($savepage == "new")
	{
		mysql_query("INSERT INTO $table_pages (id, section, name, invisible, content, module, sort, count, createdate, lastvisitdate, category, lastupdate, history) 
		    VALUES (NULL, '$section', '$title', '$invisible', '$content', '$module', '$sort', '0', '$d', '0000-00-00', '$category', '$d','$history')") or die("Invalid query: " . mysql_error());
		if ($sort == "1")
		{
			$result = mysql_query("SELECT id FROM $table_pages WHERE name = '$title' AND section = '$section'");
			$pageid = mysql_result($result, 0);
			mysql_query("UPDATE $table_sections SET frontpage='$pageid' WHERE id='$section'");
		}
	}
	else
	{
		$result = mysql_query("SELECT DATE_FORMAT(lastupdate,'%Y-%m-%d') FROM $table_pages WHERE id = '$savepage' ");
		$change_date = mysql_result($result, 0, 0);
		$d = date("Y-m-d"); //todays date 

		if ($change_date < $d)
		{ //send alerts only the first time on a date 
			include ("core/send_alerts.php"); //check to see if need to send alerts
		}

		if ($newsection == "")
		{
			$newsection = $section;
		} //if $newsection is empty we use the normal section
		$d = date("Y-m-d H:i:s"); //todays date time
		mysql_query("UPDATE $table_pages SET name='$title',
												sort='$sort' ,
												content='$content',
												module='$module',
												invisible='$invisible',
												category='$category',
												history='$history',
												lastupdate='$d',
												section='$newsection' 
												WHERE id='$savepage'");

		if (IsSet ($_POST['return_to_editor'])) //when return to editor is checked jump back to editor
		{
			print "<script language='javascript'>
							window.location = 'index.php?option=pagemanager&suboption=editpage&page=$savepage&section=$section&site=$site'
							</script>";
		}
	}
}

if (IsSet ($setstartsection))
{
	if ($site == "protected")
	{ // getting the startsection protected site
		mysql_query("UPDATE $table_configuration SET config_value='$setstartsection' WHERE config_key='protected_startsection'");
		$query = "SELECT config_value FROM $table_configuration where config_key='protected_startsection'";
	}
	else
	{ // getting the startsection normal site
		mysql_query("UPDATE $table_configuration SET config_value='$setstartsection' WHERE config_key='startsection'");
		$query = "SELECT config_value FROM $table_configuration where config_key='startsection'";
	}
	$result = mysql_query($query) or die_script($query, mysql_error());
	$startsection = mysql_result($result, 0);
	unset ($query);
	unset ($result);
}

if (IsSet ($deletesection))
{
	mysql_query("DELETE FROM $table_pages WHERE section = '$deletesection'"); //delete section and assiociated pages
	mysql_query("DELETE FROM $table_sections WHERE id = '$deletesection'");
	if ($site == "protected")
	{
		mysql_query("DELETE FROM $table_stats WHERE sectionid = '$deletepage' AND public = 1"); //delete section from stats also
	}
	else
	{
		mysql_query("DELETE FROM $table_stats WHERE sectionid = '$deletepage' AND public = 0"); //delete section from stats also
	}
}

if (IsSet ($deletepage))
{
	mysql_query("DELETE FROM $table_pages WHERE id = '$deletepage'"); //delete the page
	mysql_query("DELETE FROM $table_stats WHERE pageid = '$deletepage'"); //delete page from stats also
}

include ("interface/header.inc.php");
include ("interface/menubar.inc.php");
include ("interface/menustart.inc.php");
print "<span class=heading>" . $sas_lang['filemanager_menu'] . "</span><br><br>";
print "<span class=link><a href=index.php?option=pagemanager>" . $sas_lang['public_area'] . "</a><p>
       <a href=index.php?option=pagemanager&site=protected>" . $sas_lang['protected_area'] . "</a><br></span>";
include ("interface/menuend.inc.php");
include ("interface/pagestart.inc.php");

if (($site == "protected") AND (substr("$access", 12, 1) == "0")) //bit 13 off the accessrights
{
	print "<span class=\"heading\">" . $sas_lang['noaccess'] . "<br></span>";
	print "<span class=\"text\">" . $sas_lang['noaccess_introduction'] . "</span><br><br><br>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if ((substr("$access", 1, 1) == "0") and ($site != "protected")) //bit 2 off the accessrights
{
	print "<span class=\"heading\">" . $sas_lang['noaccess'] . "<br></span>";
	print "<span class=\"text\">" . $sas_lang['noaccess_introduction'] . "</span><br><br><br>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

print "<span class=\"heading\">" . $sas_lang['pagemanager'] . "<br></span>";
print "<span class=\"text\">" . $sas_lang['pagemanager_introduction'] . "</span><br><br>";

print "<img border=\"0\" src=\"graphics/site.gif\" width=\"16\" height=\"16\">&nbsp;";
print "<span class=\"link\"><a href=index.php?option=pagemanager&site=$site>$sitename</a></span>";
if ($site == "protected")
{
	print "<span class=\"text\"> (" . $sas_lang['protected_area'] . ")</span>";
}
print "<table border=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\">";
print "<tr>
            <td height=\"18\" colspan=\"10\"><br></td>
            </tr>";

$query = "SELECT id, name, frontpage, invisible FROM $table_sections WHERE parent = '' ORDER BY sort"; // getting sectionlist
$result = mysql_query($query) or die_script($query, mysql_error());
$RecordCount = mysql_num_rows($result);
if ($RecordCount > 0)
{
	while ($section_list = mysql_fetch_array($result))
	{
		if ((in_array($section_list[0], $user_sections)) OR ($user_sections[0] == "")) //is user allowed to edit this section?
		{
			print "<tr><td height=\"18\" width=\"20\"></td>";

			if ($section_list[0] == $startsection)
			{
				if ($highvisibility == "1")
				{
					print "<td><span class=link>[Hm]</span></td>";
				}
				else
				{
					print "<td><img border=\"0\" src=\"graphics/startsection.gif\" title=\"" . $sas_lang['startsection'] . "\" alt=\"" . $sas_lang['startsection'] . "\" width=\"16\" height=\"16\"></td>";
				}
			}
			else
			{
				if ($highvisibility == "1")
				{
					print "<td><span class=text><a href=index.php?option=pagemanager&setstartsection=$section_list[0]&site=$site>[set Hm]</span></a></td>";
				}
				else
				{
					print "<td><img border=\"0\" src=\"graphics/not_startsection.gif\" title=\"" . $sas_lang['startsection'] . "\" 
									alt=\"" . $sas_lang['startsection'] . "\" width=\"16\" height=\"16\" onMouseOver=\"this.src='graphics/startsection.gif'\" onMouseOut=\"this.src='graphics/not_startsection.gif'\" 
									onClick=\"if(confirm('" . $sas_lang['startsection_confirmation'] . "')) parent.location='index.php?option=pagemanager&setstartsection=$section_list[0]&site=$site';\"></td>";
				}
			}
			print "<td height=\"18\">";

			if ($highvisibility == "1")
			{
				print "<span class=text><a href=index.php?option=pagemanager&deletesection=$section_list[0]&site=$site>[Dl]</a> <span>";
				print "</td><td height=\"18\">";
				print "<span class=text> <a href=\"index.php?option=pagemanager&suboption=editsection&section=$section_list[0]&site=$site\">[Ed]</a> <span>";

			}
			else
			{
				print "<img border=\"0\" src=\"graphics/delete.gif\" title=\"" . $sas_lang['delete'] . "\" alt=\"" . $sas_lang['delete'] . "\" width=\"16\" height=\"16\" 
						onClick=\"if(confirm('" . $sas_lang['delete_confirmation'] . "')) parent.location='index.php?option=pagemanager&deletesection=$section_list[0]&site=$site';\">";
				print "</td><td height=\"18\">";
				print "<img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" title=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\" 
						onClick=\"parent.location='index.php?option=pagemanager&suboption=editsection&section=$section_list[0]&site=$site';\">";
			}

			print "</td>
					   <td height=\"18\" align=\"right\">";
			if ($section_list[0] == $section)
			{
				print "<img border=\"0\" src=\"graphics/folder_open.gif\" width=\"16\" height=\"16\">";
			}
			else
			{
				print "<img border=\"0\" src=\"graphics/folder_closed.gif\" width=\"16\" height=\"16\">";
			}
			print "</td>
							  <td colspan=\"2\" height=\"18\">
							  <span class=\"link\">
							  <a href=index.php?option=pagemanager&section=$section_list[0]&site=$site>";
			if ($section_list[3] == "yes")
			{
				print " $section_list[1]</a> ($section_list[0])</span> <span class=text>(" . $sas_lang['invisible'] . ")</span>";
			}
			else
			{
				print "$section_list[1]</a> ($section_list[0])</span>";
			}
			print "</td><td colspan=\"3\" height=\"18\"></td>
							  </tr>";

			if ($section_list[0] == $section)
			{
				$query1 = "SELECT id, category FROM $table_pages WHERE section = '$section' GROUP BY category"; // getting catlist
				$result2 = mysql_query($query1) or die_script($query1, mysql_error());

				while ($cat_list = mysql_fetch_array($result2)) //loop through categories
				{

					$category1 = htmlspecialchars(rawurlencode($cat_list[1]));
					if (!empty ($cat_list[1])) //only display when categrory has no blanks
					{
						print "<tr>
									  <td colspan=\"5\" height=\"18\"></td>
									  <td height=\"18\">&nbsp</td>
									  <td><span class=\"link\"><a href=index.php?option=pagemanager&category=$category1&section=$section&site=$site>$cat_list[1]</span></td>
									  <td colspan=\"4\"></td></tr>";
					}
					if (addslashes($cat_list[1]) == $category)
					{
						$query1 = "SELECT id, name, module, invisible, sort FROM $table_pages WHERE category = '$category' and section = '$section' ORDER BY sort"; // getting pagelist
						$result1 = mysql_query($query1) or die_script($query1, mysql_error());
						while ($page_list = mysql_fetch_array($result1)) //loop through pages
						{
							print "<tr>
											  <td  colspan=\"7\" height=\"18\">&nbsp;</td>
											  <td height=\"18\"><font color=\"#466C9B\">";
							if ($page_list[0] == $section_list[2])
							{
								if ($highvisibility == "1")
								{
									print "<td><span class=link>[Hm]</span></td>";
								}
								else
								{
									print "<img border=\"0\" src=\"graphics/startsection.gif\" title=\"" . $sas_lang['startpage'] . "\" alt=\"" . $sas_lang['startpage'] . "\" width=\"16\" height=\"16\">";
								}
							}
							else
							{
								if ($highvisibility == "1")
								{
									print "<td><span class=text><a href=index.php?option=pagemanager&section=$section&setstartpage=$page_list[0]&site=$site>[set Hm]</span></a></td>";
								}
								else
								{
									print "<img border=\"0\" src=\"graphics/not_startsection.gif\" title=\"" . $sas_lang['startpage'] . "\" alt=\"" . $sas_lang['startpage'] . "\" width=\"16\" height=\"16\" onMouseOver=\"this.src='graphics/startsection.gif'\"  onMouseOut=\"this.src='graphics/not_startsection.gif'\" onClick=\"parent.location='index.php?option=pagemanager&section=$section&setstartpage=$page_list[0]&site=$site';\">";
								}
							}
							print "</td>
											  <td height=\"18\">";
							if ($highvisibility == "1")
							{
								print "<span class=text><a href=\"index.php?option=pagemanager&section=$section&category=$category&deletepage=$page_list[0]&site=$site\">[Dl]</a> </span>";
							}
							else
							{
								print "<img border=\"0\" src=\"graphics/delete.gif\" width=\"16\" title=\"" . $sas_lang['delete'] . "\" 
													  alt=\"" . $sas_lang['delete'] . "\" height=\"16\" onClick=\"if(confirm('" . $sas_lang['page_delete_confirm'] . "')) parent.location='index.php?option=pagemanager&section=$section&category=$category&deletepage=$page_list[0]&site=$site';\">";
							}
							print "</td><td height=\"18\" align=\"right\">";
							if ($page_list[2] == "") // print normal or module icon
							{
								print "<a href=index.php?option=pagemanager&suboption=editpage&page=$page_list[0]&section=$section_list[0]&site=$site>
											   <img border=\"0\" src=\"graphics/document.gif\" width=\"16\" height=\"16\" title=\"" . $sas_lang['change_template_page'] . "\" alt=\"" . $sas_lang['change_template_page'] . "\"></a>";

								// by JG Preview-Button
								if ($site == "protected")
								{
									print "<a href=\"../index.php?page=$page_list[0]&section=$section&site=protected\" target=\"_blank\">
													   <img src=\"graphics/view.gif\" border=\"0\" title=\"" . $sas_lang['student_view'] . "\" alt=\"" . $sas_lang['student_view'] . "\"></a>";
								}
								else
								{
									unset ($_SESSION['site']);
									print "<a href=\"../index.php?page=$page_list[0]&section=$section\" target=\"_blank\"><img src=\"graphics/view.gif\" border=\"0\" 
												title=\"" . $sas_lang['student_view'] . "\" alt=\"" . $sas_lang['student_view'] . "\"></a>";
								}

								if ($page_list[3] == "yes")
								{
									print "</td><td height=\"18\"><span class=\"link\"><a href=index.php?option=pagemanager&suboption=editpage&page=$page_list[0]&section=$section&site=$site>($page_list[4]) $page_list[1]</a> ($section_list[0],$page_list[0])</span><span class=text> (" . $sas_lang['invisible'] . ")</span></td></tr>";
								}
								else
								{
									print "</td><td height=\"18\"><span class=\"link\"><a href=index.php?option=pagemanager&suboption=editpage&page=$page_list[0]&section=$section&site=$site>($page_list[4]) $page_list[1]</a> ($section_list[0],$page_list[0])</span></td></tr>";
								}
							}
							else
							{
								// by JG link to module management
								$module_location = substr($page_list[2], 0, strpos($page_list[2], "/"));
								$query3 = "SELECT id FROM $table_modules where location='$module_location'";
								$result3 = mysql_query($query3) or die_script($query3, mysql_error());
								if (mysql_num_rows($result3) > 0)
								{
									$module_id = mysql_result($result3, 0);
									print "<a href=index.php?option=modulemanager&module=$module_id>
												<img border=\"0\" src=\"graphics/module.gif\" alt=\"" . $sas_lang['modulemanager'] . "\" title=\"" . $sas_lang['modulemanager'] . "\" width=\"16\" height=\"16\"></a>";
								}
								else
								{
									print "<img border=\"0\" src=\"graphics/module.gif\" width=\"16\" height=\"16\">";
								}
								// by JG Preview-Button
								if ($site == "protected")
								{
									print "<a href=\"../index.php?page=$page_list[0]&section=$section&site=protected\" target=\"_blank\"><img src=\"graphics/view.gif\" border=\"0\" title=\"" . $sas_lang['student_view'] . "\" alt=\"" . $sas_lang['student_view'] . "\"></a>";
								}
								else
								{
									unset ($_SESSION['site']);
									print "<a href=\"../index.php?page=$page_list[0]&section=$section\" target=\"_blank\"><img src=\"graphics/view.gif\" border=\"0\" title=\"" . $sas_lang['student_view'] . "\" alt=\"" . $sas_lang['student_view'] . "\"></a>";
								}
								if ($page_list[3] == "yes")
								{
									print "</td><td height=\"18\"><span class=\"link\">
												 <a href=index.php?option=pagemanager&suboption=editmodpage&page=$page_list[0]&section=$section_list[0]&site=$site>($page_list[4]) $page_list[1]</a> ($section_list[0],$page_list[0])</span><span class=text> (" . $sas_lang['invisible'] . ")</td></tr>";
								}
								else
								{
									print "</td><td height=\"18\"><span class=\"link\">
												 <a href=index.php?option=pagemanager&suboption=editmodpage&page=$page_list[0]&section=$section_list[0]&site=$site>($page_list[4]) $page_list[1]</a> ($section_list[0],$page_list[0])</span></td></tr>";
								}
							}
						} // end page list 
					} // end of cat list

				}
				print "<tr>
								<td colspan=\"11\" height=\"18\"></td>
								</tr>";
				print "   <tr>
								  <td colspan=\"9\" height=\"18\"></td>
								  <td height=\"18\">
								  <img border=\"0\" src=\"graphics/document.gif\" width=\"16\" height=\"16\"></td>
								  <td height=\"18\"><span class=\"text\"><a href=index.php?option=pagemanager&suboption=newpage&section=$section_list[0]&site=$site>" . $sas_lang['newpage'] . "</a><span></td>
								  <td colspan=\"1\" height=\"18\"></td>
								</tr>
								<tr>
								  <td colspan=\"11\" height=\"18\"></td>
								</tr>";

			}

		} // end of section security
	} // end of section list
}

if ((!IsSet ($section)) AND ($user_sections[0] == "")) // if no section is set view new section option
{ // if $user_sections[0] is empty user is allowed to create new section
	print "<tr><td height=\"18\" colspan=\"6\"></td></tr><tr>
				  <td height=\"18\" colspan=\"4\"></td>
				  <td height=\"18\"><font size=\"2\" color=\"#466C9B\">
				  <img border=\"0\" src=\"graphics/folder_closed.gif\" width=\"16\" height=\"16\"></font></td>
				  <td height=\"18\"><span class=\"text\"><a href=index.php?option=pagemanager&suboption=newsection&site=$site>" .
	$sas_lang['newsection'] . "<span></td>
			</tr>";

}

print "</table><br><br>";
if (mysql_num_rows($result) < 1)
{
	print "<span class=text>" . $sas_lang['nosections'] . "</span>";
}
include ("interface/pageend.inc.php");
include ("interface/footer.inc.php");
?>