Gossamer Forum
Home : Products : DBMan : Customization :

ORDERnary

Quote Reply
ORDERnary
I have spent upwards of 12 hours trying to get this &^%$@#$@%$ Mod ( http://tangokites.org/dbman/ )to work, but it ain't happening. Anyone who can help me gets a big thank you and $25 worth of paperbacks of your choice. Please help before I jump. Luckily, there's over 2 feet of snow to soften the blow.

Quote Reply
Re: ORDERnary In reply to
Perhaps if you state how it's not working for you it would make the job of trying to help possible.

So take a deep breath and state what problems you are having please.

It does also help to search and read all related threads when installing a mod, as you can often find your solution with the posts of others who have installed or used the same mod.




Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/
Quote Reply
Re: ORDERnary In reply to
A couple of week ago I spent a lot more than 12 hours to thoroughly revise the mod, adding/changing functionality and especially to implement it fresh on a DBman out-of-the-box (only with the long/short added to the User Friendly mod). This in order to make a "clean" source code available for anyone interested to ease the implementation task. In these files all ORDERnary mod changes are clearly marked. The links to the source codes are to be found in the mod description.

Of course it is possible that with this major update that there are flaws in the installation description. I would be happy for some feedback to correct and streamline the installation description further.

So:
1. Did you have a DBman with user friendly + long/short mod up and running before you started with the ORDERnary mod?

2. Where does it start to go wrong?

3. Have you compared your files with the available source files?


O grain

Quote Reply
Re: ORDERnary In reply to
At one point I had it so that the log-in screen came up, but now I've regressed to the 'ol INTERNAL SERVER ERROR. I think I've read every thread pertaining to this, but of course, could have missed something. I suppose the first place to start is with the cgi script, so here it is:
====================
#!/usr/bin/perl
# =====================================================================
# ----------------------
# DBMan
# ----------------------
# Database Administrator
#
# File: db.cgi
# Description: This is the main program file and contains all the functionality
# of the database manager.
# Author: Alex Krohn
# Web: http://www.gossamer-threads.com/
# Version: 2.05
# CVS Revision: $Id: db.cgi,v 1.6 2000/07/08 18:14:40 alex Exp $
#
# COPYRIGHT NOTICE:
#
# Copyright 1997 Gossamer Threads Inc. All Rights Reserved.
#
# This program is being distributed as shareware. It may be used and
# modified free of charge for personal, academic, government or non-profit
# use, so long as this copyright notice and the header above remain intact.
# Any commercial use should be registered. Please also send me an email,
# and let me know where you are using this script. By using this program
# you agree to indemnify Gossamer Threads Inc. from any liability.
#
# Selling the code for this program without prior written consent is
# expressly forbidden. Obtain permission before redistributing this
# program over the Internet or in any other medium. In all cases
# copyright and header must remain intact.
#
# Please check the README file for full details on registration.
# =====================================================================

# If you run into problems, set $db_script_path to the full path
# to your directory.
$db_script_path = ".";

# Load the form information and set the config file and userid.
local(%in) = &parse_form;
$in{'db'} ? ($db_setup = $in{'db'}) : ($db_setup = 'default');
$in{'uid'} ? ($db_uid = $in{'uid'}): ($db_uid = '');

# Required Librariers
# --------------------------------------------------------
# Make sure we are using perl 5.003, load the config file, and load the auth file.
eval {
unshift (@INC, $db_script_path);
require 5.003; # We need at least Perl 5.003
unless ($db_setup =~ /^[A-Za-z0-9]+$/) { die "Invalid config file name: $db_setup"; }
require "$db_setup.cfg"; # Database Definition File
require "auth.pl"; # Authorization Routines
};
if ($@) { &cgierr ("Error loading required libraries.\nCheck that they exist, permissions are set correctly and that they compile.\nReason: $@"); }

# If we are using benchmarking, then we start a timer and stop it around &main. Then we print the difference.
if ($db_benchmark) { eval { require Benchmark; }; if ($@) { &cgierr ("Fatal Error Benchmark Module not installed: $@"); } $t0 = new Benchmark; }

eval { &main; }; # Trap any fatal errors so the program hopefully
if ($@) { &cgierr("fatal error: $@"); } # never produces that nasty 500 server error page.

# Stop the timer and print.
if ($db_benchmark) { $t1 = new Benchmark; print "<h6>Processing Time: " . timestr(timediff($t1, $t0)) . "</h6>"; }

# Display debugging information if requested.
&cgierr("Debug Information") if ($db_debug);

exit; # There are only two exit calls in the script, here and in in &cgierr.

sub main {
# --------------------------------------------------------
my ($status, $uid);
local($per_add, $per_view, $per_mod, $per_del, $per_admin);

$|++; # Flush Output Right Away

&auth_cleanup unless ($auth_no_authentication); # Remove old session files.

($status, $uid, $per_view, $per_add, $per_del, $per_mod, $per_admin)
= &auth_check_password; # Authenticate User, get permissions and userid.

if ($status eq "ok") {
# Set the script link URL with db and user info for links. Use $db_script_url for forms.
$db_script_link_url = "$db_script_url?db=$db_setup&uid=$db_uid";
if ($uid eq "default") { $db_userid = $uid; }
else { ($db_userid) = $db_uid =~ /([A-Za-z0-9]+)\.\d+/; }

# Main Menu. Check to see what the user requested, then, if he has permission for that
# request, do it. Otherwise send the user off to an unauthorized request page.
if ($in{'add_form'}) { if ($per_add) { &html_add_form; } else { &html_unauth; } }
elsif ($in{'add_record'}) { if ($per_add) { &add_record; } else { &html_unauth; } }
elsif ($in{'view_search'}) { if ($per_view) { &html_view_search; } else { &html_unauth; } }
elsif ($in{'view_records'}) { if ($per_view) { &view_records; } else { &html_unauth; } }
elsif ($in{'delete_search'}) { if ($per_del) { &html_delete_search; } else { &html_unauth; } }
elsif ($in{'delete_form'}) { if ($per_del) { &html_delete_form; } else { &html_unauth; } }
elsif ($in{'delete_records'}) { if ($per_del) { &delete_records; } else { &html_unauth; } }
elsif ($in{'modify_search'}) { if ($per_mod) { &html_modify_search; } else { &html_unauth; } }
elsif ($in{'modify_form'}) { if ($per_mod) { &html_modify_form; } else { &html_unauth; } }
elsif ($in{'modify_form_record'}) { if ($per_mod) { &html_modify_form_record; } else { &html_unauth; } }
elsif ($in{'modify_record'}) { if ($per_mod) { &modify_record; } else { &html_unauth; } }
elsif ($in{'admin_display'}) { if ($per_admin) { &admin_display; } else { &html_unauth; } }
elsif ($in{'logoff'}) { &auth_logging('logged off') if ($auth_logging);
$auth_logoff ? (print "Location: $auth_logoff\n\n") : (print "Location: $db_script_url\n\n"); }
elsif ((keys(%in) <= 2) ||
($in{'login'})) { &html_home; }

########################### ORDERnary start ##############################

elsif ($in{'create_order'}) { &html_create_order; }
elsif ($in{'verify_email'}) { &html_create_order; }
elsif ($in{'rectify_email'}) { &html_create_order; }
elsif ($in{'verify_address'}) { &html_create_address; }
elsif ($in{'rectify_address'}) { &html_create_address; }
elsif ($in{'email_address_OK'}) { &html_create_order; }
elsif ($in{'address_OK'}) { &html_send_addressed_order; }
elsif ($in{'add_order'}) { &html_add_order; }
elsif ($in{'more_order'}) { &html_view_search; }
elsif ($in{'check_order'}) { &html_check_order; }
elsif ($in{'edit_order'}) { &html_edit_order; }
elsif ($in{'change_order'}) { &html_change_order; }
elsif ($in{'send_order'}) { &html_send_order; }
elsif ($in{'cancel_order'}) { &html_cancel_order; }

############################ ORDERnary end ##############################
else { &html_unkown_action; }
}


# If we allow users to signup, and they want to, go to the signup form.
elsif ($auth_signup and $in{'signup_form'}) {
&html_signup_form;
}
elsif ($auth_signup and $in{'signup'}) {
&signup;
}
# Auth Check Password has determined that the user has not logged in, so let's send
# him to the login screen.
elsif ($status eq "no login") {
&html_login_form;
}
# Auth Check Password had an error trying to authenticate the user. Probably there was
# an invalid user/password or the user file has expired. Let's go to an error page and
# ask the user to re log on.
else {
&html_login_failure($status);
}
}

sub add_record {
# --------------------------------------------------------
# Adds a record to the database. First, validate_record is called
# to make sure the record is ok to add. If it is, then the record is
# encoded and added to the database and the user is sent to
# html_add_success, otherwise the user is sent to html_add_failure with
# an error message explaining why. The counter file is also updated to the
# next number.

my ($output, $status, $counter);
# Set the userid to the logged in user.
($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);

# First we validate the record to make sure the addition is ok.
$in{'Price'} =~ s/,//g; ### remove comma from entry
$status = &validate_record;

# We keep checking for the next available key, or until we've tried 50 times
# after which we give up.
while ($status eq "duplicate key error" and $db_key_track) {
return "duplicate key error" if ($counter++ > 50);
$in{$db_key}++;
$status = &validate_record;
}

if ($status eq "ok") {
open (DB, ">>$db_file_name") or &cgierr("error in add_record. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB &join_encode(%in);
close DB; # automatically removes file lock
if ($db_key_track) {
open (ID, ">$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) {
flock(ID, 2) or &cgierr("unable to get exclusive lock on $db_id_file_name.\nReason: $!");
}
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock
}
&auth_logging("added record: $in{$db_key}") if ($auth_logging);
&html_add_success;
}
else {
&html_add_failure($status);
}
}

sub delete_records {
# --------------------------------------------------------
# Deletes a single or multiple records. First the routine goes thrrough
# the form input and makes sure there are some records to delete. It then goes
# through the database deleting each entry and marking it deleted. If there
# are any keys not deleted, an error message will be returned saying which keys
# were not found and not deleted, otherwise the user will go to the success page.

my ($key, %delete_list, $rec_to_delete, @lines, $line, @data, $errstr, $succstr, $output, $restricted);
$rec_to_delete = 0;
foreach $key (keys %in) { # Build a hash of keys to delete.
if ($in{$key} eq "delete") {
$delete_list{$key} = 1;
$rec_to_delete = 1;
}
}
if (!$rec_to_delete) {
&html_delete_failure("no records specified.");
return;
}

open (DB, "<$db_file_name") or &cgierr("error in delete_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;

($restricted = 1) if ($auth_modify_own and !$per_admin);

LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
($output .= "$line\n" and next LINE) if ($restricted and ($db_userid ne $data[$auth_user_field]));

$delete_list{$data[$db_key_pos]} ? # if this id is one we want to delete
($delete_list{$data[$db_key_pos]} = 0) : # then mark it deleted and don't print it to the new database.
($output .= $line . "\n"); # otherwise print it.
}

foreach $key (keys %delete_list) {
$delete_list{$key} ? # Check to see if any items weren't deleted
($errstr .= "$key,") : # that should have been.
($succstr .= "$key,"); # For logging, we'll remember the one's we deleted.
}
chop($succstr); # Remove trailing delimeter
chop($errstr); # Remove trailing delimeter

open (DB, ">$db_file_name") or &cgierr("error in delete_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB $output;
close DB; # automatically removes file lock

&auth_logging("deleted records: $succstr") if ($auth_logging);
$errstr ? # Do we have an error?
&html_delete_failure($errstr) : # If so, then let's report go to the failure page,
&html_delete_success($succstr); # else, everything went fine.
}

sub modify_record {
# --------------------------------------------------------
# This routine does the actual modification of a record. It expects
# to find in %in a record that is already in the database, and will
# rewrite the database with the new entry. First it checks to make
# sure that the modified record is ok with validate record.
# It then goes through the database looking for the right record to
# modify, if found, it prints out the modified record, and returns
# the user to a success page. Otherwise the user is returned to an error
# page with a reason why.

my ($status, $line, @lines, @data, $output, $found, $restricted);

$status = &validate_record; # Check to make sure the modifications are ok!

if ($status eq "ok") {
open (DB, "<$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;

($restricted = 1) if ($auth_modify_own and !$per_admin);

$found = 0; # Make sure the record is in here!
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; } # Skip and Remove blank lines
if ($line =~ /^#/) { $output .= $line; next LINE; } # Comment Line
chomp ($line);
@data = &split_decode($line);
($output .= "$line\n" and next LINE) if ($restricted and ($db_userid ne $data[$auth_user_field]));

if ($data[$db_key_pos] eq $in{$db_key}) {
# If we have userid's and this is not an admin, then we force the record to keep it's own
# userid.
if ($auth_user_field >= 0 and (!$per_admin or !$in{$db_cols[$auth_user_field]})) {
$in{$db_cols[$auth_user_field]} = $data[$auth_user_field];
}
$output .= &join_encode(%in);
$found = 1;
}
else {
$output .= $line . "\n"; # else print regular line.
}
}
if ($found) {
open (DB, ">$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB $output;
close DB; # automatically removes file lock

&auth_logging("modified record: $in{$db_key}") if ($auth_logging);
&html_modify_success;
}
else {
&html_modify_failure("$in{$db_key} (can't find requested record)");
}
}
else {
&html_modify_failure($status); # Validation Error
}
}

sub view_records {
# --------------------------------------------------------
# This is called when a user is searching the database for
# viewing. All the work is done in query() and the routines just
# checks to see if the search was successful or not and returns
# the user to the appropriate page.

my ($status, @hits) = &query("view");
if ($status eq "ok") {
&html_view_success(@hits);
}
else {
&html_view_failure($status);
}
}

sub query {
# --------------------------------------------------------
# First let's get a list of database fields we want to search on and
# store it in @search_fields

my ($i, $column, @search_fields, @search_gt_fields, @search_lt_fields, $maxhits, $numhits, $nh,
$field, @regexp, $line, @values, $key_match, @hits, @sortedhits, $next_url, $next_hit, $prev_hit,
$first, $last, $upper, $lower, $left, $right, $restricted);
local (%sortby);

# First thing we do is find out what we are searching for. We build a list of fields
# we want to search on in @search_fields.
if ($in{'keyword'}) { # If this is a keyword search, we are searching the same
$i = 0; # thing in all fields. Make sure "match any" option is
$in{'ma'} = "on"; # on, otherwise this will almost always fail.
foreach $column (@db_cols) {
if (($db_sort{$column} eq 'date') or &date_to_unix($in{'keyword'})) { $i++; next; }
if ($i == $auth_user_field) { $i++; next; }
push (@search_fields, $i); # Search every column
$in{$column} = $in{'keyword'}; # Fill %in with keyword we are looking for.
$i++;
}
}
else { # Otherwise this is a regular search, and we only want records
$i = 0; # that match everything the user specified for.
foreach $column (@db_cols) {
if ($in{$column} =~ /^\>(.+)$/) { ($db_sort{$column} eq 'date') and (&date_to_unix($1) or return "Invalid date format: '$1'");
push (@search_gt_fields, $i); $in{"$column-gt"} = $1; $i++; next; }
if ($in{$column} =~ /^\<(.+)$/) { ($db_sort{$column} eq 'date') and (&date_to_unix($1) or return "Invalid date format: '$1'");
push (@search_lt_fields, $i); $in{"$column-lt"} = $1; $i++; next; }
if ($in{$column} !~ /^\s*$/) { ($db_sort{$column} eq 'date') and (&date_to_unix($in{$column}) or return "Invalid date format: '$in{$column}'");
push(@search_fields, $i); $i++; next; }
if ($in{"$column-gt"} !~ /^\s*$/) { ($db_sort{$column} eq 'date') and (&date_to_unix($in{"$column-gt"}) or return "Invalid date format: '$in{$column}'");
push(@search_gt_fields, $i); }
if ($in{"$column-lt"} !~ /^\s*$/) { ($db_sort{$column} eq 'date') and (&date_to_unix($in{"$column-lt"}) or return "Invalid date format: '$in{$column}'");
push(@search_lt_fields, $i); }
$i++;
}
}
# If we don't have anything to search on, let's complain.
if (!@search_fields and !@search_gt_fields and !@search_lt_fields) {
return "no search terms specified";
}

# Define the maximum number of hits we will allow, and the next hit counter.
$in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
$in{'nh'} ? ($nh = $in{'nh'}) : ($nh = 1);
$numhits = 0;

# Let's set restricted to 1 if the user can only view/mod their own and
# this isn't an admin.
($restricted = 1) if ($_[0] eq "view" and $auth_view_own and !$per_admin);
($restricted = 1) if ($_[0] eq "mod" and $auth_modify_own and !$per_admin);

# Now let's build up all the regexpressions we will use. This saves the program
# from having to recompile the same regular expression every time.
foreach $field (@search_fields) {
my $tmpreg = "$in{$db_cols[$field]}";
(!$in{'re'}) and ($tmpreg = "\Q$tmpreg\E");
($in{'ww'}) and ($tmpreg = "\\b$tmpreg\\b");
(!$in{'cs'}) and ($tmpreg = "(?i)$tmpreg");
($in{$db_cols[$field]} eq "*") and ($tmpreg = ".*"); # A "*" matches anything.
$regexp_func[$field] = eval 'sub { m/$tmpreg/o; }';
$regexp_bold[$field] = $tmpreg;
}

# Now we go through the database and do the actual searching.
# First figure out which records we want:
$first = ($maxhits * ($nh - 1));
$last = $first + $maxhits - 1;

open (DB, "<$db_file_name") or &cgierr("error in search. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE; # Skip comment Lines.
(/^\s*$/) and next LINE; # Skip blank lines.
$line = $_; chomp ($line); # Remove trailing new line.
@values = &split_decode($line);

# If we are only allowed to view/mod our own record, then let's check here.
next LINE if ($restricted and ($db_userid ne $values[$auth_user_field]));

# Normal searches.
$key_match = 0;
foreach $field (@search_fields) {
$_ = $values[$field]; # Reg function works on $_.
$in{'ma'} ?
($key_match = ($key_match or &{$regexp_func[$field]})) :
(&{$regexp_func[$field]} or next LINE);
}
# Greater then searches.
foreach $field (@search_gt_fields) {
$term = $in{"$db_cols[$field]-gt"};
if ($db_sort{$db_cols[$field]} eq "date") {
$in{'ma'} ?
($key_match = ($key_match or (&date_to_unix($values[$field])) > &date_to_unix($term))) :
(&date_to_unix($values[$field]) > (&date_to_unix($term)) or next LINE);
}
elsif ($db_sort{$db_cols[$field]} eq 'alpha') {
$in{'ma'} ?
($key_match = ($key_match or ($values[$field] > $term))) :
((lc($values[$field]) gt lc($term)) or next LINE);
}
else {
$in{'ma'} ?
($key_match = ($key_match or ($values[$field] > $term))) :
(($values[$field] > $term) or next LINE);
}
}
# Less then searches.
foreach $field (@search_lt_fields) {
$term = $in{"$db_cols[$field]-lt"};
if ($db_sort{$db_cols[$field]} eq "date") {
$in{'ma'} ?
($key_match = ($key_match or (&date_to_unix($values[$field]) < &date_to_unix($term)))) :
(&date_to_unix($values[$field]) < (&date_to_unix($term)) or next LINE);
}
elsif ($db_sort{$db_cols[$field]} eq 'alpha') {
$in{'ma'} ?
($key_match = ($key_match or ($values[$field] < $term))) :
((lc($values[$field]) lt lc($term)) or next LINE);
}
else {
$in{'ma'} ?
($key_match = ($key_match or ($values[$field] < $term))) :
(($values[$field] < $term) or next LINE);
}
}
# Did we find a match? We only add the hit to the @hits array if we need it. We can
# skip it if we are not sorting and it's not in our first < > last range.
if ($key_match || (!($in{'keyword'}) && !($in{'ma'}))) {
if (exists $in{'sb'}) {
$sortby{(($#hits+1) / ($#db_cols+1))} = $values[$in{'sb'}];
push (@hits, @values);
}
else {
(($numhits >= $first) and ($numhits <= $last)) and push (@hits, @values);
}
$numhits++; # But we always count it!
}
}
close DB;

# Now we've stored all our hits in @hits, and we've got a sorting values stored
# in %sortby indexed by their position in @hits.
$numhits ? ($db_total_hits = $numhits) : ($db_total_hits = 0);
($db_total_hits == 0) and return ("no matching records.");

# Sort the array @hits in order if we are meant to sort.
if (exists $in{'sb'}) { # Sort hits on $in{'sb'} field.
my ($sort_order, $sort_func);
$in{'so'} ? ($sort_order = $in{'so'}) : ($sort_order = "ascend");
$sort_func = "$db_sort{$db_cols[$in{'sb'}]}_$sort_order";

foreach $hit (sort $sort_func (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit; $last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sortedhits, @hits[$first .. $last]);
}
@hits = @sortedhits;
}

# If we have to many hits, let's build the next toolbar, and return only the hits we want.
if ($numhits > $maxhits) {
# Remove the nh= from the query string.
$next_url = $ENV{'QUERY_STRING'};
$next_url =~ s/\&nh=\d+//;
$next_hit = $nh + 1; $prev_hit = $nh - 1;

# Build the next hits toolbar. It seems really complicated as we have to do
# some number crunching to keep track of where we are on the toolbar, and so
# that the toolbar stays centred.

# First, set how many pages we have on the left and the right.
$left = $nh; $right = int($numhits/$maxhits) - $nh;
# Then work out what page number we can go above and below.
($left > 7) ? ($lower = $left - 7) : ($lower = 1);
($right > 7) ? ($upper = $nh + 7) : ($upper = int($numhits/$maxhits) + 1);
# Finally, adjust those page numbers if we are near an endpoint.
(7 - $nh >= 0) and ($upper = $upper + (8 - $nh));
($nh > ($numhits/$maxhits - 7)) and ($lower = $lower - ($nh - int($numhits/$maxhits - 7) - 1));
$db_next_hits = "";

# Then let's go through the pages and build the HTML.
($nh > 1) and ($db_next_hits .= qq~<a href="$db_script_url?$next_url&nh=$prev_hit">[<<]</a> ~);
for ($i = 1; $i <= int($numhits/$maxhits) + 1; $i++) {
if ($i < $lower) { $db_next_hits .= " ... "; $i = ($lower-1); next; }
if ($i > $upper) { $db_next_hits .= " ... "; last; }
($i == $nh) ?
($db_next_hits .= qq~$i ~) :
($db_next_hits .= qq~<a href="$db_script_url?$next_url&nh=$i">$i</a> ~);
if (($i * $maxhits) >= $numhits) { last; } # Special case if we hit exact.
}
$db_next_hits .= qq~<a href="$db_script_url?$next_url&nh=$next_hit">[>>]</a> ~ unless ($nh == $i);

# Slice the @hits to only return the ones we want, only have to do this if the results are sorted.
if (exists $in{'sb'}) {
$first = ($maxhits * ($nh - 1)) * ($#db_cols+1);
$last = $first + (($#db_cols+1) * $maxhits) - 1;
$last = $#hits if ($last > $#hits);
@hits = @hits[$first .. $last];
}
}

# Bold the results
if ($db_bold and $in{'view_records'}) {
for $i (0 .. (($#hits+1) / ($#db_cols+1)) - 1) {
$offset = $i * ($#db_cols+1);
foreach $field (@search_fields) {
$hits[$field + $offset] =~ s,(<[^>]+>)|($regexp_bold[$field]),defined($1) ? $1 : "<B>$2</B>",ge;
}
}
}
return ("ok", @hits);
}

sub admin_display {
# --------------------------------------------------------
# Let's an admin add/update/remove users from the authorization file.
#
my ($message, @lines, $line);

# Do we have anything to do?
CASE: {
# If we've been passed in new_username, then we are adding a new user. Do
# some basic error checking and then add him into the password file.
$in{'new_username'} and do {
unless ((length($in{'new_username'}) >= 3) and (length($in{'new_username'}) <= 12) and ($in{'new_username'} =~ /^[a-zA-Z0-9]+$/)) {
$message = "Invalid username: $in{'new_username'}. Must only contain letters and numbers and be less then 12 and greater then 3 characters.";
last CASE;
}
unless ((length($in{'password'}) >= 3) and (length($in{'password'}) <= 12)) {
$message = "Invalid password: '$in{'password'}'. Must be less then 12 and greater then 3 characters.";
last CASE;
}
open (PASS, ">>$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/');
my $salt = join '', @salt_chars[rand 64, rand 64];
my $encrypted = crypt($in{'password'}, $salt);
print PASS "$in{'new_username'}:$encrypted:$in{'per_view'}:$in{'per_add'}:$in{'per_del'}:$in{'per_mod'}:$in{'per_admin'}\n";
close PASS;
$message = "User: $in{'new_username'} created.";
last CASE;
};
# If we've been passed in delete, then we are removing a user. Check
# to make sure a user was selected then try and remove him.
$in{'delete'} and do {
unless ($in{'username'}) {
$message = "No username selected to delete.";
last CASE;
}
open (PASS, "<$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1) }
@lines = <PASS>;
close PASS;

open (PASS, ">$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
my $found = 0;
foreach $line (@lines) {
($line =~ /^$in{'username'}:/) ?
($found = 1) :
print PASS $line;
}
close PASS;
$found ?
($message = "User: $in{'username'} deleted.") :
($message = "Unable to find userid: $in{'username'} in password file.");
last CASE;
};
# If we have a username, and the admin didn't press inquire, then
# we are updating a user.
($in{'username'} && !$in{'inquire'}) and do {
open (PASS, "<$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1); }
@lines = <PASS>;
close PASS;

open (PASS, ">$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
my $found = 0;
foreach $line (@lines) {
if ($line =~ /^$in{'username'}:/) {
my $password = (split (/:/, $line))[1];
unless ($password eq $in{'password'}) {
my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/');
my $salt = join '', @salt_chars[rand 64, rand 64];
$password = crypt($in{'password'}, $salt);
}
print PASS "$in{'username'}:$password:$in{'per_view'}:$in{'per_add'}:$in{'per_del'}:$in{'per_mod'}:$in{'per_admin'}\n";
$found = 1;
}
else {
print PASS $line;
}
}
$in{'inquire'} = $in{'username'};
$found ?
($message = "User: $in{'username'} updated.") :
($message = "Unable to find user: '$in{'username'}' in the password file.");
last CASE;
};
};

# Now let's load the list of users.
open (PASS, "<$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1); }
@lines = <PASS>;
close PASS;

# If we are inquiring, let's look for the specified user.
my (@data, $user_list, $perm, $password);

$user_list = qq~<select name="username"><option> </option>~;
LINE: foreach $line (@lines) {
$line =~ /^#/ and next LINE;
$line =~ /^\s*$/ and next LINE;
chomp $line;
@data = split (/:/, $line);

if ($in{'inquire'} and ($in{'username'} eq $data[0])) {
$user_list .= qq~<option value="$data[0]" SELECTED>$data[0]</option>\n~;
$perm = qq|
View <input type=checkbox name="per_view" value="1" |; ($data[2] and $perm .= "CHECKED"); $perm .= qq|>
Add <input type=checkbox name="per_add" value="1" |; ($data[3] and $perm .= "CHECKED"); $perm .= qq|>
Delete <input type=checkbox name="per_del" value="1" |; ($data[4] and $perm .= "CHECKED"); $perm .= qq|>
Modify <input type=checkbox name="per_mod" value="1" |; ($data[5] and $perm .= "CHECKED"); $perm .= qq|>
Admin <input type=checkbox name="per_admin" value="1" |; ($data[6] and $perm .= "CHECKED"); $perm .= qq|>|;
$password = $data[1];
}
else {
$user_list .= qq~<option value="$data[0]">$data[0]</option>\n~;
}
}
$user_list .= "</select>";
# Build the permissions list if we haven't inquired in someone.
if (!$perm) {
$perm = qq|
View <input type=checkbox name="per_view" value="1" |; ($auth_default_perm[0] and $perm .= "CHECKED"); $perm .= qq|>
Add <input type=checkbox name="per_add" value="1" |; ($auth_default_perm[1] and $perm .= "CHECKED"); $perm .= qq|>
Delete <input type=checkbox name="per_del" value="1" |; ($auth_default_perm[2] and $perm .= "CHECKED"); $perm .= qq|>
Modify <input type=checkbox name="per_mod" value="1" |; ($auth_default_perm[3] and $perm .= "CHECKED"); $perm .= qq|>
Admin <input type=checkbox name="per_admin" value="1" |; ($auth_default_perm[4] and $perm .= "CHECKED"); $perm .= qq|>|;
}
&html_admin_display ($message, $user_list, $password, $perm);
}

############################ ORDERnary ###################################

###########################################################################
# 1. create_order saves initial customer data in file that has the same #
# name as the entered email address: <email_address>.txt #
###########################################################################

sub create_order {
open (FILE, ">$orders_dir$in{'order_mail'}.txt") || &cgierr("Can't open $orders_dir$in{'order_mail'}.txt.");
print FILE "Order of $merchandise to $shop, ";
$order_date = &get_date;
print FILE "$order_date\n\n";
print FILE "E-Mail: $in{'order_mail'}\n\n\n";
print FILE "--------------------------------------------------------\n\n";

close (FILE);
}

##########################################################################
# 2. add_to_order appends data concering one item of the merchandise to #
# the created file. #
# A duplicate check is made on the intire new item #
##########################################################################

sub add_to_order {

$add_error = 0;
$duplicate = 0;

if (scalar($in{'no_of_items'}) < 1) {
$message = "Invalid number of items. Must be 1 or more.";
$add_error = 1;
}
else {

$add_item = $in{'add_item'};
$add_item = "* $in{'no_of_items'} $pieces - $add_item\n";

# First check duplicate
open (FILE, "<$orders_dir$in{'order_mail'}.txt") || &cgierr("Can't open $orders_dir$in{'order_mail'}.txt.");
@lines = <FILE>;
close (FILE);
foreach $line (@lines) {
if ($line =~ /^\*/) {
if ($add_item eq $line) {
$duplicate = 1;
$message = "The item had been added to the order previously and was not added again.";
last;
}
}
}
if (!$duplicate) {
open (FILE, ">>$orders_dir$in{'order_mail'}.txt") || &cgierr("Can't open $orders_dir$in{'order_mail'}.txt.");
print FILE "$add_item";
close (FILE);
}
}
}

##########################################################################
# 3. sum_up_order calculates the number of pieces totally and the #
# total sum #
##########################################################################

sub sum_up_order {
open (FILE, "<$orders_dir$in{'order_mail'}.txt") || &cgierr("Can't open $orders_dir$in{'order_mail'}.txt.");
@lines = <FILE>;
close (FILE);


$total_sum = 0;
$total_items = 0;
foreach $line (@lines) {
if ($line =~ /^\*/) {
@this_item = split(/$currency/,$line);
$this_sum = scalar($this_item[1]);
@this_item = split(/-/,$line);
$these_items = $this_item[0];
$these_items =~ s/\*//;
$these_items =~ s/$pieces//;
$number_items = scalar($these_items);
$this_sum = scalar($number_items) * scalar($this_sum);
$total_sum += $this_sum;
$total_items += $number_items;
}
}
$shipping = 0;
if ($total_sum > 0) {
$shipping = $shipping1;
}
if (( $total_sum > $shipping1_limit) && ( $total_sum
< $shipping2_limit)) {
$shipping = $shipping2;
}
elsif ($total_sum > $shipping2_limit) {
$shipping = $shipping3;
}

@zero_check = split(/\./,$shipping);
if (length($zero_check[1]) == 0) {
$shipping = $shipping .".00";
}
elsif (length($zero_check[1]) == 1) {
$shipping = $shipping ."0";
}

@zero_check = split(/\./,$total_sum);
if (length($zero_check[1]) == 0) {
$total_sum = $total_sum .".00";
}
elsif (length($zero_check[1]) == 1) {
$total_sum = $total_sum ."0";
}
$grand_total = $total_sum + $shipping;

@zero_check = split(/\./,$grand_total);
if (length($zero_check[1]) == 0) {
$grand_total = $grand_total .".00";
}
elsif (length($zero_check[1]) == 1) {
$grand_total = $grand_total ."0";
}
}

##########################################################################
# 4. send_order mails the order to shop keeper and confirmation to #
# customer #
##########################################################################

sub send_order{

$order_date = &get_date;

open (FILE, "<$orders_dir$in{'order_mail'}.txt") || &cgierr("Can't open $orders_dir$in{'order_mail'}.txt.");
@this_order = <FILE>;

close (FILE);


open (MAIL, "$mailprog") || &cgierr( "Can't open mail program!\n");

## The next two lines are modified for the sake of the demo. De- vomment the first To: line
## and remove the second To: line

# print MAIL "To: $shop_email\n";
print MAIL "To: $in{'order_mail'}\n";
print MAIL "From: $in{'order_mail'}\n";


print MAIL "Subject: Order of $merchandise\n";
print MAIL "A new order of $merchandise:\n\n";

print MAIL "@this_order";
print MAIL "\n\nOrder date: $order_date\n\n";
print MAIL "Tracking information\n";
print MAIL "Remote Host: $ENV{REMOTE_HOST}\n";
print MAIL "Remote Addr: $ENV{'REMOTE_ADDR'}\n";
$server_time = localtime(time());
print MAIL "Server-time: $server_time";
close (MAIL);


###### Mail confirmation to customer #######

open (MAIL, "$mailprog") || &cgierr( "Can't open mail program!\n");
print MAIL "To: $in{'order_mail'}\n";
print MAIL "From: $shop_email\n";


print MAIL "Subject: Your order of $merchandise\n\n";
print MAIL "Thanks for your order according to below\n\n";

print MAIL "@this_order";
print MAIL "\n\nOrder date: $order_date\n\n";
print MAIL "We will deliver as soon as possible.\n\n";
print MAIL "$shop\n\n";
close (MAIL);
}

##########################################################################
# 5. create_order_header add the complete address information when the #
# order is to be sent #
##########################################################################

sub create_order_header {

# first retrieve and keep what has been ordered
open (FILE, "<$orders_dir$in{'order_mail'}.txt") || &cgierr("Can't open $orders_dir$in{'order_mail'}.txt.");
@the_order = <FILE>;
close (FILE);


# write the address information as a header
open (FILE, ">$orders_dir$in{'order_mail'}.txt") || &cgierr("Can't open $orders_dir$in{'order_mail'}.txt.");
print FILE "Order of $merchandise to $shop, ";
$order_date = &get_date;
print FILE "$order_date\n\n";
print FILE "Name: $in{'order_name'}\n";
print FILE "E-Mail: $in{'order_mail'}\n";
print FILE "Address: $in{'order_address'}\n";
print FILE "Postal address: $in{'order_zipcode'} ";
print FILE "$in{'order_city'}\n\n\n";
print FILE "--------------------------------------------------------\n\n";


# append the list with the ordered items
foreach $the_order (@the_order) {
if ($the_order =~ /^\*/) {
print FILE "$the_order";
}
}

close (FILE);
}

############################ end ORDERnary ############################


Quote Reply
Re: ORDERnary In reply to
OGrain - I will assume you are the writer of the Mod. Very nice job. I'm sure it's not your fault that I can't get it to work.
To answer your questions, the long/short user friendly is up and running at www.paperbacks.com. All I wanted was a simple way for folks to order and this seemed to be the way to go. When I tried to make the revisions to the files that are working I kept getting INTERNAL errors so I quit that and tried to just make some changes to the source files. When that didn't work I went in and tried a hybrid method where at one point I actually got the logon screen to come up. But when I went to do a search nothing would be returned. I would just keep getting the same form looking at me. At this point I starting researching the most politically correct forms of suicide, but feel asleep and have not touched it in 4 or 5 days. Oh, yes, at some point in there I was comparing the source files, but it all became rather hazy.
I will try again when I feel stronger.
Thank you.

Quote Reply
Re: ORDERnary - cgi In reply to
It's not bound to be an error in the cgi script. If you can run it from the prompt and it shows no compilation errors but prints out something like "This script must be called from the web", then the error is in the .pl file, maybe a missing curly bracket.

What I would do in that case is to remove the contents of all new subs and only keep the "frame" as a stub, like

sub new_html_sub {
# everything deleted here
}

If you can run the script, then add one sub at a time. If you find the fault and it is caused by en error in the mod description, please report it.

And it would have been better to have a link to your text file than including it in the post Wink. Lenghtly non formatted code is difficult to read.

O grain

Quote Reply
Re: ORDERnary - cgi In reply to
O Grain - Here's the latest. After much frustration I took your 3 files (cgi, cfg, and html) exactly as you have them in the documentaion, made my changes, and it came up like your demo. Two big problems. 1 - the search doesn't bring up anything. 2 - there's no login or home page. If you want to see what I have accomplished please go to http://209.239.40.110/cgi-bin/bdman/db.cgi . I have the script working fine w/o ORDERnary at http://209.239.40.110/cgi-bin/dbman/db.cgi (you will notice I cleverly changed db to bd for testing), but it would be great to have your order system because it fits so well. I will put the script on-line at http://www.paperbacks.com/THEBIGTHREE.

Don't forget that big reward of $25 worth of paperbacks.
Whether we get it done or note, I do sincerely appreciate your help.
Thanks,
Dave


Quote Reply
Re: ORDERnary - cgi In reply to
Hi PaperJack

Number 2 is probably easy to solve: Login is disabled in the code, like it says in the demo description. Look in dg.cgi a few lines where you added the big portion for the following lines and remove the red marked parts
Code:

# Auth Check Password has determined that the user has not logged in, so let's send
# him to the login screen.
elsif ($status eq "no login") {
# &html_login_form;
&html_view_search;
For number 1 I simply don't understand: When you click View Records you get back to the Search Form again? The query string says &view_records=View+Records but you still come to View Search?

The text files that are available are the actual files that are used in the demo, having been made available by soft links to the cgi-bin, so the problem shouldn't be there.
The code reads:
Code:
elsif ($in{'view_search'}) { if ($per_view) { &html_view_search; } else { &html_unauth; } }
elsif ($in{'view_records'}) { if ($per_view) { &view_records; } else { &html_unauth; } }


Unfortunately I cannot give much more continuos support on the ORDERnary mod for quite some time. I need to pack my computer in an hour or so, since I'll be travelling round the world (eastbound) for a year to catch up on my passions: kites and tango. There are some great kite festivals in Korea, China and Japan in April, May and June Smile!

I hope someone else can step in an help you with the this View_search - View_records problem. Maybe you should start another thread with that problem as the title?

Good luck