+} #}}}
+
+# Generates a C wrapper program for running ikiwiki in a specific way.
+# The wrapper may be safely made suid.
+sub gen_wrapper ($$) { #{{{
+ my ($svn, $rebuild)=@_;
+
+ eval q{use Cwd 'abs_path'};
+ $srcdir=abs_path($srcdir);
+ $destdir=abs_path($destdir);
+ my $this=abs_path($0);
+ if (! -x $this) {
+ error("$this doesn't seem to be executable");
+ }
+
+ my @params=($srcdir, $templatedir, $destdir, "--wikiname=$wikiname");
+ push @params, "--verbose" if $verbose;
+ push @params, "--rebuild" if $rebuild;
+ push @params, "--nosvn" if !$svn;
+ push @params, "--cgi" if $cgi;
+ push @params, "--url=$url" if $url;
+ push @params, "--cgiurl=$cgiurl" if $cgiurl;
+ push @params, "--historyurl=$historyurl" if $historyurl;
+ my $params=join(" ", @params);
+ my $call='';
+ foreach my $p ($this, $this, @params) {
+ $call.=qq{"$p", };
+ }
+ $call.="NULL";
+
+ my @envsave;
+ push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
+ CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
+ HTTP_COOKIE} if $cgi;
+ my $envsave="";
+ foreach my $var (@envsave) {
+ $envsave.=<<"EOF"
+ if ((s=getenv("$var")))
+ asprintf(&newenviron[i++], "%s=%s", "$var", s);
+EOF
+ }
+
+ open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
+ print OUT <<"EOF";
+/* A wrapper for ikiwiki, can be safely made suid. */
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+extern char **environ;
+
+int main (int argc, char **argv) {
+ /* Sanitize environment. */
+ char *s;
+ char *newenviron[$#envsave+3];
+ int i=0;
+$envsave
+ newenviron[i++]="HOME=$ENV{HOME}";
+ newenviron[i]=NULL;
+ environ=newenviron;
+
+ if (argc == 2 && strcmp(argv[1], "--params") == 0) {
+ printf("$params\\n");
+ exit(0);
+ }
+
+ execl($call);
+ perror("failed to run $this");
+ exit(1);
+}
+EOF
+ close OUT;
+ if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
+ error("failed to compile ikiwiki-wrap.c");
+ }
+ unlink("ikiwiki-wrap.c");
+ print "successfully generated ikiwiki-wrap\n";
+ exit 0;
+} #}}}
+
+sub cgi_recentchanges () { #{{{
+ my $q=shift;
+
+ my $template=HTML::Template->new(
+ filename => "$templatedir/recentchanges.tmpl");
+ $template->param(
+ title => "RecentChanges",
+ indexlink => $url,
+ wikiname => $wikiname,
+ changelog => [rcs_recentchanges(100)],
+ );
+ return $template->output;
+} #}}}
+
+sub cgi_signin ($$) { #{{{
+ my $q=shift;
+ my $session=shift;
+
+ eval q{use CGI::FormBuilder};
+ my $form = CGI::FormBuilder->new(
+ title => "$wikiname signin",
+ fields => [qw(do page name password confirm_password email)],
+ header => 1,
+ method => 'POST',
+ validate => {
+ name => '/^\w+$/',
+ confirm_password => {
+ perl => q{eq $form->field("password")},
+ },
+ email => 'EMAIL',
+ },
+ required => 'NONE',
+ javascript => 0,
+ params => $q,
+ action => $q->request_uri,
+ );
+
+ $form->sessionid($session->id);
+ $form->field(name => "name", required => 0);
+ $form->field(name => "do", type => "hidden");
+ $form->field(name => "page", type => "hidden");
+ $form->field(name => "password", type => "password", required => 0);
+ $form->field(name => "confirm_password", type => "password", required => 0);
+ $form->field(name => "email", required => 0);
+ if ($session->param("name")) {
+ $form->field(name => "name", value => $session->param("name"));
+ }
+ if ($q->param("do") ne "signin") {
+ $form->text("You need to log in before you can edit pages.");
+ }
+
+ if ($form->submitted) {
+ # Set required fields based on how form was submitted.
+ my %required=(
+ "Login" => [qw(name password)],
+ "Register" => [qw(name password confirm_password email)],
+ "Mail Password" => [qw(name)],
+ );
+ foreach my $opt (@{$required{$form->submitted}}) {
+ $form->field(name => $opt, required => 1);
+ }
+
+ # Validate password differently depending on how form was
+ # submitted.
+ if ($form->submitted eq 'Login') {
+ $form->field(
+ name => "password",
+ validate => sub {
+ # TODO get real user password
+ shift eq "foo";
+ },
+ );
+ }
+ else {
+ $form->field(name => "password", validate => 'VALUE');
+ }
+ }
+ else {
+ # Comments only shown first time.
+ $form->field(name => "name", comment => "use FirstnameLastName");
+ $form->field(name => "confirm_password", comment => "(only needed");
+ $form->field(name => "email", comment => "for registration)");
+ }
+
+ if ($form->submitted && $form->validate) {
+ if ($form->submitted eq 'Login') {
+ $session->param("name", $form->field("name"));
+ if (defined $form->field("do")) {
+ $q->redirect(
+ "$cgiurl?do=".$form->field("do").
+ "&page=".$form->field("page"));
+ }
+ else {
+ $q->redirect($url);
+ }
+ }
+ elsif ($form->submitted eq 'Register') {
+ # TODO: save registration info
+ $form->field(name => "confirm_password", type => "hidden");
+ $form->field(name => "email", type => "hidden");
+ $form->text("Registration successful. Now you can Login.");
+ print $form->render(submit => ["Login"]);;
+ }
+ elsif ($form->submitted eq 'Mail Password') {
+ # TODO mail password
+ $form->text("Your password has been emailed to you.");
+ print $form->render(submit => ["Login", "Register", "Mail Password"]);;
+ }
+ }
+ else {
+ print $form->render(submit => ["Login", "Register", "Mail Password"]);;
+ }
+} #}}}
+
+sub cgi () { #{{{
+ eval q{use CGI};
+ eval q{use CGI::Session};
+
+ my $q=CGI->new;
+ # session id has to be _sessionid for CGI::FormBuilder to work.
+ # TODO: stop having the formbuilder emit cookies and change session
+ # id to something else.
+ CGI::Session->name("_sessionid");
+ my $session = CGI::Session->new(undef, $q,
+ { Directory=> "$srcdir/.ikiwiki/sessions" });
+
+ my $do=$q->param('do');
+ if (! defined $do || ! length $do) {
+ error("\"do\" parameter missing");
+ }
+
+ if ($do eq 'recentchanges') {
+ cgi_recentchanges();
+ return;
+ }
+
+ if (! defined $session->param("name") || $do eq 'signin') {
+ cgi_signin($q, $session);
+ return;
+ }
+
+ my ($page)=$q->param('page')=~/$wiki_file_regexp/;
+ if (! defined $page || ! length $page || $page ne $q->param('page') ||
+ $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
+ error("bad page name");
+ }
+ $page=lc($page);
+
+ my $action=$q->request_uri;
+ $action=~s/\?.*//;
+
+ if ($do eq 'create') {
+ if (exists $pagesources{lc($page)}) {
+ # hmm, someone else made the page in the meantime?
+ print $q->redirect("$url/".htmlpage($page));
+ }
+
+ my @page_locs;
+ my ($from)=$q->param('from')=~/$wiki_file_regexp/;
+ if (! defined $from || ! length $from ||
+ $from ne $q->param('from') ||
+ $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
+ @page_locs=$page;
+ }
+ else {
+ my $dir=$from."/";
+ $dir=~s![^/]+/$!!;
+ push @page_locs, $dir.$page;
+ push @page_locs, "$from/$page";
+ while (length $dir) {
+ $dir=~s![^/]+/$!!;
+ push @page_locs, $dir.$page;
+ }
+ }
+
+ $q->param("do", "save");
+ print $q->header,
+ $q->start_html("Creating $page"),
+ $q->h1(indexlink()." Creating $page"),
+ $q->start_form(-action => $action),
+ $q->hidden('do'),
+ "Select page location:",
+ $q->popup_menu('page', \@page_locs),
+ $q->textarea(-name => 'content',
+ -default => "",
+ -rows => 20,
+ -columns => 80),
+ $q->br,
+ "Optional comment about this change:",
+ $q->br,
+ $q->textfield(-name => "comments", -size => 80),
+ $q->br,
+ $q->submit("Save Page"),
+ $q->end_form,
+ $q->end_html;
+ }
+ elsif ($do eq 'edit') {
+ my $content="";
+ if (exists $pagesources{lc($page)}) {
+ $content=readfile("$srcdir/$pagesources{lc($page)}");
+ $content=~s/\n/\r\n/g;
+ }
+ $q->param("do", "save");
+ print $q->header,
+ $q->start_html("Editing $page"),
+ $q->h1(indexlink()." Editing $page"),
+ $q->start_form(-action => $action),
+ $q->hidden('do'),
+ $q->hidden('page'),
+ $q->textarea(-name => 'content',
+ -default => $content,
+ -rows => 20,
+ -columns => 80),
+ $q->br,
+ "Optional comment about this change:",
+ $q->br,
+ $q->textfield(-name => "comments", -size => 80),
+ $q->br,
+ $q->submit("Save Page"),
+ $q->end_form,
+ $q->end_html;
+ }
+ elsif ($do eq 'save') {
+ my $file=$page.$default_pagetype;
+ my $newfile=1;
+ if (exists $pagesources{lc($page)}) {
+ $file=$pagesources{lc($page)};
+ $newfile=0;
+ }
+
+ my $content=$q->param('content');
+ $content=~s/\r\n/\n/g;
+ $content=~s/\r/\n/g;
+ writefile("$srcdir/$file", $content);
+
+ my $message="web commit from $ENV{REMOTE_ADDR}";
+ if (defined $q->param('comments')) {
+ $message.=": ".$q->param('comments');
+ }
+
+ if ($svn) {
+ if ($newfile) {
+ rcs_add($file);
+ }
+ # presumably the commit will trigger an update
+ # of the wiki
+ rcs_commit($message);
+ }
+ else {
+ refresh();
+ }
+
+ print $q->redirect("$url/".htmlpage($page));
+ }
+ else {
+ error("unknown do parameter");
+ }
+} #}}}
+
+# main {{{
+my $rebuild=0;
+my $wrapper=0;
+if (grep /^-/, @ARGV) {
+ eval {use Getopt::Long};
+ GetOptions(
+ "wikiname=s" => \$wikiname,
+ "verbose|v" => \$verbose,
+ "rebuild" => \$rebuild,
+ "wrapper" => \$wrapper,
+ "svn!" => \$svn,
+ "cgi" => \$cgi,
+ "url=s" => \$url,
+ "cgiurl=s" => \$cgiurl,
+ "historyurl=s" => \$historyurl,
+ ) || usage();