Source

Selador / bote.php

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
<?php

/* bote.php - Front-end for "Bote" 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: Hyprit has a nicely re-written but basic version of this. Bug him about it :)
// This code is in working condition but old and hastily put together.

require_once("php/base/world.inc.php");
require_once("php/base.php");
require_once("php/event.php");

// Don't auto-reload any of these pages when a counter expires.
$no_reload = 1;

require_once("headers.php");

$me->activevillage->update_stamp ();

?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="/scripts/jquery.autocomplete.js"></script>
<link rel="stylesheet" href="/styles/jquery.autocomplete.css" type="text/css" />
<script type="text/javascript" src="/styles/jquery.bgiframe.min.js"></script>
<script type="text/javascript">

   <!--

var allymsg = false;
var data = null;
function toggleAllyMsg ()
{
	if (allymsg == true)
		allymsg = false;
	else
		allymsg = true;
		
	var more = document.getElementById("more");
	var img = document.getElementById("addimg");
	more.innerHTML = "";
	var rec = document.getElementById("recipient0");
	
	if (allymsg)
	{
		rec.value = "[Ally]";
		rec.setAttribute('readonly', false, 'true');
		rec.className = "disabled";
		var err = document.getElementById("errormsg");
		err.innerHTML ="";
		img.innerHTML = "<img src=\"styles/<?php echo $_SESSION['theme']?>/add_disabled.png\" onclick=\"addRec()\">";
		reccounter = 1;
		
	}
	else	
	{
		rec.value = "";
		rec.removeAttribute("readonly");
		rec.className = "defaults";
		img.innerHTML = "<img src=\"styles/<?php echo $_SESSION['theme']?>/add.png\" onclick=\"addRec()\">";

	}
}

var reccounter = 1;
function addRec ()
{
	//alert("hurra");
	var elem = document.getElementById("more");

	if (!allymsg)
	{
		{
			var id = '#recipient'+reccounter;

			var ni = document.getElementById('more');
			var newdiv = document.createElement('div');
			var divIdName = 'more'+reccounter;
			newdiv.setAttribute('id',divIdName);
			var toBeAppended = "";

			if (reccounter >= 5)
			{
				var errormsg = document.getElementById("errormsg");
				errormsg.innerHTML = '<div id="errormsg"><span class="error">Du kannst h&ouml;chstens f&uuml;nf Empf&auml;nger eingeben.</span></div>';
			}
			else
			{
				toBeAppended = '<input type="text" size="20" name="recipient['+reccounter+']" id="recipient'+reccounter+'" value=""><br>';
				newdiv.innerHTML = toBeAppended;
				ni.appendChild(newdiv);
				$(id).autocomplete(data);
				reccounter++;
			}
			
		}
		/*else
		{
			elem.innerHTML = '<span class="error">Du kannst h&ouml;chstens f&uuml;nf Empf&auml;nger eingeben.</span>';
		}*/
	}
	else
	{
		elem.innerHTML = '<span class="error">Allianznachricht kann keine zusätzlichen Empfänger haben. </span>';
	}

}

<?php
if ($_REQUEST['f'] == 2)
{
?>

$(document).ready(function() {
	<?php
	//javascript autocompletion
	//preload all bus stops from the database and fill in the js data array
	$query = 'select user from user order by user asc';
	if ($res = mysql_query($query)) {
		while ($row =mysql_fetch_array($res)) {
			if (isset($datastr))
				$datastr.=',';
			$datastr .= '"'.$row['user'].'"';
		}
	}

	?>
	data = [<?php echo $datastr; ?>].sort();

	$('#recipient0').autocomplete(data);
});
// Break out of frames. This facilitates the search working as expected
if (top.location.href != location.href) {
    top.location.href = location.href;
}
<?php
}
?>
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++;
	}
}

function show_melden (submitname)
{
	var meldElem = document.getElementById ("meldarea");

	if (meldElem)
	{
		meldElem.innerHTML = '<p>Das freundliche Miteinander ist wichtig für den Spielspaß in Selador. Hier könnt ihr Beleidigungen, Diskriminierung und andere Tiefschläge melden. Auch Cheater, Skripter, Multis und Buguser können hier gemeldet werden.</p>Kurze Beschreibung des Problems:<br><textarea name="meldcont" rows="3" cols="80"></textarea><br><input type="submit" name="' + submitname + '" value="Melden" style="text-align: right;">';
	}
}
-->
</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;

?>

<div id="contentbox">
<div id="onecolumn">
<?php

// We should pass this on to the tutorial, if it is still going strong
if (is_object ($tutorial))
{
	if ($tutorial->active)
		$tutorial->is_in_messenger ($folder);
}

$msgs_per_page = 15;

/* Check for delete or archive commands */
if ((isset ($_REQUEST['msg_action'])) && ($_REQUEST['msg_action'] != "Antworten"))
{
  if (isset ($_REQUEST['o']))
    if (($_REQUEST['o'] == 3) || ($_REQUEST['o'] == 5))
    {
      $delsender = true;
    }

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


    if ($_REQUEST['msg_action'] == 'Melden')
    {
    	$query = "select A.body, A.sent, A.sender, B.user from messages A left join user B on (A.sender=B.uid) where A.id=".$id;

    	if (!($res = mysql_query ($query)))
    	{
    		$err = "Die Nachricht konnte leider nicht gemeldet werden, da ein Fehler aufgetreten ist";
    		log_err ("Error while crying about a message. Query: ".$query.", ".mysql_error ());
    	}
    	else
    	{
				if (($row = mysql_fetch_array ($res)))
				{
					$meldung = "Nachricht vom ".$row['sent'].":\n".$row['body'];
				}
				else
					$meldung = "Nachricht konnte nicht gefunden werden!!";

				$meldung = "Spieler ".$me->user." hat folgende Nachricht von Spieler ".$row['user']." (".$row['sender'].") gepetzt. Link zur Nachricht ist http://".$_SERVER['SERVER_NAME']."/".$_SERVER['PHP_SELF']."?id=".$id."\n\n".$meldung;
				
				if (isset($_REQUEST['meldcont']))
					$meldcont = mysql_real_escape_string(strip_tags(trim($_REQUEST['meldcont'])));
				else
					$meldcont = "keine Beschreibung";
				$meldung.= "\n\nBeschreibung: ".$meldcont;
				

				$query = "insert into messages values (NULL, ".$me->uid.", ".$support_uid.", \"Meldung\", \"".$meldung."\", 0, now(), 0, \"".$me->user."\", 1)";
				if (!mysql_query ($query))
				{
					$err = "Die Nachricht konnte leider nicht gemeldet werden, da ein Fehler aufgetreten ist";
					log_err ("Error while crying about a message. Query: ".$query.", ".mysql_error ());
				}
				else
				{
					$query = "update user set msgs=msgs+1 where uid=".$support_uid;

					mysql_query ($query);
				}
			}
    }

    if ($_REQUEST['msg_action'] == 'Löschen')
    {
      unset ($query);
      if ($delsender)
      {
        $query = "update messages set snd_del=1, sent=sent where id=".$id." and sender=".$me->uid;

        $folder = $_REQUEST['o'];
      }
      else
        $folder = 1;

      if (!isset ($query))
        $query = "update messages set archived=2, sent=sent where id=".$id." and recip=".$me->uid;
      @mysql_query ($query);
    }
    if ($_REQUEST['msg_action'] == 'Archivieren')
    {
      $query = "update messages set archived=1, sent=sent where id=".$id." and  recip=".$me->uid;
      @mysql_query ($query);

    }

    if ($_REQUEST['msg_action'] == 'Ungelesen')
    {
      $query = "update messages set msg_read=0, sent=sent where id=".$id." and  recip=".$me->uid;
      @mysql_query ($query);

      $folder = $_REQUEST['o'];

			$query = "select count(id) from messages where archived=0 and msg_read=0 and recip=".$me->uid;
			$res = mysql_query ($query);
			$row = mysql_fetch_row ($res);

			$query = "update user set msgs=".$row[0]." where uid=".$me->uid;
			@mysql_query ($query);

    }

  }

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

    if ($_REQUEST['msg_action'] == 'Löschen')
    {
      if ($delsender)	// FIXME EVIL SLOW do this with id in ()
        foreach ($ids as $this_id)
        {
          $query = "update messages set snd_del=1, sent=sent where id=".(int)$this_id." and sender=".$me->uid;
        	@mysql_query ($query);
        }
      else
        foreach ($ids as $this_id)
        {
          $query = "update messages set archived=2, sent=sent where id=".(int)$this_id." and recip=".$me->uid;
        	@mysql_query ($query);
        }
    }
    if ($_REQUEST['msg_action'] == 'Archivieren')
    {
      foreach ($ids as $this_id)
      {
	      $query = "update messages set archived=1, sent=sent where id=".(int)$this_id." and (sender=".$me->uid." or recip=".$me->uid.")";
      	@mysql_query ($query);
      }
    }

    $query = "select count(id) from messages where archived=0 and msg_read=0 and recip=".$me->uid;
    $res = mysql_query ($query);
    $row = mysql_fetch_row ($res);

    $query = "update user set msgs=".$row[0]." where uid=".$me->uid;
    @mysql_query ($query);
  }

  if (isset ($_REQUEST['o']))
    $folder = $_REQUEST['o'];
  else
    $folder = 1;
}

