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 my $template=template("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(-charset=>'utf-8'), $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)],
61 perl => q{eq $form->field("password")},
68 action => $config{cgiurl},
70 template => (-e "$config{templatedir}/signin.tmpl" ?
71 {template_params("signin.tmpl")} : ""),
72 stylesheet => styleurl(),
75 $form->field(name => "name", required => 0);
76 $form->field(name => "do", type => "hidden");
77 $form->field(name => "page", type => "hidden");
78 $form->field(name => "title", type => "hidden");
79 $form->field(name => "from", type => "hidden");
80 $form->field(name => "subpage", type => "hidden");
81 $form->field(name => "password", type => "password", required => 0);
82 $form->field(name => "confirm_password", type => "password", required => 0);
83 $form->field(name => "email", required => 0);
84 if ($q->param("do") ne "signin") {
85 $form->text("You need to log in first.");
88 if ($form->submitted) {
89 # Set required fields based on how form was submitted.
91 "Login" => [qw(name password)],
92 "Register" => [qw(name password confirm_password email)],
93 "Mail Password" => [qw(name)],
95 foreach my $opt (@{$required{$form->submitted}}) {
96 $form->field(name => $opt, required => 1);
99 # Validate password differently depending on how
100 # form was submitted.
101 if ($form->submitted eq 'Login') {
105 length $form->field("name") &&
106 shift eq userinfo_get($form->field("name"), 'password');
109 $form->field(name => "name", validate => '/^\w+$/');
112 $form->field(name => "password", validate => 'VALUE');
114 # And make sure the entered name exists when logging
115 # in or sending email, and does not when registering.
116 if ($form->submitted eq 'Register') {
122 $name=~/$config{wiki_file_regexp}/ &&
123 ! userinfo_get($name, "regdate");
133 userinfo_get($name, "regdate");
139 # First time settings.
140 $form->field(name => "name", comment => "use FirstnameLastName");
141 $form->field(name => "confirm_password", comment => "(only needed");
142 $form->field(name => "email", comment => "for registration)");
143 if ($session->param("name")) {
144 $form->field(name => "name", value => $session->param("name"));
148 if ($form->submitted && $form->validate) {
149 if ($form->submitted eq 'Login') {
150 $session->param("name", $form->field("name"));
151 if (defined $form->field("do") &&
152 $form->field("do") ne 'signin') {
153 print $q->redirect(cgiurl(
154 do => $form->field("do"),
155 page => $form->field("page"),
156 title => $form->field("title"),
157 subpage => $form->field("subpage"),
158 from => $form->field("from"),
162 print $q->redirect($config{url});
165 elsif ($form->submitted eq 'Register') {
166 my $user_name=$form->field('name');
167 if (userinfo_setall($user_name, {
168 'email' => $form->field('email'),
169 'password' => $form->field('password'),
172 $form->field(name => "confirm_password", type => "hidden");
173 $form->field(name => "email", type => "hidden");
174 $form->text("Registration successful. Now you can Login.");
175 print $session->header(-charset=>'utf-8');
176 print misctemplate($form->title, $form->render(submit => ["Login"]));
179 error("Error saving registration.");
182 elsif ($form->submitted eq 'Mail Password') {
183 my $user_name=$form->field("name");
184 my $template=template("passwordmail.tmpl");
186 user_name => $user_name,
187 user_password => userinfo_get($user_name, "password"),
188 wikiurl => $config{url},
189 wikiname => $config{wikiname},
190 REMOTE_ADDR => $ENV{REMOTE_ADDR},
193 eval q{use Mail::Sendmail};
195 To => userinfo_get($user_name, "email"),
196 From => "$config{wikiname} admin <$config{adminemail}>",
197 Subject => "$config{wikiname} information",
198 Message => $template->output,
199 ) or error("Failed to send mail");
201 $form->text("Your password has been emailed to you.");
202 $form->field(name => "name", required => 0);
203 print $session->header(-charset=>'utf-8');
204 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
208 print $session->header(-charset=>'utf-8');
209 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
213 sub cgi_prefs ($$) { #{{{
217 eval q{use CGI::FormBuilder};
218 my $form = CGI::FormBuilder->new(
219 title => "preferences",
220 fields => [qw(do name password confirm_password email
221 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 {template_params("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(-charset=>'utf-8');
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 editcontent comments)],
298 editcontent => '/.+/',
300 required => [qw{editcontent}],
303 action => $config{cgiurl},
305 template => {template_params("editpage.tmpl")},
307 my @buttons=("Save Page", "Preview", "Cancel");
309 # This untaint is safe because titlepage removes any problimatic
311 my ($page)=Encode::decode_utf8($form->param('page'));
312 $page=titlepage(possibly_foolish_untaint(lc($page)));
313 if (! defined $page || ! length $page ||
314 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
315 error("bad page name");
319 my $file=$page.$config{default_pageext};
320 if (exists $pagesources{lc($page)}) {
321 $file=$pagesources{lc($page)};
324 if (! -e "$config{srcdir}/$file") {
328 $form->field(name => "do", type => 'hidden');
329 $form->field(name => "from", type => 'hidden');
330 $form->field(name => "rcsinfo", type => 'hidden');
331 $form->field(name => "subpage", type => 'hidden');
332 $form->field(name => "page", value => "$page", force => 1);
333 $form->field(name => "comments", type => "text", size => 80);
334 $form->field(name => "editcontent", type => "textarea", rows => 20,
336 $form->tmpl_param("can_commit", $config{rcs});
337 $form->tmpl_param("indexlink", indexlink());
338 $form->tmpl_param("helponformattinglink",
339 htmllink("", "", "HelpOnFormatting", 1));
340 $form->tmpl_param("styleurl", styleurl());
341 $form->tmpl_param("baseurl", "$config{url}/");
342 if (! $form->submitted) {
343 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
347 if ($form->submitted eq "Cancel") {
348 print $q->redirect("$config{url}/".htmlpage($page));
351 elsif ($form->submitted eq "Preview") {
352 require IkiWiki::Render;
353 # Apparently FormBuilder doesn't not treat input as
354 # utf-8, so decode from it.
356 my $content = Encode::decode_utf8($form->field('editcontent'));
357 my $comments = Encode::decode_utf8($form->field('comments'));
358 $form->field(name => "editcontent",
359 value => $content, force => 1);
360 $form->field(name => "comments",
361 value => $comments, force => 1);
362 $form->tmpl_param("page_preview",
363 htmlize($config{default_pageext},
364 linkify($page, $page, $content)));
367 $form->tmpl_param("page_preview", "");
369 $form->tmpl_param("page_conflict", "");
371 if (! $form->submitted || $form->submitted eq "Preview" ||
373 if ($form->field("do") eq "create") {
376 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
377 if (! defined $from || ! length $from ||
378 $from ne $form->param('from') ||
379 $from=~/$config{wiki_file_prune_regexp}/ ||
381 $form->submitted eq "Preview") {
382 @page_locs=$best_loc=$page;
388 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
389 $page eq 'discussion') {
390 $best_loc="$from/$page";
393 $best_loc=$dir.$page;
396 push @page_locs, $dir.$page;
397 push @page_locs, "$from/$page";
398 while (length $dir) {
400 push @page_locs, $dir.$page;
405 ! exists $pagesources{lc($_)} &&
406 ! page_locked($_, $session, 1)
410 # hmm, someone else made the page in the
412 print $q->redirect("$config{url}/".htmlpage($page));
416 $form->tmpl_param("page_select", 1);
417 $form->field(name => "page", type => 'select',
418 options => \@page_locs, value => $best_loc);
419 $form->title("creating ".pagetitle($page));
421 elsif ($form->field("do") eq "edit") {
422 page_locked($page, $session);
423 if (! defined $form->field('editcontent') ||
424 ! length $form->field('editcontent')) {
426 if (exists $pagesources{lc($page)}) {
427 $content=readfile(srcfile($pagesources{lc($page)}));
428 $content=~s/\n/\r\n/g;
430 $form->field(name => "editcontent", value => $content,
433 $form->tmpl_param("page_select", 0);
434 $form->field(name => "page", type => 'hidden');
435 $form->title("editing ".pagetitle($page));
438 print $form->render(submit => \@buttons);
442 page_locked($page, $session);
444 # Decode utf-8 since FormBuilder does not
446 my $content=Encode::decode_utf8($form->field('editcontent'));
448 $content=~s/\r\n/\n/g;
450 writefile($file, $config{srcdir}, $content);
452 my $message="web commit ";
453 if (defined $session->param("name") &&
454 length $session->param("name")) {
455 $message.="by ".$session->param("name");
458 $message.="from $ENV{REMOTE_ADDR}";
460 if (defined $form->field('comments') &&
461 length $form->field('comments')) {
462 $message.=": ".Encode::decode_utf8($form->field('comments'));
469 # prevent deadlock with post-commit hook
471 # presumably the commit will trigger an update
473 my $conflict=rcs_commit($file, $message,
474 $form->field("rcsinfo"));
476 if (defined $conflict) {
477 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
479 $form->tmpl_param("page_conflict", 1);
480 $form->field("editcontent", value => $conflict, force => 1);
481 $form->field(name => "comments", value => Encode::decode_utf8($form->field('comments')), force => 1);
482 $form->field("do", "edit)");
483 $form->tmpl_param("page_select", 0);
484 $form->field(name => "page", type => 'hidden');
485 $form->title("editing $page");
486 print $form->render(submit => \@buttons);
491 require IkiWiki::Render;
496 # The trailing question mark tries to avoid broken
497 # caches and get the most recent version of the page.
498 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
504 eval q{use CGI::Session};
508 if (exists $hooks{cgi}) {
509 foreach my $id (keys %{$hooks{cgi}}) {
510 $hooks{cgi}{$id}{call}->($q);
514 my $do=$q->param('do');
515 if (! defined $do || ! length $do) {
516 error("\"do\" parameter missing");
519 # Things that do not need a session.
520 if ($do eq 'recentchanges') {
521 cgi_recentchanges($q);
524 elsif ($do eq 'hyperestraier') {
528 CGI::Session->name("ikiwiki_session_$config{wikiname}");
530 my $oldmask=umask(077);
531 my $session = CGI::Session->new("driver:DB_File", $q,
532 { FileName => "$config{wikistatedir}/sessions.db" });
535 # Everything below this point needs the user to be signed in.
536 if ((! $config{anonok} &&
537 (! defined $session->param("name") ||
538 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
539 cgi_signin($q, $session);
541 # Force session flush with safe umask.
542 my $oldmask=umask(077);
549 if ($do eq 'create' || $do eq 'edit') {
550 cgi_editpage($q, $session);
552 elsif ($do eq 'prefs') {
553 cgi_prefs($q, $session);
555 elsif ($do eq 'blog') {
556 my $page=titlepage(lc($q->param('title')));
557 # if the page already exists, munge it to be unique
558 my $from=$q->param('from');
560 while (exists $oldpagemtime{"$from/$page$add"}) {
561 $add=1 unless length $add;
564 $q->param('page', $page.$add);
565 # now run same as create
566 $q->param('do', 'create');
567 cgi_editpage($q, $session);
570 error("unknown do parameter");