#!/usr/bin/perl -w -s
use strict;
use DB_File;
use Tie::Cvs;
use Tie::TextDir;
use Biblio::Thesaurus;
use Fcntl ;
use CGI (":all","start_table",":nodebug");
use Data::Dumper ;
$Data::Dumper::Terse =1;
$Data::Dumper::Indent =1;
$Data::Dumper::Deepcopy=1;
use HTML::BoxML;
use Pod::Simple::HTML;
use LWP::Simple qw/get/; #import _only_ the get function
if($ENV{'PATH_INFO'}){
my $aux = $ENV{'PATH_INFO'};
$aux =~ s!^/!!;
$aux =~ s!/! !g;
param('topic', $aux); }
my %h;
our ($textdir,$tohtml,$export,$import,$create,$cvsdir,$slides,$base,$name,$print);
$tohtml ||= $slides || $print;
our %conf;
if($tohtml || $textdir){
if($ARGV[0] =~ s/^(.*?):(.*)$/$2/ ){ $base = $1 }
$conf{mode}="FILE" unless $base;
$conf{base} = $textdir || $base || ".";
}
else{
$conf{base} = $base || $0;
$conf{base} =~ s!.*/!!;
}
# Defaults
setDefault();
sub setDefault{
$conf{dir} = "/home/jj/cvs/Breviario/";
$conf{cgidir} = "/home/jj/public_html/bin/";
$conf{cvsdir} = $cvsdir || "/breviario/";
$conf{cssfile} = "";
$conf{mode} ||= "CVS" ; #by default use Tie::CVS
$conf{mode} ||= "DBFILE"; #by default use Tie::DB_File
$conf{mode} ||= "FILE"; #by default use Simple File
$conf{topic} = "apresentação";
$conf{author} = 'jj@di.uminho.pt';
$conf{name} = ucfirst($name || $conf{base} || "Breviário");
undef $conf{hosts_allow};
### $conf{hosts_allow} = "natura.di.uminho.pt 127.0.0.1";
}
my @col=("#ffffff",
"#226219;" , #Verde fundo
"#6a8394;" , #azul
"#f36e21;" , #laranja
"#f9ce87;" , #laranja2
"#fbdea7;" , #laranja3
"#d25c10;" , #laranja4
);
my $defaultcss = " ";
my $fgo= q[
var i = 0;
var max=30;
var loc;
function go(){
if(i > max){ i=0; } loc="#slide"+i;i++; this.location=loc;}];
if (-f "$conf{base}.cnf") {
open CONFIG, "$conf{base}.cnf" or die "Cannot open configuration file $conf{base}.cnf";
my @conf_keys = qw/dir cgidir cssfile base hosts_allow topic author name datadir/;
while () {
chomp;
my ($key,$data) = split /\s*=>\s*/;
for (@conf_keys) {
$conf{$_} = $data if $_ eq $key;
}
}
close CONFIG;
}
## prepare datadir
$conf{datadir} = "$conf{dir}/DataDir" unless exists $conf{datadir};
if(defined $conf{hosts_allow}){
## prepare hosts_allow
my $s=$conf{hosts_allow};
$conf{hosts_allow}={};
for (grep {/./} split /\s+/, $s) {
$conf{hosts_allow}{$_} = $_; }
}
param('base',param('base')||"$conf{dir}$conf{base}.db");
param('topic',param('topic')||$conf{topic});
my $x;
if($conf{mode} eq q{CVS}){
$x = tie %h, 'Tie::Cvs', "$conf{cvsdir}$conf{base}", 0664
or die("can't open '$conf{cvsdir}$conf{base}'".":$!\n");
}
elsif($conf{mode} eq q{DBFILE}) {$x = tie %h, 'DB_File', param('base'),
O_RDWR|O_CREAT , 0666, $DB_BTREE
or die("can't open ".param('base').":$!\n");
}
elsif($conf{mode} eq q{FILE}) {$x = tie %h, 'Tie::TextDir', $conf{'base'}
or die("can't open dir '$conf{base}':$!\n");
print STDERR Dumper($x);
}
my ($mday,$mon,$year) = (localtime(time()))[3,4,5];
##my $fulldate = localtime;
my $fulldate = sprintf('%4d/%.2d/%.2d',$year+1900,$mon+1,$mday);
my %a =
( -defaultaction => 'consultar',
-common => { action => sub { param('topic',param('topic')||'apresentação');},
inter => sub { strong(big(" ".
a({-class=>"homeurl",
-href=>url()."?todo=consultar&topic=$conf{topic}"},
$conf{name})." ")) .
textfield(-name=>'topic') }
},
consultar => { action => sub {
my $opt={};
if(param("topic") =~ m{(.*)!(\w+)}){
param("topic",$1);
$opt={$2=>1}}
print tohtml($opt,$h{"the::".param("topic")}||param('topic'),
$h{param("topic")},
lista_anexos(param("topic")));
my $the = procthe($h{"the::".param("topic")});
param("classe",$the->{thesaurus}{fmt}[0] || "sp");
},
args =>['classe','topic','n'],
},
listar => sub {
my @keys = listartopics();
print p(start_table({class=>"box",cellspacing=>"0",cellpadding=>"0"}),
Tr(th({class=>"boxtitle",width=>'60%'},"Tópico"),
th({class=>"boxtitle",width=>'20%'},"Tamanho"),
th({class=>"boxtitle",width=>'20%'},"Data")));
my $i = 1;
param('todo','consultar');
for (@keys) {
$i++;
my $clss = $i % 2 ? "odd" : "even";
print "| ",sp2url($_)," | \n";
print " ";
print format_size(length($h{$_}))." bytes | \n";
print " ",getDate($h{"the::$_"})," | \n";
print "
\n";
}
print "
\n";
},
editar => [
{ inter => sub {
my $str = "";
$str .= textarea(
-wrap =>"virtual",
-style => "width:100%",
-name => 'thesaurus',
-default => $h{"the::".param('topic')} || param('topic')
."\nfmt sp\n",
-rows => 4,
-columns => 80
);
$str .= textarea(
-wrap =>"virtual",
-style => "width:100%",
-name => 'def',
-default => $h{param('topic')} || "",
-rows => 40,
-columns => 80);
$str .= p("Anexar ficheiro:",br,
submit(-name => 'upload',
-value => 'Upload'),
br,
filefield(-name => 'file',
-size => 60));
$str .= lista_anexos(param('topic'));
$str;
},
precond => sub{
if (not valid(remote_host(),$conf{hosts_allow})) {
#Dumper($conf{hosts_allow}).
"Sorry you cant edit this topic..."; #.remote_host();
} else{""} },
args =>['topic'],
next => 'preview'
},
{ action => sub {
my $thesaurus = param("thesaurus");
$thesaurus =~ s!\ndate\s+.+\n!\n!;
$thesaurus.= "\ndate $fulldate\n";
param("thesaurus", $thesaurus);
print tohtml(param("thesaurus")||param('topic'),
param("def"),
lista_anexos(param("topic")));},
args =>['topic','def','thesaurus'],
poscond => sub{
my $thes=param('thesaurus');
if ($thes =~ m/\nfmt\s+perl\b/ and not isTheServer()){
"Just local user can make perl document";}
else{""} },
next =>'commit'
},
sub {
$h{param('topic')} = n(param('def'));
$h{"the::".param('topic')} = n(param('thesaurus'))||param('topic');
completa_parcial(param('topic'));
print tohtml(param("thesaurus")||param('topic'),
param("def"),
lista_anexos(param("topic"))); }
],
revert => [
{ action => sub {print tohtml($h{"the::".param('topic')}||param('topic'),
$h{param("topic")},
lista_anexos(param("topic")))},
precond => sub{
if (isTheServer()) {""}
else{"Sorry you cant delete this topic...";}},
args =>['topic'],
next =>'REVERT confima?' },
sub{ delete($h{param('topic')});
print p(param("topic"). ": última alteração foi removida."); },
],
slides => [ {action => sub { my $p=param('topic');
print tohtml2({slides=>1},param('topic'));},
args =>['classe','topic'],
precond => sub{
if(classof(param('topic')) eq "slides"){""}
else{ "not a presentation";}},
},
]
);
sub classof{
my $t=shift or return ("");
if(! param("classe")){
my $the1 = $h{"the::$t"} or return("") ;
my $the = procthe($h{"the::$t"});
param("classe",$the->{thesaurus}{fmt}[0] || "");
}
param("classe")
}
sub pod2html{my $doc = shift;
my $podparser = Pod::Simple::HTML->new();
my $str;
$podparser->output_string(\$str);
$podparser->parse_string_document($doc);
$str =~ s!^.*?(.*)