<?php
ini_set('display_errors',1);
session_start();
$config = explode(', ',file_get_contents('config.txt'));
require("functions.php");

$config = loadConfig();
unlock_all_files();

if(o_login_check($_POST['user'],$_POST['password']) OR o_login_check($_SESSION['user'],$_SESSION['password'])) {

if(isset($_POST['user'])) {
$_SESSION['user'] = $_POST['user'];
$_SESSION['password'] = $_POST['password'];
}
$xml = load_compressed_xml_file('discussions.xml');
// Start document
if($_GET['action']=="send_message") {
foreach($xml->discussion AS $discussion) {
if($discussion->name==$_POST['discussion']) {
$text = $discussion->addChild('text');
$text->addChild('from',$_SESSION['user']);
$text->addChild('date',date("d.m.Y - H:i",time()));
$text->addChild('content',$_POST['content']);
save_compressed_xml_file($xml,'discussions.xml');
break;
}
}
}
if($_GET['action']=="get_discussion") {
foreach($xml->discussion AS $discussion) {
if($discussion->name==$_GET['discussion']) {
foreach($discussion->text AS $text) {
if($text->from==$_SESSION['user']) {
echo '<div class="fromme" style="text-align:left;margin:25px;margin-left:40px;"><span class="from">Ich</span><br>'.$text->content.'</div>';
}
else {
echo '<div style="text-align:right;margin:25px;margin-left:40px;" class="notfromme"><span class="from">'.$text->from.'</span><br>'.$text->content.'</div>';
}
}
}
}
die();
}
if($_GET['action']=="delete_discussion") {
for($i=0;$i<count($xml->discussion);$i++) {
if($_GET['discussion']==$xml->discussion[$i]->name AND $_SESSION['user']==$config[1] || $_SESSION['user']==$xml->discussion[$i]->from) {
unset($xml->discussion[$i]);
save_compressed_xml_file($xml,'discussions.xml');
echo '<!DOCTYPE html><html><head><link rel="Shortcut Icon" href="opolytics.png"><link rel="Stylesheet" href="style.php" type="text/css"><title>Diskussion gelöscht - Opolytics</title></head><body><h1>Diskussion gelöscht</h1><p>Die Diskussion '.$_GET['discussion'].' wurde gelöscht.</p>'; include('menu.php');
}
}
}
if($_GET['action']=="change_data" AND !isset($_POST['discussion'])) {
echo '<!DOCTYPE html><html><head><link rel="Shortcut Icon" href="opolytics.png"><link rel="Stylesheet" href="style.php" type="text/css"><title>Diskussion anpassen - Opolytics</title></head><body><h1>Diskussion: '.$_GET['discussion'].' anpassen</h1><form action="#" method="post"><b>Name:</b> <input type="text" name="name"><br><b>Beschreibung:</b> <input type="text" name="description"><br><b>Sichtbar für:</b><br><input type="radio" name="visiblefor" value="everyone" id="visiblefor1" checked><label for="visiblefor1">Alle</label><br><input type="radio" name="visiblefor" value="defined_users" id="visiblefor2"><label for="visiblefor2">Benutzerdefiniert: <input type="text" name="users"></label><br><input type="radio" name="visiblefor" value="me" id="visiblefor3"><label for="visiblefor3">Nur mich</label><br>
<b>Aktive Teilnehmer:</b><br><input type="radio" name="writing_enabled_for" value="everyone" id="writing_enabled_for1" checked><label for="writing_enabled_for1">Alle</label><br><input type="radio" name="writing_enabled_for" value="defined_users" id="writing_enabled_for2"><label for="writing_enabled_for2">Benutzerdefiniert: <input type="text" name="users_writing"></label><br><input type="radio" name="writing_enabled_for" value="me" id="writing_enabled_for3"><label for="writing_enabled_for3">Nur mich</label><br>
<input type="hidden" name="discussion" value="'.$_GET['discussion'].'"><input type="submit" value="Diskussion anpassen"></form>';
include('menu.php');
}
if($_GET['action']=="change_data" AND isset($_POST['discussion'])) {
$visiblefor = $_POST['visiblefor'];
if($visiblefor=='defined_users') {
$visiblefor = $_POST['users'];
}
else if($visiblefor=='me') {
$visiblefor = $_SESSION['user'];
}
$writing_enabled_for = $_POST['writing_enabled_for'];
if($writing_enabled_for=='defined_users') {
$writing_enabled_for = $_POST['users'];
}
else if($writing_enabled_for=='me') {
$writing_enabled_for = $_SESSION['user'];
}
for($i=0;$i<count($xml->discussion);$i++) {
if($_POST['discussion']==$xml->discussion[$i]->name AND $_SESSION['user']==$config[1] || $_SESSION['user']==$xml->discussion[$i]->from) {
$xml->discussion[$i]->name = $_POST['name'];
$xml->discussion[$i]->description = $_POST['description'];
$xml->discussion[$i]['writing_enabled_for'] = $writing_enabled_for;
$xml->discussion[$i]['visiblefor'] = $visiblefor;
save_compressed_xml_file($xml,'discussions.xml');
echo '<!DOCTYPE html><html><head><link rel="Shortcut Icon" href="opolytics.png"><link rel="Stylesheet" href="style.php" type="text/css"><title>Diskussion angepasst - Opolytics</title></head><body><h1>Diskussion angepasst.</h1><p>Die Diskussion wurde angepasst und alle Einstellungen geändert.</p>';
include('menu.php');
echo '</body></html>';
}
}
}
if($_GET['action']=="create_discussion" AND !isset($_POST['name'])) {
echo '<!DOCTYPE html><html><head><link rel="Shortcut Icon" href="opolytics.png"><link rel="Stylesheet" href="style.php" type="text/css"><title>Diskussion erstellen - Opolytics</title></head><body><h1>Neue Diskussion erstellen</h1><form action="#" method="post"><b>Name:</b> <input type="text" name="name"><br><b>Beschreibung:</b> <input type="text" name="description"><br><b>Sichtbar für:</b><br><input type="radio" name="visiblefor" value="everyone" id="visiblefor1" checked><label for="visiblefor1">Alle</label><br><input type="radio" name="visiblefor" value="defined_users" id="visiblefor2"><label for="visiblefor2">Benutzerdefiniert: <input type="text" name="users"></label><br><input type="radio" name="visiblefor" value="me" id="visiblefor3"><label for="visiblefor3">Nur mich</label><br>
<b>Aktive Teilnehmer:</b><br><input type="radio" name="writing_enabled_for" value="everyone" id="writing_enabled_for1" checked><label for="writing_enabled_for1">Alle</label><br><input type="radio" name="writing_enabled_for" value="defined_users" id="writing_enabled_for2"><label for="writing_enabled_for2">Benutzerdefiniert: <input type="text" name="users_writing"></label><br><input type="radio" name="writing_enabled_for" value="me" id="writing_enabled_for3"><label for="writing_enabled_for3">Nur mich</label><br>
<input type="submit" value="Diskussion erstellen"></form>';
include('menu.php');
}
if($_GET['action']=="create_discussion" AND isset($_POST['name'])) {
$visiblefor = $_POST['visiblefor'];
if($visiblefor=='defined_users') {
$visiblefor = $_POST['users'];
}
else if($visiblefor=='me') {
$visiblefor = $_SESSION['user'];
}
$writing_enabled_for = $_POST['writing_enabled_for'];
if($writing_enabled_for=='defined_users') {
$writing_enabled_for = $_POST['users'];
}
else if($writing_enabled_for=='me') {
$writing_enabled_for = $_SESSION['user'];
}
$discussion = $xml->addChild('discussion');
$discussion->addChild('from',$_SESSION['user']);
$discussion->addChild('date',date("d.m.Y - H:i",time()));
$discussion->addChild('description',$_POST['description']);
$discussion->addChild('name',$_POST['name']);
$discussion->addAttribute('visiblefor',$visiblefor);
$discussion->addAttribute('writing_enabled_for',$writing_enabled_for);
save_compressed_xml_file($xml,'discussions.xml');
echo '<!DOCTYPE html><html><head><link rel="Shortcut Icon" href="opolytics.png"><link rel="Stylesheet" href="style.php" type="text/css"><title>Diskussion erstellt - Opolytics</title></head><body>';
include('menu.php');
echo '<h1>Diskussion gespeichert</h1><p>Die Diskussion wurde soeben gespeichert.</p><p><a href="discuss.php">Zurück</a></body></html>';
}
if(!isset($_GET['action'])) {
echo '<!DOCTYPE html><html><head><link rel="Shortcut Icon" href="opolytics.png"><link rel="Stylesheet" href="style.php" type="text/css"><title>Diskussionen - Opolytics</title></head><body>';
include('menu.php');
if(!isset($_GET['discussion'])) {
echo '<h1>Diskussionen</h1>';
foreach($xml->discussion AS $discussion) {
if($discussion['visiblefor']=="everyone" OR in_array($_SESSION['user'],explode(',',$discussion['visiblefor'])) OR $_SESSION['user']==$config[1]) {
echo '<div style="padding-left:30px;margin-bottom:20px" class="text"><a href="?discussion='.$discussion->name.'"><span class="discussions_name">'.$discussion->name.'</span><br><span class="discussions_description">'.substr($discussion->description,0,150).' - Von: '.$discussion->from.'</span></a>';
if($_SESSION['user']==$discussion->from OR $_SESSION['user']==$config[1]) {
echo '<br><span style="font-size:9pt"><a href="?action=delete_discussion&discussion='.$discussion->name.'"><span style="color:red">&cross;</span> Diskussion löschen</a> - <a href="?action=change_data&discussion='.$discussion->name.'">&#9997; Daten ändern</a></span>';
}
echo '</div>';
}
}
echo '<p><a href="?action=create_discussion"><img src="img/plus.png" height="13"> Neue Diskussion erstellen</a></p>';
}
if(isset($_GET['discussion'])) {
foreach($xml->discussion AS $discussion) {
if($discussion->name==$_GET['discussion']) {
echo '<h1 class="discussions_name" style="line-height: 60%;padding-top:30px;margin-bottom:10px;font-weight:normal">'.$discussion->name.'<br><span class="discussions_description" style="margin-top:0px">'.substr($discussion->description,0,150).' - Von: '.$discussion->from.'</span><br><span style="font-size:8pt">Erstellt: '.$discussion->date.'</span></h1><div class="text" id="discussion">';
foreach($discussion->text AS $text) {
if($text->from==$_SESSION['user']) {
echo '<div class="fromme" style="text-align:left;margin:25px;margin-left:40px;"><span class="from">Ich</span><br>'.$text->content.'</div>';
}
else {
echo '<div style="text-align:right;margin:25px;margin-left:40px;" class="notfromme"><span class="from">'.$text->from.'</span><br>'.$text->content.'</div>';
}
}
echo '</div><p><textarea id="content" style="width:100%"></textarea> <input type="button" onclick="send()" value="Senden"></p>
<script>
function send() {
var http = new XMLHttpRequest();
http.open("POST","?action=send_message",true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send("content=" + document.getElementById("content").value + "&discussion='.$discussion->name.'");
setTimeout(function() { document.getElementById("content").value = "" },100);
}
document.getElementById("content").addEventListener("keydown",function(e) {
code = e.keyCode;
if(code==13) {
e.preventDefault();
send();
}
});
function get_discussion() {
var http = new XMLHttpRequest();
http.open("GET","?action=get_discussion&discussion='.$discussion->name.'",true);
http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
        document.getElementById("discussion").innerHTML = http.responseText;
    }
}
http.send();
}
setInterval("get_discussion()",500);
</script>';
break;
}
}
}
echo '<style>
.discussions_name {
font-size: 25pt;
color: #5D5B5B;
}
.discussions_description {
font-size:10pt;
color:grey;
}
.text a {
text-decoration: none;
}
p {
padding-left: 50px;
padding-right: 50px;
}
.fromme {
padding: 10px;
padding-right: 20px;
padding-left:20px;
border-radius:10px;
background: linear-gradient(to bottom left,blue, #DFE0ED);
border:1px solid grey;
}
.notfromme {
padding: 10px;
padding-right: 20px;
padding-left:20px;
border-radius:10px;
background: linear-gradient(to bottom left,grey,white);
border:1px solid grey;
overflow:auto;
}
.from {
font-weight: bold;
}
</style>';
}
}
?>