From ca77348fb1ac3ddd7d1cfc77c211dc1097683cf9 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Tue, 9 May 2017 18:18:15 +0200 Subject: [PATCH] Refactor exit with status functions --- helpers/utils.php | 46 ++++++++++++++++++++++++++++++++++++++++++ utils/changepass.php | 42 ++++++-------------------------------- utils/getpositions.php | 1 + utils/handletrack.php | 35 +++++++------------------------- utils/handleuser.php | 37 ++++++++------------------------- utils/import.php | 46 ++++++++---------------------------------- 6 files changed, 76 insertions(+), 131 deletions(-) diff --git a/helpers/utils.php b/helpers/utils.php index 8f0246c..3b28cec 100644 --- a/helpers/utils.php +++ b/helpers/utils.php @@ -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; + } + } ?> \ No newline at end of file diff --git a/utils/changepass.php b/utils/changepass.php index 387b4a3..b6c06b1 100644 --- a/utils/changepass.php +++ b/utils/changepass.php @@ -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(); ?> \ No newline at end of file diff --git a/utils/getpositions.php b/utils/getpositions.php index 6724650..90ed143 100755 --- a/utils/getpositions.php +++ b/utils/getpositions.php @@ -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; diff --git a/utils/handletrack.php b/utils/handletrack.php index d112efe..baf90b7 100644 --- a/utils/handletrack.php +++ b/utils/handletrack.php @@ -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(); ?> \ No newline at end of file diff --git a/utils/handleuser.php b/utils/handleuser.php index 4084da3..eaa46df 100644 --- a/utils/handleuser.php +++ b/utils/handleuser.php @@ -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(); ?> \ No newline at end of file diff --git a/utils/import.php b/utils/import.php index ca3e783..3f199f3 100755 --- a/utils/import.php +++ b/utils/import.php @@ -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 ]); ?> \ No newline at end of file