flydev

Markup Google reCAPTCHA

Recommended Posts

MarkupGoogleRecaptcha

Google reCAPTCHA for ProcessWire.

This module simply adds reCAPTCHA V2 or Invisible reCAPTCHA to your form.


How To Install

  1. Download the zip file at Github or from the modules repository
  2. Drop the module files in /site/modules/MarkupGoogleRecaptcha
  3. In your admin, click Modules > Refresh
  4. Click "install" for "MarkupGoogleRecaptcha"

Official install/uninstall doc: http://modules.processwire.com/install-uninstall/

 

API

You must create an API key prior to use this module. Goto https://www.google.com/recaptcha/admin to create your own. Next, add the API keys information to the module's settings.

api.thumb.png.b0775a282bf6a51d48816dc347d9ae35.png

 

Usage

  1. Call the module : $captcha = $modules->get("MarkupGoogleRecaptcha");
  2. Call $captcha->getScript(); somewhere to get the javascript used by reCAPTCHA
  3. Render reCAPTCHA in a standard HTML <form></form> by calling $captcha->render() or Render reCAPTCHA in an InputfieldForm by passing as argument your form to the render function: $captcha->render($form)
  4. Call verifyResponse() to get the result. It return TRUE if the challenge was successful.

 

Example

  • Using ProcessWire's form API :
$out = '';

$captcha = $modules->get("MarkupGoogleRecaptcha");
// if submitted, check response
if ($captcha->verifyResponse() === true)
{
    $out .= "Hi " . $input->post["name"].", thanks for submitting the form!";
}
else
{
    $form = $modules->get("InputfieldForm");
    $form->action = $page->url;
    $form->method = "post";
    $form->attr("id+name", "form");

    $field = $this->modules->get('InputfieldText');
    $field->name = "name";
    $field->placeholder = "name";
    $form->add($field);

    // CAPTCHA - our form as argument, the function will add an InputfieldMarkup to our form
    $captcha->render($form);

    // add a submit button
    $submit = $this->modules->get("InputfieldSubmit");
    $submit->name = "submit";
    $submit->value = 'Submit';
    $form->add($submit);

    $out .= $form->render();

    // include javascript 
    $out .= $captcha->getScript();
}



echo $out;

 

  • Example using plain HTML Form :
$captcha = $modules->get("MarkupGoogleRecaptcha");
// if submitted check response
if ($captcha->verifyResponse() === true) {
	$out .= "Hi " . $input->post["name"] . ", thanks for submitting the form!";
} else {
	$out .= "<form method='post' action='{$page->url}'>\n"
         . "\t<input type='text' name='name'>\n"
         . $captcha->render() // render reCaptcha
         . "\t<input type='submit'>\n"
         . "</form>\n";

	$out .= $captcha->getScript();
}

echo $out;

 

 

captcha.thumb.png.68d131c777de7c108ef831fdb87c1c39.png

Edited by flydev
Update
  • Like 9

Share this post


Link to post
Share on other sites

@flydev Thanks for the contribution! Sooner or later everyone needs a good (re)CAPTCHA.

  • Like 1

Share this post


Link to post
Share on other sites

Hey,

I'm trying out this module on my site but for $captcha->verifyResponse() I am getting 1 not true?

I am workong on localhost which I have added to the domaine list with google so maybe thats the problem?

Thanks

Gar

Share this post


Link to post
Share on other sites

okay put it live and now getting

Parse error: syntax error, unexpected '[' in /var/www/vhosts/web1.plus-h.de/httpdocs/plus-h.de/site/assets/cache/FileCompiler/site/modules/MarkupGoogleRecaptcha/MarkupGoogleRecaptcha.module on line 6

Parse Error: syntax error, unexpected '[' (line 6 of /var/www/vhosts/web1.plus-h.de/httpdocs/plus-h.de/site/modules/MarkupGoogleRecaptcha/MarkupGoogleRecaptcha.module)

This error message was shown because: site is in debug mode. ($config->debug = true; => /site/config.php). Error has been logged.

Share this post


Link to post
Share on other sites

Looks like you're running PHP 5.3 on the live system. You could fix this error by replacing the short array notation [...] used throughout the module with its verbose notation as array(...), but you should really think of switching to a more current PHP release.

  • Like 5

Share this post


Link to post
Share on other sites

I have a pull-request for this module, pull-request adding multi language support to reCAPTCHA and multiple usage on one page. You can see usage on screenshot.

