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=HTML::Template->new(
37 filename => "$config{templatedir}/recentchanges.tmpl"
40 title => "RecentChanges",
41 indexlink => indexlink(),
42 wikiname => $config{wikiname},
43 changelog => [rcs_recentchanges(100)],
44 styleurl => styleurl(),
45 baseurl => "$config{url}/",
48 print $q->header(-charset=>'utf-8'), Encode::decode_utf8($template->output);
51 sub cgi_signin ($$) { #{{{
55 eval q{use CGI::FormBuilder};
56 my $form = CGI::FormBuilder->new(
58 fields => [qw(do title page subpage from name password confirm_password email)],
64 perl => q{eq $form->field("password")},
71 action => $config{cgiurl},
73 template => (-e "$config{templatedir}/signin.tmpl" ?
74 "$config{templatedir}/signin.tmpl" : ""),
75 stylesheet => styleurl(),
78 $form->field(name => "name", required => 0);
79 $form->field(name => "do", type => "hidden");
80 $form->field(name => "page", type => "hidden");
81 $form->field(name => "title", type => "hidden");
82 $form->field(name => "from", type => "hidden");
83 $form->field(name => "subpage", type => "hidden");
84 $form->field(name => "password", type => "password", required => 0);
85 $form->field(name => "confirm_password", type => "password", required => 0);
86 $form->field(name => "email", required => 0);
87 if ($q->param("do") ne "signin") {
88 $form->text("You need to log in first.");
91 if ($form->submitted) {
92 # Set required fields based on how form was submitted.
94 "Login" => [qw(name password)],
95 "Register" => [qw(name password confirm_password email)],
96 "Mail Password" => [qw(name)],
98 foreach my $opt (@{$required{$form->submitted}}) {
99 $form->field(name => $opt, required => 1);
102 # Validate password differently depending on how
103 # form was submitted.
104 if ($form->submitted eq 'Login') {
108 length $form->field("name") &&
109 shift eq userinfo_get($form->field("name"), 'password');
112 $form->field(name => "name", validate => '/^\w+$/');
115 $form->field(name => "password", validate => 'VALUE');
117 # And make sure the entered name exists when logging
118 # in or sending email, and does not when registering.
119 if ($form->submitted eq 'Register') {
125 $name=~/$config{wiki_file_regexp}/ &&
126 ! userinfo_get($name, "regdate");
136 userinfo_get($name, "regdate");
142 # First time settings.
143 $form->field(name => "name", comment => "use FirstnameLastName");
144 $form->field(name => "confirm_password", comment => "(only needed");
145 $form->field(name => "email", comment => "for registration)");
146 if ($session->param("name")) {
147 $form->field(name => "name", value => $session->param("name"));
151 if ($form->submitted && $form->validate) {
152 if ($form->submitted eq 'Login') {
153 $session->param("name", $form->field("name"));
154 if (defined $form->field("do") &&
155 $form->field("do") ne 'signin') {
156 print $q->redirect(cgiurl(
157 do => $form->field("do"),
158 page => $form->field("page"),
159 title => $form->field("title"),
160 subpage => $form->field("subpage"),
161 from => $form->field("from"),
165 print $q->redirect($config{url});
168 elsif ($form->submitted eq 'Register') {
169 my $user_name=$form->field('name');
170 if (userinfo_setall($user_name, {
171 'email' => $form->field('email'),
172 'password' => $form->field('password'),
175 $form->field(name => "confirm_password", type => "hidden");
176 $form->field(name => "email", type => "hidden");
177 $form->text("Registration successful. Now you can Login.");
178 print $session->header(-charset=>'utf-8');
179 print misctemplate($form->title, $form->render(submit => ["Login"]));
182 error("Error saving registration.");
185 elsif ($form->submitted eq 'Mail Password') {
186 my $user_name=$form->field("name");
187 my $template=HTML::Template->new(
188 filename => "$config{templatedir}/passwordmail.tmpl"
191 user_name => $user_name,
192 user_password => userinfo_get($user_name, "password"),
193 wikiurl => $config{url},
194 wikiname => $config{wikiname},
195 REMOTE_ADDR => $ENV{REMOTE_ADDR},
198 eval q{use Mail::Sendmail};
200 To => userinfo_get($user_name, "email"),
201 From => "$config{wikiname} admin <$config{adminemail}>",
202 Subject => "$config{wikiname} information",
203 Message => $template->output,
204 ) or error("Failed to send mail");
206 $form->text("Your password has been emailed to you.");
207 $form->field(name => "name", required => 0);
208 print $session->header(-charset=>'utf-8');
209 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
213 print $session->header(-charset=>'utf-8');
214 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
218 sub cgi_prefs ($$) { #{{{
222 eval q{use CGI::FormBuilder};
223 my $form = CGI::FormBuilder->new(
224 title => "preferences",
225 fields => [qw(do name password confirm_password email
226 subscriptions locked_pages)],
231 confirm_password => {
232 perl => q{eq $form->field("password")},
239 action => $config{cgiurl},
240 template => (-e "$config{templatedir}/prefs.tmpl" ?
241 "$config{templatedir}/prefs.tmpl" : ""),
242 stylesheet => styleurl(),
244 my @buttons=("Save Preferences", "Logout", "Cancel");
246 my $user_name=$session->param("name");
247 $form->field(name => "do", type => "hidden");
248 $form->field(name => "name", disabled => 1,
249 value => $user_name, force => 1);
250 $form->field(name => "password", type => "password");
251 $form->field(name => "confirm_password", type => "password");
252 $form->field(name => "subscriptions", size => 50,
253 comment => "(".htmllink("", "", "GlobList", 1).")");
254 $form->field(name => "locked_pages", size => 50,
255 comment => "(".htmllink("", "", "GlobList", 1).")");
257 if (! is_admin($user_name)) {
258 $form->field(name => "locked_pages", type => "hidden");
261 if (! $form->submitted) {
262 $form->field(name => "email", force => 1,
263 value => userinfo_get($user_name, "email"));
264 $form->field(name => "subscriptions", force => 1,
265 value => userinfo_get($user_name, "subscriptions"));
266 $form->field(name => "locked_pages", force => 1,
267 value => userinfo_get($user_name, "locked_pages"));
270 if ($form->submitted eq 'Logout') {
272 print $q->redirect($config{url});
275 elsif ($form->submitted eq 'Cancel') {
276 print $q->redirect($config{url});
279 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
280 foreach my $field (qw(password email subscriptions locked_pages)) {
281 if (length $form->field($field)) {
282 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
285 $form->text("Preferences saved.");
288 print $session->header(-charset=>'utf-8');
289 print misctemplate($form->title, $form->render(submit => \@buttons));
292 sub cgi_editpage ($$) { #{{{
296 eval q{use CGI::FormBuilder};
297 my $form = CGI::FormBuilder->new(
298 fields => [qw(do rcsinfo subpage from page editcontent comments)],
303 editcontent => '/.+/',
305 required => [qw{editcontent}],
308 action => $config{cgiurl},
310 template => "$config{templatedir}/editpage.tmpl"
312 my @buttons=("Save Page", "Preview", "Cancel");
314 # This untaint is safe because titlepage removes any problimatic
316 my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
317 if (! defined $page || ! length $page ||
318 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
319 error("bad page name");
323 my $file=$page.$config{default_pageext};
324 if (exists $pagesources{lc($page)}) {
325 $file=$pagesources{lc($page)};
328 if (! -e "$config{srcdir}/$file") {
332 $form->field(name => "do", type => 'hidden');
333 $form->field(name => "from", type => 'hidden');
334 $form->field(name => "rcsinfo", type => 'hidden');
335 $form->field(name => "subpage", type => 'hidden');
336 $form->field(name => "page", value => "$page", force => 1);
337 $form->field(name => "comments", type => "text", size => 80);
338 $form->field(name => "editcontent", type => "textarea", rows => 20,
340 $form->tmpl_param("can_commit", $config{rcs});
341 $form->tmpl_param("indexlink", indexlink());
342 $form->tmpl_param("helponformattinglink",
343 htmllink("", "", "HelpOnFormatting", 1));
344 $form->tmpl_param("styleurl", styleurl());
345 $form->tmpl_param("baseurl", "$config{url}/");
346 if (! $form->submitted) {
347 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
351 if ($form->submitted eq "Cancel") {
352 print $q->redirect("$config{url}/".htmlpage($page));
355 elsif ($form->submitted eq "Preview") {
356 require IkiWiki::Render;
357 # Apparently FormBuilder doesn't not treat input as
358 # utf-8, so decode from it.
360 my $content = Encode::decode_utf8($form->field('editcontent'));
361 $form->field(name => "editcontent",
362 value => $content, force => 1);
363 $form->field(name => "comments",
364 value => Encode::decode_utf8($form->field('comments')), force => 1);
365 $form->tmpl_param("page_preview",
366 htmlize($config{default_pageext},
367 linkify($page, $page, $content)));
370 $form->tmpl_param("page_preview", "");
372 $form->tmpl_param("page_conflict", "");
374 if (! $form->submitted || $form->submitted eq "Preview" ||
376 if ($form->field("do") eq "create") {
379 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
380 if (! defined $from || ! length $from ||
381 $from ne $form->param('from') ||
382 $from=~/$config{wiki_file_prune_regexp}/ ||
384 $form->submitted eq "Preview") {
385 @page_locs=$best_loc=$page;
391 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
392 $page eq 'discussion') {
393 $best_loc="$from/$page";
396 $best_loc=$dir.$page;
399 push @page_locs, $dir.$page;
400 push @page_locs, "$from/$page";
401 while (length $dir) {
403 push @page_locs, $dir.$page;
408 ! exists $pagesources{lc($_)} &&
409 ! page_locked($_, $session, 1)
413 # hmm, someone else made the page in the
415 print $q->redirect("$config{url}/".htmlpage($page));
419 $form->tmpl_param("page_select", 1);
420 $form->field(name => "page", type => 'select',
421 options => \@page_locs, value => $best_loc);
422 $form->title("creating ".pagetitle($page));
424 elsif ($form->field("do") eq "edit") {
425 page_locked($page, $session);
426 if (! defined $form->field('editcontent') ||
427 ! length $form->field('editcontent')) {
429 if (exists $pagesources{lc($page)}) {
430 $content=readfile(srcfile($pagesources{lc($page)}));
431 $content=~s/\n/\r\n/g;
433 $form->field(name => "editcontent", value => $content,
436 $form->tmpl_param("page_select", 0);
437 $form->field(name => "page", type => 'hidden');
438 $form->title("editing ".pagetitle($page));
441 print $form->render(submit => \@buttons);
445 page_locked($page, $session);
447 # Decode utf-8 since FormBuilder does not
449 my $content=Encode::decode_utf8($form->field('editcontent'));
451 $content=~s/\r\n/\n/g;
453 writefile($file, $config{srcdir}, $content);
455 my $message="web commit ";
456 if (defined $session->param("name") &&
457 length $session->param("name")) {
458 $message.="by ".$session->param("name");
461 $message.="from $ENV{REMOTE_ADDR}";
463 if (defined $form->field('comments') &&
464 length $form->field('comments')) {
465 $message.=Encode::decode_utf8(": ".$form->field('comments'));
472 # prevent deadlock with post-commit hook
474 # presumably the commit will trigger an update
476 my $conflict=rcs_commit($file, $message,
477 $form->field("rcsinfo"));
479 if (defined $conflict) {
480 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
482 $form->tmpl_param("page_conflict", 1);
483 $form->field("editcontent", value => $conflict, force => 1);
484 $form->field("do", "edit)");
485 $form->tmpl_param("page_select", 0);
486 $form->field(name => "page", type => 'hidden');
487 $form->title("editing $page");
488 print $form->render(submit => \@buttons);
493 require IkiWiki::Render;
498 # The trailing question mark tries to avoid broken
499 # caches and get the most recent version of the page.
500 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
506 eval q{use CGI::Session};
510 if (exists $hooks{cgi}) {
511 foreach my $id (keys %{$hooks{cgi}}) {
512 $hooks{cgi}{$id}{call}->($q);
516 my $do=$q->param('do');
517 if (! defined $do || ! length $do) {
518 error("\"do\" parameter missing");
521 # Things that do not need a session.
522 if ($do eq 'recentchanges') {
523 cgi_recentchanges($q);
526 elsif ($do eq 'hyperestraier') {
530 CGI::Session->name("ikiwiki_session_$config{wikiname}");
532 my $oldmask=umask(077);
533 my $session = CGI::Session->new("driver:DB_File", $q,
534 { FileName => "$config{wikistatedir}/sessions.db" });
537 # Everything below this point needs the user to be signed in.
538 if ((! $config{anonok} &&
539 (! defined $session->param("name") ||
540 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
541 cgi_signin($q, $session);
543 # Force session flush with safe umask.
544 my $oldmask=umask(077);
551 if ($do eq 'create' || $do eq 'edit') {
552 cgi_editpage($q, $session);
554 elsif ($do eq 'prefs') {
555 cgi_prefs($q, $session);
557 elsif ($do eq 'blog') {
558 my $page=titlepage(lc($q->param('title')));
559 # if the page already exists, munge it to be unique
560 my $from=$q->param('from');
562 while (exists $oldpagemtime{"$from/$page$add"}) {
563 $add=1 unless length $add;
566 $q->param('page', $page.$add);
567 # now run same as create
568 $q->param('do', 'create');
569 cgi_editpage($q, $session);
572 error("unknown do parameter");