Beispiel #1
0
    echo $OUTPUT->header();
    echo $OUTPUT->box_start();
    $logout = new single_button(new moodle_url($CFG->httpswwwroot . '/login/logout.php', array('sesskey' => sesskey(), 'loginpage' => 1)), get_string('logout'), 'post');
    $continue = new single_button(new moodle_url('/'), get_string('cancel'), 'get');
    echo $OUTPUT->confirm(get_string('cannotsignup', 'error', fullname($USER)), $logout, $continue);
    echo $OUTPUT->box_end();
    echo $OUTPUT->footer();
    exit;
}
$mform_signup = $authplugin->signup_form();
if ($mform_signup->is_cancelled()) {
    redirect(get_login_url());
} else {
    if ($user = $mform_signup->get_data()) {
        // Add missing required fields.
        $user = signup_setup_new_user($user);
        $authplugin->user_signup($user, true);
        // prints notice and link to login/index.php
        exit;
        //never reached
    }
}
// make sure we really are on the https page when https login required
$PAGE->verify_https_required();
$newaccount = get_string('newaccount');
$login = get_string('login');
$PAGE->navbar->add($login);
$PAGE->navbar->add($newaccount);
$PAGE->set_pagelayout('login');
$PAGE->set_title($newaccount);
$PAGE->set_heading($SITE->fullname);
 /**
  * Get the signup required settings and profile fields.
  *
  * @param  string $username               username
  * @param  string $password               plain text password
  * @param  string $firstname              the first name(s) of the user
  * @param  string $lastname               the family name of the user
  * @param  string $email                  a valid and unique email address
  * @param  string $city                   home city of the user
  * @param  string $country                home country code
  * @param  string $recaptchachallengehash recaptcha challenge hash
  * @param  string $recaptcharesponse      recaptcha response
  * @param  array  $customprofilefields    user custom fields (also known as user profile fields)
  * @param  string $redirect               Site url to redirect the user after confirmation
  * @return array settings and possible warnings
  * @since Moodle 3.2
  * @throws moodle_exception
  * @throws invalid_parameter_exception
  */
 public static function signup_user($username, $password, $firstname, $lastname, $email, $city = '', $country = '', $recaptchachallengehash = '', $recaptcharesponse = '', $customprofilefields = array(), $redirect = '')
 {
     global $CFG, $PAGE;
     $warnings = array();
     $params = self::validate_parameters(self::signup_user_parameters(), array('username' => $username, 'password' => $password, 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'city' => $city, 'country' => $country, 'recaptchachallengehash' => $recaptchachallengehash, 'recaptcharesponse' => $recaptcharesponse, 'customprofilefields' => $customprofilefields, 'redirect' => $redirect));
     // We need this to make work the format text functions.
     $context = context_system::instance();
     $PAGE->set_context($context);
     self::check_signup_enabled();
     // Validate profile fields param types.
     $allowedfields = profile_get_signup_fields();
     $fieldproperties = array();
     $fieldsrequired = array();
     foreach ($allowedfields as $field) {
         $fieldproperties[$field->object->inputname] = $field->object->get_field_properties();
         if ($field->object->is_required()) {
             $fieldsrequired[$field->object->inputname] = true;
         }
     }
     foreach ($params['customprofilefields'] as $profilefield) {
         if (!array_key_exists($profilefield['name'], $fieldproperties)) {
             throw new invalid_parameter_exception('Invalid field' . $profilefield['name']);
         }
         list($type, $allownull) = $fieldproperties[$profilefield['name']];
         validate_param($profilefield['value'], $type, $allownull);
         // Remove from the potential required list.
         if (isset($fieldsrequired[$profilefield['name']])) {
             unset($fieldsrequired[$profilefield['name']]);
         }
     }
     if (!empty($fieldsrequired)) {
         throw new invalid_parameter_exception('Missing required parameters: ' . implode(',', array_keys($fieldsrequired)));
     }
     // Validate the data sent.
     $data = $params;
     $data['email2'] = $data['email'];
     unset($data['recaptcharesponse']);
     unset($data['customprofilefields']);
     // Add profile fields data.
     foreach ($params['customprofilefields'] as $profilefield) {
         // First, check if the value is a json (some profile fields like text area uses an array for sending data).
         $datadecoded = json_decode($profilefield['value'], true);
         if (is_array($datadecoded) && json_last_error() == JSON_ERROR_NONE) {
             $data[$profilefield['name']] = $datadecoded;
         } else {
             $data[$profilefield['name']] = $profilefield['value'];
         }
     }
     $errors = signup_validate_data($data, array());
     // Validate recaptcha.
     if (signup_captcha_enabled()) {
         require_once $CFG->libdir . '/recaptchalib.php';
         $response = recaptcha_check_answer($CFG->recaptchaprivatekey, getremoteaddr(), $params['recaptchachallengehash'], $params['recaptcharesponse'], true);
         if (!$response->is_valid) {
             $errors['recaptcharesponse'] = $response->error;
         }
     }
     if (!empty($errors)) {
         foreach ($errors as $itemname => $message) {
             $warnings[] = array('item' => $itemname, 'itemid' => 0, 'warningcode' => 'fielderror', 'message' => s($message));
         }
         $result = array('success' => false, 'warnings' => $warnings);
     } else {
         // Save the user.
         $user = signup_setup_new_user((object) $data);
         $authplugin = get_auth_plugin('email');
         // Check if we should redirect the user once the user is confirmed.
         $confirmationurl = null;
         if (!empty($params['redirect'])) {
             // Pass via moodle_url to fix thinks like admin links.
             $redirect = new moodle_url($params['redirect']);
             $confirmationurl = new moodle_url('/login/confirm.php', array('redirect' => $redirect->out()));
         }
         $authplugin->user_signup_with_confirmation($user, false, $confirmationurl);
         $result = array('success' => true, 'warnings' => array());
     }
     return $result;
 }