Source

Selador / berichte.php

<?php

/* berichte.php - Frontend for "Berichte" page
 *
 * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
 *
 * This file is part of Selador, a browser-based fantasy strategy game
 *
 * This program is distributed under the terms of the GNU Affero General Public License.
 *
 *
 *   Selador is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   Selador 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 Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with Selador.  If not, see <http://www.gnu.org/licenses/>.
 **/

// TODO: Well this code just sucks and needs to be rewritten. Don't even try to modify it, just rewrite it please :)

require_once("php/base/user.inc.php");
require_once("php/base.php");
require_once("php/event.php");
require_once("headers.php");
require_once ("unitmap.php");
require_once ("php/base/ressourceMap.php");
require_once ("php/base/display_functions.php");

$me->activevillage->update_stamp ();

$spy_columns=5;

//TODO
//gefangenenberichte
?>

<script type="text/javascript">

function check_all ()
{
	var formElem = document.getElementById ("msgdel");
	var allCheckElem = document.getElementById ("allchecker");

	var boxCounter = 0;
	var boxElem;
	while ((boxElem = formElem.elements[boxCounter]))
	{
		if (boxElem.type == "checkbox")
			boxElem.checked = allCheckElem.checked;
		boxCounter++;
	}
}
</script>
<?php
// We include basic design parts here
require_once ("styles/php/base_design-".$_SESSION['theme'].".php");

if (isset ($_REQUEST['f']))
  $folder = (int)$_REQUEST['f'];
else
  $folder = 1;

if (isset ($_REQUEST['p']))
  $page = max (0, (int)$_REQUEST['p']);
else
  $page = 0;

$msgs_per_page = 14;

?>
<!--<div id="reiter">
<table class="inhalt"  cellspacing="0" width="100%"><tr>
<?php
	echo "<th>"; link_mark ($folder, 1, "Alle Berichte"); echo "</th>";
	echo "<th>"; link_mark ($folder, 2, "Angriffe"); echo "</th>";
	echo "<th>"; link_mark ($folder, 3, "Verteidigung"); echo "</th>";
	echo "<th>"; link_mark ($folder, 4, "Spähen"); echo "</th>";
	echo "<th>"; link_mark ($folder, 5, "Handel"); echo "</th>";
	echo "<th>"; link_mark ($folder, 7, "Archiv"); echo "</th>";
?>
</tr>
</table>
</div>-->
<div id="contentbox">
<div id="onecolumn">



<?php


$go_on = true;

if (isset ($_REQUEST['id']))
{
	$id = (int)$_REQUEST['id'];




	$query = "select A.touid, A.toaid, A.subject, date_format(A.stamp, \"%d.%c.%Y\") as date, date_format(A.stamp, \"%H:%i:%s\") as time, A.rep_read, A.touid, A.battleid, A.uid1, A.village1, A.type, A.msg as trade_msg, B.user as from_who, B.aid as from_ally, C.name as from_where, C.x as from_x, C.y as from_y, A.uid2, A.village2, D.aid as to_ally, D.user as to_who, D.race, E.name as to_where, E.x as to_x, E.y as to_y, A.type, A.stamp, A.rep_read, F.picture as from_pic, G.picture as to_pic from reports A left join user B on (A.uid1=B.uid) left join village C on (A.village1=C.villageid) left join user D on (A.uid2=D.uid) left join village E on (A.village2=E.villageid) left join profile F on (A.uid1=F.userid) left join profile G on (A.uid2=G.userid) where reportid=".$id;

	//echo $query;

	if (($res = mysql_query ($query)))
	{
		if (!($report_info = mysql_fetch_array ($res)))
		{
			echo "<b>Bericht nicht gefunden oder existiert nicht!</b>";
			$go_on = false;
		}
		else
		{
			//merchant report
			if ($report_info['type'] == 5)
			{
				$report = new MerchantReport ($id);
			}
			else
			{
				if ($report_info['type'] == 1)
					$report = new SpyReport($id);
				else
					$report = new BattleReport ($id);
			}
			//$report = new Report ($id);
			$report->readFromDatabase($report_info);
		}
	}
	else
	{
		echo "<b>Fehler beim Lesen des Berichts :(</b>";
		$go_on = false;
		log_err ("query failed in berichte.php: ".$query.": ".mysql_error());
	}

    $is_partner = in_array($report_info['toaid'], $me->allymeta);

    if (!
        (
            $report_info['touid'] == $me->uid
            || ($report_info['toaid'] == $me->aid && $me->aid !=0)
            || $is_partner
            || $me->uid == $support_uid
        ))
	{
		  echo "<b>Dieser Bericht ist nicht für Dich!</b>\n";
		  log_info ("Spieler ".$me->uid." wollte Bericht ".$id." lesen, der Spieler ".$report_info['touid']." gehört");
		  $go_on = false;
	}


}
else
{
	if ($folder == 6)
		$folder = 1;
}

