3 namespace Org\Mxchange\CoreFramework\Helper\Captcha;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate;
11 * A solveable graphical code CAPTCHA
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 <<<<<<< HEAD:framework/main/classes/helper/captcha/web/class_GraphicalCodeCaptcha.php
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
19 >>>>>>> Some updates::inc/main/classes/helper/captcha/web/class_GraphicalCodeCaptcha.php
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
38 * Hash of the CAPTCHA string
40 private $hashedString = '';
45 private $encryptedString = '';
48 * Protected constructor
52 protected function __construct () {
53 // Call parent constructor
54 parent::__construct(__CLASS__);
58 * Creates an instance of this captcha class
60 * @param $helperInstance An instance of a helper class
61 * @param $extraInstance An extra instance, just for better hash data
62 * @return $captchaInstance An instance of this captcha class
64 public static final function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = NULL) {
66 $captchaInstance = new GraphicalCodeCaptcha();
68 // Set template instance
69 $captchaInstance->setHelperInstance($helperInstance);
72 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
74 // Return the instance
75 return $captchaInstance;
79 * Initiates the CAPTCHA
83 public function initiateCaptcha () {
85 $captchaLength = $this->getConfigInstance()->getConfigEntry('captcha_string_length');
87 // Get max string length
88 $strLength = $this->getConfigInstance()->getConfigEntry('random_string_length');
90 // Calculate starting position based on random place
91 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
96 // Generate a random string for confirmation
97 $randomString = $this->getRngInstance()->randomString($strLength);
99 // Encode the string with BASE64
100 $base64String = base64_encode($randomString);
102 // Make this string a bit more readable for humans
103 $captchaString = substr($base64String, $start, $captchaLength);
105 // Get all characters we want to replace
106 $searchChars = $this->getConfigInstance()->getConfigEntry('captcha_search_chars');
108 // Get fixed salt and use it as "replacement characters"
109 $replaceChars = $this->getRngInstance()->getExtraSalt();
111 // Remove any plus, equals or slashes
112 for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
113 // Get search character
114 $search = substr($searchChars, $searchIdx, 1);
116 // Random array index
117 $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
120 $replace = substr($replaceChars, $charIdx, 1);
123 $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
126 // Get crypto instance
127 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
129 // Hash the CAPTCHA code for later comparison
130 $this->hashedString = $cryptoInstance->hashString($captchaString);
132 // Encrypt the string for later usage
133 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
137 * Render the CAPTCHA code
141 public function renderCode () {
142 // Get helper instance
143 $helperInstance = $this->getHelperInstance();
145 // Get template instance
146 $templateInstance = $helperInstance->getTemplateInstance();
148 // Load a template for this CAPTCHA
149 $templateInstance->loadCodeTemplate('captch_graphic_code');
152 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName() . '_captcha');
153 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName() . '_hash');
154 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName() . '_encrypt');
157 $templateInstance->assignVariable($helperInstance->getFormName() . '_encrypt', urlencode(base64_encode($this->encryptedString)));
158 $templateInstance->assignVariable($helperInstance->getFormName() . '_hash', $this->hashedString);
160 // Compile the template
161 $templateInstance->compileTemplate();
163 // Get the content back
164 $this->addContent($templateInstance->getRawTemplateData());