]> wagner.pp.ru Git - oss/vjournal.git/commitdiff
userinfo works in unlogged mode
authorVictor Wagner <vitus@wagner.pp.ru>
Fri, 11 Oct 2013 18:28:39 +0000 (22:28 +0400)
committerVictor Wagner <vitus@wagner.pp.ru>
Fri, 11 Oct 2013 18:28:39 +0000 (22:28 +0400)
bin/userinfo
lib/VJournal/ProviderList.pm
lib/VJournal/Session.pm

index 92a45e5fd45b1f0d76e267e622a0d9b80ecc45a7..653d099f6ab0253679f60b9c90df2233b22d92d5 100644 (file)
@@ -34,17 +34,18 @@ If user is not logged in, returns following structure:
 =cut
 
 use VJournal::Session;
+use VJournal::ProviderList;
 use JSON;
 use CGI;
 
 my $cgi=new CGI;
-my $session = VJourna::Session->new($cgi);
+my $session = VJournal::Session->new($cgi);
 my $out={};
 if (!defined $session) {
 # User is not authenticated. Return list of providers;
        $out->{state}="notlogged";
        $sites=VJournal::ProviderList->new;
-       $out->{providers}=$sites->menu;
+       $out->{providers}=[ $sites->menu ] ;
        $session=$cgi;
 } else {
        if ($session->isowner())  {
@@ -65,5 +66,5 @@ if (!defined $session) {
                
 }
 $session->header(-content_type=>"text/json",-charset=>utf-8);
-print $encode_json($out);
+print encode_json($out);
 
index 6ce8e81e39aec16add79013ea6333967ec84faaf..c4374e713309de9bee7215faa2c803666a112f24 100644 (file)
@@ -1,21 +1,26 @@
 package VJournal::ProviderList;
 use VJournal::Session;
 use Carp;
-use constant PROVIDER_LIST_FILE="providers.lst";
+use constant PROVIDER_LIST_FILE=>"providers.lst";
 
 sub new {
        my $class=shift;
-       if (!exists $VJournal::Session::config{-statedir})
-       croak("VJournal::Session config is not loaded");
+       croak("VJournal::Session config is not loaded") 
+               if (!exists $VJournal::Session::config{-statedir});
        my $f;
-       open
-       $f,"<",$VJournal::Session::config{-statedir}."/".PROVIDER_LIST_FILE or croak("cannot find provider list file");
+       open $f,"<",$VJournal::Session::config{-statedir}."/".PROVIDER_LIST_FILE or croak("cannot find provider list file");
        my $self={};
+       SITE:
        while (<$f>) {
+               next SITE if /^\s*#/;
                chomp;
-               my ($id,$name,$format,$translate_underscore,$format2,$urlasname}=split (":")
-               $self->{$id}={-id=>$id,-name=>$name,$format=>[$format,($format2?$format2:())],
-                       -translate_underscore=>$translate_underscore,-url_as_name=>$urlasname};
+               my ($id,$name,$format,$translate_underscore,$format2,$urlasname)=split(":");
+               next SITE unless $id=~/\w+/;
+               $self->{$id}={-id=>$id,
+                       -name=>$name,
+                       -format=>[$format,($format2?$format2:())],
+                       -translate_underscore=>$translate_underscore,
+                       -url_as_name=>$urlasname};
        }
        close $f;
        return bless $self,$class;
@@ -24,33 +29,33 @@ sub new {
 sub menu {
        my $self=shift;
        my @out;
-       for my $site (values $self) {
+       for my $site (values %$self) {
                push @out,{id=>$site->{-id},name=>$site->{-name}};
        }
-       push @out,{id=>"_",name=>"Other site");
+       push @out,{id=>"_",name=>"Other site"};
        return @out;
 }
 
 sub useruri {
-       my ($self,$user,$site_id) = @;
-       if (!exists $self->{$site_id}) {
+       my ($self,$user,$site_id) = @_;
+       if (!exists($self->{$site_id})) {
                # If no valid site id provided, assume that we have just openid
                # url instead of user name
                return $user;
        }
        my $site=$self->{$site_id};
-       if (!index($site->{-format}[0],"%s")>0) {
+       if (!index($site->{-format}[0],"%s")>=0) {
                return $site->{-format}[0];
        }
        if ($site->{-translate_underscore}) {
                if (defined $site->{-format}[1] && ($user=~/^_/ || $user=~/_$/))
                {
-                       return sprintf $site->{-format}[1],$user);
+                       return sprintf($site->{-format}[1],$user);
                } else {
                        $user =~ tr/_/-/;
                }
        }       
-       return sprintf $site->{-format}[0],$user);
+       return sprintf($site->{-format}[0],$user);
 }
 
 sub meaningless_url {
index e8197864dd566298ecebf04aaae4170bf579ff42..f520cedb1cb4fc346f4ce4b3b3dae9b5eabe14c2 100644 (file)
@@ -58,9 +58,9 @@ sub new {
        }       
        if (exists $attrs{-cgi} && $attrs{-cgi}->can("cookie")) {
                my $cgi=$attrs{-cgi};
+               load_config($cgi);
                my $sess_id = $cgi->cookie(COOKIE_NAME);
                return undef unless $sess_id;
-               load_config();
                if (!exists($config{-sessionbase})) {
                        croak "No VJournal config read";
                }
@@ -141,7 +141,7 @@ sub create {
                if (exists $params{-avatar}) {
                        @avatar = VJournal::Avatar::cache($params{-avatar});
                } else {        
-                       @avatar = VJournal::Avatar::by_openid($user);
+                       @avatar = VJournal::Avatar::by_openid($identity);
                }
                if (!@avatar && exists $params{-email}) {
                        @avatar = VJournal::Avatar::by_email($params{-email});
@@ -150,7 +150,7 @@ sub create {
                $users{$identity}=join(":",$params{-name}||$identity
                        ,$params{-email},$a{-src},$a{-width},$a{-height});
        }
-       my ($name,$email,$avatarsrc,$avatarwidth,$avatarheight)=split(":",$users{$user});
+       my ($name,$email,$avatarsrc,$avatarwidth,$avatarheight)=split(":",$users{$identity});
        $session->{-name} = $name;
        $session->{-email} = $email if $email;
        if ($avatarsrc) {
@@ -279,7 +279,7 @@ returns true, if current user is owner of the blog
 
 sub isowner {
        my $self=shift;
-       return $self->{-identity} eq $config->{-owner};
+       return $self->{-identity} eq $config{-owner};
 }
 
 =head2 banned
@@ -304,7 +304,7 @@ Marks user as banned in the current blog
 
 sub ban {
        my ($self,$foe) = @_;
-       if (!$self->isowner()) return undef;
+       return undef if (!$self->isowner()) ;
        my %bans;
        dbmopen %bans,$config{-topdir}."/bans",0644;
        $bans{$foe}=time();
@@ -321,6 +321,7 @@ in the blog top url
 
 sub _readban {
        my $identity = shift;
+       my %bans;
        dbmopen %bans,$config{-topdir}."/bans",0644;
        if (exists $bans{-identity}) {
                return (-ban=>1);
@@ -362,7 +363,8 @@ dies if config not found.
 
 sub load_config {
        my $cgi = $_[0];
-       my $path=$cgi->path_translated();
+       my $path; 
+       $path=$1 if $cgi->path_translated()=~/^(.*)$/;
        my @dirs = File::Spec->splitdir($path);
        my @uri = File::Spec->splitdir($cgi->path_info);
        my $found =0;
@@ -370,10 +372,10 @@ sub load_config {
                my $d=File::Spec->catdir(@dirs,CONFIG_NAME);
                if (-r $d) {
                        open F,"<",$d;
-                       local $/=undef;
-                       my $config = <F>;
+                       while (<F>) {
+                               $config{"-$1"} = $2 if /^\s*(\w+)\s*=\s*"([^"]*)"\s*$/;
+                       }
                        close F;
-                       eval "%config = {$d}";
                        $config{-topdir}=File::Spec->catdir(@dirs);
                        $config{-topurl}=$cgi->url(-base=>1).File::Spec->catdir(@uri);
                        die $@ if ($@);
@@ -387,16 +389,17 @@ sub load_config {
        foreach my $key (@reqkeys) {
                die "Required key $key missing from config" 
                        unless exists $config{$key};
+               
        }
        # sensible defaults
        $config{-sessionbase}||=$config{-statedir}."/sessions.db";
        $config{-userbase}||=$config{-statedir}."/user.db";
        $config{-sessiontime}||=86400*30;
        $config{-gracetime}||=86400;
-       $config{-templatedir}||=$config{-topdir}."/templates"
+       $config{-templatedir}||=$config{-topdir}."/templates";
        $config{-avatardir}||=$config{-topdir}."/avatars";
        if (!$config{-owner}) {
-               my $uri=substr($config{-topurl},index($config{-topurl},"://")+2;
+               my $uri=substr($config{-topurl},index($config{-topurl},"://")+3);
                $uri =~ s/:\d+(\/.*)?$//;
                $config{-owner} = $uri;
        }