-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchfoodnutrients.php
executable file
·52 lines (44 loc) · 2.52 KB
/
fetchfoodnutrients.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
$food_id = $_GET["food"];
$service_id = $_GET["service"];
// Authenticate using provided service ID.
$service_id = strtolower($service_id);
if ($service_id != preg_replace("/[^a-f0-9]/", '', $service_id)) { // Check to see if the service identifier contains disallowed characters.
echo "{'error': {'id': 'invalid_service', 'reason': 'disallowed_characters', 'description': 'The service identifier contains invalid characters.'}}";
exit();
}
if (strlen($service_id) > 100) { // Check to see if the service identifier is excessively long.
echo "{'error': {'id': 'invalid_service', 'reason': 'too_long', 'description': 'The service identifier is excessively long.'}}";
exit();
} else if (strlen($service_id) < 8) {
echo "{'error': {'id': 'invalid_service', 'reason': 'too_short', 'description': 'The service identifier is too short.'}}";
exit();
}
include "./servicedata.php";
$services = load_servicedata();
$associated_user = find_serviceid($service_id, $services); // Search for the provided service ID in the service database.
if ($associated_user == false) {
echo "{'error': {'id': 'invalid_service', 'reason': 'not_found', 'description': 'The specified service identifier does not exist.'}}";
exit();
}
if (check_permissions_action($service_id, "foods-fetch-nutrients", $services) == false) {
echo "{'error': {'id': 'invalid_service', 'reason': 'permission_denied', 'description': 'The specified service identifier does not have permission to fetch food nutrient information.'}}";
exit();
}
include "./fooddata.php";
$food_data = load_food();
if ($food_id != preg_replace("/[^a-zA-Z0-9\-_]/", '', $food_id)) { // Check to see if the provided food_id contains disallowed values.
echo "{'error': {'id': 'invalid_id', 'description': 'The submitted food_id is invalid because it contains disallowed characters.'}}";
echo "<p>The provided food ID contains disallowed characters.</p>";
exit();
} else if (strlen($food_id) >= 100) { // Check of the provided food_id is excessively long.
echo "{'error': {'id': 'invalid_id', 'description': 'The submitted food_id is invalid because it is excessively long.'}}";
exit();
}
if (!in_array($food_id, array_keys($food_data["entries"][$associated_user]["foods"]))) {
echo "{'error': {'id': 'invalid_id', 'description': 'The specified food_id does not exist in this user's food database.'}}";
exit();
}
unset($food_data["entries"][$associated_user]["foods"][$food_id]["service"]);
echo json_encode($food_data["entries"][$associated_user]["foods"][$food_id]);
?>