if (isset($_REQUEST['msg_action']) && ($_POST['delete_all_really'] == "Nein, abbrechen"))
{
	unset ($_REQUEST['msg_action']);
}

if (isset ($_REQUEST['msg_action']))
{
	if ($_POST['msg_action'] == "Alle löschen" && !isset($_POST['delete_all_really']))
	{
		echo '
			<form name="deleteAll" method="POST" action="berichte.php?f='.$folder.'">
			<input type="hidden" name="msg_action" value="Alle löschen">
			<b><span id="bug">Willst du wirklich alle Berichte l&ouml;schen?</span></b><br><br>
			<input type="submit" name="delete_all_really" value="Ja, jetzt löschen">&nbsp;&nbsp;
			<input type="submit" name="delete_all_really" value="Nein, abbrechen"></form><br><br>';

	}

	if (($_REQUEST['msg_action'] == "Archivieren") ||
		($_REQUEST['msg_action'] == "Löschen") || (
		($_REQUEST['msg_action'] == "Alle löschen") &&
		($_POST['delete_all_really'] == "Ja, jetzt löschen")) ||
		($_REQUEST['msg_action'] == "Alle gelesen"))
	{
		if (isset ($_REQUEST['id']))
		{
			$ids[0] = $_REQUEST['id'];
		}

		if (isset ($_REQUEST['ids']))
			$ids = $_REQUEST['ids'];

		if (isset ($ids)|| $_REQUEST['msg_action'] == "Alle löschen" || $_REQUEST['msg_action'] == "Alle gelesen")
		{
			if ((is_array ($ids) && (count($ids) > 0)) || $_REQUEST['msg_action'] == "Alle löschen" || $_REQUEST['msg_action'] == "Alle gelesen")
			{
				if ($_REQUEST['msg_action'] == "Archivieren")
				  $setmode = 1;
				else
				  $setmode = 2;

				// Check how many where unread so we can decrease unread report count
				$query = "select count(reportid) from reports ";
				if (($_REQUEST['msg_action'] != "Alle löschen") && ($_REQUEST['msg_action'] != "Alle gelesen"))
					$query .= "where reportid in (".implode (",", $ids).") ";
				else
				{
					if ($folder == 1)
					{
						$type="in (1,2,3,5)";
					}
					else
					{
						if ($folder == 4)
							$type="=1";
						else
						{
							if ($folder == 7)
								$type="false";
							else
								$type = "=".$folder;
						}
					}
					$query .= "where type ".$type;//ausser f=1--> kein Type, f=4-->type=1, f= 7
				}
				$query .= " and archived != 2 and rep_read=0 and touid=".$me->uid;
				if (!($res = mysql_query ($query)))
					log_err ("Cannot fetch count of unread reports when deleting/archiving reports. Query: ".$query.", ".mysql_error());
				else
				{
					$row = mysql_fetch_row($res);
					if ($row[0] > 0)
					{
						$query = "update user set reports=".max (0, ($user_row[2]-$row[0]))." where uid=".$me->uid;
						if (!mysql_query ($query))
							log_err ("Cannot decrease unread report count for user. Query: ".$query.", ".mysql_error());
					}
				}
				if ($_REQUEST['msg_action'] == "Alle gelesen")
				{
					$query = "update reports set rep_read=1 where type ".$type." AND archived!=1 and touid=".$me->uid;
				}
				else
				{
					 $query = "update reports set archived=".$setmode." where touid=".$me->uid." and ";
					if ($_REQUEST['msg_action'] != "Alle löschen")
						$query .= "reportid in (".implode (",", $ids).")";
					else
						$query .= " type ".$type." AND archived!=1";
				}
				if (!mysql_query ($query))
					log_err ("Cannot set reports to ".$_REQUEST['msg_action']." (".$setmode."). Query: ".$query.", ".mysql_error());
			}
		}
	}
}

// side is 0 for attacker before, 1 after combat
// 2 for defender before, 3 after combat
if ($folder == 6)
{
	require_once("reports/reports_show.php");
}
else
{
	require_once("reports/reports_list.php");
}



echo "</div></div>";
require_once("footer.php");

?>