class_GraphicalCodeCaptcha.php

Go to the documentation of this file.
00001 <?php
00024 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
00028         private $hashedString = "";
00029 
00033         private $encryptedString = "";
00034 
00040         protected function __construct () {
00041                 // Call parent constructor
00042                 parent::__construct(__CLASS__);
00043         }
00044 
00052         public final static function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = null) {
00053                 // Get a new instance
00054                 $captchaInstance = new GraphicalCodeCaptcha();
00055 
00056                 // Set template instance
00057                 $captchaInstance->setHelperInstance($helperInstance);
00058 
00059                 // Initialize the RNG
00060                 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
00061 
00062                 // Return the instance
00063                 return $captchaInstance;
00064         }
00065 
00071         public function initiateCaptcha () {
00072                 // Get total length
00073                 $captchaLength = $this->getConfigInstance()->readConfig('captcha_string_length');
00074 
00075                 // Get max string length
00076                 $strLength = $this->getConfigInstance()->readConfig('random_string_length');
00077 
00078                 // Calculate starting position based on random place
00079                 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
00080 
00081                 // Test it
00082                 assert($start >= 0);
00083 
00084                 // Generate a random string for confirmation
00085                 $randomString = $this->getRngInstance()->randomString($strLength);
00086 
00087                 // Encode the string with BASE64
00088                 $base64String = base64_encode($randomString);
00089 
00090                 // Make this string a bit more readable for humans
00091                 $captchaString = substr($base64String, $start, $captchaLength);
00092 
00093                 // Get all characters we want to replace
00094                 $searchChars = $this->getConfigInstance()->readConfig('captcha_search_chars');
00095 
00096                 // Get fixed salt and use it as "replacement characters"
00097                 $replaceChars = $this->getRngInstance()->getExtraSalt();
00098 
00099                 // Remove any plus, equals or slashes
00100                 for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
00101                         // Get search character
00102                         $search = substr($searchChars, $searchIdx, 1);
00103 
00104                         // Random array index
00105                         $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
00106 
00107                         // Get replacement
00108                         $replace = substr($replaceChars, $charIdx, 1);
00109 
00110                         // Replace character
00111                         $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
00112                 } // END - foreach
00113 
00114                 // Get crypto instance
00115                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
00116 
00117                 // Hash the CAPTCHA code for later comparison
00118                 $this->hashedString = $cryptoInstance->hashString($captchaString);
00119 
00120                 // Encrypt the string for later usage
00121                 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
00122         }
00123 
00129         public function renderCode () {
00130                 // Get helper instance
00131                 $helperInstance = $this->getHelperInstance();
00132 
00133                 // Get template instance
00134                 $templateInstance = $helperInstance->getTemplateInstance();
00135 
00136                 // Load a template for this CAPTCHA
00137                 $templateInstance->loadCodeTemplate('captch_graphic_code');
00138 
00139                 // Rename variable
00140                 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName().'_captcha');
00141                 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName().'_hash');
00142                 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName().'_encrypt');
00143 
00144                 // Assign variables
00145                 $templateInstance->assignVariable($helperInstance->getFormName().'_encrypt', urlencode(base64_encode($this->encryptedString)));
00146                 $templateInstance->assignVariable($helperInstance->getFormName().'_hash', $this->hashedString);
00147 
00148                 // Compile the template
00149                 $templateInstance->compileTemplate();
00150 
00151                 // Get the content back
00152                 $this->addContent($templateInstance->getRawTemplateData());
00153         }
00154 }
00155 
00156 // [EOF]
00157 ?>

Generated on Mon Dec 8 01:06:45 2008 for Ship-Simulator by  doxygen 1.5.6