#!/usr/bin/perl -w # Very simple Wiki to demonstrate use of CGI::Wiki. use strict; use warnings; use CGI qw/:standard/; use CGI::Wiki; use CGI::Wiki::Store::MySQL; use CGI::Wiki::Search::DBIxFTS; use CGI::Wiki::Formatter::Default; use CGI::Wiki::Formatter::UseMod; use Template; # Alex use Error qw(:try); use Data::Dumper; # Initialise my %macros = ( qr/\@RECENTCHANGES(\b|$)/ => qq(Recent Changes), qr/\@SEARCHBOX(\b|$)/ => qq(
) ); my $store = CGI::Wiki::Store::MySQL->new( dbname => "gopherit", dbuser => "gopherit", dbpass => "fiawol" ); my $storeOLD = CGI::Wiki::Store::MySQL->new( dbname => "foyleswiki", dbuser => "techdept", dbpass => "techdept" ); my $dbh = $store->dbh; my $search = CGI::Wiki::Search::DBIxFTS->new( dbh => $dbh ); my $defaultformatter = CGI::Wiki::Formatter::Default->new( # extended_links => 0, # implicit_links => 1, # force_ucfirst_nodes => 1, # $FreeUpper # use_headings => 1, # $UseHeadings allowed_tags => [qw(a p b strong i em pre small img table td tr th br hr ul li center blockquote kbd div code strike)], macros => \%macros, node_prefix => 'wiki.cgi?', edit_prefix => 'wiki.cgi?action=edit&id=' ); my $formatter = CGI::Wiki::Formatter::UseMod->new( extended_links => 1, implicit_links => 1, force_ucfirst_nodes => 1, # $FreeUpper use_headings => 1, # $UseHeadings allowed_tags => [qw(a p b strong i em pre small img table td tr th br hr ul li center blockquote kbd div code strike)], macros => \%macros, node_prefix => 'wiki.cgi?', edit_prefix => 'wiki.cgi?action=edit&id=' ); my ($wiki, $q); eval { $wiki = CGI::Wiki->new( store => $store, search => $search, formatter => $formatter ); # Get CGI object, find out what to do. $q = CGI->new; my $node = $q->param('node') || $q->param('id') || ""; my $action = $q->param('action') || 'display'; my $commit = $q->param('commit') || 0; my $preview = $q->param('preview') || 0; my $days = $q->param('days') || 7; if ($commit) { commit_node($node); # ALEX removed this for security reasons } elsif ($preview) { preview_node($node); # ALEX REMOVED THIS } elsif ($action eq 'edit') { edit_node($node); # ALEX REMOVED THIS } elsif ($action eq 'search') { do_search($q->param('terms')); } elsif ($action eq 'index') { my @nodes = $wiki->list_all_nodes(); process_template("site_index.tt", "index", { nodes => \@nodes }); } elsif ($action eq 'history') { # old versions of nodes not implemented yet. } else { if( !$node ) { # maybe we were passed wiki.cgi?nodename # This is a CGI.pm special case $node = $q->param('keywords'); } else { # print STDERR "display_node $node : " ; } display_node($node, $days); } }; if ($@) { my $error = $@; warn $error; print CGI::header; print qq(ERROR

Sorry! Something went wrong. Please contact the Wiki administrator at alexmc\@yahoo.com and quote the following error message:

) . CGI::escapeHTML($error) . qq(

Return to the Wiki home page ); } exit 0; ############################ subroutines ################################### sub display_node { my $node = shift; $node ||= "Home"; my $days = shift || '7'; my $raw = ''; try { $raw = $wiki->retrieve_node($node); # print STDERR Dumper($raw); } catch Error with { my $ex = shift; # Get hold of the exception object # $raw = ''; # handle the exception; # print STDERR Dumper($ex); }; my $content = $wiki->format($raw); # print STDERR Dumper ($content); my %tt_vars = ( content => $content, node_name => CGI::escapeHTML($node), node_param => CGI::escape($node) ); if ($node eq "Recent Changes") { my @recent = $wiki->list_recent_changes( days => $days ); @recent = map { {name => CGI::escapeHTML($_->{name}), last_modified => CGI::escapeHTML($_->{last_modified}), comment => CGI::escapeHTML($_->{comment}), url => "wiki.cgi?node=" . CGI::escape($_->{name}) } } @recent; $tt_vars{recent_changes} = \@recent; $tt_vars{days} = $days; process_template("recent_changes.tt", $node, \%tt_vars); } else { process_template("node.tt", $node, \%tt_vars); } } sub preview_node { my $node = shift; my $content = $q->param('content'); my $checksum = $q->param('checksum'); my $ok = 0; try { $ok = $wiki->verify_checksum($node, $checksum) ; } catch Error with { my $ex = shift; # ignore if page doesn't exist print STDERR Dumper($ex); }; if ($ok) { my %tt_vars = ( content => CGI::escapeHTML($content), preview_html => $wiki->format($content), checksum => CGI::escapeHTML($checksum) ); process_template("edit_form.tt", $node, \%tt_vars); } else { my %node_data; try { %node_data = $wiki->retrieve_node($node); } catch Error with { my $ex = shift; # print STDERR Dumper($ex); }; # my %node_data = $wiki->retrieve_node($node); my ($stored, $checksum) = @node_data{ qw( content checksum ) }; my %tt_vars = ( checksum => CGI::escapeHTML($checksum), new_content => CGI::escapeHTML($content), stored => CGI::escapeHTML($stored) ); process_template("edit_conflict.tt", $node, \%tt_vars); } } sub edit_node { my $node = shift; my %node_data; try { %node_data = $wiki->retrieve_node($node); } catch Error with { my $ex = shift; # print STDERR Dumper($ex); }; my ($content, $checksum) = @node_data{ qw( content checksum ) }; my %tt_vars = ( content => CGI::escapeHTML($content), checksum => CGI::escapeHTML($checksum) ); process_template("edit_form.tt", $node, \%tt_vars); } sub process_template { my ($template, $node, $vars, $conf) = @_; $vars ||= {}; $conf ||= {}; my %tt_vars = ( %$vars, site_name => "GopherIT", cgi_url => "wiki.cgi", contact_email => "alexmc\@yahoo.com", description => "", keywords => "", stylesheet => "/gopherit2/styles.css", home_link => "wiki.cgi", home_name => "Home" ); if ($node) { $tt_vars{node_name} = CGI::escapeHTML($node); $tt_vars{node_param} = CGI::escape($node); } my %tt_conf = ( %$conf, INCLUDE_PATH => "/home/sflink.net/gopherit/templates" ); # Create Template object, print CGI header, process template. my $tt = Template->new(\%tt_conf); print CGI::header; unless ($tt->process($template, \%tt_vars)) { print qq(ERROR

