From 200e065d42299c1d605d503fb9c30fe3728bf0da Mon Sep 17 00:00:00 2001 From: Victor Wagner Date: Fri, 11 Oct 2013 22:28:39 +0400 Subject: [PATCH] userinfo works in unlogged mode --- bin/userinfo | 7 ++++--- lib/VJournal/ProviderList.pm | 35 ++++++++++++++++++++--------------- lib/VJournal/Session.pm | 25 ++++++++++++++----------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/bin/userinfo b/bin/userinfo index 92a45e5..653d099 100644 --- a/bin/userinfo +++ b/bin/userinfo @@ -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); diff --git a/lib/VJournal/ProviderList.pm b/lib/VJournal/ProviderList.pm index 6ce8e81..c4374e7 100644 --- a/lib/VJournal/ProviderList.pm +++ b/lib/VJournal/ProviderList.pm @@ -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 { diff --git a/lib/VJournal/Session.pm b/lib/VJournal/Session.pm index e819786..f520ced 100644 --- a/lib/VJournal/Session.pm +++ b/lib/VJournal/Session.pm @@ -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 = ; + while () { + $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; } -- 2.39.5