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