diff --git a/www/api/v1/api-init.php b/www/api/v1/api-init.php index e188fa4..4a2e99a 100644 --- a/www/api/v1/api-init.php +++ b/www/api/v1/api-init.php @@ -22,6 +22,28 @@ function returns($content = 'Success', $code = 0){ die(); } +function returnsErr($alternate = ''){ + global $db; + if($error = $db->error){ + returns("Database error: ".$error, 1); + } + if($alternate != ''){ + returns($alternate, 2); + } + + returns("Error", 2); +} + +function checkArgs($args){ + foreach($args as $key => $arg){ + if($arg == ""){ + return $key; + } + } + return true; +} + + function sanitize(): array{ global $_GET, $_POST; $data = array(); @@ -29,6 +51,7 @@ function sanitize(): array{ foreach([$_GET, $_POST] as $request){ if(!empty($request)){ foreach($request as $key => $value){ + if(in_array($value, ['string', 'integer', 'number', 'boolean'])){ $value = ''; } // API DEFAULTS CLEAN if(($data[$key] = filter($value)) === false){ print_r($value); echo "Failed to sanitize: `".$key."`: ".$value." \t-\t type: ".gettype($value)."\n"; diff --git a/www/api/v1/docs.php b/www/api/v1/docs.php index 678400d..55b9ec3 100644 --- a/www/api/v1/docs.php +++ b/www/api/v1/docs.php @@ -7,6 +7,9 @@ error_reporting(E_ALL); $docs['general'][] = array("method"=>'GET', "href"=>"/", "name"=>"Nothing", "body"=>"{}"); $docs['recipe'][] = array("method"=>'GET', "href"=>"/recipe", "name"=>"Get all available recipes", "body"=>"{}"); $docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Create recipe", "body"=>"{\n \"name\": \"string\",\n \"portions\": \"integer\",\n \"public\": \"boolean\" \n}"); +$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Add item to recipe", "body"=>"{\n \"recipe_id\": \"integer\",\n \"name\": \"string\",\n \"price\": \"number\",\n \"amount\": \"integer\" \n}"); +$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Edit recipe item", "body"=>"{\n \"recipe_id\": \"integer\",\n \"recipe_item_id\": \"integer\",\n \"newName\": \"string\",\n \"newPrice\": \"number\",\n \"newAmount\": \"integer\",\n \"newItem_id\": \"integer\" \n}"); +$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Delete recipe item", "body"=>"{\n \"recipe_id\": \"integer\",\n \"del_item_id\": \"integer\",\n \"delName\": \"string\" \n}"); @@ -61,7 +64,7 @@ function capitalizeFirst($input){ ?>