Advertisement
Zgragselus

product_createphp

Jun 17th, 2023
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.12 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. This file is subject to the terms and conditions defined in
  6. file 'LICENSE', which is part of this source code package.
  7.  
  8. © 2022 OtteIT s.r.o.
  9. All Rights Reserved.
  10.  
  11. Author: Vilem Otte <dev@otte.cz>
  12.  
  13. */
  14.  
  15. /**
  16.  * Endpoint to create product record
  17.  *
  18.  * @param eshop_id Reference (Required), references ID in eshop table
  19.  * @param bottle_id Reference (Required), references ID in bottle table
  20.  * @param name String (Required), name of the product
  21.  * @param link String (Required), link (hyperlink) to the product on the eshop
  22.  * @param auction Number (Required), 1 for auction, 0 otherwise (boolean)
  23.  * @param stock Number (Required), 0 for unavailable, non-0 for available
  24.  * @param confirmed Number, 1 for confirmed product record, 0 otherwise (boolean)
  25.  * @param auction_start String, ISO time when auction starts
  26.  * @param auction_end String, ISO time when auction ends
  27.  *
  28.  * @return _ JSON with result (HTTP response code), error (details, if error happens) or product (holding result of create call)
  29.  */
  30.  
  31. header("Access-Control-Allow-Origin: *");
  32. header("Content-Type: application/json; charset=UTF-8");
  33. header("Access-Control-Allow-Methods: POST");
  34. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  35.  
  36. require_once(__DIR__."/../../db.php");
  37. require_once(__DIR__."/../../util.php");
  38. require_once(__DIR__."/product.php");
  39. require_once(__DIR__."/../../session/session.php");
  40. require_once(__DIR__."/../../auth/auth.php");
  41. require_once(__DIR__."/../../permission/permission.php");
  42.  
  43. // Session set up
  44. $auth = new Auth();
  45. $session = new Session();
  46.  
  47. // Payload requirements
  48. $data = json_decode(file_get_contents("php://input"));
  49.  
  50. $payloadError = Util::PayloadCheck($data, "eshop_id", "bottle_id", "name", "link", "auction", "stock");
  51. if ($payloadError != false)
  52. {
  53.     http_response_code(200);
  54.     echo json_encode(array("result" => 500, "error" => $payloadError));
  55.  
  56.     exit();
  57. }
  58.  
  59. // Connect to database, attach to session
  60. $db = new Database();
  61. $session->SetDB($db);
  62.  
  63. // Require user authentication
  64. $auth_id = $session->GetUserID($auth);
  65. if ($auth_id != null)
  66. {
  67.     // Check permission
  68.     $permission = new Permission($db);
  69.     $permissionCheck = $permission->Check($auth_id, "rum", Permission::WRITE);
  70.  
  71.     if ($permissionCheck == true)
  72.     {
  73.         // Permission check success - create record        
  74.         $product = new Rum_Product($db);
  75.  
  76.         $product->eshop_id = $data->eshop_id;
  77.         $product->bottle_id = $data->bottle_id;
  78.         $product->link = $data->link;
  79.         $product->name = $data->name;
  80.         $product->auction = $data->auction;
  81.         $product->stock = $data->stock;
  82.        
  83.         if (isset($data->confirmed))
  84.         {
  85.             $product->confirmed = $data->confirmed;
  86.         }
  87.  
  88.         if (isset($data->auction_start))
  89.         {
  90.             $product->auction_start = $data->auction_start;
  91.         }
  92.  
  93.         if (isset($data->auction_end))
  94.         {
  95.             $product->auction_end = $data->auction_end;
  96.         }
  97.  
  98.         $result = $product->Create();
  99.  
  100.         if ($result != null)
  101.         {
  102.             // Successfully created record
  103.             http_response_code(200);
  104.             echo json_encode(array("result" => 200, "product" => $result));
  105.         }
  106.         else
  107.         {
  108.             // DB Error during record creation
  109.             http_response_code(200);
  110.             echo json_encode(array("result" => 500, "error" => $db->GetLastError()));
  111.         }
  112.     }
  113.     else if ($permissionCheck === false)
  114.     {
  115.         // Permission check failure
  116.         http_response_code(200);
  117.         echo json_encode(array("result" => 401, "error" => "Unauthorized: Permission level too low."));
  118.     }
  119.     else
  120.     {
  121.         // DB Error during permission check
  122.         http_response_code(200);
  123.         echo json_encode(array("result" => 500, "error" => $db->GetLastError()));
  124.     }
  125. }
  126. else
  127. {
  128.     // User unauthorized
  129.     http_response_code(200);
  130.     echo json_encode(array("result" => 401, "error" => "Unauthorized"));
  131. }
  132.  
  133. // Close database connection
  134. $db->Disconnect();
  135.  
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement