CGI.pm detects mod_perl and calls appropriate exit (Registry's override doesn't
[freeside.git] / FS / FS / CGI.pm
1 package FS::CGI;
2
3 use strict;
4 use vars qw(@EXPORT_OK @ISA);
5 use Exporter;
6 use CGI;
7 use URI::URL;
8 use CGI::Carp qw(fatalsToBrowser);
9 use FS::UID;
10
11 @ISA = qw(Exporter);
12 @EXPORT_OK = qw(header menubar idiot eidiot popurl table itable ntable);
13
14 =head1 NAME
15
16 FS::CGI - Subroutines for the web interface
17
18 =head1 SYNOPSIS
19
20   use FS::CGI qw(header menubar idiot eidiot popurl);
21
22   print header( 'Title', '' );
23   print header( 'Title', menubar('item', 'URL', ... ) );
24
25   idiot "error message"; 
26   eidiot "error message";
27
28   $url = popurl; #returns current url
29   $url = popurl(3); #three levels up
30
31 =head1 DESCRIPTION
32
33 Provides a few common subroutines for the web interface.
34
35 =head1 SUBROUTINES
36
37 =over 4
38
39 =item header TITLE, MENUBAR
40
41 Returns an HTML header.
42
43 =cut
44
45 sub header {
46   my($title,$menubar)=@_;
47
48   my $x =  <<END;
49     <HTML>
50       <HEAD>
51         <TITLE>
52           $title
53         </TITLE>
54       </HEAD>
55       <BODY BGCOLOR="#e8e8e8">
56           <FONT SIZE=7>
57             $title
58           </FONT>
59           <BR><BR>
60 END
61   $x .=  $menubar. "<BR><BR>" if $menubar;
62   $x;
63 }
64
65 =item menubar ITEM, URL, ...
66
67 Returns an HTML menubar.
68
69 =cut
70
71 sub menubar { #$menubar=menubar('Main Menu', '../', 'Item', 'url', ... );
72   my($item,$url,@html);
73   while (@_) {
74     ($item,$url)=splice(@_,0,2);
75     push @html, qq!<A HREF="$url">$item</A>!;
76   }
77   join(' | ',@html);
78 }
79
80 =item idiot ERROR
81
82 This is depriciated.  Don't use it.
83
84 Sends headers and an HTML error message.
85
86 =cut
87
88 sub idiot {
89   #warn "idiot depriciated";
90   my($error)=@_;
91   my $cgi = &FS::UID::cgi();
92   if ( $cgi->isa('CGI::Base') ) {
93     no strict 'subs';
94     &CGI::Base::SendHeaders;
95   } else {
96     print $cgi->header( '-expires' => 'now' );
97   }
98   print <<END;
99 <HTML>
100   <HEAD>
101     <TITLE>Error processing your request</TITLE>
102   </HEAD>
103   <BODY>
104     <CENTER>
105     <H4>Error processing your request</H4>
106     </CENTER>
107     Your request could not be processed because of the following error:
108     <P><B>$error</B>
109   </BODY>
110 </HTML>
111 END
112
113 }
114
115 =item eidiot ERROR
116
117 This is depriciated.  Don't use it.
118
119 Sends headers and an HTML error message, then exits.
120
121 =cut
122
123 sub eidiot {
124   #warn "eidiot depriciated";
125   idiot(@_);
126   if (exists $ENV{MOD_PERL}) {
127     eval {
128       use Apache;
129       Apache::exit();
130     };
131   } else {
132     exit;
133   }
134 }
135
136 =item popurl LEVEL
137
138 Returns current URL with LEVEL levels of path removed from the end (default 0).
139
140 =cut
141
142 sub popurl {
143   my($up)=@_;
144   my($cgi)=&FS::UID::cgi;
145   my($url)=new URI::URL $cgi->url;
146   my(@path)=$url->path_components;
147   splice @path, 0-$up;
148   $url->path_components(@path);
149   my $x = $url->as_string;
150   $x .= '/' unless $x =~ /\/$/;
151   $x;
152 }
153
154 =item table
155
156 Returns HTML tag for beginning a table.
157
158 =cut
159
160 sub table {
161   my $col = shift;
162   if ( $col ) {
163     qq!<TABLE BGCOLOR="$col" BORDER=1 WIDTH="100%">!;
164   } else { 
165     "<TABLE BORDER=1>";
166   }
167 }
168
169 =item itable
170
171 Returns HTML tag for beginning an (invisible) table.
172
173 =cut
174
175 sub itable {
176   my $col = shift;
177   my $cellspacing = shift || 0;
178   if ( $col ) {
179     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing WIDTH="100%">!;
180   } else {
181     qq!<TABLE BORDER=0 CELLSPACING=$cellspacing WIDTH="100%">!;
182   }
183 }
184
185 =item ntable
186
187 This is getting silly.
188
189 =cut
190
191 sub ntable {
192   my $col = shift;
193   my $cellspacing = shift || 0;
194   if ( $col ) {
195     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing>!;
196   } else {
197     "<TABLE BORDER>";
198   }
199
200 }
201
202 =back
203
204 =head1 BUGS
205
206 Not OO.
207
208 Not complete.
209
210 =head1 SEE ALSO
211
212 L<CGI>, L<CGI::Base>
213
214 =cut
215
216 1;
217
218