9 sub page_locked ($$;$) { #{{{
14 my $user=$session->param("name");
15 return if length $user && is_admin($user);
17 foreach my $admin (@{$config{adminuser}}) {
18 my $locked_pages=userinfo_get($admin, "locked_pages");
19 if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
20 return 1 if $nonfatal;
21 error(htmllink("", $page, 1)." is locked by ".
22 htmllink("", $admin, 1)." and cannot be edited.");
29 sub cgi_recentchanges ($) { #{{{
34 my $template=HTML::Template->new(
35 filename => "$config{templatedir}/recentchanges.tmpl"
38 title => "RecentChanges",
39 indexlink => indexlink(),
40 wikiname => $config{wikiname},
41 changelog => [rcs_recentchanges(100)],
42 styleurl => styleurl(),
43 baseurl => "$config{url}/",
45 print $q->header, $template->output;
48 sub cgi_signin ($$) { #{{{
52 eval q{use CGI::FormBuilder};
53 my $form = CGI::FormBuilder->new(
55 fields => [qw(do title page subpage from name password confirm_password email)],
60 perl => q{eq $form->field("password")},
67 action => $config{cgiurl},
69 template => (-e "$config{templatedir}/signin.tmpl" ?
70 "$config{templatedir}/signin.tmpl" : ""),
71 stylesheet => styleurl(),
74 $form->field(name => "name", required => 0);
75 $form->field(name => "do", type => "hidden");
76 $form->field(name => "page", type => "hidden");
77 $form->field(name => "title", type => "hidden");
78 $form->field(name => "from", type => "hidden");
79 $form->field(name => "subpage", type => "hidden");
80 $form->field(name => "password", type => "password", required => 0);
81 $form->field(name => "confirm_password", type => "password", required => 0);
82 $form->field(name => "email", required => 0);
83 if ($q->param("do") ne "signin") {
84 $form->text("You need to log in first.");
87 if ($form->submitted) {
88 # Set required fields based on how form was submitted.
90 "Login" => [qw(name password)],
91 "Register" => [qw(name password confirm_password email)],
92 "Mail Password" => [qw(name)],
94 foreach my $opt (@{$required{$form->submitted}}) {
95 $form->field(name => $opt, required => 1);
98 # Validate password differently depending on how
100 if ($form->submitted eq 'Login') {
104 length $form->field("name") &&
105 shift eq userinfo_get($form->field("name"), 'password');
108 $form->field(name => "name", validate => '/^\w+$/');
111 $form->field(name => "password", validate => 'VALUE');
113 # And make sure the entered name exists when logging
114 # in or sending email, and does not when registering.
115 if ($form->submitted eq 'Register') {
121 $name=~/$wiki_file_regexp/ &&
122 ! userinfo_get($name, "regdate");
132 userinfo_get($name, "regdate");
138 # First time settings.
139 $form->field(name => "name", comment => "use FirstnameLastName");
140 $form->field(name => "confirm_password", comment => "(only needed");
141 $form->field(name => "email", comment => "for registration)");
142 if ($session->param("name")) {
143 $form->field(name => "name", value => $session->param("name"));
147 if ($form->submitted && $form->validate) {
148 if ($form->submitted eq 'Login') {
149 $session->param("name", $form->field("name"));
150 if (defined $form->field("do") &&
151 $form->field("do") ne 'signin') {
152 print $q->redirect(cgiurl(
153 do => $form->field("do"),
154 page => $form->field("page"),
155 title => $form->field("title"),
156 subpage => $form->field("subpage"),
157 from => $form->field("from"),
161 print $q->redirect($config{url});
164 elsif ($form->submitted eq 'Register') {
165 my $user_name=$form->field('name');
166 if (userinfo_setall($user_name, {
167 'email' => $form->field('email'),
168 'password' => $form->field('password'),
171 $form->field(name => "confirm_password", type => "hidden");
172 $form->field(name => "email", type => "hidden");
173 $form->text("Registration successful. Now you can Login.");
174 print $session->header();
175 print misctemplate($form->title, $form->render(submit => ["Login"]));
178 error("Error saving registration.");
181 elsif ($form->submitted eq 'Mail Password') {
182 my $user_name=$form->field("name");
183 my $template=HTML::Template->new(
184 filename => "$config{templatedir}/passwordmail.tmpl"
187 user_name => $user_name,
188 user_password => userinfo_get($user_name, "password"),
189 wikiurl => $config{url},
190 wikiname => $config{wikiname},
191 REMOTE_ADDR => $ENV{REMOTE_ADDR},
194 eval q{use Mail::Sendmail};
196 To => userinfo_get($user_name, "email"),
197 From => "$config{wikiname} admin <$config{adminemail}>",
198 Subject => "$config{wikiname} information",
199 Message => $template->output,
200 ) or error("Failed to send mail");
202 $form->text("Your password has been emailed to you.");
203 $form->field(name => "name", required => 0);
204 print $session->header();
205 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
209 print $session->header();
210 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
214 sub cgi_prefs ($$) { #{{{
218 eval q{use CGI::FormBuilder};
219 my $form = CGI::FormBuilder->new(
220 title => "preferences",
221 fields => [qw(do name password confirm_password email
222 subscriptions locked_pages)],
226 confirm_password => {
227 perl => q{eq $form->field("password")},
234 action => $config{cgiurl},
235 template => (-e "$config{templatedir}/prefs.tmpl" ?
236 "$config{templatedir}/prefs.tmpl" : ""),
237 stylesheet => styleurl(),
239 my @buttons=("Save Preferences", "Logout", "Cancel");
241 my $user_name=$session->param("name");
242 $form->field(name => "do", type => "hidden");
243 $form->field(name => "name", disabled => 1,
244 value => $user_name, force => 1);
245 $form->field(name => "password", type => "password");
246 $form->field(name => "confirm_password", type => "password");
247 $form->field(name => "subscriptions", size => 50,
248 comment => "(".htmllink("", "GlobList", 1).")");
249 $form->field(name => "locked_pages", size => 50,
250 comment => "(".htmllink("", "GlobList", 1).")");
252 if (! is_admin($user_name)) {
253 $form->field(name => "locked_pages", type => "hidden");
256 if (! $form->submitted) {
257 $form->field(name => "email", force => 1,
258 value => userinfo_get($user_name, "email"));
259 $form->field(name => "subscriptions", force => 1,
260 value => userinfo_get($user_name, "subscriptions"));
261 $form->field(name => "locked_pages", force => 1,
262 value => userinfo_get($user_name, "locked_pages"));
265 if ($form->submitted eq 'Logout') {
267 print $q->redirect($config{url});
270 elsif ($form->submitted eq 'Cancel') {
271 print $q->redirect($config{url});
274 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
275 foreach my $field (qw(password email subscriptions locked_pages)) {
276 if (length $form->field($field)) {
277 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
280 $form->text("Preferences saved.");
283 print $session->header();
284 print misctemplate($form->title, $form->render(submit => \@buttons));
287 sub cgi_editpage ($$) { #{{{
291 eval q{use CGI::FormBuilder};
292 my $form = CGI::FormBuilder->new(
293 fields => [qw(do rcsinfo subpage from page content comments)],
299 required => [qw{content}],
302 action => $config{cgiurl},
304 template => "$config{templatedir}/editpage.tmpl"
306 my @buttons=("Save Page", "Preview", "Cancel");
308 # This untaint is safe because titlepage removes any problimatic
310 my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
311 if (! defined $page || ! length $page ||
312 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
313 error("bad page name");
317 my $file=$page.$config{default_pageext};
318 if (exists $pagesources{lc($page)}) {
319 $file=$pagesources{lc($page)};
322 if (! -e "$config{srcdir}/$file") {
326 $form->field(name => "do", type => 'hidden');
327 $form->field(name => "from", type => 'hidden');
328 $form->field(name => "rcsinfo", type => 'hidden');
329 $form->field(name => "subpage", type => 'hidden');
330 $form->field(name => "page", value => "$page", force => 1);
331 $form->field(name => "comments", type => "text", size => 80);
332 $form->field(name => "content", type => "textarea", rows => 20,
334 $form->tmpl_param("can_commit", $config{rcs});
335 $form->tmpl_param("indexlink", indexlink());
336 $form->tmpl_param("helponformattinglink",
337 htmllink("", "HelpOnFormatting", 1));
338 $form->tmpl_param("styleurl", styleurl());
339 $form->tmpl_param("baseurl", "$config{url}/");
340 if (! $form->submitted) {
341 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
345 if ($form->submitted eq "Cancel") {
346 print $q->redirect("$config{url}/".htmlpage($page));
349 elsif ($form->submitted eq "Preview") {
350 require IkiWiki::Render;
351 $form->tmpl_param("page_preview",
352 htmlize($config{default_pageext},
353 linkify($form->field('content'), $page)));
356 $form->tmpl_param("page_preview", "");
358 $form->tmpl_param("page_conflict", "");
360 if (! $form->submitted || $form->submitted eq "Preview" ||
362 if ($form->field("do") eq "create") {
363 if (exists $pagesources{lc($page)}) {
364 # hmm, someone else made the page in the
366 print $q->redirect("$config{url}/".htmlpage($page));
372 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
373 if (! defined $from || ! length $from ||
374 $from ne $form->param('from') ||
375 $from=~/$config{wiki_file_prune_regexp}/ ||
377 $form->submitted eq "Preview") {
378 @page_locs=$best_loc=$page;
384 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
385 $page eq 'discussion') {
386 $best_loc="$from/$page";
389 $best_loc=$dir.$page;
392 push @page_locs, $dir.$page;
393 push @page_locs, "$from/$page";
394 while (length $dir) {
396 push @page_locs, $dir.$page;
400 ! exists $pagesources{lc($_)} &&
401 ! page_locked($_, $session, 1)
405 $form->tmpl_param("page_select", 1);
406 $form->field(name => "page", type => 'select',
407 options => \@page_locs, value => $best_loc);
408 $form->title("creating ".pagetitle($page));
410 elsif ($form->field("do") eq "edit") {
411 page_locked($page, $session);
412 if (! defined $form->field('content') ||
413 ! length $form->field('content')) {
415 if (exists $pagesources{lc($page)}) {
416 $content=readfile(srcfile($pagesources{lc($page)}));
417 $content=~s/\n/\r\n/g;
419 $form->field(name => "content", value => $content,
422 $form->tmpl_param("page_select", 0);
423 $form->field(name => "page", type => 'hidden');
424 $form->title("editing ".pagetitle($page));
427 print $form->render(submit => \@buttons);
431 page_locked($page, $session);
433 my $content=$form->field('content');
434 $content=~s/\r\n/\n/g;
436 writefile($file, $config{srcdir}, $content);
438 my $message="web commit ";
439 if (length $session->param("name")) {
440 $message.="by ".$session->param("name");
443 $message.="from $ENV{REMOTE_ADDR}";
445 if (defined $form->field('comments') &&
446 length $form->field('comments')) {
447 $message.=": ".$form->field('comments');
454 # prevent deadlock with post-commit hook
456 # presumably the commit will trigger an update
458 my $conflict=rcs_commit($file, $message,
459 $form->field("rcsinfo"));
461 if (defined $conflict) {
462 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
464 $form->tmpl_param("page_conflict", 1);
465 $form->field("content", value => $conflict, force => 1);
466 $form->field("do", "edit)");
467 $form->tmpl_param("page_select", 0);
468 $form->field(name => "page", type => 'hidden');
469 $form->title("editing $page");
470 print $form->render(submit => \@buttons);
475 require IkiWiki::Render;
480 # The trailing question mark tries to avoid broken
481 # caches and get the most recent version of the page.
482 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
486 sub cgi_hyperestraier () { #{{{
487 # only works for GET requests
488 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
489 exec("./".basename($config{cgiurl})) || error("estseek.cgi failed");
494 eval q{use CGI::Session};
498 my $do=$q->param('do');
499 if (! defined $do || ! length $do) {
500 if (defined $q->param('phrase')) {
504 error("\"do\" parameter missing");
508 # Things that do not need a session.
509 if ($do eq 'recentchanges') {
510 cgi_recentchanges($q);
513 elsif ($do eq 'hyperestraier') {
517 CGI::Session->name("ikiwiki_session_$config{wikiname}");
519 my $oldmask=umask(077);
520 my $session = CGI::Session->new("driver:DB_File", $q,
521 { FileName => "$config{wikistatedir}/sessions.db" });
524 # Everything below this point needs the user to be signed in.
525 if ((! $config{anonok} &&
526 (! defined $session->param("name") ||
527 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
528 cgi_signin($q, $session);
530 # Force session flush with safe umask.
531 my $oldmask=umask(077);
538 if ($do eq 'create' || $do eq 'edit') {
539 cgi_editpage($q, $session);
541 elsif ($do eq 'prefs') {
542 cgi_prefs($q, $session);
544 elsif ($do eq 'blog') {
545 my $page=titlepage(lc($q->param('title')));
546 # if the page already exists, munge it to be unique
547 my $from=$q->param('from');
549 while (exists $oldpagemtime{"$from/$page$add"}) {
550 $add=1 unless length $add;
553 $q->param('page', $page.$add);
554 # now run same as create
555 $q->param('do', 'create');
556 cgi_editpage($q, $session);
559 error("unknown do parameter");