package IIfe7ed66bde3aa68899b7883eab4be165::mod_Drupal; use strict; use base 'JinPluginBaseExtScript'; use DBI; use Digest::MD5 qw(md5_hex); 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->sql('show tables like ?', "$this->{tblprefix}users"); return $this->msg('error.tables_not_found', $this->{tblprefix}) unless $sth->rows > 0; return; } sub addUser { my $this = shift; my $user = shift; my $script_uid = $this->_get_script_uid($user); if ($script_uid) { $this->updateUser($user); } else { my $sth = $this->sql("SELECT max(uid) FROM $this->{tblprefix}users") or return; my $max_id = $sth->fetchrow; $max_id++; my $memberName = $this->getUsernameForUser($user); my $time = time; my $regOptions = { uid => $max_id, name => $memberName, pass => md5_hex($user->{passwd}), mail => $user->{email}, created => $time, status => 1, }; my @f = keys %$regOptions; $this->sql("INSERT INTO $this->{tblprefix}users SET " . join(',', map { "$_=?" } @f), map { $regOptions->{$_} } @f) or return; $script_uid = $this->_get_script_uid($user); $this->fixScriptUID($user, $script_uid); } if ($this->{assign_user_role}) { my $sth = $this->sql("SELECT rid FROM $this->{tblprefix}role WHERE name=?", $this->{assign_user_role}) or return; if (my $rid = $sth->fetchrow) { $this->sql("REPLACE INTO $this->{tblprefix}users_roles SET uid=?, rid=?", $script_uid, $rid) or return; } } return $script_uid; } sub updateUser { my $this = shift; my $user = shift; if (my $script_uid = $this->_get_script_uid($user)) { $this->sql("UPDATE $this->{tblprefix}users SET pass=?,mail=?,status=1 WHERE uid=?", md5_hex($user->{passwd}), $user->{email}, $script_uid) or return; $this->fixScriptUID($user, $script_uid); } else { $this->addUser($user); } return; } sub deleteUser { my $this = shift; my $user = shift; my $script_uid = $this->_get_script_uid($user) or return; my @queries = ( "UPDATE $this->{tblprefix}authmap SET uid = 0 WHERE uid=?", "UPDATE $this->{tblprefix}comments SET uid = 0 WHERE uid=?", "UPDATE $this->{tblprefix}files SET uid = 0 WHERE uid=?", "UPDATE $this->{tblprefix}history SET uid = 0 WHERE uid=?", "UPDATE $this->{tblprefix}node SET uid = 0 WHERE uid=?", "UPDATE $this->{tblprefix}node_comment_statistics SET last_comment_uid = 0 WHERE uid=?", "UPDATE $this->{tblprefix}node_revisions SET uid = 0 WHERE uid=?", "UPDATE $this->{tblprefix}node SET uid = 0 WHERE uid=?", "UPDATE $this->{tblprefix}sessions SET uid = 0 WHERE uid=?", "DELETE FROM $this->{tblprefix}users WHERE uid=?", "DELETE FROM $this->{tblprefix}users_roles WHERE uid=?", "UPDATE $this->{tblprefix}watchdog SET uid = 0 WHERE uid=?", ); for (@queries) { $this->sql($_, $script_uid) or return; } $this->fixScriptUID($user, undef); return; } sub suspendUser { my $this = shift; my $user = shift; my $script_uid = $this->_get_script_uid($user) or return; $this->sql("UPDATE $this->{tblprefix}users SET status=0 WHERE uid=?", $script_uid) or return; return; } sub getUsernameForUser { my $this = shift; my $user = shift; $user->{email}; } sub _get_script_uid { my $this = shift; my $user = shift; my $script_uid = $this->getScriptUID($user); if ($script_uid) { my $sth = $this->sql("SELECT uid FROM $this->{tblprefix}users WHERE uid=?", $script_uid) or return; $this->fixScriptUID($user, undef) unless $sth->rows > 0; } my $sth = $this->sql("SELECT uid FROM $this->{tblprefix}users WHERE mail=?", $user->{email}) or return; $script_uid = $sth->fetchrow_array if $sth->rows > 0; $script_uid; } 1; 