3words/ucp.php

170 lines
5.1 KiB
PHP
Raw Normal View History

<?php
/* This file is part of 3words
*
* (c) 2014 Leafcat Coding -- http://leafc.at
*
* License: AGPLv3, see LICENSE for full license text
*
* This file was touched by:
* - nilsding <nilsding@nilsding.org>
*
* Oh, and before I forget...
* ________ __________ __ ____ __ ______
* / ____/ / / / ____/ //_/ / __ \/ / / / __ \
* / /_ / / / / / / ,< / /_/ / /_/ / /_/ / with
* / __/ / /_/ / /___/ /| | / ____/ __ / ____/ a
* /_/ \____/\____/_/ |_| /_/ /_/ /_/_/ cactus!
*
* Thanks for listening.
*/
include_once 'config.php';
2014-08-18 18:13:34 +02:00
function check_privileges($ajax = false) {
2014-08-18 15:30:09 +02:00
if (!$_SESSION['logged_in']) {
2014-08-18 18:13:34 +02:00
if ($ajax) {
echo json_encode(array("success" => false));
} else {
2015-08-13 17:23:12 +02:00
$_SESSION['flash'] = "Einloggen um fortzufahren.";
2014-08-18 18:13:34 +02:00
header('Location: ucp.php?page=login');
}
2014-08-18 15:30:09 +02:00
exit();
}
}
switch ($_GET['page']) {
2014-08-18 18:13:34 +02:00
case "ajax": {
check_privileges(true);
$response = array("success" => false);
switch ($_GET['action']) {
case "delete-word": {
if (isset($_GET['id'])) {
if (is_numeric($_GET['id'])) {
$id = (int) $_GET['id'];
if ($sql->query("DELETE FROM `words` WHERE `id`=" . $id . ";")) {
$response["success"] = true;
}
}
}
break;
}
}
echo json_encode($response);
2014-08-18 18:26:47 +02:00
exit();
2014-08-18 18:13:34 +02:00
break;
}
case "login": {
2014-08-18 15:30:09 +02:00
if ($_SESSION['logged_in']) {
2015-08-13 17:23:12 +02:00
$_SESSION['flash'] = "Du bist bereits eingeloggt.";
2014-08-18 15:30:09 +02:00
header('Location: ucp.php');
exit();
}
if (!isset($_POST['login'])) {
$tpl->draw("login");
} else {
$res = $sql->query("SELECT `value` FROM `config` WHERE `key` = \"username\";")->fetch_assoc();
$username = $res['value'];
$res = $sql->query("SELECT `value` FROM `config` WHERE `key` = \"password\";")->fetch_assoc();
$password = $res['value'];
$post_pass = crypt($_POST['password'], $password);
if (($_POST['username'] === $username) && ($post_pass === $password)) {
// successful login
$_SESSION['logged_in'] = true;
2015-08-13 17:23:12 +02:00
$_SESSION['flash'] = "Erfolgreich eingeloggt.";
2014-08-18 15:30:09 +02:00
header('Location: ucp.php');
exit();
} else {
// failed login
2015-08-13 17:23:12 +02:00
$_SESSION['flash'] = "Falscher Name oder Passwort";
2014-08-18 15:30:09 +02:00
header('Location: ucp.php?page=login');
exit();
}
}
break;
}
case "logout": {
2014-08-18 15:30:09 +02:00
check_privileges();
session_destroy();
session_start();
2015-08-13 17:23:12 +02:00
$_SESSION['flash'] = "Erfolgreich ausgeloggt.";
header('Location: index.php');
2014-08-18 15:08:15 +02:00
exit();
break;
}
case "settings": {
2014-08-18 15:30:09 +02:00
check_privileges();
2014-08-18 17:49:20 +02:00
if (!isset($_POST['action'])) {
$tpl->draw("settings");
} else {
switch ($_POST['action']) {
case "generic": {
if (isset($_POST['sitename'])) {
$sql->query("UPDATE `config` SET `value`='" . $sql->real_escape_string(trim($_POST['sitename'])) . "' WHERE `key`='sitename'");
}
if (isset($_POST['recent_check'])) {
$sql->query("UPDATE `config` SET `value`='true' WHERE `key`='recent_public'");
} else {
$sql->query("UPDATE `config` SET `value`='false' WHERE `key`='recent_public'");
}
if (isset($_POST['recent_count'])) {
if (is_numeric($_POST['recent_count'])) {
$sql->query("UPDATE `config` SET `value`='" . (int) $_POST['recent_count'] . "' WHERE `key`='recent_count'");
}
}
2015-08-13 17:23:12 +02:00
$_SESSION['flash'] = "Änderungen erfolgreich gespeichert.";
2014-08-18 17:49:20 +02:00
header('Location: ucp.php?page=settings');
exit();
break;
}
case "password": {
if (isset($_POST['password_change']) && isset($_POST['password_verify'])) {
if ($_POST['password_change'] === $_POST['password_verify']) {
if (strlen($_POST['password_change']) > 3) {
$sql->query("UPDATE `config` SET `value`='" . $sql->real_escape_string(crypt_password($_POST['password_change'], gen_salt(22))) . "' WHERE `key`='password';");
2015-08-13 17:23:12 +02:00
$_SESSION['flash'] = "Passwort erfolgreich geändert.";
2014-08-18 17:49:20 +02:00
header('Location: ucp.php?page=settings');
exit();
}
}
}
2015-08-13 17:23:12 +02:00
$_SESSION['flash'] = "Das Passwort stimmt nicht überein oder ist zu kurz.";
2014-08-18 17:49:20 +02:00
header('Location: ucp.php?page=settings');
exit();
break;
}
default: {
$tpl->draw("settings");
}
}
}
break;
}
case "inbox":
default: {
2014-08-18 15:30:09 +02:00
check_privileges();
2014-08-18 18:14:05 +02:00
$sql_str = "SELECT `id`, `word1`, `word2`, `word3`, `author`, `new` FROM `words` ORDER BY `id` DESC;";
2014-08-18 16:03:17 +02:00
$res = $sql->query($sql_str);
$words = array();
while ($r = $res->fetch_assoc()) {
array_push($words, array(
2014-08-18 18:13:34 +02:00
"id" => $r['id'],
2014-08-18 16:03:17 +02:00
"word1" => $r['word1'],
"word2" => $r['word2'],
"word3" => $r['word3'],
"author" => $r['author'],
"new" => ($r['new'] == 1 ? true : false)
));
}
$sql_str = "UPDATE `words` SET `new` = 0;";
$sql->query($sql_str);
$tpl->assign("words", $words);
$tpl->draw("inbox");
}
}