3 namespace CoreFramework\Captcha;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\FrameworkInterface;
10 * A solveable graphical code CAPTCHA
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
33 * Hash of the CAPTCHA string
35 private $hashedString = '';
40 private $encryptedString = '';
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this captcha class
55 * @param $helperInstance An instance of a helper class
56 * @param $extraInstance An extra instance, just for better hash data
57 * @return $captchaInstance An instance of this captcha class
59 public static final function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = NULL) {
61 $captchaInstance = new GraphicalCodeCaptcha();
63 // Set template instance
64 $captchaInstance->setHelperInstance($helperInstance);
67 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
69 // Return the instance
70 return $captchaInstance;
74 * Initiates the CAPTCHA
78 public function initiateCaptcha () {
80 $captchaLength = $this->getConfigInstance()->getConfigEntry('captcha_string_length');
82 // Get max string length
83 $strLength = $this->getConfigInstance()->getConfigEntry('random_string_length');
85 // Calculate starting position based on random place
86 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
91 // Generate a random string for confirmation
92 $randomString = $this->getRngInstance()->randomString($strLength);
94 // Encode the string with BASE64
95 $base64String = base64_encode($randomString);
97 // Make this string a bit more readable for humans
98 $captchaString = substr($base64String, $start, $captchaLength);
100 // Get all characters we want to replace
101 $searchChars = $this->getConfigInstance()->getConfigEntry('captcha_search_chars');
103 // Get fixed salt and use it as "replacement characters"
104 $replaceChars = $this->getRngInstance()->getExtraSalt();
106 // Remove any plus, equals or slashes
107 for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
108 // Get search character
109 $search = substr($searchChars, $searchIdx, 1);
111 // Random array index
112 $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
115 $replace = substr($replaceChars, $charIdx, 1);
118 $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
121 // Get crypto instance
122 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
124 // Hash the CAPTCHA code for later comparison
125 $this->hashedString = $cryptoInstance->hashString($captchaString);
127 // Encrypt the string for later usage
128 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
132 * Render the CAPTCHA code
136 public function renderCode () {
137 // Get helper instance
138 $helperInstance = $this->getHelperInstance();
140 // Get template instance
141 $templateInstance = $helperInstance->getTemplateInstance();
143 // Load a template for this CAPTCHA
144 $templateInstance->loadCodeTemplate('captch_graphic_code');
147 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName() . '_captcha');
148 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName() . '_hash');
149 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName() . '_encrypt');
152 $templateInstance->assignVariable($helperInstance->getFormName() . '_encrypt', urlencode(base64_encode($this->encryptedString)));
153 $templateInstance->assignVariable($helperInstance->getFormName() . '_hash', $this->hashedString);
155 // Compile the template
156 $templateInstance->compileTemplate();
158 // Get the content back
159 $this->addContent($templateInstance->getRawTemplateData());