=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()) {
}
$session->header(-content_type=>"text/json",-charset=>utf-8);
-print $encode_json($out);
+print encode_json($out);
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;
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 {
}
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";
}
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});
$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) {
sub isowner {
my $self=shift;
- return $self->{-identity} eq $config->{-owner};
+ return $self->{-identity} eq $config{-owner};
}
=head2 banned
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();
sub _readban {
my $identity = shift;
+ my %bans;
dbmopen %bans,$config{-topdir}."/bans",0644;
if (exists $bans{-identity}) {
return (-ban=>1);
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;
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 ($@);
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;
}