瀏覽代碼

more adjustment to tpsgi

George Baugh 5 月之前
父節點
當前提交
17a06c5820
共有 4 個文件被更改,包括 77 次插入 和 16 次删除
  1. 0 2
      bin/tcms-useradd
  2. 71 10
      lib/TCMS.pm
  3. 1 1
      lib/Trog/Log.pm
  4. 5 3
      lib/Trog/Routes/HTML.pm

+ 0 - 2
bin/tcms-useradd

@@ -62,8 +62,6 @@ Display this output.
 =cut
 
 sub main {
-    Trog::Log::log_init();
-
     my %options;
     Getopt::Long::GetOptionsFromArray(
         \@_,

+ 71 - 10
lib/TCMS.pm

@@ -49,10 +49,9 @@ my $cur_query = {};
 
 # Build routes
 sub build_routes {
-    Trog::Log::log_init();
-    my $conf //= Trog::Config::get();
-    my $data //= Trog::Data->new($conf);
-    my %routes = %{ _routes($data) };
+    my $conf    = Trog::Config::get();
+    my $data    = Trog::Data->new($conf);
+    my %routes  = %{ _routes($data) };
 
     # Transform 'method' / 'callback' to new scheme
     my %routes_adj = map {
@@ -61,8 +60,64 @@ sub build_routes {
         my %cb;
         my $method = $v->{method};
         my $callback = delete $v->{callback};
+
         #XXX For now we will discard the tPSGI object.  We might want it later.
-        my $cb_wrap = sub { shift; $callback->(shift); };
+        my $cb_wrap = sub {
+            my ($tpsgi, $query) = @_;
+
+            # Set the urchin parameters if necessary.
+            %$Trog::Log::DBI::urchin = map { $_ => delete $query->{$_} } qw{utm_source utm_medium utm_campaign utm_term utm_content};
+
+            # Set the IP of the request so we can fail2ban
+            $Trog::Log::ip = $query->{req_address};
+
+            # Set the referer & ua to go into DB logs, but not logs in general.
+            # The referer/ua largely has no importance beyond being a proto bug report for log messages.
+            $Trog::Log::DBI::referer = $query->{referer};
+            $Trog::Log::DBI::ua      = $query->{ua};
+
+            # Figure out if we have a logged in user, so we can serve them user-specific files
+            my $cookies = {};
+            $cookies = CGI::Cookie->parse( $query->{cookies} ) if $query->{cookies};
+
+            my $active_user = '';
+            $Trog::Log::user = 'nobody';
+            if ( exists $cookies->{tcmslogin} ) {
+                $active_user     = Trog::Auth::session2user( $cookies->{tcmslogin}->value );
+                $Trog::Log::user = $active_user if $active_user;
+            }
+
+            # Enrich the query with tcms specific foo
+            $query->{user_acls} = [];
+            $query->{user_acls} = Trog::Auth::acls4user($active_user) // [] if $active_user;
+
+            # Grab the list of ACLs we want to add to a post, if any.
+            $query->{acls} = [ $query->{acls} ] if ( $query->{acls} && ref $query->{acls} ne 'ARRAY' );
+
+            # Filter out passed ACLs which are naughty
+            my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
+            @{ $query->{acls} } = grep { $_ ne 'admin' } @{ $query->{acls} } unless $is_admin;
+
+            $query->{user}    = $active_user;
+            $query->{body}         = '';
+            $query->{social_meta}  = 1;
+            $query->{primary_post} = {};
+
+            # Redirecting somewhere naughty not allow
+            $query->{to} = URI->new( $query->{to} // '' )->path() || $query->{to} if $query->{to};
+
+            # Now that we have firmed up the actual routing, let's validate.
+            return $tpsgi->forbidden($query) if exists $query->{dispatcher}{auth} && !$active_user;
+
+            # Disallow any paths that are naughty ( starman auto-removes .. up-traversal)
+            if ( index( $query->{route}, '/templates' ) == 0 || index( $query->{route}, '/statics' ) == 0 || $query->{route} =~ m/.*(\.psgi|\.pm)$/i ) {
+                return $tpsgi->forbidden($query);
+            }
+
+            no strict 'refs';
+            $callback->($query);
+            use strict;
+        };
 
         #XXX todo support different callbacks per requested content-type
         $cb{'*'} = $cb_wrap if $callback;
@@ -74,6 +129,7 @@ sub build_routes {
 }
 
 our @routes = @{build_routes()};
+our %aliases = aliases();
 
 =head2 app()
 
@@ -197,7 +253,7 @@ sub _app {
     # It's mod_rewrite!
     $path = '/index' if $path eq '/';
 
-    #XXX this is hardcoded in browsers, so just rewrite the path
+    #XXX this is hardcoded in browsers, so just rewrite the path - neeeds to be an alias
     $path = '/img/icon/favicon.ico' if $path eq '/favicon.ico';
 
     # Translate alias paths into their actual path
@@ -376,10 +432,9 @@ sub _routes ( $data = {} ) {
     state %routes;
     return clone( \%routes ) if %routes;
 
-    if ( !$data ) {
-        my $conf = Trog::Config::get();
-        $data = Trog::Data->new($conf);
-    }
+    my $conf = Trog::Config::get();
+    $data = Trog::Data->new($conf) unless $data;
+
     my %roots = $data->routes();
     %routes                                      = %Trog::Routes::HTML::routes;
     @routes{ keys(%Trog::Routes::JSON::routes) } = values(%Trog::Routes::JSON::routes);
@@ -395,6 +450,12 @@ sub _routes ( $data = {} ) {
     return clone( \%routes );
 }
 
+sub aliases {
+    my $conf = Trog::Config::get();
+    my $data = Trog::Data->new($conf);
+    return $data->aliases();
+}
+
 =head2 robots
 
 Return an appropriate robots.txt

+ 1 - 1
lib/Trog/Log.pm

@@ -110,7 +110,7 @@ sub FATAL {
 }
 
 sub _check_init {
-    die "You must run log_init() before using other Trog::Log methods" unless $log;
+    log_init() unless $log;
 }
 
 1;

+ 5 - 3
lib/Trog/Routes/HTML.pm

@@ -219,8 +219,10 @@ our %routes = (
 
 # Grab theme routes
 my $themed = 0;
-if ($Trog::Themes::theme_dir) {
-    my $theme_mod = "$Trog::Themes::theme_dir/routes.pm";
+my $theme_dir = Trog::Themes::get_dir();
+if ($theme_dir) {
+
+    my $theme_mod = "$theme_dir/routes.pm";
     if ( -f $theme_mod ) {
         use lib '.';
         require $theme_mod;
@@ -375,7 +377,7 @@ sub _build_social_meta ( $query, $title ) {
     $meta_tags =~ s/content="video"/content="video:other"/mg if $meta_tags;
     $meta_tags .= $extra_tags                                if $extra_tags;
 
-    print STDERR "WARNING: Theme misconfigured, social media tags will not be included\n$@\n" if $Trog::Themes::theme_dir && !$meta_tags;
+    print STDERR "WARNING: Theme misconfigured, social media tags will not be included\n$@\n" if $theme_dir && !$meta_tags;
     return ( $default_tags, $meta_desc, $meta_tags );
 }