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)],
42 print $q->header, $template->output;
45 sub cgi_signin ($$) { #{{{
49 eval q{use CGI::FormBuilder};
50 my $form = CGI::FormBuilder->new(
52 fields => [qw(do title page subpage from name password confirm_password email)],
57 perl => q{eq $form->field("password")},
64 action => $q->request_uri,
66 template => (-e "$config{templatedir}/signin.tmpl" ?
67 "$config{templatedir}/signin.tmpl" : "")
70 $form->field(name => "name", required => 0);
71 $form->field(name => "do", type => "hidden");
72 $form->field(name => "page", type => "hidden");
73 $form->field(name => "title", type => "hidden");
74 $form->field(name => "from", type => "hidden");
75 $form->field(name => "subpage", type => "hidden");
76 $form->field(name => "password", type => "password", required => 0);
77 $form->field(name => "confirm_password", type => "password", required => 0);
78 $form->field(name => "email", required => 0);
79 if ($q->param("do") ne "signin") {
80 $form->text("You need to log in first.");
83 if ($form->submitted) {
84 # Set required fields based on how form was submitted.
86 "Login" => [qw(name password)],
87 "Register" => [qw(name password confirm_password email)],
88 "Mail Password" => [qw(name)],
90 foreach my $opt (@{$required{$form->submitted}}) {
91 $form->field(name => $opt, required => 1);
94 # Validate password differently depending on how
96 if ($form->submitted eq 'Login') {
100 length $form->field("name") &&
101 shift eq userinfo_get($form->field("name"), 'password');
104 $form->field(name => "name", validate => '/^\w+$/');
107 $form->field(name => "password", validate => 'VALUE');
109 # And make sure the entered name exists when logging
110 # in or sending email, and does not when registering.
111 if ($form->submitted eq 'Register') {
117 ! userinfo_get($name, "regdate");
127 userinfo_get($name, "regdate");
133 # First time settings.
134 $form->field(name => "name", comment => "use FirstnameLastName");
135 $form->field(name => "confirm_password", comment => "(only needed");
136 $form->field(name => "email", comment => "for registration)");
137 if ($session->param("name")) {
138 $form->field(name => "name", value => $session->param("name"));
142 if ($form->submitted && $form->validate) {
143 if ($form->submitted eq 'Login') {
144 $session->param("name", $form->field("name"));
145 if (defined $form->field("do") &&
146 $form->field("do") ne 'signin') {
148 "$config{cgiurl}?do=".$form->field("do").
149 "&page=".$form->field("page").
150 "&title=".$form->field("title").
151 "&subpage=".$form->field("subpage").
152 "&from=".$form->field("from"));;
155 print $q->redirect($config{url});
158 elsif ($form->submitted eq 'Register') {
159 my $user_name=$form->field('name');
160 if (userinfo_setall($user_name, {
161 'email' => $form->field('email'),
162 'password' => $form->field('password'),
165 $form->field(name => "confirm_password", type => "hidden");
166 $form->field(name => "email", type => "hidden");
167 $form->text("Registration successful. Now you can Login.");
168 print $session->header();
169 print misctemplate($form->title, $form->render(submit => ["Login"]));
172 error("Error saving registration.");
175 elsif ($form->submitted eq 'Mail Password') {
176 my $user_name=$form->field("name");
177 my $template=HTML::Template->new(
178 filename => "$config{templatedir}/passwordmail.tmpl"
181 user_name => $user_name,
182 user_password => userinfo_get($user_name, "password"),
183 wikiurl => $config{url},
184 wikiname => $config{wikiname},
185 REMOTE_ADDR => $ENV{REMOTE_ADDR},
188 eval q{use Mail::Sendmail};
189 my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
191 To => userinfo_get($user_name, "email"),
192 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
193 Subject => "$config{wikiname} information",
194 Message => $template->output,
195 ) or error("Failed to send mail");
197 $form->text("Your password has been emailed to you.");
198 $form->field(name => "name", required => 0);
199 print $session->header();
200 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
204 print $session->header();
205 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
209 sub cgi_prefs ($$) { #{{{
213 eval q{use CGI::FormBuilder};
214 my $form = CGI::FormBuilder->new(
215 title => "preferences",
216 fields => [qw(do name password confirm_password email locked_pages)],
220 confirm_password => {
221 perl => q{eq $form->field("password")},
228 action => $q->request_uri,
229 template => (-e "$config{templatedir}/prefs.tmpl" ?
230 "$config{templatedir}/prefs.tmpl" : "")
232 my @buttons=("Save Preferences", "Logout", "Cancel");
234 my $user_name=$session->param("name");
235 $form->field(name => "do", type => "hidden");
236 $form->field(name => "name", disabled => 1,
237 value => $user_name, force => 1);
238 $form->field(name => "password", type => "password");
239 $form->field(name => "confirm_password", type => "password");
240 $form->field(name => "locked_pages", size => 50,
241 comment => "(".htmllink("", "GlobList", 1).")");
243 if (! is_admin($user_name)) {
244 $form->field(name => "locked_pages", type => "hidden");
247 if (! $form->submitted) {
248 $form->field(name => "email", force => 1,
249 value => userinfo_get($user_name, "email"));
250 $form->field(name => "locked_pages", force => 1,
251 value => userinfo_get($user_name, "locked_pages"));
254 if ($form->submitted eq 'Logout') {
256 print $q->redirect($config{url});
259 elsif ($form->submitted eq 'Cancel') {
260 print $q->redirect($config{url});
263 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
264 foreach my $field (qw(password email locked_pages)) {
265 if (length $form->field($field)) {
266 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
269 $form->text("Preferences saved.");
272 print $session->header();
273 print misctemplate($form->title, $form->render(submit => \@buttons));
276 sub cgi_editpage ($$) { #{{{
280 eval q{use CGI::FormBuilder};
281 my $form = CGI::FormBuilder->new(
282 fields => [qw(do rcsinfo subpage from page content comments)],
288 required => [qw{content}],
291 action => $q->request_uri,
293 template => "$config{templatedir}/editpage.tmpl"
295 my @buttons=("Save Page", "Preview", "Cancel");
297 my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
298 if (! defined $page || ! length $page || $page ne $q->param('page') ||
299 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
300 error("bad page name");
304 my $file=$page.$config{default_pageext};
306 if (exists $pagesources{lc($page)}) {
307 $file=$pagesources{lc($page)};
311 $form->field(name => "do", type => 'hidden');
312 $form->field(name => "from", type => 'hidden');
313 $form->field(name => "rcsinfo", type => 'hidden');
314 $form->field(name => "subpage", type => 'hidden');
315 $form->field(name => "page", value => "$page", force => 1);
316 $form->field(name => "comments", type => "text", size => 80);
317 $form->field(name => "content", type => "textarea", rows => 20,
319 $form->tmpl_param("can_commit", $config{rcs});
320 $form->tmpl_param("indexlink", indexlink());
321 $form->tmpl_param("helponformattinglink",
322 htmllink("", "HelpOnFormatting", 1));
323 if (! $form->submitted) {
324 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
328 if ($form->submitted eq "Cancel") {
329 print $q->redirect("$config{url}/".htmlpage($page));
332 elsif ($form->submitted eq "Preview") {
333 require IkiWiki::Render;
334 $form->tmpl_param("page_preview",
335 htmlize($config{default_pageext},
336 linkify($form->field('content'), $page)));
339 $form->tmpl_param("page_preview", "");
341 $form->tmpl_param("page_conflict", "");
343 if (! $form->submitted || $form->submitted eq "Preview" ||
345 if ($form->field("do") eq "create") {
346 if (exists $pagesources{lc($page)}) {
347 # hmm, someone else made the page in the
349 print $q->redirect("$config{url}/".htmlpage($page));
355 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
356 if (! defined $from || ! length $from ||
357 $from ne $form->param('from') ||
358 $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
359 @page_locs=$best_loc=$page;
365 if (length $form->param('subpage') ||
366 $page eq 'discussion') {
367 $best_loc="$from/$page";
370 $best_loc=$dir.$page;
373 push @page_locs, $dir.$page;
374 push @page_locs, "$from/$page";
375 while (length $dir) {
377 push @page_locs, $dir.$page;
381 ! exists $pagesources{lc($_)} &&
382 ! page_locked($_, $session, 1)
386 $form->tmpl_param("page_select", 1);
387 $form->field(name => "page", type => 'select',
388 options => \@page_locs, value => $best_loc);
389 $form->title("creating ".pagetitle($page));
391 elsif ($form->field("do") eq "edit") {
392 page_locked($page, $session);
393 if (! defined $form->field('content') ||
394 ! length $form->field('content')) {
396 if (exists $pagesources{lc($page)}) {
397 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
398 $content=~s/\n/\r\n/g;
400 $form->field(name => "content", value => $content,
403 $form->tmpl_param("page_select", 0);
404 $form->field(name => "page", type => 'hidden');
405 $form->title("editing ".pagetitle($page));
408 print $form->render(submit => \@buttons);
412 page_locked($page, $session);
414 my $content=$form->field('content');
415 $content=~s/\r\n/\n/g;
417 writefile("$config{srcdir}/$file", $content);
419 my $message="web commit ";
420 if (length $session->param("name")) {
421 $message.="by ".$session->param("name");
424 $message.="from $ENV{REMOTE_ADDR}";
426 if (defined $form->field('comments') &&
427 length $form->field('comments')) {
428 $message.=": ".$form->field('comments');
435 # prevent deadlock with post-commit hook
437 # presumably the commit will trigger an update
439 my $conflict=rcs_commit($file, $message,
440 $form->field("rcsinfo"));
442 if (defined $conflict) {
443 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
445 $form->tmpl_param("page_conflict", 1);
446 $form->field("content", value => $conflict, force => 1);
447 $form->field("do", "edit)");
448 $form->tmpl_param("page_select", 0);
449 $form->field(name => "page", type => 'hidden');
450 $form->title("editing $page");
451 print $form->render(submit => \@buttons);
456 require IkiWiki::Render;
461 # The trailing question mark tries to avoid broken
462 # caches and get the most recent version of the page.
463 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
469 eval q{use CGI::Session};
473 my $do=$q->param('do');
474 if (! defined $do || ! length $do) {
475 error("\"do\" parameter missing");
478 # Things that do not need a session.
479 if ($do eq 'recentchanges') {
480 cgi_recentchanges($q);
484 CGI::Session->name("ikiwiki_session");
486 my $oldmask=umask(077);
487 my $session = CGI::Session->new("driver:db_file", $q,
488 { FileName => "$config{wikistatedir}/sessions.db" });
491 # Everything below this point needs the user to be signed in.
492 if ((! $config{anonok} && ! defined $session->param("name") ||
493 ! defined $session->param("name") ||
494 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
495 cgi_signin($q, $session);
497 # Force session flush with safe umask.
498 my $oldmask=umask(077);
505 if ($do eq 'create' || $do eq 'edit') {
506 cgi_editpage($q, $session);
508 elsif ($do eq 'prefs') {
509 cgi_prefs($q, $session);
511 elsif ($do eq 'blog') {
512 # munge page name to be valid, no matter what freeform text
514 my $page=lc($q->param('title'));
516 $page=~s/([^-A-Za-z0-9_:+\/])/"__".ord($1)."__"/eg;
517 # if the page already exist, munge it to be unique
518 my $from=$q->param('from');
520 while (exists $oldpagemtime{"$from/$page$add"}) {
521 $add=1 unless length $add;
524 $q->param('page', $page.$add);
525 # now run same as create
526 $q->param('do', 'create');
527 cgi_editpage($q, $session);
530 error("unknown do parameter");