CMS  Version 3.9
recaptcha_v2_field_renderer.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9 Copyright (c) 2018 Sonjara, Inc
10 
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use,
15 copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the
17 Software is furnished to do so, subject to the following
18 conditions:
19 
20 The above copyright notice and this permission notice shall be
21 included in all copies or substantial portions of the Software.
22 
23 Except as contained in this notice, the name(s) of the above
24 copyright holders shall not be used in advertising or otherwise
25 to promote the sale, use or other dealings in this Software
26 without prior written authorization.
27 
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::usingFeature("field_renderers", "validation");
40 Fakoli::using("settings");
41 
42 
43 class RecaptchaV2Validator extends AbstractValidator
44 {
45  var $message;
47 
53  function __construct()
54  {
55  $this->message = "The code you entered did not match. Please try again.";
56  $this->compareField = "recaptcha_response_field";
57  $this->title = "Captcha";
58  // Pass the field name hello captcha creators give to the user input to compare against the test
59  $this->AbstractValidator("recaptcha_user_answer", "Captcha");
60  }
61 
62  function writeClient()
63  {
64  // No client-side component
65 
66  return "";
67  }
68 
69  function validate()
70  {
71  if ($this->readOnly) return "";
72 
73  $secret_key = Settings::getValue("captcha", "recaptcha_secret_key");
74 
75  if (!isset($_POST["g-recaptcha-response"]) || $_POST["g-recaptcha-response"] === $this->empty)
76  {
77  return "{$this->title} is a required field.";
78  }
79 
80  $data = array( "secret" => $secret_key,
81  "response" => $_POST["g-recaptcha-response"],
82  "remoteip" => $_SERVER["REMOTE_ADDR"]);
84  $result = json_decode($response);
85  if ($result->success == false)
86  {
87  return "CAPTCHA failed";
88  }
89 
90  return "";
91  }
92 }
93 
94 
95 class RecaptchaV2FieldRenderer extends FieldRenderer
96 {
97  function __construct(&$form)
98  {
99  parent::__construct($form);
100 
101  $this->field = "captcha";
102  $form->add($this);
103 
104  $site_key = Settings::getValue("captcha", "recaptcha_site_key");
105  $secret_key = Settings::getValue("captcha", "recaptcha_secret_key");
106 
107  if(!$site_key || !$secret_key)
108  {
109  throw new FakoliException("reCAPTCHA v2 keys must be provided in CMS Application Settings.");
110  }
111 
112  $this->hideLabel = true;
113  $this->colspan = 2;
114  $form->validator->add(new RecaptchaV2Validator());
115  }
116 
118  {
119 ?>
120  <script src='https://www.google.com/recaptcha/api.js'></script>
121 <?
122  }
123 
124  function renderField($field = "")
125  {
126  $site_key = Settings::getValue("captcha", "recaptcha_site_key");
127 
128  if (!$field) $field = $this->field;
129 
130  $this->_startField($field);
131 ?>
132  <div class='captcha_wrapper'>
133  <div class="g-recaptcha" data-sitekey="<?echo $site_key?>"></div>
134  </div>
135 <?
136  $this->_endField($field);
137  }
138 }
139 ?>
$form
$_POST["owner_id"]
Definition: blog_form.inc:54
$bookmark title
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
__construct()
We do not receive parameters for this validator because the field names for retrieving values in $_PO...
$compareField
The field where the turing test id will be set in $_POST; controlled by hello captcha creator.
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
$topicList colspan
Definition: group_form.inc:54
$result
$openData field
if(! $user) if(! $response_id) $response
$typeSelect hideLabel
$email message