<?php
include("connect.php");
include("login_cookie.php");
while($check_logon_com = mysql_fetch_array($db_community)) {

$users = mysql_query("SELECT * FROM `pytox_community` ORDER BY `bruger` ASC"); 

if ($_COOKIE[pass] == $check_logon_com[kode] && $_COOKIE[com] == "1") {

	if ($check_logon_com["exist"] == "1") {

		if ($_GET["choose"] == "send") {

if ($_COOKIE["user"] != "" && $_POST["emne"] != "" && $_POST["message"] != "" && $_POST["Validate"] != "") {
 if ($_POST["sendtouser"] == $check_logon_com["bruger"]) {

print "Du kan ikke sende beskeder til dig selv!";

 } else if ($_POST["sendtouser"] == "start") {

print "Du kan kun sende post til brugere!";

 } else {

  if ($_POST["Validate"] == $_SESSION["tjekcode"]) {
//OPRETTER!
?>
Beskeden er sendt!
<br />
<br />
<br />
<table class="blackborder" style="width:398px;" cellpadding="0" cellspacing="0">
 <tr>
  <td align="left">
<strong><u>Besked faktura:</u></strong>
  </td>
 </tr>
 <tr>
  <td align="left">
Sendt fra: <?php print $_COOKIE["user"];?>
<br />
<br />
Sendt til: <?php print $_POST["sendtouser"];?>
<br />
<br />
Afsendelsestid: <?php print $tid;?>
<br />
<br />
Emne: <?php print $_POST["emne"];?>
<br />
<br />
Besked: <?php print nl2br(stripslashes($_POST["message"]));?>
  </td>
 </tr>
</table>
<?php
$mysql_message = nl2br($_POST[message]);

$replace["message"] = str_replace("Ø", "&Oslash;", $mysql_message);
$replace["message"] = str_replace("ø", "&oslash;", $replace["message"]);
$replace["message"] = str_replace("Å", "&Aring;", $replace["message"]);
$replace["message"] = str_replace("å", "&aring;", $replace["message"]);
$replace["message"] = str_replace("Æ", "&Aelig;", $replace["message"]);
$replace["message"] = str_replace("æ", "&aelig;", $replace["message"]);

if ($_POST["sendtouser"] == "alle") {

$sendtothoseusers = mysql_query("SELECT * FROM `pytox_community` ORDER BY `id` DESC");

while ($sendto = mysql_fetch_array($sendtothoseusers)) {

mysql_query("INSERT INTO `pytox_post` (`id`, `til`, `fra`, `tid`, `emne`, `besked`, `laest`, `show`) VALUES ('', '{$sendto[bruger]}', '{$_COOKIE[user]}', '{$tid}', '{$_POST[emne]}', '{$replace[message]}', '0', '1')") or die(mysql_error());

}

} else if ($_POST["sendtouser"] == "crewet") {

$sendtothoseusers = mysql_query("SELECT * FROM `pytox_community` WHERE `crew` LIKE '1' ORDER BY `id` DESC");

while ($sendto = mysql_fetch_array($sendtothoseusers)) {

mysql_query("INSERT INTO `pytox_post` (`id`, `til`, `fra`, `tid`, `emne`, `besked`, `laest`, `show`) VALUES ('', '{$sendto[bruger]}', '{$_COOKIE[user]}', '{$tid}', '{$_POST[emne]}', '{$replace[message]}', '0', '1')") or die(mysql_error());

}

} else {

	if ($_SESSION[sendingto] != "") {
mysql_query("INSERT INTO `pytox_post` (`id`, `til`, `fra`, `tid`, `emne`, `besked`, `laest`, `show`) VALUES ('', '{$_SESSION[sendingto]}', '{$_COOKIE[user]}', '{$tid}', '{$_POST[emne]}', '{$replace[message]}', '0', '1')") or die(mysql_error());
} else {
mysql_query("INSERT INTO `pytox_post` (`id`, `til`, `fra`, `tid`, `emne`, `besked`, `laest`, `show`) VALUES ('', '{$_POST[sendtouser]}', '{$_COOKIE[user]}', '{$tid}', '{$_POST[emne]}', '{$replace[message]}', '0', '1')") or die(mysql_error());
}

}

  } else {
header("Location: index.php?message=failure&errorid=54308");
  }

 }

} else {
header("Location: index.php?message=failure&errorid=27173");
}

		} else {

?>
<form action="?site=Pmail&choose=send" method="post">
<strong>Send en Pytox-mail til en Pytox-bruger:</strong>
<br />
<br />
<strong>Vælg brugeren du vil sende beskeden til (Må ikke være første punkt!):</strong>
<br />
<?php
if ($_GET[touser] != "" && $_GET[emne] != "") {

$send[to] = mysql_query("SELECT * FROM `pytox_community` WHERE `id` LIKE '{$_GET[touser]}' LIMIT 1");
while ($to_user = mysql_fetch_array($send[to])) {
$to[user] = $to_user["bruger"];
}

$_SESSION[sendingto] = $to[user];
?>
<input type="text" name="sendtouser" value="<?php print $to[user];?>" disabled="disabled"/>
<?php
} else {
?>
<select name="sendtouser" style="background-color:#EEE;color:#000;width:150px;border:1px solid #000;">
<option value="start">- V&aelig;lg en bruger -</option>
<?php
if ($check_logon_com[admin] == 1) {
print "<option value=\"alle\">- Alle brugere -</option>";
}
if ($check_logon_com[crew] == 1) {
print "<option value=\"crewet\">- Crewet -</option>";
}

while($user = mysql_fetch_array($users)) {
print "<option value=\"".$user[bruger]."\">".$user[bruger]."</option>";
}
?>
</select>
<?php
}
?>
<br />
<strong>Dit brugernavn:</strong>
<br />
<input type="text" name="usersend" value="<?=$_COOKIE[user];?>" disabled="disabled" />
<br />
<br />
<!-- Validerinskode start -->
<?php
$kode[1] = mt_rand(99999, 0);
$kode[2] = mt_rand(99999, 0);
$kode[3] = mt_rand(99999, 0);

if (strlen($kode[1]) == 5) {
$sikkerhedskode = $kode[1];

} else if (strlen($kode[2]) == 5) {
$sikkerhedskode = $kode[2];

} else if (strlen($kode[3]) == 5) {
$sikkerhedskode = $kode[3];

} else {
$sikkerhedskode = "ERROR!";

}

if ($_GET["func"] == "" && $_GET["new"] == "") {
							if ($sikkerhedskode == "ERROR!") {
$_SEESION[tjekcode] = "1615165151561561651651";
							} else {
$_SESSION[tjekcode] = $sikkerhedskode;
							}
} else {
$_SESSION[tjekcode] = "";
}
?>
<strong>Valideringskode:</strong>
<br />
<?php
if ($sikkerhedskode == "ERROR!") {
print "DER SKETE EN FEJL! \"ERROR!\" ER IKKE EN GYLDIG SIKKERHEDSKODE. Opdater eller prøv igen senere.<br />";
}
?>
<img class="blackborder" src="../../img.php?text=<?php print $sikkerhedskode; ?>" alt="Loading..." title="<?php print $sikkerhedskode; ?>" align="middle" /> = *<input type="text" maxlength="5" style="width:100px;" name="Validate" />
<!-- Valideringskode slut -->
<br />
<strong>Emne:</strong>
<br />
<?php
if ($_GET[emne] != "" && $_GET[touser] != "") {

print "<input type=\"text\" name=\"emne\" value=\"Ang. ".$_GET[emne]."\" />";

} else {
?>
<input type="text" name="emne" />
<?php
}
?>
<br />
<strong>Besked:</strong>
<br />
<textarea name="message" cols="30" rows="7"></textarea>
<br />
<br />
<input type="submit" value="Send!" class="submit" />
</form>
<?php
		}
	} else {//exist
	header("Location: index.php?message=failure&errorid=60065");
	}//exist
}//cookies
}//whilen
?>