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

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

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

  • Like 2

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 2

Share this post


Link to post
Share on other sites

Hi @flydev. Great module. I'm using it on a custom form. I'd like to add it to an existing site where I've used Ryan's Form Template Processor module. (It's a long form; I'd rather not start from scratch). I've had a look at what you did with the log in form above; would I take a similar approach? Is there an easy way to slot the Captcha form in before the submit button (instead of outside the generated form)? I'm pretty new to hooks, if that's what's needed. Any suggestions appreciated.

  • Thanks 1

Share this post


Link to post
Share on other sites

hi @strandoo thanks you.

 

I never used the module you are talking about but I read the module code on Github and it look like you can insert the ReCaptcha before the submit button by hooking into the function ___buildForm().

Please try the following (not tested) and let me know. In ready.php put :

wire()->addHookAfter('FormTemplateProcessor::buildForm', function ($event) {
        /* @var $form InputfieldForm */
        $form = $event->return;
        $f = new InputfieldMarkup();
        $f->markupText = wire('modules')->get("MarkupGoogleRecaptcha")->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;
    });

 

Share this post


Link to post
Share on other sites

Hi @flydev My, that was quick! I did put this in my ready.php file, but don't see a captcha. Do I need to include your '$captcha = $modules->get("MarkupGoogleRecaptcha");' on the page? (I've tried it both ways). I've also tried this $f->markupText = 'Foobar'; (instead of your module) and 'Foobar' appears above the submit button, as expected.

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 blad
      Hi guys!
      I just uploaded a module to explore files based on elFinder. By default it will show the "Files" folder.
      Screenshots:

      Video:
       
      To do:
       More options To fix:
       The function of rotating or scaling an image fails  Image editors Github:
      https://github.com/LuisSantiago/ProcessElFinder/
      I hope you like it.
    • By BitPoet
      I'm really in love with FormBuilder, but the one thing missing to match all my end users' expectations were repeatable field groups. Think repeaters, in ProcessWire terms. Our primary application of PW is our corporate intranet, so "lines" of fields are quite common in the forms I build. We have all kinds of request forms where the information for a varying number of colleagues needs to be entered (from meal order to flight booking request) and where it is simply impractical to send a form for each, and I don't want to clutter my forms with multiple instances of fields that may only get used ten percent of the time.
      That's why I started to build FormBuilderMultiplier (link to GitHub).
      What it does:
      Adds an option to make a regular Fieldgroup repeatable Lets you limit the number of instances of a Fieldgroup on the form Adds an "Add row" button the form that adds another instance of the Fieldgroup's fields Adds a counter suffix at the end of every affected field's label Stores the entered values just like regular fields Makes the entered values available in preview and email notifications Supports most text based fields, textareas and selects (really, I haven't had enough time to test all the available choices yet) What it doesn't do (yet):
      Support saving to ProcessWire pages (i.e. real Repeaters) I haven't tested all the validation stuff, Date/Time inputs etc. yet, but since I'm utterly swamped with other stuff at work, I didn't want to wait until I have it polished. Any feedback is welcome. There might also be some issues with different output frameworks that I haven't encountered yet. The forms I work with mostly use UIKit.
      Status:
      Still alpha, so test well before using it in the field.
      Known issues:
      When rows are added, the form's iframe needs to be resized, which isn't completely clean yet.
      How it works:
      The Fieldgroup settings are added through regular hooks, as is the logic that adds the necessary field copies for processing the form and displaying previews.
      "Multiplied" field instances are suffixed with _NUM, where NUM is an incremental integer starting from 1. So if you have add two fields named "surname" and "givenname" to a fieldgroup and check the "multiply" checkbox, the form will initially have "surname_1" and "givenname_1" field (I'm still considering changing that to make the risk to shoot oneself into the foot by having a regular "surname_1" field somewhere else in the form less likely).
      When a "row" is added, the first row is cloned through JS and the counter in the fields' IDs, names and "for" attributes as well as the counter in the label are incremented before appending the copies to the Fieldset container in the form.
      To keep backend and frontend in sync, a hidden field named [name of the fieldset]__multiplier_rows is added to the form. Both the backend and the frontend script use this to store and retrieve the number of "rows".
      ToDo:
      Naturally, add the option to store the data in real repeaters when saving to pages. Do a lot of testing (and likely fixing). Make a few things (like the "Add row" button label etc.) configurable in field(set) context. Add a smooth API to retrieve the multiplied values as WireArrays. The mandatory moving screenshot:

    • By MoritzLost
      Hello there,
      I'm working on a tiny textformatter module that searches the text for titles of other pages on your site and creates hyperlinks to them. I'm not sure if something like this exists already, but I haven't found anything in the module directory, so I wrote my own solution 🙂
      It's not properly tested yet and is still missing some functionality I would like to implement, so at the moment it should be considered in BETA. Features include limiting the pages that will get searched by template, and adding a custom CSS class to the generated hyperlinks. As I'm writing this I noticed that it will probably include unpublished and hidden pages at the moment, so yeah ... it's still in development alright 😅
      You can download the module from Github:
      https://github.com/MoritzLost/TextformatterPageTitleLinks
      There's some more information in the readme as well.
      Anyway, let me know what you think! I'm happy about any feedback, possible improvements or ideas on how to improve the module. Cheers.
    • By adrian
      This module provides a way to rapidly generate Page fields and the required templates and pages for use as a drop down select (or any other Page field type).
      This module will let you create a full page field setup in literally a few seconds 
      To use, run Page Field Select Creator from the Setup Menu
      Enter a Field Title, eg: Room Types Select Options - These will become the child pages that will populate the page field select options. There are two different options.
       
      Option 1. TITLE FIELD ONLY - enter one option per line, eg:
       
      Single
      Double
      Suite
       
       
      Option 2. MULTIPLE FIELDS - the first line is used for the field names and the first field must be 'Title'. Subsequent lines are the values for the fields, eg:
       
      Title, Number of Beds, Number of People, Kitchen Facilities
      Single, 1, 1, Fridge Only
      Double, 2, 2, Fridge Only
      Suite, 3, 6, Full Kitchen
        Choose the parent where the page tree of options will be created, eg a hidden "Options" parent page Select a "Deference in API as" option depending on your needs Choose the input field type Check whether "Allow new pages to be created from field?" should be enabled. As an example, if you entered "Room Types" as the field title, you would end up with all of the following automatically created:
      a fully configured page field called: room_types MULTIPLE FIELDS OPTION - 3 additional fields - number_of_beds, number_of_people, kitchen a parent template called: room_types a child template called: room_types_items (with either just a title field, or with the 3 additional fields as well) a parent page called: Room Types a series of child pages named and titled based on the per line entries in the Select Options textarea The templates are configured such that the "room_types_items" child template can only have the main "room_types" template as a parent, and vice versa.

      Then all you have to do is add the newly created page field to any template you want and you're ready to go!
       
      You can grab it from:
       
      Modules directory: http://modules.processwire.com/modules/process-page-field-select-creator/
      Github: https://github.com/adrianbj/ProcessPageFieldSelectCreator
       

    • By bernhard
      WHY?
      This module was built to fill the gap between simple $pages->find() operations and complex SQL queries.
      The problem with $pages->find() is that it loads all pages into memory and that can be a problem when querying multiple thousands of pages. Even $pages->findMany() loads all pages into memory and therefore is a lot slower than regular SQL.
      The problem with SQL on the other hand is, that the queries are quite complex to build. All fields are separate tables, some repeatable fields use multiple rows for their content that belong to only one single page, you always need to check for the page status (which is not necessary on regular find() operations and therefore nobody is used to that).
      In short: It is far too much work to efficiently and easily get an array of data based on PW pages and fields and I need that a lot for my RockGrid module to build all kinds of tabular data.

      Basic Usage

       
      Docs & Download
      https://modules.processwire.com/modules/rock-finder/
      https://github.com/BernhardBaumrock/RockFinder
       
      Changelog
      180817, v1.0.6, support for joining multiple finders 180810, v1.0.5, basic support for options fields 180528, v1.0.4, add custom select statement option 180516, change sql query method, bump version to 1.0.0 180515, multilang bugfix 180513, beta release <180513, preview/discussion took place here: https://processwire.com/talk/topic/18983-rocksqlfinder-highly-efficient-and-flexible-sql-finder-module/