ulogger-server/utils/handleposition.php

91 lines
2.5 KiB
PHP
Raw Normal View History

2020-01-13 22:57:13 +01:00
<?php
/* μlogger
*
* Copyright(C) 2020 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
2020-02-20 17:08:47 +01:00
require_once(dirname(__DIR__) . "/helpers/auth.php");
require_once(ROOT_DIR . "/helpers/lang.php");
require_once(ROOT_DIR . "/helpers/track.php");
require_once(ROOT_DIR . "/helpers/utils.php");
require_once(ROOT_DIR . "/helpers/config.php");
2020-01-13 22:57:13 +01:00
2020-02-20 17:08:47 +01:00
$auth = new uAuth();
2020-01-13 22:57:13 +01:00
2020-06-02 22:29:16 +02:00
$action = uUtils::postString("action");
$positionId = uUtils::postInt("posid");
$comment = uUtils::postString("comment");
2020-01-13 22:57:13 +01:00
2020-02-20 17:08:47 +01:00
$config = uConfig::getInstance();
$lang = (new uLang($config))->getStrings();
2020-01-13 22:57:13 +01:00
2020-02-20 17:08:47 +01:00
if (empty($action) || empty($positionId)) {
uUtils::exitWithError($lang["servererror"]);
}
$position = new uPosition($positionId);
if (!$position->isValid || !$auth->hasReadWriteAccess($position->userId)) {
uUtils::exitWithError($lang["notauthorized"]);
2020-02-20 17:08:47 +01:00
}
2020-01-13 22:57:13 +01:00
2020-06-02 22:29:16 +02:00
$data = null;
2020-02-20 17:08:47 +01:00
switch ($action) {
2020-01-13 22:57:13 +01:00
2020-06-02 22:29:16 +02:00
case "update":
2020-02-20 17:08:47 +01:00
$position->comment = $comment;
if ($position->update() === false) {
uUtils::exitWithError($lang["servererror"]);
}
break;
2020-01-13 22:57:13 +01:00
2020-06-02 22:29:16 +02:00
case "delete":
2020-02-20 17:08:47 +01:00
if ($position->delete() === false) {
2020-01-13 22:57:13 +01:00
uUtils::exitWithError($lang["servererror"]);
2020-02-20 17:08:47 +01:00
}
break;
2020-06-02 22:29:16 +02:00
case "imageadd":
try {
$fileMeta = uUtils::requireFile("image");
if ($position->setImage($fileMeta) === false) {
uUtils::exitWithError($lang["servererror"]);
}
$data = [ "image" => $position->image ];
} catch (ErrorException $ee) {
$message = $lang["servererror"];
$message .= ": {$ee->getMessage()}";
uUtils::exitWithError($message);
} catch (Exception $e) {
$message = $lang["iuploadfailure"];
$message .= ": {$e->getMessage()}";
uUtils::exitWithError($message);
}
break;
case "imagedel":
if ($position->removeImage() === false) {
uUtils::exitWithError($lang["servererror"]);
}
break;
2020-02-20 17:08:47 +01:00
default:
uUtils::exitWithError($lang["servererror"]);
break;
}
2020-01-13 22:57:13 +01:00
2020-06-02 22:29:16 +02:00
uUtils::exitWithSuccess($data);
2020-01-13 22:57:13 +01:00
?>