For set reCAPTCHA language, (reCAPTCHA Language Codes) :

<?php if(modules()->isInstalled('MarkupGoogleRecaptcha')) { echo modules()->MarkupGoogleRecaptcha->render($lang = "en"); }; ?>

For multiple reCAPTCHA usage, after all form render, call multiple usage script :

<?php if(modules()->isInstalled('MarkupGoogleRecaptcha')) { echo modules()->MarkupGoogleRecaptcha->getScriptMulti(); }; ?>

 

  • Like 2

Share this post


Link to post
Share on other sites

Good day!

Thank you @flydev for the very useful module and @ukyo for making it possible to have multiple instances of reCAPCHA on the same page.

I am having just that: two forms on the same page both with reCAPCHA via the module. Both work using jQuery ajax. After an unsuccessful call I get response with error messages. Right here I need to reset reCAPCHA like described here in the JavaScript API section. But as I have two instances of reCAPCHA on the page I need to explicitly specify the opt_widget_id option. Is there a way to I get it with the module?

Share this post


Link to post
Share on other sites

You have an option, don't call getScriptMulti(); function.

<?php if(modules()->isInstalled('MarkupGoogleRecaptcha')) { echo modules()->MarkupGoogleRecaptcha->getScriptMulti(); }; ?>

Write your own javascript by referencing getScriptMulti(); function.

    public function getScriptMulti() {
        $return = "<script type=\"text/javascript\">
                    var onloadReCaptchaCallback = function(){
                        jQuery('.g-recaptcha').each(function() {
                            var _this = jQuery(this);
                            var recaptchaID = _this.data('id'),
                                hl = _this.data('hl'),
                                sitekey = _this.data('sitekey'),
                                theme = _this.data('theme'),
                                type = _this.data('type'),
                                size = _this.data('size'),
                                index = _this.data('index');
                            if(recaptchaID !== undefined) {
                                var recaptchaWidget = grecaptcha.render(recaptchaID, {
                                    'hl' : hl,
                                    'sitekey' : sitekey,
                                    'theme' : theme,
                                    'type' : type,
                                    'size' : size,
                                    'index' : index
                                });
                                grecaptcha.getResponse(recaptchaWidget);
                                // grecaptcha.reset(recaptchaWidget);
                            }
                        });
                    };
                </script>";
        $return .= "<script src='".self::SITE_RECAPTCHA_API_URL."?onload=onloadReCaptchaCallback&render=explicit' async defer></script>";

        return $return;
    }

 

  • Like 2

Share this post


Link to post
Share on other sites

Just bumped the module to version 2.

- Added  Google Invisible reCAPTCHA.

 

To use it, no change is necessary on the frontend, you just have to configure the module to use reCAPTCHA V2 or Invisible reCAPTCHA.

I will update the readme to reflect the change made by @ukyo and the new available options.

 

  • Like 3

Share this post


Link to post
Share on other sites

@Lyndaa,

Moderator note

I have removed the link from your post. We do not allow links to third parties for first posters. This is to discourage spam (including subtle advertising). In fact, I don't see how your post is related to ProcessWire at all. I will revisit this and remove your post altogether unless you can otherwise show how it is related to ProcessWire.

  • Like 1

Share this post


Link to post
Share on other sites

I am wondering to add the reCAPTCHA to the login form of the Login-Register module.

I tried to make it by using the MarkupGoogleRecaptcha API in a hook, but without success:

// ready.php file
wire()->addHookAfter('LoginRegister::buildLoginForm', function($event) {

  $form = $event->return;

  // call the module and render reCAPTCHA
  $captcha = wire('modules')->get("MarkupGoogleRecaptcha");
  $captcha->render($form);

  // add JavaScript to the form
  $js = $captcha->getScript();
  // ... str_replace("</form>", "{$js}</form>", $form);

  // Call verifyResponse() to get the result.
  // ...

  $event->return = $form;

});

Furthermore, I do know if it is possible to add reCAPTCHA from within a hook.

Can someone help me in adding the reCAPTCHA to the login form of the Login-Register module?

Share this post


Link to post
Share on other sites

Hi @LAPS

try the following :  In ready.php put the following code :

$captchaobj = null;

    wire()->addHookProperty('LoginRegister::captcha', function ($event) {
        $event->return = wire('modules')->get("MarkupGoogleRecaptcha");
    });

    wire()->addHookAfter('LoginRegister::buildLoginForm', function ($event) {
        /* @var $form InputfieldForm */
        $obj = $event->object;
        $form = $event->return;
        $form->description = false; // remove the description
        $f = new InputfieldMarkup();
        $f->markupText = $obj->captcha->render();
        foreach ($form->children as $field) {
            if ($field instanceof InputfieldSubmit) { // find the submit button
                $form->insertBefore($f, $field);      // insert reCAPTCHA before the submit button
            }
        }

        $event->return = $form;
    });

    wire()->addHookBefore('LoginRegister::processLoginForm', function ($event) use (&$captchaobj) {
        $obj = $event->object;
        $captchaobj = $obj->captcha;
    });

    wire()->addHookBefore('Session::login', function ($event) use (&$captchaobj) {
        if (!is_null($captchaobj) && $captchaobj->verifyResponse() == false) {
            $event->arguments(0, '');
            $event->arguments(1, '');
            $event->arguments(2, false);
            $event->return = false;
        }
    });

 

Just one thing, I couldn't get the invisible mode to work with LoginRegister, only the reCAPTCHA v2.

edit:  a small hint about the getScript() :  you should call it from the main file, I mean, your "index.php" and only if the user isn't loggedin.

if(!$user->isLoggedin()) {
	echo $modules->get("MarkupGoogleRecaptcha")->getScript();
}

 

  • Like 1
  • Haha 1

Share this post


Link to post
Share on other sites

Hi @flydev,

thank you for you reply.

However, your code seems do not work. In particular, it seems do not output the JavaScript-related code ($captcha->getScript()). Can be this the problem?

Note: I am testing MarkupGoogleRecaptcha in localhost, so in Google Admin I added the domains 'localhost' and '127.0.0.1'.

LoginRegister form.png

Share this post


Link to post
Share on other sites
3 minutes ago, LAPS said:

In particular, it seems do not output the JavaScript-related code ($captcha->getScript()). Can be this the problem?

Yes it is. Read my last edit in my previous post.

 

Localhost for testing is fine, no problem with that.

Share this post


Link to post
Share on other sites

@flydev,

Thank you for your hint. Now the reCAPTCHA is visible in the login form (see the attached image).

However, even when submitting the correct user credentials, I get always the "Login failed" message. What's wrong?

LoginRegister form.png

Share this post


Link to post
Share on other sites

Good, now I think that your google challenge is not successful. Be sure that you saved the changes in your list of supported domains once you added the localhost.

 

Anything in the developer console ?

Share this post


Link to post
Share on other sites
12 minutes ago, flydev said:

Good, now I think that your google challenge is not successful. Be sure that you saved the changes in your list of supported domains once you added the localhost.

In Google Admin I set 'localhost' and '127.0.0.1' (see attached image).

I tried to submit the login form from both 'http://localhost:8888/login' and 'http://127.0.0.1:8888/login' without success: I get the "Login failed" message.

 

Note: If it can help, in Google Admin I cannot set 'localhost:8888' nor '127.0.0.1:8888' since I get the error "URL starts with an invalid scheme".

Domains.png

Share this post


Link to post
Share on other sites

Did the challenge work ? I mean, the reCapatcha widget is "green-checked" ?

To be sure that the problem is not in the code I give you (I am currently using the same snippet on a website) ,  you should try to get the challenge working on a regular module call in a regular form. Can you test it  and report back ?

Share this post


Link to post
Share on other sites
14 minutes ago, flydev said:

Did the challenge work ? I mean, the reCapatcha widget is "green-checked" ?

Yes, the reCapatcha widget is "green-checked".

14 minutes ago, flydev said:

To be sure that the problem is not in the code I give you (I am currently using the same snippet on a website) ,  you should try to get the challenge working on a regular module call in a regular form. Can you test it  and report back ?

I tested it in a regular form, and it works as expected. It does not work just for the Login-Register login form.

Share this post


Link to post
Share on other sites
5 hours ago, flydev said:

Thank you - I will check, as now, I don't know why isn't working :lol:

OK, we are close to the solution.

If it can help, I am using:

  • ProcessWire v3.0.62
  • Login/Register v0.0.2 with option "Features to use" set to all (including "Use email address for login rather that user name") and option "Profile form fields" set to "E-mail Address (required)" and "Set Password (required)" more "First name" and "Last name".

Furthermore, after the login form submission (failed) in the Session logs I get these 2 rows:

