From ecaa540af575077444d5ef96d730ff0c96cfca73 Mon Sep 17 00:00:00 2001 From: cos Date: Sat, 3 Oct 2015 21:45:16 +0200 Subject: Disable debug rulers in sheetprint. --- sheetprint | 54 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/sheetprint b/sheetprint index f0052f5..701b9b5 100755 --- a/sheetprint +++ b/sheetprint @@ -26,37 +26,39 @@ my $white = $a4->colorAllocate(255,255,255); my $black = $a4->colorAllocate(0,0,0); $a4->fill(0, 0, $white); -# Draw a cross -$a4->line(0, 0, $a4->width(), $a4->height, $black); -$a4->line($a4->width(), 0, 0, $a4->height, $black); +sub draw_rulers() +{ + # Draw a cross + $a4->line(0, 0, $a4->width(), $a4->height, $black); + $a4->line($a4->width(), 0, 0, $a4->height, $black); -# Draw some rulers -for ( my $i = 1; $i < 297; $i++) { - if ( $i % 10 == 0 ) { - $a4->line(0, mm2dots($i), 20, mm2dots($i), $black); - $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 20, mm2dots($i), $black); - } elsif ( $i % 5 == 0 ) { - $a4->line(0, mm2dots($i), 15, mm2dots($i), $black); - $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 15, mm2dots($i), $black); - } else { - $a4->line(0, mm2dots($i), 10, mm2dots($i), $black); - $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 10, mm2dots($i), $black); + for ( my $i = 1; $i < 297; $i++) { + if ( $i % 10 == 0 ) { + $a4->line(0, mm2dots($i), 20, mm2dots($i), $black); + $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 20, mm2dots($i), $black); + } elsif ( $i % 5 == 0 ) { + $a4->line(0, mm2dots($i), 15, mm2dots($i), $black); + $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 15, mm2dots($i), $black); + } else { + $a4->line(0, mm2dots($i), 10, mm2dots($i), $black); + $a4->line(mm2dots(105), mm2dots($i), mm2dots(105) + 10, mm2dots($i), $black); + } } -} -for ( my $i = 1; $i < 210; $i++) { - if ( $i % 10 == 0 ) { - $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 20, $black); - $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 20, $black); - } elsif ( $i % 5 == 0 ) { - $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 15, $black); - $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 15, $black); - } else { - $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 10, $black); - $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 10, $black); + for ( my $i = 1; $i < 210; $i++) { + if ( $i % 10 == 0 ) { + $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 20, $black); + $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 20, $black); + } elsif ( $i % 5 == 0 ) { + $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 15, $black); + $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 15, $black); + } else { + $a4->line(mm2dots($i), mm2dots(297), mm2dots($i), mm2dots(297) - 10, $black); + $a4->line(mm2dots($i), mm2dots(149), mm2dots($i), mm2dots(149) - 10, $black); + } } } -my $skip = 2; +my $skip = 0; my $x = mm2dots(25); my $y = mm2dots(32) + $skip * mm2dots(25); -- cgit v1.2.3 From 50bfecf318df7c4a2d008d18c496a2058d6789e2 Mon Sep 17 00:00:00 2001 From: cos Date: Sat, 3 Oct 2015 21:56:21 +0200 Subject: Initialize counter correctly. --- mat | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mat b/mat index 707a675..12c67d8 100755 --- a/mat +++ b/mat @@ -14,6 +14,7 @@ use Text::Iconv; use utf8; binmode(STDOUT, 'utf8:'); +# FIXME die on non-existant config tie my %Config, "Config::Simple", '/etc/mat.conf'; #use Data::Dumper; @@ -297,6 +298,7 @@ sub cmd_inventory { $sql .= " WHERE storage='$storage'" if ( $storage ); $sql .= " GROUP BY storage, preparation_date, recipe_id"; + $total{'all'} = 0; my $all = $db->selectall_arrayref($sql); foreach my $row (@$all) { $total{'all'} += @$row[1]; -- cgit v1.2.3 From 7ccd884b6d57e84df601e689336479a74b913ce2 Mon Sep 17 00:00:00 2001 From: cos Date: Sat, 3 Oct 2015 22:15:08 +0200 Subject: mat.cgi: Output weight & energy. --- cgi-bin/mat.cgi | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cgi-bin/mat.cgi b/cgi-bin/mat.cgi index 793671a..d61db32 100755 --- a/cgi-bin/mat.cgi +++ b/cgi-bin/mat.cgi @@ -50,11 +50,13 @@ sub cmd_view($) my $db = DBI->connect($Config{'database'}, "", "", {HandleError => \&misconfigured, AutoCommit => 1}); - my $recipe_row = $db->selectrow_arrayref("SELECT name, storage, uri FROM ". - "recipes AS r JOIN inventory AS i ON i.recipe_id=r.id WHERE i.id=".$id. - ";"); + my $recipe_row = $db->selectrow_arrayref("SELECT name, storage, uri, ". + "amount, energy FROM recipes AS r JOIN inventory AS i ". + "ON i.recipe_id=r.id WHERE i.id=".$id.";"); print $id, "
", $$recipe_row[0], "
", $$recipe_row[1], "
"; + print ($$recipe_row[3] ? $$recipe_row[3]." g
" : ""), "
"; + print ($$recipe_row[4] ? $$recipe_row[4]." kJ
" : ""), "
"; if(substr($$recipe_row[2], 0, 4) eq "http") { print ''.$$recipe_row[2].'', "
"; } else { -- cgit v1.2.3 From f28631984cb8ea86193541c0564873182a9c79d6 Mon Sep 17 00:00:00 2001 From: cos Date: Sat, 3 Oct 2015 23:43:14 +0200 Subject: Add some unit conversion to shoppinglist. --- mat | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/mat b/mat index 12c67d8..7d60ec8 100755 --- a/mat +++ b/mat @@ -127,7 +127,7 @@ my @schedule = ( sub convert_to_unit { my ( $out_unit, $id, $in_amount, $in_unit ) = @_; my %out_amount; - my %convert; + my %convert; # volume [l], weight [kg], piece [st] # SELECT DISTINCT unit FROM contents; # msk, krm, dl, tsk, cl, l, ml, nypa @@ -147,9 +147,9 @@ sub convert_to_unit { } elsif ($in_unit eq "krm") { $convert{'volume'} = $in_amount * 0.001; } elsif ($in_unit eq "g") { - $convert{'weight'} = $in_amount; + $convert{'weight'} = $in_amount / 1000; } elsif ($in_unit eq "kg") { - $convert{'weight'} = $in_amount * 1000; + $convert{'weight'} = $in_amount; } elsif ($in_unit eq "st") { $convert{'piece'} = $in_amount; } @@ -165,10 +165,11 @@ sub convert_to_unit { if ( $convert{'volume'} ) { $volume = 0.001 * $convert{'volume'}; # m³ if ($ingredient->{density}) { - $out_amount{'weight'} = 1000 * $density * $volume; # g + $out_amount{'weight'} = $density * $volume; # kg } } elsif ( $convert{'weight'} ) { - $weight = $convert{'weight'} / 1000; # kg + $weight = $convert{'weight'}; # kg + $out_amount{'weight'} = $weight; if ($ingredient->{'piece_weight'}) { $piece_weight = $ingredient->{'piece_weight'}; # g/piece $out_amount{'piece'} = $convert{'weight'} / @@ -185,6 +186,12 @@ sub convert_to_unit { } if ($out_unit eq "g") { + if($out_amount{'weight'}) { + return $out_amount{'weight'} * 1000; + } else { + return + } + } elsif ($out_unit eq "kg") { return $out_amount{'weight'}; } elsif ($out_unit eq "st") { return $out_amount{'piece'}; @@ -926,7 +933,7 @@ sub cmd_shoppinglist { for my $content ( keys(%$contents)) { my %shop; # print %$contents, "\n"; - my $ingredientcol = $db->selectcol_arrayref("SELECT name FROM ingredients WHERE id=".$content.";"); + my $ingredientrow = $db->selectrow_arrayref("SELECT name, primary_unit FROM ingredients WHERE id=".$content.";"); # print $content, " ", @$ingredientcol[0], "\n"; # print $$contents{$content}{unit}, $content, " ", @$ingredientcol[0], "\n"; # print keys(%$contents), "\n"; @@ -934,12 +941,34 @@ sub cmd_shoppinglist { $shop{quantity} = $$contents{$content}{quantity}; # $shop{shop_position} = $shop{unit} = $$contents{$content}{unit}; - $shop{ingredient} = @$ingredientcol[0]; + $shop{ingredient} = @$ingredientrow[0]; + $shop{primary_unit} = @$ingredientrow[1]; $shop{recipe}[0] = $recipe; push @shop_ingredients, \%shop; } } - my @sorted_ingredients = sort { $a->{ingredient} cmp $b->{ingredient} } @shop_ingredients; + + + my @converted_ingredients; + for my $ingredient ( @shop_ingredients ) + { + my $convert_unit; + my $convert_result; + + $convert_unit = "g"; + + if ($convert_unit) { + $convert_result = convert_to_unit($convert_unit, $ingredient->{'id'}, + $ingredient->{'quantity'}, $ingredient->{'unit'}); + if ( $convert_result ) { + $ingredient->{'unit'} = $convert_unit; + $ingredient->{'quantity'} = $convert_result; + } + } + push @converted_ingredients, $ingredient; + } + + my @sorted_ingredients = sort { $a->{ingredient} cmp $b->{ingredient} } @converted_ingredients; my @squeezed_ingredients; for my $squeeze ( @sorted_ingredients ) { @@ -950,10 +979,21 @@ sub cmd_shoppinglist { push @squeezed_ingredients, $squeeze; } } -# for my $shop ( @sorted_ingredients ) for my $shop ( @squeezed_ingredients ) { - printf "%4s %-8s %-40s", $shop->{quantity}, $shop->{unit}, $shop->{ingredient}; + my $convert_result; + if ($shop->{'primary_unit'}) { + $convert_result = convert_to_unit($shop->{'primary_unit'}, $shop->{'id'}, + $shop->{'quantity'}, $shop->{'unit'}); + } + + printf "%4s %-8s ", $shop->{quantity}, $shop->{unit}; + if ($convert_result) { + printf "%4.4s %-8s ", $convert_result, $shop->{'primary_unit'}; + } else { + printf "%4.4s %-8s ", "", ""; + } + printf "%-40s", $shop->{ingredient}; for ( @{$shop->{recipe}} ) { print get_recipe_name($_), ", "; } -- cgit v1.2.3 From 7ab3e35b073fe488315c6f9603151d2302a0261b Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 17 Jul 2016 20:09:33 +0200 Subject: Make code a tiny little bit more clean. --- cgi-bin/mat.cgi | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/cgi-bin/mat.cgi b/cgi-bin/mat.cgi index d61db32..52239d5 100755 --- a/cgi-bin/mat.cgi +++ b/cgi-bin/mat.cgi @@ -50,17 +50,18 @@ sub cmd_view($) my $db = DBI->connect($Config{'database'}, "", "", {HandleError => \&misconfigured, AutoCommit => 1}); - my $recipe_row = $db->selectrow_arrayref("SELECT name, storage, uri, ". - "amount, energy FROM recipes AS r JOIN inventory AS i ". - "ON i.recipe_id=r.id WHERE i.id=".$id.";"); - - print $id, "
", $$recipe_row[0], "
", $$recipe_row[1], "
"; - print ($$recipe_row[3] ? $$recipe_row[3]." g
" : ""), "
"; - print ($$recipe_row[4] ? $$recipe_row[4]." kJ
" : ""), "
"; - if(substr($$recipe_row[2], 0, 4) eq "http") { - print ''.$$recipe_row[2].'', "
"; + my $portion = $db->selectrow_hashref("SELECT name, storage, uri, amount, ". + "energy FROM recipes AS r JOIN inventory AS i ON i.recipe_id=r.id ". + "WHERE i.id=".$id.";"); + + print $id, "
", $portion->{name}, "
\n"; + print $portion->{storage}, "
\n"; + print $portion->{amount} ? $portion->{amount}." g
" : "", "
\n"; + print $portion->{energy} ? $portion->{energy}." kJ
" : "", "
\n"; + if(substr($portion->{uri}, 0, 4) eq "http") { + print ''.$portion->{uri}.'', "
\n"; } else { - print $$recipe_row[2], "
"; + print $portion->{uri}, "
\n"; } if ($session->param('authenticated') eq "yes") { print "\n"; @@ -68,7 +69,7 @@ sub cmd_view($) print " ". "$store \n"; } - print ""; + print "\n"; } } @@ -90,8 +91,8 @@ misconfigured unless ($Config{'database'}); misconfigured unless $session; invalid_input unless ($id and $action); -invalid_input unless (($id =~ m/^[0-9]+$/) and ($action =~ m/^view|relocate$/) and - ($storage =~ /^[a-z0-9]*$/)); +invalid_input unless (($id =~ m/^[0-9]+$/) + and ($action =~ m/^view|relocate$/) and ($storage =~ /^[a-z0-9]*$/)); send_cookie; for ($action) { -- cgit v1.2.3 From f97a907b9c3a52ef78ebe12223157bac9399e042 Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 6 Dec 2015 22:32:36 +0100 Subject: Have authenticate_session modify a file when an argument is provided. --- authenticate_session | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/authenticate_session b/authenticate_session index 8995080..4ff6ace 100755 --- a/authenticate_session +++ b/authenticate_session @@ -2,4 +2,17 @@ AUTH="'authenticated' => 'yes'" -sed "s/\('_SESSION_REMOTE_ADDR'\)/$AUTH, \1/" +replace_stream() +{ + sed "s/\('_SESSION_REMOTE_ADDR'\)/$AUTH, \1/" +} + +if [ "$1" ]; +then + cd /tmp + mv "$1" "$1".unauthenticated + cat "$1".unauthenticated|replace_stream > "$1" + chown www-data.www-data "$1" +else + replace_stream +fi -- cgit v1.2.3 From ae8c9242c9dc0155a6e1c5ab9e0cf6bdc726e430 Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 6 Dec 2015 22:37:48 +0100 Subject: Make some fixes to faulty unit conversion. --- mat | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mat b/mat index 7d60ec8..2a2bff7 100755 --- a/mat +++ b/mat @@ -127,7 +127,7 @@ my @schedule = ( sub convert_to_unit { my ( $out_unit, $id, $in_amount, $in_unit ) = @_; my %out_amount; - my %convert; # volume [l], weight [kg], piece [st] + my %convert; # volume [l], weight [g], piece [st] # SELECT DISTINCT unit FROM contents; # msk, krm, dl, tsk, cl, l, ml, nypa @@ -147,9 +147,9 @@ sub convert_to_unit { } elsif ($in_unit eq "krm") { $convert{'volume'} = $in_amount * 0.001; } elsif ($in_unit eq "g") { - $convert{'weight'} = $in_amount / 1000; - } elsif ($in_unit eq "kg") { $convert{'weight'} = $in_amount; + } elsif ($in_unit eq "kg") { + $convert{'weight'} = $in_amount * 1000; } elsif ($in_unit eq "st") { $convert{'piece'} = $in_amount; } @@ -165,10 +165,10 @@ sub convert_to_unit { if ( $convert{'volume'} ) { $volume = 0.001 * $convert{'volume'}; # m³ if ($ingredient->{density}) { - $out_amount{'weight'} = $density * $volume; # kg + $out_amount{'weight'} = $density * $volume * 1000; # g } } elsif ( $convert{'weight'} ) { - $weight = $convert{'weight'}; # kg + $weight = $convert{'weight'}; # g $out_amount{'weight'} = $weight; if ($ingredient->{'piece_weight'}) { $piece_weight = $ingredient->{'piece_weight'}; # g/piece @@ -176,7 +176,7 @@ sub convert_to_unit { $ingredient->{'piece_weight'}; # piece } if ($ingredient->{density}) { - $out_amount{'volume'} = 1000 * $weight / $density; # g + $out_amount{'volume'} = $weight / $density; # g } } elsif ( $convert{'piece'} ) { if ($ingredient->{'piece_weight'}) { @@ -187,7 +187,7 @@ sub convert_to_unit { if ($out_unit eq "g") { if($out_amount{'weight'}) { - return $out_amount{'weight'} * 1000; + return $out_amount{'weight'}; } else { return } -- cgit v1.2.3 From f9131183fcf95f8ffefa513df7014df61fc9147d Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 6 Dec 2015 22:39:32 +0100 Subject: Invalid servings count should still count. --- mat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mat b/mat index 2a2bff7..5407621 100755 --- a/mat +++ b/mat @@ -910,7 +910,7 @@ sub cmd_shoppinglist { my $servings_col = $db->selectcol_arrayref("SELECT servings FROM recipes WHERE id=".$recipe.";"); my $servings; - if(defined(@$servings_col[0])) { + if(defined(@$servings_col[0]) && (@$servings_col[0]) gt 0) { $servings = @$servings_col[0]; } else { $servings = 1; -- cgit v1.2.3 From 8459903fa0ab8e61a9a373ef74c2e6c3963b7c78 Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 19 Jun 2016 20:24:32 +0200 Subject: Implement login via web. --- cgi-bin/lada-20150110 | 110 ++++++++++++++++++++++++++++++++++++++++++ cgi-bin/lada.cgi | 109 +++++++++++++++++++++++++++++++++++++++++ cgi-bin/mat.cgi | 37 +++++++++++--- cgi-bin/mat.cgi-package | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ cgi-bin/relocatehook.sh | 3 ++ 5 files changed, 377 insertions(+), 7 deletions(-) create mode 100755 cgi-bin/lada-20150110 create mode 100755 cgi-bin/lada.cgi create mode 100755 cgi-bin/mat.cgi-package create mode 100755 cgi-bin/relocatehook.sh diff --git a/cgi-bin/lada-20150110 b/cgi-bin/lada-20150110 new file mode 100755 index 0000000..793671a --- /dev/null +++ b/cgi-bin/lada-20150110 @@ -0,0 +1,110 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Config::Simple; +use CGI::Simple; +use CGI::Session; +use DBI; + +tie my %Config, "Config::Simple", '/etc/mat.conf'; + +my $q = new CGI::Simple; +my $id = $q->param('id'); +my $action = $q->param('action'); +my $storage = $q->param('storage'); +my $session = CGI::Session->new(undef, undef, {Directory => + $Config{'session_directory'}}); + +sub misconfigured +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "This system is misconfigured.\n"; + exit 1; +} + +sub invalid_input() +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "Invalid input.\n"; + exit 1; +} + +sub unauthorized() +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "Not authorized!\n"; + exit 1; +} + +sub send_cookie() +{ + print $session->header(-type => 'text/html', -charset => 'utf8'); +} + +sub cmd_view($) +{ + my ( $id ) = @_; + + my $db = DBI->connect($Config{'database'}, "", "", + {HandleError => \&misconfigured, AutoCommit => 1}); + + my $recipe_row = $db->selectrow_arrayref("SELECT name, storage, uri FROM ". + "recipes AS r JOIN inventory AS i ON i.recipe_id=r.id WHERE i.id=".$id. + ";"); + + print $id, "
", $$recipe_row[0], "
", $$recipe_row[1], "
"; + if(substr($$recipe_row[2], 0, 4) eq "http") { + print ''.$$recipe_row[2].'', "
"; + } else { + print $$recipe_row[2], "
"; + } + if ($session->param('authenticated') eq "yes") { + print "\n"; + for my $store ( split(" ", $Config{'relocate_stores'}) ) { + print " ". + "$store \n"; + } + print ""; + } +} + +sub cmd_relocate($$) +{ + my ( $id, $storage ) = @_; + + my $db = DBI->connect($Config{'database'}, "", "", + {HandleError => \&misconfigured, AutoCommit => 1}); + + $db->do('UPDATE inventory SET storage="'.$storage.'" WHERE id='.$id); + + cmd_view($id); +} + +### MAIN PROGRAM ############################################################## + +misconfigured unless ($Config{'database'}); +misconfigured unless $session; +invalid_input unless ($id and $action); + +invalid_input unless (($id =~ m/^[0-9]+$/) and ($action =~ m/^view|relocate$/) and + ($storage =~ /^[a-z0-9]*$/)); +send_cookie; + +for ($action) { + if (/^view$/) { + cmd_view($id); + } + elsif (/^relocate$/) { + if ($session->param('authenticated') eq "yes") { + $session->expire("5y"); + cmd_relocate($id, $storage); + } else { + unauthorized(); + } + } + else { + invalid_input; + } +} diff --git a/cgi-bin/lada.cgi b/cgi-bin/lada.cgi new file mode 100755 index 0000000..f3b2554 --- /dev/null +++ b/cgi-bin/lada.cgi @@ -0,0 +1,109 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Config::Simple; +use CGI::Simple; +use CGI::Session; +use DBI; + +tie my %Config, "Config::Simple", '/etc/mat.conf'; + +my $q = new CGI::Simple; +my $id = $q->param('id'); +my $action = $q->param('action'); +my $storage = $q->param('storage'); +my $session = CGI::Session->new(undef, undef, {Directory => + $Config{'session_directory'}}); + +sub misconfigured +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "This system is misconfigured.\n"; + exit 1; +} + +sub invalid_input() +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "Invalid input.\n"; + exit 1; +} + +sub unauthorized() +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "Not authorized!\n"; + exit 1; +} + +sub send_cookie() +{ + print $session->header(-type => 'text/html', -charset => 'utf8'); +} + +sub cmd_view($) +{ + my ( $id ) = @_; + + my $db = DBI->connect($Config{'database'}, "", "", + {HandleError => \&misconfigured, AutoCommit => 1}); + + my $recipe_row = $db->selectrow_arrayref("SELECT name, storage, uri FROM ". + "recipes AS r JOIN inventory AS i ON i.recipe_id=r.id WHERE i.id=".$id. + ";"); + + print $id, "
", $$recipe_row[0], "
", $$recipe_row[1], "
"; + if(substr($$recipe_row[2], 0, 4) eq "http") { + print ''.$$recipe_row[2].'', "
"; + } else { + print $$recipe_row[2], "
"; + } + if ($session->param('authenticated') eq "yes") { + print ''; + print "magen "; + print "hemkyl "; + print "jobbfrys "; + print "jobbkyl"; + print ''; + } +} + +sub cmd_relocate($$) +{ + my ( $id, $storage ) = @_; + + my $db = DBI->connect($Config{'database'}, "", "", + {HandleError => \&misconfigured, AutoCommit => 1}); + + $db->do('UPDATE inventory SET storage="'.$storage.'" WHERE id='.$id); + + cmd_view($id); +} + +### MAIN PROGRAM ############################################################## + +misconfigured unless ($Config{'database'}); +misconfigured unless $session; +invalid_input unless ($id and $action); + +invalid_input unless (($id =~ m/^[0-9]+$/) and ($action =~ m/^view|relocate$/) and + ($storage =~ /^[a-z0-9]*$/)); +send_cookie; + +for ($action) { + if (/^view$/) { + cmd_view($id); + } + elsif (/^relocate$/) { + if ($session->param('authenticated') eq "yes") { + cmd_relocate($id, $storage); + } else { + unauthorized(); + } + } + else { + invalid_input; + } +} diff --git a/cgi-bin/mat.cgi b/cgi-bin/mat.cgi index 52239d5..d8845f7 100755 --- a/cgi-bin/mat.cgi +++ b/cgi-bin/mat.cgi @@ -14,6 +14,7 @@ my $q = new CGI::Simple; my $id = $q->param('id'); my $action = $q->param('action'); my $storage = $q->param('storage'); +my $password = $q->param('password'); my $session = CGI::Session->new(undef, undef, {Directory => $Config{'session_directory'}}); @@ -85,29 +86,51 @@ sub cmd_relocate($$) cmd_view($id); } +sub cmd_login($) +{ + my ( $password ) = @_; + + if ( $password and ($password eq $Config{'password'} )) { + $session->param('authenticated', "yes"); + print "welcome."; + } else { + print "
\n"; + print "\n"; + print "\n"; + print "
\n"; + } +} + ### MAIN PROGRAM ############################################################## misconfigured unless ($Config{'database'}); misconfigured unless $session; -invalid_input unless ($id and $action); +invalid_input unless $action; + +if ($action eq "view") { + invalid_input unless ($id and $id =~ m/^[0-9]+$/); +} elsif ($action eq "relocate") { + invalid_input unless (($id =~ m/^[0-9]+$/) and ($storage =~ /^[a-z0-9]*$/)); +} elsif ($action eq "login") { +} else { + invalid_input; +} -invalid_input unless (($id =~ m/^[0-9]+$/) - and ($action =~ m/^view|relocate$/) and ($storage =~ /^[a-z0-9]*$/)); send_cookie; for ($action) { if (/^view$/) { cmd_view($id); - } - elsif (/^relocate$/) { + } elsif (/^relocate$/) { if ($session->param('authenticated') eq "yes") { $session->expire("5y"); cmd_relocate($id, $storage); } else { unauthorized(); } - } - else { + } elsif (/^login$/) { + cmd_login($password); + } else { invalid_input; } } diff --git a/cgi-bin/mat.cgi-package b/cgi-bin/mat.cgi-package new file mode 100755 index 0000000..15ad893 --- /dev/null +++ b/cgi-bin/mat.cgi-package @@ -0,0 +1,125 @@ +#!/usr/bin/perl + +package cgi::mat; + +require Exporter; +@ISA = qw(Exporter); +@EXPORT = qw(misconfigured); + +use strict; +use warnings; + +use Config::Simple; +use CGI::Simple; +use CGI::Session; +use DBI; + +tie my %Config, "Config::Simple", '/etc/mat.conf'; + +my $q = new CGI::Simple; +my $id = $q->param('id'); +my $action = $q->param('action'); +my $storage = $q->param('storage'); +my $session = CGI::Session->new(undef, undef, {Directory => + $Config{'session_directory'}}); + +sub misconfigured +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "This system is misconfigured.\n"; + exit 1; +} + +sub invalid_input() +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "Invalid input.\n"; + exit 1; +} + +sub unauthorized() +{ + print "Content-Type: text/plain; charset=utf-8\n\r\n\r"; + print "Not authorized!\n"; + exit 1; +} + +sub send_cookie() +{ + print $session->header(-type => 'text/html', -charset => 'utf8'); +} + +sub cmd_view($) +{ + my ( $id ) = @_; + + my $db = DBI->connect($Config{'database'}, "", "", + {HandleError => \&misconfigured, AutoCommit => 1}); + + my $recipe_row = $db->selectrow_arrayref("SELECT name, storage, uri, ". + "amount, energy FROM recipes AS r JOIN inventory AS i ". + "ON i.recipe_id=r.id WHERE i.id=".$id.";"); + + print $id, "
", $$recipe_row[0], "
", $$recipe_row[1], "
"; + print $$recipe_row[3] ? $$recipe_row[3]." g
" : "", "
"; + print $$recipe_row[4] ? $$recipe_row[4]." kJ
" : "", "
"; + if(substr($$recipe_row[2], 0, 4) eq "http") { + print ''.$$recipe_row[2].'', "
"; + } else { + print $$recipe_row[2], "
"; + } + if ($session->param('authenticated') eq "yes") { + print "\n"; + for my $store ( split(" ", $Config{'relocate_stores'}) ) { + print " ". + "$store \n"; + } + print ""; + } +} + +sub cmd_relocate($$) +{ + my ( $id, $storage ) = @_; + + my $db = DBI->connect($Config{'database'}, "", "", + {HandleError => \&misconfigured, AutoCommit => 1}); + + $db->do('UPDATE inventory SET storage="'.$storage.'" WHERE id='.$id); + + cmd_view($id); +} + +### MAIN PROGRAM ############################################################## + +sub run +{ + misconfigured unless ($Config{'database'}); + misconfigured unless $session; + invalid_input unless ($id and $action); + + invalid_input unless (($id =~ m/^[0-9]+$/) and ($action =~ m/^view|relocate$/) and + ($storage =~ /^[a-z0-9]*$/)); + send_cookie; + + for ($action) { + if (/^view$/) { + cmd_view($id); + } + elsif (/^relocate$/) { + if ($session->param('authenticated') eq "yes") { + $session->expire("5y"); + cmd_relocate($id, $storage); + } else { + unauthorized(); + } + } + else { + invalid_input; + } + } +} + +__PACKAGE__->run( @ARGV ) unless caller(); + +__END__ diff --git a/cgi-bin/relocatehook.sh b/cgi-bin/relocatehook.sh new file mode 100755 index 0000000..19cc279 --- /dev/null +++ b/cgi-bin/relocatehook.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +date "+%s|%Y%m%d %H%M|$1|$2|$3|$4|$5|$6" >> /var/tmp/relocatelogg.txt -- cgit v1.2.3 From 255a31c21a215bd06cd1884f958130b8e63d3322 Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 17 Jul 2016 20:00:45 +0200 Subject: Add missing unit conversion for cl & ml. --- mat | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mat b/mat index 5407621..2ec9415 100755 --- a/mat +++ b/mat @@ -140,6 +140,10 @@ sub convert_to_unit { $convert{'volume'} = $in_amount; } elsif ($in_unit eq "dl") { $convert{'volume'} = $in_amount / 10; + } elsif ($in_unit eq "cl") { + $convert{'volume'} = $in_amount / 100; + } elsif ($in_unit eq "ml") { + $convert{'volume'} = $in_amount / 1000; } elsif ($in_unit eq "msk") { $convert{'volume'} = $in_amount * 0.015; } elsif ($in_unit eq "tsk") { -- cgit v1.2.3