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; 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); define("headless", true);
require_once(dirname(__DIR__) . "/auth.php"); // sets $user require_once(dirname(__DIR__) . "/auth.php"); // sets $user
require_once(ROOT_DIR . "/helpers/utils.php");
/**
* 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;
}
$login = isset($_REQUEST['login']) ? trim($_REQUEST['login']) : NULL; $login = isset($_REQUEST['login']) ? trim($_REQUEST['login']) : NULL;
$oldpass = isset($_REQUEST['oldpass']) ? $_REQUEST['oldpass'] : NULL; $oldpass = isset($_REQUEST['oldpass']) ? $_REQUEST['oldpass'] : NULL;
$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL; $pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL;
if (empty($pass)) { if (empty($pass)) {
exitWithError("Empty password"); uUtils::exitWithError("Empty password");
} }
if ($user->isAdmin && !empty($login)) { if ($user->isAdmin && !empty($login)) {
// different user, only admin // different user, only admin
$passUser = new uUser($login); $passUser = new uUser($login);
if (!$passUser->valid) { if (!$passUser->valid) {
exitWithError("User unknown"); uUtils::exitWithError("User unknown");
} }
} else { } else {
// current user // current user
$passUser = $user; $passUser = $user;
if (!$passUser->validPassword($oldpass)) { if (!$passUser->validPassword($oldpass)) {
exitWithError("Wrong old password"); uUtils::exitWithError("Wrong old password");
} }
} }
if ($passUser->setPass($pass) === false) { if ($passUser->setPass($pass) === false) {
exitWithError("Server error"); uUtils::exitWithError("Server error");
} }
exitWithStatus(); uUtils::exitWithSuccess();
?> ?>

View File

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

View File

@ -20,59 +20,38 @@
define("headless", true); define("headless", true);
require_once(dirname(__DIR__) . "/auth.php"); // sets $user require_once(dirname(__DIR__) . "/auth.php"); // sets $user
require_once(ROOT_DIR . "/helpers/track.php"); require_once(ROOT_DIR . "/helpers/track.php");
require_once(ROOT_DIR . "/helpers/utils.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;
}
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL; $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL;
$trackId = isset($_REQUEST['trackid']) ? trim($_REQUEST['trackid']) : NULL; $trackId = isset($_REQUEST['trackid']) ? trim($_REQUEST['trackid']) : NULL;
$trackName = isset($_REQUEST['trackname']) ? trim($_REQUEST['trackname']) : NULL; $trackName = isset($_REQUEST['trackname']) ? trim($_REQUEST['trackname']) : NULL;
if (empty($action) || empty($trackId)) { if (empty($action) || empty($trackId)) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
$track = new uTrack($trackId); $track = new uTrack($trackId);
if (!$track->isValid || (!$user->isAdmin && $user->id != $track->userId)) { if (!$track->isValid || (!$user->isAdmin && $user->id != $track->userId)) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
switch ($action) { switch ($action) {
case 'update': case 'update':
if (empty($trackName) || $track->update($trackName) === false) { if (empty($trackName) || $track->update($trackName) === false) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
break; break;
case 'delete': case 'delete':
if ($track->delete() === false) { if ($track->delete() === false) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
break; break;
default: default:
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
break; break;
} }
exitWithStatus(false); uUtils::exitWithSuccess();
?> ?>

View File

@ -19,34 +19,13 @@
define("headless", true); define("headless", true);
require_once(dirname(__DIR__) . "/auth.php"); // sets $user require_once(dirname(__DIR__) . "/auth.php"); // sets $user
require_once(ROOT_DIR . "/helpers/utils.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;
}
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL; $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL;
$login = isset($_REQUEST['login']) ? trim($_REQUEST['login']) : NULL; $login = isset($_REQUEST['login']) ? trim($_REQUEST['login']) : NULL;
$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL; $pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL;
if (!$user->isAdmin || empty($action) || empty($login) || $user->login == $login) { if (!$user->isAdmin || empty($action) || empty($login) || $user->login == $login) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
$aUser = new uUser($login); $aUser = new uUser($login);
@ -54,31 +33,31 @@
switch ($action) { switch ($action) {
case 'add': case 'add':
if ($aUser->isValid) { if ($aUser->isValid) {
exitWithStatus(true, $lang["userexists"]); uUtils::exitWithError($lang["userexists"]);
} }
if (empty($pass) || $aUser->add($login, $pass) === false) { if (empty($pass) || $aUser->add($login, $pass) === false) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
break; break;
case 'update': case 'update':
// update password // update password
if (empty($pass) || $aUser->setPass($pass) === false) { if (empty($pass) || $aUser->setPass($pass) === false) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
break; break;
case 'delete': case 'delete':
if ($aUser->delete() === false) { if ($aUser->delete() === false) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
break; break;
default: default:
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
break; 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_CANT_WRITE] = "Failed to write file to disk";
$uploadErrors[UPLOAD_ERR_EXTENSION] = "A PHP extension stopped the file upload"; $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) { if (!$user->isValid) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
if (!isset($_FILES["gpx"])) { if (!isset($_FILES["gpx"])) {
@ -71,7 +41,7 @@ if (!isset($_FILES["gpx"])) {
if (!empty($lastErr)) { if (!empty($lastErr)) {
$message .= ": " . $lastErr["message"]; $message .= ": " . $lastErr["message"];
} }
exitWithStatus(true, $message); uUtils::exitWithError($message);
} }
$gpxFile = NULL; $gpxFile = NULL;
@ -89,7 +59,7 @@ if ($uploadErr == UPLOAD_ERR_OK) {
$message .= ": " . $errorMessage[$uploadErr]; $message .= ": " . $errorMessage[$uploadErr];
} }
$message .= " ($uploadErr)"; $message .= " ($uploadErr)";
exitWithStatus(true, $message); uUtils::exitWithError($message);
} }
$gpx = false; $gpx = false;
@ -108,10 +78,10 @@ if ($gpx === false) {
if (!empty($parserMessage)) { if (!empty($parserMessage)) {
$message .= ": $parserMessage"; $message .= ": $parserMessage";
} }
exitWithStatus(true, $message); uUtils::exitWithError($message);
} }
else if (empty($gpx->trk)) { else if (empty($gpx->trk)) {
exitWithStatus(true, $lang["idatafailure"]); uUtils::exitWithError($lang["idatafailure"]);
} }
$trackCnt = 0; $trackCnt = 0;
@ -121,7 +91,7 @@ foreach ($gpx->trk as $trk) {
$track = new uTrack(); $track = new uTrack();
$trackId = $track->add($user->id, $trackName, $metaName); $trackId = $track->add($user->id, $trackName, $metaName);
if ($trackId === false) { if ($trackId === false) {
exitWithStatus(true, $lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
break; break;
} }
@ -132,7 +102,7 @@ foreach ($gpx->trk as $trk) {
strtotime($point->time), $point["lat"], $point["lon"], $point->ele, strtotime($point->time), $point["lat"], $point["lon"], $point->ele,
NULL, NULL, NULL, "gps", NULL, NULL); NULL, NULL, NULL, "gps", NULL, NULL);
if ($ret === false) { 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 // return track id and tracks count
exitWithStatus(false, NULL, [ "trackid" => $trackId, "trackcnt" => $trackCnt ]); uUtils::exitWithSuccess([ "trackid" => $trackId, "trackcnt" => $trackCnt ]);
?> ?>