<?php

#############################################################################
#                              Site@School
# Site@School is a website management system designed for schools.
#
# About Site@School:
# Original Idea: Mark de Haan (mark@starwave.nl)
# PHP coding & Design: Mark de Haan (mark@starwave.nl)
# Comissioned by: Stadsdeel de Baarsjes, Amsterdam, Netherlands
############################################################################
# view_links.php: last changed 24/05/2006  S@S version 2.4
#            by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: view_links.php,v 1.2 2006/10/15 19:08:54 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 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");
}
if (IsSet ($_GET['option']))
{
	$option = $_GET['option'];
}

$links_table = $dbprefix . "m_links";
$links_table_cat = $dbprefix . "m_links_cat";

load_language($cmsdir . '/', 'sn_links'); //load language file
?>
<script language="JavaScript" type="text/javascript">
function checkinput ( form )
{
    if (form.link_title.value == "") 
	{
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.link_title.focus();
    return false ;
    }
    if (form.link_url.value == "http://") 
	{
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.link_url.focus();
    return false ;
    }
    if (form.link_description.value == "") 
	{
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.link_description.focus();
    return false ;
    }
	if (form.link_name.value == "") 
	{
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.link_name.focus();
    return false ;
    }
	if (form.link_email.value == "") 
	{
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.link_email.focus();
    return false ;
    }
  return true ;
}

</script>
<?php


