-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.pm
More file actions
180 lines (149 loc) · 3.57 KB
/
Utility.pm
File metadata and controls
180 lines (149 loc) · 3.57 KB
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
# -*- Mode: perl; indent-tabs-mode: nil -*-
# Copyright(c) 2003-2007 Robert L. Brown. This is licensed software
# only to be used with an explicit right-to-use license from the
# copyright holder.
##
## Utility routines
package Utility;
use 5.00;
use strict;
our($VERSION, @ISA, @EXPORT, @EXPORT_OK);
$VERSION = "1.00";
require Exporter;
@ISA=('Exporter');
@EXPORT = qw(
preHTMLAbort
);
@EXPORT_OK = qw();
use CGI qw(:standard *p);
use Data::Dumper;
require "globals.pl";
##
## true => enable display of errors in HTML
## false => enable display of errors in TEXT
##
sub setHTMLErrors
{
my $arg = shift;
$::TEXT_ERRORS = !$arg;
}
##
## for debugging, disables all insert and updates to the database
##
sub setSQLNoWrite
{
$::SQL_NOWRITE = shift;
}
##
## Formats and returns an error message in red (HTML only)
##
sub errorMessage
{
return p(font({-color=>"#ff0000"}, "ERROR: ", @_));
}
sub preHTMLAbort
{
print header;
print start_html;
print Utility::errorMessage(@_);
print p("Aborting.");
print end_html;
exit 0;
}
##
## Formats and prints an error and a traceback
##
sub redError
{
my $str = shift;
if ( $::TEXT_ERRORS ) {
print "$str at\n";
} else {
print start_p, b(font({-color=>"#ff0000"},"$str at ")), br;
}
for ( my $i=1; $i<6 ; $i++ ) {
my ($pack, $file, $line, $subname, $hasargs, $wantarray) = caller($i);
if ( defined $subname ) {
if ( $::TEXT_ERRORS ) {
print " " . $subname . "() called from $file:$line\n";
} else {
print font({-color=>"#ff0000"}," " . $subname . "() called from $file:$line",br);
}
}
}
if ( ! $::TEXT_ERRORS) {
print end_p;
}
}
##
## converts a text area to HTML-displayable text, escaping HTML characters
## and converting links
##
sub cvtTextarea
{
my $str = shift;
$str =~ s/&/&/g;
$str =~ s/</</g;
$str =~ s/>/>/g;
$str =~ s/"/"/g; # "
$str =~ s{((?:https?|ftp|telnet)://(?:[\w-]+\.)?[\w-]+\.\S+)}{<A HREF="$1">$1</A>}g;
$str =~ s/\n/<br>/sg;
return $str;
}
sub cvtTextline
{
my $str = shift;
$str =~ s/&/&/g;
$str =~ s/</</g;
$str =~ s/>/>/g;
$str =~ s/"/"/g; # "
$str =~ s{((?:https?|ftp|telnet)://(?:[\w-]+\.)?[\w-]+\.\S+)}{<A HREF="$1">$1</A>}g;
$str =~ s/\n/¶/sg;
return $str;
}
##
## for debugging, dumps a Perl object into HTML
##
sub ObjDump
{
print "<pre>", Dumper(@_), "</pre>";
}
##
## returns the current date & time in standard form (YYYY-MM-DD HH:MM:SS)
sub now
{
my ($seconds, $minutes, $hours, $day_of_month, $month, $year,
$wday, $yday, $isdst) = localtime(time);
return sprintf("%04d-%02d-%02d %02d:%02d:%02d",
1900+$year, $month+1, $day_of_month, $hours, $minutes, $seconds);
}
sub copyright
{
my $copyright = <<EOF;
Copyright © 2003-2006 Robert L. Brown. All Rights Reserved. Reproduction of this
document without explicit written permission is prohibited.
EOF
return $copyright;
}
##
## rootURL - generates (or guesses) the URL of the home page and
## returns it.
##
## e.g. returns / or /hiring or /dir1/dir1/hiring
##
sub rootURL
{
my $url;
if ( exists $::ENV{'REQUEST_URI'} ) {
($url = $::ENV{'REQUEST_URI'}) =~ s(/[^/]*$)();
} elsif ( exists $::ENV{'SCRIPT_NAME'} ) {
($url = $::ENV{'SCRIPT_NAME'}) =~ s(/[^/]*$)();
} else {
$url = "";
}
if ( length($url) == 0 ) {
$url = "/";
}
return $url;
}
1;