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