10 sub page_locked ($$;$) { #{{{
15 my $user=$session->param("name");
16 return if defined $user && is_admin($user);
18 foreach my $admin (@{$config{adminuser}}) {
19 my $locked_pages=userinfo_get($admin, "locked_pages");
20 if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
21 return 1 if $nonfatal;
22 error(htmllink("", $page, 1)." is locked by ".
23 htmllink("", $admin, 1)." and cannot be edited.");
30 sub cgi_recentchanges ($) { #{{{
35 my $template=HTML::Template->new(
36 filename => "$config{templatedir}/recentchanges.tmpl"
39 title => "RecentChanges",
40 indexlink => indexlink(),
41 wikiname => $config{wikiname},
42 changelog => [rcs_recentchanges(100)],
43 styleurl => styleurl(),
44 baseurl => "$config{url}/",
46 print $q->header, $template->output;
49 sub cgi_signin ($$) { #{{{
53 eval q{use CGI::FormBuilder};
54 my $form = CGI::FormBuilder->new(
56 fields => [qw(do title page subpage from name password confirm_password email)],
61 perl => q{eq $form->field("password")},
68 action => $config{cgiurl},
70 template => (-e "$config{templatedir}/signin.tmpl" ?
71 "$config{templatedir}/signin.tmpl" : ""),
72 stylesheet => styleurl(),
75 $form->field(name => "name", required => 0);
76 $form->field(name => "do", type => "hidden");
77 $form->field(name => "page", type => "hidden");
78 $form->field(name => "title", type => "hidden");
79 $form->field(name => "from", type => "hidden");
80 $form->field(name => "subpage", type => "hidden");
81 $form->field(name => "password", type => "password", required => 0);
82 $form->field(name => "confirm_password", type => "password", required => 0);
83 $form->field(name => "email", required => 0);
84 if ($q->param("do") ne "signin") {
85 $form->text("You need to log in first.");
88 if ($form->submitted) {
89 # Set required fields based on how form was submitted.
91 "Login" => [qw(name password)],
92 "Register" => [qw(name password confirm_password email)],
93 "Mail Password" => [qw(name)],
95 foreach my $opt (@{$required{$form->submitted}}) {
96 $form->field(name => $opt, required => 1);
99 # Validate password differently depending on how
100 # form was submitted.
101 if ($form->submitted eq 'Login') {
105 length $form->field("name") &&
106 shift eq userinfo_get($form->field("name"), 'password');
109 $form->field(name => "name", validate => '/^\w+$/');
112 $form->field(name => "password", validate => 'VALUE');
114 # And make sure the entered name exists when logging
115 # in or sending email, and does not when registering.
116 if ($form->submitted eq 'Register') {
122 $name=~/$config{wiki_file_regexp}/ &&
123 ! userinfo_get($name, "regdate");
133 userinfo_get($name, "regdate");
139 # First time settings.
140 $form->field(name => "name", comment => "use FirstnameLastName");
141 $form->field(name => "confirm_password", comment => "(only needed");
142 $form->field(name => "email", comment => "for registration)");
143 if ($session->param("name")) {
144 $form->field(name => "name", value => $session->param("name"));
148 if ($form->submitted && $form->validate) {
149 if ($form->submitted eq 'Login') {
150 $session->param("name", $form->field("name"));
151 if (defined $form->field("do") &&
152 $form->field("do") ne 'signin') {
153 print $q->redirect(cgiurl(
154 do => $form->field("do"),
155 page => $form->field("page"),
156 title => $form->field("title"),
157 subpage => $form->field("subpage"),
158 from => $form->field("from"),
162 print $q->redirect($config{url});
165 elsif ($form->submitted eq 'Register') {
166 my $user_name=$form->field('name');
167 if (userinfo_setall($user_name, {
168 'email' => $form->field('email'),
169 'password' => $form->field('password'),
172 $form->field(name => "confirm_password", type => "hidden");
173 $form->field(name => "email", type => "hidden");
174 $form->text("Registration successful. Now you can Login.");
175 print $session->header();
176 print misctemplate($form->title, $form->render(submit => ["Login"]));
179 error("Error saving registration.");
182 elsif ($form->submitted eq 'Mail Password') {
183 my $user_name=$form->field("name");
184 my $template=HTML::Template->new(
185 filename => "$config{templatedir}/passwordmail.tmpl"
188 user_name => $user_name,
189 user_password => userinfo_get($user_name, "password"),
190 wikiurl => $config{url},
191 wikiname => $config{wikiname},
192 REMOTE_ADDR => $ENV{REMOTE_ADDR},
195 eval q{use Mail::Sendmail};
197 To => userinfo_get($user_name, "email"),
198 From => "$config{wikiname} admin <$config{adminemail}>",
199 Subject => "$config{wikiname} information",
200 Message => $template->output,
201 ) or error("Failed to send mail");
203 $form->text("Your password has been emailed to you.");
204 $form->field(name => "name", required => 0);
205 print $session->header();
206 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
210 print $session->header();
211 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
215 sub cgi_prefs ($$) { #{{{
219 eval q{use CGI::FormBuilder};
220 my $form = CGI::FormBuilder->new(
221 title => "preferences",
222 fields => [qw(do name password confirm_password email
223 subscriptions locked_pages)],
227 confirm_password => {
228 perl => q{eq $form->field("password")},
235 action => $config{cgiurl},
236 template => (-e "$config{templatedir}/prefs.tmpl" ?
237 "$config{templatedir}/prefs.tmpl" : ""),
238 stylesheet => styleurl(),
240 my @buttons=("Save Preferences", "Logout", "Cancel");
242 my $user_name=$session->param("name");
243 $form->field(name => "do", type => "hidden");
244 $form->field(name => "name", disabled => 1,
245 value => $user_name, force => 1);
246 $form->field(name => "password", type => "password");
247 $form->field(name => "confirm_password", type => "password");
248 $form->field(name => "subscriptions", size => 50,
249 comment => "(".htmllink("", "GlobList", 1).")");
250 $form->field(name => "locked_pages", size => 50,
251 comment => "(".htmllink("", "GlobList", 1).")");
253 if (! is_admin($user_name)) {
254 $form->field(name => "locked_pages", type => "hidden");
257 if (! $form->submitted) {
258 $form->field(name => "email", force => 1,
259 value => userinfo_get($user_name, "email"));
260 $form->field(name => "subscriptions", force => 1,
261 value => userinfo_get($user_name, "subscriptions"));
262 $form->field(name => "locked_pages", force => 1,
263 value => userinfo_get($user_name, "locked_pages"));
266 if ($form->submitted eq 'Logout') {
268 print $q->redirect($config{url});
271 elsif ($form->submitted eq 'Cancel') {
272 print $q->redirect($config{url});
275 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
276 foreach my $field (qw(password email subscriptions locked_pages)) {
277 if (length $form->field($field)) {
278 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
281 $form->text("Preferences saved.");
284 print $session->header();
285 print misctemplate($form->title, $form->render(submit => \@buttons));
288 sub cgi_editpage ($$) { #{{{
292 eval q{use CGI::FormBuilder};
293 my $form = CGI::FormBuilder->new(
294 fields => [qw(do rcsinfo subpage from page content comments)],
300 required => [qw{content}],
303 action => $config{cgiurl},
305 template => "$config{templatedir}/editpage.tmpl"
307 my @buttons=("Save Page", "Preview", "Cancel");
309 # This untaint is safe because titlepage removes any problimatic
311 my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
312 if (! defined $page || ! length $page ||
313 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
314 error("bad page name");
318 my $file=$page.$config{default_pageext};
319 if (exists $pagesources{lc($page)}) {
320 $file=$pagesources{lc($page)};
323 if (! -e "$config{srcdir}/$file") {
327 $form->field(name => "do", type => 'hidden');
328 $form->field(name => "from", type => 'hidden');
329 $form->field(name => "rcsinfo", type => 'hidden');
330 $form->field(name => "subpage", type => 'hidden');
331 $form->field(name => "page", value => "$page", force => 1);
332 $form->field(name => "comments", type => "text", size => 80);
333 $form->field(name => "content", type => "textarea", rows => 20,
335 $form->tmpl_param("can_commit", $config{rcs});
336 $form->tmpl_param("indexlink", indexlink());
337 $form->tmpl_param("helponformattinglink",
338 htmllink("", "HelpOnFormatting", 1));
339 $form->tmpl_param("styleurl", styleurl());
340 $form->tmpl_param("baseurl", "$config{url}/");
341 if (! $form->submitted) {
342 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
346 if ($form->submitted eq "Cancel") {
347 print $q->redirect("$config{url}/".htmlpage($page));
350 elsif ($form->submitted eq "Preview") {
351 require IkiWiki::Render;
352 $form->tmpl_param("page_preview",
353 htmlize($config{default_pageext},
354 linkify($form->field('content'), $page)));
357 $form->tmpl_param("page_preview", "");
359 $form->tmpl_param("page_conflict", "");
361 if (! $form->submitted || $form->submitted eq "Preview" ||
363 if ($form->field("do") eq "create") {
366 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
367 if (! defined $from || ! length $from ||
368 $from ne $form->param('from') ||
369 $from=~/$config{wiki_file_prune_regexp}/ ||
371 $form->submitted eq "Preview") {
372 @page_locs=$best_loc=$page;
378 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
379 $page eq 'discussion') {
380 $best_loc="$from/$page";
383 $best_loc=$dir.$page;
386 push @page_locs, $dir.$page;
387 push @page_locs, "$from/$page";
388 while (length $dir) {
390 push @page_locs, $dir.$page;
395 ! exists $pagesources{lc($_)} &&
396 ! page_locked($_, $session, 1)
400 # hmm, someone else made the page in the
402 print $q->redirect("$config{url}/".htmlpage($page));
406 $form->tmpl_param("page_select", 1);
407 $form->field(name => "page", type => 'select',
408 options => \@page_locs, value => $best_loc);
409 $form->title("creating ".pagetitle($page));
411 elsif ($form->field("do") eq "edit") {
412 page_locked($page, $session);
413 if (! defined $form->field('content') ||
414 ! length $form->field('content')) {
416 if (exists $pagesources{lc($page)}) {
417 $content=readfile(srcfile($pagesources{lc($page)}));
418 $content=~s/\n/\r\n/g;
420 $form->field(name => "content", value => $content,
423 $form->tmpl_param("page_select", 0);
424 $form->field(name => "page", type => 'hidden');
425 $form->title("editing ".pagetitle($page));
428 print $form->render(submit => \@buttons);
432 page_locked($page, $session);
434 my $content=$form->field('content');
435 $content=~s/\r\n/\n/g;
437 writefile($file, $config{srcdir}, $content);
439 my $message="web commit ";
440 if (length $session->param("name")) {
441 $message.="by ".$session->param("name");
444 $message.="from $ENV{REMOTE_ADDR}";
446 if (defined $form->field('comments') &&
447 length $form->field('comments')) {
448 $message.=": ".$form->field('comments');
455 # prevent deadlock with post-commit hook
457 # presumably the commit will trigger an update
459 my $conflict=rcs_commit($file, $message,
460 $form->field("rcsinfo"));
462 if (defined $conflict) {
463 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
465 $form->tmpl_param("page_conflict", 1);
466 $form->field("content", value => $conflict, force => 1);
467 $form->field("do", "edit)");
468 $form->tmpl_param("page_select", 0);
469 $form->field(name => "page", type => 'hidden');
470 $form->title("editing $page");
471 print $form->render(submit => \@buttons);
476 require IkiWiki::Render;
481 # The trailing question mark tries to avoid broken
482 # caches and get the most recent version of the page.
483 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
487 sub cgi_hyperestraier () { #{{{
488 # only works for GET requests
489 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
490 exec("./".basename($config{cgiurl})) || error("estseek.cgi failed");
495 eval q{use CGI::Session};
499 my $do=$q->param('do');
500 if (! defined $do || ! length $do) {
501 if (defined $q->param('phrase')) {
505 error("\"do\" parameter missing");
509 # Things that do not need a session.
510 if ($do eq 'recentchanges') {
511 cgi_recentchanges($q);
514 elsif ($do eq 'hyperestraier') {
518 CGI::Session->name("ikiwiki_session_$config{wikiname}");
520 my $oldmask=umask(077);
521 my $session = CGI::Session->new("driver:DB_File", $q,
522 { FileName => "$config{wikistatedir}/sessions.db" });
525 # Everything below this point needs the user to be signed in.
526 if ((! $config{anonok} &&
527 (! defined $session->param("name") ||
528 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
529 cgi_signin($q, $session);
531 # Force session flush with safe umask.
532 my $oldmask=umask(077);
539 if ($do eq 'create' || $do eq 'edit') {
540 cgi_editpage($q, $session);
542 elsif ($do eq 'prefs') {
543 cgi_prefs($q, $session);
545 elsif ($do eq 'blog') {
546 my $page=titlepage(lc($q->param('title')));
547 # if the page already exists, munge it to be unique
548 my $from=$q->param('from');
550 while (exists $oldpagemtime{"$from/$page$add"}) {
551 $add=1 unless length $add;
554 $q->param('page', $page.$add);
555 # now run same as create
556 $q->param('do', 'create');
557 cgi_editpage($q, $session);
560 error("unknown do parameter");