From dani@enpl.es Tue Feb 6 14:46:48 2001 Date: Tue, 6 Feb 2001 09:46:48 -0500 (EST) From: dani@enpl.es dani@enpl.es Subject: [Barcode] Web Frontend Hi All. I've written a little CGI that acts as a frontend to the barcode utility. You can specify the destination printer, the numer of pages, encoding, margins etc.. It might be useful to some dumb user in your netowrk in order to easily print barcodes. =20 Notice it might be full of security holes,=20 It also has a missing feature: It doesn't print EAN-128 O:-) =20 ------------------------------------------------------------- #!/usr/bin/perl -w use strict; use CGI; my $query; my @codificacions; my @impresores; my $codificacio; my $codi; my $impresora; my $horitz; my $pagina; my $copies; my $vert; my $marge_sup; my $marge_inf; my $sh; @codificacions=3D("EAN-13","EAN-8","UPC","ISBN","CODE39","CODE128","i25","C= ODABAR","MSI","PLESSEY"); @impresores=3D("hpmanip","haley","hpexped","apple","lexmark","laserjet5"); $query=3Dnew CGI; if($query->param()) { # Recollida de parametres $codificacio=3D$query->param("codificacio"); $codi=3D$query->param("codi"); $impresora=3D$query->param("impresora"); $horitz=3D$query->param("horitz"); $vert=3D$query->param("vert"); $pagina=3D$query->param("pagina"); $copies=3D$query->param("copies"); $marge_sup=3D$query->param("marge_sup"); $marge_inf=3D$query->param("marge_inf"); # Capcalera HTML print $query->header(); print $query->start_html("Generaci=F3 de codis de barra"); print ''; print ''; =20 if ($codi eq "") { print "
Error: No has posat res en el el camp codi";exit; } if (!($horitz=3D~/(\d+)/) or(!($vert=3D~/(\d+)/))) { print "
Error: No has entrat b=E9 la distribuci=F3 d'etiquetes";exit; } if (!($pagina=3D~/(\d+)x(\d+)/)) { print "
Error: No has entrat b=E9 el tipo de pagina"; exit; } =20 if (!($copies=3D~/(\d+)/)) { print "
Error: No has entrat be el Num. de copies"; exit; } if((!($marge_sup=3D~/\d+/)) or (!($marge_inf=3D~/\d+/))) { print "
Error: No has entrat b=E9 els marges";exit(); } $sh=3D"/usr/local/bin/barcode -u mm -e " . lc($codificacio); $sh.=3D " -t " . $horitz . "x" . $vert . "+0+0-0-" . $marge_inf; $sh.=3D " -g " . $pagina . "+2+" . $marge_sup; # Marge esquere de 2 mm.= =20 =20 =20 $sh .=3D (" -b " . $codi) x ($horitz * $vert); # printf("%s",$sh); =20 open(PIPE,"$sh 2>&1 1>/tmp/ean |"); while() { # Si hi ha algo a STDERR, ho ensenyem i sortim printf("
%s",$_); exit();=09 } close(PIPE); system("/usr/bin/lp -d $impresora -n $copies /tmp/ean >/dev/null"); print "Fet"; print $query->end_html; } else { =09# No hi ha parametres, fem la pagina print $query->header(); print $query->start_html("Generaci=F3 de codis de barra"); print ''; print ''; print $query->start_form; $query->delete_all(); print ''; print ''; print ''; print '
'; print '

    Generaci=F3 de codis de barra '; print '
'; print $query->hr; print 'Codificaci=F3: ' . $query->popup_menu("codificacio",\@codificacions= ) . " Codi: " . $query->textfield("codi","8410435",20); print '
Impresora: ' . $query->popup_menu("impresora",\@impresores); print $query->hr; print 'Etiquetes
Horitz: ' . $query->textfield("horitz","3",5) .= "Vert: " . $query->textfield("vert","5",5); print '

Copies
Treure ' . $query->textfield("copies","1",= 5) . ' Copies'; =20 print '

P=E0gina
Dimensions(mm): ' . $query->textfield("p= agina","210x297",10); print '
Marge Superior(mm): ' . $query->textfield("marge_sup","20",10); print '
Marge Inferior(mm): ' . $query->textfield("marge_inf","20",10); print '

