-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
188 lines (182 loc) · 7.74 KB
/
api.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function getFileJSON($file) {
if (file_exists($file)) {
$handle = fopen($file, "r") or die("Unable to open file!");
$raw = fread($handle, filesize($file));
fclose($handle);
$json = json_decode($raw, true);
return $json;
} else {
return null;
}
}
function printJSON($json) {
header("Access-Control-Allow-Origin: *");
$output = json_encode($json, JSON_PRETTY_PRINT);
if(isset($_GET['pretty'])) {
echo '<pre>'; print_r($output); echo '</pre>';
} else {
echo $output."\n\r";
}
}
$SETTINGS = getFileJSON('settings.json');
if (!$SETTINGS) {
$SETTINGS = [
"API_KEY" => "DEFAULT_KEY"
];
}
if(isset($_GET['key']) && $_GET['key'] == $SETTINGS["API_KEY"]) {
function getRecipePhoto($recipe_name) {
$photo_path = $recipe_name.'/photo.jpg';
if (file_exists('recipes/'.$photo_path)) {
return $_SERVER['HTTP_HOST'].'/recipes/'.$photo_path;
} else if (file_exists('preparations/'.$photo_path)) {
return $_SERVER['HTTP_HOST'].'/preparations/'.$photo_path;
} else {
return $_SERVER['HTTP_HOST'].'/recipes/food.svg';
}
}
function displayDocs($title, $message) {
$doc_json = getFileJSON('documentation.json');
$doc_json['title'] = $title;
$doc_json['message'] = $message;
printJSON($doc_json);
}
if(isset($_GET['request'])) {
switch ($_GET['request']) {
case 'help':
displayDocs('Recipe API', 'Version 0.1.0');
break;
case 'get_one': # Get all of one recipe.
$recipe_set = (isset($_GET['recipe']) ? true : false);
$preparation_set = (isset($_GET['preparation']) ? true : false);
if ($recipe_set || $preparation_set) {
if ($recipe_set) {
$output_name = $_GET['recipe'];
$output_file = 'recipes/'.$output_name.'/recipe.json';
} else {
$output_name = $_GET['preparation'];
$output_file = 'preparations/'.$output_name.'/recipe.json';
}
if (file_exists($output_file)) {
$json = getFileJSON($output_file);
if (!is_null($json)) {
$photo_url = getRecipePhoto($output_name);
$json['photo'] = $photo_url;
printJSON($json);
} else {
displayDocs('Parse Error.', 'There was a problem getting the recipe from the data directory.');
}
} else {
displayDocs('Not Found.', "The recipe you requested doesn't exist.");
}
} else {
displayDocs('Missing Query String', 'The recipe query string is required for this request type');
}
break;
case 'get_multiple': # Get multiple recipes.
$output_json = [];
$files = glob('recipes/*/recipe.json');
shuffle($files);
foreach($files as $index=>$file) {
$recipe_name = explode('/', $file)[1];
$json = getFileJSON($file);
if (!is_null($json)) {
$recipe_json = [];
$recipe_json['recipe_id'] = $recipe_name;
$recipe_json['recipe_name'] = $json['recipe_name'];
$recipe_json['description'] = $json['description'];
$display_tags = array_merge(
$json['tags'],
$json['key_ingredients']
);
$recipe_json['tags'] = $display_tags;
$recipe_json['times'] = $json['times'];
$recipe_json['yields'] = $json['yields'];
$recipe_json['photo'] = getRecipePhoto($recipe_name);
array_push($output_json, $recipe_json);
}
}
printJSON($output_json);
break;
case 'get_all_filters': # Get all unique tags from all recipes.
case 'get_all_tags': # Get all unique tags from all recipes.
$files = glob('recipes/*/recipe.json');
$output_tags = [];
foreach($files as $file) {
$recipe_name = explode('/', $file)[1];
$json = getFileJSON($file);
if (!is_null($json)) {
foreach ($json['tags'] as $tag) {
if (!in_array($tag, $output_tags)) {
array_push($output_tags, $tag);
}
}
}
}
sort($output_tags);
printJSON($output_tags);
break;
case 'get_all_key_ingredients': # Get all unique tags from all recipes.
$files = glob('recipes/*/recipe.json');
$output_key_ingredients = [];
foreach($files as $file) {
$recipe_name = explode('/', $file)[1];
$json = getFileJSON($file);
if (!is_null($json)) {
foreach ($json['key_ingredients'] as $tag) {
if (!in_array($tag, $output_key_ingredients)) {
array_push($output_key_ingredients, $tag);
}
}
}
}
sort($output_key_ingredients);
printJSON($output_key_ingredients);
break;
case 'get_all_dish_types': # Get all unique tags from all recipes.
$files = glob('recipes/*/recipe.json');
$output_dish_types = [];
foreach($files as $file) {
$recipe_name = explode('/', $file)[1];
$json = getFileJSON($file);
if (!is_null($json)) {
foreach ($json['dish_type'] as $tag) {
if (!in_array($tag, $output_dish_types)) {
array_push($output_dish_types, $tag);
}
}
}
}
sort($output_dish_types);
printJSON($output_dish_types);
break;
case 'get_all_meal_types': # Get all unique tags from all recipes.
$files = glob('recipes/*/recipe.json');
$output_meal_types = [];
foreach($files as $file) {
$recipe_name = explode('/', $file)[1];
$json = getFileJSON($file);
if (!is_null($json)) {
foreach ($json['meal'] as $tag) {
if (!in_array($tag, $output_meal_types)) {
array_push($output_meal_types, $tag);
}
}
}
}
sort($output_meal_types);
printJSON($output_meal_types);
break;
default:
displayDocs('Malformed Request', 'You must pass a recognized request type');
break;
}
} else { displayDocs('Not Found.', 'There is something wrong with your request.'); }
} else {
print "Invalid API Key. You must provide a valid API key.";
}
?>