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') {
147 print $q->redirect(cgiurl(
148 do => $form->field("do"),
149 page => $form->field("page"),
150 title => $form->field("title"),
151 subpage => $form->field("subpage"),
152 from => $form->field("from"),
156 print $q->redirect($config{url});
159 elsif ($form->submitted eq 'Register') {
160 my $user_name=$form->field('name');
161 if (userinfo_setall($user_name, {
162 'email' => $form->field('email'),
163 'password' => $form->field('password'),
166 $form->field(name => "confirm_password", type => "hidden");
167 $form->field(name => "email", type => "hidden");
168 $form->text("Registration successful. Now you can Login.");
169 print $session->header();
170 print misctemplate($form->title, $form->render(submit => ["Login"]));
173 error("Error saving registration.");
176 elsif ($form->submitted eq 'Mail Password') {
177 my $user_name=$form->field("name");
178 my $template=HTML::Template->new(
179 filename => "$config{templatedir}/passwordmail.tmpl"
182 user_name => $user_name,
183 user_password => userinfo_get($user_name, "password"),
184 wikiurl => $config{url},
185 wikiname => $config{wikiname},
186 REMOTE_ADDR => $ENV{REMOTE_ADDR},
189 eval q{use Mail::Sendmail};
190 my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
192 To => userinfo_get($user_name, "email"),
193 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
194 Subject => "$config{wikiname} information",
195 Message => $template->output,
196 ) or error("Failed to send mail");
198 $form->text("Your password has been emailed to you.");
199 $form->field(name => "name", required => 0);
200 print $session->header();
201 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
205 print $session->header();
206 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
210 sub cgi_prefs ($$) { #{{{
214 eval q{use CGI::FormBuilder};
215 my $form = CGI::FormBuilder->new(
216 title => "preferences",
217 fields => [qw(do name password confirm_password email locked_pages)],
221 confirm_password => {
222 perl => q{eq $form->field("password")},
229 action => $q->request_uri,
230 template => (-e "$config{templatedir}/prefs.tmpl" ?
231 "$config{templatedir}/prefs.tmpl" : "")
233 my @buttons=("Save Preferences", "Logout", "Cancel");
235 my $user_name=$session->param("name");
236 $form->field(name => "do", type => "hidden");
237 $form->field(name => "name", disabled => 1,
238 value => $user_name, force => 1);
239 $form->field(name => "password", type => "password");
240 $form->field(name => "confirm_password", type => "password");
241 $form->field(name => "locked_pages", size => 50,
242 comment => "(".htmllink("", "GlobList", 1).")");
244 if (! is_admin($user_name)) {
245 $form->field(name => "locked_pages", type => "hidden");
248 if (! $form->submitted) {
249 $form->field(name => "email", force => 1,
250 value => userinfo_get($user_name, "email"));
251 $form->field(name => "locked_pages", force => 1,
252 value => userinfo_get($user_name, "locked_pages"));
255 if ($form->submitted eq 'Logout') {
257 print $q->redirect($config{url});
260 elsif ($form->submitted eq 'Cancel') {
261 print $q->redirect($config{url});
264 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
265 foreach my $field (qw(password email locked_pages)) {
266 if (length $form->field($field)) {
267 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
270 $form->text("Preferences saved.");
273 print $session->header();
274 print misctemplate($form->title, $form->render(submit => \@buttons));
277 sub cgi_editpage ($$) { #{{{
281 eval q{use CGI::FormBuilder};
282 my $form = CGI::FormBuilder->new(
283 fields => [qw(do rcsinfo subpage from page content comments)],
289 required => [qw{content}],
292 action => $q->request_uri,
294 template => "$config{templatedir}/editpage.tmpl"
296 my @buttons=("Save Page", "Preview", "Cancel");
298 # This untaint is safe because titlepage removes any problimatic
300 my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
301 if (! defined $page || ! length $page ||
302 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
303 error("bad page name");
307 my $file=$page.$config{default_pageext};
309 if (exists $pagesources{lc($page)}) {
310 $file=$pagesources{lc($page)};
314 $form->field(name => "do", type => 'hidden');
315 $form->field(name => "from", type => 'hidden');
316 $form->field(name => "rcsinfo", type => 'hidden');
317 $form->field(name => "subpage", type => 'hidden');
318 $form->field(name => "page", value => "$page", force => 1);
319 $form->field(name => "comments", type => "text", size => 80);
320 $form->field(name => "content", type => "textarea", rows => 20,
322 $form->tmpl_param("can_commit", $config{rcs});
323 $form->tmpl_param("indexlink", indexlink());
324 $form->tmpl_param("helponformattinglink",
325 htmllink("", "HelpOnFormatting", 1));
326 if (! $form->submitted) {
327 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
331 if ($form->submitted eq "Cancel") {
332 print $q->redirect("$config{url}/".htmlpage($page));
335 elsif ($form->submitted eq "Preview") {
336 require IkiWiki::Render;
337 $form->tmpl_param("page_preview",
338 htmlize($config{default_pageext},
339 linkify($form->field('content'), $page)));
342 $form->tmpl_param("page_preview", "");
344 $form->tmpl_param("page_conflict", "");
346 if (! $form->submitted || $form->submitted eq "Preview" ||
348 if ($form->field("do") eq "create") {
349 if (exists $pagesources{lc($page)}) {
350 # hmm, someone else made the page in the
352 print $q->redirect("$config{url}/".htmlpage($page));
358 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
359 if (! defined $from || ! length $from ||
360 $from ne $form->param('from') ||
361 $from=~/$config{wiki_file_prune_regexp}/ ||
363 $form->submitted eq "Preview") {
364 @page_locs=$best_loc=$page;
370 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
371 $page eq 'discussion') {
372 $best_loc="$from/$page";
375 $best_loc=$dir.$page;
378 push @page_locs, $dir.$page;
379 push @page_locs, "$from/$page";
380 while (length $dir) {
382 push @page_locs, $dir.$page;
386 ! exists $pagesources{lc($_)} &&
387 ! page_locked($_, $session, 1)
391 $form->tmpl_param("page_select", 1);
392 $form->field(name => "page", type => 'select',
393 options => \@page_locs, value => $best_loc);
394 $form->title("creating ".pagetitle($page));
396 elsif ($form->field("do") eq "edit") {
397 page_locked($page, $session);
398 if (! defined $form->field('content') ||
399 ! length $form->field('content')) {
401 if (exists $pagesources{lc($page)}) {
402 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
403 $content=~s/\n/\r\n/g;
405 $form->field(name => "content", value => $content,
408 $form->tmpl_param("page_select", 0);
409 $form->field(name => "page", type => 'hidden');
410 $form->title("editing ".pagetitle($page));
413 print $form->render(submit => \@buttons);
417 page_locked($page, $session);
419 my $content=$form->field('content');
420 $content=~s/\r\n/\n/g;
422 writefile("$config{srcdir}/$file", $content);
424 my $message="web commit ";
425 if (length $session->param("name")) {
426 $message.="by ".$session->param("name");
429 $message.="from $ENV{REMOTE_ADDR}";
431 if (defined $form->field('comments') &&
432 length $form->field('comments')) {
433 $message.=": ".$form->field('comments');
440 # prevent deadlock with post-commit hook
442 # presumably the commit will trigger an update
444 my $conflict=rcs_commit($file, $message,
445 $form->field("rcsinfo"));
447 if (defined $conflict) {
448 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
450 $form->tmpl_param("page_conflict", 1);
451 $form->field("content", value => $conflict, force => 1);
452 $form->field("do", "edit)");
453 $form->tmpl_param("page_select", 0);
454 $form->field(name => "page", type => 'hidden');
455 $form->title("editing $page");
456 print $form->render(submit => \@buttons);
461 require IkiWiki::Render;
466 # The trailing question mark tries to avoid broken
467 # caches and get the most recent version of the page.
468 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
474 eval q{use CGI::Session};
478 my $do=$q->param('do');
479 if (! defined $do || ! length $do) {
480 error("\"do\" parameter missing");
483 # Things that do not need a session.
484 if ($do eq 'recentchanges') {
485 cgi_recentchanges($q);
489 CGI::Session->name("ikiwiki_session_$config{wikiname}");
491 my $oldmask=umask(077);
492 my $session = CGI::Session->new("driver:db_file", $q,
493 { FileName => "$config{wikistatedir}/sessions.db" });
496 # Everything below this point needs the user to be signed in.
497 if ((! $config{anonok} && ! defined $session->param("name") ||
498 ! defined $session->param("name") ||
499 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
500 cgi_signin($q, $session);
502 # Force session flush with safe umask.
503 my $oldmask=umask(077);
510 if ($do eq 'create' || $do eq 'edit') {
511 cgi_editpage($q, $session);
513 elsif ($do eq 'prefs') {
514 cgi_prefs($q, $session);
516 elsif ($do eq 'blog') {
517 my $page=titlepage(lc($q->param('title')));
518 # if the page already exists, munge it to be unique
519 my $from=$q->param('from');
521 while (exists $oldpagemtime{"$from/$page$add"}) {
522 $add=1 unless length $add;
525 $q->param('page', $page.$add);
526 # now run same as create
527 $q->param('do', 'create');
528 cgi_editpage($q, $session);
531 error("unknown do parameter");