You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.7 KiB
PHP

<?php
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Make things happen when buttons are pressed and forms submitted.
*/
require_once __DIR__ . "/required.php";
if ($VARS['action'] !== "signout") {
dieifnotloggedin();
}
/**
* Redirects back to the page ID in $_POST/$_GET['source'] with the given message ID.
* The message will be displayed by the app.
* @param string $msg message ID (see lang/messages.php)
* @param string $arg If set, replaces "{arg}" in the message string when displayed to the user.
*/
function returnToSender($msg, $arg = "") {
global $VARS;
if ($arg == "") {
header("Location: app.php?page=" . urlencode($VARS['source']) . "&msg=" . $msg);
} else {
header("Location: app.php?page=" . urlencode($VARS['source']) . "&msg=$msg&arg=$arg");
}
die();
}
switch ($VARS['action']) {
case "signout":
session_destroy();
header('Location: index.php');
die("Logged out.");
case "savenote":
if (!isset($VARS['content']) || empty($VARS['noteid'])) {
die($Strings->get("invalid parameters", false));
}
$note = Note::loadNote($VARS['noteid']);
if (!$note->hasWriteAccess(new User($_SESSION['uid']))) {
die($Strings->get("invalid parameters", false));
}
$note->setText($VARS['content']);
$note->setColor($VARS['color']);
$note->saveNote();
if (isset($VARS['reload'])) {
returnToSender("", "&note=" . $note->getID());
} else {
http_response_code(204);
}
break;
case "deletenote":
if (empty($VARS['noteid'])) {
die($Strings->get("invalid parameters"));
}
$note = Note::loadNote($VARS['noteid']);
if (!$note->hasWriteAccess(new User($_SESSION['uid']))) {
die($Strings->get("invalid parameters"));
}
$note->deleteNote();
returnToSender("note_deleted");
break;
case "downloadnote":
if (empty($VARS['noteid'])) {
die($Strings->get("invalid parameters", false));
}
$note = Note::loadNote($VARS['noteid']);
if (!$note->hasReadAccess(new User($_SESSION['uid']))) {
die($Strings->get("invalid parameters", false));
}
if (empty($VARS['type'])) {
$VARS['type'] = "markdown";
}
switch ($VARS['type']) {
case "html":
header("Content-Type: text/html; charset=UTF-8");
header("Content-disposition: attachment; filename=\"" . $note->getCleanTitle() . "_" . $note->getModified() . ".html\"");
echo $note->getHTML(false);
break;
case "odt":
if (PANDOC_BIN != "") {
header("Content-Type: application/vnd.oasis.opendocument.text");
header("Content-disposition: attachment; filename=\"" . $note->getCleanTitle() . "_" . $note->getModified() . ".odt\"");
$pandoc = new Pandoc\Pandoc();
echo $pandoc->convert($note->getText(), "markdown_github", "odt");
break;
}
default:
header("Content-Type: text/markdown; charset=UTF-8");
header("Content-disposition: attachment; filename=\"" . $note->getCleanTitle() . "_" . $note->getModified() . ".md\"");
echo $note->getText();
}
break;
case "favoritenote":
if (empty($VARS['noteid'])) {
die($Strings->get("invalid parameters"));
}
$note = Note::loadNote($VARS['noteid']);
if (!$note->hasWriteAccess(new User($_SESSION['uid']))) {
die($Strings->get("invalid parameters"));
}
$note->setFavorite(!$note->getFavorite());
$note->saveNote();
returnToSender("");
break;
case "getnotes":
header("Content-Type: application/json");
$noteids = $database->select('notes', 'noteid', ['ownerid' => $_SESSION['uid']]);
$notes = [];
foreach ($noteids as $n) {
$notes[] = Note::loadNote($n)->toArray();
}
exit(json_encode($notes));
case "setcolor":
if (empty($VARS['noteid'])) {
die($Strings->get("invalid parameters"));
}
$note = Note::loadNote($VARS['noteid']);
if (!$note->hasWriteAccess(new User($_SESSION['uid']))) {
die($Strings->get("invalid parameters"));
}
$note->setColor($VARS['color']);
$note->saveNote();
returnToSender("");
break;
case "maketodolist":
if (empty($VARS['noteid'])) {
die($Strings->get("invalid parameters"));
}
$note = Note::loadNote($VARS['noteid']);
if (!$note->hasWriteAccess(new User($_SESSION['uid']))) {
die($Strings->get("invalid parameters"));
}
$note->toChecklist();
$note->saveNote();
returnToSender("");
break;
case "togglecheckitem":
if (empty($VARS['noteid'])) {
die($Strings->get("invalid parameters"));
}
$note = Note::loadNote($VARS['noteid']);
if (!$note->hasWriteAccess(new User($_SESSION['uid']))) {
die($Strings->get("invalid parameters"));
}
if (!empty($VARS['text'])) {
$note->toggleChecklistItem($VARS['text']);
$note->saveNote();
}
if (isset($VARS['reload'])) {
returnToSender("");
} else {
http_response_code(204);
}
break;
}