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(),
42 baseurl => "$config{url}/",
44 print $q->header, $template->output;
47 sub cgi_signin ($$) { #{{{
51 eval q{use CGI::FormBuilder};
52 my $form = CGI::FormBuilder->new(
54 fields => [qw(do title page subpage from name password confirm_password email)],
59 perl => q{eq $form->field("password")},
66 action => $config{cgiurl},
68 template => (-e "$config{templatedir}/signin.tmpl" ?
69 "$config{templatedir}/signin.tmpl" : ""),
70 stylesheet => styleurl(),
73 $form->field(name => "name", required => 0);
74 $form->field(name => "do", type => "hidden");
75 $form->field(name => "page", type => "hidden");
76 $form->field(name => "title", type => "hidden");
77 $form->field(name => "from", type => "hidden");
78 $form->field(name => "subpage", type => "hidden");
79 $form->field(name => "password", type => "password", required => 0);
80 $form->field(name => "confirm_password", type => "password", required => 0);
81 $form->field(name => "email", required => 0);
82 if ($q->param("do") ne "signin") {
83 $form->text("You need to log in first.");
86 if ($form->submitted) {
87 # Set required fields based on how form was submitted.
89 "Login" => [qw(name password)],
90 "Register" => [qw(name password confirm_password email)],
91 "Mail Password" => [qw(name)],
93 foreach my $opt (@{$required{$form->submitted}}) {
94 $form->field(name => $opt, required => 1);
97 # Validate password differently depending on how
99 if ($form->submitted eq 'Login') {
103 length $form->field("name") &&
104 shift eq userinfo_get($form->field("name"), 'password');
107 $form->field(name => "name", validate => '/^\w+$/');
110 $form->field(name => "password", validate => 'VALUE');
112 # And make sure the entered name exists when logging
113 # in or sending email, and does not when registering.
114 if ($form->submitted eq 'Register') {
120 ! userinfo_get($name, "regdate");
130 userinfo_get($name, "regdate");
136 # First time settings.
137 $form->field(name => "name", comment => "use FirstnameLastName");
138 $form->field(name => "confirm_password", comment => "(only needed");
139 $form->field(name => "email", comment => "for registration)");
140 if ($session->param("name")) {
141 $form->field(name => "name", value => $session->param("name"));
145 if ($form->submitted && $form->validate) {
146 if ($form->submitted eq 'Login') {
147 $session->param("name", $form->field("name"));
148 if (defined $form->field("do") &&
149 $form->field("do") ne 'signin') {
150 print $q->redirect(cgiurl(
151 do => $form->field("do"),
152 page => $form->field("page"),
153 title => $form->field("title"),
154 subpage => $form->field("subpage"),
155 from => $form->field("from"),
159 print $q->redirect($config{url});
162 elsif ($form->submitted eq 'Register') {
163 my $user_name=$form->field('name');
164 if (userinfo_setall($user_name, {
165 'email' => $form->field('email'),
166 'password' => $form->field('password'),
169 $form->field(name => "confirm_password", type => "hidden");
170 $form->field(name => "email", type => "hidden");
171 $form->text("Registration successful. Now you can Login.");
172 print $session->header();
173 print misctemplate($form->title, $form->render(submit => ["Login"]));
176 error("Error saving registration.");
179 elsif ($form->submitted eq 'Mail Password') {
180 my $user_name=$form->field("name");
181 my $template=HTML::Template->new(
182 filename => "$config{templatedir}/passwordmail.tmpl"
185 user_name => $user_name,
186 user_password => userinfo_get($user_name, "password"),
187 wikiurl => $config{url},
188 wikiname => $config{wikiname},
189 REMOTE_ADDR => $ENV{REMOTE_ADDR},
192 eval q{use Mail::Sendmail};
193 my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
195 To => userinfo_get($user_name, "email"),
196 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
197 Subject => "$config{wikiname} information",
198 Message => $template->output,
199 ) or error("Failed to send mail");
201 $form->text("Your password has been emailed to you.");
202 $form->field(name => "name", required => 0);
203 print $session->header();
204 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
208 print $session->header();
209 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
213 sub cgi_prefs ($$) { #{{{
217 eval q{use CGI::FormBuilder};
218 my $form = CGI::FormBuilder->new(
219 title => "preferences",
220 fields => [qw(do name password confirm_password email locked_pages)],
224 confirm_password => {
225 perl => q{eq $form->field("password")},
232 action => $config{cgiurl},
233 template => (-e "$config{templatedir}/prefs.tmpl" ?
234 "$config{templatedir}/prefs.tmpl" : ""),
235 stylesheet => styleurl(),
237 my @buttons=("Save Preferences", "Logout", "Cancel");
239 my $user_name=$session->param("name");
240 $form->field(name => "do", type => "hidden");
241 $form->field(name => "name", disabled => 1,
242 value => $user_name, force => 1);
243 $form->field(name => "password", type => "password");
244 $form->field(name => "confirm_password", type => "password");
245 $form->field(name => "locked_pages", size => 50,
246 comment => "(".htmllink("", "GlobList", 1).")");
248 if (! is_admin($user_name)) {
249 $form->field(name => "locked_pages", type => "hidden");
252 if (! $form->submitted) {
253 $form->field(name => "email", force => 1,
254 value => userinfo_get($user_name, "email"));
255 $form->field(name => "locked_pages", force => 1,
256 value => userinfo_get($user_name, "locked_pages"));
259 if ($form->submitted eq 'Logout') {
261 print $q->redirect($config{url});
264 elsif ($form->submitted eq 'Cancel') {
265 print $q->redirect($config{url});
268 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
269 foreach my $field (qw(password email locked_pages)) {
270 if (length $form->field($field)) {
271 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
274 $form->text("Preferences saved.");
277 print $session->header();
278 print misctemplate($form->title, $form->render(submit => \@buttons));
281 sub cgi_editpage ($$) { #{{{
285 eval q{use CGI::FormBuilder};
286 my $form = CGI::FormBuilder->new(
287 fields => [qw(do rcsinfo subpage from page content comments)],
293 required => [qw{content}],
296 action => $config{cgiurl},
298 template => "$config{templatedir}/editpage.tmpl"
300 my @buttons=("Save Page", "Preview", "Cancel");
302 # This untaint is safe because titlepage removes any problimatic
304 my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
305 if (! defined $page || ! length $page ||
306 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
307 error("bad page name");
311 my $file=$page.$config{default_pageext};
312 if (exists $pagesources{lc($page)}) {
313 $file=$pagesources{lc($page)};
316 if (! -e "$config{srcdir}/$file") {
320 $form->field(name => "do", type => 'hidden');
321 $form->field(name => "from", type => 'hidden');
322 $form->field(name => "rcsinfo", type => 'hidden');
323 $form->field(name => "subpage", type => 'hidden');
324 $form->field(name => "page", value => "$page", force => 1);
325 $form->field(name => "comments", type => "text", size => 80);
326 $form->field(name => "content", type => "textarea", rows => 20,
328 $form->tmpl_param("can_commit", $config{rcs});
329 $form->tmpl_param("indexlink", indexlink());
330 $form->tmpl_param("helponformattinglink",
331 htmllink("", "HelpOnFormatting", 1));
332 $form->tmpl_param("styleurl", styleurl());
333 $form->tmpl_param("baseurl", "$config{url}/");
334 if (! $form->submitted) {
335 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
339 if ($form->submitted eq "Cancel") {
340 print $q->redirect("$config{url}/".htmlpage($page));
343 elsif ($form->submitted eq "Preview") {
344 require IkiWiki::Render;
345 $form->tmpl_param("page_preview",
346 htmlize($config{default_pageext},
347 linkify($form->field('content'), $page)));
350 $form->tmpl_param("page_preview", "");
352 $form->tmpl_param("page_conflict", "");
354 if (! $form->submitted || $form->submitted eq "Preview" ||
356 if ($form->field("do") eq "create") {
357 if (exists $pagesources{lc($page)}) {
358 # hmm, someone else made the page in the
360 print $q->redirect("$config{url}/".htmlpage($page));
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;
394 ! exists $pagesources{lc($_)} &&
395 ! page_locked($_, $session, 1)
399 $form->tmpl_param("page_select", 1);
400 $form->field(name => "page", type => 'select',
401 options => \@page_locs, value => $best_loc);
402 $form->title("creating ".pagetitle($page));
404 elsif ($form->field("do") eq "edit") {
405 page_locked($page, $session);
406 if (! defined $form->field('content') ||
407 ! length $form->field('content')) {
409 if (exists $pagesources{lc($page)}) {
410 $content=readfile(srcfile($pagesources{lc($page)}));
411 $content=~s/\n/\r\n/g;
413 $form->field(name => "content", value => $content,
416 $form->tmpl_param("page_select", 0);
417 $form->field(name => "page", type => 'hidden');
418 $form->title("editing ".pagetitle($page));
421 print $form->render(submit => \@buttons);
425 page_locked($page, $session);
427 my $content=$form->field('content');
428 $content=~s/\r\n/\n/g;
430 writefile($file, $config{srcdir}, $content);
432 my $message="web commit ";
433 if (length $session->param("name")) {
434 $message.="by ".$session->param("name");
437 $message.="from $ENV{REMOTE_ADDR}";
439 if (defined $form->field('comments') &&
440 length $form->field('comments')) {
441 $message.=": ".$form->field('comments');
448 # prevent deadlock with post-commit hook
450 # presumably the commit will trigger an update
452 my $conflict=rcs_commit($file, $message,
453 $form->field("rcsinfo"));
455 if (defined $conflict) {
456 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
458 $form->tmpl_param("page_conflict", 1);
459 $form->field("content", value => $conflict, force => 1);
460 $form->field("do", "edit)");
461 $form->tmpl_param("page_select", 0);
462 $form->field(name => "page", type => 'hidden');
463 $form->title("editing $page");
464 print $form->render(submit => \@buttons);
469 require IkiWiki::Render;
474 # The trailing question mark tries to avoid broken
475 # caches and get the most recent version of the page.
476 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
480 sub cgi_hyperestraier () { #{{{
481 # only works for GET requests
482 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
483 exec("./".basename($config{cgiurl})) || error("estseek.cgi failed");
488 eval q{use CGI::Session};
492 my $do=$q->param('do');
493 if (! defined $do || ! length $do) {
494 if (defined $q->param('phrase')) {
498 error("\"do\" parameter missing");
502 # Things that do not need a session.
503 if ($do eq 'recentchanges') {
504 cgi_recentchanges($q);
507 elsif ($do eq 'hyperestraier') {
511 CGI::Session->name("ikiwiki_session_$config{wikiname}");
513 my $oldmask=umask(077);
514 my $session = CGI::Session->new("driver:DB_File", $q,
515 { FileName => "$config{wikistatedir}/sessions.db" });
518 # Everything below this point needs the user to be signed in.
519 if ((! $config{anonok} &&
520 (! defined $session->param("name") ||
521 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
522 cgi_signin($q, $session);
524 # Force session flush with safe umask.
525 my $oldmask=umask(077);
532 if ($do eq 'create' || $do eq 'edit') {
533 cgi_editpage($q, $session);
535 elsif ($do eq 'prefs') {
536 cgi_prefs($q, $session);
538 elsif ($do eq 'blog') {
539 my $page=titlepage(lc($q->param('title')));
540 # if the page already exists, munge it to be unique
541 my $from=$q->param('from');
543 while (exists $oldpagemtime{"$from/$page$add"}) {
544 $add=1 unless length $add;
547 $q->param('page', $page.$add);
548 # now run same as create
549 $q->param('do', 'create');
550 cgi_editpage($q, $session);
553 error("unknown do parameter");