/**
  * Get the signup required settings and profile fields.
  *
  * @return array settings and possible warnings
  * @since Moodle 3.2
  * @throws moodle_exception
  */
 public static function get_signup_settings()
 {
     global $CFG, $PAGE;
     $context = context_system::instance();
     // We need this to make work the format text functions.
     $PAGE->set_context($context);
     self::check_signup_enabled();
     $result = array();
     $result['namefields'] = useredit_get_required_name_fields();
     if (!empty($CFG->passwordpolicy)) {
         $result['passwordpolicy'] = print_password_policy();
     }
     if (!empty($CFG->sitepolicy)) {
         $result['sitepolicy'] = $CFG->sitepolicy;
     }
     if (!empty($CFG->defaultcity)) {
         $result['defaultcity'] = $CFG->defaultcity;
     }
     if (!empty($CFG->country)) {
         $result['country'] = $CFG->country;
     }
     if ($fields = profile_get_signup_fields()) {
         $result['profilefields'] = array();
         foreach ($fields as $field) {
             $fielddata = $field->object->get_field_config_for_external();
             $fielddata['categoryname'] = external_format_string($field->categoryname, $context->id);
             $fielddata['name'] = external_format_string($fielddata['name'], $context->id);
             list($fielddata['defaultdata'], $fielddata['defaultdataformat']) = external_format_text($fielddata['defaultdata'], $fielddata['defaultdataformat'], $context->id);
             $result['profilefields'][] = $fielddata;
         }
     }
     if (signup_captcha_enabled()) {
         require_once $CFG->libdir . '/recaptchalib.php';
         // We return the public key, maybe we want to use the javascript api to get the image.
         $result['recaptchapublickey'] = $CFG->recaptchapublickey;
         list($result['recaptchachallengehash'], $result['recaptchachallengeimage'], $result['recaptchachallengejs']) = recaptcha_get_challenge_hash_and_urls(RECAPTCHA_API_SECURE_SERVER, $CFG->recaptchapublickey);
     }
     $result['warnings'] = array();
     return $result;
 }
Beispiel #2
0
/**
 * Gets the challenge HTML (javascript and non-javascript version).
 * This is called from the browser, and the resulting reCAPTCHA HTML widget
 * is embedded within the HTML form it was called from.
 *
 * @global object
 * @param string $pubkey A public key for reCAPTCHA
 * @param string $error The error given by reCAPTCHA (optional, default is null)
 * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
 * @return string - The HTML to be embedded in the user's form.
 */
function recaptcha_get_html($pubkey, $error = null, $use_ssl = false)
{
    global $PAGE;
    $recaptchatype = optional_param('recaptcha', 'image', PARAM_TEXT);
    if ($pubkey == null || $pubkey == '') {
        die("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
    }
    if ($use_ssl) {
        $server = RECAPTCHA_API_SECURE_SERVER;
    } else {
        $server = RECAPTCHA_API_SERVER;
    }
    $errorpart = "";
    if ($error) {
        $errorpart = "&amp;error=" . $error;
    }
    list($challengehash, $imageurl, $jsurl) = recaptcha_get_challenge_hash_and_urls($server, $pubkey, $errorpart);
    $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
    $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
    $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
    $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
    $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
    $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
    $return = html_writer::script('', $jsurl);
    $return .= '<noscript>
        <div id="recaptcha_widget_noscript">
        <div id="recaptcha_image_noscript"><img src="' . $imageurl . '" alt="reCAPTCHA"/></div>';
    if ($error == 'incorrect-captcha-sol') {
        $return .= '<div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>';
    }
    if ($recaptchatype == 'image') {
        $return .= '<span class="recaptcha_only_if_image">' . $strenterthewordsabove . '</span>';
    } elseif ($recaptchatype == 'audio') {
        $return .= '<span class="recaptcha_only_if_audio">' . $strenterthenumbersyouhear . '</span>';
    }
    $return .= '<input type="text" id="recaptcha_response_field_noscript" name="recaptcha_response_field" />';
    $return .= '<input type="hidden" id="recaptcha_challenge_field_noscript" name="recaptcha_challenge_field" value="' . $challengehash . '" />';
    $return .= '<div><a href="signup.php">' . $strgetanothercaptcha . '</a></div>';
    // Disabling audio recaptchas for now: not language-independent
    /*
    if ($recaptchatype == 'image') {
        $return .= '<div class="recaptcha_only_if_image"><a href="signup.php?recaptcha=audio">' . $strgetanaudiocaptcha . '</a></div>';
    } elseif ($recaptchatype == 'audio') {
        $return .= '<div class="recaptcha_only_if_audio"><a href="signup.php?recaptcha=image">' . $strgetanimagecaptcha . '</a></div>';
    }
    */
    $return .= '
        </div>
    </noscript>';
    return $return;
}