3 namespace Org\Mxchange\CoreFramework\Helper\Captcha;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate;
12 * A solveable graphical code CAPTCHA
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
35 * Hash of the CAPTCHA string
37 private $hashedString = '';
42 private $encryptedString = '';
45 * Protected constructor
49 private function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
55 * Creates an instance of this captcha class
57 * @param $helperInstance An instance of a helper class
58 * @param $extraInstance An extra instance, just for better hash data
59 * @return $captchaInstance An instance of this captcha class
61 public static final function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = NULL) {
63 $captchaInstance = new GraphicalCodeCaptcha();
65 // Set template instance
66 $captchaInstance->setHelperInstance($helperInstance);
69 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
71 // Return the instance
72 return $captchaInstance;
76 * Initiates the CAPTCHA
80 public function initiateCaptcha () {
82 $captchaLength = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('captcha_string_length');
84 // Get max string length
85 $strLength = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('random_string_length');
87 // Calculate starting position based on random place
88 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
93 // Generate a random string for confirmation
94 $randomString = $this->getRngInstance()->randomString($strLength);
96 // Encode the string with BASE64
97 $base64String = base64_encode($randomString);
99 // Make this string a bit more readable for humans
100 $captchaString = substr($base64String, $start, $captchaLength);
102 // Get all characters we want to replace
103 $searchChars = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('captcha_search_chars');
105 // Get fixed salt and use it as "replacement characters"
106 $replaceChars = $this->getRngInstance()->getExtraSalt();
108 // Remove any plus, equals or slashes
109 for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
110 // Get search character
111 $search = substr($searchChars, $searchIdx, 1);
113 // Random array index
114 $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
117 $replace = substr($replaceChars, $charIdx, 1);
120 $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
123 // Get crypto instance
124 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
126 // Hash the CAPTCHA code for later comparison
127 $this->hashedString = $cryptoInstance->hashString($captchaString);
129 // Encrypt the string for later usage
130 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
134 * Render the CAPTCHA code
138 public function renderCode () {
139 // Get helper instance
140 $helperInstance = $this->getHelperInstance();
142 // Get template instance
143 $templateInstance = $helperInstance->getTemplateInstance();
145 // Load a template for this CAPTCHA
146 $templateInstance->loadCodeTemplate('captch_graphic_code');
149 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName() . '_captcha');
150 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName() . '_hash');
151 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName() . '_encrypt');
154 $templateInstance->assignVariable($helperInstance->getFormName() . '_encrypt', urlencode(base64_encode($this->encryptedString)));
155 $templateInstance->assignVariable($helperInstance->getFormName() . '_hash', $this->hashedString);
157 // Compile the template
158 $templateInstance->compileTemplate();
160 // Get the content back
161 $this->addContent($templateInstance->getRawTemplateData());