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