www/includes/db_recipes.php

Thu, 20 Sep 2018 16:19:09 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 20 Sep 2018 16:19:09 +0200
changeset 57
bb9a06aa9acd
parent 52
b1f2a893572f
child 60
dbbe408108ea
permissions
-rw-r--r--

Completed framework inline fermentables editor

<?php

require($_SERVER['DOCUMENT_ROOT']."/config.php");
require($_SERVER['DOCUMENT_ROOT']."/version.php");
//require("../config.php");
//require("../version.php");


#Connect to the database
$connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
if (! $connect) {
	die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

$escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
$replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
$disallowed = array('visibleindex','uniqueid','boundindex','uid');

// get data and store in a json array
$query = "SELECT * FROM recipes ORDER BY st_guide,st_letter,st_name,name";
if (isset($_GET['insert']) || isset($_GET['update'])) {
	if (isset($_GET['insert'])) {
		// INSERT COMMAND
		$sql  = "INSERT INTO `recipes` SET st_name='" . mysqli_real_escape_string($connect, $_GET['st_name']);
	}
	if (isset($_GET['update'])) {
		// UPDATE COMMAND
		$sql  = "UPDATE `recipes` SET st_name='" . mysqli_real_escape_string($connect, $_GET['st_name']);
	}
	$sql .= "', st_letter='" . mysqli_real_escape_string($connect, $_GET['st_letter']);
	$sql .= "', st_guide='" . mysqli_real_escape_string($connect, $_GET['st_guide']);
	$sql .= "', st_og_min='" . $_GET['st_og_min'];
	$sql .= "', st_og_max='" . $_GET['st_og_max'];
	$sql .= "', st_fg_min='" . $_GET['st_fg_min'];
	$sql .= "', st_fg_max='" . $_GET['st_fg_max'];
	$sql .= "', st_ibu_min='" . $_GET['st_ibu_min'];
	$sql .= "', st_ibu_max='" . $_GET['st_ibu_max'];
	$sql .= "', st_color_min='" . $_GET['st_color_min'];
	$sql .= "', st_color_max='" . $_GET['st_color_max'];
	$sql .= "', st_carb_min='" . $_GET['st_carb_min'];
	$sql .= "', st_carb_max='" . $_GET['st_carb_max'];
	$sql .= "', st_abv_min='" . $_GET['st_abv_min'];
	$sql .= "', st_abv_max='" . $_GET['st_abv_max'];
	$sql .= "', name='" . mysqli_real_escape_string($connect, $_GET['name']);
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_GET['notes']);
	$sql .= "', type='" . $_GET['type'];
	$sql .= "', batch_size='" . $_GET['batch_size'];
	$sql .= "', boil_time='" . $_GET['boil_time'];
	$sql .= "', efficiency='" . $_GET['efficiency'];
	$sql .= "', est_og='" . $_GET['est_og'];
	$sql .= "', est_fg='" . $_GET['est_fg'];
	$sql .= "', est_color='" . $_GET['est_color'];
	$sql .= "', color_method='" . $_GET['color_method'];
	$sql .= "', est_ibu='" . $_GET['est_ibu'];
	$sql .= "', ibu_method='" . $_GET['ibu_method'];
	$sql .= "', mash_sparge_temp='" . $_GET['mash_sparge_temp'];
	$sql .= "', mash_ph='" . $_GET['mash_ph'];
	$sql .= "', mash_name='" . $_GET['mash_name'];

	syslog(LOG_NOTICE, $sql);	// Log upto this part.

	$array = $_GET['fermentables'];
	foreach($array as $key => $item){
		foreach ($disallowed as $disallowed_key) {
			unset($array[$key]["$disallowed_key"]);
		}
	}
	syslog(LOG_NOTICE, "json_fermentables: " . json_encode($array));
	$sql .= "', json_fermentables='" . json_encode($array);

	$array = $_GET['waters'];
	foreach($array as $key => $item){
		foreach ($disallowed as $disallowed_key) {
			unset($array[$key]["$disallowed_key"]);
		}
	}
	syslog(LOG_NOTICE, "json_waters: " . json_encode($array));
	$sql .= "', json_waters='" . json_encode($array);

	if (isset($_GET['insert'])) {
		$sql .= "';";
	}
	if (isset($_GET['update'])) {
		$sql .= "' WHERE record='" . $_GET['record'] . "';";
	}
//	$result = mysqli_query($connect, $sql);
//	if (! $result) {
//		syslog(LOG_NOTICE, "db_recipes: ".$sql." result: ".mysqli_error($connect));
//	} else {
//		syslog(LOG_NOTICE, "db_recipes: inserted ".$_GET['name']);
//	}
//	echo $result;

} else if (isset($_GET['delete'])) {
	// DELETE COMMAND
	$sql = "DELETE FROM `recipes` WHERE record='".$_GET['record']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_recipes: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_recipes: deleted record ".$_GET['record']);
	}
	echo $result;

} else {
	// SELECT COMMAND
	$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
	$recipes = '[';
	$comma = FALSE;
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		// Manual encode to JSON.
		if ($comma) {
			$recipes .= ',';
		}
		$comma = TRUE;
		$recipes .= '{"record":' . $row['record'];
		$recipes .=  ',"st_guide":"' . str_replace($escapers, $replacements, $row['st_guide']);
		$recipes .= '","st_letter":"' . str_replace($escapers, $replacements, $row['st_letter']);
		$recipes .= '","st_name":"'  . str_replace($escapers, $replacements, $row['st_name']);
		$recipes .= '","st_og_min":' . floatval($row['st_og_min']);
		$recipes .= ',"st_og_max":' . floatval($row['st_og_max']);
		$recipes .= ',"st_fg_min":' . floatval($row['st_fg_min']);
		$recipes .= ',"st_fg_max":' . floatval($row['st_fg_max']);
		$recipes .= ',"st_ibu_min":' . floatval($row['st_ibu_min']);
		$recipes .= ',"st_ibu_max":' . floatval($row['st_ibu_max']);
		$recipes .= ',"st_color_min":' . floatval($row['st_color_min']);
		$recipes .= ',"st_color_max":' . floatval($row['st_color_max']);
		$recipes .= ',"st_carb_min":' . floatval($row['st_carb_min']);
		$recipes .= ',"st_carb_max":' . floatval($row['st_carb_max']);
		$recipes .= ',"st_abv_min":' . floatval($row['st_abv_min']);
		$recipes .= ',"st_abv_max":' . floatval($row['st_abv_max']);
		$recipes .= ',"name":"' . str_replace($escapers, $replacements, $row['name']);
		$recipes .= '","notes":"' . str_replace($escapers, $replacements, $row['notes']);
		$recipes .= '","type":"' . $row['type'];
		$recipes .= '","batch_size":' . floatval($row['batch_size']);
		$recipes .= ',"boil_time":' . floatval($row['boil_time']);
		$recipes .= ',"efficiency":' . floatval($row['efficiency']);
		$recipes .= ',"est_og":' . floatval($row['est_og']);
		$recipes .= ',"est_fg":' . floatval($row['est_fg']);
		$recipes .= ',"est_color":' . floatval($row['est_color']);
		$recipes .= ',"color_method":"' . $row['color_method'];
		$recipes .= '","est_ibu":' . floatval($row['est_ibu']);
		$recipes .= ',"ibu_method":"' . $row['ibu_method'];
		$recipes .= '","mash_sparge_temp":' . $row['mash_sparge_temp'];
		$recipes .= ',"mash_ph":' . $row['mash_ph'];
		$recipes .= ',"mash_name":"' . $row['mash_name'];
		$recipes .= '","fermentables":' . $row['json_fermentables'];
		$recipes .= ',"hops":' . $row['json_hops'];
		$recipes .= ',"miscs":' . $row['json_miscs'];
		$recipes .= ',"yeasts":' . $row['json_yeasts'];
		$recipes .= ',"waters":' . $row['json_waters'];
		$recipes .= ',"mashs":' . $row['json_mashs'];
		$recipes .= '}';
	}
	$recipes .= ']';
	header("Content-type: application/json");
	echo $recipes;
}
?>

mercurial