summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/getsource.pm
blob: 0a21413bdb9f9cdd591c93cf1785a3e4993d556e (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::getsource;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use open qw{:utf8 :std};
  7. sub import {
  8. hook(type => "getsetup", id => "getsource", call => \&getsetup);
  9. hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate);
  10. hook(type => "cgi", id => "getsource", call => \&cgi_getsource);
  11. }
  12. sub getsetup () {
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => 1,
  17. section => "web",
  18. },
  19. getsource_mimetype => {
  20. type => "string",
  21. example => "text/plain; charset=utf-8",
  22. description => "Mime type for returned source.",
  23. safe => 1,
  24. rebuild => 0,
  25. },
  26. }
  27. sub pagetemplate (@) {
  28. my %params=@_;
  29. my $page=$params{page};
  30. my $template=$params{template};
  31. if (length $config{cgiurl}) {
  32. $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page));
  33. $template->param(have_actions => 1);
  34. }
  35. }
  36. sub cgi_getsource ($) {
  37. my $cgi=shift;
  38. return unless defined $cgi->param('do') &&
  39. $cgi->param("do") eq "getsource";
  40. IkiWiki::decode_cgi_utf8($cgi);
  41. my $page=$cgi->param('page');
  42. if (! defined $page || $page !~ /$config{wiki_file_regexp}/) {
  43. error("invalid page parameter");
  44. }
  45. # For %pagesources.
  46. IkiWiki::loadindex();
  47. if (! exists $pagesources{$page}) {
  48. IkiWiki::cgi_custom_failure(
  49. $cgi,
  50. "404 Not Found",
  51. IkiWiki::cgitemplate($cgi, gettext("missing page"),
  52. "<p>".
  53. sprintf(gettext("The page %s does not exist."),
  54. htmllink("", "", $page)).
  55. "</p>"));
  56. exit;
  57. }
  58. if (! defined pagetype($pagesources{$page})) {
  59. IkiWiki::cgi_custom_failure(
  60. $cgi->header(-status => "403 Forbidden"),
  61. IkiWiki::cgitemplate($cgi, gettext("not a page"),
  62. "<p>".
  63. sprintf(gettext("%s is an attachment, not a page."),
  64. htmllink("", "", $page)).
  65. "</p>"));
  66. exit;
  67. }
  68. if (! $config{getsource_mimetype}) {
  69. $config{getsource_mimetype} = "text/plain; charset=utf-8";
  70. }
  71. print "Content-Type: $config{getsource_mimetype}\r\n";
  72. print ("\r\n");
  73. print readfile(srcfile($pagesources{$page}));
  74. exit 0;
  75. }
  76. 1