Example #1
0
/**
 * MyBB 1.6
 * Copyright 2010 MyBB Group, All Rights Reserved
 *
 * Website: http://mybb.com
 * License: http://mybb.com/about/license
 *
 * $Id$
 */
function task_promotions($task)
{
    global $mybb, $db, $lang, $cache;
    $usergroups = $cache->read("usergroups");
    // Iterate through all our promotions
    $query = $db->simple_select("promotions", "*", "enabled = '1'");
    while ($promotion = $db->fetch_array($query)) {
        // Does the destination usergroup even exist?? If it doesn't and it moves a user to it, the user will get PHP errors.
        if (!array_key_exists($promotion['newusergroup'], $usergroups)) {
            // Instead of just skipping this promotion, disable it to stop it even being selected when this task is run.
            $update = array("enabled" => 0);
            $db->update_query("promotions", $update, "pid = '" . intval($promotion['pid']) . "'");
            continue;
        }
        $and = "";
        $sql_where = "";
        // Based on the promotion generate criteria for user selection
        $requirements = explode(',', $promotion['requirements']);
        if (in_array('postcount', $requirements) && intval($promotion['posts']) >= 0 && !empty($promotion['posttype'])) {
            $sql_where .= "{$and}postnum {$promotion['posttype']} '{$promotion['posts']}'";
            $and = " AND ";
        }
        if (in_array('reputation', $requirements) && !empty($promotion['reputationtype'])) {
            $sql_where .= "{$and}reputation {$promotion['reputationtype']} '{$promotion['reputations']}'";
            $and = " AND ";
        }
        if (in_array('referrals', $requirements) && intval($promotion['referrals']) >= 0 && !empty($promotion['referralstype'])) {
            $sql_where .= "{$and}referrals {$promotion['referralstype']} '{$promotion['referrals']}'";
            $and = " AND ";
        }
        if (in_array('timeregistered', $requirements) && intval($promotion['registered']) > 0 && !empty($promotion['registeredtype'])) {
            switch ($promotion['registeredtype']) {
                case "hours":
                    $regdate = $promotion['registered'] * 60 * 60;
                    break;
                case "days":
                    $regdate = $promotion['registered'] * 60 * 60 * 24;
                    break;
                case "weeks":
                    $regdate = $promotion['registered'] * 60 * 60 * 24 * 7;
                    break;
                case "months":
                    $regdate = $promotion['registered'] * 60 * 60 * 24 * 30;
                    break;
                case "years":
                    $regdate = $promotion['registered'] * 60 * 60 * 24 * 365;
                    break;
                default:
                    $regdate = $promotion['registered'] * 60 * 60 * 24;
            }
            $sql_where .= "{$and}regdate <= '" . (TIME_NOW - $regdate) . "'";
            $and = " AND ";
        }
        if (!empty($promotion['originalusergroup']) && $promotion['originalusergroup'] != '*') {
            $sql_where .= "{$and}usergroup IN ({$promotion['originalusergroup']})";
            $and = " AND ";
        }
        if (!empty($promotion['newusergroup'])) {
            $sql_where .= "{$and}usergroup != '{$promotion['newusergroup']}'";
            $and = " AND ";
        }
        $sql_where .= "{$and}lastactive >= '{$task['lastrun']}'";
        // Exclude super admins
        $sql_where .= not_super_admins(true);
        $uid = array();
        $log_inserts = array();
        if ($promotion['usergrouptype'] == "secondary") {
            $usergroup_select = "additionalgroups";
        } else {
            $usergroup_select = "usergroup";
        }
        $query2 = $db->simple_select("users", "uid,{$usergroup_select}", $sql_where);
        while ($user = $db->fetch_array($query2)) {
            // super admin check?
            if ($usergroup_select == "additionalgroups") {
                $log_inserts[] = array('pid' => $promotion['pid'], 'uid' => $user['uid'], 'oldusergroup' => $user['additionalgroups'], 'newusergroup' => $promotion['newusergroup'], 'dateline' => TIME_NOW, 'type' => "secondary");
            } else {
                $log_inserts[] = array('pid' => $promotion['pid'], 'uid' => $user['uid'], 'oldusergroup' => $user['usergroup'], 'newusergroup' => $promotion['newusergroup'], 'dateline' => TIME_NOW, 'type' => "primary");
            }
            $uids[] = $user['uid'];
            if ($usergroup_select == "additionalgroups") {
                if (join_usergroup($user['uid'], $promotion['newusergroup']) === false) {
                    // Did the user already have the additional usergroup?
                    array_pop($log_inserts);
                    array_pop($uids);
                }
            }
            if (count($uids) % 20 == 0) {
                if ($usergroup_select == "usergroup") {
                    $db->update_query("users", array('usergroup' => $promotion['newusergroup']), "uid IN(" . implode(",", $uids) . ")");
                }
                if (!empty($log_inserts)) {
                    $db->insert_query_multiple("promotionlogs", $log_inserts);
                }
                $uids = array();
                $log_inserts = array();
            }
        }
        if (count($uids) > 0) {
            if ($usergroup_select == "usergroup") {
                $db->update_query("users", array('usergroup' => $promotion['newusergroup']), "uid IN(" . implode(",", $uids) . ")");
            }
            if (!empty($log_inserts)) {
                $db->insert_query_multiple("promotionlogs", $log_inserts);
            }
            $uids = array();
            $log_inserts = array();
        }
    }
    $cache->update_moderators();
    add_task_log($task, $lang->task_promotions_ran);
}
Example #2
0
/**
 * MyBB 1.6
 * Copyright 2010 MyBB Group, All Rights Reserved
 *
 * Website: http://mybb.com
 * License: http://mybb.com/about/license
 *
 * $Id$
 */
