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
00042 parent::__construct(__CLASS__);
00043 }
00044
00052 public final static function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = null) {
00053
00054 $captchaInstance = new GraphicalCodeCaptcha();
00055
00056
00057 $captchaInstance->setHelperInstance($helperInstance);
00058
00059
00060 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
00061
00062
00063 return $captchaInstance;
00064 }
00065
00071 public function initiateCaptcha () {
00072
00073 $captchaLength = $this->getConfigInstance()->readConfig('captcha_string_length');
00074
00075
00076 $strLength = $this->getConfigInstance()->readConfig('random_string_length');
00077
00078
00079 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
00080
00081
00082 assert($start >= 0);
00083
00084
00085 $randomString = $this->getRngInstance()->randomString($strLength);
00086
00087
00088 $base64String = base64_encode($randomString);
00089
00090
00091 $captchaString = substr($base64String, $start, $captchaLength);
00092
00093
00094 $searchChars = $this->getConfigInstance()->readConfig('captcha_search_chars');
00095
00096
00097 $replaceChars = $this->getRngInstance()->getExtraSalt();
00098
00099
00100 for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
00101
00102 $search = substr($searchChars, $searchIdx, 1);
00103
00104
00105 $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
00106
00107
00108 $replace = substr($replaceChars, $charIdx, 1);
00109
00110
00111 $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
00112 }
00113
00114
00115 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
00116
00117
00118 $this->hashedString = $cryptoInstance->hashString($captchaString);
00119
00120
00121 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
00122 }
00123
00129 public function renderCode () {
00130
00131 $helperInstance = $this->getHelperInstance();
00132
00133
00134 $templateInstance = $helperInstance->getTemplateInstance();
00135
00136
00137 $templateInstance->loadCodeTemplate('captch_graphic_code');
00138
00139
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
00145 $templateInstance->assignVariable($helperInstance->getFormName().'_encrypt', urlencode(base64_encode($this->encryptedString)));
00146 $templateInstance->assignVariable($helperInstance->getFormName().'_hash', $this->hashedString);
00147
00148
00149 $templateInstance->compileTemplate();
00150
00151
00152 $this->addContent($templateInstance->getRawTemplateData());
00153 }
00154 }
00155
00156
00157 ?>