Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- This file is subject to the terms and conditions defined in
- file 'LICENSE', which is part of this source code package.
- © 2022 OtteIT s.r.o.
- All Rights Reserved.
- Author: Vilem Otte <dev@otte.cz>
- */
- /**
- * Read currency record
- *
- * @param id Reference, references ID in currency table
- *
- * @return _ JSON, result (HTTP response code), error (in case of any), currency (either blank array or holding resulting record as JSON)
- */
- header("Access-Control-Allow-Origin: *");
- header("Content-Type: application/json; charset=UTF-8");
- header("Access-Control-Allow-Methods: POST");
- header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
- require_once(__DIR__."/../../db.php");
- require_once(__DIR__."/../../util.php");
- require_once(__DIR__."/currency.php");
- require_once(__DIR__."/../../session/session.php");
- require_once(__DIR__."/../../auth/auth.php");
- require_once(__DIR__."/../../permission/permission.php");
- // Session set up
- $auth = new Auth();
- $session = new Session();
- // Payload requirements
- $data = json_decode(file_get_contents("php://input"));
- $payloadError = Util::PayloadCheck($data);
- if ($payloadError != false)
- {
- http_response_code(200);
- echo json_encode(array("result" => 500, "error" => $payloadError));
- exit();
- }
- // Connect to database, attach to session
- $db = new Database();
- $session->SetDB($db);
- // Require user authentication
- $auth_id = $session->GetUserID($auth);
- if ($auth_id != null)
- {
- // Check permission - generic record read can only be performed by admins, users can only see their records
- $permission = new Permission($db);
- $permissionCheck = $permission->Check($auth_id, "rum", Permission::READ | Permission::WRITE | Permission::WRITE_SELF);
- if ($permissionCheck == true)
- {
- // Permission check success - read record
- $currency = new Rum_Currency($db);
- if ($data->id != null)
- {
- $currency->id = intval($data->id);
- }
- $result = $currency->Read();
- if ($result != null)
- {
- // Successfully read record
- http_response_code(200);
- echo json_encode(array("result" => 200, "currency" => $result));
- }
- else
- {
- if ($db->GetLastError())
- {
- // DB Error during record reading
- http_response_code(200);
- echo json_encode(array("result" => 500, "error" => $db->GetLastError()));
- }
- else
- {
- // Successfully read, but no record matching
- http_response_code(200);
- echo json_encode(array("result" => 200, "currency" => []));
- }
- }
- }
- else if ($permissionCheck === false)
- {
- // Permission check failure
- http_response_code(200);
- echo json_encode(array("result" => 401, "error" => "Unauthorized: Permission level too low."));
- }
- else
- {
- // DB Error during permission check
- http_response_code(200);
- echo json_encode(array("result" => 500, "error" => $db->GetLastError()));
- }
- }
- else
- {
- // User unauthorized
- http_response_code(200);
- echo json_encode(array("result" => 401, "error" => "Unauthorized"));
- }
- // Close database connection
- $db->Disconnect();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement