#! /usr/bin/perl
use strict;
no strict 'refs';
use warnings;
use DBI;
use Date::Calc qw(:all);
use File::Temp qw/ tempfile tempdir /;
use File::Copy;
my $version = '1.0';
#############################################################################
# CONTENTS
#############################################################################
#Section 1. Description and copyright statement
#Section 2. Setup and instructions
#Section 3. User definable variables
#Section 4. Code
#############################################################################
# Section 1. DESCRIPTION AND COPYRIGHT STATEMENT
#############################################################################
#this script is used to import patient demographics in a batch file
# (c) Robbie Coull, 2009
# robbie@coull.net
#
# This code 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 3 of the License, or
# (at your option) any later version.
#
# This code 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.
#
# see .
#############################################################################
# Section 2. SETUP AND INSTRUCTIONS
#############################################################################
# Copy this script to a suitable location (eg: /usr/local/scripts)
# sudo mv oscarscan.pl /usr/local/scripts/oscarscan.pl
# Make sure that it can be used by any user
# sudo chmod 777 /usr/local/scripts/oscarscan.pl
#note: must have mysql root password set to '' and the test database created
#for this to work and make sure MySQL is running!
# sudo apt-get install mysql-server
# sudo /etc/init.d/mysql start
#For this script to work you need to install perl modules
# sudo perl -MCPAN -e shell
# cpan> install Bundle::CPAN
# cpan> install Bundle::DBI
# cpan> install Date::Calc
# cpan> install File::Temp
# cpan> exit
#once the modules are installed, make sure MySQL on the local server is NOT running
# sudo /etc/init.d/mysql stop
#turn MySQL off in the services section of admin to prevent it starting at boot
#if OSCAR is installed on a remote server
#then you will need to set up a keychain access to that server
#to allow this script to access the upload folders
#
#eg: ssh -D 8080 -fN $USER@$remoteserver
# ssh -nNL 3306:127.0.0.1:3306 $USER@$remoteserver &> /dev/null &
#
#which provides SSH tunnel access to the server and to the MySQL server
#I recommend using three user logins for MySQL (read only, read write, admin)
#This helps prevent disasters when the wrong command is issued by accident.
#If you do most of your database work from a read only user, then you can't
#accidentally delete anything.
#
#Set up a read-only user and a read-write user as follows:
#log in to mysql
#mysql -uroot -p
#
#GRANT all ON oscar_mcmaster.* TO readonly@localhost IDENTIFIED BY '[password]';
#GRANT select ON oscar_mcmaster.* TO readwrite@localhost IDENTIFIED BY '[password]';
#
#exit
#
#You will need to enter the user names and passwords of these two users
#in the section below - make sure this script can only be accessed by trusted users!
#your import data must be a flat text file with the following information:
#last name-->first name-->dob-->health#-->phone number
#
#the --> is used to separate each text field, a new line is required for each patient.
#
#each patient record will be tagged with the 'Imported patient' marker placed in the Newsletter database field.
#this allows the imported patients to be identified easily. I different import tag should be used for different groups/batches
#############################################################################
# Section 3. USER VARIABLES
#############################################################################
my $user = 'yourlogin';
my $import_file = "/home/$user/importlist.txt"
my $province_code = 'PE';
my $area_code = '902';
my $current_provider = '1000';
my $program_id = '10015';
my $imported_patient_tag = 'Imported patient';
#set diagnostics on or off
my $diagnostic;
#$diagnostic = 'on';
my %pref;
$pref{mysql_database} = 'oscar_mcmaster';
$pref{mysql_read} = 'read_user_name';
$pref{mysql_read_pass} = 'read user password';
$pref{mysql_write} = 'write user name';
$pref{mysql_write_pass} = 'write user password';
$pref{mysql_log} = "/home/$user/logs/mysql.log";
$pref{email_log} = "/home/$user/logs/email.log";
$pref{domain} = 'webaddress.com';
$pref{http} = 'http://www.youraddress.com';
$pref{error_email} = 'yourname\@yourdomain.com';
$pref{'email_test_mode'}= 'on';
#$pref{'email_test_mode'}= 'test only';
#declare variables
my $provider_data;
my @months=qw(
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
);
my @days=qw(
Sun
Mon
Tue
Wed
Thu
Fri
Sat
);
#declare subroutine variables
my $sec;
my $min;
my $hr;
my $mday;
my $mon;
my $year;
my $wday;
my $yday;
my $isdst;
my $longyr;
my $fixmo;
my $sql_date;
my $sql_time;
my $sql_datetime;
my $string_to_be_padded;
my $desired_string_length;
my $padding_character;
my $right_or_left_justify;
my $decimal_places;
my $decimals;
my $decimal_places_digit;
my $missing_digits;
my $mysql_user;
my $mysql_login;
my $mysql_pass;
my $dbh_name;
my $mysql_preferred_user;
my $mysql_original_user;
my $dbh_inuse;
my $sql;
my @mysql_row;
my $mysql_query;
my $mysql_record;
my @mysql_result;
my @mysql_insert;
my %mysql_update;
my $mysql_update_table;
my $mysql_update_table_array;
my $mysql_disconnect;
my $mysql_where;
my $error;
my $this_sql_insert;
my $mysql_insert_table;
my $sql_data;
my $tempath;
my $mysql_result;
my $mysql_set_insert;
my %mysql_set_insert;
my $oscar_date;
my $use_alternative_error_manager = 1;
my $email_only;
my $cgi_title;
my $error_code;
my $notify_only;
my %form_data;
my $no_error;
my $message;
#############################################################################
# Section 4. CODE
#############################################################################
#get the date time
get_time();
#connect to the mysql database
mysql_connect();
header();
#get a list of files in the online registration folder
open (DATA,$import_file) || die("unable to open patient file");
my @lines=;
close (DATA);
my $this_line;
foreach $this_line (@lines) {
my %this_patient;
($this_patient{'last_name'}, $this_patient{'first_name'}, $this_patient{'dob'}, $this_patient{'health_number'}, $this_patient{'home_phone'}) = split(/-->/, $this_line);
$this_patient{'last_name'} = uc $this_patient{'last_name'};
$this_patient{'first_name'} = uc $this_patient{'first_name'};
$this_patient{'home_phone'} = convert_phone_number('1', $this_patient{'home_phone'});
#only process files that contain data
if ($this_patient{'health_number'}) {
#now we have the key result sets for all the data for this patient
#split the data of birth up
($this_patient{'date_of_birth'}, $this_patient{'month_of_birth'}, $this_patient{'year_of_birth'}) = split(/[\ \-\/]/, $this_patient{'dob'});
if ($this_patient{'year_of_birth'} < 10) {
$this_patient{'year_of_birth'}="20$this_patient{'year_of_birth'}";
}
else {
$this_patient{'year_of_birth'}="19$this_patient{'year_of_birth'}";
}
#get the registration status
my $already_registered = (mysql_array_query("SELECT patient_status FROM demographic WHERE hin='$this_patient{'health_number'}' && hc_type='$province_code'"));
#only insert them into the system if they are not already registered
unless ($already_registered eq 'AC') {
#not registered as an active user
if ($already_registered) {
#this person is registered but not as active, so
#update any missing details on the system and update status to active
$mysql_where="hin='$this_patient{'health_number'}' && hc_type='$province_code'";
%mysql_update=(
'provider_no' => '1000',
'roster_status' => 'RO',
'patient_status' => 'AC',
'newsletter' => $imported_patient_tag
);
mysql_hash_update('demographic');
}
else {
#insert this new patient into the system
%mysql_set_insert=(
'last_name' => $this_patient{'last_name'},
'first_name' => $this_patient{'first_name'},
'address' => '',
'city' => '',
'province' => '',
'postal' => '',
'phone' => $this_patient{'home_phone'},
'phone2' => '',
'email' => '',
'pin' => '',
'year_of_birth' => $this_patient{'year_of_birth'},
'month_of_birth' => $this_patient{'month_of_birth'},
'date_of_birth' => $this_patient{'date_of_birth'},
'hin' => $this_patient{'health_number'},
'ver' => '',
'roster_status' => 'RO',
'patient_status' => 'AC',
'date_joined' => $sql_date,
'chart_no' => '',
'provider_no' => $current_provider,
'sex' => '',
'end_date' => '0001-01-01',
'hc_type' => $province_code,
'eff_date' => $sql_date,
'hc_renew_date' => $sql_date,
'family_doctor' => '',
'sin' => '',
'country_of_origin' => '-1',
'newsletter' => $imported_patient_tag
);
mysql_set_insert('demographic');
}
#reload and double check we now have patient data
my $check_patient = mysql_hash_query("SELECT * FROM demographic WHERE hin='$this_patient{'health_number'}' && hc_type='$province_code'");
unless ($check_patient) {
ErrorMessage("ERROR: PATIENT DATA NOT STORED PROPERLY");
}
#add an admission record linking this patient to the clinic provider
%mysql_set_insert=(
'client_id' => ${$check_patient}{'demographic_no'},
'program_id' => $program_id,
'provider_no' => $current_provider,
'admission_date' => $sql_date,
'admission_from_transfer' => '0',
'discharge_from_transfer' => '0',
'admission_status' => 'current',
'team_id' => '0',
'temporary_admission_flag' => '0',
'radioDischargeReason' => '0',
'clientstatus_id' => '0',
'automatic_discharge' => '0'
);
mysql_set_insert('admission');
}
}
last;
}
#disconnect from the database
mysql_disconnect();
print "\n\nGoodbye.\n\n";
exit;
###########################
#subroutines
###########################
sub casemgmt_note {
#pass this subroutine three variables
# 0. demographic_no of the current patient
# 1. the text to enter as this note
# 2. the type of note being entered:
# 34 = other meds
# 35 = social history
# 36 = medical history
# 37 = ongoing concerns
# 38 = reminders
# 15400 = family history
# 15401 = risk factors
my $casemgmt_demographic_no = $_[0];
my $casemgmt_note = $_[1];
my $casemgmt_type = $_[2];
#first insert the casemgmt_note itself
%mysql_set_insert=(
update_date => $sql_datetime,
observation_date => $sql_datetime,
demographic_no => $casemgmt_demographic_no,
provider_no => $current_provider,
note => $casemgmt_note,
signed => '1',
include_issue_innote=> '1',
signing_provider_no => $current_provider,
encounter_type => '',
billing_code => '',
program_no => '10015',
reporter_caisi_role => '1',
reporter_program_team=> '0',
history => "$casemgmt_note\n[Signed on $mday-$months[$mon]-$longyr $hr:$min by System, System]",
password => 'NULL',
locked => '0',
archived => '0',
uuid => 'MySQL:UUID()',
position => '0'
);
mysql_set_insert('casemgmt_note');
#now we need to find out what the note_id was for the entry we have just created
my $casemgmt_note_id = mysql_array_query("
SELECT note_id
FROM casemgmt_note
WHERE observation_date='$sql_datetime'
&& demographic_no='$casemgmt_demographic_no'
&& note='$casemgmt_note'
");
#we need to get a checksum for this entry to enter in the hash_audit table
my $casemgmt_md5 = system(" echo -n '$casemgmt_note' | md5sum");
$casemgmt_md5 = ~s/[^A-Z0-9]//gi;
#now make the hash_audit entry for this note
%mysql_set_insert=(
signature => $casemgmt_md5,
id => $casemgmt_note_id,
type => 'enc',
algorithm => 'MD5'
);
mysql_set_insert('hash_audit');
#before we can link this item, we have to work out if this demographic_no already has a casemgmt_issue_notes id number for this type of entry
my $casemgmt_issue_id = qw{};
until ($casemgmt_issue_id) {
$casemgmt_issue_id = mysql_array_query("
SELECT id
FROM casemgmt_issue
WHERE demographic_no='$casemgmt_demographic_no'
&& issue_id='$casemgmt_type'
");
unless ($casemgmt_issue_id) {
#not listed yet, so add a listing
%mysql_set_insert=(
demographic_no => $casemgmt_demographic_no,
issue_id => $casemgmt_type,
acute => '0',
certain => '0',
major => '0',
resolved => '0',
program_id => '10015',
type => 'nurse',
update_date => $sql_datetime
);
mysql_set_insert('casemgmt_issue');
}
}
#finally, make the note visible by making an entry in the casemgmt_issue_notes section
%mysql_set_insert=(
note_id => $casemgmt_note_id,
id => $casemgmt_issue_id
);
mysql_set_insert('casemgmt_issue_notes');
}
sub header {
system ("clear");
print "-------------------------------\n";
print "OSCAR ONLINE REGISTRATION SYSTEM\n";
print "-------------------------------\n";
print "\n" ;
}
sub get_time {
#get the date time
( $sec, $min, $hr, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
$longyr = $year + 1900;
$fixmo = $mon + 1;
if ($isdst == 1) {
my $tz = "CDT";
} else {
my $tz = "CST";
}
if ($hr<10) {
$hr="0".$hr;
}
if ($min<10) {
$min="0".$min;
}
my $sqlmo=$fixmo;
if ($sqlmo<10) {
$sqlmo="0".$sqlmo;
}
my $sqlmday=$mday;
if ($sqlmday<10) {
$sqlmday="0".$sqlmday;
}
$sql_date="$longyr-$fixmo-$mday";
$sql_time="$hr:$min:$sec";
$sql_datetime="$sql_date $sql_time";
$oscar_date="$days[$wday] $months[$mon] $mday $sql_time GMT $longyr";
}
sub mysql_log {
if ($_[0]) {
$sql_data=$_[0];
}
if ($pref{mysql_log}) {
unless ($sql_data) { $sql_data=$sql; }
$sql_data=~s/\s/ /g;
open (MYSQL,">>$pref{mysql_log}");
flock (MYSQL, 2);
print MYSQL "$longyr-$fixmo-$mday $hr:$min:$sec - $sql_data\n";
close (MYSQL);
}
return;
}
sub mysql_connect {
#set up login and password for this user
unless ($mysql_user) {
$mysql_user="read";
}
$mysql_login="mysql_".$mysql_user;
$mysql_pass="mysql_".$mysql_user."_pass";
$dbh_name="dbh_$mysql_user";
# Connect to the database
mysql_log("mysql_connect : mysql_user=$mysql_user, dbh_name=$dbh_name, mysql_login=$mysql_login, mysql_pass=$mysql_pass");
${$dbh_name} = DBI->connect("DBI:mysql:$pref{mysql_database}","$pref{$mysql_login}","$pref{$mysql_pass}") || ErrorMessage('Could not connect to database');
#record that this database is in use
$dbh_inuse="dbh_inuse_$mysql_user";
${$dbh_inuse}=1;
return;
}
sub mysql_disconnect {
# Disconnect the current user ($mysql_user) from the database
# but don't disconnect the read-only user
#get the passed user name if passed directly
if ($_[0]) {
$mysql_user=$_[0];
}
if (($mysql_user) && ($mysql_user ne "read")) {
#check that the user is connected first
$dbh_inuse="dbh_inuse_$mysql_user";
if (${$dbh_inuse}) {
#this user is connected
$dbh_name="dbh_$mysql_user";
mysql_log("mysql_disconnect : mysql_user=$mysql_user, dbh_name=$dbh_name, mysql_login=$mysql_login, mysql_pass=$mysql_pass");
${$dbh_name}->disconnect || mysql_log('ERROR could not disconnect database');
#record that this database is no longer in use
${$dbh_inuse}=0;
}
}
return;
}
sub mysql_check_connection {
#check that the preferred user is the one currently selected
if ($mysql_user eq $mysql_preferred_user) {
$mysql_original_user=$mysql_user;
} else {
$mysql_original_user=$mysql_user;
$mysql_user=$mysql_preferred_user;
}
$dbh_name="dbh_$mysql_user";
$dbh_inuse="dbh_inuse_$mysql_user";
$mysql_disconnect=0;
#check that the preferred user is connected
unless (${$dbh_inuse}) {
#this user is not connected
$mysql_disconnect=1;
mysql_connect();
}
return;
}
sub mysql_check_connection_finish {
#check if this user was only connected for this event
if ($mysql_disconnect) {
mysql_disconnect();
}
#reset the current user to the original user
$mysql_user=$mysql_original_user;
return $mysql_user;
}
sub mysql_simple {
#this subroutine is passed a full statement as $sql and sends it to the database as a 'read' user
#get passed sql string if present
if ($_[0]) {
$sql=$_[0];
}
$mysql_preferred_user="read";
mysql_submit_simple($sql);
return 1;
}
sub mysql_write_simple {
#this subroutine is passed a full statement as $sql and sends it to the database as a 'write' user
#get passed sql string if present
if ($_[0]) {
$sql=$_[0];
}
#print "\n\n**test mode** sub mysql_write_simple has had mysql_submit_simple($sql) commented out - your data has NOT been saved!\n\n$sql\n\n";
$mysql_preferred_user="write";
mysql_submit_simple($sql);
return 1;
}
sub mysql_submit_simple {
#this subroutine is passed a full statement as $sql and sends it to the database as the user $preferred_user
#get passed sql string if present
if ($_[0]) {
$sql=$_[0];
}
@mysql_row=();
mysql_check_connection();
mysql_log("mysql_submit_simple : $sql");
$mysql_query = ${$dbh_name}->do ($sql) || ErrorMessage('Could not submit data to the database');
mysql_check_connection_finish();
return 1;
}
sub mysql_array_query {
#get passed sql string if present
if ($_[0]) {
$sql=$_[0];
}
$mysql_preferred_user="read";
mysql_check_connection();
$mysql_query = ${$dbh_name}->prepare ($sql);
@mysql_result=();
if (defined($mysql_query)) {
mysql_log("mysql_array_query : $sql");
$mysql_query->execute() || ErrorMessage('Could not execute array query on database');
while (@mysql_row = $mysql_query->fetchrow_array()) {
@mysql_result=@mysql_row;
}
} else {
mysql_log("mysql_array_query (not found) : $sql");
ErrorMessage('Could not find query to submit to database');
}
$mysql_query->finish();
mysql_check_connection_finish();
#pass the first result back as a return value (or undefined, if no result)
if ($mysql_result[0]) {
return $mysql_result[0];
}
return;
}
sub mysql_hash_query {
#get passed sql string if present
if ($_[0]) {
$sql=$_[0];
}
$mysql_preferred_user="read";
mysql_check_connection();
$mysql_query = ${$dbh_name}->prepare ($sql);
@mysql_result=();
if (defined($mysql_query)) {
mysql_log("mysql_hash_query : $sql");
$mysql_query->execute() || ErrorMessage('Could not execute hash query on database');
@mysql_result=();
while ($mysql_record = $mysql_query->fetchrow_hashref()) {
push (@mysql_result, $mysql_record);
}
} else {
mysql_log("mysql_hash_query (not found) : $sql");
ErrorMessage('Could not find hash query to submit to database');
}
$mysql_query->finish();
mysql_check_connection_finish();
#return the mysql_result array (containing hash references), or undefined if no result
if ($mysql_result[0]) {
return $mysql_result[0];
}
return;
}
sub mysql_hash_update {
#this subroutine is passed:
# 1. the table name ($mysql_update_table)
# 2. a hash of elements to update (%mysql_update)
# 3. a where statement ($mysql_where) to select which entries to update
#and it then creates a mysql statement ($sql) and sends it to the database as a 'write' user
#get the passed table name if passed directly
if ($_[0]) {
$mysql_update_table=$_[0];
}
#get the passed table array name if passed directly
if ($_[1]) {
my $mysql_update_table_hash=$_[1];
%mysql_update=%{$mysql_update_table_hash};
}
#get the passed where string if passed directly
if ($_[2]) {
$mysql_where=$_[2];
}
$mysql_preferred_user="write";
mysql_check_connection();
#insert this line in the database
$error="mysql_update";
$sql="UPDATE $mysql_update_table SET ";
for my $this_sql_update (keys %mysql_update) {
$mysql_update{$this_sql_update}=~s/\'/\`/g;
$mysql_update{$this_sql_update}=~s/\&\#39\;/\ /g;
$sql.="$this_sql_update='$mysql_update{$this_sql_update}', ";
}
$sql=~s/, $/ WHERE $mysql_where/;
unless ($sql=~m/WHERE/) {
ErrorMessage('No where statement in update:$sql');
exit;
}
mysql_write_simple($sql);
mysql_check_connection_finish();
return;
}
sub mysql_set_insert {
#this subroutine is passed:
# 1. the table name ($mysql_update_table)
# 2. a hash of elements to include (%mysql_update)
#and it then creates a mysql statement ($sql) and sends it to the database as a 'write' user
#get the passed table name if passed directly
if ($_[0]) {
$mysql_insert_table=$_[0];
}
$mysql_preferred_user="write";
mysql_check_connection();
#insert this line in the database
$error="mysql_insert";
$sql="INSERT INTO $mysql_insert_table SET ";
for $this_sql_insert (keys %mysql_set_insert) {
$mysql_set_insert{$this_sql_insert}=~s/\'/\`/g;
$mysql_set_insert{$this_sql_insert}=~s/\&\#39\;/\ /g;
if ($mysql_set_insert{$this_sql_insert}=~m/^MySQL\:/) {
$mysql_set_insert{$this_sql_insert}=~s/^MySQL\://;
$sql.="$this_sql_insert=$mysql_set_insert{$this_sql_insert}, ";
}
else {
$sql.="$this_sql_insert='$mysql_set_insert{$this_sql_insert}', ";
}
}
$sql=~s/, $//;
mysql_log("mysql_set_insert : $sql");
mysql_write_simple($sql);
mysql_check_connection_finish();
return;
}
sub convert_phone_number {
#subroutine to internationalise and put spaces back in telephone numbers
(my $convert_country, my $convert_number) = @_;
if ($convert_number=~m/^\d{3}[\ \.\-]?\d{4}$/) {
$convert_number = $area_code.$convert_number;
}
#convert country code into country phone code
my $convert_int_code = "1";
$convert_number=~s/\D//g;
$convert_number=~s/^$convert_int_code//g;
my $convert1=$convert_number;
my $convert_zero = my $convert2 = my $convert3 = my $convert4 = my $convert5 = "";
#US and Canada phone number converter
$convert_zero="";
($convert1, $convert2, $convert3) = unpack ("A3 A3 A*", $convert_number);
my $converted_number="+$convert_int_code $convert_zero$convert1 $convert2 $convert3 $convert4 $convert5";
return $converted_number;
}
sub pad_for_tabulation {
#pads out the string
#pass the following variables:
#1. the string to be padded - including decimals, decimal places, and thousands separators
#2. the desired length
#3. the character to use for padding (usually ' ' or '0', defaults to '0')
#4. right of left justify ('R' or 'L')
#5. the number of decimal places to use, preceeded by T if thousands commas to be used (eg: '2', 'T2' or just'T' to just include thousands)
($string_to_be_padded, $desired_string_length, $padding_character, $right_or_left_justify, $decimal_places) = @_;
unless ($padding_character) {
$padding_character='0';
}
#add decimal places
if ($decimal_places) {
$decimals=q{};
if ($decimal_places =~m/\d/) {
$decimal_places_digit=$decimal_places;
$decimal_places_digit=~s/^T//;
($string_to_be_padded, $decimals) = split (/\./, $string_to_be_padded);
$decimals.="0"x$decimal_places_digit;
$decimals=substr ($decimals, 0, $decimal_places_digit);
$decimals=".$decimals";
}
#add thousands separators if required
if ($decimal_places =~m/T/) {
$string_to_be_padded=~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
}
$string_to_be_padded.=$decimals;
}
#next check if the string is too long, and truncate if required
if (length $string_to_be_padded > $desired_string_length) {
$string_to_be_padded=substr ($string_to_be_padded, 0, $desired_string_length);
}
$missing_digits = $desired_string_length - (length $string_to_be_padded);
$missing_digits="$padding_character"x$missing_digits;
if ($right_or_left_justify eq 'L') {
$string_to_be_padded=$string_to_be_padded.$missing_digits;
}
else {
$string_to_be_padded=$missing_digits.$string_to_be_padded;
}
return $string_to_be_padded;
}
sub rand_id {
my $rand_id_digits = $_[0];
unless ($rand_id_digits) {
$rand_id_digits = 5;
}
my $rand_id='';
while (length $rand_id < $rand_id_digits) {
my $ftemp=int(rand 9)+1;
$rand_id.=$ftemp;
}
return $rand_id;
}
sub sendmail {
if ($pref{'email_test_mode'}) {
$tempath=">>$pref{'email_log'}";
open (EMAIL, $tempath);
print EMAIL "$message\n\n\n";
close (EMAIL);
}
unless ($pref{'email_test_mode'} eq "test_only") {
$ENV{"PATH"} = "";
$pref{domain}=~s/[^\w\.]//g;
my $mail_connect="| /usr/sbin/sendmail -t";
if ($no_error) {
open(MAIL,"$mail_connect");
print MAIL $message;
} else {
$form_data{'message'}=$message;
$notify_only=1;
open(MAIL,"$mail_connect") || ErrorMessage('Unable to connect to send mail');
print MAIL $message || ErrorMessage('Unable to send mail');
}
close (MAIL);
}
return;
}
sub ErrorMessage {
#check if an error message was passed directly
if ($_[0]) {
$error=$_[0];
}
if ($_[1] eq 'email only') {
$email_only = 1;
}
if ($_[1] eq 'alternative') {
$email_only = 1;
}
if ($use_alternative_error_manager) {
print "\n\n*****Error: $error";
exit;
}
else {
unless ($cgi_title eq "error500.cgi") {
$error="$cgi_title: $error";
$error_code=200;
error_manager();
}
}
return;
}
sub error_manager {
#set $no_error to prevent an infinite loop of error messages
$no_error=1;
if ($pref{error_email}) {
######now send the email message about the error
$message="Subject: ERROR $error \n";
$message.="To: $pref{domain} <$pref{error_email}> \n";
$message.="Reply-to: $pref{domain} \n";
$message.="From: $pref{domain} \n";
$message.="Content-type: text/plain\n\n";
$message.="===========================================================";
$message.="\n";
$message.="\nERROR on $pref{domain}";
$message.="\n";
$message.="\n-----------------------------------------------------------";
$message.="\n";
$message.="\nERROR: $error";
$message.="\n";
$message.="\n-----------------------------------------------------------";
$message.="\n";
$message.="\n";
$message.="\n$pref{domain}";
$message.="\nweb site: $pref{http}";
#setting no_error to 1 prevents an error message infinite loop
sendmail();
}
unless ($notify_only) {
exit;
}
$notify_only=0;
$email_only=0;
return;
}