+<?php
+/**
+ * A solveable graphical code CAPTCHA
+ *
+ * @author Roland Haeder <webmaster@ship-simu.org>
+ * @version 0.0.0
+ * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @license GNU GPL 3.0 or any newer version
+ * @link http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
+ /**
+ * Hash of the CAPTCHA string
+ */
+ private $hashedString = "";
+
+ /**
+ * Encrypted string
+ */
+ private $encryptedString = "";
+
+ /**
+ * Protected constructor
+ *
+ * @return void
+ */
+ protected function __construct () {
+ // Call parent constructor
+ parent::__construct(__CLASS__);
+
+ // Set part description
+ $this->setObjectDescription("A solveable graphical code CAPTCHA");
+
+ // Create unique ID number
+ $this->generateUniqueId();
+ }
+
+ /**
+ * Creates an instance of this captcha class
+ *
+ * @param $templateInstance An instance of a template engine
+ * @param $extraInstance An extra instance, just for better hash data
+ * @return $captchaInstance An instance of this captcha class
+ */
+ public final static function createGraphicalCodeCaptcha (CompileableTemplate $templateInstance, FrameworkInterface $extraInstance = null) {
+ // Get a new instance
+ $captchaInstance = new GraphicalCodeCaptcha();
+
+ // Set template instance
+ $captchaInstance->setTemplateInstance($templateInstance);
+
+ // Initialize the RNG
+ $captchaInstance->initializeRandomNumberGenerator($extraInstance);
+
+ // Return the instance
+ return $captchaInstance;
+ }
+
+ /**
+ * Initiates the CAPTCHA
+ *
+ * @return void
+ */
+ public function initiateCaptcha () {
+ // Get total length
+ $captchaLength = $this->getConfigInstance()->readConfig('captcha_string_length');
+
+ // Get max string length
+ $strLength = $this->getConfigInstance()->readConfig('random_string_length');
+
+ // Calculate starting position based on random place
+ $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
+
+ // Test it
+ assert($start >= 0);
+
+ // Generate a random string for confirmation
+ $randomString = $this->getRngInstance()->randomString($strLength);
+
+ // Encode the string with BASE64
+ $base64String = base64_encode($randomString);
+
+ // Make this string a bit more readable for humans
+ $captchaString = substr($base64String, $start, $captchaLength);
+
+ // Get all characters we want to replace
+ $searchChars = $this->getConfigInstance()->readConfig('captcha_search_chars');
+
+ // Get fixed salt and use it as "replacement characters"
+ $replaceChars = $this->getRngInstance()->getFixedSalt();
+
+ // Remove any plus, equals or slashes
+ for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
+ // Get search character
+ $search = substr($searchChars, $searchIdx, 1);
+
+ // Random array index
+ $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
+
+ // Get replacement
+ $replace = substr($replaceChars, $charIdx, 1);
+
+ // Replace character
+ $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
+ } // END - foreach
+
+ // Get crypto instance
+ $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
+
+ // Encrypt the string for later usage
+ $this->encryptedString = $cryptoInstance->encryptString($captchaString);
+
+ // Hash the encrypted string for later comparison
+ $this->hashedString = $cryptoInstance->hashString($this->encryptedString);
+ }
+
+ /**
+ * Render the CAPTCHA code
+ *
+ * @return void
+ */
+ public function renderCode () {
+ $this->partialStub("Please implement this method.");
+ }
+}
+
+// [EOF]
+?>