Commit 5ee13202 authored by Michael Ochmann's avatar Michael Ochmann

added alert-system and recovering of post-data on error

parent c0513c5a
<?php
namespace FSI;
class Alert {
public $level;
public $message;
public $data;
public function __construct($message, $level = AlertLevel::INFO, $data = null) {
$this->message = $message;
$this->level = $level;
if ($data === null) {
$this->data = new \stdClass();
$this->data->url = $_POST["url"];
$this->data->shorturl = $_POST["shorturl"];
}
else
$this->data = $data;
$_SESSION["alert"] = $this->toString();
}
public function toString() {
return json_encode($this);
}
}
\ No newline at end of file
<?php
namespace FSI;
class AlertLevel {
const ERROR = "alert-danger";
const WARNING = "alert-warning";
const INFO = "alert-info";
const NORMAL = "alert-default";
}
\ No newline at end of file
......@@ -7,12 +7,19 @@ class Frontend {
private $template;
private $db;
private $settings;
private $alert;
public function __construct(URLShortener $root) {
$this->root = $root;
$this->db = DatabaseHandler::Instance();
$this->settings = SettingsHandler::Instance();
$this->template = new Template("main.php");
$this->alert = null;
if (isset($_SESSION["alert"])) {
$this->alert = json_decode($_SESSION["alert"]);
unset($_SESSION["alert"]);
}
if (isset($_POST["new"]) && $this->root->authenticator->authenticated())
new PostHandler($this->root);
}
......@@ -26,9 +33,15 @@ class Frontend {
while ($url = $query->fetch_object()) {
array_push($links, $url);
}
$url = $this->alert !== null && $this->alert->data->url !== null ? $this->alert->data->url : "";
$shorturl = $this->alert !== null && $this->alert->data->shorturl !== null ? $this->alert->data->shorturl : "";
$template->setValues([
"links" => $links,
"baseurl" => $this->settings->system->baseurl
"links" => $links,
"baseurl" => $this->settings->system->baseurl,
"url" => $url,
"shorturl" => $shorturl
]);
}
else {
......@@ -36,9 +49,21 @@ class Frontend {
$userLabel = "";
}
if ($this->alert !== null) {
$alert = new Template("alert.php");
$alert->setValues([
"level" => $this->alert->level,
"message" => $this->alert->message
]);
$alert = $alert->render(false);
}
else
$alert = "";
$this->template->setValues([
"content" => $template->render(false),
"user" => $userLabel,
"alert" => $alert
]);
$this->template->render();
}
......
......@@ -18,7 +18,21 @@ class PostHandler {
if (!isset($_POST["url"]) || $_POST["url"] == "")
return;
if (!preg_match("/^(http|https):\/\//", $_POST["url"])) {
new Alert("In deiner URL fehlt das Protokoll. Bitte ergänze <code>http://</code> oder <code>https://</code>.", AlertLevel::ERROR);
header("Location: ./");
exit;
}
$shortcode = $_POST["shorturl"] == "" ? $this->generateShortcode() : $_POST["shorturl"];
$query = $this->db->prepare("SELECT shortcode FROM urls WHERE shortcode = ?");
$query->bind_param("s", $shortcode);
$query->execute();
$query->store_result();
if ($query->num_rows > 0)
new Alert("Der Shortcode <code>$shortcode</code> ist leider bereits vergeben.", AlertLevel::WARNING);
$query = $this->db->prepare("INSERT INTO urls (url, shortcode, rft) VALUES(?, ?, ?)");
$query->bind_param(
"sss",
......
......@@ -39,6 +39,10 @@ table.table-list tr td input {
padding: 0 8px;
}
.alert {
margin-top: 12px;
}
.container {
width: 50%;
}
......
<section class="alert {{level}}">{{message}}</section>
......@@ -8,6 +8,7 @@
<body>
<main class="container">
<section class="user">{{user}}</section>
{{alert}}
<h1>fsi.rocks</h1>
<h2 class="logo"><img src="images/fsi_rocks_logo.svg" alt="fsi.rocks - url shortener – Fachschaft Informatik"></h2>
{{content}}
......
<form action="./" method="post">
<div class="form-group">
<p>
<input type="text" class="form-control" name="url" placeholder="URL to shorten">
<input type="text" class="form-control" name="url" value="{{url}}" placeholder="URL to shorten">
</p>
<p>
<div class="input-group">
<div class="input-group-addon">fsi.rocks/</div>
<input type="text" class="form-control" name="shorturl" placeholder="your Shortcode (leave empty for random string - max. 20 characters)">
<input type="text" class="form-control" name="shorturl" value="{{shorturl}}" placeholder="your Shortcode (leave empty for random string - max. 20 characters)">
</div>
</p>
<p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment