Refactor exit with status functions

This commit is contained in:
Bartek Fabiszewski 2017-05-09 18:18:15 +02:00
parent a7f52aa215
commit ca77348fb1
6 changed files with 76 additions and 131 deletions

View File

@ -62,6 +62,52 @@
}
return (int) $val;
}
/**
* Exit with error message
*
* @param string $errorMessage Message
* @param array|null $extra Optional array of extra parameters
*/
public static function exitWithError($errorMessage, $extra = NULL) {
$extra['message'] = $errorMessage;
self::exitWithStatus(true, $extra);
}
/**
* Exit with successful status code
*
* @param array|null $extra Optional array of extra parameters
*/
public static function exitWithSuccess($extra = NULL) {
self::exitWithStatus(false, $extra);
}
/**
* Exit with xml response
* @param boolean $isError Error if true
* @param array|null $extra Optional array of extra parameters
*/
private static function exitWithStatus($isError, $extra = NULL) {
header("Content-type: text/xml");
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument("1.0");
$xml->setIndent(true);
$xml->startElement("root");
$xml->writeElement("error", (int) $isError);
if (!empty($extra)) {
foreach ($extra as $key => $value) {
$xml->writeElement($key, $value);
}
}
$xml->endElement();
$xml->endDocument();
$xml->flush();
exit;
}
}
?>

View File

@ -19,61 +19,31 @@
define("headless", true);
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
/**
* Exit with error message
*
* @param string $errorMessage Message
*/
function exitWithError($errorMessage) {
return exitWithStatus(true, $errorMessage);
}
/**
* Exit with xml response
* @param boolean $isError Error if true
* @param string $errorMessage Optional error message
*/
function exitWithStatus($isError = false, $errorMessage = NULL) {
header("Content-type: text/xml");
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument("1.0");
$xml->setIndent(true);
$xml->startElement('root');
$xml->writeElement("error", (int) $isError);
if ($isError) {
$xml->writeElement("message", $errorMessage);
}
$xml->endElement();
$xml->endDocument();
$xml->flush();
exit;
}
require_once(ROOT_DIR . "/helpers/utils.php");
$login = isset($_REQUEST['login']) ? trim($_REQUEST['login']) : NULL;
$oldpass = isset($_REQUEST['oldpass']) ? $_REQUEST['oldpass'] : NULL;
$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL;
if (empty($pass)) {
exitWithError("Empty password");
uUtils::exitWithError("Empty password");
}
if ($user->isAdmin && !empty($login)) {
// different user, only admin
$passUser = new uUser($login);
if (!$passUser->valid) {
exitWithError("User unknown");
uUtils::exitWithError("User unknown");
}
} else {
// current user
$passUser = $user;
if (!$passUser->validPassword($oldpass)) {
exitWithError("Wrong old password");
uUtils::exitWithError("Wrong old password");
}
}
if ($passUser->setPass($pass) === false) {
exitWithError("Server error");
uUtils::exitWithError("Server error");
}
exitWithStatus();
uUtils::exitWithSuccess();
?>

View File

@ -20,6 +20,7 @@
define("headless", true);
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
require_once(ROOT_DIR . "/helpers/position.php");
require_once(ROOT_DIR . "/helpers/utils.php");
$userId = (isset($_REQUEST["userid"]) && is_numeric($_REQUEST["userid"])) ? (int) $_REQUEST["userid"] : NULL;
$trackId = (isset($_REQUEST["trackid"]) && is_numeric($_REQUEST["trackid"])) ? (int) $_REQUEST["trackid"] : NULL;

View File

@ -20,59 +20,38 @@
define("headless", true);
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
require_once(ROOT_DIR . "/helpers/track.php");
/**
* Exit with xml response
* @param boolean $isError Error if true
* @param string $errorMessage Optional error message
*/
function exitWithStatus($isError, $errorMessage = NULL) {
header("Content-type: text/xml");
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument("1.0");
$xml->setIndent(true);
$xml->startElement('root');
$xml->writeElement("error", (int) $isError);
if ($isError) {
$xml->writeElement("message", $errorMessage);
}
$xml->endElement();
$xml->endDocument();
$xml->flush();
exit;
}
require_once(ROOT_DIR . "/helpers/utils.php");
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL;
$trackId = isset($_REQUEST['trackid']) ? trim($_REQUEST['trackid']) : NULL;
$trackName = isset($_REQUEST['trackname']) ? trim($_REQUEST['trackname']) : NULL;
if (empty($action) || empty($trackId)) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
$track = new uTrack($trackId);
if (!$track->isValid || (!$user->isAdmin && $user->id != $track->userId)) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
switch ($action) {
case 'update':
if (empty($trackName) || $track->update($trackName) === false) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
break;
case 'delete':
if ($track->delete() === false) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
break;
default:
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
break;
}
exitWithStatus(false);
uUtils::exitWithSuccess();
?>

