2006-03-23 07:51:15 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2006-05-02 08:53:33 +02:00
|
|
|
use IkiWiki;
|
2006-04-25 01:09:26 +02:00
|
|
|
use IkiWiki::UserInfo;
|
2006-07-04 00:14:52 +02:00
|
|
|
use open qw{:utf8 :std};
|
2006-07-03 22:18:16 +02:00
|
|
|
use Encode;
|
2006-03-23 07:51:15 +01:00
|
|
|
|
|
|
|
package IkiWiki;
|
|
|
|
|
2006-08-27 22:25:05 +02:00
|
|
|
sub printheader ($) { #{{{
|
|
|
|
my $session=shift;
|
|
|
|
|
|
|
|
if ($config{sslcookie}) {
|
|
|
|
print $session->header(-charset => 'utf-8',
|
|
|
|
-cookie => $session->cookie(-secure => 1));
|
|
|
|
} else {
|
|
|
|
print $session->header(-charset => 'utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
} #}}}
|
2008-01-07 22:34:13 +01:00
|
|
|
|
2007-08-22 23:06:13 +02:00
|
|
|
sub showform ($$$$) { #{{{
|
|
|
|
my $form=shift;
|
|
|
|
my $buttons=shift;
|
|
|
|
my $session=shift;
|
|
|
|
my $cgi=shift;
|
|
|
|
|
|
|
|
if (exists $hooks{formbuilder}) {
|
|
|
|
run_hooks(formbuilder => sub {
|
|
|
|
shift->(form => $form, cgi => $cgi, session => $session,
|
|
|
|
buttons => $buttons);
|
|
|
|
});
|
|
|
|
}
|
2007-12-12 09:01:15 +01:00
|
|
|
|
|
|
|
printheader($session);
|
|
|
|
print misctemplate($form->title, $form->render(submit => $buttons));
|
2007-08-22 23:06:13 +02:00
|
|
|
}
|
2006-08-27 22:25:05 +02:00
|
|
|
|
2006-07-07 23:00:48 +02:00
|
|
|
sub redirect ($$) { #{{{
|
|
|
|
my $q=shift;
|
|
|
|
my $url=shift;
|
|
|
|
if (! $config{w3mmode}) {
|
|
|
|
print $q->redirect($url);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "Content-type: text/plain\n";
|
|
|
|
print "W3m-control: GOTO $url\n\n";
|
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
2007-02-02 03:33:03 +01:00
|
|
|
sub check_canedit ($$$;$) { #{{{
|
2006-03-23 07:51:15 +01:00
|
|
|
my $page=shift;
|
2007-02-02 03:33:03 +01:00
|
|
|
my $q=shift;
|
2006-03-23 07:51:15 +01:00
|
|
|
my $session=shift;
|
|
|
|
my $nonfatal=shift;
|
|
|
|
|
2007-02-02 03:33:03 +01:00
|
|
|
my $canedit;
|
|
|
|
run_hooks(canedit => sub {
|
|
|
|
return if defined $canedit;
|
|
|
|
my $ret=shift->($page, $q, $session);
|
2008-01-07 22:34:13 +01:00
|
|
|
if (defined $ret) {
|
|
|
|
if ($ret eq "") {
|
|
|
|
$canedit=1;
|
|
|
|
}
|
|
|
|
elsif (ref $ret eq 'CODE') {
|
2008-01-07 22:35:16 +01:00
|
|
|
$ret->() unless $nonfatal;
|
2008-01-07 22:34:13 +01:00
|
|
|
$canedit=0;
|
|
|
|
}
|
|
|
|
elsif (defined $ret) {
|
|
|
|
error($ret) unless $nonfatal;
|
2008-01-07 22:35:16 +01:00
|
|
|
$canedit=0;
|
2008-01-07 22:34:13 +01:00
|
|
|
}
|
2007-02-02 03:33:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return $canedit;
|
2006-03-23 07:51:15 +01:00
|
|
|
} #}}}
|
|
|
|
|
2008-01-02 01:58:45 +01:00
|
|
|
sub decode_cgi_utf8 ($) { #{{{
|
|
|
|
my $cgi = shift;
|
|
|
|
foreach my $f ($cgi->param) {
|
|
|
|
$cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
|
2006-07-11 23:20:14 +02:00
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
2007-02-02 03:33:03 +01:00
|
|
|
# Check if the user is signed in. If not, redirect to the signin form and
|
|
|
|
# save their place to return to later.
|
|
|
|
sub needsignin ($$) { #{{{
|
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
|
|
|
if (! defined $session->param("name") ||
|
|
|
|
! userinfo_get($session->param("name"), "regdate")) {
|
2008-01-07 21:56:39 +01:00
|
|
|
$session->param(postsignin => $ENV{QUERY_STRING});
|
2007-02-02 03:33:03 +01:00
|
|
|
cgi_signin($q, $session);
|
|
|
|
cgi_savesession($session);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
2006-03-23 07:51:15 +01:00
|
|
|
sub cgi_signin ($$) { #{{{
|
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
2008-01-02 01:58:45 +01:00
|
|
|
decode_cgi_utf8($q);
|
2006-03-23 07:51:15 +01:00
|
|
|
eval q{use CGI::FormBuilder};
|
2006-11-08 22:03:33 +01:00
|
|
|
error($@) if $@;
|
2006-03-23 07:51:15 +01:00
|
|
|
my $form = CGI::FormBuilder->new(
|
|
|
|
title => "signin",
|
2007-04-30 23:27:58 +02:00
|
|
|
name => "signin",
|
2006-06-11 20:51:49 +02:00
|
|
|
charset => "utf-8",
|
2006-03-23 07:51:15 +01:00
|
|
|
method => 'POST',
|
|
|
|
required => 'NONE',
|
|
|
|
javascript => 0,
|
|
|
|
params => $q,
|
2006-04-04 20:43:26 +02:00
|
|
|
action => $config{cgiurl},
|
2006-03-23 07:51:15 +01:00
|
|
|
header => 0,
|
2007-04-30 23:27:58 +02:00
|
|
|
template => {type => 'div'},
|
2006-08-22 00:27:02 +02:00
|
|
|
stylesheet => baseurl()."style.css",
|
2006-03-23 07:51:15 +01:00
|
|
|
);
|
2006-11-20 21:37:27 +01:00
|
|
|
my $buttons=["Login"];
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-07-31 01:08:10 +02:00
|
|
|
if ($q->param("do") ne "signin" && !$form->submitted) {
|
2006-12-29 05:38:40 +01:00
|
|
|
$form->text(gettext("You need to log in first."));
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
2007-02-02 03:33:03 +01:00
|
|
|
$form->field(name => "do", type => "hidden", value => "signin",
|
|
|
|
force => 1);
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-11-20 21:37:27 +01:00
|
|
|
run_hooks(formbuilder_setup => sub {
|
2007-08-17 07:34:59 +02:00
|
|
|
shift->(form => $form, cgi => $q, session => $session,
|
|
|
|
buttons => $buttons);
|
2006-11-20 21:37:27 +01:00
|
|
|
});
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2007-08-22 23:06:13 +02:00
|
|
|
if ($form->submitted) {
|
|
|
|
$form->validate;
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
2007-08-22 23:06:13 +02:00
|
|
|
|
|
|
|
showform($form, $buttons, $session, $q);
|
2006-03-23 07:51:15 +01:00
|
|
|
} #}}}
|
|
|
|
|
2006-11-20 10:40:09 +01:00
|
|
|
sub cgi_postsignin ($$) { #{{{
|
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
2007-02-24 01:20:36 +01:00
|
|
|
|
2006-11-20 10:40:09 +01:00
|
|
|
# Continue with whatever was being done before the signin process.
|
2007-02-24 01:20:36 +01:00
|
|
|
if (defined $session->param("postsignin")) {
|
|
|
|
my $postsignin=CGI->new($session->param("postsignin"));
|
|
|
|
$session->clear("postsignin");
|
|
|
|
cgi($postsignin, $session);
|
|
|
|
cgi_savesession($session);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
else {
|
2008-01-07 21:56:39 +01:00
|
|
|
error(gettext("login failed, perhaps you need to turn on cookies?"));
|
2007-02-24 01:20:36 +01:00
|
|
|
}
|
2006-11-20 10:40:09 +01:00
|
|
|
} #}}}
|
|
|
|
|
2006-03-23 07:51:15 +01:00
|
|
|
sub cgi_prefs ($$) { #{{{
|
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
2007-02-02 03:33:03 +01:00
|
|
|
needsignin($q, $session);
|
|
|
|
|
2008-01-02 01:58:45 +01:00
|
|
|
decode_cgi_utf8($q);
|
2006-03-23 07:51:15 +01:00
|
|
|
eval q{use CGI::FormBuilder};
|
2006-11-08 22:03:33 +01:00
|
|
|
error($@) if $@;
|
2006-03-23 07:51:15 +01:00
|
|
|
my $form = CGI::FormBuilder->new(
|
|
|
|
title => "preferences",
|
2007-04-30 23:27:58 +02:00
|
|
|
name => "preferences",
|
2006-03-23 07:51:15 +01:00
|
|
|
header => 0,
|
2006-06-11 20:51:49 +02:00
|
|
|
charset => "utf-8",
|
2006-03-23 07:51:15 +01:00
|
|
|
method => 'POST',
|
|
|
|
validate => {
|
|
|
|
email => 'EMAIL',
|
|
|
|
},
|
|
|
|
required => 'NONE',
|
|
|
|
javascript => 0,
|
|
|
|
params => $q,
|
2006-04-04 20:43:26 +02:00
|
|
|
action => $config{cgiurl},
|
2007-04-30 23:27:58 +02:00
|
|
|
template => {type => 'div'},
|
2006-08-22 00:27:02 +02:00
|
|
|
stylesheet => baseurl()."style.css",
|
2007-04-29 23:57:25 +02:00
|
|
|
fieldsets => [
|
|
|
|
[login => gettext("Login")],
|
|
|
|
[preferences => gettext("Preferences")],
|
|
|
|
[admin => gettext("Admin")]
|
|
|
|
],
|
2006-03-23 07:51:15 +01:00
|
|
|
);
|
2006-11-20 21:37:27 +01:00
|
|
|
my $buttons=["Save Preferences", "Logout", "Cancel"];
|
|
|
|
|
|
|
|
run_hooks(formbuilder_setup => sub {
|
2007-08-17 07:34:59 +02:00
|
|
|
shift->(form => $form, cgi => $q, session => $session,
|
|
|
|
buttons => $buttons);
|
2006-11-20 21:37:27 +01:00
|
|
|
});
|
2006-03-23 07:51:15 +01:00
|
|
|
|
|
|
|
$form->field(name => "do", type => "hidden");
|
2007-04-29 23:57:25 +02:00
|
|
|
$form->field(name => "email", size => 50, fieldset => "preferences");
|
|
|
|
$form->field(name => "banned_users", size => 50,
|
|
|
|
fieldset => "admin");
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-11-20 21:37:27 +01:00
|
|
|
my $user_name=$session->param("name");
|
2006-03-23 07:51:15 +01:00
|
|
|
if (! is_admin($user_name)) {
|
2006-10-28 02:35:33 +02:00
|
|
|
$form->field(name => "banned_users", type => "hidden");
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
2006-10-03 00:56:09 +02:00
|
|
|
|
2006-03-23 07:51:15 +01:00
|
|
|
if (! $form->submitted) {
|
|
|
|
$form->field(name => "email", force => 1,
|
|
|
|
value => userinfo_get($user_name, "email"));
|
2006-10-28 02:35:33 +02:00
|
|
|
if (is_admin($user_name)) {
|
|
|
|
$form->field(name => "banned_users", force => 1,
|
|
|
|
value => join(" ", get_banned_users()));
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->submitted eq 'Logout') {
|
|
|
|
$session->delete();
|
2006-07-07 23:00:48 +02:00
|
|
|
redirect($q, $config{url});
|
2006-03-23 07:51:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
elsif ($form->submitted eq 'Cancel') {
|
2006-07-07 23:00:48 +02:00
|
|
|
redirect($q, $config{url});
|
2006-03-23 07:51:15 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-12-29 05:38:40 +01:00
|
|
|
elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
|
2008-01-29 06:36:58 +01:00
|
|
|
if (defined $form->field('email')) {
|
|
|
|
userinfo_set($user_name, 'email', $form->field('email')) ||
|
|
|
|
error("failed to set email");
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
2006-10-28 02:35:33 +02:00
|
|
|
if (is_admin($user_name)) {
|
|
|
|
set_banned_users(grep { ! is_admin($_) }
|
2007-02-15 03:22:08 +01:00
|
|
|
split(' ',
|
|
|
|
$form->field("banned_users"))) ||
|
|
|
|
error("failed saving changes");
|
2006-10-28 02:35:33 +02:00
|
|
|
}
|
2006-12-29 05:38:40 +01:00
|
|
|
$form->text(gettext("Preferences saved."));
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
|
2007-08-22 23:06:13 +02:00
|
|
|
showform($form, $buttons, $session, $q);
|
2006-03-23 07:51:15 +01:00
|
|
|
} #}}}
|
|
|
|
|
2007-03-08 07:03:59 +01:00
|
|
|
sub cgi_editpage ($$) { #{{{
|
2006-03-23 07:51:15 +01:00
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
2007-03-18 00:20:27 +01:00
|
|
|
my @fields=qw(do rcsinfo subpage from page type editcontent comments);
|
2006-09-16 02:52:26 +02:00
|
|
|
my @buttons=("Save Page", "Preview", "Cancel");
|
|
|
|
|
2008-01-02 01:58:45 +01:00
|
|
|
decode_cgi_utf8($q);
|
2006-12-02 01:19:55 +01:00
|
|
|
eval q{use CGI::FormBuilder};
|
2006-11-08 22:03:33 +01:00
|
|
|
error($@) if $@;
|
2006-03-23 07:51:15 +01:00
|
|
|
my $form = CGI::FormBuilder->new(
|
2007-08-17 07:34:59 +02:00
|
|
|
title => "editpage",
|
2006-09-16 02:52:26 +02:00
|
|
|
fields => \@fields,
|
2006-06-11 20:51:49 +02:00
|
|
|
charset => "utf-8",
|
2006-03-23 07:51:15 +01:00
|
|
|
method => 'POST',
|
2006-05-27 21:04:46 +02:00
|
|
|
required => [qw{editcontent}],
|
2006-03-23 07:51:15 +01:00
|
|
|
javascript => 0,
|
|
|
|
params => $q,
|
2006-04-04 20:43:26 +02:00
|
|
|
action => $config{cgiurl},
|
2007-07-16 07:24:31 +02:00
|
|
|
header => 0,
|
2006-03-23 07:51:15 +01:00
|
|
|
table => 0,
|
2007-01-12 21:48:19 +01:00
|
|
|
template => scalar template_params("editpage.tmpl"),
|
2007-05-11 22:09:58 +02:00
|
|
|
wikiname => $config{wikiname},
|
2006-03-23 07:51:15 +01:00
|
|
|
);
|
|
|
|
|
2006-11-20 21:37:27 +01:00
|
|
|
run_hooks(formbuilder_setup => sub {
|
2007-08-17 07:34:59 +02:00
|
|
|
shift->(form => $form, cgi => $q, session => $session,
|
|
|
|
buttons => \@buttons);
|
2006-11-20 21:37:27 +01:00
|
|
|
});
|
|
|
|
|
2006-07-11 23:20:14 +02:00
|
|
|
# This untaint is safe because titlepage removes any problematic
|
2006-03-29 04:14:55 +02:00
|
|
|
# characters.
|
2006-07-11 23:20:14 +02:00
|
|
|
my ($page)=$form->field('page');
|
2006-08-13 04:03:43 +02:00
|
|
|
$page=titlepage(possibly_foolish_untaint($page));
|
2007-08-28 03:59:01 +02:00
|
|
|
if (! defined $page || ! length $page ||
|
|
|
|
file_pruned($page, $config{srcdir}) || $page=~/^\//) {
|
2006-03-23 07:51:15 +01:00
|
|
|
error("bad page name");
|
|
|
|
}
|
|
|
|
|
2006-07-26 23:23:06 +02:00
|
|
|
my $from;
|
|
|
|
if (defined $form->field('from')) {
|
|
|
|
($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
|
|
|
|
}
|
|
|
|
|
2006-07-04 03:29:45 +02:00
|
|
|
my $file;
|
2006-08-13 04:03:43 +02:00
|
|
|
my $type;
|
2007-03-18 00:20:27 +01:00
|
|
|
if (exists $pagesources{$page} && $form->field("do") ne "create") {
|
2006-07-26 23:23:06 +02:00
|
|
|
$file=$pagesources{$page};
|
|
|
|
$type=pagetype($file);
|
2008-01-29 05:08:48 +01:00
|
|
|
if (! defined $type || $type=~/^_/) {
|
2007-02-10 21:37:36 +01:00
|
|
|
error(sprintf(gettext("%s is not an editable page"), $page));
|
|
|
|
}
|
2007-03-18 00:20:27 +01:00
|
|
|
if (! $form->submitted) {
|
|
|
|
$form->field(name => "rcsinfo",
|
|
|
|
value => rcs_prepedit($file), force => 1);
|
|
|
|
}
|
2007-12-13 01:11:29 +01:00
|
|
|
$form->field(name => "editcontent", validate => '/.*/');
|
2006-03-29 20:35:04 +02:00
|
|
|
}
|
2006-07-04 03:29:45 +02:00
|
|
|
else {
|
2006-07-26 23:23:06 +02:00
|
|
|
$type=$form->param('type');
|
|
|
|
if (defined $type && length $type && $hooks{htmlize}{$type}) {
|
|
|
|
$type=possibly_foolish_untaint($type);
|
|
|
|
}
|
2008-01-05 07:47:04 +01:00
|
|
|
elsif (defined $from && exists $pagesources{$from}) {
|
2006-07-26 23:23:06 +02:00
|
|
|
# favor the type of linking page
|
|
|
|
$type=pagetype($pagesources{$from});
|
|
|
|
}
|
2006-08-13 07:56:01 +02:00
|
|
|
$type=$config{default_pageext} unless defined $type;
|
2006-07-26 23:23:06 +02:00
|
|
|
$file=$page.".".$type;
|
2007-03-18 00:20:27 +01:00
|
|
|
if (! $form->submitted) {
|
|
|
|
$form->field(name => "rcsinfo", value => "", force => 1);
|
|
|
|
}
|
2007-12-13 01:11:29 +01:00
|
|
|
$form->field(name => "editcontent", validate => '/.+/');
|
2006-07-04 03:29:45 +02:00
|
|
|
}
|
2006-07-26 23:23:06 +02:00
|
|
|
|
2006-03-23 07:51:15 +01:00
|
|
|
$form->field(name => "do", type => 'hidden');
|
|
|
|
$form->field(name => "from", type => 'hidden');
|
|
|
|
$form->field(name => "rcsinfo", type => 'hidden');
|
2006-03-24 06:33:23 +01:00
|
|
|
$form->field(name => "subpage", type => 'hidden');
|
2007-03-08 07:03:59 +01:00
|
|
|
$form->field(name => "page", value => pagetitle($page, 1), force => 1);
|
2006-07-26 23:23:06 +02:00
|
|
|
$form->field(name => "type", value => $type, force => 1);
|
2006-03-23 07:51:15 +01:00
|
|
|
$form->field(name => "comments", type => "text", size => 80);
|
2006-05-27 21:04:46 +02:00
|
|
|
$form->field(name => "editcontent", type => "textarea", rows => 20,
|
2006-03-23 07:51:15 +01:00
|
|
|
cols => 80);
|
|
|
|
$form->tmpl_param("can_commit", $config{rcs});
|
|
|
|
$form->tmpl_param("indexlink", indexlink());
|
|
|
|
$form->tmpl_param("helponformattinglink",
|
2007-12-08 21:59:08 +01:00
|
|
|
htmllink("", "", "ikiwiki/formatting",
|
|
|
|
noimageinline => 1,
|
|
|
|
linktext => "FormattingHelp"));
|
2006-08-22 00:27:02 +02:00
|
|
|
$form->tmpl_param("baseurl", baseurl());
|
2006-03-23 07:51:15 +01:00
|
|
|
|
|
|
|
if ($form->submitted eq "Cancel") {
|
2007-03-18 00:20:27 +01:00
|
|
|
if ($form->field("do") eq "create" && defined $from) {
|
2006-09-16 03:23:14 +02:00
|
|
|
redirect($q, "$config{url}/".htmlpage($from));
|
|
|
|
}
|
2007-03-18 00:20:27 +01:00
|
|
|
elsif ($form->field("do") eq "create") {
|
2006-09-16 03:23:14 +02:00
|
|
|
redirect($q, $config{url});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
redirect($q, "$config{url}/".htmlpage($page));
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
elsif ($form->submitted eq "Preview") {
|
2007-08-26 23:33:25 +02:00
|
|
|
my $content=$form->field('editcontent');
|
|
|
|
run_hooks(editcontent => sub {
|
|
|
|
$content=shift->(
|
|
|
|
content => $content,
|
|
|
|
page => $page,
|
|
|
|
cgi => $q,
|
|
|
|
session => $session,
|
|
|
|
);
|
|
|
|
});
|
2006-03-23 07:51:15 +01:00
|
|
|
$form->tmpl_param("page_preview",
|
2006-08-28 20:17:59 +02:00
|
|
|
htmlize($page, $type,
|
2006-08-23 22:23:57 +02:00
|
|
|
linkify($page, "",
|
|
|
|
preprocess($page, $page,
|
2007-08-26 23:33:25 +02:00
|
|
|
filter($page, $page, $content), 0, 1))));
|
2008-02-04 01:51:00 +01:00
|
|
|
# previewing may have created files on disk
|
|
|
|
saveindex();
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
2007-08-17 07:34:59 +02:00
|
|
|
elsif ($form->submitted eq "Save Page") {
|
2006-03-23 07:51:15 +01:00
|
|
|
$form->tmpl_param("page_preview", "");
|
|
|
|
}
|
|
|
|
$form->tmpl_param("page_conflict", "");
|
|
|
|
|
2007-08-26 23:33:25 +02:00
|
|
|
if ($form->submitted ne "Save Page" || ! $form->validate) {
|
2006-03-23 07:51:15 +01:00
|
|
|
if ($form->field("do") eq "create") {
|
|
|
|
my @page_locs;
|
|
|
|
my $best_loc;
|
|
|
|
if (! defined $from || ! length $from ||
|
2006-07-11 23:20:14 +02:00
|
|
|
$from ne $form->field('from') ||
|
2006-12-21 20:36:15 +01:00
|
|
|
file_pruned($from, $config{srcdir}) ||
|
2006-03-29 00:56:56 +02:00
|
|
|
$from=~/^\// ||
|
|
|
|
$form->submitted eq "Preview") {
|
2006-03-23 07:51:15 +01:00
|
|
|
@page_locs=$best_loc=$page;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $dir=$from."/";
|
2006-04-25 03:15:20 +02:00
|
|
|
$dir=~s![^/]+/+$!!;
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-07-11 23:20:14 +02:00
|
|
|
if ((defined $form->field('subpage') && length $form->field('subpage')) ||
|
2006-12-29 05:38:40 +01:00
|
|
|
$page eq gettext('discussion')) {
|
2006-03-23 07:51:15 +01:00
|
|
|
$best_loc="$from/$page";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$best_loc=$dir.$page;
|
|
|
|
}
|
|
|
|
|
|
|
|
push @page_locs, $dir.$page;
|
|
|
|
push @page_locs, "$from/$page";
|
|
|
|
while (length $dir) {
|
2006-04-25 03:15:20 +02:00
|
|
|
$dir=~s![^/]+/+$!!;
|
2006-03-23 07:51:15 +01:00
|
|
|
push @page_locs, $dir.$page;
|
|
|
|
}
|
2008-02-14 21:42:14 +01:00
|
|
|
|
|
|
|
push @page_locs, "$config{userdir}/$page"
|
|
|
|
if length $config{userdir};
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
|
2006-05-02 04:57:45 +02:00
|
|
|
@page_locs = grep {
|
2007-02-02 03:33:03 +01:00
|
|
|
! exists $pagecase{lc $_}
|
2006-05-02 04:57:45 +02:00
|
|
|
} @page_locs;
|
|
|
|
if (! @page_locs) {
|
|
|
|
# hmm, someone else made the page in the
|
|
|
|
# meantime?
|
2008-02-14 21:42:14 +01:00
|
|
|
if ($form->submitted eq "Preview") {
|
|
|
|
# let them go ahead with the edit
|
|
|
|
# and resolve the conflict at save
|
|
|
|
# time
|
|
|
|
@page_locs=$page;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
redirect($q, "$config{url}/".htmlpage($page));
|
|
|
|
return;
|
|
|
|
}
|
2006-05-02 04:57:45 +02:00
|
|
|
}
|
2007-02-02 03:33:03 +01:00
|
|
|
|
|
|
|
my @editable_locs = grep {
|
|
|
|
check_canedit($_, $q, $session, 1)
|
|
|
|
} @page_locs;
|
|
|
|
if (! @editable_locs) {
|
|
|
|
# let it throw an error this time
|
|
|
|
map { check_canedit($_, $q, $session) } @page_locs;
|
|
|
|
}
|
2006-07-26 23:23:06 +02:00
|
|
|
|
|
|
|
my @page_types;
|
|
|
|
if (exists $hooks{htmlize}) {
|
2008-01-29 05:08:48 +01:00
|
|
|
@page_types=grep { !/^_/ }
|
|
|
|
keys %{$hooks{htmlize}};
|
2006-07-26 23:23:06 +02:00
|
|
|
}
|
|
|
|
|
2006-03-23 07:51:15 +01:00
|
|
|
$form->tmpl_param("page_select", 1);
|
|
|
|
$form->field(name => "page", type => 'select',
|
2007-03-07 10:23:13 +01:00
|
|
|
options => [ map { pagetitle($_, 1) } @editable_locs ],
|
|
|
|
value => pagetitle($best_loc, 1));
|
2006-07-26 23:23:06 +02:00
|
|
|
$form->field(name => "type", type => 'select',
|
|
|
|
options => \@page_types);
|
2006-12-29 05:38:40 +01:00
|
|
|
$form->title(sprintf(gettext("creating %s"), pagetitle($page)));
|
2007-07-16 07:24:31 +02:00
|
|
|
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
elsif ($form->field("do") eq "edit") {
|
2007-02-02 03:33:03 +01:00
|
|
|
check_canedit($page, $q, $session);
|
2006-05-27 21:04:46 +02:00
|
|
|
if (! defined $form->field('editcontent') ||
|
|
|
|
! length $form->field('editcontent')) {
|
2006-03-23 07:51:15 +01:00
|
|
|
my $content="";
|
2006-08-13 04:03:43 +02:00
|
|
|
if (exists $pagesources{$page}) {
|
|
|
|
$content=readfile(srcfile($pagesources{$page}));
|
2006-03-23 07:51:15 +01:00
|
|
|
$content=~s/\n/\r\n/g;
|
|
|
|
}
|
2006-05-27 21:04:46 +02:00
|
|
|
$form->field(name => "editcontent", value => $content,
|
2006-03-23 07:51:15 +01:00
|
|
|
force => 1);
|
|
|
|
}
|
|
|
|
$form->tmpl_param("page_select", 0);
|
|
|
|
$form->field(name => "page", type => 'hidden');
|
2006-07-26 23:23:06 +02:00
|
|
|
$form->field(name => "type", type => 'hidden');
|
2006-12-29 05:38:40 +01:00
|
|
|
$form->title(sprintf(gettext("editing %s"), pagetitle($page)));
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
|
2007-08-22 23:06:13 +02:00
|
|
|
showform($form, \@buttons, $session, $q);
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
# save page
|
2007-02-02 03:33:03 +01:00
|
|
|
check_canedit($page, $q, $session);
|
2007-08-15 02:06:20 +02:00
|
|
|
|
|
|
|
my $exists=-e "$config{srcdir}/$file";
|
|
|
|
|
2007-08-28 03:59:01 +02:00
|
|
|
if ($form->field("do") ne "create" && ! $exists &&
|
|
|
|
! eval { srcfile($file) }) {
|
2007-02-24 01:39:06 +01:00
|
|
|
$form->tmpl_param("page_gone", 1);
|
2007-03-18 00:20:27 +01:00
|
|
|
$form->field(name => "do", value => "create", force => 1);
|
2007-02-24 01:39:06 +01:00
|
|
|
$form->tmpl_param("page_select", 0);
|
|
|
|
$form->field(name => "page", type => 'hidden');
|
|
|
|
$form->field(name => "type", type => 'hidden');
|
2007-03-18 00:20:27 +01:00
|
|
|
$form->title(sprintf(gettext("editing %s"), $page));
|
2007-08-22 23:06:13 +02:00
|
|
|
showform($form, \@buttons, $session, $q);
|
2007-02-24 01:39:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-08-15 02:06:20 +02:00
|
|
|
elsif ($form->field("do") eq "create" && $exists) {
|
2007-03-18 00:57:03 +01:00
|
|
|
$form->tmpl_param("creation_conflict", 1);
|
|
|
|
$form->field(name => "do", value => "edit", force => 1);
|
|
|
|
$form->tmpl_param("page_select", 0);
|
|
|
|
$form->field(name => "page", type => 'hidden');
|
|
|
|
$form->field(name => "type", type => 'hidden');
|
|
|
|
$form->title(sprintf(gettext("editing %s"), $page));
|
|
|
|
$form->field("editcontent",
|
|
|
|
value => readfile("$config{srcdir}/$file").
|
|
|
|
"\n\n\n".$form->field("editcontent"),
|
|
|
|
force => 1);
|
2007-08-22 23:06:13 +02:00
|
|
|
showform($form, \@buttons, $session, $q);
|
2007-03-18 00:57:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-07-11 23:20:14 +02:00
|
|
|
my $content=$form->field('editcontent');
|
2007-08-26 23:33:25 +02:00
|
|
|
run_hooks(editcontent => sub {
|
|
|
|
$content=shift->(
|
|
|
|
content => $content,
|
|
|
|
page => $page,
|
|
|
|
cgi => $q,
|
|
|
|
session => $session,
|
|
|
|
);
|
|
|
|
});
|
2006-03-23 07:51:15 +01:00
|
|
|
$content=~s/\r\n/\n/g;
|
|
|
|
$content=~s/\r/\n/g;
|
2007-12-12 19:41:21 +01:00
|
|
|
$content.="\n" if $content !~ /\n$/;
|
2007-02-15 03:22:08 +01:00
|
|
|
|
|
|
|
$config{cgi}=0; # avoid cgi error message
|
|
|
|
eval { writefile($file, $config{srcdir}, $content) };
|
|
|
|
$config{cgi}=1;
|
|
|
|
if ($@) {
|
|
|
|
$form->field(name => "rcsinfo", value => rcs_prepedit($file),
|
|
|
|
force => 1);
|
|
|
|
$form->tmpl_param("failed_save", 1);
|
|
|
|
$form->tmpl_param("error_message", $@);
|
|
|
|
$form->field("editcontent", value => $content, force => 1);
|
|
|
|
$form->tmpl_param("page_select", 0);
|
|
|
|
$form->field(name => "page", type => 'hidden');
|
|
|
|
$form->field(name => "type", type => 'hidden');
|
|
|
|
$form->title(sprintf(gettext("editing %s"), $page));
|
2007-08-22 23:06:13 +02:00
|
|
|
showform($form, \@buttons, $session, $q);
|
2007-02-15 03:22:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2007-02-21 09:55:28 +01:00
|
|
|
my $conflict;
|
2006-03-23 07:51:15 +01:00
|
|
|
if ($config{rcs}) {
|
2006-11-22 15:28:38 +01:00
|
|
|
my $message="";
|
|
|
|
if (defined $form->field('comments') &&
|
|
|
|
length $form->field('comments')) {
|
|
|
|
$message=$form->field('comments');
|
|
|
|
}
|
|
|
|
|
2007-08-15 02:06:20 +02:00
|
|
|
if (! $exists) {
|
2006-03-23 07:51:15 +01:00
|
|
|
rcs_add($file);
|
|
|
|
}
|
2007-02-21 09:55:28 +01:00
|
|
|
|
|
|
|
# Prevent deadlock with post-commit hook by
|
|
|
|
# signaling to it that it should not try to
|
2008-02-04 01:47:01 +01:00
|
|
|
# do anything.
|
2007-02-21 09:55:28 +01:00
|
|
|
disable_commit_hook();
|
|
|
|
$conflict=rcs_commit($file, $message,
|
2006-11-22 15:28:38 +01:00
|
|
|
$form->field("rcsinfo"),
|
|
|
|
$session->param("name"), $ENV{REMOTE_ADDR});
|
2007-02-21 09:55:28 +01:00
|
|
|
enable_commit_hook();
|
|
|
|
rcs_update();
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2007-02-21 09:55:28 +01:00
|
|
|
# Refresh even if there was a conflict, since other changes
|
|
|
|
# may have been committed while the post-commit hook was
|
|
|
|
# disabled.
|
|
|
|
require IkiWiki::Render;
|
|
|
|
refresh();
|
|
|
|
saveindex();
|
|
|
|
|
|
|
|
if (defined $conflict) {
|
|
|
|
$form->field(name => "rcsinfo", value => rcs_prepedit($file),
|
|
|
|
force => 1);
|
|
|
|
$form->tmpl_param("page_conflict", 1);
|
|
|
|
$form->field("editcontent", value => $conflict, force => 1);
|
2007-02-24 01:39:06 +01:00
|
|
|
$form->field("do", "edit", force => 1);
|
2007-02-21 09:55:28 +01:00
|
|
|
$form->tmpl_param("page_select", 0);
|
|
|
|
$form->field(name => "page", type => 'hidden');
|
|
|
|
$form->field(name => "type", type => 'hidden');
|
|
|
|
$form->title(sprintf(gettext("editing %s"), $page));
|
2007-08-22 23:06:13 +02:00
|
|
|
showform($form, \@buttons, $session, $q);
|
2007-02-21 09:55:28 +01:00
|
|
|
return;
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
else {
|
2007-02-21 09:55:28 +01:00
|
|
|
# The trailing question mark tries to avoid broken
|
|
|
|
# caches and get the most recent version of the page.
|
|
|
|
redirect($q, "$config{url}/".htmlpage($page)."?updated");
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
2006-11-26 20:43:50 +01:00
|
|
|
sub cgi_getsession ($) { #{{{
|
|
|
|
my $q=shift;
|
|
|
|
|
|
|
|
eval q{use CGI::Session};
|
|
|
|
CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
|
|
|
|
|
|
|
|
my $oldmask=umask(077);
|
|
|
|
my $session = CGI::Session->new("driver:DB_File", $q,
|
|
|
|
{ FileName => "$config{wikistatedir}/sessions.db" });
|
|
|
|
umask($oldmask);
|
|
|
|
|
|
|
|
return $session;
|
|
|
|
} #}}}
|
|
|
|
|
|
|
|
sub cgi_savesession ($) { #{{{
|
|
|
|
my $session=shift;
|
|
|
|
|
|
|
|
# Force session flush with safe umask.
|
|
|
|
my $oldmask=umask(077);
|
|
|
|
$session->flush;
|
|
|
|
umask($oldmask);
|
2007-02-02 03:33:03 +01:00
|
|
|
} #}}}
|
2006-11-26 20:43:50 +01:00
|
|
|
|
2006-11-20 10:40:09 +01:00
|
|
|
sub cgi (;$$) { #{{{
|
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
|
|
|
if (! $q) {
|
2006-11-26 20:43:50 +01:00
|
|
|
eval q{use CGI};
|
2006-11-20 10:40:09 +01:00
|
|
|
error($@) if $@;
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-11-20 10:40:09 +01:00
|
|
|
$q=CGI->new;
|
2006-05-03 23:50:39 +02:00
|
|
|
|
2006-11-20 10:40:09 +01:00
|
|
|
run_hooks(cgi => sub { shift->($q) });
|
|
|
|
}
|
|
|
|
|
2006-03-23 07:51:15 +01:00
|
|
|
my $do=$q->param('do');
|
|
|
|
if (! defined $do || ! length $do) {
|
2006-07-10 23:13:41 +02:00
|
|
|
my $error = $q->cgi_error;
|
|
|
|
if ($error) {
|
|
|
|
error("Request not processed: $error");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error("\"do\" parameter missing");
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
|
2006-11-20 13:03:35 +01:00
|
|
|
# Need to lock the wiki before getting a session.
|
|
|
|
lockwiki();
|
2008-02-03 06:23:04 +01:00
|
|
|
loadindex();
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-11-20 10:40:09 +01:00
|
|
|
if (! $session) {
|
2006-11-26 20:43:50 +01:00
|
|
|
$session=cgi_getsession($q);
|
2006-11-20 10:40:09 +01:00
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
|
2006-11-20 02:52:18 +01:00
|
|
|
# Auth hooks can sign a user in.
|
|
|
|
if ($do ne 'signin' && ! defined $session->param("name")) {
|
|
|
|
run_hooks(auth => sub {
|
|
|
|
shift->($q, $session)
|
|
|
|
});
|
|
|
|
if (defined $session->param("name")) {
|
|
|
|
# Make sure whatever user was authed is in the
|
|
|
|
# userinfo db.
|
|
|
|
if (! userinfo_get($session->param("name"), "regdate")) {
|
|
|
|
userinfo_setall($session->param("name"), {
|
|
|
|
email => "",
|
|
|
|
password => "",
|
|
|
|
regdate => time,
|
2007-02-15 03:22:08 +01:00
|
|
|
}) || error("failed adding user");
|
2006-11-20 02:52:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-02 03:33:03 +01:00
|
|
|
|
|
|
|
if (defined $session->param("name") &&
|
|
|
|
userinfo_get($session->param("name"), "banned")) {
|
2006-10-28 02:35:33 +02:00
|
|
|
print $q->header(-status => "403 Forbidden");
|
2006-10-28 02:36:34 +02:00
|
|
|
$session->delete();
|
2006-12-29 05:38:40 +01:00
|
|
|
print gettext("You are banned.");
|
2006-11-26 20:43:50 +01:00
|
|
|
cgi_savesession($session);
|
2006-10-28 02:35:33 +02:00
|
|
|
}
|
2007-08-05 23:38:27 +02:00
|
|
|
|
|
|
|
run_hooks(sessioncgi => sub { shift->($q, $session) });
|
|
|
|
|
|
|
|
if ($do eq 'signin') {
|
2007-02-02 03:33:03 +01:00
|
|
|
cgi_signin($q, $session);
|
|
|
|
cgi_savesession($session);
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
elsif ($do eq 'prefs') {
|
|
|
|
cgi_prefs($q, $session);
|
|
|
|
}
|
2007-02-02 03:33:03 +01:00
|
|
|
elsif ($do eq 'create' || $do eq 'edit') {
|
|
|
|
cgi_editpage($q, $session);
|
|
|
|
}
|
2008-01-07 22:39:49 +01:00
|
|
|
elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
|
2008-01-07 21:56:39 +01:00
|
|
|
cgi_postsignin($q, $session);
|
2007-01-12 21:56:54 +01:00
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
else {
|
|
|
|
error("unknown do parameter");
|
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
|
|
|
1
|