3 namespace CoreFramework\Captcha;
5 // Load framework stuff
6 use CoreFramework\Generic\FrameworkInterface;
9 * A solveable graphical code CAPTCHA
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
32 * Hash of the CAPTCHA string
34 private $hashedString = '';
39 private $encryptedString = '';
42 * Protected constructor
46 protected function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Creates an instance of this captcha class
54 * @param $helperInstance An instance of a helper class
55 * @param $extraInstance An extra instance, just for better hash data
56 * @return $captchaInstance An instance of this captcha class
58 public static final function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = NULL) {
60 $captchaInstance = new GraphicalCodeCaptcha();
62 // Set template instance
63 $captchaInstance->setHelperInstance($helperInstance);
66 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
68 // Return the instance
69 return $captchaInstance;
73 * Initiates the CAPTCHA
77 public function initiateCaptcha () {
79 $captchaLength = $this->getConfigInstance()->getConfigEntry('captcha_string_length');
81 // Get max string length
82 $strLength = $this->getConfigInstance()->getConfigEntry('random_string_length');
84 // Calculate starting position based on random place
85 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
90 // Generate a random string for confirmation
91 $randomString = $this->getRngInstance()->randomString($strLength);
93 // Encode the string with BASE64
94 $base64String = base64_encode($randomString);
96 // Make this string a bit more readable for humans
97 $captchaString = substr($base64String, $start, $captchaLength);
99 // Get all characters we want to replace
100 $searchChars = $this->getConfigInstance()->getConfigEntry('captcha_search_chars');
102 // Get fixed salt and use it as "replacement characters"
103 $replaceChars = $this->getRngInstance()->getExtraSalt();
105 // Remove any plus, equals or slashes
106 for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
107 // Get search character
108 $search = substr($searchChars, $searchIdx, 1);
110 // Random array index
111 $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
114 $replace = substr($replaceChars, $charIdx, 1);
117 $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
120 // Get crypto instance
121 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
123 // Hash the CAPTCHA code for later comparison
124 $this->hashedString = $cryptoInstance->hashString($captchaString);
126 // Encrypt the string for later usage
127 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
131 * Render the CAPTCHA code
135 public function renderCode () {
136 // Get helper instance
137 $helperInstance = $this->getHelperInstance();
139 // Get template instance
140 $templateInstance = $helperInstance->getTemplateInstance();
142 // Load a template for this CAPTCHA
143 $templateInstance->loadCodeTemplate('captch_graphic_code');
146 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName() . '_captcha');
147 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName() . '_hash');
148 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName() . '_encrypt');
151 $templateInstance->assignVariable($helperInstance->getFormName() . '_encrypt', urlencode(base64_encode($this->encryptedString)));
152 $templateInstance->assignVariable($helperInstance->getFormName() . '_hash', $this->hashedString);
154 // Compile the template
155 $templateInstance->compileTemplate();
157 // Get the content back
158 $this->addContent($templateInstance->getRawTemplateData());