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};
312 if (exists $pagesources{lc($page)}) {
313 $file=$pagesources{lc($page)};
317 $form->field(name => "do", type => 'hidden');
318 $form->field(name => "from", type => 'hidden');
319 $form->field(name => "rcsinfo", type => 'hidden');
320 $form->field(name => "subpage", type => 'hidden');
321 $form->field(name => "page", value => "$page", force => 1);
322 $form->field(name => "comments", type => "text", size => 80);
323 $form->field(name => "content", type => "textarea", rows => 20,
325 $form->tmpl_param("can_commit", $config{rcs});
326 $form->tmpl_param("indexlink", indexlink());
327 $form->tmpl_param("helponformattinglink",
328 htmllink("", "HelpOnFormatting", 1));
329 $form->tmpl_param("styleurl", styleurl());
330 if (! $form->submitted) {
331 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
335 if ($form->submitted eq "Cancel") {
336 print $q->redirect("$config{url}/".htmlpage($page));
339 elsif ($form->submitted eq "Preview") {
340 require IkiWiki::Render;
341 $form->tmpl_param("page_preview",
342 htmlize($config{default_pageext},
343 linkify($form->field('content'), $page)));
346 $form->tmpl_param("page_preview", "");
348 $form->tmpl_param("page_conflict", "");
350 if (! $form->submitted || $form->submitted eq "Preview" ||
352 if ($form->field("do") eq "create") {
353 if (exists $pagesources{lc($page)}) {
354 # hmm, someone else made the page in the
356 print $q->redirect("$config{url}/".htmlpage($page));
362 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
363 if (! defined $from || ! length $from ||
364 $from ne $form->param('from') ||
365 $from=~/$config{wiki_file_prune_regexp}/ ||
367 $form->submitted eq "Preview") {
368 @page_locs=$best_loc=$page;
374 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
375 $page eq 'discussion') {
376 $best_loc="$from/$page";
379 $best_loc=$dir.$page;
382 push @page_locs, $dir.$page;
383 push @page_locs, "$from/$page";
384 while (length $dir) {
386 push @page_locs, $dir.$page;
390 ! exists $pagesources{lc($_)} &&
391 ! page_locked($_, $session, 1)
395 $form->tmpl_param("page_select", 1);
396 $form->field(name => "page", type => 'select',
397 options => \@page_locs, value => $best_loc);
398 $form->title("creating ".pagetitle($page));
400 elsif ($form->field("do") eq "edit") {
401 page_locked($page, $session);
402 if (! defined $form->field('content') ||
403 ! length $form->field('content')) {
405 if (exists $pagesources{lc($page)}) {
406 $content=readfile(srcfile($pagesources{lc($page)}));
407 $content=~s/\n/\r\n/g;
409 $form->field(name => "content", value => $content,
412 $form->tmpl_param("page_select", 0);
413 $form->field(name => "page", type => 'hidden');
414 $form->title("editing ".pagetitle($page));
417 print $form->render(submit => \@buttons);
421 page_locked($page, $session);
423 my $content=$form->field('content');
424 $content=~s/\r\n/\n/g;
426 writefile("$config{srcdir}/$file", $content);
428 my $message="web commit ";
429 if (length $session->param("name")) {
430 $message.="by ".$session->param("name");
433 $message.="from $ENV{REMOTE_ADDR}";
435 if (defined $form->field('comments') &&
436 length $form->field('comments')) {
437 $message.=": ".$form->field('comments');
444 # prevent deadlock with post-commit hook
446 # presumably the commit will trigger an update
448 my $conflict=rcs_commit($file, $message,
449 $form->field("rcsinfo"));
451 if (defined $conflict) {
452 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
454 $form->tmpl_param("page_conflict", 1);
455 $form->field("content", value => $conflict, force => 1);
456 $form->field("do", "edit)");
457 $form->tmpl_param("page_select", 0);
458 $form->field(name => "page", type => 'hidden');
459 $form->title("editing $page");
460 print $form->render(submit => \@buttons);
465 require IkiWiki::Render;
470 # The trailing question mark tries to avoid broken
471 # caches and get the most recent version of the page.
472 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
478 eval q{use CGI::Session};
482 my $do=$q->param('do');
483 if (! defined $do || ! length $do) {
484 error("\"do\" parameter missing");
487 # Things that do not need a session.
488 if ($do eq 'recentchanges') {
489 cgi_recentchanges($q);
493 CGI::Session->name("ikiwiki_session_$config{wikiname}");
495 my $oldmask=umask(077);
496 my $session = CGI::Session->new("driver:db_file", $q,
497 { FileName => "$config{wikistatedir}/sessions.db" });
500 # Everything below this point needs the user to be signed in.
501 if ((! $config{anonok} && ! defined $session->param("name") ||
502 ! defined $session->param("name") ||
503 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
504 cgi_signin($q, $session);
506 # Force session flush with safe umask.
507 my $oldmask=umask(077);
514 if ($do eq 'create' || $do eq 'edit') {
515 cgi_editpage($q, $session);
517 elsif ($do eq 'prefs') {
518 cgi_prefs($q, $session);
520 elsif ($do eq 'blog') {
521 my $page=titlepage(lc($q->param('title')));
522 # if the page already exists, munge it to be unique
523 my $from=$q->param('from');
525 while (exists $oldpagemtime{"$from/$page$add"}) {
526 $add=1 unless length $add;
529 $q->param('page', $page.$add);
530 # now run same as create
531 $q->param('do', 'create');
532 cgi_editpage($q, $session);
535 error("unknown do parameter");