Example #1
0
/**
 * Lockout user and send notification email.
 *
 * @param stdClass $user
 */
function login_lock_account($user)
{
    global $CFG;
    if ($user->mnethostid != $CFG->mnet_localhost_id) {
        return;
    }
    if (isguestuser($user)) {
        return;
    }
    if (get_user_preferences('login_lockout_ignored', 0, $user)) {
        // This user can not be locked out.
        return;
    }
    $alreadylockedout = get_user_preferences('login_lockout', 0, $user);
    set_user_preference('login_lockout', time(), $user);
    if ($alreadylockedout == 0) {
        $secret = random_string(15);
        set_user_preference('login_lockout_secret', $secret, $user);
        $oldforcelang = force_current_language($user->lang);
        $site = get_site();
        $supportuser = core_user::get_support_user();
        $data = new stdClass();
        $data->firstname = $user->firstname;
        $data->lastname = $user->lastname;
        $data->username = $user->username;
        $data->sitename = format_string($site->fullname);
        $data->link = $CFG->wwwroot . '/login/unlock_account.php?u=' . $user->id . '&s=' . $secret;
        $data->admin = generate_email_signoff();
        $message = get_string('lockoutemailbody', 'admin', $data);
        $subject = get_string('lockoutemailsubject', 'admin', format_string($site->fullname));
        if ($message) {
            // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
            email_to_user($user, $supportuser, $subject, $message);
        }
        force_current_language($oldforcelang);
    }
}
Example #2
0
/**
 * send_password_change_info.
 *
 * @uses $CFG
 * @param user $user A {@link $USER} object
 * @return bool|string Returns "true" if mail was sent OK, "emailstop" if email
 *          was blocked by user and "false" if there was another sort of error.
 */
function send_password_change_info($user)
{
    global $CFG;
    $site = get_site();
    $supportuser = generate_email_supportuser();
    $systemcontext = get_context_instance(CONTEXT_SYSTEM);
    $data = new object();
    $data->firstname = $user->firstname;
    $data->lastname = $user->lastname;
    $data->sitename = format_string($site->fullname);
    $data->admin = generate_email_signoff();
    $userauth = get_auth_plugin($user->auth);
    if (!is_enabled_auth($user->auth) or $user->auth == 'nologin') {
        $message = get_string('emailpasswordchangeinfodisabled', '', $data);
        $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
        return email_to_user($user, $supportuser, $subject, $message);
    }
    if ($userauth->can_change_password() and $userauth->change_password_url()) {
        // we have some external url for password changing
        $data->link .= $userauth->change_password_url();
    } else {
        //no way to change password, sorry
        $data->link = '';
    }
    if (!empty($data->link) and has_capability('moodle/user:changeownpassword', $systemcontext, $user->id)) {
        $message = get_string('emailpasswordchangeinfo', '', $data);
        $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
    } else {
        $message = get_string('emailpasswordchangeinfofail', '', $data);
        $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
    }
    return email_to_user($user, $supportuser, $subject, $message);
}
Example #3
0
/**
 * Sends an email containinginformation on how to change your password.
 *
 * @param stdClass $user A {@link $USER} object
 * @return bool Returns true if mail was sent OK and false if there was an error.
 */
