- <?php
- /***************************************************************
- * Copyright notice
- *
- * (c) 2008 Alex Kellner <alexander.kellner@einpraegsam.net>
- * 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 tx_wtcalculatingcaptcha {
- public $extKey = 'wt_calculating_captcha'; // Extension Key
- public $captchaimage = 'EXT:wt_calculating_captcha/captcha.png'; // Path to captcha image
- private $configuration = array();
- private $img = '';
- private $number1 = '';
- private $number2 = '';
- private $subfolder = '';
- /**
- * Function generateCaptcha() generate a new captcha image and save the result to the session
- *
- * @return string HTML code for captcha image
- */
- function generateCaptcha() {
- // config
- $this->conf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_wtcalculatingcaptcha.']; // Typoscript configuration
- $this->subfolder = '';
- if (t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '/' != t3lib_div::getIndpEnv('TYPO3_SITE_URL')) { // if request_host is different to site_url (TYPO3 runs in a subfolder)
- $this->subfolder = str_replace(t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '/', '', t3lib_div::getIndpEnv('TYPO3_SITE_URL')); // get the folder (like "subfolder/")
- }
- $this->startimage = t3lib_div::getIndpEnv('TYPO3_DOCUMENT_ROOT') . '/' . $this->subfolder . $GLOBALS['TSFE']->tmpl->getFileName($this->conf['background.']['image']); // background image
- if ($this->conf['debug']) t3lib_div::debug($this->conf, $this->extKey . ': Typoscript configuration'); // debug output
- // 1. Get random numbers
- $this->no1 = t3lib_div::trimExplode(',', $this->conf['number1.']['minmax'], 1); // give me the min and the max of number 1
- $this->no2 = t3lib_div::trimExplode(',', $this->conf['number2.']['minmax'], 1); // give me the min and the max of number 2
- $op = mt_rand(0, $this->conf['operation.']['number']); // operator (should be 0 or 1 or 2 or 3)
- for ($i=0; $i<100; $i++) { // loop max. 100 times
- $this->number1 = mt_rand($this->no1[0], $this->no1[1]); // random number 1
- $this->number2 = mt_rand($this->no2[0], $this->no2[1]); // random number 2
- if ($op != 1 || $this->number1 > $this->number2 || $this->conf['number.']['notNegative'] != 1) break; // stop loop IF operator is not "-" OR if first is greater than second number OR if plugin don't care about negative numbers (function to disable negative numbers)
- }
- switch ($op) { // give me the operator
- case 0:
- $this->operator = '+'; // operator
- $this->result = $this->number1 + $this->number2; // result
- break;
- case 1:
- $this->operator = '-';
- $this->result = $this->number1 - $this->number2; // result
- break;
- case 2:
- $this->operator = 'x';
- $this->result = $this->number1 * $this->number2; // result
- break;
- case 3:
- $this->operator = ':';
- $this->result = $this->number1 / $this->number2; // result
- break;
- }
- $this->content = $this->number1 . ' ' . $this->operator . ' ' . $this->number2;
- // 2. Save result to session
- $GLOBALS['TSFE']->fe_user->setKey('ses', $this->extKey . '_value', $this->result); // Generate Session with result
- $GLOBALS['TSFE']->storeSessionData(); // Save session
- // 3. generate captcha
- if (is_file($this->startimage)) { // if file is correct
- $this->configuration['color_rgb'] = sscanf($this->conf['font.']['color'], '#%2x%2x%2x'); // change HEX color to RGB
- $this->img = ImageCreateFromPNG($this->startimage); // Backgroundimage
- $this->configuration['color'] = ImageColorAllocate($this->img, $this->configuration['color_rgb'][0], $this->configuration['color_rgb'][1], $this->configuration['color_rgb'][2]); // Font color
- $this->configuration['font'] = t3lib_div::getIndpEnv('TYPO3_DOCUMENT_ROOT') . '/' . $this->subfolder . $GLOBALS['TSFE']->tmpl->getFileName($this->conf['font.']['path']); // fontfile
- $this->configuration['fontsize'] = $this->conf['font.']['size']; // Fontsize
- $this->configuration['angle'] = t3lib_div::trimExplode(',', $this->conf['font.']['angle'], 1); // give me the angles for the font
- $this->configuration['fontangle'] = mt_rand($this->configuration['angle'][0], $this->configuration['angle'][1]); // random angle
- $this->configuration['distance_hor'] = t3lib_div::trimExplode(',', $this->conf['font.']['distance_hor'], 1); // give me the horizontal distances
- $this->configuration['fontdistance_hor'] = mt_rand($this->configuration['distance_hor'][0], $this->configuration['distance_hor'][1]); // random distance
- $this->configuration['distance_vert'] = t3lib_div::trimExplode(',', $this->conf['font.']['distance_vert'], 1); // give me the vertical distances
- $this->configuration['fontdistance_vert'] = mt_rand($this->configuration['distance_vert'][0], $this->configuration['distance_vert'][1]); // random distance
- if ($this->conf['debug']) t3lib_div::debug($this->configuration, $this->extKey . ': Image configuration'); // debug output
- imagettftext($this->img, $this->configuration['fontsize'], $this->configuration['fontangle'], $this->configuration['fontdistance_hor'], $this->configuration['fontdistance_vert'], $this->configuration['color'], $this->configuration['font'], $this->content); // add text to image
- imagepng($this->img, t3lib_div::getIndpEnv('TYPO3_DOCUMENT_ROOT') . '/' . $this->subfolder . $GLOBALS['TSFE']->tmpl->getFileName($this->captchaimage)); // save image file
- imagedestroy($this->img); // delete temp image
- // 4. return
- if ($this->conf['debug']) t3lib_div::debug($this->content, $this->extKey . ': Image content'); // debug output
- if ($this->conf['debug']) t3lib_div::debug($this->result, $this->extKey . ': Result'); // debug output
- return '<img src="' . $GLOBALS['TSFE']->tmpl->getFileName($this->captchaimage) . '?hash=' . time() . '" alt="captcha" class="wtcalculatingcaptcha" id="wtcalculatingcaptcha" />';
- } else { // Path to the background image is wrong
- if (!$this->conf['debug']) return '<strong>wt_calculating_captcha ERROR:</strong> Please check the path to the background image file: "' . $GLOBALS['TSFE']->tmpl->getFileName($this->conf['background.']['image']) . '"';
- else return '<strong>wt_calculating_captcha ERROR:</strong> Path to image is wrong: ' . $this->startimage;
- }
- }
- /**
- * Function correctCode() returns true or false if the given code is right
- *
- * @param string $code: captcha code from input field
- * @return boolean True/False for correct or incorrect code
- */
- function correctCode($code) {
- if (!empty($GLOBALS['TSFE']->fe_user->sesData[$this->extKey . '_value'])) { // if there is a value in session
- // config
- $this->conf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_wtcalculatingcaptcha.']; // Typoscript configuration
- // let's go
- if (intval($code) == $GLOBALS['TSFE']->fe_user->sesData[$this->extKey . '_value'] && !empty($code)) { // if code is set and equal to session value
- if ($this->conf['debug']) t3lib_div::debug('Yes, captcha code is correct!', $this->extKey . ': captcha code'); // debug output
- return true; // correct code
- } else {
- if ($this->conf['debug']) t3lib_div::debug('No, captcha code is NOT correct!', $this->extKey . ': captcha code'); // debug output
- return false; // wrong code
- }
- } return false; // false per default
- }
- }
- if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/wt_calculating_captcha/class.tx_wtcalculatingcaptcha.php']) {
- include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/wt_calculating_captcha/class.tx_wtcalculatingcaptcha.php']);
- }