3e57d241e65dddf8c1fba3773db2b3f679f9ad61
[shipsimu.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@ship-simu.org>
6  * @version             0.0.0
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
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                 // Set part description
45                 $this->setObjectDescription("A solveable graphical code CAPTCHA");
46
47                 // Create unique ID number
48                 $this->generateUniqueId();
49         }
50
51         /**
52          * Creates an instance of this captcha class
53          *
54          * @param       $helperInstance         An instance of a helper class
55          * @param       $extraInstance          An extra instance, just for better hash data
56          * @return      $captchaInstance        An instance of this captcha class
57          */
58         public final static function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = null) {
59                 // Get a new instance
60                 $captchaInstance = new GraphicalCodeCaptcha();
61
62                 // Set template instance
63                 $captchaInstance->setHelperInstance($helperInstance);
64
65                 // Initialize the RNG
66                 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
67
68                 // Return the instance
69                 return $captchaInstance;
70         }
71
72         /**
73          * Initiates the CAPTCHA
74          *
75          * @return      void
76          */
77         public function initiateCaptcha () {
78                 // Get total length
79                 $captchaLength = $this->getConfigInstance()->readConfig('captcha_string_length');
80
81                 // Get max string length
82                 $strLength = $this->getConfigInstance()->readConfig('random_string_length');
83
84                 // Calculate starting position based on random place
85                 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
86
87                 // Test it
88                 assert($start >= 0);
89
90                 // Generate a random string for confirmation
91                 $randomString = $this->getRngInstance()->randomString($strLength);
92
93                 // Encode the string with BASE64
94                 $base64String = base64_encode($randomString);
95
96                 // Make this string a bit more readable for humans
97                 $captchaString = substr($base64String, $start, $captchaLength);
98
99                 // Get all characters we want to replace
100                 $searchChars = $this->getConfigInstance()->readConfig('captcha_search_chars');
101
102                 // Get fixed salt and use it as "replacement characters"
103                 $replaceChars = $this->getRngInstance()->getExtraSalt();
104
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);
109
110                         // Random array index
111                         $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
112
113                         // Get replacement
114                         $replace = substr($replaceChars, $charIdx, 1);
115
116                         // Replace character
117                         $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
118                 } // END - foreach
119
120                 // Get crypto instance
121                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
122
123                 // Hash the CAPTCHA code for later comparison
124                 $this->hashedString = $cryptoInstance->hashString($captchaString);
125
126                 // Encrypt the string for later usage
127                 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
128         }
129
130         /**
131          * Render the CAPTCHA code
132          *
133          * @return      void
134          */
135         public function renderCode () {
136                 // Get helper instance
137                 $helperInstance = $this->getHelperInstance();
138
139                 // Get template instance
140                 $templateInstance = $helperInstance->getTemplateInstance();
141
142                 // Load a template for this CAPTCHA
143                 $templateInstance->loadCodeTemplate("captch_graphic_code");
144
145                 // Rename variable
146                 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName().'_captcha');
147                 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName().'_hash');
148                 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName().'_encrypt');
149
150                 // Assign variables
151                 $templateInstance->assignVariable($helperInstance->getFormName().'_encrypt', urlencode(base64_encode($this->encryptedString)));
152                 $templateInstance->assignVariable($helperInstance->getFormName().'_hash', $this->hashedString);
153
154                 // Compile the template
155                 $templateInstance->compileTemplate();
156
157                 // Get the content back
158                 $this->addContent($templateInstance->getRawTemplateData());
159         }
160 }
161
162 // [EOF]
163 ?>