<%perl_init>
#
# Main previewer interface. See admin manual for details on setting up previewer.
#

my ($previewDir,$previewServer,@sites,@versions,@sitePorts,@previewPorts);

######################################################################
#
# ADMIN CONFIGURATION

#
# Where preview files are stored. Replace <data_dir> with this server's data directory
# (should match data_dir parameter in handler.pl).
#
$previewDir = "<data_dir>/preview";

#
# Preview ports.  Each port represents a separate configuration that
# a user can store. You must listen to these ports in your httpd.conf.
#
@previewPorts = qw(3001 3002 3003 3004 3005);

#
# Domain name of server where preview ports are answered. If same as this server,
# leave this line alone.
#
$previewServer = $r->server->server_hostname;

#
# Ports that users commonly visit on your site.  For a standard site
# generally visited on port 80, leave this line alone.
#
@sitePorts = ('80');


# END CONFIGURATION
#
####################################################################### 

my %versionsMap;
foreach (@versions) { $versionsMap{$_->{name}} = $_ }


</%perl_init>

%if ($mode eq 'home') {
<html>
<head>
<title>Mason Previewer</title>
</head>
<body bgcolor="fffff0">
<form action="<%$r->uri%>" method=post>
<input type=hidden name=mode value=save>

<table border=0 cellpadding=1 cellspacing=1>
<tr>
<td colspan=5 bgcolor="003399">&nbsp;<font size="4" color="FFFFF0"><b>Mason Previewer</b></font></td>
</tr>

<tr><td>&nbsp;</td></tr>

<tr>
<td>
%if (@versions) {
<tt><font color="#FF0000">Version</font></tt>
<br>
<%$query->popup_menu(name=>'b_version','values'=>[keys(%versionsMap)],default=>$conf{b_version})%>
</td>
%}

<td>
<tt><font color="#FF0000">Port</font></tt>
<br>
<%$query->popup_menu(name=>'b_port','values'=>[@sitePorts,'Other...'],default=>$conf{b_port})%>
</td>

<td>
<tt><font color="#FF0000">Time</font></tt><br>
<%$query->popup_menu(name=>'b_time','values'=>['Real','+1 hour','+4 hours','+1 day','+2 days','+3 days','+4 days','+5 days','Other...'],default=>$conf{b_time})%>
</td>
</tr>

<tr><td>&nbsp;</td></tr>

<tr>
<td>
<tt><font color="#FF0000">Display</font></tt><br>
<%$query->popup_menu(name=>'outputtype','values'=>['HTML','Debug'],default=>$conf{outputtype})%>
</td>

<td>
<tt><font color="#FF0000">Preview port</font></tt><br>
<%$query->popup_menu(name=>'previewPort','values'=>\@previewPorts,default=>$previewPort)%>
</td>
<td>
<input type=submit name=action value="Save settings">
</td>
</tr>
</table>

<p><br>

<tt><font color=#ff0000>View:</font></tt>
&nbsp;
<input type=submit name=action value="This window">
&nbsp;
<input type=submit name=action value="Window A">
&nbsp;
<input type=submit name=action value="Window B">
<br>
<tt><font color=#ff0000>Initial URI:</font></tt>
<input name=initialUri type=text value="/" size=20>

<br>
<hr>

<h2>Saved Port Settings</h2>

%if (@settings) {
<table border=1>
<tr>
<th>&nbsp;</th>
%if (@sites) {
<th>Site</th>
%}
<th>Port</th>
%if (@versions) {
<th>Version</th>
%}
<th>Time</th>
<th>Display</th>
</tr>
%foreach $setting (@settings) {
<tr>
<td>
<a href="<%$r->uri%>?previewPort=<%$setting->{previewPort}%>">
<%$setting->{previewPort}%>
</a>
</td>
%if (@sites) {
<td>
<%$setting->{b_site}%>
</td>
%}
<td>
<%$setting->{request}->{env}->{SERVER_PORT}%>
</td>
%if (@versions) {
<td>
<%$setting->{b_version}%>
</td>
%}
<td>
<%$setting->{timestr}%>
</td>
<td>
<%$setting->{outputtype}%>
</td>
</tr>
%}
</table>
%} else {
(none)<br>
%}

<hr>

<h2>Advanced Options</h2>

%my $spacer = '&nbsp;'x10;

To activate these options, select "Other..." in the corresponding menu above.<p>

%if (@sites) {
<tt><font color="#FF0000">Site</font></tt><br>
<%$spacer%>Host name: &nbsp;<input type=text name=a_site_server_name size=20 value=<%$conf{a_site_server_name}%>>&nbsp;&nbsp;
Component prefix: &nbsp;<input type=text name=a_site_prefix size=20 value=<%$conf{a_site_prefix}%>> 
<p>
%}

<tt><font color="#FF0000">Port</font></tt><br>
<%$spacer%>Port number: &nbsp;<input type=text name=a_port size=7 value=<%$conf{a_port}%>>
<p>

<tt><font color="#FF0000">Time</font></tt><br>
<%$spacer%><input type=radio name=a_timetype value=relative <%$conf{a_timetype} eq 'relative' ? 'checked' : ''%>>
Relative: <input type=text name=a_reltimenum size=3 value="<%$conf{a_reltimenum}%>"> <select name=a_reltimeunit>
<option value=h <%$conf{a_reltimeunit} eq 'h' ? 'selected' : ''%>>hour(s)
<option value=d <%$conf{a_reltimeunit} eq 'd' ? 'selected' : ''%>>day(s)
</select>
<select name=a_reltimedirection>
<option value=future <%$conf{a_reltimedirection} eq 'future' ? 'selected' : ''%>>in the future
<option value=past <%$conf{a_reltimedirection} eq 'past' ? 'selected' : ''%>>in the past
</select><br>
<%$spacer%><input type=radio name=a_timetype value=absolute <%$conf{a_timetype} eq 'absolute' ? 'checked' : ''%>>
Absolute:
<%$query->popup_menu(name=>'a_abstimemonth','values'=>[1..12],labels=>{1=>'Jan',2=>'Feb',3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec'},default=>$conf{a_abstimemonth})%>
<%$query->popup_menu(name=>'a_abstimeday','values'=>[1..31],default=>$conf{a_abstimeday})%>
<%$query->popup_menu(name=>'a_abstimeyear','values'=>[1997..2010],default=>$conf{a_abstimeyear})%>
at <SELECT NAME="a_abstimehour">
%for ($i=1; $i<=12; $i++) {
<option value="<%$i%>"
%if ($i == $conf{a_abstimehour}) {
selected
%}
><%$i%>:00
%}
</select> <input type="radio" name="a_abstimeampm" value="am" <%$conf{a_abstimeampm} eq 'am' ? 'checked' : ''%>>a.m. <input type="radio" name="a_abstimeampm" value="pm" <%$conf{a_abstimeampm} eq 'pm' ? 'checked' : ''%>>p.m.
<p>

To activate these options, check the boxes.<p>

<input type="checkbox" name=af_data_cache value=1 <%$conf{af_data_cache} ? 'checked' : ''%>>&nbsp;<tt><font color="#FF0000">Caching</font></tt>
<p>

<input type="checkbox" name=af_useragent value=1 <%$conf{af_useragent} ? 'checked' : ''%>>&nbsp;<tt><font color="#FF0000">User agent</font></tt><br>
<%$spacer%>OS: <%$query->popup_menu(name=>'a_os','values'=>[qw(win95 win31 winnt mac unix)],labels=>{win95=>'Windows 95',win31=>'Windows 3.1',winnt=>'Windows NT',mac=>'Macintosh',unix=>'Unix'},default=>$conf{a_os})%>
&nbsp;Browser: <%$query->popup_menu(name=>'a_browser','values'=>[qw(netscape msie aol robot)],labels=>{netscape=>'Netscape',msie=>'Internet Explorer',aol=>'AOL',robot=>'Robot'},default=>$conf{a_browser})%>
<%$query->popup_menu(name=>'a_browserVersion','values'=>[qw(2.0 3.0 4.0)],default=>$conf{a_browserVersion})%>    
<p>

<input type="checkbox" name=af_referer value=1 <%$conf{af_referer} ? 'checked' : ''%>>&nbsp;<tt><font color="#FF0000">Referer</font></tt><br>
<%$spacer%>Url: <input type="text" name="a_referer" value="<%$conf{a_referer}%>" size=25>
<p>

</form>

</body>
</html>
%}

%if ($mode eq 'save') {
%if ($action =~ /^Window ./) {
%my ($windowLetter) = ($action =~ /Window (.)/);
<script language=JavaScript>
<!--
viewWindow = window.open("<%$viewUrl%>","MasonPreviewView<%$windowLetter%>")
self.history.back()
//-->
</script>
mc_abort();
%}
<%mc_comp($thisComp,previewPort=>$previewPort)%>
%}
<%perl_init>
my ($i,%conf,$href,$query,$viewUrl,$setting,@settings);
my $userName = $r->connection->user || 'anonymous';
my $thisComp = (mc_call_stack)[0];
$previewPort = $previewPorts[0] if !defined($previewPort);
if ($mode eq 'home') {
    $query = new CGI('');
    $query->use_named_parameters(1);
    my $in = HTML::Mason::Preview::open_preview_settings($previewDir,$userName,0);
    foreach my $p (@previewPorts) {
	if (exists($in->{$p})) {
	    my %c = %{$in->{$p}};
	    %conf = %c if $previewPort eq $p;
	    $c{previewPort} = $p;
	    my $timeconf = $c{request}->{'time'};
	    for ($timeconf->{type}) {
		$c{timestr} =
		    (/real/) ? 'real' :
			(/relative/) ? $timeconf->{delta} :
			    (/absolute/) ? sprintf("%s %d, %d %d:00 %s",$c{a_abstimemonth},$c{a_abstimeday},$c{a_abstimeyear},$c{a_abstimehour},$c{a_abstimeampm}) :
				'';
	    }
	    push(@settings,{%c});
	}
    }
    untie(%$in);
    if (!%conf) {
	%conf =
	    (
	     b_site => (@sites ? $sites[0]->{name} : undef),
	     b_port => $previewPorts[0],
	     b_version => (@versions ? $versions[0]->{name} : undef),
	     b_time => 'Real',
	     outputtype => 'HTML',
	     a_port => '',
	     a_hostname => '',
	     a_fileroot => '',
	     a_timetype => 'relative',
	     a_reltimenum => '',
	     a_reltimeunit => 'h',
	     a_reltimedirection => 'future'
	     );
	($conf{a_abstimemonth},$conf{a_abstimeday},$conf{a_abstimeyear},$conf{a_abstimehour},$conf{a_abstimeampm}) = split("/",UnixDate('now',"%b/%d/%Y/%i/%p"));
	$conf{a_abstimeampm} = lc($conf{a_abstimeampm});
	#($conf{a_os},$conf{a_browser},$conf{a_browserVersion}) = ;
    }
}
if ($mode eq 'save') {
    my %options = %ARGS;
    foreach (qw(previewPort initialUri action)) {
	delete($options{$_});
    }
    my (%request);

    # Port
    my $port;
    if ($options{b_port} !~ /^Other/) {
	($port) = ($options{b_port} =~ /([0-9]+)/);
    } elsif ($options{a_port} =~ /^([0-9]+)$/) {
	$port = $options{a_port};
    } else {
	$port = 80;
    }
    $request{env}->{SERVER_PORT} = $port;

    # Version
    if (@versions) {
	my $v = $versionsMap{$options{b_version}};
	foreach my $field (qw(comp_root data_dir static_file_root)) {
	    $request{interp}->{$field} = $v->{$field} if defined($v->{$field});
	}
	$request{alternate_source} = $v->{alternate_source};
    }

    # Site
    if (@sites) {
	my $siteName = $options{b_site};
	my ($serverName,$topPrefix);
	if ($siteName !~ /^Other/) {
	    $serverName = $sitesMap{$siteName}->{server_name};
	    $topPrefix = $sitesMap{$siteName}->{prefix};
	} else {
	    $serverName = $options{a_site_server_name};
	    $topPrefix = $options{a_site_prefix};
	}
	$request{top_prefix} = $topPrefix;
	$request{env}->{SERVER_NAME} = $serverName;
	my $host = $serverName;
	$host .= ":$port" if $port ne '80';
	$request{headers_in}->{Host} = $host;
    }

    # Time
    # - time->type (real, relative, or absolute)
    # - time->value (if absolute)
    # - time->delta (if relative)
    if ($options{b_time} eq 'Real') {
	$request{'time'}->{type} = 'real';
    } elsif ($options{b_time} =~ /^\+/) {
	$request{'time'}->{type} = 'relative';
	$request{'time'}->{delta} = $options{b_time};
    } elsif ($options{a_timetype} eq 'absolute') {
	$request{'time'}->{type} = 'absolute';
	$request{'time'}->{value} = ParseDate(sprintf("%s %d, %d %d:00 %s",$options{a_abstimemonth},$options{a_abstimeday},$options{a_abstimeyear},$options{a_abstimehour},$options{a_abstimeampm}));
    } elsif ($options{a_timetype} eq 'relative') {
	$request{'time'}->{type} = 'relative';
	$request{'time'}->{delta} = sprintf("%s%d%s",($options{a_reltimedirection} eq 'future' ? '+' : '-'),$options{a_reltimenum},$options{a_reltimeunit});
    } else {
	die "preview save: cannot determine time value\n";
    }

    # Data caching
    $request{interp}->{use_data_cache} = ($options{af_data_cache} ? 1 : 0);

    # User agent
    if ($options{af_useragent}) {
	my $osStr = {win95=>'Win95',winnt=>'WinNT',mac=>'Macintosh',unix=>'SunOS 5.5 sun4c'}->{$options{a_os}};
	my $v = $options{a_browserVersion};
	my $browserStr = {netscape=>'',msie=>"compatible; MSIE $v; ",aol=>"compatible; MSIE $v; AOL $v; ",robot=>''}->{$options{a_browser}};
	my $robotStr = ($options{a_browser} eq 'robot') ? " via Downtown/1.0" : "";
	$request{headers_in}->{'User-Agent'} = sprintf("Mozilla/$v (%s%s)%s",$browserStr,$osStr,$robotStr);
    }

    # Referer
    if ($options{af_referer}) {
	$request{headers_in}->{Referer} = $options{a_referer};
    }

    # Output type
    $request{output_type} = $options{outputtype};

    #
    # Dump configuration to DB file
    #
    $options{request} = {%request};
    my $out = HTML::Mason::Preview::open_preview_settings($previewDir,$userName,1);
    $out->{$previewPort} = \%options;
    untie (%$out);

    #
    # View in this or other window
    #
    if ($action =~ /window/i) {
	$initialUri = '' if $initialUri eq '/';
	$viewUrl = "http://${previewServer}:${previewPort}${initialUri}";
	if ($action eq 'This window') {
	    $r->send_cgi_header("Location: $viewUrl\n\n");
	}
    }
}
</%perl_init>
<%perl_args>
$previewPort=>undef
$mode=>'home'
$action=>undef
$initialUri=>undef
</%perl_args>
