This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
/
minivend
3154 lines (2730 loc) · 79.6 KB
/
minivend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# MiniVend version 3.12
#
# $Id: minivend,v 1.38 1999/02/12 12:24:23 mike Exp $
#
# This program is largely based on Vend 0.2
# Copyright 1995 by Andrew M. Wilcox <awilcox@world.std.com>
#
# Portions from Vend 0.3
# Copyright 1995 by Andrew M. Wilcox <awilcox@world.std.com>
#
# Enhancements made by and
# Copyright 1996-1998 by Michael J. Heins <mikeh@iac.net>
#
# See the file 'Changes' for information.
#
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
use Config;
BEGIN {
$Vend::Foreground = 1;
eval {
require 5.004;
require FindBin;
1 and $Global::VendRoot = $FindBin::RealBin;
1 and $Global::VendRoot =~ s/.bin$//i ;
};
($Global::VendRoot = $ENV{MINIVEND_ROOT})
if defined $ENV{MINIVEND_ROOT};
$Global::VendRoot = $Global::VendRoot || '/home/minivend';
$Global::ConfigFile = 'minivend.cfg';
die "MiniVend not configured, no $Global::ConfigFile.\n"
unless -f "$Global::VendRoot/$Global::ConfigFile";
$Global::ConfDir = "$Global::VendRoot/etc";
$Global::SendMailLocation = '/usr/lib/sendmail';
if($Config{osname} =~ /win32/i) {
$Global::Windows = 1;
}
# Uncomment next line if you want to guarantee use of DB_File
#$ENV{MINIVEND_DBFILE} = 1;
# Uncomment next line if you want to guarantee use of GDBM and not DB_File
#$ENV{MINIVEND_GDBM} = 1;
# Uncomment next line if you want to use no DBM, sessions
# stored in files and databases in memory (or SQL)
#$ENV{MINIVEND_NODBM} = 1;
# Uncomment next line if you DON'T want to use DBI, can
# save a bit on code size
#$ENV{MINIVEND_NO_DBI} = 1;
# Uncomment next line if you want to use the Storable
# module for storing session data. It improves session performance
# to a good degree. We will also do a bit of auto-detect below.
#$ENV{MINIVEND_STORABLE} = 1;
# Uncomment next line if you want to use the Storable
# module for storing database data. It improves GBDM/DB_File performance
# to a good degree. We will also do a bit of auto-detect below.
#$ENV{MINIVEND_STORABLE_DB} = 1;
# Uncomment AND SET next line to set PGP path to somewhere besides
# the MiniVend user path
#$ENV{PGPPATH} = '/usr/local/pgp';
# Uncomment AND SET next line to work around bad POSIX strftime
# routines if on a Linux system with bad STRUCT_HAS_TZ.
if(-f "$Global::VendRoot/linux.badposix") {
$ENV{MINIVEND_BADPOSIX} = 1;
}
# Use the Storable module for storing data in DBM files.
if (-f "$Global::VendRoot/_session_storable") {
$ENV{MINIVEND_STORABLE} = 1;
}
if (-f "$Global::VendRoot/_db_storable") {
$ENV{MINIVEND_STORABLE_DB} = 1;
}
# If you set this to true, you can create an alias that doesn't begin
# with a / and do a direct database query with vlink. For example:
#
# Catalog simple /home/minivend/simple /cgi-bin/simple simple
#
# then:
#
# % bin/vlink simple products title 00-0011
#
# or
#
# % bin/vlink simple scan sf=category/se=Impressionists/rf=0,title,artist
#
$Global::AllowQuery = 1;
$Global::ErrorFile = 'error.log';
}
### END CONFIGURABLE VARIABLES
use lib "$Global::VendRoot/lib";
use vars qw($VERSION);
%Global::DHASH = (
# DEBUG
# OFF => 0,
# NORMAL => 1,
# TAG => 2,
# TAGS => 2,
# DATA => 4,
# DATABASE => 4,
# CONFIG => 8,
# SEARCH => 16,
# SESSION => 32,
# SERVER => 64,
# CACHE => 128,
# CALLER => 512,
# VERBOSE => 0x3FF,
# COMMENT => 1024,
# FILE => 2048,
# FOREGROUND => 4096,
# END DEBUG
);
sub debug {
return ($_[0] || 0) & $Global::DEBUG;
}
BEGIN {
$VERSION = '3.12';
}
use strict;
use Fcntl;
# BSD, among others, defines sendmail to be in /usr/sbin, and
# we want to make sure the program is there. Insert the location
# of you sendmail binary (the configure script should do this)
$Global::SendMailLocation = ($Global::Windows and $Global::SendMailLocation) ||
(-x $Global::SendMailLocation and $Global::SendMailLocation) ||
(-x '/usr/lib/sendmail' and '/usr/lib/sendmail') ||
(-x '/usr/sbin/sendmail' and '/usr/sbin/sendmail') ||
$Config{sendmail};
#select a DBM
BEGIN {
chdir $Global::VendRoot
or die "Couldn't change directory to $Global::VendRoot: $!\n";
$Global::GDBM = $Global::DB_File = $Global::DBI = 0;
# This is for standard DBI
eval {
die if $ENV{MINIVEND_NODBI};
require DBI and $Global::DBI = 1
};
# This is for experimenting with Win32::ODBC
#use DBI::W32ODBC;
#$Global::DBI = 1;
# Clean HTML if module there
eval {require HTML::Clean; };
# Now can use any type of database
AUTO: {
last AUTO if
(defined $ENV{MINIVEND_DBFILE} and $Global::DB_File = 1);
last AUTO if
(defined $ENV{MINIVEND_NODBM});
eval {require GDBM_File and $Global::GDBM = 1};
last AUTO if
(defined $ENV{MINIVEND_GDBM} and $Global::GDBM = 1);
eval {require DB_File and $Global::DB_File = 1};
}
if($Global::GDBM) {
require Vend::Table::GDBM;
import GDBM_File;
$Global::GDBM = 1;
$Global::Default_database = 'GDBM'
unless defined $Global::Default_database;
}
if($Global::DB_File) {
require Vend::Table::DB_File;
import DB_File;
$Global::DB_File = 1;
$Global::Default_database = 'DB_FILE'
unless defined $Global::Default_database;
}
$Global::Default_database = 'MEMORY'
unless defined $Global::Default_database;
require Vend::Table::InMemory;
}
use Vend::Server;
use Vend::Session;
use Vend::Config;
use Vend::Imagemap;
# GLIMPSE
use Vend::Glimpse;
# END GLIMPSE
use Vend::Scan;
use Vend::TextSearch;
use Vend::Order;
use Vend::Data;
use Vend::Util;
use Vend::UserDB;
use Vend::Interpolate;
use Vend::ValidCC;
use Vend::Cart;
use File::CounterFile;
use File::Path;
# OPTION_EXTENSION
#use Getopt::Long;
# END OPTION_EXTENSION
# STATICPAGE
use Vend::PageBuild;
# END STATICPAGE
my $H;
my $wantref = 1;
sub http {
$H;
}
$Global::ConfigFile = "$Global::VendRoot/$Global::ConfigFile"
if ($Global::ConfigFile !~ m.^/.);
$Global::ErrorFile = "$Global::VendRoot/$Global::ErrorFile"
if ($Global::ErrorFile !~ m.^/.);
sub check_html {
my($out) = @_;
unless($Global::CheckHTML) {
# logError("Can't check HTML: No global CheckHTML defined. Contact admin.");
logError( errmsg('bin/minivend:1', "Can't check HTML: No global CheckHTML defined. Contact admin." ) );
}
my $file = POSIX::tmpnam();
open(CHECK, "|$Global::CheckHTML > $file 2>&1") or die "Couldn't fork: $!\n";
print CHECK $$out;
close CHECK;
my $begin = "<!-- HTML Check via '$Global::CheckHTML'\n";
my $end = "\n-->";
my $check = readfile($file);
unlink $file or die "Couldn't unlink temp file $file: $!\n";
$$out .= $begin . $check . $end;
return;
}
sub response {
my ($type,$output,$debug) = @_;
#$Vend::content_type = $type;
return 1 if $Vend::BuildingPages;
my $out = ref $output ? $output : \$output;
check_html($out) if defined $Vend::CheckHTML;
if(defined $Vend::CleanHTML) {
my $html = new HTML::Clean($out);
$html->compat();
$html->strip();
$out = $html->data();
}
if (defined $Vend::DebugHTML) {
$$out .= "\n<!-- DEBUG\n" .
$Vend::DebugHTML .
"\nEND DEBUG -->\n";
undef $Vend::DebugHTML;
}
my $content = $Vend::ContentType || "text/$type";
http()->respond($content,$out,$debug);
}
## INTERFACE ERROR
# An incorrect response was returned from the browser, either because of a
# browser bug or bad html pages.
sub interaction_error {
my($msg) = @_;
my($page);
# logError ("Difficulty interacting with browser: $msg");
logError( errmsg('bin/minivend:2', "Difficulty interacting with browser: %s" , $msg) );
$page = readin(find_special_page('interact'));
if (defined $page) {
$page =~ s#\[message\]#$msg#ig;
response('html',interpolate_html($page, $wantref));
}
else {
# logError("Missing special page: interact");
logError( errmsg('bin/minivend:3', "Missing special page: interact" ) );
response('plain',"$msg\n");
}
}
## EXPECT FORM
# Check that a form is being submitted.
sub minidump() {
my $s = $Vend::Session;
my $v = $Vend::Session->{'values'};
my $host = http()->Client_Hostname;
my $ip = http()->Client_IP_Address;
my $query = http()->Query;
my $out = <<EOF;
Full client host name: $host
Full client IP address: $ip
Query string was: $query
EOF
for(qw/browser last_url prev_url/) {
$out .= "$_: $s->{$_}\n";
}
for(keys %{$s->{carts}}) {
$out .= scalar @{$s->{carts}->{$_}};
$out .= " items in $_ cart.\n";
}
return $out;
}
sub expect_form {
return 1 if $Global::TolerateGet;
if ( uc($CGI::request_method) ne 'POST') {
interaction_error("Request method '$CGI::request_method' for form submission is not POST\n");
logError(minidump());
return 0;
}
if ( lc($CGI::content_type) ne 'application/x-www-form-urlencoded') {
interaction_error("Content type '$CGI::content_type' for form submission" .
" is not application/x-www-form-urlencoded");
logError(minidump());
return 0;
}
return 1;
}
## ACTIONS SPECIFIED BY THE INVOKING URL
## DO CATALOG
# Display the initial catalog page.
sub do_catalog {
do_page(find_special_page('catalog'));
}
## DO PAGE
sub display_special_page {
my($name, $subject) = @_;
my($page);
$subject = $subject || 'unspecified error';
$page = readin($name);
die "Missing special page: $name\n" unless defined $page;
$page =~ s#\[subject\]#$subject#ig;
return response('html',interpolate_html($page, $wantref));
}
# Displays the catalog page NAME. If the file is not found, displays
# the special page 'missing'.
#
sub display_page {
my($name, $arg) = @_;
my($page);
if($Vend::Cfg->{'ExtraSecure'} and
$Vend::Cfg->{AlwaysSecure}->{$name}
and !$CGI::secure) {
$name = find_special_page('violation');
}
$page = readin($name);
# Try for on-the-fly if not there
if(! defined $page) {
$page = fly_page($name,$arg);
}
if (defined $page) {
response('html',cache_html($page, $wantref));
return 1;
} else {
logData($Vend::Cfg->{'LogFile'}, format_log_msg('page not found'))
if defined $Vend::Cfg->{CollectData}->{nopage};
display_special_page(find_special_page('missing'), $name);
return 0;
}
}
sub cache_page {
my($name, $arg) = @_;
my($page);
my $pagedir = $Vend::Cfg->{ScratchDir} . '/PageCache';
my $pagename = generate_key($name,$arg);
if($CGI::pragma !~ /no-cache/i
and $page = readfile("$pagedir/$pagename.html")) {
# DEBUG
#Vend::Util::logDebug ("Hit cache $pagename\n") if ::debug(0x80);
# END DEBUG
response('html', \$page);
return 1;
}
$page = readin($name);
# Try for on-the-fly if not there
if(! defined $page) {
$page = fly_page($name, $arg);
}
$Vend::PageCacheCopy = '';
if (defined $page) {
my $complete;
($page, $complete) = cache_html($page, $wantref);
if(defined $Vend::CachePage) {
my $out = $complete || $page;
logData($Vend::Cfg->{'LogFile'}, format_log_msg('add to page cache'))
if defined $Vend::Cfg->{CollectData}->{cache};
open PAGECACHE, ">$pagedir/$pagename.html"
or do {
# logGlobal("Page cache failure: $!");
logGlobal( errmsg('bin/minivend:4', "Page cache failure: %s", $! ) );
die "Page cache failure: $!\n";
};
print PAGECACHE $$out;
close PAGECACHE;
}
put_session();
response('html',$page);
return 1;
}
else {
logData($Vend::Cfg->{'LogFile'}, format_log_msg('page not found'))
if defined $Vend::Cfg->{CollectData}->{nopage};
return display_special_page(find_special_page('missing'), $name);
}
}
# Display the catalog page NAME.
sub do_page {
my($name, $arg) = @_;
logData($Vend::Cfg->{LogFile}, format_log_msg("$name " . $arg || undef))
if defined $Vend::Cfg->{CollectData}->{page};
if($Vend::Cfg->{PageCache} and
$Vend::Cookie and
! defined $Vend::Cfg->{NoCache}->{$name})
{
cache_page($name, $arg || undef) and $Vend::Session->{'page'} = $name;
}
else {
display_page($name, $arg || undef) and $Vend::Session->{'page'} = $name;
put_session();
}
}
## DO ORDER
# Order an item with product code CODE.
sub do_order
{
my($code,$path,$catalog,$quantity) = @_;
my($i, $found, $item, $save, %att);
# DEBUG
#Vend::Util::logDebug("do_order: '" . (join "','", @_) . "'\n") if ::debug(0x1);
# END DEBUG
my($cartname,$page) = split m:/:, $path || '', 2;
my $cart = get_cart $cartname;
my $base = product_code_exists_tag($code, $catalog || undef);
$quantity = (defined $quantity and $quantity =~ /^[-+_](\d+)$/)
? ($quantity =~ s/^[_+]//, $quantity)
: 1;
if (! $base ) {
# logError("Attempt to order missing product code: $code");
logError( errmsg('bin/minivend:5', "Attempt to order missing product code: %s" , $code) );
display_special_page(find_special_page('noproduct'), $code);
return;
}
if(defined $Vend::Cfg->{CollectData}->{basket}) {
$cartname = $cartname || 'main';
my $string = "code=$code";
$string .= " cart=" . ($cartname || '');
$string .= " page=" . ($page || '');
$string .= " base=$base";
logData $Vend::Cfg->{LogFile}, format_log_msg($string);
}
INC: {
# Check that the item has not been already ordered.
$found = -1;
# Check to see if we should push an already-ordered item instead of
# ignoring it
my $separate =
$Vend::Cfg->{SeparateItems} ||
$CGI::values{mv_separate_items} ||
(
defined $Vend::Session->{scratch}->{mv_separate_items}
&& is_yes( $Vend::Session->{scratch}->{mv_separate_items} )
);
last INC if $separate;
foreach $i (0 .. $#$cart) {
if ($cart->[$i]->{'code'} eq $code) {
next unless $cart->[$i]->{mv_ib} eq $base;
$found = $i;
$cart->[$i]->{'quantity'} += $quantity
if $quantity =~ /^[-+]/;
}
}
} # INC
# And if not found or separate, start with a single quantity.
if ($found == -1 and $quantity > 0) {
$quantity =~ s/^[-+]//;
$item = {'code' => $code, 'quantity' => $quantity, mv_ib => $base};
if($Vend::Cfg->{UseModifier}) {
foreach $i (@{$Vend::Cfg->{UseModifier}}) {
$item->{$i} = delete($Vend::Session->{scratch}{"mv_${i}_$code"}) || '';
}
}
push @$cart, $item;
}
my $limit;
if($limit = $Vend::Cfg->{OrderLineLimit} and $#$cart >= $limit ) {
@$cart = ();
my $msg = <<EOF;
WARNING:
Possible bad robot. Cart limit of $limit exceeded. Cart emptied.
EOF
my $cmd;
if($cmd = $Global::LockoutCommand) {
my $host = http()->Client_IP_Address;
$cmd =~ s/%s/$host/ or $cmd .= " $host";
$msg .= "Performing lockout command '$cmd'.";
system $cmd;
$msg .= "Bad status $? from '$cmd': $!\n"
if $?;
}
logError $msg;
logGlobal $msg;
}
toss_cart($cart);
order_page($page); # display the order page
put_session();
}
## DO SEARCH
sub do_search {
my $more = shift;
my($c) = \%CGI::values;
#::logGlobal($more);
if ($more) {
$c = { mv_more_matches => $more };
$Vend::Session->{last_search} = "scan/MM=$more";
$more =~ s/[^a-zA-Z0-9].*//;
$c->{mv_cache_key} = $more;
}
else {
create_last_search($c);
}
if($Vend::Cfg->{SearchCache} and $Vend::Cookie) {
my($key,$page) = check_search_cache($Vend::Session->{last_search});
if (defined $page and $CGI::pragma !~ /no-cache/i) {
put_session if $Vend::Session->{scratch}->{mv_put_session};
return response('html',$page);
}
$c->{mv_cache_key} = $key unless defined $c->{mv_cache_key}
}
else {
$c->{mv_cache_key} = generate_key($Vend::Session->{last_search})
unless defined $c->{mv_cache_key};
}
my $status = perform_search($c);
put_session if $Vend::Session->{scratch}->{mv_put_session};
return $status;
}
sub do_scan {
my($argument,$path) = @_;
my ($key,$page);
my $c = {};
find_search_params($c,$path);
if($key = $c->{mv_more_matches}) {
$Vend::Session->{last_search} = "scan/MM=$key";
$key =~ s/[^a-zA-Z0-9].*//;
$c->{mv_cache_key} = $key;
undef $key;
}
else {
$key = generate_key(create_last_search($c));
$c->{mv_cache_key} = $key;
}
if($Vend::Cfg->{SearchCache} and $Vend::Cookie) {
($key,$page) = check_search_cache($Vend::Session->{last_search},$key);
if (defined $page and $CGI::pragma !~ /no-cache/i) {
put_session if $Vend::Session->{scratch}->{mv_put_session};
return response('html',$page);
}
}
my $status = perform_search($c,$argument);
put_session if $Vend::Session->{scratch}->{mv_put_session};
return $status;
}
sub fake_scan {
my($argument,$path) = @_;
my ($key,$page);
my $c = {};
find_search_params($c,$path);
return perform_search($c,$argument);
}
# Returns undef if interaction error
sub update_quantity {
my($h, $i, $quantity, $modifier, $cart);
return 1 unless defined $CGI::values{"quantity0"};
$cart = get_cart($CGI::values{mv_cartname});
if(ref $Vend::Cfg->{UseModifier}) {
foreach $h (@{$Vend::Cfg->{UseModifier}}) {
foreach $i (0 .. $#$cart) {
$modifier = $CGI::values{"$h$i"} || undef;
if (defined($modifier)) {
$modifier =~ s/\0+/\0/g;
$modifier =~ s/\0$//;
$modifier =~ s/^\0//;
$modifier =~ s/\0/, /g;
$cart->[$i]->{$h} = $modifier;
$Vend::Session->{'values'}->{"$h$i"} = $modifier;
#delete $Vend::Session->{'values'}->{"$h$i"};
}
}
}
}
foreach $i (0 .. $#$cart) {
$quantity = $CGI::values{"quantity$i"};
if (! defined $quantity) {
interaction_error("Variable '$quantity' not passed from form\n");
return undef;
}
elsif ($quantity =~ m/^\d*$/) {
$cart->[$i]->{'quantity'} = $quantity || 0;
}
elsif ($quantity =~ m/^[\d.]+$/
and $Vend::Cfg->{FractionalItems} ) {
$cart->[$i]->{'quantity'} = $quantity;
}
# This allows a multiple input of item quantity to
# pass -- FIRST ONE CONTROLS
elsif ($quantity =~ s/\0.*//) {
$CGI::values{"quantity$i"} = $quantity;
redo;
}
else {
my $item = $cart->[$i]->{'code'};
interaction_error("'$quantity' for item $item is not numeric\n");
return undef;
}
}
# If the user has put in "0" for any quantity, delete that item
# from the order list.
toss_cart($cart);
1;
}
sub add_items {
my($items,$quantities,$bases) = @_;
my(@items);
my($code,$found,$item,$base,$quantity,$i,$j,$q);
my(@quantities);
my(@bases);
my($attr,%attr);
@items = split /\0/, $items, -1;
my $cartname = $CGI::values{mv_cartname};
my $cart = get_cart($cartname);
if($quantities) {
@quantities = split /\0/, $quantities, -1;
}
$bases = $bases || $CGI::values{mv_order_mv_ib} || '';
if($bases) {
@bases = split /\0/, $bases, $1;
}
if ($Vend::Cfg->{UseModifier}) {
foreach $attr (@{$Vend::Cfg->{UseModifier} || []}) {
$attr{$attr} = [];
next unless defined $CGI::values{"mv_order_$attr"};
@{$attr{$attr}} = split /\0/, $CGI::values{"mv_order_$attr"}, -1;
}
}
my ($group, $found_master, $mv_mi, $mv_si, @group);
@group = split /\0/, (delete $CGI::values{mv_order_group} || ''), -1;
for( $i = 0; $i < @group; $i++ ) {
$attr{mv_mi}->[$i] = $group[$i] ? ++$Vend::Session->{pageCount} : 0;
}
my $separate =
$Vend::Cfg->{SeparateItems} ||
$CGI::values{mv_separate_items} ||
(
defined $Vend::Session->{scratch}->{mv_separate_items}
&& is_yes( $Vend::Session->{scratch}->{mv_separate_items} )
);
$j = 0;
foreach $code (@items) {
$quantity = defined $quantities[$j] ? $quantities[$j] : 1;
($j++,next) unless $quantity;
$base = product_code_exists_tag($code, $bases[$j] || undef);
if (! $base ) {
# logError("Attempt to order missing product code: $code");
logError( errmsg('bin/minivend:6', "Attempt to order missing product code: %s" , $code) );
return;
}
if(defined $Vend::Cfg->{CollectData}->{basket}) {
$cartname = $cartname || 'main';
logData $Vend::Cfg->{LogFile},
format_log_msg(
"FORM ORDER code=$code qty=$quantity cart=$cartname base=$base");
}
INCREMENT: {
# Check that the item has not been already ordered.
# But let us order separates if so configured
$found = -1;
last INCREMENT if $separate;
foreach $i (0 .. $#$cart) {
if ($cart->[$i]->{'code'} eq $code) {
next unless $base eq $cart->[$i]->{mv_ib};
$found = $i;
# Increment quantity. This is different than
# the standard handling because we are ordering
# accessories, and may want more than 1 of each
$cart->[$i]->{'quantity'} += $quantity;
}
}
} # INCREMENT
# An if not, start of with a single quantity.
if ($found == -1) {
$item = {'code' => $code, 'quantity' => $quantity, mv_ib => $base};
# Add the master item/sub item ids if appropriate
if(@group) {
if($attr{mv_mi}->[$j]) {
$item->{mv_mi} = $mv_mi = $attr{mv_mi}->[$j];
$item->{mv_si} = $mv_si = 0;
}
else {
$item->{mv_mi} = $mv_mi;
$item->{mv_si} = ++$mv_si;
}
}
if($Vend::Cfg->{UseModifier}) {
foreach $i (@{$Vend::Cfg->{UseModifier}}) {
$item->{$i} = $attr{$i}->[$j];
}
}
my $next = $#$cart + 1;
push @$cart, $item;
$CGI::values{"quantity$next"} = $quantity;
}
$j++;
}
}
## DO FINISH
# Finish an incomplete order.
sub do_finish {
my($page) = shift || $Vend::Cfg->{'CheckoutPage'};
$page =~ s/^finish_?_?//;
order_page($page);
put_session();
}
# Update the user-entered fields.
sub update_data {
my($key,$value);
# Update a database record
unless (defined $CGI::values{'mv_data_table'} and
defined $CGI::values{'mv_data_key'} ) {
logError( errmsg(
'bin/minivend:7', "Attempted database operation without table, fields, or key.\n" .
"Table: '%s'\n" .
"Fields:'%s'\n" .
"Key: '%s'\n",
$CGI::values{mv_data_table},
$CGI::values{mv_data_fields},
$CGI::values{mv_data_key}
)
);
return undef;
}
my $function = lc (delete $CGI::values{'mv_data_function'});
if($function eq 'delete' and ! delete $CGI::values{'mv_data_verify'}) {
logError("DELETE without VERIFY, abort");
return undef;
}
my $table = $CGI::values{'mv_data_table'};
my $prikey = $CGI::values{'mv_data_key'};
my $decode = is_yes($CGI::values{'mv_data_decode'});
my ($ref, $type, $db, $database);
$ref = $Vend::Cfg->{Database}->{$table} || '';
if (! $ref) {
logError("set: non-existent table $table");
return undef;
}
$Vend::WriteDatabase{$table} = 1;
if($ref->{'type'} eq '8') {
$type = 'sql';
$database = $table;
}
else {
$type = 'dbm';
}
$db = database_exists_ref($table)
or die "Not a defined database '$table': $!\n";
$db = $db->ref();
my @fields = grep $_, split /[\s\0,]+/, $CGI::values{'mv_data_fields'};
$function = 'update' unless $function;
my (%data);
for(@fields, $prikey) {
$data{$_} = [];
}
while (($key, $value) = each %CGI::values) {
next unless defined $data{$key};
@{$data{$key}} = split /\0/, $value;
}
unless ($data{$prikey}) {
logError("No key '%s' found in database='%s' table='%s' key='%s'",
$prikey, $function,
$CGI::values{mv_data_table},
$CGI::values{mv_data_key} );
return undef;
}
my ($query,$i);
my (@k);
my (@v);
my (@c);
my $select_key;
for($i = 0; $i < @{$data{$prikey}}; $i++) {
@k = (); @v = ();
for(keys %data) {
next unless (($value = $data{$_}->[$i]) || $CGI::values{mv_update_empty});
push(@k, $_);
HTML::Entities::decode($value) if $decode;
$select_key = $value if $_ eq $prikey;
push(@v, $value);
}
if($function eq 'delete') {
$db->delete_record($select_key);
}
else {
my $field;
$key = $data{$prikey}->[$i];
while($field = shift @k) {
$value = shift @v;
next if $field eq $prikey;
Vend::Data::set_field($db, $key, $field, $value);
}
}
}
}
# Parse the mv_click and mv_check special variables
sub parse_click {
my ($ref, $click, $extra) = @_;
my($codere) = '[\w-_#/.]+';
my $params;
if($params = $Vend::Session->{'scratch'}->{$click}) {
# Do nothing, we found the click
}
elsif($params = $Vend::Session->{'scratch'}->{"mv_click $click"}) {
$Vend::Session->{'scratch'}->{mv_click_arg} = $click;
}
elsif($params = $Vend::Session->{'scratch'}->{mv_click}) {
$Vend::Session->{'scratch'}->{mv_click_arg} = $click;
}
else { return 1; } # No click processor
my($var,$val,$parameter);
$params = interpolate_html($params);
my(@param) = split /\n+/, $params;
for(@param) {
next unless /\S/;
next if /^\s*#/;
s/^[\r\s]+//;
s/[\r\s]+$//;
$parameter = $_;
($var,$val) = split /[\s=]+/, $parameter, 2;
$val =~ s/&#(\d+);/chr($1)/ge;
$ref->{$var} = $val;
$extra->{$var} = $val
if defined $extra;
}
}