' . $query->submit(-name=3D>'Imprmir'); print $query->end_form; print $query->end_html; } From rubini@linux.it Tue Feb 6 14:38:03 2001 Date: Tue, 6 Feb 2001 15:38:03 +0100 From: Alessandro Rubini rubini@linux.it Subject: [Barcode] Web Frontend Thanks Dani. It's interesting. I'd like to include it in contrib/ (to be created) but I see a few problems. - there's no copyright note and no license at all - it's in spanish - there's no templace of html form to drive it. - not a line of documentation (where to put it, etc). If you fix these , I'll include it (as for spanish, I wouldn't like you to remove spanish, just add english as default choice). As for the html form, I may be wrong (I am not a web expert at all), but this demonstrates how the lack of documentation is an issue. > It also has a missing feature: It doesn't print EAN-128 O:-) Yes, that's a real problem of mine. I've been very busy and very unlucky, lately. /alessandro From dani@enpl.es Tue Feb 6 23:44:54 2001 Date: Tue, 6 Feb 2001 18:44:54 -0500 (EST) From: dani@enpl.es dani@enpl.es Subject: [Barcode] Web Frontend On Tue, 6 Feb 2001, Alessandro Rubini wrote: > > Thanks Dani. It's interesting. I'd like to include it in contrib/ (to be > created) but I see a few problems. > > - there's no copyright note and no license at all Well, I sent it to the list just like a funny "curiosity" that's been useful to me and would be useful to others. GPL is the place where the script should live I guess. > - there's no templace of html form to drive it. Sorry, I don't understand that. I'm pretty lamer in HTML afairs, and all CGI's I've written look like this. What do you mean with the html form? Do you mean the Web page doen't contain instructioins for the user? > - not a line of documentation (where to put it, etc). > > If you fix these , I'll include it (as for spanish, I wouldn't like > you to remove spanish, just add english as default choice). Yep it's easy. I'll modify the thing, make it (a bit) more secure, add comments, and translate it to English. It's the least I can do. > As for the > html form, I may be wrong (I am not a web expert at all), but this > demonstrates how the lack of documentation is an issue. Yes.. Tell me more about the html form problem. Meanwhile I'll build a "deliverable" version :) See you, --- Dani Pardo, dani@enpl.es Enplater S.A From rubini@linux.it Tue Feb 6 18:12:44 2001 Date: Tue, 6 Feb 2001 19:12:44 +0100 From: Alessandro Rubini rubini@linux.it Subject: [Barcode] Web Frontend > Yes.. Tell me more about the html form problem. Meanwhile I'll build > a "deliverable" version :) To my (little, very little) knowledge, a CGI gets parameters by either a PUT or a GET query. But the parameters must be defined somehow. So, either you put it in the query by hand or need a form that build the request for you. A "GET" URL looks like this: http://www.linux-mag.com/cgi-bin/atomz?search=rubini or like this: http://store.advantech.com.tw/italy/product.asp?pfid=198&mscssid=4HJEB2J99SU9V9NIE5YXQXJRDJE23U8G But you usually build the query by clicking in an html form. So is there a form or should the user type the hairy query by hand? (BTW: I *love* avoiding forms. That's how my "translate" script works, for example: it builds a query for babelfish and prints results on stdout; but I am not a typical user). Note: don't ask for the translator script, is not yet finished, but that's how I validate my html pages: url=`echo $1 | sed 's/:/%3A/g; s,/,%2F,g'` LINK="sylly-link-for-lynx-which-is-a-crappy-tool-not-using-stdin.html" ln -s /dev/stdin ./$LINK echo "chekcing $1 ($url)" > /dev/stderr echo -e "GET http://validator.w3.org/check?uri=$url HTTP/1.0\n" | \ nc validator.w3.org 80 | lynx -dump $LINK rm $LINK /alessandro From dani@enpl.es Wed Feb 7 14:38:01 2001 Date: Wed, 7 Feb 2001 09:38:01 -0500 (EST) From: dani@enpl.es dani@enpl.es Subject: [Barcode] Web Frontend On Tue, 6 Feb 2001, Alessandro Rubini wrote: > > > Yes.. Tell me more about the html form problem. Meanwhile I'll build > > a "deliverable" version :) > > To my (little, very little) knowledge, a CGI gets parameters by either > a PUT or a GET query. But the parameters must be defined somehow. So, > either you put it in the query by hand or need a form that build the > request for you. > > A "GET" URL looks like this: > http://www.linux-mag.com/cgi-bin/atomz?search=rubini > or like this: > http://store.advantech.com.tw/italy/product.asp?pfid=198&mscssid=4HJEB2J99SU9V9NIE5YXQXJRDJE23U8G > > But you usually build the query by clicking in an html form. So is > there a form or should the user type the hairy query by hand? You can do both ways. If you use a browser, the user will always see the same URL (params=values in the url are not shown to the user). But you can still do a GET URL using param=value;param2=value2 way. I guess the average user will use the HTML form. And "other" users can see the HTML soruce, looking for the . -- Dani Pardo, dani@enpl.es Enplater S.A From rubini@linux.it Wed Feb 7 09:21:16 2001 Date: Wed, 7 Feb 2001 10:21:16 +0100 From: Alessandro Rubini rubini@linux.it Subject: [Barcode] Web Frontend > You can do both ways. If you use a browser, the user will always see > the same URL (params=values in the url are not shown to the user). But > you can still do a GET URL using param=value;param2=value2 way. That's fine. That's what I've been stating. So we need a sample html form, don't we? /alessandro From dani@enpl.es Wed Feb 7 16:40:24 2001 Date: Wed, 7 Feb 2001 11:40:24 -0500 (EST) From: dani@enpl.es dani@enpl.es Subject: [Barcode] Web Frontend On Wed, 7 Feb 2001, Alessandro Rubini wrote: > > > You can do both ways. If you use a browser, the user will always see > > the same URL (params=values in the url are not shown to the user). But > > you can still do a GET URL using param=value;param2=value2 way. > > That's fine. That's what I've been stating. So we need a sample html > form, don't we? Oops, now I see the problem :) I've tried: bash$ lynx http://montgri/cgi-bin/cb-eng.pl?encoding=CODE128;code="344";printer= haley;horitz=3;vert=5;page=210x217;copies=1;upper_margin=0;lower_margin=0 And the CGI says: Error: nothing to encode. Meaning $code is null. I guess don't know how to write CGIs that aren't form-based. I send you the English version, more documented and (a bit) more secure. When I discover how to write form-less based cgi's, I'll send the modifications. <---------------------- CUT ---------------------------> #!/usr/bin/perl -w # # barcode.pl is a Web frontend to the Alessandro Rubini barcode's utility. # You can modify and distribute it under the terms of the GNU General # public license. # You should change # 1- $executable, @printers and line 104 according to your system. # 2- Copy barcode.pl where your cgi-bin/ dir. resides. use strict; use CGI; my $query; # The from class my @encodings; # Different encodings Barcode utility can handle my @printers; # Array of printers where the user can print my $encoding; # Desired encoding my $code; # What to encode my $printer; # Desired printer my $horitz; # Number of labels in the horitzontal my $page; # my $copies; # Number of copies my $vert; # Number of lables in the vert. my $upper_margin; my $lower_margin; my $sh; my $executable="/usr/local/bin/barcode"; # Change this line according # to the path where you have barcode @encodings=("EAN-13","EAN-8","UPC","ISBN","CODE39","CODE128","i25","CODABAR","MSI","PLESSEY"); # Change @printers according to the printers you have # or you want your users to print at. @printers=("hpmanip","haley","hpexped","apple","lexmark","laserjet5"); $query=new CGI; if($query->param()) # If we come from the submit button { # We take parameters $encoding=$query->param("encoding"); $code=$query->param("code"); $printer=$query->param("printer"); $horitz=$query->param("horitz"); $vert=$query->param("vert"); $page=$query->param("page"); $copies=$query->param("copies"); $upper_margin=$query->param("upper_margin"); $lower_margin=$query->param("lower_margin"); # HTML header print $query->header(); print $query->start_html("Barcode generator"); print ''; print ''; # Check for valid input. if ($code eq "") { print "
Error: Nothing to encode";exit; } if (!($horitz=~/(\d+)/) or(!($vert=~/(\d+)/))) { print "
Error: Incorrect label distribution";exit; } if (!($page=~/(\d+)x(\d+)/)) { print "
Error: Incorrect page type"; exit; } if (!($copies=~/(\d+)/)) { print "
Error: Incorrect number of copies"; exit; } if((!($upper_margin=~/\d+/)) or (!($lower_margin=~/\d+/))) { print "
Error: Incorrect margins";exit(); } # We build the shell command to execute $sh="$executable -u mm -e " . lc($encoding); $sh.= " -t " . $horitz . "x" . $vert . "+0+0-0-" . $lower_margin; $sh.= " -g " . $page . "+2+" . $upper_margin; # 2 mm for left margin # $code may contain any character, so # we quote it. $sh .= (" -b '" . $code . "'") x ($horitz * $vert); # printf("%s",$sh); open(PIPE,"$sh 2>&1 1>/tmp/ean |"); while() # We are reading stderr { # print barcode utility error message printf("
%s",$_); # and exit exit(); } close(PIPE); # Change next line according to your printing system. system("/usr/bin/lp -d $printer -n $copies /tmp/ean >/dev/null && rm /tmp/ean"); print "Done"; print $query->end_html; } else #There are no parameters, let's build the html form. { print $query->header; print $query->start_html("Barcode generator"); print ''; print ''; print $query->start_form; # $query->delete_all(); print ''; print ''; print ''; print '
'; print '

    Barcode generator '; print '
'; print $query->hr; print 'Encoding: ' . $query->popup_menu("encoding",\@encodings) . " Code: " . $query->textfield("code","8410435",20); print '
Printer: ' . $query->popup_menu("printer",\@printers); print $query->hr; print 'Stickers
Horitz: ' . $query->textfield("horitz","3",5) . "Vert: " . $query->textfield("vert","5",5); print '

Copies
Print ' . $query->textfield("copies","1",5) . ' Copies'; print '

Page
Dimensions(mm): ' . $query->textfield("page","210x297",10); print '
Upper margin(mm): ' . $query->textfield("upper_margin","20",10); print '
Lower margin(mm): ' . $query->textfield("lower_margin","20",10); print "

" . $query->submit(-name=>'Print!'); print $query->end_form; print $query->end_html; } From rubini@linux.it Wed Feb 7 10:49:36 2001 Date: Wed, 7 Feb 2001 11:49:36 +0100 From: Alessandro Rubini rubini@linux.it Subject: [Barcode] Web Frontend > Oops, now I see the problem :) > I guess don't know how to write CGIs that aren't form-based. Oops, now I see the problem. The form is printed by your perl code. I didn't notice it. Sorry. /alessandro