2 seconds ago --- Error: Failed login for '' - Unknown user:
2 seconds ago --- Error: Failed login for 'my_email-email.com' - Unknown user: my_email-email.com

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By BitPoet
      Since I was stuck to my flat today I took up a wish and rolled a Process module / CKEditor plugin combo that adds @-autocomplete like the mentions here in the forum to CKEditor fields. It's configurable, but only in module settings for now, the positioning of the select list is quite off and there's still some visual work to be done, so it is in early alpha state.
      Nonetheless, if you want to take a look, here it is:
      https://github.com/BitPoet/ProcessMention

       
      After installation, you may want to look into the "Additional selector" entry in the module's settings. You will most likely want to limit results to certain templates there.
       
      Edit: Updated to version 0.0.30 with fixed positioning of the dropdown.
      Edit2: Settings are configurable in field context now. If pwmentions is enabled, the according settings are shown on the "Input" tab.
       
    • By thuijzer
      https://github.com/thuijzer/processwire-FieldtypeBusinessHours
      Fieldtype Business Hours
      ProcessWire Fieldtype for entering business hours (opening hours)
       
      Input format
      Leave a day empty when closed on that day
      Times are in 24 hour format.
      9:00-12:00 9:00-12:00, 13:00-17:30 16:00-2:00  
      Usage in templates
      Days are from 1 to 7 where 1 is Monday and 7 is Sunday (ISO-8601)
      echo $page->field_name->isNowOpen() ? 'Now open' : 'Now closed'; if($page->field_name[1] == null) { echo 'Closed on Monday'; } if($page->field_name[2]->inRange('11:00')) { echo 'Tuesday at 11:00 open'; } echo $page->field_name[1]; echo $page->field_name[1]->getEntries()->getFrom()->format('H:i');  
    • By hellomoto
      I was working on this:
      class PWCRM extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Client Relationship Management', 'version' => .001, 'singular' => true, 'autoload' => true ); } public function init() {} public function ready() { $this->pages->addHookBefore('render', $this, 'accessHook'); $this->pages->addHookAfter('render', $this, 'hookAfterPageRender'); } public function accessHook(HookEvent $event) { $page = $this->wire('page'); if (!strpos($page->template->tags, 'crm')) return; if (!$this->wire('user')->hasRole('crm')) $this->wire('session')->redirect($this->wire('config')->urls->login);//throw new Wire404Exception(); } public function hookAfterPageRender(HookEvent $event) { $page = $event->object; echo $page->template->tags; if (!strpos($page->template->tags, 'crm')) return; echo $this->wire('config')->urls->templates; include_once($this->wire('config')->urls->templates.'functions.inc'); $pagehtml = $event->return; $pagehtml = str_replace( '</head>', '<link id="css_crm" rel="stylesheet" href="'.$this->wire('config')->urls->templates.'css/crm.css"> </head>', $pagehtml ); $event->return = $pagehtml; //$event->replace = true; } } I have tried placing the hooks into the init() function, and more... Neither method is effective. The echoes now output, but no redirection (although I have the access settings for the top-level template for this set to render a 404 for underprivileged users, which it does, overriding this, but nonetheless this should work aside from that.
      Then I wrote this up quick:
      <?php namespace ProcessWire; class MaintenanceMode extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Maintenance Mode', 'version' => 1, 'summary' => 'Disables the website frontend for non-superusers.', 'singular' => true, 'autoload' => true, 'permanent' => false ); } public function init() { $this->addHookBefore('Page::render', $this, 'displayDecide'); } public function displayDecide($event) { $page = $event->object; if ($page->template == 'admin' || $this->wire('user')->hasRole('superuser')) return; // replace the method hooked $event->replace = true; $event->return = "Patience please while we undergo some brief maintenance work."; } } which likewise avails nothing. What the hell is my problem here? 
    • By adrian
           Tracy Debugger for ProcessWire
      The ultimate “swiss army knife” debugging and development tool for the ProcessWire CMF/CMS
       

       
      Integrates and extends Nette's Tracy debugging tool and adds 30+ custom tools designed for effective ProcessWire debugging and lightning fast development
      The most comprehensive set of instructions and examples is available at: https://adrianbj.github.io/TracyDebugger
      Modules Directory: http://modules.processwire.com/modules/tracy-debugger/
      Github: https://github.com/adrianbj/TracyDebugger
      A big thanks to @tpr for introducing me to Tracy and for the idea for this module and for significant feedback, testing, and feature suggestions.