TCMS.pm 7.2 KB

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