Joomla 3 reCaptcha validation - joomla

I have a simple custom form in Joomla 3.6 that I have added reCaptcha to successfully. However, I am struggling with validating this.
After a few web searches, I can up with the following code:
$joomla_captcha = JFactory::getConfig()->get('captcha');
if ( $joomla_captcha != '0') {
$jpost = JFactory::getApplication()->input->post;
$reCaptcha = $jpost->get("g-recaptcha-response");
$dispatcher = JEventDispatcher::getInstance();
$captcha_response = $dispatcher->trigger('onCheckAnswer', $reCaptcha);
}
if ( ! $captcha_response[0] ) {
die("Invalid Captcha");
}
However, the form is passing whether the captcha is done or not.
What changes do I need to make to pick up whether the captcha was passed or not?
The form is really basic and I am loathe to install yet another component just for this validation.

Okay, it appears I hit a brick wall in trying to find a "Joomla" way to do this that calls on inbuilt API methods.
Since I am only using Google reCaptcha, I found on Google's site that the "g-recaptcha-response" field is empty if the captcha challenge has not been completed and not empty if correctly completed.
So, for Google reCaptcha, I need to test the "g-recaptcha-response" field and my code example becomes:
$joomla_captcha = JFactory::getConfig()->get('captcha');
if ( $joomla_captcha != '0') {
$jpost = JFactory::getApplication()->input->post;
$reCaptcha = $jpost->get("g-recaptcha-response");
}
if ( isset( $reCaptcha ) && empty( $reCaptcha ) {
die("Invalid Captcha");
}
This is obviously limited to Google reCaptcha and would have been nice to query the Joomla API Layer instead to allow flexibility but good enough.
Edit
The following Joomla API calls will return "true" or "false" into $completed indicating whether the captcha passed or not
$config = JFactory::getConfig()->get('captcha');
$captcha = JCaptcha::getInstance($config);
$completed = $captcha->CheckAnswer();
if ($completed === false) {
die("Invalid Captcha");
}
Preferable to the earlier approach as will be able to work with other captcha plugins that may be added to Joomla.

Related

WordPress ajax request not translated with Polylang

I've had problem with ajax requests that my requests translating __() functions didn't work.
Googled it from everywhere.
Everywhere was as an answer that url parameter in the following form ?lang=fi should be added for the requests.
It turned out that if you are using Polylang your ajax requests url must in format /fi. Note for the default language this shouldn't be defined at all.
Solved the problem with this change.
I hope this helps someone.
I know am late to the party but I couldn't find any answers anywhere that will solve this problem and Libla's answer it's just right.
Just a little context:
I have a multilingual Woocommerce website and all translations were working great except for the checkout "order review" that is loading with Ajax.
This part after Ajax is showing in the website language (the one you have set up in settings).
All of that is because the Ajax call ignores the "?lang=" parameter (probably just the Polylang plugin, I never checked with other translation plugins).
Instead, Ajax should contain the language parameter (like in the browser www.yourwebsite.com/fi).
To solve that, I've added a filter (in functions.php)
/** Fix ajax handler translation */
add_filter( 'woocommerce_get_script_data', 'ajax_handler_fix_translation' );
if ( ! function_exists( 'ajax_handler_fix_translation' ) ) {
function ajax_handler_fix_translation( $params ) {
/** Get the current language */
$locale = determine_locale();
/** Take just the first part of the $locale */
$lang = ( ! empty( $locale ) ) ? strstr( $locale, '_', true ) : '';
if ( empty( $lang ) ) {
/** If there is no $lang parameter, just return to standard */
return $params;
}
if ( isset( $params['wc_ajax_url'] ) ) {
$params['wc_ajax_url'] = '/'.$lang.$params['wc_ajax_url'];
}
return $params;
}
}

How can I implement in TYPO 3 server side validation?

I am using TYPO 3 version 6.2.14. In this version I am using Formhandler Plugin to generate a contact form. I have implemented custom code of Google
reCAPTCHA V2 Explicitly render. Now reCAPTCHA code is generated. But it is not performing server side validation. I have also created php file for serverside validation inside fileadmin/templates/fromhandler/serversidevalidation.php
if(isset($_POST['g-recaptcha-response']))
{
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
echo $responseKeys;
if(intval($responseKeys["success"]) !== 1) {
echo '<h2>You are spammer ! Get the bot out</h2>';
} else {
echo '<h2>Thanks for posting </h2>';
}
But this simple php file is not including in TypoScript. How can I implement server side validation with formhandler extension. I do not want to use extension because of compatibility issue. Can someone guide me?

Set AuthComponent when doing a manual login with AJAX

Using CakePHP 2.0, when logging in the normal way, a helpful set of cookies is set and accessible via AuthComponent::user(). However, this does not get set when doing it the AJAX way. The verification works fine, but I would like to figure out how to set AuthComponent without a hard refresh.
Maybe I could do without AuthComponent and just store cookies, but I wanted to check to see if there's an easy way to do this before doing all of that work.
I've checked the JsHelper and Authentication pages in the CakePHP 2.0 documentation.
Any ideas?
Why dont you create a function in the user around the lines of:
public function autologin() {
$this->autoRender = false;
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$cuser = $this->Auth->user();
$this->Session->write('Udata', $udata);
$fD = array('loggedIn'=>true,'vdata'=>$udata);
} else {
$fD = array('loggedIn'=>false,'vdata'=>'Your username/password combination was incorrect');
}
echo json_encode($fD);
}
}
and call this page with your ajax. with the JSON run some check;

One list, two different thank you pages with Mailchimp