if ($option == "save_link")
{
	if (IsSet ($_POST['link_url']))
	{
		$link_url = $_POST['link_url'];
		$topicdetail = htmlspecialchars(strip_tags($topicdetail)); //remove html tags
	}
	if (IsSet ($_POST['link_name']))
	{
		$link_name = $_POST['link_name'];
		$topicdetail = htmlspecialchars(strip_tags($topicdetail)); //remove html tags
	}
	if (IsSet ($_POST['link_email']))
	{
		$link_email = $_POST['link_email'];
		$topicdetail = htmlspecialchars(strip_tags($topicdetail)); //remove html tags
	}

	$link_description = sql_addslashes($_POST['link_description']);
	$topicdetail = htmlspecialchars(strip_tags($topicdetail)); //remove html tags
	$link_title = addslashes($_POST['link_title']);

	$query = "SELECT id, owner_email FROM $links_table_cat WHERE pagename = '$pagetitle'";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$link_pageid = mysql_result($result, 0, 0);
	$email_owner = mysql_result($result, 0, 1);

	mysql_query("INSERT INTO $links_table (id, title, url, description, pagename, initial)
		VALUES (NULL, '$link_title', '$link_url', '$link_description', '$link_pageid', '0')");

	if ($email_owner != "")
	{
		$message_to_sent = "<html><body><div style='margin-top:-300px'>
								<table width=80%>
								<tr><td colspan=\"2\">" . $sas_lang['links_module_email_intro'] . "</td></tr>
								<tr><td width=20%>" . $sas_lang['links_module_pagename'] . "</td>
								<td><b>$pagetitle</b></td></tr>
								<tr><td width=20%>" . $sas_lang['links_module_title'] . "</td>
								<td><b>$link_title</b></td></tr>
								<tr><td>" . $sas_lang['links_module_url'] . " </td>
								<td><b>$link_url</b></td></tr>
								<tr><td>" . $sas_lang['links_module_description'] . " </td>
								<td><b>$link_description</b></td></tr>
								<tr><td>" . $sas_lang['email'] . " </td>
								<td><b>$link_email</b></td></tr>
								<tr><td>" . $sas_lang['links_module_name'] . " </td>
								<td><b>$link_name</b></td></tr>
								<tr><td>" . $sas_lang['links_module_ip'] . " </td>
								<td><b>$ip</b></td></tr>
								</table>
								</body></html>";
		$message_to_sent = nl2br($message_to_sent);
		require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
		$mail = new PHPMailer();
		$mail->From = $email; //site email address
		$mail->FromName = $sitename;
		$mail->Subject = $sas_lang['links_module_email_subject'] . " " . $pagetitle;
		$mail->Body = $message_to_sent;
		$mail->IsHTML(true);
		$mail->AddAddress($email_owner); //add email address links module owner
		$mail->Send();
	}
	print "<script language=\"Javascript\">
		   <!--
		   alert (\"" . $sas_lang['links_module_propose_link_send'] . "\");
		   window.location = 'index.php?page=$page&section=$section' ;	
		   //-->
		   </script>"; //return to the user edit window.
}

$query = "SELECT id, welcome, row_count, owner_email, icon, propose, propose_welcome, showlink FROM $links_table_cat WHERE pagename = '$pagetitle'";
$result = mysql_query($query) or die_script($query, mysql_error());
$RecordCount = mysql_num_rows($result);
if ($RecordCount > 0)
{
	$cat_id = mysql_result($result, 0, 0);
	$welcome = mysql_result($result, 0, 1);
	$row_count = mysql_result($result, 0, 2);
	$email_owner = mysql_result($result, 0, 3);
	$link_icon = mysql_result($result, 0, 4);
	$link_propose = mysql_result($result, 0, 5);
	$showlink = mysql_result($result, 0, 7);

	if ($option == "propose")
	{
		$propose_welcome = mysql_result($result, 0, 6);
		print "<form METHOD=\"POST\" enctype=\"multipart/link-data\" ACTION=\"index.php?page=$page&section=$section&option=save_link\" 
		       onsubmit=\"return checkinput(this);\">";
		print "<table cellspacing=\"0\" cellpadding=\"0\">";
		//print "<tr><td colspan=\"2\" align=\"center\"><h2>".$sas_lang['links_module_propose_button']."</h2></td></tr>";
		print "<tr><td colspan=\"2\">$propose_welcome</td></tr>";
		print "<tr><td colspan=\"2\">&nbsp;</td></tr>";
		print "<tr><td><b>" . $sas_lang['links_module_title'] . "&nbsp;(*)</b></td>
					<td><input type=\"text\" size=50 class=\"textfield\" name=\"link_title\"></td></tr>";
		print "<tr><td colspan=\"2\">&nbsp;</td></tr>";
		print "<tr><td><b>" . $sas_lang['links_module_url'] . "&nbsp;(*)</b></td>
					<td><input type=\"text\" size=50 class=\"textfield\" name=\"link_url\" value=\"http://\"></td></tr>";
		print "<tr><td colspan=\"2\">&nbsp;</td></tr>";
		print "<tr><td valign=\"top\"><b>" . $sas_lang['links_module_description'] . "&nbsp;(*)</b></td>
					<td><textarea cols=\"50\" rows=\"4\" name=\"link_description\"></textarea></td></tr>";
		print "<tr><td colspan=\"2\">&nbsp;</td></tr>";
		print "<tr><td><b>" . $sas_lang['links_module_name'] . ":&nbsp;(*)</b></td>
					<td><input type=\"text\" size=50 class=\"textfield\" name=\"link_name\"></td></tr>";
		print "<tr><td colspan=\"2\">&nbsp;</td></tr>";
		print "<tr><td><b>" . $sas_lang['email'] . ":&nbsp;(*)</b></td>
					<td><input type=\"text\" size=50 class=\"textfield\" name=\"link_email\"></td></tr>";
		print "<tr><td colspan=\"2\">&nbsp;</td></tr>";
		print "<tr><td>" . $sas_lang['required_fields'] . "</td><td>&nbsp;</td></tr>";
		print "<tr><td colspan=\"2\">&nbsp;</td></tr></table>";
		print "<input type=\"submit\" value=\"&nbsp;&nbsp;$sas_lang[send]\" name=\"savebutton\">";
		print "</form>";
	}
	else
	{
		print "<center>" . $welcome . "\n<br></center><br>";

		print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";

		$row_percetage = round(100 / $row_count);
		$query = "SELECT category FROM $links_table WHERE pagename = '$cat_id' AND initial = 1 GROUP BY category";
		$result = mysql_query($query) or die_script($query, mysql_error());
		while ($cat_list = mysql_fetch_array($result))
		{
			$i = 0;
			$j = 0;
			$query2 = "SELECT id, title, url, description, category FROM $links_table WHERE pagename = '$cat_id' AND initial = 1 AND category = '$cat_list[0]' ORDER BY sort";
			$result2 = mysql_query($query2) or die_script($query2, mysql_error());
			while ($links_list = mysql_fetch_array($result2))
			{
				if ($j == 0)
				{
					print "<tr><td colspan=\"$row_count\"><b>$links_list[4]<b></td></tr>"; //print category
					$j = 1;
				}
				$i++;
				if ($i == 1)
				{
					print "<tr>";
				}
				print "<td valign=\"top\" width=\"$row_percetage%\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"2\">
									<tr>";
				print "<td width=\"20\">
										<img border=\"0\" src=\"" . $link_icon . "\"></td><td><a href=\"$links_list[2]\" target=\"_blank\">$links_list[1]</a>
										</td></tr>";
				if ($showlink == "1")
				{
					print "<tr><td>&nbsp;</td>
											<td>
											<a href=\"$links_list[2]\" target=\"_blank\">($links_list[2])</a>
											</td></tr>";
				}
				if ($links_list[3] != "")
				{
					print "<tr><td>&nbsp;</td>
											<td>
											$links_list[3]
											</td></tr>";
				}
				print "</table><br></td>";

				if ($i == $row_count)
				{
					print "</tr>";
					$i = 0;
				}

			}

		}
		if ($link_propose == "1")
		{
			print "<tr><td colspan=\"$row_count\">&nbsp;</td></tr>";
			print "<tr><td colspan=\"$row_count\" align=\"left\"><input type=\"button\" value=\"" . $sas_lang['links_module_propose_button'] . "\"
				onClick=\"javascript:parent.location='index.php?section=$section&page=$page&option=propose';\"></td></tr>";
		}
		print "</table>";
	}
}
else
{
	print $sas_lang['links_module_page_error'];
}
?>