$no_go = false;
/* First, check for outgoing message data */
if (isset ($_REQUEST['msg_submit']))
{
	if (isset ($_REQUEST['c']) && ($_REQUEST['c'] == $me->reload_counter))
	{
		$me->reload_counter++;
		$recipients = $_REQUEST['recipient'];
		$empfaenger = false;
		foreach ($recipients as $idx => $rec)
		{
			if (strlen($rec) > 3)
			{
				if ($rec != "[Ally]")
					$empfaenger = true;
			}
		}
		if (($empfaenger == true) && ($_POST['to_all_members'] == "ON"))
		{
			$no_go = true;
			echo "<span class=\"error\">Du hast einen Empf&auml;nger eingegeben und zus&auml;tzlich \"Allianz-Rundmail verschicken\" angew&auml;hlt. Es geht aber nur eins von beidem!</span>";
		}
		else
		{
			if ($empfaenger == true)
			{
				$counter = 0;
				foreach ($recipients as $idx => $rec)
				{
					if ($counter > 5)
						break;
					if (strlen (trim ($rec)) > 0)
					{
						$query = "select uid from user where user=\"".trim($rec)."\"";
						$res = mysql_query ($query);
						if ($row = mysql_fetch_row ($res))
						{
							$recuid[] = $row[0];
							$recipients[$idx] = mysql_real_escape_string (trim ($rec));
						}
						else
						{
							unset ($recipients[$idx]);
							echo "<span class=\"error\">Empfänger ".$rec." nicht gefunden :(</span>\n";
							$no_go = true;
						}
					}
				}
				if (!$no_go)
				{
					if (isset ($_REQUEST['subject']))
					{
						$subject = mysql_real_escape_string (trim (strip_tags ($_REQUEST['subject'])));
						if (strlen ($subject) < 1)
							$subject = "Kein Betreff";

						if (isset ($_REQUEST['body']))
						{

							if (stristr($_REQUEST['body'], "<" ))
							{
								echo "<b>Fehler: Die Nachricht enth&auml;t ung&uuml;ltige Zeichen (<).<br> </b>";
								$no_go = true;
							}
							$body = mysql_real_escape_string (trim (strip_tags ($_REQUEST['body'])));
							//echo $body;

							if (strlen ($body) < 3)
							{
								echo "<b>Nachricht leer?</b>\n";
								$no_go = true;
							}

							if ($me->sat_by)
								$body = "Diese Nachricht wurde von einem Sitter verfasst:\n".$body;
						}
						else
						{
							echo "<b>Nachrichtentext kaputt??</b>\n";
							$no_go = true;
						}
					}
					else
					{
						echo "<b>Betreff defekt???</b>\n";
						$no_go = true;
					}
				}
			}
			else
			{
				//allianzrundmail
				if ($_POST['to_all_members'] != "ON")
				{
					echo "<span class=\"error\">Empfänger eingeben</span>";
					$no_go = true;
				}
				else
				{
					if (!$me->aid)
						echo "<span class=\"error\">Es ist ein Fehler beim Versenden der Nachricht aufgetreten!</span>";
					else
					{
						if (strlen($_REQUEST['subject']) > 0)
							$subject = "[Ally]".mysql_real_escape_string(strip_tags($_REQUEST['subject']));
						else
							$subject = "[Ally]kein Betreff";
						$already_sent = true;
						$query_select = "select uid,user from user where aid=".$me->aid." and uid!=".$me->uid;
						if (!$res = mysql_query($query_select))
							echo "<span class=\"error\">Es ist ein Fehler beim Versenden der Nachricht aufgetreten!</span>";
						else
						{

							if ($me->sat_by)
								$_REQUEST['body'] = "Diese Nachricht wurde von einem Sitter verfasst:\n".$_REQUEST['body'];
							//$body = mysql_real_escape_string(trim(strip_tags($REQUEST['body'])));

							$query_anfang = "insert into messages (sender,recip,subject,body,msg_read,sent,archived,name,snd_del) values ";
							while ($row = mysql_fetch_array($res) )
							{
								if (isset($query_rundmail))
									$query_rundmail .= ",";
								$query_rundmail .= "(".$me->uid.",".$row['uid'].",'".$subject."','".mysql_real_escape_string(trim(strip_tags($_REQUEST['body'])))."',0,now(),0,'".mysql_real_escape_string($me->user)."',0)";
							}
							$query_ally =  $query_anfang . $query_rundmail;
							if (!mysql_query($query_ally))
							{
								echo "<span class=\"error\">Es ist ein Datenbankfehler aufgetreten</span>";
								$me->log_err ("Cannot send ally mail. Query: ".$query_ally.", ".mysql_error ());
							}
							else
							{
								echo "<span class=\"success\">Erfolgreich verschickt</span>";
								$query = "update user set msgs=msgs+1 where aid=".$me->aid." and uid!=".$me->uid;

								if (!mysql_query ($query))
									$me->log_err ("Cannot update msgs after sending ally mail. Query: ".$query.", ".mysql_error ());
							}
							$folder=1;
							$already_sent = true;
						}
					}
				}
			}
		}

		if (!$no_go)
		{
			if (!$already_sent)
			{
				foreach ($recuid as $idx => $this_recip)
				{
					$query = "insert into messages (sender, recip, name, subject, body) values (".$me->uid.", ".$this_recip.", \"".mysql_real_escape_string($me->user)."\", \"".$subject."\", \"".$body."\")";
					if (@mysql_query ($query))
					{
						$me->reload_counter++;
						$folder = 1;
						$query = "update user set msgs=msgs+1 where uid=".$this_recip;
						@mysql_query ($query);
					}
					else
					{
						echo "<b>Nachricht konnte nicht an alle gesendet werden :(</b>\n";
						$folder = 2;
					}
				}
			}
			else
				$folder = 1;

		}
		else
		  $folder = 2;
  }
  else
  {
    $folder = 1;
  }
}

