function rest_get($req) { global $JSON; if (checkPrivileges($req[0]) == false) { return; } $resp = reqRouter($req, "GET"); $response; switch ($resp) { case 1: global $routes; $order = @$routes[$req[0]]["orderBy"]; if (isset($order)) { $response = sql_GET_ALL($req[0], [$order, "ASC"]); } else { $response = sql_GET_ALL($req[0], [$routes[$req[0]]['identifier'], "ASC"]); } break; case 2: $response = sql_GET($req); break; case 3: $response = sql_GET_ROW($req); break; case 4: $response = sql_GET_SORT($req, true); break; case 5: $response = sql_GET_SORT($req, false); break; case 6: $response = sql_GET_COLUMNS(); break; case 7: $response = sql_GET_JOIN($JSON); break; case 8: $response = getPrice($JSON); break; case 9: $response = getByTime($req, $JSON); break; case 0: default: rest_error("Mal-Formed request, check url params", 400); return; } if (isset($response) && (is_array($response) && count($response) > 0)) { rest_success(json_encode($response)); } else { rest_error("Empty Results, Check if item exists in dataBase, Check Url requested.", 404); } return 0; }
function getPrice($JSON) { include '../../includes/database.php'; //json should contain: order(s) to be priced $orderName = "orderName"; if (isset($JSON[$orderName]) == false) { //return empty array to throw error return []; } $price = isset($JSON["actual"]) ? "Cost" : "Price"; $arr = []; $places = pow(10, 5); $allPossibles = sql_GET_JOIN(["tables" => ["symbols"], "from" => "ingredients", "relations" => [["symbols.Name", "ingredients.Symbol"]], "select" => ["symbols.Symbol", "ingredients." . $price, "ingredients.Units"]]); $order = explode(sql_GET(["settings", "search", "keyKey", "dbdelimiter"])[0]["val"], $JSON[$orderName]); foreach ($order as $i => $ingrediant) { $num = isInside($allPossibles, "Symbol", $ingrediant); if ($num == -1) { return []; } $cur = $allPossibles[$num]; array_push($arr, $cur[$price] / $cur["Units"]); } return [floor(array_reduce($arr, "add") * $places) / $places]; }
function rest_put($req) { global $routes; global $JSON; include '../../includes/database.php'; $table = $req[0]; if (checkPrivileges($table) == false || checkTableReqs($table, $JSON) == false) { rest_error("Insufficient Priveleges OR incorrect JSON Requirements", 401); return; } $ret = reqRouter($req, "PUT"); if ($ret == 0) { rest_error("Item Exists Or Incorrect JSON Properties.", 409); return; } else { if ($ret == 2) { if (!isset($JSON["OrderSymbols"])) { rest_error("NO Order received, check JSON", 406); } $list = $JSON["OrderSymbols"]; $orders = explode(" , ", $list); $arr = []; $allPossibles = sql_GET_JOIN(["tables" => ["symbols"], "from" => "ingredients", "relations" => [["symbols.Name", "ingredients.Symbol"]], "select" => ["symbols.Symbol"]]); for ($i = 0; $i < count($orders); $i++) { $ingredients = explode(" ", $orders[$i]); //from here we need to check that each ingrediant is valid and available? for ($x = 0; $x < count($ingredients); $x++) { $ingrediant = $ingredients[$x]; $num = isInside($allPossibles, "Symbol", $ingrediant); if ($num == -1) { rest_error($ingrediant . " is not a valid ingredient!", 406); return; } $cur = $allPossibles[$num]; } } $table = "orders"; $JSON["TransactionID"] = getTransaction(); } } if ($req[0] == "users") { if (!filter_var($JSON["Email"], FILTER_VALIDATE_EMAIL)) { rest_error("Invalid Email, Please Enter a Valid Email address.", 406); return; } } $stmt = $db->prepare(sql_PUT($table)); $ex = buildJSONInputWProps($table, $JSON); if (is_string($ex)) { rest_error("Property: '" . $ex . "' is not set on provided JSON Object. Your JSON May be Mal-Formed,incorrect for the database or some other error may have occured", 400); return; } $var = $stmt->execute($ex); if ($var) { rest_success('Inputted Successfully Into the DataBase!'); } else { rest_error('Input unsuccessful. Check spelling this is usually thrown when an item should match another tables item.', 406); } /* $stmt = $db->prepare(sql_PUT($req)); $stmt->execute(array(':fname' => $fname, ':lname' => $lname,':email' => $email,':pass' => $password,':verified'=>0));*/ return 0; }