Failed to process template: ) . $tt->error . qq(

); } } sub commit_node { my $node = shift; my $content = $q->param('content'); my $checksum = $q->param('checksum'); # ALEX my $password = $q->param('password'); if ($password !~ /fiawol/i) { my %tt_vars = ( ); process_template("wrong_password.tt", $node, \%tt_vars); return; } my $written = 0 ; try { $written = $wiki->write_node ($node, $content, $checksum); } catch Error with { my $ex = shift ; }; if ($written) { display_node($node); } else { my %node_data ; try { %node_data = $wiki->retrieve_node($node); } catch Error with { my $ex = shift; print STDERR Dumper($ex); }; # my %node_data = $wiki->retrieve_node($node); my ($stored, $checksum) = @node_data{ qw( content checksum ) }; my %tt_vars = ( checksum => CGI::escapeHTML($checksum), new_content => CGI::escapeHTML($content), stored => CGI::escapeHTML($stored) ); process_template("edit_conflict.tt", $node, \%tt_vars); } } sub do_search { my $terms = shift; my %finds = $wiki->search_nodes($terms); my @sorted = sort { $finds{$a} cmp $finds{$b} } keys %finds; my @results = map { { url => CGI::escape($_), title => CGI::escapeHTML($_) } } @sorted; my %tt_vars = ( results => \@results ); process_template("search_results.tt", "", \%tt_vars); }