module Recaptcha::ClientHelper

Public Instance Methods

recaptcha_tags(options = {}) click to toggle source

Your public API can be specified in the options hash or preferably using the Configuration.

# File lib/recaptcha/client_helper.rb, line 5
def recaptcha_tags(options = {})
  return v1_tags(options) if Recaptcha.configuration.v1?
  return v2_tags(options) if Recaptcha.configuration.v2?
end
v1_tags(options) click to toggle source
# File lib/recaptcha/client_helper.rb, line 10
def v1_tags(options)
  # Default options
  key   = options[:public_key] ||= Recaptcha.configuration.public_key
  raise RecaptchaError, "No public key specified." unless key
  error = options[:error] ||= ((defined? flash) ? flash[:recaptcha_error] : "")
  uri   = Recaptcha.configuration.api_server_url(options[:ssl])
  lang  = options[:display] && options[:display][:lang] ? options[:display][:lang].to_sym : ""
  html  = ""
  if options[:display]
    html << %Q{<script type="text/javascript">\n}
    html << %Q{  var RecaptchaOptions = #{hash_to_json(options[:display])};\n}
    html << %Q{</script>\n}
  end
  if options[:ajax]
    if options[:display] && options[:display][:custom_theme_widget]
      widget = options[:display][:custom_theme_widget]
    else
      widget = "dynamic_recaptcha"
      html << <<-EOS
       <div id="#{widget}"></div>
      EOS
    end
    html << <<-EOS
      <script type="text/javascript">
        var rc_script_tag = document.createElement('script'),
            rc_init_func = function(){Recaptcha.create("#{key}", document.getElementById("#{widget}")#{',RecaptchaOptions' if options[:display]});}
        rc_script_tag.src = "#{uri}/js/recaptcha_ajax.js";
        rc_script_tag.type = 'text/javascript';
        rc_script_tag.onload = function(){rc_init_func.call();};
        rc_script_tag.onreadystatechange = function(){
          if (rc_script_tag.readyState == 'loaded' || rc_script_tag.readyState == 'complete') {rc_init_func.call();}
        };
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(rc_script_tag);
      </script>
    EOS
  else
    html << %Q{<script type="text/javascript" src="#{uri}/challenge?k=#{key}}
    html << %Q{#{error ? "&amp;error=#{CGI::escape(error)}" : ""}}
    html << %Q{#{lang ? "&amp;lang=#{lang}" : ""}"></script>\n}
    unless options[:noscript] == false
      html << %Q{<noscript>\n  }
      html << %Q{<iframe src="#{uri}/noscript?k=#{key}" }
      html << %Q{height="#{options[:iframe_height] ||= 300}" }
      html << %Q{width="#{options[:iframe_width]   ||= 500}" }
      html << %Q{style="border:none;"></iframe><br/>\n  }
      html << %Q{<textarea name="recaptcha_challenge_field" }
      html << %Q{rows="#{options[:textarea_rows] ||= 3}" }
      html << %Q{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n  }
      html << %Q{<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>}
      html << %Q{</noscript>\n}
    end
  end
  return (html.respond_to?(:html_safe) && html.html_safe) || html
end
v2_tags(options) click to toggle source
# File lib/recaptcha/client_helper.rb, line 65
def v2_tags(options)
  key   = options[:public_key] ||= Recaptcha.configuration.public_key
  raise RecaptchaError, "No public key specified." unless key
  error = options[:error] ||= ((defined? flash) ? flash[:recaptcha_error] : "")
  uri   = Recaptcha.configuration.api_server_url(options[:ssl])
  uri += "?hl=#{options[:hl]}" unless options[:hl].blank?
  
  v2_options = options.slice(:theme, :type, :callback).map {|k,v| %Q{data-#{k}="#{v}"} }.join(" ")

  html = ""
  html << %Q{<script src="#{uri}" async defer></script>\n}
  html << %Q{<div class="g-recaptcha" data-sitekey="#{key}" #{v2_options}></div>\n}

  unless options[:noscript] == false
    fallback_uri = "#{uri.chomp('.js')}/fallback?k=#{key}"
    html << %Q{<noscript>}
    html << %Q{<div style="width: 302px; height: 352px;">}
    html << %Q{  <div style="width: 302px; height: 352px; position: relative;">}
    html << %Q{    <div style="width: 302px; height: 352px; position: absolute;">}
    html << %Q{      <iframe src="#{fallback_uri}"}
    html << %Q{                frameborder="0" scrolling="no"}
    html << %Q{                style="width: 302px; height:352px; border-style: none;">}
    html << %Q{        </iframe>}
    html << %Q{      </div>}
    html << %Q{      <div style="width: 250px; height: 80px; position: absolute; border-style: none; }
    html << %Q{             bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;">}
    html << %Q{        <textarea id="g-recaptcha-response" name="g-recaptcha-response" }
    html << %Q{                  class="g-recaptcha-response" }
    html << %Q{                  style="width: 250px; height: 80px; border: 1px solid #c1c1c1; }
    html << %Q{                  margin: 0px; padding: 0px; resize: none;" value=""> }
    html << %Q{        </textarea>}
    html << %Q{      </div>}
    html << %Q{    </div>}
    html << %Q{  </div>}
    html << %Q{</noscript>}
  end

  return (html.respond_to?(:html_safe) && html.html_safe) || html
end

Private Instance Methods

hash_to_json(hash) click to toggle source
# File lib/recaptcha/client_helper.rb, line 107
def hash_to_json(hash)
  result = "{"
  result << hash.map do |k, v|
    if v.is_a?(Hash)
      "\"#{k}\": #{hash_to_json(v)}"
    elsif ! v.is_a?(String) || k.to_s == "callback"
      "\"#{k}\": #{v}"
    else
      "\"#{k}\": \"#{v}\""
    end
  end.join(", ")
  result << "}"
end