/**
 * Sends notification e-mails to users in two situations:
 *
 *  - Their account is about to expire. This is controlled by the 'expiry'
 *    field of the usr table. Once that time has passed, the user may not
 *    log in.
 *  - They have not logged in for close to a certain amount of time. If that
 *    amount of time has passed, the user may not log in.
 *
 * The actual prevention of users logging in is handled by the authentication
 * code. This cron job sends e-mails to notify users that these events will
 * happen soon.
 */
function auth_handle_account_expiries()
{
    // The 'expiry' flag on the usr table
    $sitename = get_config('sitename');
    $wwwroot = get_config('wwwroot');
    $expire = get_config('defaultaccountinactiveexpire');
    $warn = get_config('defaultaccountinactivewarn');
    $daystoexpire = ceil($warn / 86400) . ' ';
    $daystoexpire .= $daystoexpire == 1 ? get_string('day') : get_string('days');
    // Expiry warning messages
    if ($users = get_records_sql_array('SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff
        FROM {usr} u
        WHERE ' . db_format_tsfield('u.expiry', false) . ' < ?
        AND expirymailsent = 0 AND deleted = 0', array(time() + $warn))) {
        foreach ($users as $user) {
            $displayname = display_name($user);
            _email_or_notify($user, get_string('accountexpirywarning'), get_string('accountexpirywarningtext', 'mahara', $displayname, $sitename, $daystoexpire, $wwwroot . 'contact.php', $sitename), get_string('accountexpirywarninghtml', 'mahara', $displayname, $sitename, $daystoexpire, $wwwroot . 'contact.php', $sitename));
            set_field('usr', 'expirymailsent', 1, 'id', $user->id);
        }
    }
    // Actual expired users
    if ($users = get_records_sql_array('SELECT id
        FROM {usr}
        WHERE ' . db_format_tsfield('expiry', false) . ' < ?', array(time()))) {
        // Users have expired!
        foreach ($users as $user) {
            expire_user($user->id);
        }
    }
    if ($expire) {
        // Inactivity (lastlogin is too old)
        // MySQL doesn't want to compare intervals, so when editing the where clauses below, make sure
        // the intervals are always added to datetimes first.
        $dbexpire = db_interval($expire);
        $dbwarn = db_interval($warn);
        $installationtime = get_config('installation_time');
        $lastactive = "COALESCE(u.lastaccess, u.lastlogin, u.ctime, ?)";
        // Actual inactive users
        if ($users = get_records_sql_array("\n            SELECT u.id\n            FROM {usr} u\n            WHERE {$lastactive} + {$dbexpire} < current_timestamp\n                AND (u.expiry IS NULL OR u.expiry > current_timestamp) AND id > 0", array($installationtime))) {
            // Users have become inactive!
            foreach ($users as $user) {
                deactivate_user($user->id);
            }
        }
        // Inactivity warning emails
        if ($users = get_records_sql_array("\n            SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff\n            FROM {usr} u\n            WHERE {$lastactive} + {$dbexpire} < current_timestamp + {$dbwarn}\n                AND (u.expiry IS NULL OR u.expiry > current_timestamp)\n                AND inactivemailsent = 0 AND deleted = 0 AND id > 0", array($installationtime))) {
            foreach ($users as $user) {
                $displayname = display_name($user);
                _email_or_notify($user, get_string('accountinactivewarning'), get_string('accountinactivewarningtext', 'mahara', $displayname, $sitename, $daystoexpire, $sitename), get_string('accountinactivewarninghtml', 'mahara', $displayname, $sitename, $daystoexpire, $sitename));
                set_field('usr', 'inactivemailsent', 1, 'id', $user->id);
            }
        }
    }
    // Institution membership expiry
    delete_records_sql('DELETE FROM {usr_institution}
        WHERE ' . db_format_tsfield('expiry', false) . ' < ? AND expirymailsent = 1', array(time()));
    // Institution membership expiry warnings
    if ($users = get_records_sql_array('
        SELECT
            u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff,
            ui.institution, ui.expiry, i.displayname as institutionname
        FROM {usr} u
        INNER JOIN {usr_institution} ui ON u.id = ui.usr
        INNER JOIN {institution} i ON ui.institution = i.name
        WHERE ' . db_format_tsfield('ui.expiry', false) . ' < ?
        AND ui.expirymailsent = 0 AND u.deleted = 0', array(time() + $warn))) {
        foreach ($users as $user) {
            $displayname = display_name($user);
            _email_or_notify($user, get_string('institutionmembershipexpirywarning'), get_string('institutionmembershipexpirywarningtext', 'mahara', $displayname, $user->institutionname, $sitename, $daystoexpire, $wwwroot . 'contact.php', $sitename), get_string('institutionmembershipexpirywarninghtml', 'mahara', $displayname, $user->institutionname, $sitename, $daystoexpire, $wwwroot . 'contact.php', $sitename));
            set_field('usr_institution', 'expirymailsent', 1, 'usr', $user->id, 'institution', $user->institution);
        }
    }
}
示例#2
0
文件: lib.php 项目: Br3nda/mahara
/**
 * Sends notification e-mails to users in two situations:
 *
 *  - Their account is about to expire. This is controlled by the 'expiry'
 *    field of the usr table. Once that time has passed, the user may not
 *    log in.
 *  - They have not logged in for close to a certain amount of time. If that
 *    amount of time has passed, the user may not log in.
 *
 * The actual prevention of users logging in is handled by the authentication
 * code. This cron job sends e-mails to notify users that these events will
 * happen soon.
 */
function auth_handle_account_expiries()
{
    // The 'expiry' flag on the usr table
    $sitename = get_config('sitename');
    $wwwroot = get_config('wwwroot');
    $expire = get_config('defaultaccountinactiveexpire');
    $warn = get_config('defaultaccountinactivewarn');
    $daystoexpire = ceil($warn / 86400) . ' ';
    $daystoexpire .= $daystoexpire == 1 ? get_string('day') : get_string('days');
    // Expiry warning messages
    if ($users = get_records_sql_array('SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff
        FROM {usr} u
        WHERE ' . db_format_tsfield('u.expiry', false) . ' < ?
        AND expirymailsent = 0', array(time() + $warn))) {
        foreach ($users as $user) {
            $displayname = display_name($user);
            email_user($user, null, get_string('accountexpirywarning'), get_string('accountexpirywarningtext', 'mahara', $displayname, $sitename, $daystoexpire, $wwwroot . 'contact.php', $sitename), get_string('accountexpirywarninghtml', 'mahara', $displayname, $sitename, $daystoexpire, $wwwroot . 'contact.php', $sitename));
            set_field('usr', 'expirymailsent', 1, 'id', $user->id);
        }
    }
    // Actual expired users
    if ($users = get_records_sql_array('SELECT id
        FROM {usr}
        WHERE ' . db_format_tsfield('expiry', false) . ' < ?', array(time()))) {
        // Users have expired!
        foreach ($users as $user) {
            expire_user($user->id);
        }
    }
    if ($expire) {
        // Inactivity (lastlogin is too old)
        if ($users = get_records_sql_array('SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff
            FROM {usr} u
            WHERE (? - ' . db_format_tsfield('u.lastlogin', false) . ') > ' . ($expire - $warn) . '
            AND inactivemailsent = 0', array(time()))) {
            foreach ($users as $user) {
                $displayname = display_name($user);
                email_user($user, null, get_string('accountinactivewarning'), get_string('accountinactivewarningtext', 'mahara', $displayname, $sitename, $daystoexpire, $sitename), get_string('accountinactivewarninghtml', 'mahara', $displayname, $sitename, $daystoexpire, $sitename));
                set_field('usr', 'inactivemailsent', 1, 'id', $user->id);
            }
        }
        // Actual inactive users
        if ($users = get_records_sql_array('SELECT u.id
            FROM {usr} u
            WHERE (? - ' . db_format_tsfield('lastlogin', false) . ') > ?', array(time(), $expire))) {
            // Users have become inactive!
            foreach ($users as $user) {
                deactivate_user($user->id);
            }
        }
    }
    // Institution membership expiry
    delete_records_sql('DELETE FROM {usr_institution} 
        WHERE ' . db_format_tsfield('expiry', false) . ' < ? AND expirymailsent = 1', array(time()));
    // Institution membership expiry warnings
    if ($users = get_records_sql_array('
        SELECT
            u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff,
            ui.institution, ui.expiry, i.displayname as institutionname
        FROM {usr} u
        INNER JOIN {usr_institution} ui ON u.id = ui.usr
        INNER JOIN {institution} i ON ui.institution = i.name
        WHERE ' . db_format_tsfield('ui.expiry', false) . ' < ?
        AND ui.expirymailsent = 0', array(time() + $warn))) {
        foreach ($users as $user) {
            $displayname = display_name($user);
            email_user($user, null, get_string('institutionmembershipexpirywarning'), get_string('institutionmembershipexpirywarningtext', 'mahara', $displayname, $user->institutionname, $sitename, $daystoexpire, $wwwroot . 'contact.php', $sitename), get_string('institutionmembershipexpirywarninghtml', 'mahara', $displayname, $user->institutionname, $sitename, $daystoexpire, $wwwroot . 'contact.php', $sitename));
            set_field('usr_institution', 'expirymailsent', 1, 'usr', $user->id, 'institution', $user->institution);
        }
    }
}