package IIfe7ed66bde3aa68899b7883eab4be165::mod_TDM2; use strict; use base qw/JinPluginBase/; use DBI; use Digest::MD5 qw(md5_hex); use Zen; use Zen::PerlHandler; sub init { my $this = shift()->SUPER::init(@_); return $this->msg('error.missing_config') if grep { ! $this->{$_} } qw/dbhost dbname dbuser/; $this->{dbh} = DBI->connect("DBI:mysql:$this->{dbname}:$this->{dbhost}", $this->{dbuser}, $this->{dbpass}) or return $this->msg('error.connect_db', $DBI::errstr); my $sth = $this->{dbh}->prepare('SHOW TABLES LIKE ?'); $sth->execute('tdm2_user'); return $this->msg('error.table_not_found') unless $sth->rows > 0; return; } sub addUser { my ($this, $user) = @_; my $sth; my $grp = $this->{assign_user_group} + 0; my $points_per_subscr; if ($grp) { $sth = $this->sql('SELECT points_per_subscr FROM tdm2_group WHERE id=?', $grp) or return; $points_per_subscr = $sth->fetchrow; } $sth = $this->sql('SELECT * FROM tdm2_user WHERE email=?', $user->{email}) or return; if ($sth->rows > 0) { my $r = $sth->fetchrow_hashref; $this->updateUser($user->{id}, $user); $this->sql('UPDATE tdm2_user SET points=?,grp=? WHERE id=? and grp!=?', $points_per_subscr + 0, $grp, $r->{id}, $grp) or return; return $this; } $sth = $this->sql('SELECT max(id) FROM tdm2_user') or return; my $max_id = $sth->fetchrow; $max_id++; my $regOptions = { id => $max_id, confirmed => 1, enabled => 1, email => $user->{email}, passwd => md5_hex($user->{passwd}), name => $user->{name}, points => ($points_per_subscr + 0), grp => $grp, ext_id => $user->{id}, }; my @f = keys %$regOptions; $this->sql('INSERT INTO tdm2_user SET ' . join(',', map { "$_=?" } @f), map { $regOptions->{$_} } @f) or return; $this->sql("UPDATE tdm2_user SET regtime = NOW() WHERE id=$max_id LIMIT 1") or return; $sth = $this->sql("DELETE FROM zen_id_dbtable WHERE sqltable='tdm2_user'") or return; $this; } sub updateUser { my ($this, $id, $user) = @_; my $remoteUserId = $this->getRemoteUserId($id, $user->{email}); if ($remoteUserId) { $this->sql('UPDATE tdm2_user SET email=?,passwd=?,name=? WHERE id=?', $user->{email}, md5_hex($user->{passwd}), $user->{name}, $remoteUserId) or return; return $this; } $this->addUser($user); } sub deleteUser { my ($this, $id) = @_; my $remoteUserId = $this->getRemoteUserId($id) or return $this->failure('error.user_not_found', $id); $this->sql('DELETE FROM tdm2_user WHERE id=?', $remoteUserId) or return; $this; } sub suspendUser { my $this = shift; my $user = shift; $this->deleteUser($user); return; } sub getUsernameForUser { my ($this, $user) = @_; $user->{email}; } sub getRemoteUserId { my ($this, $id, $email)=@_; my $sth = $this->sql('SELECT * FROM tdm2_user WHERE ext_id=?', $id) or return; if ($sth->rows > 0) { my $r = $sth->fetchrow_hashref; return $r->{id}; } elsif ($email) { my $sth = $this->sql("SELECT * FROM tdm2_user WHERE email=?", $email) or return; return $this->failure('error.user_not_found', $email) unless $sth->rows > 0; my $r = $sth->fetchrow_hashref; $this->sql("UPDATE tdm2_user SET ext_id=? WHERE id=?", $id, $r->{id}) or return; return $r->{id}; } } sub act_transfer_points { my $class = shift; my $printMsg = sub { my $msg = shift; Zen->getRequest->printHeader("text/plain"); print $msg; Zen->getRequest->finish; return; }; my $in = Zen->getRequest->getInput; my $userClass = Zen::PerlHandler->getCallerPackage->getUserClass; my $admin = $userClass->search(email => $in->{admin_email}, NUM => 1)->[0]; unless ($admin) { &$printMsg('Admin e-mail not found'); return; } unless ($in->{admin_passwd} eq md5_hex($admin->{passwd})) { &$printMsg('Admin password mismatch'); return; } unless ($in->{email}) { &$printMsg('OK'); return; } my @users = $userClass->search(email => $in->{email}); my $user; for my $u (@users) { $user = $u if $admin->{email} eq $u->getSys->getAdmin->{email} && $admin->{passwd} eq $u->getSys->getAdmin->{passwd}; } unless ($user) { &$printMsg('User e-mail not found'); return; } my $points = $in->{points} + 0; unless ($points) { &$printMsg($user->{points}); return; } if ($user->{points} + $points < 0) { &$printMsg('Not enough points'); return; } $user->{points} += $points; $user->{points_change_comment} = 'Transfer from TDM2'; $user->save; &$printMsg('Action complete'); return; } 1; 