function task_userpruning($task)
{
    global $db, $lang, $mybb, $cache;
    if ($mybb->settings['enablepruning'] != 1) {
        return;
    }
    // Are we pruning by posts?
    if ($mybb->settings['enableprunebyposts'] == 1) {
        $in_usergroups = array();
        $users = array();
        $usergroups = $cache->read("usergroups");
        foreach ($usergroups as $gid => $usergroup) {
            // Exclude admin, moderators, super moderators, banned
            if ($usergroup['canmodcp'] == 1 || $usergroup['cancp'] == 1 || $usergroup['issupermod'] == 1 || $usergroup['isbannedgroup'] == 1) {
                continue;
            }
            $in_usergroups[] = $gid;
        }
        // If we're not pruning unactivated users, then remove them from the criteria
        if ($mybb->settings['pruneunactived'] == 0) {
            $key = array_search('5', $in_usergroups);
            unset($in_usergroups[$key]);
        }
        // Exclude super admins
        $exclude_super_admins = not_super_admins(true);
        $regdate = TIME_NOW - intval($mybb->settings['dayspruneregistered']) * 24 * 60 * 60;
        $query = $db->simple_select("users", "uid", "regdate <= " . intval($regdate) . " AND postnum <= " . intval($mybb->settings['prunepostcount']) . " AND usergroup IN(" . $db->escape_string(implode(',', $in_usergroups)) . ")" . $exclude_super_admins);
        while ($user = $db->fetch_array($query)) {
            $users[$user['uid']] = $user['uid'];
        }
    }
    // Are we pruning unactivated users?
    if ($mybb->settings['pruneunactived'] == 1) {
        $regdate = TIME_NOW - intval($mybb->settings['dayspruneunactivated']) * 24 * 60 * 60;
        $query = $db->simple_select("users", "uid", "regdate <= " . intval($regdate) . " AND usergroup='5'");
        while ($user = $db->fetch_array($query)) {
            $users[$user['uid']] = $user['uid'];
        }
    }
    if (!empty($users)) {
        $uid_list = $db->escape_string(implode(',', $users));
        // Delete the user
        $db->delete_query("userfields", "ufid IN({$uid_list})");
        $db->delete_query("privatemessages", "uid IN({$uid_list})");
        $db->delete_query("events", "uid IN({$uid_list})");
        $db->delete_query("moderators", "id IN({$uid_list}) AND isgroup='0'");
        $db->delete_query("forumsubscriptions", "uid IN({$uid_list})");
        $db->delete_query("threadsubscriptions", "uid IN({$uid_list})");
        $db->delete_query("sessions", "uid IN({$uid_list})");
        $db->delete_query("banned", "uid IN({$uid_list})");
        $db->delete_query("threadratings", "uid IN({$uid_list})");
        $db->delete_query("joinrequests", "uid IN({$uid_list})");
        $db->delete_query("awaitingactivation", "uid IN({$uid_list})");
        $query = $db->delete_query("users", "uid IN({$uid_list})");
        $num_deleted = $db->affected_rows($query);
        // Remove any of the user(s) uploaded avatars
        $query = $db->simple_select("users", "avatar", "uid IN ({$uid_list}) AND avatartype = 'upload'");
        if ($db->num_rows($query)) {
            while ($avatar = $db->fetch_field($query, "avatar")) {
                $avatar = substr($avatar, 2, -20);
                @unlink(MYBB_ROOT . $avatar);
            }
        }
        // Are we removing the posts/threads of a user?
        if ($mybb->settings['prunethreads'] == 1) {
            require_once MYBB_ROOT . "inc/class_moderation.php";
            $moderation = new Moderation();
            // Threads
            $query = $db->simple_select("threads", "tid", "uid IN({$uid_list})");
            while ($thread = $db->fetch_array($query)) {
                $moderation->delete_thread($thread['tid']);
            }
            // Posts
            $query = $db->simple_select("posts", "pid", "uid IN({$uid_list})");
            while ($post = $db->fetch_array($query)) {
                $moderation->delete_post($post['pid']);
            }
        } else {
            // We're just updating the UID
            $db->update_query("posts", array('uid' => 0), "uid IN({$uid_list})");
            $db->update_query("threads", array('uid' => 0), "uid IN({$uid_list})");
        }
        // Update forum stats
        update_stats(array('numusers' => '-' . intval($num_deleted)));
        $cache->update_moderators();
        $cache->update_banned();
    }
    add_task_log($task, $lang->task_userpruning_ran);
}