Michael Greb on 20 Nov 2007 01:12:48 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] mod_perl and dynamic HTML generation


Mag Gam wrote:
> ok, I too came across Mason.
> 
> Couple of questions for it...
> 1) How easy is it to configure?
> 2) Can I connect postgresql + mason together (I guess my question is, is it
> easy to use DBI)?
> 3) How similar is it to PHP?
> 
> I have been reading a lot about it, but just hesitant to try it..

Attatched is the source to the page at
<http://thegrebs.com/ip-info/index.html>.  Of note, the header/footer
gets automatically wrapped around the page with the title customized to
the page.  There are lines of pure perl code denoted by a % at the
beginning.  There are HTML lines with perl in them (wrapped with <% %>).
 You can also do blocks of perl code (<%perl IIRCC).

Output of perl code by default gets HTML escaped but the default
behavior can be changed, as well as changing per code block to URL
encoded or no encoding.  You can also have files that generate reused
elements, such as a search box or some such and include them throughout
the site via <& /path/to/file, optional_args &>.  I use this to generate
the links in the sidebar:

<li><a href="/docs/"<& /lib/active_link.mas, link=>'/docs'
&>>How-Tos</a></li>

<li><a href="/irc/"<& /lib/active_link.mas, link=>'/irc' &>>IRC
Logs</a></li>

/lib/active_link.mas is pretty simple:
<%args>
    $link
</%args>
 class="active"\
<%init>
    return unless ($r->uri =~ /^$link/);
</%init>

I could of course pass the link location and the Link name and have it
do the whole thing, in fact, I'm not sure why I didn't and will change
it in a moment ;).

As for configuration...  In my Apache config:
PerlModule Apache2::Request
PerlModule Apache2::Cookie
PerlModule Digest::SHA1
PerlModule Net::Abuse::Utils
PerlModule HTML::Mason::ApacheHandler
PerlModule DBI
PerlSetVar MasonAllowGlobals $Chans

<Directory /www/thegrebs>
  Options +ExecCGI
 <FilesMatch "(\.html|\.mason)$">
  SetHandler perl-script
  PerlHandler HTML::Mason::ApacheHandler
 </FilesMatch>

 <FilesMatch "(\.mas|handler)$">
  SetHandler perl-script
  PerlHandler "sub { return 404 } "
 </FilesMatch>
</Directory>

Of note, perl modules used get loaded once when apache loads, before it
forks, so they get shared.  In addition the .mas files I use as
components, that are only included in other pages and not meant to be
accessed directly, always return 404.

Just a quick overview feel free to ask about anything in particular you
are interested in.

Mike
<%args>
	$ip => undef
</%args>
<h3>&rsaquo;  IP Abuse Contact Info</h3>

<p>This page aims to aid you in determining the correct contact address(es) for a specified IP.  In
addition, information such as probable geographic location for the IP and if the IP is listed on 
several commonly used DNS blocking lists.</p>

% if ($error_msg) {
<p style="color: red;"><% $error_msg %></p>
% }

<form action="/ip-info/index.html" method="GET">
IP: <input type="TEXT" name="ip" value="<% $form_ip %>"><input type="submit" value="Look up">
</form>
<br /><br />
% if ($ip) {
%	$m->flush_buffer;
<pre>
IP Info:
	IP:         <% $ip %>
	RDNS:       <% Net::Abuse::Utils::get_rdns($ip) %>
	IP Country: <% Net::Abuse::Utils::get_ip_country($ip) %>
%	$m->flush_buffer;

AS Info:
% if (my @asn = Net::Abuse::Utils::get_asn_info($ip) ) {
	ASN:        <% "$asn[0] - $asn[1]" %>
	AS Org:     <% Net::Abuse::Utils::get_as_description($asn[0]) || ''  %>
	AS Country: <% Net::Abuse::Utils::get_asn_country($asn[0]) %>
% } else {
	Unknown ASN
%}
%	$m->flush_buffer;

DNSBL Listings:
% foreach my $bl (keys %dnsbl) {
%	my $txt = Net::Abuse::Utils::get_dnsbl_listing($ip, $dnsbl{$bl}) || 'not listed';
	<% $bl %>:	<% $txt %>
%	$m->flush_buffer;
%}

Contact Addresses:
%	my $ptr_soa = Net::Abuse::Utils::get_soa_contact($ip) || 'not found'; 
	PTR SOA Contact: <% $ptr_soa %>
%	$m->flush_buffer;
	IP Whois Contacts: <% join (' ', Net::Abuse::Utils::get_ipwi_contacts($ip) ) %>
%	$m->flush_buffer;
%	if ($ptr_soa =~ /\@(\S+)$/) {
	Abuse.net (<% $1 %>): <% Net::Abuse::Utils::get_abusenet_contact($1) %> 
%	$m->flush_buffer;
% }

- End of Results -
</pre>
% }
<%method title><& PARENT:title &>:IP Abuse Contact Info</%method>
<%init>
	my $error_msg;
	if ($ip && !Net::Abuse::Utils::is_ip($ip)) {
		$ip = undef;
		$error_msg = "The value supplied doesn't look like an IP.";
	}

	my $form_ip = $ip || $ENV{'REMOTE_ADDR'};

	my %dnsbl = (
#		'Spamhaus'      => 'sbl-xbl.spamhause.org',
		'SpamCop'       => 'bl.spamcop.net',
		'Relays VISI'   => 'relays.vsi.com',
		'Composite BL'  => 'cbl.abuseat.org',
		'Dynablock BL'  => 'dnsbl.njabl.org',
		'DSBL Proxy'    => 'list.dsbl.org',
		'DSBL Multihop' => 'multihop.dsbl.org',
		'SORBS OR'      => 'dnsbl.sorbs.net',
		'SPEWS L1'      => 'l1.spews.dnsbl.sorbs.net',
		'SPEWS L2'      => 'l2.spews.dnsbl.sorbs.net',
#		'Blitzed OPM'   => 'opm.blitzed.org',
		);
	
</%init>
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug