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