7 use open qw{:utf8 :std};
12 sub page_locked ($$;$) { #{{{
17 my $user=$session->param("name");
18 return if defined $user && is_admin($user);
20 foreach my $admin (@{$config{adminuser}}) {
21 my $locked_pages=userinfo_get($admin, "locked_pages");
22 if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
23 return 1 if $nonfatal;
24 error(htmllink("", "", $page, 1)." is locked by ".
25 htmllink("", "", $admin, 1)." and cannot be edited.");
32 sub cgi_recentchanges ($) { #{{{
37 # Optimisation: building recentchanges means calculating lots of
38 # links. Memoizing htmllink speeds it up a lot (can't be memoized
39 # during page builds as the return values may change, but they
44 my $template=template("recentchanges.tmpl");
46 title => "RecentChanges",
47 indexlink => indexlink(),
48 wikiname => $config{wikiname},
49 changelog => [rcs_recentchanges(100)],
50 styleurl => styleurl(),
51 baseurl => "$config{url}/",
53 print $q->header(-charset=>'utf-8'), $template->output;
56 sub cgi_signin ($$) { #{{{
60 eval q{use CGI::FormBuilder};
61 my $form = CGI::FormBuilder->new(
63 fields => [qw(do title page subpage from name password confirm_password email)],
69 perl => q{eq $form->field("password")},
76 action => $config{cgiurl},
78 template => (-e "$config{templatedir}/signin.tmpl" ?
79 {template_params("signin.tmpl")} : ""),
80 stylesheet => styleurl(),
83 $form->field(name => "name", required => 0);
84 $form->field(name => "do", type => "hidden");
85 $form->field(name => "page", type => "hidden");
86 $form->field(name => "title", type => "hidden");
87 $form->field(name => "from", type => "hidden");
88 $form->field(name => "subpage", type => "hidden");
89 $form->field(name => "password", type => "password", required => 0);
90 $form->field(name => "confirm_password", type => "password", required => 0);
91 $form->field(name => "email", required => 0);
92 if ($q->param("do") ne "signin") {
93 $form->text("You need to log in first.");
96 if ($form->submitted) {
97 # Set required fields based on how form was submitted.
99 "Login" => [qw(name password)],
100 "Register" => [qw(name password confirm_password email)],
101 "Mail Password" => [qw(name)],
103 foreach my $opt (@{$required{$form->submitted}}) {
104 $form->field(name => $opt, required => 1);
107 # Validate password differently depending on how
108 # form was submitted.
109 if ($form->submitted eq 'Login') {
113 length $form->field("name") &&
114 shift eq userinfo_get($form->field("name"), 'password');
117 $form->field(name => "name", validate => '/^\w+$/');
120 $form->field(name => "password", validate => 'VALUE');
122 # And make sure the entered name exists when logging
123 # in or sending email, and does not when registering.
124 if ($form->submitted eq 'Register') {
130 $name=~/$config{wiki_file_regexp}/ &&
131 ! userinfo_get($name, "regdate");
141 userinfo_get($name, "regdate");
147 # First time settings.
148 $form->field(name => "name", comment => "use FirstnameLastName");
149 $form->field(name => "confirm_password", comment => "(only needed");
150 $form->field(name => "email", comment => "for registration)");
151 if ($session->param("name")) {
152 $form->field(name => "name", value => $session->param("name"));
156 if ($form->submitted && $form->validate) {
157 if ($form->submitted eq 'Login') {
158 $session->param("name", $form->field("name"));
159 if (defined $form->field("do") &&
160 $form->field("do") ne 'signin') {
161 print $q->redirect(cgiurl(
162 do => $form->field("do"),
163 page => $form->field("page"),
164 title => $form->field("title"),
165 subpage => $form->field("subpage"),
166 from => $form->field("from"),
170 print $q->redirect($config{url});
173 elsif ($form->submitted eq 'Register') {
174 my $user_name=$form->field('name');
175 if (userinfo_setall($user_name, {
176 'email' => $form->field('email'),
177 'password' => $form->field('password'),
180 $form->field(name => "confirm_password", type => "hidden");
181 $form->field(name => "email", type => "hidden");
182 $form->text("Registration successful. Now you can Login.");
183 print $session->header(-charset=>'utf-8');
184 print misctemplate($form->title, $form->render(submit => ["Login"]));
187 error("Error saving registration.");
190 elsif ($form->submitted eq 'Mail Password') {
191 my $user_name=$form->field("name");
192 my $template=template("passwordmail.tmpl");
194 user_name => $user_name,
195 user_password => userinfo_get($user_name, "password"),
196 wikiurl => $config{url},
197 wikiname => $config{wikiname},
198 REMOTE_ADDR => $ENV{REMOTE_ADDR},
201 eval q{use Mail::Sendmail};
203 To => userinfo_get($user_name, "email"),
204 From => "$config{wikiname} admin <$config{adminemail}>",
205 Subject => "$config{wikiname} information",
206 Message => $template->output,
207 ) or error("Failed to send mail");
209 $form->text("Your password has been emailed to you.");
210 $form->field(name => "name", required => 0);
211 print $session->header(-charset=>'utf-8');
212 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
216 print $session->header(-charset=>'utf-8');
217 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
221 sub cgi_prefs ($$) { #{{{
225 eval q{use CGI::FormBuilder};
226 my $form = CGI::FormBuilder->new(
227 title => "preferences",
228 fields => [qw(do name password confirm_password email
229 subscriptions locked_pages)],
234 confirm_password => {
235 perl => q{eq $form->field("password")},
242 action => $config{cgiurl},
243 template => (-e "$config{templatedir}/prefs.tmpl" ?
244 {template_params("prefs.tmpl")} : ""),
245 stylesheet => styleurl(),
247 my @buttons=("Save Preferences", "Logout", "Cancel");
249 my $user_name=$session->param("name");
250 $form->field(name => "do", type => "hidden");
251 $form->field(name => "name", disabled => 1,
252 value => $user_name, force => 1);
253 $form->field(name => "password", type => "password");
254 $form->field(name => "confirm_password", type => "password");
255 $form->field(name => "subscriptions", size => 50,
256 comment => "(".htmllink("", "", "GlobList", 1).")");
257 $form->field(name => "locked_pages", size => 50,
258 comment => "(".htmllink("", "", "GlobList", 1).")");
260 if (! is_admin($user_name)) {
261 $form->field(name => "locked_pages", type => "hidden");
264 if (! $form->submitted) {
265 $form->field(name => "email", force => 1,
266 value => userinfo_get($user_name, "email"));
267 $form->field(name => "subscriptions", force => 1,
268 value => userinfo_get($user_name, "subscriptions"));
269 $form->field(name => "locked_pages", force => 1,
270 value => userinfo_get($user_name, "locked_pages"));
273 if ($form->submitted eq 'Logout') {
275 print $q->redirect($config{url});
278 elsif ($form->submitted eq 'Cancel') {
279 print $q->redirect($config{url});
282 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
283 foreach my $field (qw(password email subscriptions locked_pages)) {
284 if (length $form->field($field)) {
285 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
288 $form->text("Preferences saved.");
291 print $session->header(-charset=>'utf-8');
292 print misctemplate($form->title, $form->render(submit => \@buttons));
295 sub cgi_editpage ($$) { #{{{
299 eval q{use CGI::FormBuilder};
300 my $form = CGI::FormBuilder->new(
301 fields => [qw(do rcsinfo subpage from page editcontent comments)],
306 editcontent => '/.+/',
308 required => [qw{editcontent}],
311 action => $config{cgiurl},
313 template => {template_params("editpage.tmpl")},
315 my @buttons=("Save Page", "Preview", "Cancel");
317 # This untaint is safe because titlepage removes any problimatic
319 my ($page)=Encode::decode_utf8($form->param('page'));
320 $page=titlepage(possibly_foolish_untaint(lc($page)));
321 if (! defined $page || ! length $page ||
322 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
323 error("bad page name");
328 if (exists $pagesources{lc($page)}) {
329 $file=$pagesources{lc($page)};
332 $file=$page.".".$config{default_pageext};
335 if (! -e "$config{srcdir}/$file") {
339 $form->field(name => "do", type => 'hidden');
340 $form->field(name => "from", type => 'hidden');
341 $form->field(name => "rcsinfo", type => 'hidden');
342 $form->field(name => "subpage", type => 'hidden');
343 $form->field(name => "page", value => "$page", force => 1);
344 $form->field(name => "comments", type => "text", size => 80);
345 $form->field(name => "editcontent", type => "textarea", rows => 20,
347 $form->tmpl_param("can_commit", $config{rcs});
348 $form->tmpl_param("indexlink", indexlink());
349 $form->tmpl_param("helponformattinglink",
350 htmllink("", "", "HelpOnFormatting", 1));
351 $form->tmpl_param("styleurl", styleurl());
352 $form->tmpl_param("baseurl", "$config{url}/");
353 if (! $form->submitted) {
354 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
358 if ($form->submitted eq "Cancel") {
359 print $q->redirect("$config{url}/".htmlpage($page));
362 elsif ($form->submitted eq "Preview") {
363 require IkiWiki::Render;
364 # Apparently FormBuilder doesn't not treat input as
365 # utf-8, so decode from it.
366 my $content = Encode::decode_utf8($form->field('editcontent'));
367 my $comments = Encode::decode_utf8($form->field('comments'));
368 $form->field(name => "editcontent",
369 value => $content, force => 1);
370 $form->field(name => "comments",
371 value => $comments, force => 1);
372 $form->tmpl_param("page_preview",
373 htmlize(pagetype($file),
374 linkify($page, $page, $content)));
377 $form->tmpl_param("page_preview", "");
379 $form->tmpl_param("page_conflict", "");
381 if (! $form->submitted || $form->submitted eq "Preview" ||
383 if ($form->field("do") eq "create") {
386 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
387 if (! defined $from || ! length $from ||
388 $from ne $form->param('from') ||
389 $from=~/$config{wiki_file_prune_regexp}/ ||
391 $form->submitted eq "Preview") {
392 @page_locs=$best_loc=$page;
398 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
399 $page eq 'discussion') {
400 $best_loc="$from/$page";
403 $best_loc=$dir.$page;
406 push @page_locs, $dir.$page;
407 push @page_locs, "$from/$page";
408 while (length $dir) {
410 push @page_locs, $dir.$page;
415 ! exists $pagesources{lc($_)} &&
416 ! page_locked($_, $session, 1)
420 # hmm, someone else made the page in the
422 print $q->redirect("$config{url}/".htmlpage($page));
426 $form->tmpl_param("page_select", 1);
427 $form->field(name => "page", type => 'select',
428 options => \@page_locs, value => $best_loc);
429 $form->title("creating ".pagetitle($page));
431 elsif ($form->field("do") eq "edit") {
432 page_locked($page, $session);
433 if (! defined $form->field('editcontent') ||
434 ! length $form->field('editcontent')) {
436 if (exists $pagesources{lc($page)}) {
437 $content=readfile(srcfile($pagesources{lc($page)}));
438 $content=~s/\n/\r\n/g;
440 $form->field(name => "editcontent", value => $content,
443 $form->tmpl_param("page_select", 0);
444 $form->field(name => "page", type => 'hidden');
445 $form->title("editing ".pagetitle($page));
448 print $form->render(submit => \@buttons);
452 page_locked($page, $session);
454 # Decode utf-8 since FormBuilder does not
455 my $content=Encode::decode_utf8($form->field('editcontent'));
457 $content=~s/\r\n/\n/g;
459 writefile($file, $config{srcdir}, $content);
461 my $message="web commit ";
462 if (defined $session->param("name") &&
463 length $session->param("name")) {
464 $message.="by ".$session->param("name");
467 $message.="from $ENV{REMOTE_ADDR}";
469 if (defined $form->field('comments') &&
470 length $form->field('comments')) {
471 $message.=": ".Encode::decode_utf8($form->field('comments'));
478 # prevent deadlock with post-commit hook
480 # presumably the commit will trigger an update
482 my $conflict=rcs_commit($file, $message,
483 $form->field("rcsinfo"));
485 if (defined $conflict) {
486 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
488 $form->tmpl_param("page_conflict", 1);
489 $form->field("editcontent", value => $conflict, force => 1);
490 $form->field(name => "comments", value => Encode::decode_utf8($form->field('comments')), force => 1);
491 $form->field("do", "edit)");
492 $form->tmpl_param("page_select", 0);
493 $form->field(name => "page", type => 'hidden');
494 $form->title("editing $page");
495 print $form->render(submit => \@buttons);
500 require IkiWiki::Render;
505 # The trailing question mark tries to avoid broken
506 # caches and get the most recent version of the page.
507 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
513 eval q{use CGI::Session};
517 if (exists $hooks{cgi}) {
518 foreach my $id (keys %{$hooks{cgi}}) {
519 $hooks{cgi}{$id}{call}->($q);
523 my $do=$q->param('do');
524 if (! defined $do || ! length $do) {
525 error("\"do\" parameter missing");
528 # Things that do not need a session.
529 if ($do eq 'recentchanges') {
530 cgi_recentchanges($q);
533 elsif ($do eq 'hyperestraier') {
537 CGI::Session->name("ikiwiki_session_$config{wikiname}");
539 my $oldmask=umask(077);
540 my $session = CGI::Session->new("driver:DB_File", $q,
541 { FileName => "$config{wikistatedir}/sessions.db" });
544 # Everything below this point needs the user to be signed in.
545 if ((! $config{anonok} &&
546 (! defined $session->param("name") ||
547 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
548 cgi_signin($q, $session);
550 # Force session flush with safe umask.
551 my $oldmask=umask(077);
558 if ($do eq 'create' || $do eq 'edit') {
559 cgi_editpage($q, $session);
561 elsif ($do eq 'prefs') {
562 cgi_prefs($q, $session);
564 elsif ($do eq 'blog') {
565 my $page=titlepage(lc($q->param('title')));
566 # if the page already exists, munge it to be unique
567 my $from=$q->param('from');
569 while (exists $oldpagemtime{"$from/$page$add"}) {
570 $add=1 unless length $add;
573 $q->param('page', $page.$add);
574 # now run same as create
575 $q->param('do', 'create');
576 cgi_editpage($q, $session);
579 error("unknown do parameter");