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\Crypto\CryptoHelper;
10 use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate;
13 * A solveable graphical code CAPTCHA
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
36 * Hash of the CAPTCHA string
38 private $hashedString = '';
43 private $encryptedString = '';
46 * Protected constructor
50 private function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of this captcha class
58 * @param $helperInstance An instance of a helper class
59 * @param $extraInstance An extra instance, just for better hash data
60 * @return $captchaInstance An instance of this captcha class
62 public static final function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = NULL) {
64 $captchaInstance = new GraphicalCodeCaptcha();
66 // Set template instance
67 $captchaInstance->setHelperInstance($helperInstance);
70 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
72 // Return the instance
73 return $captchaInstance;
77 * Initiates the CAPTCHA
81 public function initiateCaptcha () {
83 $captchaLength = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('captcha_string_length');
85 // Get max string length
86 $strLength = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('random_string_length');
88 // Calculate starting position based on random place
89 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
94 // Generate a random string for confirmation
95 $randomString = $this->getRngInstance()->randomString($strLength);
97 // Encode the string with BASE64
98 $base64String = base64_encode($randomString);
100 // Make this string a bit more readable for humans
101 $captchaString = substr($base64String, $start, $captchaLength);
103 // Get all characters we want to replace
104 $searchChars = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('captcha_search_chars');
106 // Get fixed salt and use it as "replacement characters"
107 $replaceChars = $this->getRngInstance()->getExtraSalt();
109 // Remove any plus, equals or slashes
110 for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
111 // Get search character
112 $search = substr($searchChars, $searchIdx, 1);
114 // Random array index
115 $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
118 $replace = substr($replaceChars, $charIdx, 1);
121 $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
124 // Get crypto instance
125 $cryptoInstance = CryptoHelper::getSelfInstance();
127 // Hash the CAPTCHA code for later comparison
128 $this->hashedString = $cryptoInstance->hashString($captchaString);
130 // Encrypt the string for later usage
131 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
135 * Render the CAPTCHA code
139 public function renderCode () {
140 // Get helper instance
141 $helperInstance = $this->getHelperInstance();
143 // Get template instance
144 $templateInstance = $helperInstance->getTemplateInstance();
146 // Load a template for this CAPTCHA
147 $templateInstance->loadCodeTemplate('captch_graphic_code');
150 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName() . '_captcha');
151 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName() . '_hash');
152 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName() . '_encrypt');
155 $templateInstance->assignVariable($helperInstance->getFormName() . '_encrypt', urlencode(base64_encode($this->encryptedString)));
156 $templateInstance->assignVariable($helperInstance->getFormName() . '_hash', $this->hashedString);
158 // Compile the template
159 $templateInstance->compileTemplate();
161 // Get the content back
162 $this->addContent($templateInstance->getRawTemplateData());