default to a session cookie instead of setting an explicit timeout, weird timezone...
[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 qw( cgi );
10
11 @ISA = qw(Exporter);
12 @EXPORT_OK = qw( header menubar idiot eidiot popurl rooturl table itable ntable
13                  myexit http_header);
14
15 =head1 NAME
16
17 FS::CGI - Subroutines for the web interface
18
19 =head1 SYNOPSIS
20
21   use FS::CGI qw(header menubar idiot eidiot popurl);
22
23   print header( 'Title', '' );
24   print header( 'Title', menubar('item', 'URL', ... ) );
25
26   idiot "error message"; 
27   eidiot "error message";
28
29   $url = popurl; #returns current url
30   $url = popurl(3); #three levels up
31
32 =head1 DESCRIPTION
33
34 Provides a few common subroutines for the web interface.
35
36 =head1 SUBROUTINES
37
38 =over 4
39
40 =item header TITLE, MENUBAR
41
42 Returns an HTML header.
43
44 =cut
45
46 sub header {
47   use Carp;
48   carp 'FS::CGI::header deprecated; include /elements/header.html instead';
49
50   my($title,$menubar,$etc)=@_; #$etc is for things like onLoad= etc.
51   $etc = '' unless defined $etc;
52
53   my $x =  <<END;
54     <HTML>
55       <HEAD>
56         <TITLE>
57           $title
58         </TITLE>
59         <META HTTP-Equiv="Cache-Control" Content="no-cache">
60         <META HTTP-Equiv="Pragma" Content="no-cache">
61         <META HTTP-Equiv="Expires" Content="0"> 
62       </HEAD>
63       <BODY BGCOLOR="#e8e8e8"$etc>
64           <FONT SIZE=6>
65             <CENTER>$title</CENTER>
66           </FONT>
67           <BR><!--<BR>-->
68 END
69   $x .=  $menubar. "<BR><BR>" if $menubar;
70   $x;
71 }
72
73 =item http_header
74
75 Sets an http header.
76
77 =cut
78
79 sub http_header {
80   my ( $header, $value ) = @_;
81   if ( defined $HTML::Mason::Commands::r  ) { #Mason + apache
82     if ( $header =~ /^Content-Type$/ ) {
83       $HTML::Mason::Commands::r->content_type($value);
84     } else {
85       $HTML::Mason::Commands::r->header_out( $header => $value );
86     }
87   } elsif ( defined $HTML::Mason::Commands::m ) {
88     $HTML::Mason::Commands::m->notes(lc("header-$header"), $value);
89   } else {
90     warn "http_header($header, $value) called with no way to set headers\n";
91   }
92 }
93
94 =item menubar ITEM, URL, ...
95
96 Returns an HTML menubar.
97
98 =cut
99
100 sub menubar { #$menubar=menubar('Main Menu', '../', 'Item', 'url', ... );
101   use Carp;
102   carp 'FS::CGI::menubar deprecated; include /elements/menubar.html instead';
103
104   my($item,$url,@html);
105   while (@_) {
106     ($item,$url)=splice(@_,0,2);
107     next if $item =~ /^\s*Main\s+Menu\s*$/i;
108     push @html, qq!<A HREF="$url">$item</A>!;
109   }
110   join(' | ',@html);
111 }
112
113 =item idiot ERROR
114
115 This is depriciated.  Don't use it.
116
117 Sends an HTML error message.
118
119 =cut
120
121 sub idiot {
122   #warn "idiot depriciated";
123   my($error)=@_;
124 #  my $cgi = &FS::UID::cgi();
125 #  if ( $cgi->isa('CGI::Base') ) {
126 #    no strict 'subs';
127 #    &CGI::Base::SendHeaders;
128 #  } else {
129 #    print $cgi->header( @FS::CGI::header );
130 #  }
131   print <<END;
132 <HTML>
133   <HEAD>
134     <TITLE>Error processing your request</TITLE>
135     <META HTTP-Equiv="Cache-Control" Content="no-cache">
136     <META HTTP-Equiv="Pragma" Content="no-cache">
137     <META HTTP-Equiv="Expires" Content="0"> 
138   </HEAD>
139   <BODY>
140     <CENTER>
141     <H4>Error processing your request</H4>
142     </CENTER>
143     Your request could not be processed because of the following error:
144     <P><B>$error</B>
145   </BODY>
146 </HTML>
147 END
148
149 }
150
151 =item eidiot ERROR
152
153 This is depriciated.  Don't use it.
154
155 Sends an HTML error message, then exits.
156
157 =cut
158
159 sub eidiot {
160   warn "eidiot depriciated";
161   $HTML::Mason::Commands::r->send_http_header
162     if defined $HTML::Mason::Commands::r;
163   idiot(@_);
164   &myexit();
165 }
166
167 =item myexit
168
169 You probably shouldn't use this; but if you must:
170
171 If running under mod_perl, calles Apache::exit, otherwise, calls exit.
172
173 =cut
174
175 sub myexit {
176   if (exists $ENV{MOD_PERL}) {
177
178     if ( defined $HTML::Mason::Commands::m  ) { #Mason
179       #$HTML::Mason::Commands::m->flush_buffer();
180       $HTML::Mason::Commands::m->abort();
181       die "shouldn't fall through to here (mason \$m->abort didn't)";
182     } else {
183       #??? well, it is $ENV{MOD_PERL}
184       warn "running under unknown mod_perl environment; trying Apache::exit()";
185       require Apache;
186       Apache::exit();
187     }
188   } else {
189     exit;
190   }
191 }
192
193 =item popurl LEVEL [URL]
194
195 Returns current (or, optionally, passed) URL with LEVEL levels of path removed
196 from the end (default 0).
197
198 =cut
199
200 sub popurl {
201   my $up = shift;
202
203   my $url_string;
204   if ( scalar(@_) ) {
205     $url_string = shift;
206   } else {
207     my $cgi = &FS::UID::cgi;
208     $url_string = $cgi->isa('Apache') ? $cgi->uri : $cgi->url;
209   }
210
211   $url_string =~ s/\?.*//;
212   my $url = new URI::URL ( $url_string );
213   my(@path)=$url->path_components;
214   splice @path, 0-$up;
215   $url->path_components(@path);
216   my $x = $url->as_string;
217   $x .= '/' unless $x =~ /\/$/;
218   $x;
219 }
220
221 =item rooturl 
222
223 =cut
224
225 sub rooturl {
226   my $url_string;
227   if ( scalar(@_) ) {
228     $url_string = shift;
229   } else {
230     # better to start with the client-provided URL
231     my $cgi = cgi;
232     $url_string = $cgi->isa('Apache') ? $cgi->uri : $cgi->url;
233   }
234
235   $url_string =~ s/\?.*//;
236
237   #even though this is kludgy
238   $url_string =~ s{ / index\.html /? $ }
239                   {/}x;
240   $url_string =~
241     s{
242        /
243        (browse|config|docs|edit|graph|misc|search|view|loginout|pref|rt|torrus)
244        (/process)?
245        ([\w\-\.\/]*)
246        $
247      }
248      {}x;
249
250   $url_string .= '/' unless $url_string =~ /\/$/;
251
252   $url_string;
253
254 }
255
256 =item table
257
258 Returns HTML tag for beginning a table.
259
260 =cut
261
262 sub table {
263   use Carp;
264   carp 'FS::CGI::table deprecated; include /elements/table.html instead';
265
266   my $col = shift;
267   if ( $col ) {
268     qq!<TABLE BGCOLOR="$col" BORDER=1 WIDTH="100%" CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">!;
269   } else { 
270     '<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">';
271   }
272 }
273
274 =item itable
275
276 Returns HTML tag for beginning an (invisible) table.
277
278 =cut
279
280 sub itable {
281   my $col = shift;
282   my $cellspacing = shift || 0;
283   my $width = ( scalar(@_) && shift ) ? '' : 'WIDTH="100%"';  #bah
284   if ( $col ) {
285     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing $width>!;
286   } else {
287     qq!<TABLE BORDER=0 CELLSPACING=$cellspacing $width>!;
288   }
289 }
290
291 =item ntable
292
293 This is getting silly.
294
295 =cut
296
297 sub ntable {
298   my $col = shift;
299   my $cellspacing = shift || 0;
300   if ( $col ) {
301     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing>!;
302   } else {
303     '<TABLE BORDER CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">';
304   }
305
306 }
307
308 =back
309
310 =head1 BUGS
311
312 Not OO.
313
314 Not complete.
315
316 =head1 SEE ALSO
317
318 L<CGI>, L<CGI::Base>
319
320 =cut
321
322 1;
323
324