switch ($folder)
{
  case 1:
    $query = "select count(id) from messages where archived=0 and recip=".$me->uid;
    $res = mysql_query ($query);
    $row = mysql_fetch_row ($res);
    $num_msgs = $row[0];
    $pages = (int)(($num_msgs-1) / $msgs_per_page);

    if ($page-1 > $pages)
      $page = $pages;

    // FIXME: Left joins here, or messages won't be displayed if user deletes himself!
    $query = "select A.id, A.sender, A.subject, date_format(A.sent, \"%d.%c.%Y - %H:%i\") as sent, A.msg_read, B.user, B.uid, B.aid, C.tag, A.name from messages A left join user B on (A.sender=B.uid) left join allies C on (B.aid=C.aid) where archived=0 and recip=".$me->uid." order by A.sent desc limit ".($page*$msgs_per_page).",".$msgs_per_page;
    $tablehead = "<th width=\"20\" class=\"bold\">&nbsp;</th><th  class=\"bold\">Betreff</th><th class=\"bold\">Absender</th><th class=\"bold\">Ally</td><th  class=\"bold\">Gesendet</th>\n";
  case 3:
  
    if (!isset ($pages))
    {
	    $query = "select count(id) from messages where snd_del=0 and sender=".$me->uid;
	    $res = mysql_query ($query);
	    $row = mysql_fetch_row ($res);
	    $num_msgs = $row[0];
	    $pages = (int)(($num_msgs-1) / $msgs_per_page);

      if ($page-1 > $pages)
        $page = $pages;

	    $query = "select A.id, A.recip, A.subject, date_format(A.sent, \"%d.%c.%Y - %H:%i\") as sent, A.msg_read, B.user, B.uid, B.aid, C.tag, A.name from messages A  left join user B on (A.recip=B.uid) left join allies C on (B.aid=C.aid) where  snd_del=0 and sender=".$me->uid." order by A.sent desc limit ".($page*$msgs_per_page).",".$msgs_per_page;
      $tablehead = "<th width=\"24px\">&nbsp;</th><th  class=\"bold\">Betreff</th><th   class=\"bold\">Empfänger</th><th class=\"bold\">Ally</th><th  class=\"bold\">Gesendet</th>\n";


}

  case 4:
    if (!isset ($pages))
    {
	    $query = "select count(id) from messages where archived=1 and recip=".$me->uid;
	    $res = mysql_query ($query);
	    $row = mysql_fetch_row ($res);
	    $num_msgs = $row[0];
	    $pages = (int)(($num_msgs-1) / $msgs_per_page);

      if ($page-1 > $pages)
        $page = $pages;

	    $query = "select A.id, A.sender, A.subject, date_format(A.sent, \"%d.%c.%Y - %H:%i\") as sent, A.msg_read, B.user, B.uid, B.aid, C.tag, A.name from messages A left join user B on (A.sender=B.uid) left join allies C on (B.aid=C.aid) where archived=1 and recip=".$me->uid." order by A.sent desc limit ".($page*$msgs_per_page).",".$msgs_per_page;
      $tablehead = "<th class=\"bold\" width=\"24px\">&nbsp;</th><th class=\"bold\" >Betreff</th><th  class=\"bold\"  >Absender</th><th class=\"bold\">Ally</th><th  class=\"bold\">Gesendet</th>\n";
    }

//  case 5:
    /*if (!isset ($pages))
    {
	    $query = "select count(id) from messages where archived=1 and snd_del=0 and sender=".$me->uid;
	    $res = mysql_query ($query);
	    $row = mysql_fetch_row ($res);
	    $num_msgs = $row[0];
	    $pages = (int)(($num_msgs-1) / $msgs_per_page);

      if ($page-1 > $pages)
        $page = $pages;

	    $query = "select A.id, A.recip, A.subject, date_format(A.sent, \"%d.%c.%Y - %H:%i\") as sent, A.msg_read, B.user, B.uid, B.aid, C.tag from messages A left join user B on (A.recip=B.uid )  left join allies C  on (B.aid=C.aid) where archived=1 and snd_del=0 and sender=".$me->uid." order by A.sent desc limit ".($page*$msgs_per_page).",".$msgs_per_page;
      $tablehead = "<th  width=\"50%\">Betreff</th><th  width=\"120\">Empfänger</th><th>Ally</th><th width=\"120\">Gesendet</th>\n";
    }*/

    echo "<form name=\"msgdel\" id=\"msgdel\" action=\"bote.php\" method=\"POST\"><table class=\"border\" ><tr >";

    echo "<th width=\"22\" class=\"bold\">&nbsp;</th>".$tablehead."</tr>";
    

    $res = mysql_query ($query);
	$counter = 0;
    while ($row = mysql_fetch_array ($res))
    {
	if ($counter  % 2 == 0)
		$class = ' class="iterate"';
	else
		$class = '';
	$counter++;
      echo "<tr ".$class.">";
	echo "<td> ";
      if ($row['msg_read'] == 0)
		echo  '<img src="styles/'.$_SESSION['theme'].'/new.png" >';;
	echo "</td>\n";
      echo "<td ><input class=\"eingabe\" type=\"checkbox\" name=\"ids[]\" value=\"".$row['id']."\"></td>";
      

	echo "<td ><a href=\"bote.php?o=".$folder."&f=6&id=".$row['id']."\" class=\"dark\">".stripslashes ($row['subject'])."</a></td>";

      if ($row['uid'])
		echo "<td ><a href=\"profile.php?u=".$row['uid']."\">".$row['user']."</a>";
      else
      {
		if ($folder == 1)
			echo "<td >".$row['name']."";
		else
			echo "<td>(gel&ouml;scht)";
	}


	/*</td>";
      else*/
        echo "</td>";
	//if ($folder==1)
		echo "<td><a href=\"allianz.php?a=".$row['aid']."\">".$row['tag']."</a></td>";
//	if ($folder==3)
//		echo "<td><a href=\"allianz.php?a=".$row[7]."\">".$row[8]."</a></td>";
      echo "<td >".$row[3]."</td></tr>\n";
    }
    echo "</table>\n";
    echo "<input type=\"hidden\" name=\"f\" value=\"".$folder."\">\n";
    echo "<input type=\"hidden\" name=\"o\" value=\"".$folder."\">\n";
   // echo "</div>\n";

    // Correctly format and paint arrows - they shouldn't be links if we cannot scroll in this direction
    echo "</div><div id=\"leiste-unten\">";
    echo "<input class=\"eingabe\" type=\"checkbox\" id=\"allchecker\" onClick=\"check_all();\">Alle markieren&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";


    if ($page == 0)
      echo "<img src=\"styles/".$_SESSION['theme']."/pfeil_lang_links_grau.png\" alt=\"Nach links bl&auml;ttern!\" title=\"Nach links bl&auml;ttern!\">\n";
    else
      echo "<a href=\"".$_SERVER['PHP_SELF']."?p=".($page-1)."&f=".$folder."\"><img src=\"styles/".$_SESSION['theme']."/pfeil_lang_links.png\" border=\"0\" alt=\"Nach links bl&auml;ttern!\" title=\"Nach links bl&auml;ttern!\"></a>\n";
    if ($page < $pages)
      echo "<a href=\"".$_SERVER['PHP_SELF']."?p=".($page+1)."&f=".$folder."\"><img src=\"styles/".$_SESSION['theme']."/pfeil_lang_rechts.png\"  alt=\"Nach rechts bl&auml;ttern!\" title=\"Nach rechts bl&auml;ttern!\"></a>\n";
    else
    echo "<img src=\"styles/".$_SESSION['theme']."/pfeil_lang_rechts_grau.png\"  alt=\"Nach rechts bl&auml;ttern!\" title=\"Nach rechts bl&auml;ttern!\">\n";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Seite ".($page+1)." von ".($pages+1)."";
    echo "";
    if ($folder < 4)		// Cannot archive already archived messages
    {
      echo "";
    }
     echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class=\"eingabe\" type=\"submit\" name=\"msg_action\" value=\"Löschen\">";
     if ((($folder == 6 ) && $row['recip'] == $me->uid) || ($folder == 1))
     echo "&nbsp;&nbsp;&nbsp;<input class=\"eingabe\" type=\"submit\" name=\"msg_action\" value=\"Archivieren\"></form>\n";

    break;


  case 2:
    if (isset ($_REQUEST['r']))
      $recipients[] = $_REQUEST['r'];

    if (isset ($_REQUEST['subject']))
    {
	//$sub_tmp = split_string($_REQUEST['subject'], 2);
	$sub_tmp_array = explode(":", $_REQUEST['subject']);
	if (substr($sub_tmp_array[0], 0, 6) == "[Ally]")
	{
		//mixed str_replace  ( mixed $search  , mixed $replace  , mixed $subject  [, int &$count  ] )
		$sub_tmp_array[0] = str_replace("[Ally]", "", $sub_tmp_array[0]);
	}
	if (substr($sub_tmp_array[0], 0, 2) == "Re") // we answer to an answer. so we have to upgrade the counter
	{
		$counter = (int)substr($sub_tmp_array[0], 3);
		$counter++;
		$subject = "Re^".$counter;
		foreach ($sub_tmp_array as $idx => $value)
		{
			//ignore the "Re:"-entry
			if ($idx != 0)
			{
				$subject = $subject.":".stripslashes($sub_tmp_array[$idx]);
			}

		}
	}
	else // that message is the origin message. so we create the new counter
	{
		$subject = stripslashes ("Re^1: ".$_REQUEST['subject']);
	}
	//$subject = stripslashes ("Re: ".$_REQUEST['subject']);  // FIXME: Need more logic here
      }

    if (isset ($_REQUEST['body']))
    {
      if ($no_go)
      {
        $body = $_REQUEST['body'];    // This is the case if something went wrong while trying to send a submitted message
      }
      else
      {
        if (($_REQUEST['o'] == 3) || ($_REQUEST['o'] == 5))
          $body = "\n\n___________________________________________\nAm ".$_REQUEST['olddate']." schrieb ".stripslashes ($me->user).":\n".stripslashes ($_REQUEST['body']);
        else
          $body = "\n\n___________________________________________\nAm ".$_REQUEST['olddate']." schrieb ".stripslashes ($_REQUEST['r']).":\n".stripslashes ($_REQUEST['body']);
      }
    }

    echo "<div><form name=\"msg\" action=\"bote.php\" method=\"POST\"><table class=\"border\">\n";
     	echo "<tr><th class=\"bold\" colspan=\"2\">Empfänger</th></tr>";
		echo '<tr><td colspan="2"><span class="info">Du kannst mehrere Empfänger eingeben, indem du auf das Plus klickst.</span></td></tr>';
	echo "<tr><td colspan=\"2\"><input  id=\"recipient0\" name=\"recipient[0]\" value=\"".$_REQUEST['r']."\" size=\"20\">";
	echo "<div id=\"addimg\"><img src=\"styles/".$_SESSION['theme']."/add.png\" onclick=\"addRec()\"></div><div id=\"more\"></div><div id=\"errormsg\"></div></td></tr>\n";
    if ($me->aid)
    {
     	echo "<tr><td colspan=\"2\">oder <input type=\"checkbox\" name=\"to_all_members\" value=\"ON\" onclick=\"toggleAllyMsg()\">Allianz-Rundmail schreiben. ";
	echo '<span class="info">Du brauchst dann keinen Empf&auml;nger einzugeben.</span> </td></tr>';
}
	echo '<tr><th class="bold" colspan="2">Nachricht</th></tr>';
    echo "<tr><td align=\"left\">Betreff:</td><td ><input class=\"eingabe\" maxlength=\"30\" name=\"subject\" size=\"30\" value=\"".$subject."\"></td><tr>\n";
    echo "<tr><td colspan=\"3\" style=\"position:absolute; height:10px; overflow:visible;\"></td></tr>\n";
    echo "<tr><td colspan=\"3\"><textarea  name=\"body\" cols=\"80\" rows=\"13\">".$body."</textarea>\n";
    echo "<input class=\"eingabe\" type=\"submit\" name=\"msg_submit\" value=\"Absenden\"></td></tr>";
    echo "</table><input type=\"hidden\" name=\"f\" value=\"1\"><input type=\"hidden\" name=\"c\" value=\"".$me->reload_counter."\"></form>\n";
    break;

  case 6:
    if (isset ($_REQUEST['id']))
    {
      $id = $_REQUEST['id'];

      unset ($query);

      if (isset ($_REQUEST['o']))
      {
        if (($_REQUEST['o'] == 3) || ($_REQUEST['o'] == 5))
        {
          $query = "select A.*, date_format(A.sent, \"%d.%c.%Y um %H:%i\") as sent, B.user from messages A left join user B on (A.recip=B.uid) where A.id=".$id." and A.sender=".$me->uid;
          $title = "Empfänger";
        }
       }

      if (!isset ($query))
      {
        $query = "select A.*, date_format(A.sent, \"%d.%c.%Y um %H:%i\") as sent, B.user from messages A left join user B on (A.sender=B.uid) where A.id=".$id." and A.recip=".$me->uid;
        $title = "Absender";
      }

      $res = mysql_query ($query);
      if ($row = mysql_fetch_array ($res))
      {
        echo "<div><form name=\"message\" action=\"bote.php\" method=\"POST\">\n";
	      echo "<table class=\"border\">\n";
	      echo '<tr><th colspan="2" class="bold">'.stripslashes ($row['subject']).'';
	      echo "<tr><td >".$title.":</td><td >";

	      if ($row['user'])
	      	echo stripslashes ($row['user'])."</td></tr>\n";
	      else
	      	echo stripslashes ($row['name'])."</td></tr>\n";
	    //  echo "<tr><td >Betreff:</td><td >".stripslashes ($row['subject'])."</td></tr>\n";
	      echo "<tr><td >Gesendet am:</td><td>".stripslashes ($row['sent'])."</td></tr>\n";
	      echo "<tr><td colspan=\"2\" style=\"position:absolute; height:5px; overflow:visible;\"></td></tr>\n";
	      echo "<tr><td colspan=\"2\" align=\"left\"><textarea name=\"body\" cols=\"80\" rows=\"14\" readonly>".stripslashes ($row['body'])."</textarea></td></tr>\n";
	      echo "<tr><td colspan=\"2\" style=\"position:absolute; height:5px; overflow:visible;\"></td></tr>\n";

	      echo "<tr><td><input type=\"button\" onClick=\"show_melden ('msg_action');\" value=\"Melden\">&nbsp;<!--<small>Beleidigungen, Rassismus usw. melden--></small></td>\n";
	      echo "<td align=\"right\"><input type=\"submit\" name=\"msg_action\" value=\"Löschen\">&nbsp;&nbsp;";
	      echo "<input type=\"submit\" name=\"msg_action\" value=\"Ungelesen\">&nbsp;";

	      if (($row['archived'] == 0) && ($row['recip'] == $me->uid))
					echo "<input type=\"submit\" name=\"msg_action\" value=\"Archivieren\">&nbsp;&nbsp;";
	      echo "<input type=\"submit\" name=\"msg_action\" value=\"Antworten\"></td></tr>\n";
	      echo "</table><input type=\"hidden\" name=\"f\" value=\"2\">\n";
	      echo "<input type=\"hidden\" name=\"id\" value=\"".$id."\">";

        echo "<input type=\"hidden\" name=\"r\" value=\"".$row['user']."\">\n";
	      echo "<input type=\"hidden\" name=\"subject\" value=\"".$row['subject']."\">\n";
	      echo "<input type=\"hidden\" name=\"olddate\" value=\"".$row['sent']."\">\n";
	      echo "<input type=\"hidden\" name=\"c\" value=\"".$me->reload_counter."\">\n";
        if (isset ($_REQUEST['o']))
	        echo "<input type=\"hidden\" name=\"o\" value=\"".$_REQUEST['o']."\">\n";
	      echo '<div id="meldarea"></div>';
	      echo "</form></div>\n";

		if (($row['msg_read'] == 0) && ($title == "Absender"))
		{
	      	$query = "update messages set msg_read=1, sent=sent where id=".$id;
	      	if (!@mysql_query ($query))
	      	{
	      	  echo "Konnte nicht als gelesen markieren! ".mysql_error();
	      	}

					if ($user_row[1] > 0)
					{
	      		$query = "update user set msgs=msgs-1 where uid=".$me->uid;
	      		if (!@mysql_query ($query))
	      		{
	      		  echo "Konnte ungelesene Nachrichten nicht dekrementieren! ".mysql_error();
	      		}
	      	}
	      }
	    }
	    else
	      echo "<b>Nachricht existiert nicht!</b>\n";
	  }
	  break;
	  case 7:


	/*  echo "<table width=\"98%\"><tr><td colspan=\"2\" height=\"1px\"><img src=\"styles/".$_SESSION['theme']."/line_thin.gif\" width=\"98%\" height=\"1px\"></td></tr>";
	  echo "<tr><th colspan=\"2\">Offene Quests</th></tr>";
	  	echo "<tr><td colspan=\"2\" height=\"1px\"><img src=\"styles/".$_SESSION['theme']."/line_thin.gif\" width=\"98%\" height=\"1px\"></td></tr>";
	  echo "<tr><td>Quest 4 [draufklicklink] Name[/dkl]: </td><td>Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</td>";
		echo "<tr><td colspan=\"2\" height=\"1px\"><img src=\"styles/".$_SESSION['theme']."/line_thin.gif\" width=\"98%\" height=\"1px\"></td></tr>";
	    echo "<tr><th colspan=\"2\">Abgeschlossene Quests</th></tr>";
	  	echo "<tr><td colspan=\"2\" height=\"1px\"><img src=\"styles/".$_SESSION['theme']."/line_thin.gif\" width=\"98%\" height=\"1px\"></td></tr>";
	  echo "<tr><td>Quest 3 [draufklicklink]: </td><td>Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</td>";
		echo "<tr><td colspan=\"2\" height=\"1px\"><img src=\"styles/".$_SESSION['theme']."/line_thin.gif\" width=\"98%\" height=\"1px\"></td></tr>";
	  echo "<tr><td>Quest 2 [draufklicklink]: </td><td>+ Lorem ipsum dolor sit amet, consectetur adipisici ....</td>";
		echo "<tr><td colspan=\"2\" height=\"1px\"><img src=\"styles/".$_SESSION['theme']."/line_thin.gif\" width=\"98%\" height=\"1px\"></td></tr>";
		echo "<tr><td>Quest 1 [draufklicklink]: </td><td>+ Lorem ipsum dolor sit amet, consectetur adipisici ....</td>";
		echo "<tr><td colspan=\"2\" height=\"1px\"><img src=\"styles/".$_SESSION['theme']."/line_thin.gif\" width=\"98%\" height=\"1px\"></td></tr>";
	  echo "<table><tr><td></td><td></td>";*/

	  break;
}
?>
	  </form>
</div>
</div>

 <?php
require_once("footer.php");
 ?>