]> git.mxchange.org Git - core.git/blob - inc/classes/main/helper/captcha/web/class_GraphicalCodeCaptcha.php
Copyright year updated, converted double->single quotes
[core.git] / inc / classes / main / helper / captcha / web / class_GraphicalCodeCaptcha.php
1 <?php
2 /**
3  * A solveable graphical code CAPTCHA
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
25         /**
26          * Hash of the CAPTCHA string
27          */
28         private $hashedString = '';
29
30         /**
31          * Encrypted string
32          */
33         private $encryptedString = '';
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43         }
44
45         /**
46          * Creates an instance of this captcha class
47          *
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
51          */
52         public static final function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = NULL) {
53                 // Get a new instance
54                 $captchaInstance = new GraphicalCodeCaptcha();
55
56                 // Set template instance
57                 $captchaInstance->setHelperInstance($helperInstance);
58
59                 // Initialize the RNG
60                 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
61
62                 // Return the instance
63                 return $captchaInstance;
64         }
65
66         /**
67          * Initiates the CAPTCHA
68          *
69          * @return      void
70          */
71         public function initiateCaptcha () {
72                 // Get total length
73                 $captchaLength = $this->getConfigInstance()->getConfigEntry('captcha_string_length');
74
75                 // Get max string length
76                 $strLength = $this->getConfigInstance()->getConfigEntry('random_string_length');
77
78                 // Calculate starting position based on random place
79                 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
80
81                 // Test it
82                 assert($start >= 0);
83
84                 // Generate a random string for confirmation
85                 $randomString = $this->getRngInstance()->randomString($strLength);
86
87                 // Encode the string with BASE64
88                 $base64String = base64_encode($randomString);
89
90                 // Make this string a bit more readable for humans
91                 $captchaString = substr($base64String, $start, $captchaLength);
92
93                 // Get all characters we want to replace
94                 $searchChars = $this->getConfigInstance()->getConfigEntry('captcha_search_chars');
95
96                 // Get fixed salt and use it as "replacement characters"
97                 $replaceChars = $this->getRngInstance()->getExtraSalt();
98
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);
103
104                         // Random array index
105                         $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
106
107                         // Get replacement
108                         $replace = substr($replaceChars, $charIdx, 1);
109
110                         // Replace character
111                         $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
112                 } // END - foreach
113
114                 // Get crypto instance
115                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
116
117                 // Hash the CAPTCHA code for later comparison
118                 $this->hashedString = $cryptoInstance->hashString($captchaString);
119
120                 // Encrypt the string for later usage
121                 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
122         }
123
124         /**
125          * Render the CAPTCHA code
126          *
127          * @return      void
128          */
129         public function renderCode () {
130                 // Get helper instance
131                 $helperInstance = $this->getHelperInstance();
132
133                 // Get template instance
134                 $templateInstance = $helperInstance->getTemplateInstance();
135
136                 // Load a template for this CAPTCHA
137                 $templateInstance->loadCodeTemplate('captch_graphic_code');
138
139                 // Rename variable
140                 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName().'_captcha');
141                 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName().'_hash');
142                 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName().'_encrypt');
143
144                 // Assign variables
145                 $templateInstance->assignVariable($helperInstance->getFormName().'_encrypt', urlencode(base64_encode($this->encryptedString)));
146                 $templateInstance->assignVariable($helperInstance->getFormName().'_hash', $this->hashedString);
147
148                 // Compile the template
149                 $templateInstance->compileTemplate();
150
151                 // Get the content back
152                 $this->addContent($templateInstance->getRawTemplateData());
153         }
154 }
155
156 // [EOF]
157 ?>