Mailchimp ties each form to one list.
I'd like to have a signup form on Page1.html that sends users to Page1ty.html and another form on Page2.html that sends users to Page2ty.html. But both forms need to feed users into the same list. As stated above, this isn't possible using their basic forms. I would need two list.
Mailchimp says this kind of routing might be possible using their API. Does any one know how to go about accomplishing the above kind of signups?
You would just create custom forms and tie into the MailChimp API, but as of their latest update you'll need to make sure you have administrator privileges.
You include (require) the MCAPI.class.php and config.inc.php files from their API downloads, and then write your process (I use PHP).
Once you have downloaded the files and set up your 'config.inc.php` file with the proper credentials, (API Key and list ID) you're ready to go.
Here's a sample in PHP that subscribes a user to a list, but you'll have to read the API docs to get the exact functionality you're looking for.
<?php
session_start();
// --- Sample fields - depends on your list
$mailChimpTIME = date('Y-m-d H:i:s');
$mailChimpFirstName = // First Name
$mailChimpLastName = // Last Name
$mailChimpEmailAddress = // Email Address
require_once 'MCAPI.class.php';
require_once 'config.inc.php'; //contains apikey
$api = new MCAPI($apikey);
$merge_vars = array(
'FNAME'=>$mailChimpFirstName,
'LNAME'=>$mailChimpLastName,
'EMAIL'=>$mailChimpEmailAddress,
'OPTIN_IP'=>$_SERVER['REMOTE_ADDR'],
'OPTIN_TIME'=>$mailChimpTIME
);
$email_type = 'html';
$double_optin = true;
$update_existing = true;
$replace_interests = false;
// By default this sends a confirmation email - you will not see new members
// until the link contained in it is clicked!
$retval = $api->listSubscribe( $listId, $mailChimpEmailAddress, $merge_vars, $email_type, $double_optin, $update_existing, $replace_interests);
if ($api->errorCode){
echo "Unable to load listSubscribe()!\n";
echo "\tCode=".$api->errorCode."\n";
echo "\tMsg=".$api->errorMessage."\n";
} else {
// Success
//echo "Subscribed - look for the confirmation email!\n";
}
?>

Codeigniter - best routes configuration for CMS?

I would like to create a custom CMS within Codeigniter, and I need a mechanism to route general pages to a default controller - for instance:
mydomain.com/about
mydomain.com/services/maintenance
These would be routed through my pagehandler controller. The default routing behaviour in Codeigniter is of course to route to a matching controller and method, so with the above examples it would require an About controller and a Services controller. This is obviously not a practical or even sensible approach.
I've seen the following solution to place in routes.php:
$route['^(?!admin|products).*'] = "pagehandler/$0";
But this poses it's own problems I believe. For example, it simply looks for "products" in the request uri and if found routes to the Products controller - but what if we have services/products as a CMS page? Does this not then get routed to the products controller?
Is there a perfect approach to this? I don't wish to have a routing where all CMS content is prefixed with the controller name, but I also need to be able to generically override the routing for other controllers.
If you use CodeIgniter 2.0 (which has been stable enough to use for months) then you can use:
$route['404_override'] = 'pages';
This will send anything that isn't a controller, method or valid route to your pages controller. Then you can use whatever PHP you like to either show the page or show a much nicer 404 page.
Read me guide explaining how you upgrade to CodeIgniter 2.0. Also, you might be interested in using an existing CMS such as PyroCMS which is now nearing the final v1.0 and has a massive following.
You are in luck. I am developing a CMS myself and it took me ages to find a viable solution to this. Let me explain myself to make sure that we are on the same page here, but I am fairly certain that we area.
Your URLS can be formatted the following ways:
http://www.mydomain.com/about - a top level page with no category
http://www.mydomain.com/services/maintenance - a page with a parent category
http://www.mydomain.com/services/maintenace/server-maintenance - a page with a category and sub category.
In my pages controller I am using the _remap function that basically captures all requests to your controllers and lets you do what you want with them.
Here is my code, commented for your convenience:
<?php
class Pages extends Controller {
// Captures all calls to this controller
public function _remap()
{
// Get out URL segments
$segments = $this->uri->uri_string();
$segments = explode("/", $segments);
// Remove blank segments from array
foreach($segments as $key => $value) {
if($value == "" || $value == "NULL") {
unset($segments[$key]);
}
}
// Store our newly filtered array segments
$segments = array_values($segments);
// Works out what segments we have
switch (count($segments))
{
// We have a category/subcategory/page-name
case 3:
list($cat, $subcat, $page_name) = $segments;
break;
// We have a category/page-name
case 2:
list($cat, $page_name) = $segments;
$subcat = NULL;
break;
// We just have a page name, no categories. So /page-name
default:
list($page_name) = $segments;
$cat = $subcat = NULL;
break;
}
if ($cat == '' && $subcat == '') {
$page = $this->mpages->fetch_page('', '', $page_name);
} else if ($cat != '' && $subcat == '') {
$page = $this->mpages->fetch_page($cat, '', $page_name);
} else if ($category != "" && $sub_category != "") {
$page = $this->mpages->fetch_page($cat, $subcat, $page_name);
}
// $page contains your page data, do with it what you wish.
}
?>
You of course would need to modify your page fetching model function accept 3 parameters and then pass in info depending on what page type you are viewing.
In your application/config/routes.php file simply put what specific URL's you would like to route and at the very bottom put this:
/* Admin routes, login routes etc here first */
$route['(:any)'] = "pages"; // Redirect all requests except for ones defined above to the pages controller.
Let me know if you need any more clarification or downloadable example code.

Resources