View File

@ -19,34 +19,13 @@
define("headless", true);
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
/**
* Exit with xml response
* @param boolean $isError Error if true
* @param string $errorMessage Optional error message
*/
function exitWithStatus($isError, $errorMessage = NULL) {
header("Content-type: text/xml");
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument("1.0");
$xml->setIndent(true);
$xml->startElement('root');
$xml->writeElement("error", (int) $isError);
if ($isError) {
$xml->writeElement("message", $errorMessage);
}
$xml->endElement();
$xml->endDocument();
$xml->flush();
exit;
}
require_once(ROOT_DIR . "/helpers/utils.php");
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL;
$login = isset($_REQUEST['login']) ? trim($_REQUEST['login']) : NULL;
$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL;
if (!$user->isAdmin || empty($action) || empty($login) || $user->login == $login) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
$aUser = new uUser($login);
@ -54,31 +33,31 @@
switch ($action) {
case 'add':
if ($aUser->isValid) {
exitWithStatus(true, $lang["userexists"]);
uUtils::exitWithError($lang["userexists"]);
}
if (empty($pass) || $aUser->add($login, $pass) === false) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
break;
case 'update':
// update password
if (empty($pass) || $aUser->setPass($pass) === false) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
break;
case 'delete':
if ($aUser->delete() === false) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
break;
default:
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
break;
}
exitWithStatus(false);
uUtils::exitWithSuccess();
?>

View File

@ -31,38 +31,8 @@ $uploadErrors[UPLOAD_ERR_NO_TMP_DIR] = "Missing a temporary folder";
$uploadErrors[UPLOAD_ERR_CANT_WRITE] = "Failed to write file to disk";
$uploadErrors[UPLOAD_ERR_EXTENSION] = "A PHP extension stopped the file upload";
/**
* Exit with xml response
* @param boolean $isError Error if true
* @param string $errorMessage Optional error message
* @param array|null $extra Optional array of extra parameters
*/
function exitWithStatus($isError, $errorMessage = NULL, $extra = NULL) {
header("Content-type: text/xml");
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument("1.0");
$xml->setIndent(true);
$xml->startElement('root');
$xml->writeElement("error", (int) $isError);
if ($isError) {
$xml->writeElement("message", $errorMessage);
}
if (!empty($extra)) {
foreach ($extra as $key => $value) {
$xml->writeElement($key, $value);
}
}
$xml->endElement();
$xml->endDocument();
$xml->flush();
exit;
}
if (!$user->isValid) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
if (!isset($_FILES["gpx"])) {
@ -71,7 +41,7 @@ if (!isset($_FILES["gpx"])) {
if (!empty($lastErr)) {
$message .= ": " . $lastErr["message"];
}
exitWithStatus(true, $message);
uUtils::exitWithError($message);
}
$gpxFile = NULL;
@ -89,7 +59,7 @@ if ($uploadErr == UPLOAD_ERR_OK) {
$message .= ": " . $errorMessage[$uploadErr];
}
$message .= " ($uploadErr)";
exitWithStatus(true, $message);
uUtils::exitWithError($message);
}
$gpx = false;
@ -108,10 +78,10 @@ if ($gpx === false) {
if (!empty($parserMessage)) {
$message .= ": $parserMessage";
}
exitWithStatus(true, $message);
uUtils::exitWithError($message);
}
else if (empty($gpx->trk)) {
exitWithStatus(true, $lang["idatafailure"]);
uUtils::exitWithError($lang["idatafailure"]);
}
$trackCnt = 0;
@ -121,7 +91,7 @@ foreach ($gpx->trk as $trk) {
$track = new uTrack();
$trackId = $track->add($user->id, $trackName, $metaName);
if ($trackId === false) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
break;
}
@ -132,7 +102,7 @@ foreach ($gpx->trk as $trk) {
strtotime($point->time), $point["lat"], $point["lon"], $point->ele,
NULL, NULL, NULL, "gps", NULL, NULL);
if ($ret === false) {
exitWithStatus(true, $lang["servererror"]);
uUtils::exitWithError($lang["servererror"]);
}
}
}
@ -140,6 +110,6 @@ foreach ($gpx->trk as $trk) {
}
// return track id and tracks count
exitWithStatus(false, NULL, [ "trackid" => $trackId, "trackcnt" => $trackCnt ]);
uUtils::exitWithSuccess([ "trackid" => $trackId, "trackcnt" => $trackCnt ]);
?>