* Add sessioncgi hook.
[ikiwiki.git] / IkiWiki / CGI.pm
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use IkiWiki;
6 use IkiWiki::UserInfo;
7 use open qw{:utf8 :std};
8 use Encode;
9
10 package IkiWiki;
11
12 sub printheader ($) { #{{{
13         my $session=shift;
14         
15         if ($config{sslcookie}) {
16                 print $session->header(-charset => 'utf-8',
17                         -cookie => $session->cookie(-secure => 1));
18         } else {
19                 print $session->header(-charset => 'utf-8');
20         }
21
22 } #}}}
23
24 sub redirect ($$) { #{{{
25         my $q=shift;
26         my $url=shift;
27         if (! $config{w3mmode}) {
28                 print $q->redirect($url);
29         }
30         else {
31                 print "Content-type: text/plain\n";
32                 print "W3m-control: GOTO $url\n\n";
33         }
34 } #}}}
35
36 sub check_canedit ($$$;$) { #{{{
37         my $page=shift;
38         my $q=shift;
39         my $session=shift;
40         my $nonfatal=shift;
41         
42         my $canedit;
43         run_hooks(canedit => sub {
44                 return if defined $canedit;
45                 my $ret=shift->($page, $q, $session);
46                 if (defined $ret && $ret eq "") {
47                         $canedit=1;
48                 }
49                 elsif (defined $ret) {
50                         $canedit=0;
51                         error($ret) unless $nonfatal;
52                 }
53         });
54         return $canedit;
55 } #}}}
56
57 sub decode_form_utf8 ($) { #{{{
58         my $form = shift;
59         foreach my $f ($form->field) {
60                 next if Encode::is_utf8(scalar $form->field($f));
61                 $form->field(name  => $f,
62                              value => decode_utf8($form->field($f)),
63                              force => 1,
64                             );
65         }
66 } #}}}
67
68 sub cgi_recentchanges ($) { #{{{
69         my $q=shift;
70         
71         # Optimisation: building recentchanges means calculating lots of
72         # links. Memoizing htmllink speeds it up a lot (can't be memoized
73         # during page builds as the return values may change, but they
74         # won't here.)
75         eval q{use Memoize};
76         error($@) if $@;
77         memoize("htmllink");
78
79         eval q{use Time::Duration};
80         error($@) if $@;
81
82         my $changelog=[rcs_recentchanges(100)];
83         foreach my $change (@$changelog) {
84                 $change->{when} = concise(ago($change->{when}));
85
86                 $change->{user} = userlink($change->{user});
87
88                 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
89                 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
90                 $change->{pages} = [
91                         map {
92                                 $_->{link} = htmllink("", "", $_->{page},
93                                         noimageinline => 1,
94                                         linktext => pagetitle($_->{page}));
95                                 $_;
96                         } @{$change->{pages}}
97                 ];
98                 push @{$change->{pages}}, { link => '...' } if $is_excess;
99         }
100
101         my $template=template("recentchanges.tmpl"); 
102         $template->param(
103                 title => "RecentChanges",
104                 indexlink => indexlink(),
105                 wikiname => $config{wikiname},
106                 changelog => $changelog,
107                 baseurl => baseurl(),
108         );
109         run_hooks(pagetemplate => sub {
110                 shift->(page => "", destpage => "", template => $template);
111         });
112         print $q->header(-charset => 'utf-8'), $template->output;
113 } #}}}
114
115 # Check if the user is signed in. If not, redirect to the signin form and
116 # save their place to return to later.
117 sub needsignin ($$) { #{{{
118         my $q=shift;
119         my $session=shift;
120
121         if (! defined $session->param("name") ||
122             ! userinfo_get($session->param("name"), "regdate")) {
123                 if (! defined $session->param("postsignin")) {
124                         $session->param(postsignin => $ENV{QUERY_STRING});
125                 }
126                 cgi_signin($q, $session);
127                 cgi_savesession($session);
128                 exit;
129         }
130 } #}}}  
131
132 sub cgi_signin ($$) { #{{{
133         my $q=shift;
134         my $session=shift;
135
136         eval q{use CGI::FormBuilder};
137         error($@) if $@;
138         my $form = CGI::FormBuilder->new(
139                 title => "signin",
140                 name => "signin",
141                 header => 1,
142                 charset => "utf-8",
143                 method => 'POST',
144                 required => 'NONE',
145                 javascript => 0,
146                 params => $q,
147                 action => $config{cgiurl},
148                 header => 0,
149                 template => {type => 'div'},
150                 stylesheet => baseurl()."style.css",
151         );
152         my $buttons=["Login"];
153         
154         if ($q->param("do") ne "signin" && !$form->submitted) {
155                 $form->text(gettext("You need to log in first."));
156         }
157         $form->field(name => "do", type => "hidden", value => "signin",
158                 force => 1);
159         
160         run_hooks(formbuilder_setup => sub {
161                 shift->(form => $form, cgi => $q, session => $session);
162         });
163         
164         decode_form_utf8($form);
165
166         if (exists $hooks{formbuilder}) {
167                 run_hooks(formbuilder => sub {
168                         shift->(form => $form, cgi => $q, session => $session,
169                                 buttons => $buttons);
170                 });
171         }
172         else {
173                 if ($form->submitted) {
174                         $form->validate;
175                 }
176                 printheader($session);
177                 print misctemplate($form->title, $form->render(submit => $buttons));
178         }
179 } #}}}
180
181 sub cgi_postsignin ($$) { #{{{
182         my $q=shift;
183         my $session=shift;
184         
185         # Continue with whatever was being done before the signin process.
186         if (defined $session->param("postsignin")) {
187                 my $postsignin=CGI->new($session->param("postsignin"));
188                 $session->clear("postsignin");
189                 cgi($postsignin, $session);
190                 cgi_savesession($session);
191                 exit;
192         }
193         else {
194                 # This can occur, for example, if a user went to the signin
195                 # url via a bookmark.
196                 redirect($q, $config{url});
197         }
198 } #}}}
199
200 sub cgi_prefs ($$) { #{{{
201         my $q=shift;
202         my $session=shift;
203
204         needsignin($q, $session);
205
206         eval q{use CGI::FormBuilder};
207         error($@) if $@;
208         my $form = CGI::FormBuilder->new(
209                 title => "preferences",
210                 name => "preferences",
211                 header => 0,
212                 charset => "utf-8",
213                 method => 'POST',
214                 validate => {
215                         email => 'EMAIL',
216                 },
217                 required => 'NONE',
218                 javascript => 0,
219                 params => $q,
220                 action => $config{cgiurl},
221                 template => {type => 'div'},
222                 stylesheet => baseurl()."style.css",
223                 fieldsets => [
224                         [login => gettext("Login")],
225                         [preferences => gettext("Preferences")],
226                         [admin => gettext("Admin")]
227                 ],
228         );
229         my $buttons=["Save Preferences", "Logout", "Cancel"];
230
231         run_hooks(formbuilder_setup => sub {
232                 shift->(form => $form, cgi => $q, session => $session);
233         });
234         
235         $form->field(name => "do", type => "hidden");
236         $form->field(name => "email", size => 50, fieldset => "preferences");
237         $form->field(name => "subscriptions", size => 50,
238                 fieldset => "preferences",
239                 comment => "(".htmllink("", "", "PageSpec", noimageinline => 1).")");
240         $form->field(name => "banned_users", size => 50,
241                 fieldset => "admin");
242         
243         my $user_name=$session->param("name");
244         if (! is_admin($user_name)) {
245                 $form->field(name => "banned_users", type => "hidden");
246         }
247
248         if (! $form->submitted) {
249                 $form->field(name => "email", force => 1,
250                         value => userinfo_get($user_name, "email"));
251                 $form->field(name => "subscriptions", force => 1,
252                         value => userinfo_get($user_name, "subscriptions"));
253                 if (is_admin($user_name)) {
254                         $form->field(name => "banned_users", force => 1,
255                                 value => join(" ", get_banned_users()));
256                 }
257         }
258         
259         decode_form_utf8($form);
260         
261         if ($form->submitted eq 'Logout') {
262                 $session->delete();
263                 redirect($q, $config{url});
264                 return;
265         }
266         elsif ($form->submitted eq 'Cancel') {
267                 redirect($q, $config{url});
268                 return;
269         }
270         elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
271                 foreach my $field (qw(email subscriptions)) {
272                         if (defined $form->field($field) && length $form->field($field)) {
273                                 userinfo_set($user_name, $field, $form->field($field)) ||
274                                         error("failed to set $field");
275                         }
276                 }
277                 if (is_admin($user_name)) {
278                         set_banned_users(grep { ! is_admin($_) }
279                                         split(' ',
280                                                 $form->field("banned_users"))) ||
281                                 error("failed saving changes");
282                 }
283                 $form->text(gettext("Preferences saved."));
284         }
285         
286         if (exists $hooks{formbuilder}) {
287                 run_hooks(formbuilder => sub {
288                         shift->(form => $form, cgi => $q, session => $session,
289                                 buttons => $buttons);
290                 });
291         }
292         else {
293                 printheader($session);
294                 print misctemplate($form->title, $form->render(submit => $buttons));
295         }
296 } #}}}
297
298 sub cgi_editpage ($$) { #{{{
299         my $q=shift;
300         my $session=shift;
301
302         my @fields=qw(do rcsinfo subpage from page type editcontent comments);
303         my @buttons=("Save Page", "Preview", "Cancel");
304         
305         eval q{use CGI::FormBuilder};
306         error($@) if $@;
307         my $form = CGI::FormBuilder->new(
308                 fields => \@fields,
309                 header => 1,
310                 charset => "utf-8",
311                 method => 'POST',
312                 validate => {
313                         editcontent => '/.+/',
314                 },
315                 required => [qw{editcontent}],
316                 javascript => 0,
317                 params => $q,
318                 action => $config{cgiurl},
319                 header => 0,
320                 table => 0,
321                 template => scalar template_params("editpage.tmpl"),
322                 wikiname => $config{wikiname},
323         );
324         
325         run_hooks(formbuilder_setup => sub {
326                 shift->(form => $form, cgi => $q, session => $session);
327         });
328         
329         decode_form_utf8($form);
330         
331         # This untaint is safe because titlepage removes any problematic
332         # characters.
333         my ($page)=$form->field('page');
334         $page=titlepage(possibly_foolish_untaint($page));
335         if (! defined $page || ! length $page || file_pruned($page, $config{srcdir}) || $page=~/^\//) {
336                 error("bad page name");
337         }
338         
339         my $from;
340         if (defined $form->field('from')) {
341                 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
342         }
343         
344         my $file;
345         my $type;
346         if (exists $pagesources{$page} && $form->field("do") ne "create") {
347                 $file=$pagesources{$page};
348                 $type=pagetype($file);
349                 if (! defined $type) {
350                         error(sprintf(gettext("%s is not an editable page"), $page));
351                 }
352                 if (! $form->submitted) {
353                         $form->field(name => "rcsinfo",
354                                 value => rcs_prepedit($file), force => 1);
355                 }
356         }
357         else {
358                 $type=$form->param('type');
359                 if (defined $type && length $type && $hooks{htmlize}{$type}) {
360                         $type=possibly_foolish_untaint($type);
361                 }
362                 elsif (defined $from) {
363                         # favor the type of linking page
364                         $type=pagetype($pagesources{$from});
365                 }
366                 $type=$config{default_pageext} unless defined $type;
367                 $file=$page.".".$type;
368                 if (! $form->submitted) {
369                         $form->field(name => "rcsinfo", value => "", force => 1);
370                 }
371         }
372
373         $form->field(name => "do", type => 'hidden');
374         $form->field(name => "from", type => 'hidden');
375         $form->field(name => "rcsinfo", type => 'hidden');
376         $form->field(name => "subpage", type => 'hidden');
377         $form->field(name => "page", value => pagetitle($page, 1), force => 1);
378         $form->field(name => "type", value => $type, force => 1);
379         $form->field(name => "comments", type => "text", size => 80);
380         $form->field(name => "editcontent", type => "textarea", rows => 20,
381                 cols => 80);
382         $form->tmpl_param("can_commit", $config{rcs});
383         $form->tmpl_param("indexlink", indexlink());
384         $form->tmpl_param("helponformattinglink",
385                 htmllink("", "", "HelpOnFormatting", noimageinline => 1));
386         $form->tmpl_param("baseurl", baseurl());
387         
388         if ($form->submitted eq "Cancel") {
389                 if ($form->field("do") eq "create" && defined $from) {
390                         redirect($q, "$config{url}/".htmlpage($from));
391                 }
392                 elsif ($form->field("do") eq "create") {
393                         redirect($q, $config{url});
394                 }
395                 else {
396                         redirect($q, "$config{url}/".htmlpage($page));
397                 }
398                 return;
399         }
400         elsif ($form->submitted eq "Preview") {
401                 $form->tmpl_param("page_preview",
402                         htmlize($page, $type,
403                         linkify($page, "",
404                         preprocess($page, $page,
405                         filter($page, $page, $form->field('editcontent')), 0, 1))));
406         }
407         else {
408                 $form->tmpl_param("page_preview", "");
409         }
410         $form->tmpl_param("page_conflict", "");
411         
412         if (! $form->submitted || $form->submitted eq "Preview" || 
413             ! $form->validate) {
414                 if ($form->field("do") eq "create") {
415                         my @page_locs;
416                         my $best_loc;
417                         if (! defined $from || ! length $from ||
418                             $from ne $form->field('from') ||
419                             file_pruned($from, $config{srcdir}) ||
420                             $from=~/^\// ||
421                             $form->submitted eq "Preview") {
422                                 @page_locs=$best_loc=$page;
423                         }
424                         else {
425                                 my $dir=$from."/";
426                                 $dir=~s![^/]+/+$!!;
427                                 
428                                 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
429                                     $page eq gettext('discussion')) {
430                                         $best_loc="$from/$page";
431                                 }
432                                 else {
433                                         $best_loc=$dir.$page;
434                                 }
435                                 
436                                 push @page_locs, $dir.$page;
437                                 push @page_locs, "$from/$page";
438                                 while (length $dir) {
439                                         $dir=~s![^/]+/+$!!;
440                                         push @page_locs, $dir.$page;
441                                 }
442                         }
443                         push @page_locs, "$config{userdir}/$page"
444                                 if length $config{userdir};
445
446                         @page_locs = grep {
447                                 ! exists $pagecase{lc $_}
448                         } @page_locs;
449                         if (! @page_locs) {
450                                 # hmm, someone else made the page in the
451                                 # meantime?
452                                 redirect($q, "$config{url}/".htmlpage($page));
453                                 return;
454                         }
455
456                         my @editable_locs = grep {
457                                 check_canedit($_, $q, $session, 1)
458                         } @page_locs;
459                         if (! @editable_locs) {
460                                 # let it throw an error this time
461                                 map { check_canedit($_, $q, $session) } @page_locs;
462                         }
463                         
464                         my @page_types;
465                         if (exists $hooks{htmlize}) {
466                                 @page_types=keys %{$hooks{htmlize}};
467                         }
468                         
469                         $form->tmpl_param("page_select", 1);
470                         $form->field(name => "page", type => 'select',
471                                 options => [ map { pagetitle($_, 1) } @editable_locs ],
472                                 value => pagetitle($best_loc, 1));
473                         $form->field(name => "type", type => 'select',
474                                 options => \@page_types);
475                         $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
476                         
477                 }
478                 elsif ($form->field("do") eq "edit") {
479                         check_canedit($page, $q, $session);
480                         if (! defined $form->field('editcontent') || 
481                             ! length $form->field('editcontent')) {
482                                 my $content="";
483                                 if (exists $pagesources{$page}) {
484                                         $content=readfile(srcfile($pagesources{$page}));
485                                         $content=~s/\n/\r\n/g;
486                                 }
487                                 $form->field(name => "editcontent", value => $content,
488                                         force => 1);
489                         }
490                         $form->tmpl_param("page_select", 0);
491                         $form->field(name => "page", type => 'hidden');
492                         $form->field(name => "type", type => 'hidden');
493                         $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
494                 }
495                 
496                 printheader($session);
497                 print misctemplate($form->title, $form->render(submit => \@buttons));
498         }
499         else {
500                 # save page
501                 check_canedit($page, $q, $session);
502                 if (! -e "$config{srcdir}/$file" &&
503                     $form->field("do") ne "create") {
504                         $form->tmpl_param("page_gone", 1);
505                         $form->field(name => "do", value => "create", force => 1);
506                         $form->tmpl_param("page_select", 0);
507                         $form->field(name => "page", type => 'hidden');
508                         $form->field(name => "type", type => 'hidden');
509                         $form->title(sprintf(gettext("editing %s"), $page));
510                         printheader($session);
511                         print misctemplate($form->title, $form->render(submit => \@buttons));
512                         return;
513                 }
514                 elsif (-e "$config{srcdir}/$file" &&
515                        $form->field("do") eq "create") {
516                         $form->tmpl_param("creation_conflict", 1);
517                         $form->field(name => "do", value => "edit", force => 1);
518                         $form->tmpl_param("page_select", 0);
519                         $form->field(name => "page", type => 'hidden');
520                         $form->field(name => "type", type => 'hidden');
521                         $form->title(sprintf(gettext("editing %s"), $page));
522                         $form->field("editcontent", 
523                                 value => readfile("$config{srcdir}/$file").
524                                          "\n\n\n".$form->field("editcontent"),
525                                 force => 1);
526                         printheader($session);
527                         print misctemplate($form->title, $form->render(submit => \@buttons));
528                         return;
529                 }
530                 
531                 my $content=$form->field('editcontent');
532
533                 $content=~s/\r\n/\n/g;
534                 $content=~s/\r/\n/g;
535
536                 $config{cgi}=0; # avoid cgi error message
537                 eval { writefile($file, $config{srcdir}, $content) };
538                 $config{cgi}=1;
539                 if ($@) {
540                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
541                                 force => 1);
542                         $form->tmpl_param("failed_save", 1);
543                         $form->tmpl_param("error_message", $@);
544                         $form->field("editcontent", value => $content, force => 1);
545                         $form->tmpl_param("page_select", 0);
546                         $form->field(name => "page", type => 'hidden');
547                         $form->field(name => "type", type => 'hidden');
548                         $form->title(sprintf(gettext("editing %s"), $page));
549                         printheader($session);
550                         print misctemplate($form->title, $form->render(submit => \@buttons));
551                         return;
552                 }
553                 
554                 my $conflict;
555                 if ($config{rcs}) {
556                         my $message="";
557                         if (defined $form->field('comments') &&
558                             length $form->field('comments')) {
559                                 $message=$form->field('comments');
560                         }
561                         
562                         if ($form->field("do") eq "create") {
563                                 rcs_add($file);
564                         }
565
566                         # Prevent deadlock with post-commit hook by
567                         # signaling to it that it should not try to
568                         # do anything (except send commit mails).
569                         disable_commit_hook();
570                         $conflict=rcs_commit($file, $message,
571                                 $form->field("rcsinfo"),
572                                 $session->param("name"), $ENV{REMOTE_ADDR});
573                         enable_commit_hook();
574                         rcs_update();
575                 }
576                 
577                 # Refresh even if there was a conflict, since other changes
578                 # may have been committed while the post-commit hook was
579                 # disabled.
580                 require IkiWiki::Render;
581                 refresh();
582                 saveindex();
583
584                 if (defined $conflict) {
585                         $form->field(name => "rcsinfo", value => rcs_prepedit($file),
586                                 force => 1);
587                         $form->tmpl_param("page_conflict", 1);
588                         $form->field("editcontent", value => $conflict, force => 1);
589                         $form->field("do", "edit", force => 1);
590                         $form->tmpl_param("page_select", 0);
591                         $form->field(name => "page", type => 'hidden');
592                         $form->field(name => "type", type => 'hidden');
593                         $form->title(sprintf(gettext("editing %s"), $page));
594                         printheader($session);
595                         print misctemplate($form->title, $form->render(submit => \@buttons));
596                         return;
597                 }
598                 else {
599                         # The trailing question mark tries to avoid broken
600                         # caches and get the most recent version of the page.
601                         redirect($q, "$config{url}/".htmlpage($page)."?updated");
602                 }
603         }
604 } #}}}
605
606 sub cgi_getsession ($) { #{{{
607         my $q=shift;
608
609         eval q{use CGI::Session};
610         CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
611         
612         my $oldmask=umask(077);
613         my $session = CGI::Session->new("driver:DB_File", $q,
614                 { FileName => "$config{wikistatedir}/sessions.db" });
615         umask($oldmask);
616
617         return $session;
618 } #}}}
619
620 sub cgi_savesession ($) { #{{{
621         my $session=shift;
622
623         # Force session flush with safe umask.
624         my $oldmask=umask(077);
625         $session->flush;
626         umask($oldmask);
627 } #}}}
628
629 sub cgi (;$$) { #{{{
630         my $q=shift;
631         my $session=shift;
632
633         if (! $q) {
634                 eval q{use CGI};
635                 error($@) if $@;
636         
637                 $q=CGI->new;
638         
639                 run_hooks(cgi => sub { shift->($q) });
640         }
641
642         my $do=$q->param('do');
643         if (! defined $do || ! length $do) {
644                 my $error = $q->cgi_error;
645                 if ($error) {
646                         error("Request not processed: $error");
647                 }
648                 else {
649                         error("\"do\" parameter missing");
650                 }
651         }
652         
653         # Things that do not need a session.
654         if ($do eq 'recentchanges') {
655                 cgi_recentchanges($q);
656                 return;
657         }
658
659         # Need to lock the wiki before getting a session.
660         lockwiki();
661         
662         if (! $session) {
663                 $session=cgi_getsession($q);
664         }
665         
666         # Auth hooks can sign a user in.
667         if ($do ne 'signin' && ! defined $session->param("name")) {
668                 run_hooks(auth => sub {
669                         shift->($q, $session)
670                 });
671                 if (defined $session->param("name")) {
672                         # Make sure whatever user was authed is in the
673                         # userinfo db.
674                         if (! userinfo_get($session->param("name"), "regdate")) {
675                                 userinfo_setall($session->param("name"), {
676                                         email => "",
677                                         password => "",
678                                         regdate => time,
679                                 }) || error("failed adding user");
680                         }
681                 }
682         }
683         
684         if (defined $session->param("name") &&
685             userinfo_get($session->param("name"), "banned")) {
686                 print $q->header(-status => "403 Forbidden");
687                 $session->delete();
688                 print gettext("You are banned.");
689                 cgi_savesession($session);
690         }
691
692         run_hooks(sessioncgi => sub { shift->($q, $session) });
693
694         if ($do eq 'signin') {
695                 cgi_signin($q, $session);
696                 cgi_savesession($session);
697         }
698         elsif (defined $session->param("postsignin")) {
699                 cgi_postsignin($q, $session);
700         }
701         elsif ($do eq 'prefs') {
702                 cgi_prefs($q, $session);
703         }
704         elsif ($do eq 'create' || $do eq 'edit') {
705                 cgi_editpage($q, $session);
706         }
707         elsif ($do eq 'blog') {
708                 my $page=decode_utf8($q->param('title'));
709                 $page=~s/\///g; # no slashes in blog posts
710                 # if the page already exists, munge it to be unique
711                 my $from=$q->param('from');
712                 my $add="";
713                 while (exists $pagecase{lc($from."/".titlepage($page).$add)}) {
714                         $add=1 unless length $add;
715                         $add++;
716                 }
717                 $q->param('page', $page.$add);
718                 # now run same as create
719                 $q->param('do', 'create');
720                 cgi_editpage($q, $session);
721         }
722         elsif ($do eq 'postsignin') {
723                 error(gettext("login failed, perhaps you need to turn on cookies?"));
724         }
725         else {
726                 error("unknown do parameter");
727         }
728 } #}}}
729
730 sub userlink ($) { #{{{
731         my $user=shift;
732
733         eval q{use CGI 'escapeHTML'};
734         error($@) if $@;
735         if ($user =~ m!^https?://! &&
736             eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
737                 # Munge user-urls, as used by eg, OpenID.
738                 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
739                 my $display=$oid->display;
740                 # Convert "user.somehost.com" to "user [somehost.com]".
741                 if ($display !~ /\[/) {
742                         $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
743                 }
744                 # Convert "http://somehost.com/user" to "user [somehost.com]".
745                 if ($display !~ /\[/) {
746                         $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
747                 }
748                 $display=~s!^https?://!!; # make sure this is removed
749                 return "<a href=\"$user\">".escapeHTML($display)."</a>";
750         }
751         else {
752                 return htmllink("", "", escapeHTML(
753                         length $config{userdir} ? $config{userdir}."/".$user : $user
754                 ), noimageinline => 1);
755         }
756 } #}}}
757
758 1