7 use encoding 'utf8'; # force use of utf8 for io layer
11 sub page_locked ($$;$) { #{{{
16 my $user=$session->param("name");
17 return if defined $user && is_admin($user);
19 foreach my $admin (@{$config{adminuser}}) {
20 my $locked_pages=userinfo_get($admin, "locked_pages");
21 if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
22 return 1 if $nonfatal;
23 error(htmllink("", "", $page, 1)." is locked by ".
24 htmllink("", "", $admin, 1)." and cannot be edited.");
31 sub cgi_recentchanges ($) { #{{{
36 # Force reading the template as utf-8, necessary if
37 # rcs_recentchanges returns true utf-8 strings.
38 open(TMPL, "<:utf8", "$config{templatedir}/recentchanges.tmpl");
39 my $template=HTML::Template->new(filehandle => *TMPL);
41 my $template=HTML::Template->new(
42 filename => "$config{templatedir}/recentchanges.tmpl"
45 title => "RecentChanges",
46 indexlink => indexlink(),
47 wikiname => $config{wikiname},
48 changelog => [rcs_recentchanges(100)],
49 styleurl => styleurl(),
50 baseurl => "$config{url}/",
52 print $q->header(-charset=>'utf-8'), $template->output;
55 sub cgi_signin ($$) { #{{{
59 eval q{use CGI::FormBuilder};
60 my $form = CGI::FormBuilder->new(
62 fields => [qw(do title page subpage from name password confirm_password email)],
68 perl => q{eq $form->field("password")},
75 action => $config{cgiurl},
77 template => (-e "$config{templatedir}/signin.tmpl" ?
78 "$config{templatedir}/signin.tmpl" : ""),
79 stylesheet => styleurl(),
82 $form->field(name => "name", required => 0);
83 $form->field(name => "do", type => "hidden");
84 $form->field(name => "page", type => "hidden");
85 $form->field(name => "title", type => "hidden");
86 $form->field(name => "from", type => "hidden");
87 $form->field(name => "subpage", type => "hidden");
88 $form->field(name => "password", type => "password", required => 0);
89 $form->field(name => "confirm_password", type => "password", required => 0);
90 $form->field(name => "email", required => 0);
91 if ($q->param("do") ne "signin") {
92 $form->text("You need to log in first.");
95 if ($form->submitted) {
96 # Set required fields based on how form was submitted.
98 "Login" => [qw(name password)],
99 "Register" => [qw(name password confirm_password email)],
100 "Mail Password" => [qw(name)],
102 foreach my $opt (@{$required{$form->submitted}}) {
103 $form->field(name => $opt, required => 1);
106 # Validate password differently depending on how
107 # form was submitted.
108 if ($form->submitted eq 'Login') {
112 length $form->field("name") &&
113 shift eq userinfo_get($form->field("name"), 'password');
116 $form->field(name => "name", validate => '/^\w+$/');
119 $form->field(name => "password", validate => 'VALUE');
121 # And make sure the entered name exists when logging
122 # in or sending email, and does not when registering.
123 if ($form->submitted eq 'Register') {
129 $name=~/$config{wiki_file_regexp}/ &&
130 ! userinfo_get($name, "regdate");
140 userinfo_get($name, "regdate");
146 # First time settings.
147 $form->field(name => "name", comment => "use FirstnameLastName");
148 $form->field(name => "confirm_password", comment => "(only needed");
149 $form->field(name => "email", comment => "for registration)");
150 if ($session->param("name")) {
151 $form->field(name => "name", value => $session->param("name"));
155 if ($form->submitted && $form->validate) {
156 if ($form->submitted eq 'Login') {
157 $session->param("name", $form->field("name"));
158 if (defined $form->field("do") &&
159 $form->field("do") ne 'signin') {
160 print $q->redirect(cgiurl(
161 do => $form->field("do"),
162 page => $form->field("page"),
163 title => $form->field("title"),
164 subpage => $form->field("subpage"),
165 from => $form->field("from"),
169 print $q->redirect($config{url});
172 elsif ($form->submitted eq 'Register') {
173 my $user_name=$form->field('name');
174 if (userinfo_setall($user_name, {
175 'email' => $form->field('email'),
176 'password' => $form->field('password'),
179 $form->field(name => "confirm_password", type => "hidden");
180 $form->field(name => "email", type => "hidden");
181 $form->text("Registration successful. Now you can Login.");
182 print $session->header(-charset=>'utf-8');
183 print misctemplate($form->title, $form->render(submit => ["Login"]));
186 error("Error saving registration.");
189 elsif ($form->submitted eq 'Mail Password') {
190 my $user_name=$form->field("name");
191 my $template=HTML::Template->new(
192 filename => "$config{templatedir}/passwordmail.tmpl"
195 user_name => $user_name,
196 user_password => userinfo_get($user_name, "password"),
197 wikiurl => $config{url},
198 wikiname => $config{wikiname},
199 REMOTE_ADDR => $ENV{REMOTE_ADDR},
202 eval q{use Mail::Sendmail};
204 To => userinfo_get($user_name, "email"),
205 From => "$config{wikiname} admin <$config{adminemail}>",
206 Subject => "$config{wikiname} information",
207 Message => $template->output,
208 ) or error("Failed to send mail");
210 $form->text("Your password has been emailed to you.");
211 $form->field(name => "name", required => 0);
212 print $session->header(-charset=>'utf-8');
213 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
217 print $session->header(-charset=>'utf-8');
218 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
222 sub cgi_prefs ($$) { #{{{
226 eval q{use CGI::FormBuilder};
227 my $form = CGI::FormBuilder->new(
228 title => "preferences",
229 fields => [qw(do name password confirm_password email
230 subscriptions locked_pages)],
235 confirm_password => {
236 perl => q{eq $form->field("password")},
243 action => $config{cgiurl},
244 template => (-e "$config{templatedir}/prefs.tmpl" ?
245 "$config{templatedir}/prefs.tmpl" : ""),
246 stylesheet => styleurl(),
248 my @buttons=("Save Preferences", "Logout", "Cancel");
250 my $user_name=$session->param("name");
251 $form->field(name => "do", type => "hidden");
252 $form->field(name => "name", disabled => 1,
253 value => $user_name, force => 1);
254 $form->field(name => "password", type => "password");
255 $form->field(name => "confirm_password", type => "password");
256 $form->field(name => "subscriptions", size => 50,
257 comment => "(".htmllink("", "", "GlobList", 1).")");
258 $form->field(name => "locked_pages", size => 50,
259 comment => "(".htmllink("", "", "GlobList", 1).")");
261 if (! is_admin($user_name)) {
262 $form->field(name => "locked_pages", type => "hidden");
265 if (! $form->submitted) {
266 $form->field(name => "email", force => 1,
267 value => userinfo_get($user_name, "email"));
268 $form->field(name => "subscriptions", force => 1,
269 value => userinfo_get($user_name, "subscriptions"));
270 $form->field(name => "locked_pages", force => 1,
271 value => userinfo_get($user_name, "locked_pages"));
274 if ($form->submitted eq 'Logout') {
276 print $q->redirect($config{url});
279 elsif ($form->submitted eq 'Cancel') {
280 print $q->redirect($config{url});
283 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
284 foreach my $field (qw(password email subscriptions locked_pages)) {
285 if (length $form->field($field)) {
286 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
289 $form->text("Preferences saved.");
292 print $session->header(-charset=>'utf-8');
293 print misctemplate($form->title, $form->render(submit => \@buttons));
296 sub cgi_editpage ($$) { #{{{
300 eval q{use CGI::FormBuilder};
301 my $form = CGI::FormBuilder->new(
302 fields => [qw(do rcsinfo subpage from page editcontent comments)],
307 editcontent => '/.+/',
309 required => [qw{editcontent}],
312 action => $config{cgiurl},
314 template => "$config{templatedir}/editpage.tmpl"
316 my @buttons=("Save Page", "Preview", "Cancel");
318 # This untaint is safe because titlepage removes any problimatic
320 my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
321 if (! defined $page || ! length $page ||
322 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
323 error("bad page name");
327 my $file=$page.$config{default_pageext};
328 if (exists $pagesources{lc($page)}) {
329 $file=$pagesources{lc($page)};
332 if (! -e "$config{srcdir}/$file") {
336 $form->field(name => "do", type => 'hidden');
337 $form->field(name => "from", type => 'hidden');
338 $form->field(name => "rcsinfo", type => 'hidden');
339 $form->field(name => "subpage", type => 'hidden');
340 $form->field(name => "page", value => "$page", force => 1);
341 $form->field(name => "comments", type => "text", size => 80);
342 $form->field(name => "editcontent", type => "textarea", rows => 20,
344 $form->tmpl_param("can_commit", $config{rcs});
345 $form->tmpl_param("indexlink", indexlink());
346 $form->tmpl_param("helponformattinglink",
347 htmllink("", "", "HelpOnFormatting", 1));
348 $form->tmpl_param("styleurl", styleurl());
349 $form->tmpl_param("baseurl", "$config{url}/");
350 if (! $form->submitted) {
351 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
355 if ($form->submitted eq "Cancel") {
356 print $q->redirect("$config{url}/".htmlpage($page));
359 elsif ($form->submitted eq "Preview") {
360 require IkiWiki::Render;
361 # Apparently FormBuilder doesn't not treat input as
362 # utf-8, so decode from it.
364 my $content = Encode::decode_utf8($form->field('editcontent'));
365 $form->field(name => "editcontent",
366 value => $content, force => 1);
367 $form->field(name => "comments",
368 value => Encode::decode_utf8($form->field('comments')), force => 1);
369 $form->tmpl_param("page_preview",
370 htmlize($config{default_pageext},
371 linkify($page, $page, $content)));
374 $form->tmpl_param("page_preview", "");
376 $form->tmpl_param("page_conflict", "");
378 if (! $form->submitted || $form->submitted eq "Preview" ||
380 if ($form->field("do") eq "create") {
383 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
384 if (! defined $from || ! length $from ||
385 $from ne $form->param('from') ||
386 $from=~/$config{wiki_file_prune_regexp}/ ||
388 $form->submitted eq "Preview") {
389 @page_locs=$best_loc=$page;
395 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
396 $page eq 'discussion') {
397 $best_loc="$from/$page";
400 $best_loc=$dir.$page;
403 push @page_locs, $dir.$page;
404 push @page_locs, "$from/$page";
405 while (length $dir) {
407 push @page_locs, $dir.$page;
412 ! exists $pagesources{lc($_)} &&
413 ! page_locked($_, $session, 1)
417 # hmm, someone else made the page in the
419 print $q->redirect("$config{url}/".htmlpage($page));
423 $form->tmpl_param("page_select", 1);
424 $form->field(name => "page", type => 'select',
425 options => \@page_locs, value => $best_loc);
426 $form->title("creating ".pagetitle($page));
428 elsif ($form->field("do") eq "edit") {
429 page_locked($page, $session);
430 if (! defined $form->field('editcontent') ||
431 ! length $form->field('editcontent')) {
433 if (exists $pagesources{lc($page)}) {
434 $content=readfile(srcfile($pagesources{lc($page)}));
435 $content=~s/\n/\r\n/g;
437 $form->field(name => "editcontent", value => $content,
440 $form->tmpl_param("page_select", 0);
441 $form->field(name => "page", type => 'hidden');
442 $form->title("editing ".pagetitle($page));
445 print $form->render(submit => \@buttons);
449 page_locked($page, $session);
451 # Decode utf-8 since FormBuilder does not
453 my $content=Encode::decode_utf8($form->field('editcontent'));
455 $content=~s/\r\n/\n/g;
457 writefile($file, $config{srcdir}, $content);
459 my $message="web commit ";
460 if (defined $session->param("name") &&
461 length $session->param("name")) {
462 $message.="by ".$session->param("name");
465 $message.="from $ENV{REMOTE_ADDR}";
467 if (defined $form->field('comments') &&
468 length $form->field('comments')) {
469 $message.=Encode::decode_utf8(": ".$form->field('comments'));
476 # prevent deadlock with post-commit hook
478 # presumably the commit will trigger an update
480 my $conflict=rcs_commit($file, $message,
481 $form->field("rcsinfo"));
483 if (defined $conflict) {
484 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
486 $form->tmpl_param("page_conflict", 1);
487 $form->field("editcontent", value => $conflict, force => 1);
488 $form->field("do", "edit)");
489 $form->tmpl_param("page_select", 0);
490 $form->field(name => "page", type => 'hidden');
491 $form->title("editing $page");
492 print $form->render(submit => \@buttons);
497 require IkiWiki::Render;
502 # The trailing question mark tries to avoid broken
503 # caches and get the most recent version of the page.
504 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
510 eval q{use CGI::Session};
514 if (exists $hooks{cgi}) {
515 foreach my $id (keys %{$hooks{cgi}}) {
516 $hooks{cgi}{$id}{call}->($q);
520 my $do=$q->param('do');
521 if (! defined $do || ! length $do) {
522 error("\"do\" parameter missing");
525 # Things that do not need a session.
526 if ($do eq 'recentchanges') {
527 cgi_recentchanges($q);
530 elsif ($do eq 'hyperestraier') {
534 CGI::Session->name("ikiwiki_session_$config{wikiname}");
536 my $oldmask=umask(077);
537 my $session = CGI::Session->new("driver:DB_File", $q,
538 { FileName => "$config{wikistatedir}/sessions.db" });
541 # Everything below this point needs the user to be signed in.
542 if ((! $config{anonok} &&
543 (! defined $session->param("name") ||
544 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
545 cgi_signin($q, $session);
547 # Force session flush with safe umask.
548 my $oldmask=umask(077);
555 if ($do eq 'create' || $do eq 'edit') {
556 cgi_editpage($q, $session);
558 elsif ($do eq 'prefs') {
559 cgi_prefs($q, $session);
561 elsif ($do eq 'blog') {
562 my $page=titlepage(lc($q->param('title')));
563 # if the page already exists, munge it to be unique
564 my $from=$q->param('from');
566 while (exists $oldpagemtime{"$from/$page$add"}) {
567 $add=1 unless length $add;
570 $q->param('page', $page.$add);
571 # now run same as create
572 $q->param('do', 'create');
573 cgi_editpage($q, $session);
576 error("unknown do parameter");