how to banned several users by email list from txt file ?

TBDragon

Active member
#1
hi i have many users that have invalid email address
and i want to banned them

but its hard for me to search for email one by one

so i was thinking of making a query to get the user_id by searching for the email

like
Code:
select user_id from xf_user where email like '%email@email.com%'
then
Code:
UPDATE `xf_user` SET  `user_group_id` =  '6',
`is_banned` =  '1' WHERE  `xf_user`.`user_id` =x; // where x the id i got from last query
but how i can make a php code to read the whole emails in the txt file i have then read them one by one
sorry =( i didnt work with files in php =(

and is my query is ok or there is another way to do that ?
 

Aayush

Well-known member
#2
Go to PHP docs and read about file_get_contents()

This should help you read the file into a var and then you can process it(explode the user list separated by a ',' or a space whatever)

I wouldn't suggest you to directly ban the user in SQL though, it would be better if you could call a User ban function from the Xenforo API cause I am not sure if there is something other than just updating the xf_users table that also needs to be done when banning people.
 

TBDragon

Active member
#3
Go to PHP docs and read about file_get_contents()

This should help you read the file into a var and then you can process it(explode the user list separated by a ',' or a space whatever)
thanks ^_^ i will

I wouldn't suggest you to directly ban the user in SQL though, it would be better if you could call a User ban function from the Xenforo API cause I am not sure if there is something other than just updating the xf_users table that also needs to be done when banning people.
but how to do that exactly ?! i didnt use Xenforo API before >_<
 

Aayush

Well-known member
#4
thanks ^_^ i will


but how to do that exactly ?! i didnt use Xenforo API before >_<
You know how to make an addon right?

Use the Xenforo/models/banning.php class and use the method
Code:
banEmail()
It will also rebuild the cache.

Basically banning somebody requires to first ban their email and then rebuild the xenforo cache so that it works fast.

Code:
/**
     * Bans the specified email. Wildcards are allowed. If no wildcards are given, they are automatically
     * added in logical places (no or leading @ gives wildcard prefix; no or trailing . gives wildcard suffix).
     *
     * Throws exceptions on malformed input.
     *
     * @param string $email
     *
     * @return boolean Returns true if the ban is inserted
     */
    public function banEmail($email)
    {
        if ($email == '*' || $email === '')
        {
            throw new XenForo_Exception(new XenForo_Phrase('you_must_enter_at_least_one_non_wildcard_character'), true);
        }

        if (strpos($email, '*') === false)
        {
            if (strpos($email, '@') === false)
            {
                $email = '*' . $email;
            }
            if (strpos($email, '.') === false)
            {
                $email .= '*';
            }
        }

        if ($email[0] == '@')
        {
            $email = '*' . $email;
        }

        $lastChar = substr($email, -1);
        if ($lastChar == '.' || $lastChar == '@')
        {
            $email .= '*';
        }

        $atPos = strpos($email, '@');
        if ($atPos !== false && strpos($email, '.', $atPos) === false && strpos($email, '*', $atPos) === false)
        {
            $email .= '*';
        }

        if ($email == '*@*' || $email == '*.*')
        {
            throw new XenForo_Exception(new XenForo_Phrase('this_would_ban_all_email_addresses'), true);
        }

        $email = preg_replace('/\*{2,}/', '*', $email);

        $db = $this->_getDb();
        XenForo_Db::beginTransaction($db);

        $result = $this->_getDb()->query('
            INSERT IGNORE INTO xf_ban_email
                (banned_email)
            VALUES
                (?)
        ', array($email));

        $inserted = ($result->rowCount() ? true : false);
        if ($inserted)
        {
            $this->rebuildBannedEmailCache();
        }

        XenForo_Db::commit($db);

        return $inserted;
    }
Code:
    /**
     * Rebuilds the cache of banned emails.
     *
     * @return array Banned email cache
     */
    public function rebuildBannedEmailCache()
    {
        $cache = array();
        foreach ($this->getBannedEmails() AS $bannedEmail)
        {
            $cache[] = $bannedEmail['banned_email'];
        }

        $this->_getDataRegistryModel()->set('bannedEmails', $cache);

        return $cache;
    }
 

TBDragon

Active member
#5
You know how to make an addon right?

Use the Xenforo/models/banning.php class and use the method
Code:
banEmail()
It will also rebuild the cache.

Basically banning somebody requires to first ban their email and then rebuild the xenforo cache so that it works fast.

Code:
/**
     * Bans the specified email. Wildcards are allowed. If no wildcards are given, they are automatically
     * added in logical places (no or leading @ gives wildcard prefix; no or trailing . gives wildcard suffix).
     *
     * Throws exceptions on malformed input.
     *
     * @param string $email
     *
     * @return boolean Returns true if the ban is inserted
     */
    public function banEmail($email)
    {
        if ($email == '*' || $email === '')
        {
            throw new XenForo_Exception(new XenForo_Phrase('you_must_enter_at_least_one_non_wildcard_character'), true);
        }

        if (strpos($email, '*') === false)
        {
            if (strpos($email, '@') === false)
            {
                $email = '*' . $email;
            }
            if (strpos($email, '.') === false)
            {
                $email .= '*';
            }
        }

        if ($email[0] == '@')
        {
            $email = '*' . $email;
        }

        $lastChar = substr($email, -1);
        if ($lastChar == '.' || $lastChar == '@')
        {
            $email .= '*';
        }

        $atPos = strpos($email, '@');
        if ($atPos !== false && strpos($email, '.', $atPos) === false && strpos($email, '*', $atPos) === false)
        {
            $email .= '*';
        }

        if ($email == '*@*' || $email == '*.*')
        {
            throw new XenForo_Exception(new XenForo_Phrase('this_would_ban_all_email_addresses'), true);
        }

        $email = preg_replace('/\*{2,}/', '*', $email);

        $db = $this->_getDb();
        XenForo_Db::beginTransaction($db);

        $result = $this->_getDb()->query('
            INSERT IGNORE INTO xf_ban_email
                (banned_email)
            VALUES
                (?)
        ', array($email));

        $inserted = ($result->rowCount() ? true : false);
        if ($inserted)
        {
            $this->rebuildBannedEmailCache();
        }

        XenForo_Db::commit($db);

        return $inserted;
    }
Code:
    /**
     * Rebuilds the cache of banned emails.
     *
     * @return array Banned email cache
     */
    public function rebuildBannedEmailCache()
    {
        $cache = array();
        foreach ($this->getBannedEmails() AS $bannedEmail)
        {
            $cache[] = $bannedEmail['banned_email'];
        }

        $this->_getDataRegistryModel()->set('bannedEmails', $cache);

        return $cache;
    }
well this something interesting
i didnt know that Xenforo API mean the classes and files of XF it self

anyway i found an addon

http://xenforo.com/community/resources/freddyshouse-bounced-email.2673/

if its help then ok if not i will try to implement ur code


and iam happy cuz of ur reply can`t find many programmers help that much in this way *-* thanks

God Bless u
 

Aayush

Well-known member
#6
Top