8 sub page_locked ($$;$) { #{{{
13 my $user=$session->param("name");
14 return if length $user && is_admin($user);
16 foreach my $admin (@{$config{adminuser}}) {
17 my $locked_pages=userinfo_get($admin, "locked_pages");
18 if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
19 return 1 if $nonfatal;
20 error(htmllink("", $page, 1)." is locked by ".
21 htmllink("", $admin, 1)." and cannot be edited.");
28 sub cgi_recentchanges ($) { #{{{
33 my $template=HTML::Template->new(
34 filename => "$config{templatedir}/recentchanges.tmpl"
37 title => "RecentChanges",
38 indexlink => indexlink(),
39 wikiname => $config{wikiname},
40 changelog => [rcs_recentchanges(100)],
41 styleurl => styleurl(),
43 print $q->header, $template->output;
46 sub cgi_signin ($$) { #{{{
50 eval q{use CGI::FormBuilder};
51 my $form = CGI::FormBuilder->new(
53 fields => [qw(do title page subpage from name password confirm_password email)],
58 perl => q{eq $form->field("password")},
65 action => $q->request_uri,
67 template => (-e "$config{templatedir}/signin.tmpl" ?
68 "$config{templatedir}/signin.tmpl" : ""),
69 stylesheet => styleurl(),
72 $form->field(name => "name", required => 0);
73 $form->field(name => "do", type => "hidden");
74 $form->field(name => "page", type => "hidden");
75 $form->field(name => "title", type => "hidden");
76 $form->field(name => "from", type => "hidden");
77 $form->field(name => "subpage", type => "hidden");
78 $form->field(name => "password", type => "password", required => 0);
79 $form->field(name => "confirm_password", type => "password", required => 0);
80 $form->field(name => "email", required => 0);
81 if ($q->param("do") ne "signin") {
82 $form->text("You need to log in first.");
85 if ($form->submitted) {
86 # Set required fields based on how form was submitted.
88 "Login" => [qw(name password)],
89 "Register" => [qw(name password confirm_password email)],
90 "Mail Password" => [qw(name)],
92 foreach my $opt (@{$required{$form->submitted}}) {
93 $form->field(name => $opt, required => 1);
96 # Validate password differently depending on how
98 if ($form->submitted eq 'Login') {
102 length $form->field("name") &&
103 shift eq userinfo_get($form->field("name"), 'password');
106 $form->field(name => "name", validate => '/^\w+$/');
109 $form->field(name => "password", validate => 'VALUE');
111 # And make sure the entered name exists when logging
112 # in or sending email, and does not when registering.
113 if ($form->submitted eq 'Register') {
119 ! userinfo_get($name, "regdate");
129 userinfo_get($name, "regdate");
135 # First time settings.
136 $form->field(name => "name", comment => "use FirstnameLastName");
137 $form->field(name => "confirm_password", comment => "(only needed");
138 $form->field(name => "email", comment => "for registration)");
139 if ($session->param("name")) {
140 $form->field(name => "name", value => $session->param("name"));
144 if ($form->submitted && $form->validate) {
145 if ($form->submitted eq 'Login') {
146 $session->param("name", $form->field("name"));
147 if (defined $form->field("do") &&
148 $form->field("do") ne 'signin') {
149 print $q->redirect(cgiurl(
150 do => $form->field("do"),
151 page => $form->field("page"),
152 title => $form->field("title"),
153 subpage => $form->field("subpage"),
154 from => $form->field("from"),
158 print $q->redirect($config{url});
161 elsif ($form->submitted eq 'Register') {
162 my $user_name=$form->field('name');
163 if (userinfo_setall($user_name, {
164 'email' => $form->field('email'),
165 'password' => $form->field('password'),
168 $form->field(name => "confirm_password", type => "hidden");
169 $form->field(name => "email", type => "hidden");
170 $form->text("Registration successful. Now you can Login.");
171 print $session->header();
172 print misctemplate($form->title, $form->render(submit => ["Login"]));
175 error("Error saving registration.");
178 elsif ($form->submitted eq 'Mail Password') {
179 my $user_name=$form->field("name");
180 my $template=HTML::Template->new(
181 filename => "$config{templatedir}/passwordmail.tmpl"
184 user_name => $user_name,
185 user_password => userinfo_get($user_name, "password"),
186 wikiurl => $config{url},
187 wikiname => $config{wikiname},
188 REMOTE_ADDR => $ENV{REMOTE_ADDR},
191 eval q{use Mail::Sendmail};
192 my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
194 To => userinfo_get($user_name, "email"),
195 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
196 Subject => "$config{wikiname} information",
197 Message => $template->output,
198 ) or error("Failed to send mail");
200 $form->text("Your password has been emailed to you.");
201 $form->field(name => "name", required => 0);
202 print $session->header();
203 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
207 print $session->header();
208 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
212 sub cgi_prefs ($$) { #{{{
216 eval q{use CGI::FormBuilder};
217 my $form = CGI::FormBuilder->new(
218 title => "preferences",
219 fields => [qw(do name password confirm_password email locked_pages)],
223 confirm_password => {
224 perl => q{eq $form->field("password")},
231 action => $q->request_uri,
232 template => (-e "$config{templatedir}/prefs.tmpl" ?
233 "$config{templatedir}/prefs.tmpl" : ""),
234 stylesheet => styleurl(),
236 my @buttons=("Save Preferences", "Logout", "Cancel");
238 my $user_name=$session->param("name");
239 $form->field(name => "do", type => "hidden");
240 $form->field(name => "name", disabled => 1,
241 value => $user_name, force => 1);
242 $form->field(name => "password", type => "password");
243 $form->field(name => "confirm_password", type => "password");
244 $form->field(name => "locked_pages", size => 50,
245 comment => "(".htmllink("", "GlobList", 1).")");
247 if (! is_admin($user_name)) {
248 $form->field(name => "locked_pages", type => "hidden");
251 if (! $form->submitted) {
252 $form->field(name => "email", force => 1,
253 value => userinfo_get($user_name, "email"));
254 $form->field(name => "locked_pages", force => 1,
255 value => userinfo_get($user_name, "locked_pages"));
258 if ($form->submitted eq 'Logout') {
260 print $q->redirect($config{url});
263 elsif ($form->submitted eq 'Cancel') {
264 print $q->redirect($config{url});
267 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
268 foreach my $field (qw(password email locked_pages)) {
269 if (length $form->field($field)) {
270 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
273 $form->text("Preferences saved.");
276 print $session->header();
277 print misctemplate($form->title, $form->render(submit => \@buttons));
280 sub cgi_editpage ($$) { #{{{
284 eval q{use CGI::FormBuilder};
285 my $form = CGI::FormBuilder->new(
286 fields => [qw(do rcsinfo subpage from page content comments)],
292 required => [qw{content}],
295 action => $q->request_uri,
297 template => "$config{templatedir}/editpage.tmpl"
299 my @buttons=("Save Page", "Preview", "Cancel");
301 # This untaint is safe because titlepage removes any problimatic
303 my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
304 if (! defined $page || ! length $page ||
305 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
306 error("bad page name");
310 my $file=$page.$config{default_pageext};
311 if (exists $pagesources{lc($page)}) {
312 $file=$pagesources{lc($page)};
315 if (! -e "$config{srcdir}/$file") {
319 $form->field(name => "do", type => 'hidden');
320 $form->field(name => "from", type => 'hidden');
321 $form->field(name => "rcsinfo", type => 'hidden');
322 $form->field(name => "subpage", type => 'hidden');
323 $form->field(name => "page", value => "$page", force => 1);
324 $form->field(name => "comments", type => "text", size => 80);
325 $form->field(name => "content", type => "textarea", rows => 20,
327 $form->tmpl_param("can_commit", $config{rcs});
328 $form->tmpl_param("indexlink", indexlink());
329 $form->tmpl_param("helponformattinglink",
330 htmllink("", "HelpOnFormatting", 1));
331 $form->tmpl_param("styleurl", styleurl());
332 if (! $form->submitted) {
333 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
337 if ($form->submitted eq "Cancel") {
338 print $q->redirect("$config{url}/".htmlpage($page));
341 elsif ($form->submitted eq "Preview") {
342 require IkiWiki::Render;
343 $form->tmpl_param("page_preview",
344 htmlize($config{default_pageext},
345 linkify($form->field('content'), $page)));
348 $form->tmpl_param("page_preview", "");
350 $form->tmpl_param("page_conflict", "");
352 if (! $form->submitted || $form->submitted eq "Preview" ||
354 if ($form->field("do") eq "create") {
355 if (exists $pagesources{lc($page)}) {
356 # hmm, someone else made the page in the
358 print $q->redirect("$config{url}/".htmlpage($page));
364 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
365 if (! defined $from || ! length $from ||
366 $from ne $form->param('from') ||
367 $from=~/$config{wiki_file_prune_regexp}/ ||
369 $form->submitted eq "Preview") {
370 @page_locs=$best_loc=$page;
376 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
377 $page eq 'discussion') {
378 $best_loc="$from/$page";
381 $best_loc=$dir.$page;
384 push @page_locs, $dir.$page;
385 push @page_locs, "$from/$page";
386 while (length $dir) {
388 push @page_locs, $dir.$page;
392 ! exists $pagesources{lc($_)} &&
393 ! page_locked($_, $session, 1)
397 $form->tmpl_param("page_select", 1);
398 $form->field(name => "page", type => 'select',
399 options => \@page_locs, value => $best_loc);
400 $form->title("creating ".pagetitle($page));
402 elsif ($form->field("do") eq "edit") {
403 page_locked($page, $session);
404 if (! defined $form->field('content') ||
405 ! length $form->field('content')) {
407 if (exists $pagesources{lc($page)}) {
408 $content=readfile(srcfile($pagesources{lc($page)}));
409 $content=~s/\n/\r\n/g;
411 $form->field(name => "content", value => $content,
414 $form->tmpl_param("page_select", 0);
415 $form->field(name => "page", type => 'hidden');
416 $form->title("editing ".pagetitle($page));
419 print $form->render(submit => \@buttons);
423 page_locked($page, $session);
425 my $content=$form->field('content');
426 $content=~s/\r\n/\n/g;
428 writefile($file, $config{srcdir}, $content);
430 my $message="web commit ";
431 if (length $session->param("name")) {
432 $message.="by ".$session->param("name");
435 $message.="from $ENV{REMOTE_ADDR}";
437 if (defined $form->field('comments') &&
438 length $form->field('comments')) {
439 $message.=": ".$form->field('comments');
446 # prevent deadlock with post-commit hook
448 # presumably the commit will trigger an update
450 my $conflict=rcs_commit($file, $message,
451 $form->field("rcsinfo"));
453 if (defined $conflict) {
454 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
456 $form->tmpl_param("page_conflict", 1);
457 $form->field("content", value => $conflict, force => 1);
458 $form->field("do", "edit)");
459 $form->tmpl_param("page_select", 0);
460 $form->field(name => "page", type => 'hidden');
461 $form->title("editing $page");
462 print $form->render(submit => \@buttons);
467 require IkiWiki::Render;
472 # The trailing question mark tries to avoid broken
473 # caches and get the most recent version of the page.
474 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
478 sub cgi_hyperestraier () { #{{{
479 # only works for GET requests
480 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
481 exec("./".basename($config{cgiurl})) || error("estseek.cgi failed");
486 eval q{use CGI::Session};
490 if (defined $q->param('phrase')) {
494 my $do=$q->param('do');
495 if (! defined $do || ! length $do) {
496 error("\"do\" parameter missing");
499 # Things that do not need a session.
500 if ($do eq 'recentchanges') {
501 cgi_recentchanges($q);
504 elsif ($do eq 'hyperestraier') {
508 CGI::Session->name("ikiwiki_session_$config{wikiname}");
510 my $oldmask=umask(077);
511 my $session = CGI::Session->new("driver:db_file", $q,
512 { FileName => "$config{wikistatedir}/sessions.db" });
515 # Everything below this point needs the user to be signed in.
516 if ((! $config{anonok} && ! defined $session->param("name") ||
517 ! defined $session->param("name") ||
518 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
519 cgi_signin($q, $session);
521 # Force session flush with safe umask.
522 my $oldmask=umask(077);
529 if ($do eq 'create' || $do eq 'edit') {
530 cgi_editpage($q, $session);
532 elsif ($do eq 'prefs') {
533 cgi_prefs($q, $session);
535 elsif ($do eq 'blog') {
536 my $page=titlepage(lc($q->param('title')));
537 # if the page already exists, munge it to be unique
538 my $from=$q->param('from');
540 while (exists $oldpagemtime{"$from/$page$add"}) {
541 $add=1 unless length $add;
544 $q->param('page', $page.$add);
545 # now run same as create
546 $q->param('do', 'create');
547 cgi_editpage($q, $session);
550 error("unknown do parameter");