Sat, 26 Jan 2019 19:25:10 +0100
Water profile uses POST instead of GET. Added tooltips to the editor screen. Updated the editor screen. Total alkalinity and Bicarbonate update each other. More and smaller columns in the listing screen. Rearranged the editor screen.
<?php require($_SERVER['DOCUMENT_ROOT']."/config.php"); require($_SERVER['DOCUMENT_ROOT']."/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()); } mysqli_set_charset($connect, "utf8" ); $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); $rescapers = array("'"); $rreplacements = array("\\'"); $disallowed = array('visibleindex','uniqueid','boundindex','uid','h_weight','m_weight'); if (isset($_POST['insert']) || isset($_POST['update'])) { if (isset($_POST['insert'])) { // INSERT COMMAND $sql = "INSERT INTO `recipes` SET "; } if (isset($_POST['update'])) { // UPDATE COMMAND $sql = "UPDATE `recipes` SET "; } // Basic settings if (isset($_POST['uuid'])) { $sql .= "uuid='" . $_POST['uuid']; } else { $uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid')); $sql .= "uuid='" . $uuid; } ($_POST['locked'] == 'true') ? $sql .= "', locked='1" : $sql .= "', locked='0"; $sql .= "', st_name='" . mysqli_real_escape_string($connect, $_POST['st_name']); $sql .= "', st_letter='" . mysqli_real_escape_string($connect, $_POST['st_letter']); $sql .= "', st_guide='" . mysqli_real_escape_string($connect, $_POST['st_guide']); $sql .= "', st_type='" . mysqli_real_escape_string($connect, $_POST['st_type']); $sql .= "', st_category='" . mysqli_real_escape_string($connect, $_POST['st_category']); $sql .= "', st_category_number='" . $_POST['st_category_number']; $sql .= "', st_og_min='" . $_POST['st_og_min']; $sql .= "', st_og_max='" . $_POST['st_og_max']; $sql .= "', st_fg_min='" . $_POST['st_fg_min']; $sql .= "', st_fg_max='" . $_POST['st_fg_max']; $sql .= "', st_ibu_min='" . $_POST['st_ibu_min']; $sql .= "', st_ibu_max='" . $_POST['st_ibu_max']; $sql .= "', st_color_min='" . $_POST['st_color_min']; $sql .= "', st_color_max='" . $_POST['st_color_max']; $sql .= "', st_carb_min='" . $_POST['st_carb_min']; $sql .= "', st_carb_max='" . $_POST['st_carb_max']; $sql .= "', st_abv_min='" . $_POST['st_abv_min']; $sql .= "', st_abv_max='" . $_POST['st_abv_max']; $sql .= "', name='" . mysqli_real_escape_string($connect, $_POST['name']); $sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']); $sql .= "', type='" . $_POST['type']; $sql .= "', batch_size='" . $_POST['batch_size']; $sql .= "', boil_size='" . $_POST['boil_size']; $sql .= "', boil_time='" . $_POST['boil_time']; $sql .= "', efficiency='" . $_POST['efficiency']; $sql .= "', est_og='" . $_POST['est_og']; $sql .= "', est_fg='" . $_POST['est_fg']; $sql .= "', est_abv='" . $_POST['est_abv']; $sql .= "', est_carb='" . $_POST['est_carb']; $sql .= "', est_color='" . $_POST['est_color']; $sql .= "', color_method='" . $_POST['color_method']; $sql .= "', est_ibu='" . $_POST['est_ibu']; $sql .= "', ibu_method='" . $_POST['ibu_method']; $sql .= "', sparge_temp='" . $_POST['sparge_temp']; $sql .= "', sparge_ph='" . $_POST['sparge_ph']; $sql .= "', sparge_volume='" . $_POST['sparge_volume']; $sql .= "', sparge_source='" . $_POST['sparge_source']; $sql .= "', sparge_acid_type='" . $_POST['sparge_acid_type']; $sql .= "', sparge_acid_perc='" . $_POST['sparge_acid_perc']; $sql .= "', sparge_acid_amount='" . $_POST['sparge_acid_amount']; $sql .= "', mash_ph='" . $_POST['mash_ph']; $sql .= "', mash_name='" . $_POST['mash_name']; ($_POST['calc_acid'] == 'true') ? $sql .= "', calc_acid='1" : $sql .= "', calc_acid='0"; if (isset($_POST['w1_name'])) { $sql .= "', w1_name='" . mysqli_real_escape_string($connect, $_POST['w1_name']); $sql .= "', w1_amount='" . $_POST['w1_amount']; $sql .= "', w1_calcium='" . $_POST['w1_calcium']; $sql .= "', w1_sulfate='" . $_POST['w1_sulfate']; $sql .= "', w1_chloride='" . $_POST['w1_chloride']; $sql .= "', w1_sodium='" . $_POST['w1_sodium']; $sql .= "', w1_magnesium='" . $_POST['w1_magnesium']; $sql .= "', w1_total_alkalinity='" . $_POST['w1_total_alkalinity']; $sql .= "', w1_ph='" . $_POST['w1_ph']; $sql .= "', w1_cost='" . $_POST['w1_cost']; } if (isset($_POST['w2_name'])) { $sql .= "', w2_name='" . mysqli_real_escape_string($connect, $_POST['w2_name']); $sql .= "', w2_amount='" . $_POST['w2_amount']; $sql .= "', w2_calcium='" . $_POST['w2_calcium']; $sql .= "', w2_sulfate='" . $_POST['w2_sulfate']; $sql .= "', w2_chloride='" . $_POST['w2_chloride']; $sql .= "', w2_sodium='" . $_POST['w2_sodium']; $sql .= "', w2_magnesium='" . $_POST['w2_magnesium']; $sql .= "', w2_total_alkalinity='" . $_POST['w2_total_alkalinity']; $sql .= "', w2_ph='" . $_POST['w2_ph']; $sql .= "', w2_cost='" . $_POST['w2_cost']; } syslog(LOG_NOTICE, $sql); if (isset($_POST['fermentables'])) { $array = $_POST['fermentables']; foreach($array as $key => $item){ foreach ($disallowed as $disallowed_key) { unset($array[$key]["$disallowed_key"]); } } syslog(LOG_NOTICE, "json_fermentables=: ".str_replace($rescapers,$rreplacements,json_encode($array))); $sql .= "', json_fermentables='" . str_replace($rescapers,$rreplacements,json_encode($array)); } if (isset($_POST['hops'])) { $array = $_POST['hops']; foreach($array as $key => $item){ foreach ($disallowed as $disallowed_key) { unset($array[$key]["$disallowed_key"]); } } syslog(LOG_NOTICE, "json_hops: ".str_replace($rescapers,$rreplacements,json_encode($array))); $sql .= "', json_hops='" . str_replace($rescapers,$rreplacements,json_encode($array)); } if (isset($_POST['miscs'])) { $array = $_POST['miscs']; foreach($array as $key => $item){ foreach ($disallowed as $disallowed_key) { unset($array[$key]["$disallowed_key"]); } } syslog(LOG_NOTICE, "json_miscs: ".str_replace($rescapers,$rreplacements,json_encode($array))); $sql .= "', json_miscs='" . str_replace($rescapers,$rreplacements,json_encode($array)); } if (isset($_POST['yeasts'])) { $array = $_POST['yeasts']; foreach($array as $key => $item){ foreach ($disallowed as $disallowed_key) { unset($array[$key]["$disallowed_key"]); } } syslog(LOG_NOTICE, "json_yeasts: ". str_replace($rescapers,$rreplacements,json_encode($array))); $sql .= "', json_yeasts='" . str_replace($rescapers,$rreplacements,json_encode($array)); } if (isset($_POST['mashs'])) { $array = $_POST['mashs']; foreach($array as $key => $item){ foreach ($disallowed as $disallowed_key) { unset($array[$key]["$disallowed_key"]); } } syslog(LOG_NOTICE, "json_mashs: ".str_replace($rescapers,$rreplacements,json_encode($array))); $sql .= "', json_mashs='" . str_replace($rescapers,$rreplacements,json_encode($array)); } if (isset($_POST['insert'])) { $sql .= "';"; } if (isset($_POST['update'])) { $sql .= "' WHERE record='" . $_POST['record'] . "';"; } $result = mysqli_query($connect, $sql); if (! $result) { syslog(LOG_NOTICE, "db_recipes: result: ".mysqli_error($connect)); } else { if (isset($_POST['update'])) { syslog(LOG_NOTICE, "db_recipes: updated record ".$_POST['record']); } else { $lastid = mysqli_insert_id($connect); syslog(LOG_NOTICE, "db_recipes: inserted record ".$lastid); } } echo $result; } else if (isset($_POST['delete'])) { // DELETE COMMAND $sql = "DELETE FROM `recipes` WHERE record='".$_POST['record']."';"; $result = mysqli_query($connect, $sql); if (! $result) { syslog(LOG_NOTICE, "db_recipes: result: ".mysqli_error($connect)); } else { syslog(LOG_NOTICE, "db_recipes: deleted record ".$_POST['record']); } echo $result; } else { /* * Select all or a given record. */ if (isset($_GET['record'])) { $query = "SELECT * FROM recipes WHERE record='" . $_GET['record'] . "';"; } else { $query = "SELECT * FROM recipes ORDER BY st_guide,st_letter,st_name,name"; } $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 .= ',"uuid":"' . $row['uuid']; $recipes .= '","locked":' . $row['locked']; $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_type":"' . str_replace($escapers, $replacements, $row['st_type']); $recipes .= '","st_category":"' . str_replace($escapers, $replacements, $row['st_category']); $recipes .= '","st_category_number":' . floatval($row['st_category_number']); $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_size":' . floatval($row['boil_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_abv":' . floatval($row['est_abv']); $recipes .= ',"est_carb":' . floatval($row['est_carb']); $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 .= '","sparge_temp":' . $row['sparge_temp']; $recipes .= ',"sparge_ph":' . $row['sparge_ph']; $recipes .= ',"sparge_volume":' . $row['sparge_volume']; $recipes .= ',"sparge_source":"' . $row['sparge_source']; $recipes .= '","sparge_acid_type":"' . $row['sparge_acid_type']; $recipes .= '","sparge_acid_perc":' . $row['sparge_acid_perc']; $recipes .= ',"sparge_acid_amount":' . $row['sparge_acid_amount']; $recipes .= ',"mash_ph":' . $row['mash_ph']; $recipes .= ',"mash_name":"' . $row['mash_name']; $recipes .= '","calc_acid":' . $row['calc_acid']; $recipes .= ',"w1_name":"' . str_replace($escapers, $replacements, $row['w1_name']); $recipes .= '","w1_amount":' . $row['w1_amount']; $recipes .= ',"w1_calcium":' . $row['w1_calcium']; $recipes .= ',"w1_sulfate":' . $row['w1_sulfate']; $recipes .= ',"w1_chloride":' . $row['w1_chloride']; $recipes .= ',"w1_sodium":' . $row['w1_sodium']; $recipes .= ',"w1_magnesium":' . $row['w1_magnesium']; $recipes .= ',"w1_total_alkalinity":' . $row['w1_total_alkalinity']; $recipes .= ',"w1_ph":' . $row['w1_ph']; $recipes .= ',"w1_cost":' . $row['w1_cost']; $recipes .= ',"w2_name":"' . str_replace($escapers, $replacements, $row['w2_name']); $recipes .= '","w2_amount":' . $row['w2_amount']; $recipes .= ',"w2_calcium":' . $row['w2_calcium']; $recipes .= ',"w2_sulfate":' . $row['w2_sulfate']; $recipes .= ',"w2_chloride":' . $row['w2_chloride']; $recipes .= ',"w2_sodium":' . $row['w2_sodium']; $recipes .= ',"w2_magnesium":' . $row['w2_magnesium']; $recipes .= ',"w2_total_alkalinity":' . $row['w2_total_alkalinity']; $recipes .= ',"w2_ph":' . $row['w2_ph']; $recipes .= ',"w2_cost":' . $row['w2_cost']; $recipes .= ',"fermentables":' . $row['json_fermentables']; $recipes .= ',"hops":' . $row['json_hops']; $recipes .= ',"miscs":' . $row['json_miscs']; $recipes .= ',"yeasts":' . $row['json_yeasts']; $recipes .= ',"mashs":' . $row['json_mashs']; $recipes .= '}'; } $recipes .= ']'; header("Content-type: application/json"); echo $recipes; } ?>