[TASK] Add base files 29/37729/2
authorSebastian Fischer <typo3@evoweb.de>
Wed, 11 Mar 2015 14:03:21 +0000 (15:03 +0100)
committerSebastian Fischer <typo3@evoweb.de>
Wed, 11 Mar 2015 14:06:02 +0000 (15:06 +0100)
Change-Id: Ia88fa37ee5312b2053029eb84d274e042ba36c49
Reviewed-on: http://review.typo3.org/37729
Reviewed-by: Sebastian Fischer <typo3@evoweb.de>
Tested-by: Sebastian Fischer <typo3@evoweb.de>
Classes/Services/Captcha/RecaptchaAdapter.php [new file with mode: 0644]
Classes/Services/CaptchaService.php [new file with mode: 0644]
Configuration/TCA/Overrides/tt_content.php [new file with mode: 0644]
Configuration/TypoScript/constants.txt [new file with mode: 0644]
Configuration/TypoScript/setup.txt [new file with mode: 0644]
ext_emconf.php [new file with mode: 0644]
ext_icon.png [new file with mode: 0644]
locallang.xml [new file with mode: 0644]

diff --git a/Classes/Services/Captcha/RecaptchaAdapter.php b/Classes/Services/Captcha/RecaptchaAdapter.php
new file mode 100644 (file)
index 0000000..27750ea
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+namespace Evoweb\Recaptcha\Services\Captcha;
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2015 Sebastian Fischer <typo3@evoweb.de>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Class RecaptchaAdapter
+ */
+class RecaptchaAdapter extends \Evoweb\SfRegister\Services\Captcha\AbstractAdapter {
+       /**
+        * Object manager
+        *
+        * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
+        * @inject
+        */
+       protected $objectManager;
+
+       /**
+        * Captcha object
+        *
+        * @var \Evoweb\Recaptcha\Services\CaptchaService
+        */
+       protected $captcha = NULL;
+
+       /**
+        * Constructor
+        */
+       public function __construct() {
+               $this->captcha = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Evoweb\\Recaptcha\\Services\\CaptchaService');
+       }
+
+       /**
+        * Rendering the output of the captcha
+        *
+        * @return string
+        */
+       public function render() {
+               $this->objectManager->get('Evoweb\\SfRegister\\Services\\Session')->remove('captchaWasValidPreviously');
+
+               if ($this->captcha !== NULL) {
+                       $output = $this->captcha->getReCaptcha();
+               } else {
+                       $output = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
+                               'error_captcha.notinstalled', 'Recaptcha', array('recaptcha')
+                       );
+               }
+
+               return $output;
+       }
+
+       /**
+        * Validate the captcha value from the request and output an error if not valid
+        *
+        * @param string $value
+        * @return bool
+        */
+       public function isValid($value) {
+               $validCaptcha = TRUE;
+
+               $session = $this->objectManager->get('Evoweb\\SfRegister\\Services\\Session');
+               $captchaWasValidPreviously = $session->get('captchaWasValidPreviously');
+               if ($this->captcha !== NULL && $captchaWasValidPreviously !== TRUE) {
+                       $status = $this->captcha->validateReCaptcha();
+
+                       if ($status == FALSE || $status['error'] !== '') {
+                               $validCaptcha = FALSE;
+                               $this->addError(
+                                       \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('error_recaptcha_' . $status['error'], 'Recaptcha'),
+                                       1307421960
+                               );
+                       }
+               }
+
+               $session->set('captchaWasValidPreviously', $validCaptcha);
+
+               return $validCaptcha;
+       }
+}
diff --git a/Classes/Services/CaptchaService.php b/Classes/Services/CaptchaService.php
new file mode 100644 (file)
index 0000000..1faab2a
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+namespace Evoweb\Recaptcha\Services;
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2015 Sebastian Fischer <typo3@evoweb.de>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Class RecaptchaService
+ */
+class CaptchaService {
+
+       /**
+        * @var array
+        */
+       protected $configuration = array();
+
+       /**
+        * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
+        */
+       protected $contentObject;
+
+       /**
+        * @return self
+        * @throws \Exception
+        */
+       public function __construct() {
+               /**
+                * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $frontend
+                */
+               $frontend = $GLOBALS['TSFE'];
+               $this->configuration = $frontend->tmpl->setup['plugin.']['tx_recaptcha.'];
+
+               if (!is_array($this->configuration) || empty($this->configuration)) {
+                       throw new \Exception('Please configure plugin.tx_recaptcha. before rendering the recaptcha', 1417680291);
+               }
+
+               $this->contentObject = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
+       }
+
+       /**
+        * Build reCAPTCHA Frontend HTML-Code
+        *
+        * @return string reCAPTCHA HTML-Code
+        */
+       public function getReCaptcha() {
+               return $this->contentObject->stdWrap($this->configuration['public_key'], $this->configuration['public_key.']);
+       }
+
+       /**
+        * Validate reCAPTCHA challenge/response
+        *
+        * @return array Array with verified- (boolean) and error-code (string)
+        */
+       public function validateReCaptcha() {
+               $request = array(
+                       'secret' => $this->configuration['private_key'],
+                       'response' => trim(GeneralUtility::_GP('g-recaptcha-response')),
+                       'remoteip' => GeneralUtility::getIndpEnv('REMOTE_ADDR'),
+               );
+
+               $result = array('verified' => FALSE, 'error' => '');
+               if (empty($data['response'])) {
+                       $result['error'] = 'Recaptcha response missing';
+               } else {
+                       $response = $this->queryVerificationServer($request);
+                       if (!$response) {
+                               $result['error'] = 'Verification server did not responde';
+                       }
+
+                       if ($result['success']) {
+                               $result['verified'] = TRUE;
+                       } else {
+                               $result['error'] = $result['error-codes'];
+                       }
+               }
+
+               return $result;
+       }
+
+       /**
+        * Query reCAPTCHA server for captcha-verification
+        *
+        * @param array $data
+        * @return array Array with verified- (boolean) and error-code (string)
+        */
+       protected function queryVerificationServer($data) {
+               $verifyServerInfo = @parse_url($this->configuration['verify_server']);
+
+               if (empty($verifyServerInfo)) {
+                       return array(FALSE, 'recaptcha-not-reachable');
+               }
+
+               $request = GeneralUtility::implodeArrayForUrl('', $data);
+               $response = GeneralUtility::getURL($this->configuration['verify_server'] . '?' . $request);
+
+               return json_decode($response);
+       }
+}
\ No newline at end of file
diff --git a/Configuration/TCA/Overrides/tt_content.php b/Configuration/TCA/Overrides/tt_content.php
new file mode 100644 (file)
index 0000000..ac5ebfa
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+defined('TYPO3_MODE') or die('Access denied.');
+
+// Add Default TS to Include static (from extensions)
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile(
+       'recaptcha', 'Configuration/TypoScript/', 'reCAPTCHA'
+);
diff --git a/Configuration/TypoScript/constants.txt b/Configuration/TypoScript/constants.txt
new file mode 100644 (file)
index 0000000..3dd478d
--- /dev/null
@@ -0,0 +1,22 @@
+plugin.tx_recaptcha {
+       # cat=plugin.recaptcha//01; type=string; label= reCAPTCHA public key: Public key of your reCAPTCHA-account
+       public_key = 6LdcuP4SAAAAAMPd3ETvHbvRG45odEPc_DkCRaTt
+
+       # cat=plugin.recaptcha//02; type=string; label= reCAPTCHA private key: Private key of your reCAPTCHA-account
+       private_key = 6LdcuP4SAAAAAMDK27S56123NMJKwc8KWeMnaTjj
+
+       # cat=plugin.recaptcha//03; type=string; label= reCAPTCHA API-server address
+       api_server = https://www.google.com/recaptcha/api.js
+
+       # cat=plugin.recaptcha//05; type=string; label= reCAPTCHA VERIFY-server address
+       verify_server = https://www.google.com/recaptcha/api/siteverify
+
+       # cat=plugin.recaptcha//08; type=int+; label= reCAPTCHA TabIndex: TabIndex of reCAPTCHA field
+       tabindex = 0
+
+       # cat=plugin.recaptcha//09; type=string; label= reCAPTCHA language: Language of reCAPTCHA frontend (autodetected if empty)
+       lang =
+}
+
+page.includeJSFooterlibs.recaptcha < plugin.tx_recaptcha.api_server
+page.includeJSFooterlibs.recaptcha.external = 1
\ No newline at end of file
diff --git a/Configuration/TypoScript/setup.txt b/Configuration/TypoScript/setup.txt
new file mode 100644 (file)
index 0000000..866eadb
--- /dev/null
@@ -0,0 +1,17 @@
+plugin.tx_recaptcha {
+       public_key = {$plugin.tx_recaptcha.public_key}
+       public_key.wrap = <div class="g-recaptcha" data-sitekey="|"></div>
+
+       private_key = {$plugin.tx_recaptcha.private_key}
+
+       api_server = {$plugin.tx_recaptcha.api_server}
+       verify_server = {$plugin.tx_recaptcha.verify_server}
+
+       tabindex = {$plugin.tx_recaptcha.tabindex}
+       lang = {$plugin.tx_recaptcha.lang}
+
+}
+
+
+// register recaptcha as captcha for sf_register
+plugin.tx_sfregister.settings.captcha.recaptcha = Evoweb\Recaptcha\Services\Captcha\RecaptchaAdapter
diff --git a/ext_emconf.php b/ext_emconf.php
new file mode 100644 (file)
index 0000000..91da8dd
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+$EM_CONF[$_EXTKEY] = array(
+       'title' => 'reCAPTCHA',
+       'description' => 'Easy on Humans, Hard on Bots',
+       'category' => 'fe',
+       'state' => 'stable',
+       'uploadfolder' => 0,
+       'createDirs' => '',
+       'clearcacheonload' => 0,
+       'author' => 'Sebastian Fischer',
+       'author_email' => 'typo3@evoweb.de',
+       'author_company' => 'evoweb',
+       'version' => '1.0.0',
+       '_md5_values_when_last_written' => '',
+       'constraints' => array(
+               'depends' => array(
+                       'typo3' => '6.2.0-7.99.99',
+               ),
+               'conflicts' => array(
+               ),
+               'suggests' => array(
+               ),
+       ),
+);
diff --git a/ext_icon.png b/ext_icon.png
new file mode 100644 (file)
index 0000000..20ae110
Binary files /dev/null and b/ext_icon.png differ
diff --git a/locallang.xml b/locallang.xml
new file mode 100644 (file)
index 0000000..058d071
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
+<T3locallang>
+       <meta type="array">
+               <type>database</type>
+               <description>Language labels for database tables/fields belonging to extension 'schattauer_framework'</description>
+       </meta>
+       <data type="array">
+               <languageKey index="default" type="array">
+                       <label index="enter_code_here">Please enter the code here:</label>
+               </languageKey>
+               <languageKey index="de" type="array">
+                       <label index="enter_code_here">Bitte hier den Code reinkopieren:</label>
+               </languageKey>
+       </data>
+</T3locallang>
\ No newline at end of file