3 * A solveable graphical code CAPTCHA
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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__);
44 // Set part description
45 $this->setObjectDescription("A solveable graphical code CAPTCHA");
47 // Create unique ID number
48 $this->generateUniqueId();
52 * Creates an instance of this captcha class
54 * @param $templateInstance An instance of a template engine
55 * @param $extraInstance An extra instance, just for better hash data
56 * @return $captchaInstance An instance of this captcha class
58 public final static function createGraphicalCodeCaptcha (CompileableTemplate $templateInstance, FrameworkInterface $extraInstance = null) {
60 $captchaInstance = new GraphicalCodeCaptcha();
62 // Set template instance
63 $captchaInstance->setTemplateInstance($templateInstance);
66 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
68 // Return the instance
69 return $captchaInstance;
73 * Initiates the CAPTCHA
77 public function initiateCaptcha () {
79 $captchaLength = $this->getConfigInstance()->readConfig('captcha_string_length');
81 // Get max string length
82 $strLength = $this->getConfigInstance()->readConfig('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()->readConfig('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 // Encrypt the string for later usage
124 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
126 // Hash the encrypted string for later comparison
127 $this->hashedString = $cryptoInstance->hashString($this->encryptedString);
131 * Render the CAPTCHA code
135 public function renderCode () {
136 $this->partialStub("Please implement this method.");