function send_password_change_info($user)
{
    global $CFG;
    $site = get_site();
    $supportuser = core_user::get_support_user();
    $systemcontext = context_system::instance();
    $data = new stdClass();
    $data->firstname = $user->firstname;
    $data->lastname = $user->lastname;
    $data->sitename = format_string($site->fullname);
    $data->admin = generate_email_signoff();
    $userauth = get_auth_plugin($user->auth);
    if (!is_enabled_auth($user->auth) or $user->auth == 'nologin') {
        $message = get_string('emailpasswordchangeinfodisabled', '', $data);
        $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
        // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
        return email_to_user($user, $supportuser, $subject, $message);
    }
    if ($userauth->can_change_password() and $userauth->change_password_url()) {
        // We have some external url for password changing.
        $data->link .= $userauth->change_password_url();
    } else {
        // No way to change password, sorry.
        $data->link = '';
    }
    if (!empty($data->link) and has_capability('moodle/user:changeownpassword', $systemcontext, $user->id)) {
        $message = get_string('emailpasswordchangeinfo', '', $data);
        $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
    } else {
        $message = get_string('emailpasswordchangeinfofail', '', $data);
        $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
    }
    // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
    return email_to_user($user, $supportuser, $subject, $message);
}
function app_reset_password_and_mail($user)
{
    global $CFG;
    $site = get_site();
    $supportuser = generate_email_supportuser();
    $userauth = get_auth_plugin($user->auth);
    if (!$userauth->can_reset_password() or !is_enabled_auth($user->auth)) {
        trigger_error("Attempt to reset user password for user {$user->username} with Auth {$user->auth}.");
        return false;
    }
    $newpassword = generate_password();
    if (!$userauth->user_update_password($user, $newpassword)) {
        $error->error = true;
        $error->msg = 'fp_passwordgen_failure';
        echo json_encode($error);
        die;
    }
    $a = new stdClass();
    $a->firstname = $user->firstname;
    $a->lastname = $user->lastname;
    $a->sitename = format_string($site->fullname);
    $a->username = $user->username;
    $a->newpassword = $newpassword;
    $a->signoff = generate_email_signoff();
    $message = 'Hi ' . $a->firstname . ',

Your account password at \'' . $a->sitename . '\' has been reset
and you have been issued with a new temporary password.

Your current login information is now:
   username: '******'
   password: '******'

Cheers from the \'' . $a->sitename . '\' administrator,
' . $a->signoff;
    //$message = get_string('newpasswordtext', '', $a);
    $subject = format_string($site->fullname) . ': ' . get_string('changedpassword');
    unset_user_preference('create_password', $user);
    // prevent cron from generating the password
    //directly email rather than using the messaging system to ensure its not routed to a popup or jabber
    return email_to_user($user, $supportuser, $subject, $message);
}
Example #5
0
function local_send_confirmation_email($user)
{
    global $CFG, $USER;
    if (function_exists('login_signup_form')) {
        $mform_signup = new login_signup_form();
        $formdata = $mform_signup->get_data();
    }
    $site = get_site();
    $sitecontext = get_context_instance(CONTEXT_SYSTEM);
    $data = new object();
    $data->firstname = fullname($user);
    $data->sitename = format_string($site->fullname);
    $data->admin = generate_email_signoff();
    $data->custommsg = '';
    $invite = false;
    if (isloggedin() && has_capability('moodle/local:invitenewuser', $sitecontext)) {
        $supportuser = $USER;
        $data->fromuser = fullname($supportuser);
        $invite = true;
    } else {
        $supportuser = generate_email_supportuser();
    }
    if (!empty($formdata->message)) {
        $data->custommsg = $formdata->message;
    }
    $subject = get_string('emailconfirmationsubject', '', format_string($site->fullname));
    if ($invite) {
        $data->link = $CFG->wwwroot . '/local/login/confirm.php?data=' . $user->secret . '/' . urlencode($user->username);
        $message = get_string('emailconfirmation', 'block_tao_team_groups', $data);
    } else {
        $data->link = $CFG->wwwroot . '/login/confirm.php?data=' . $user->secret . '/' . urlencode($user->username);
        $message = get_string('emailconfirmation', 'block_tao_team_groups', $data);
    }
    $messagehtml = text_to_html($message, false, false, true);
    $user->mailformat = 1;
    // Always send HTML version as well
    return email_to_user($user, $supportuser, $subject, $message, $messagehtml);
}
 /**
  * Send the password to the user via email.
  *
  * @global object
  * @global object
  * @param user $user A {@link $USER} object
  * @return boolean|string Returns "true" if mail was sent OK and "false" if there was an error
  */
 public function mail_password($user, $password)
 {
     global $CFG, $DB;
     $site = get_site();
     $supportuser = generate_email_supportuser();
     $a = new stdClass();
     $a->firstname = fullname($user, true);
     $a->sitename = format_string($site->fullname);
     $a->username = $user->username;
     $a->newpassword = $password;
     $a->link = $CFG->wwwroot . '/login/';
     $a->signoff = generate_email_signoff();
     $message = get_string('newusernewpasswordtext', '', $a);
     $subject = format_string($site->fullname) . ': ' . get_string('newusernewpasswordsubj');
     return email_to_user($user, $supportuser, $subject, $message);
 }
         }
     }
     // commit the transaction
     $transaction->allow_commit();
 } catch (Exception $e) {
     $transaction->rollback($e);
     notice("There was an error storing the generated tokens into the database. Please try again.");
     exit;
 }
 $course = get_course($data->course);
 $data->coursename = $course->fullname;
 $data->tokennumberplural = $data->tokennumber > 1 ? 's' : '';
 $data->seatspertokenplural = $data->seatspertoken > 1 ? 's' : '';
 $data->wwwroot = $CFG->wwwroot;
 $data->tokens = implode(', ', $tokens);
 $data->adminsignoff = generate_email_signoff();
 // moodlelib
 // get text to use for on-screen notice and email
 $messagehtml = get_string('noticetext', 'block_enrol_token_manager', $data);
 $messagetext = html_to_text($messagehtml, 75, false);
 // queue email for sending if required
 if (isset($data->emailaddress) === true && trim($data->emailaddress != '')) {
     // create a fake user to send email to because the email recipient may not be a system user (yet)
     $fakeUser = new stdClass();
     $fakeUser->email = $data->emailaddress;
     $fakeUser->deleted = false;
     $fakeUser->id = 0;
     $fakeUser->mailformat = 1;
     // if email fails to send - warn token creating user on screen
     if (email_to_user($fakeUser, core_user::get_support_user, get_string('emailsubject', 'block_enrol_token_manager'), $messagetext, $messagehtml) === false) {
         $messagehtml = $OUTPUT->error_text('Warning - there was a problem automatcially emailing these token codes. ' . 'Please copy the message below and paste it into a manually generated email.') . $messagehtml;
function send_application_confirmation_email($user)
{
    global $CFG;
    $data = new stdClass();
    $data->firstname = fullname($user);
    $data->sitename = format_string($CFG->pageheading);
    $data->admin = generate_email_signoff();
    $subject = get_string('emailconfirmationsubject', '', $data->sitename);
    $username = urlencode($user->username);
    $username = str_replace('.', '%2E', $username);
    // Prevent problems with trailing dots
    $link = $CFG->wwwroot . '/local/obu_application/confirm.php?data=' . $user->secret . '/' . $username;
    $data->link = '<a href="' . $link . '">' . $link . '</a>';
    $message = get_string('emailconfirmation', '', $data);
    $messagehtml = text_to_html($message, false, false, true);
    $user->mailformat = 1;
    // Always send HTML version as well.
    // Send from HLS
    $hls = get_complete_user_data('username', 'hls');
    $hls->customheaders = array('Precedence: Bulk', 'X-Auto-Response-Suppress: All', 'Auto-Submitted: auto-generated');
    return email_to_user($user, $hls, $subject, $message, $messagehtml);
}
Example #9
0
 function user_signup($user, $notify = false)
 {
     global $CFG, $DB, $SESSION;
     require_once $CFG->dirroot . '/user/profile/lib.php';
     require_once $CFG->dirroot . '/enrol/token/lib.php';
     $lang = empty($user->lang) ? $CFG->lang : $user->lang;
     $site = get_site();
     $supportuser = core_user::get_support_user();
     $newpassword = generate_password();
     // the token the user entered (which is now validated)
     $tokenValue = $user->token;
     $newuser = new stdClass();
     $newuser->auth = 'token';
     // since technially this auth plugin is a skin
     $newuser->firstname = $user->firstname;
     $newuser->lastname = $user->lastname;
     $newuser->password = hash_internal_user_password($newpassword, false);
     $newuser->policyagreed = 1;
     // might need to put this in somewhere
     $newuser->username = $user->email;
     $newuser->email = $user->email;
     $newuser->lastip = getremoteaddr();
     $newuser->timecreated = time();
     $newuser->timemodified = $newuser->timecreated;
     $newuser->mnethostid = $CFG->mnet_localhost_id;
     $newuser = self::truncate_user_obj($newuser);
     if (($newuser->id = $DB->insert_record('user', $newuser)) === false) {
         notice(get_string('signupfailure', 'auth_token'), $CFG->wwwroot);
         return false;
     }
     $user = get_complete_user_data('id', $newuser->id);
     \core\event\user_created::create_from_userid($user->id)->trigger();
     // just the query part of post-login redirect
     $params = empty($SESSION->wantsurl) === true ? '' : parse_url($SESSION->wantsurl, PHP_URL_QUERY);
     $a = new stdClass();
     $a->firstname = $user->firstname;
     $a->lastname = $user->lastname;
     $a->username = $user->username;
     $a->password = $newpassword;
     $a->sitename = format_string($site->fullname);
     $a->link = $CFG->wwwroot . '/auth/token/login.php?' . $params;
     $a->signoff = generate_email_signoff();
     $message = (string) new lang_string('signup_userregoemail', 'auth_token', $a, $lang);
     $subject = format_string($site->fullname) . ': ' . (string) new lang_string('newusernewpasswordsubj', '', $a, $lang);
     // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
     email_to_user($user, $supportuser, $subject, $message);
     // log the user in immediately
     if (($user = authenticate_user_login($user->username, $newpassword)) === false || complete_user_login($user) == null) {
         notice(get_string('autologinfailure', 'auth_token'), $CFG->wwwroot);
         return false;
     }
     // now, actually DO the enrolment for this course / user
     $token_plugin = new enrol_token_plugin();
     $courseId = 0;
     $return_to_url = empty($SESSION->wantsurl) === true ? $CFG->wwwroot : $SESSION->wantsurl;
     $enrolled_ok = $token_plugin->doEnrolment($tokenValue, $courseId, $return_to_url);
     if ($enrolled_ok == true) {
         redirect($return_to_url);
     }
     return $enrolled_ok == true;
 }
Example #10
0
/**
 * Lockout user and send notification email.
 *
 * @param stdClass $user
 */
function login_lock_account($user)
{
    global $CFG, $SESSION;
    if ($user->mnethostid != $CFG->mnet_localhost_id) {
        return;
    }
    if (isguestuser($user)) {
        return;
    }
    if (get_user_preferences('login_lockout_ignored', 0, $user)) {
        // This user can not be locked out.
        return;
    }
    $alreadylockedout = get_user_preferences('login_lockout', 0, $user);
    set_user_preference('login_lockout', time(), $user);
    if ($alreadylockedout == 0) {
        $secret = random_string(15);
        set_user_preference('login_lockout_secret', $secret, $user);
        // Some nasty hackery to get strings and dates localised for target user.
        $sessionlang = isset($SESSION->lang) ? $SESSION->lang : null;
        if (get_string_manager()->translation_exists($user->lang, false)) {
            $SESSION->lang = $user->lang;
            moodle_setlocale();
        }
        $site = get_site();
        $supportuser = core_user::get_support_user();
        $data = new stdClass();
        $data->firstname = $user->firstname;
        $data->lastname = $user->lastname;
        $data->username = $user->username;
        $data->sitename = format_string($site->fullname);
        $data->link = $CFG->wwwroot . '/login/unlock_account.php?u=' . $user->id . '&s=' . $secret;
        $data->admin = generate_email_signoff();
        $message = get_string('lockoutemailbody', 'admin', $data);
        $subject = get_string('lockoutemailsubject', 'admin', format_string($site->fullname));
        if ($message) {
            // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
            email_to_user($user, $supportuser, $subject, $message);
        }
        if ($SESSION->lang !== $sessionlang) {
            $SESSION->lang = $sessionlang;
            moodle_setlocale();
        }
    }
}