TCMS.pm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package TCMS;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use Clone qw{clone};
  7. use Date::Format qw{strftime};
  8. use Sys::Hostname();
  9. use HTTP::Body ();
  10. use URL::Encode ();
  11. use Text::Xslate ();
  12. use DateTime::Format::HTTP();
  13. use CGI::Cookie ();
  14. use File::Basename();
  15. use IO::Compress::Gzip();
  16. use Time::HiRes qw{gettimeofday tv_interval};
  17. use HTTP::Parser::XS qw{HEADERS_AS_HASHREF};
  18. use List::Util;
  19. use URI();
  20. use Ref::Util qw{is_coderef is_hashref is_arrayref};
  21. #Grab our custom routes
  22. use FindBin;
  23. use lib "$FindBin::Bin/../lib";
  24. use FindBin::libs;
  25. use Trog::Routes::HTML;
  26. use Trog::Routes::JSON;
  27. use Trog::Log qw{:all};
  28. use Trog::Log::DBI;
  29. use Trog::Auth;
  30. use Trog::Utils;
  31. use Trog::Config;
  32. use Trog::Data;
  33. use Trog::Vars;
  34. use Trog::FileHandler;
  35. # Troglodyne philosophy - simple as possible
  36. my $cur_query = {};
  37. # Build routes
  38. sub build_routes {
  39. my $conf = Trog::Config::get();
  40. my $data = Trog::Data->new($conf);
  41. my %routes = %{ _routes($data) };
  42. # Transform 'method' / 'callback' to new scheme
  43. my %routes_adj = map {
  44. my $k = $_;
  45. my $v = $routes{$k};
  46. my %cb;
  47. my $method = $v->{method};
  48. my $callback = delete $v->{callback};
  49. #XXX For now we will discard the tPSGI object. We might want it later.
  50. my $cb_wrap = sub {
  51. my ( $tpsgi, $query ) = @_;
  52. # Let's open up our default route if needed before we bother thinking any harder
  53. return $routes{default}{callback}->($query) unless -f "config/setup";
  54. # Set the urchin parameters if necessary.
  55. %$Trog::Log::DBI::urchin = map { $_ => delete $query->{$_} } qw{utm_source utm_medium utm_campaign utm_term utm_content};
  56. # Set the IP of the request so we can fail2ban
  57. $Trog::Log::ip = $query->{req_address};
  58. # Set the referer & ua to go into DB logs, but not logs in general.
  59. # The referer/ua largely has no importance beyond being a proto bug report for log messages.
  60. $Trog::Log::DBI::referer = $query->{referer};
  61. $Trog::Log::DBI::ua = $query->{ua};
  62. # Figure out if we have a logged in user, so we can serve them user-specific files
  63. my $cookies = {};
  64. $cookies = CGI::Cookie->parse( $query->{cookies} ) if $query->{cookies};
  65. my $active_user = '';
  66. $Trog::Log::user = 'nobody';
  67. if ( exists $cookies->{tcmslogin} ) {
  68. $active_user = Trog::Auth::session2user( $cookies->{tcmslogin}->value );
  69. $Trog::Log::user = $active_user if $active_user;
  70. }
  71. # Enrich the query with tcms specific foo
  72. $query->{user_acls} = [];
  73. $query->{user_acls} = Trog::Auth::acls4user($active_user) // [] if $active_user;
  74. # Grab the list of ACLs we want to add to a post, if any.
  75. $query->{acls} = [ $query->{acls} ] if ( $query->{acls} && ref $query->{acls} ne 'ARRAY' );
  76. # Filter out passed ACLs which are naughty
  77. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  78. @{ $query->{acls} } = grep { $_ ne 'admin' } @{ $query->{acls} } unless $is_admin;
  79. $query->{user} = $active_user;
  80. $query->{body} = '';
  81. $query->{social_meta} = 1;
  82. $query->{primary_post} = {};
  83. # Some routes may just need to do serve()
  84. $query->{tpsgi} = $tpsgi;
  85. # Redirecting somewhere naughty not allow
  86. $query->{to} = URI->new( $query->{to} // '' )->path() || $query->{to} if $query->{to};
  87. # Now that we have firmed up the actual routing, let's validate.
  88. return $tpsgi->forbidden($query) if exists $query->{dispatcher}{auth} && !$active_user;
  89. no strict 'refs';
  90. $callback->($query);
  91. use strict;
  92. };
  93. #XXX todo support different callbacks per requested content-type
  94. $cb{'*'} = $cb_wrap if $callback;
  95. $v->{callbacks} = \%cb;
  96. $k => $v
  97. } keys(%routes);
  98. return [%routes_adj];
  99. }
  100. # Override the generic error handler to look spiffy
  101. sub generic_route ( $rname, $code, $title, $query ) {
  102. $query->{code} = $code;
  103. $query->{route} //= $rname;
  104. $query->{title} = $title;
  105. $query->{template} = "$rname.tx";
  106. return Trog::Routes::HTML::index($query);
  107. }
  108. our @routes = @{ build_routes() };
  109. our %aliases = aliases();
  110. #XXX Return a clone of the routing table ref, because code modifies it later
  111. sub _routes ( $data = {} ) {
  112. state %routes;
  113. return clone( \%routes ) if %routes;
  114. my $conf = Trog::Config::get();
  115. $data = Trog::Data->new($conf) unless $data;
  116. my %roots = $data->routes();
  117. %routes = %Trog::Routes::HTML::routes;
  118. @routes{ keys(%Trog::Routes::JSON::routes) } = values(%Trog::Routes::JSON::routes);
  119. @routes{ keys(%roots) } = values(%roots);
  120. # Add in global routes, here because they *must* know about all other routes
  121. # Also, nobody should ever override these.
  122. $routes{'/robots.txt'} = {
  123. method => 'GET',
  124. callback => \&robots,
  125. };
  126. return clone( \%routes );
  127. }
  128. sub aliases {
  129. my $conf = Trog::Config::get();
  130. my $data = Trog::Data->new($conf);
  131. return $data->aliases();
  132. }
  133. =head2 robots
  134. Return an appropriate robots.txt
  135. This is a "special" route as it needs to know about all the routes in order to disallow noindex=1 routes.
  136. =cut
  137. # TODO anytime that the routing table changes, we need to invalidate the cache.
  138. sub robots ($query) {
  139. state $etag = "robots-" . time();
  140. my $routes = _routes();
  141. # If there's a 'capture' route, we need to format it correctly.
  142. state @banned = map { exists $routes->{$_}{robot_name} ? $routes->{$_}{robot_name} : $_ } grep { $routes->{$_}{noindex} } sort keys(%$routes);
  143. return Trog::Renderer->render(
  144. contenttype => 'text/plain',
  145. template => 'robots.tx',
  146. data => {
  147. etag => $etag,
  148. banned => \@banned,
  149. %$query,
  150. },
  151. code => 200,
  152. );
  153. }
  154. 1;