cee32d53f1bf3fb4e8ea045d9d7ac2be67785ebe
[core.git] / framework / main / classes / helper / captcha / web / class_GraphicalCodeCaptcha.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Captcha;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\FrameworkInterface;
8
9 /**
10  * A solveable graphical code CAPTCHA
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.shipsimu.org
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
32         /**
33          * Hash of the CAPTCHA string
34          */
35         private $hashedString = '';
36
37         /**
38          * Encrypted string
39          */
40         private $encryptedString = '';
41
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Creates an instance of this captcha class
54          *
55          * @param       $helperInstance         An instance of a helper class
56          * @param       $extraInstance          An extra instance, just for better hash data
57          * @return      $captchaInstance        An instance of this captcha class
58          */
59         public static final function createGraphicalCodeCaptcha (HelpableTemplate $helperInstance, FrameworkInterface $extraInstance = NULL) {
60                 // Get a new instance
61                 $captchaInstance = new GraphicalCodeCaptcha();
62
63                 // Set template instance
64                 $captchaInstance->setHelperInstance($helperInstance);
65
66                 // Initialize the RNG
67                 $captchaInstance->initializeRandomNumberGenerator($extraInstance);
68
69                 // Return the instance
70                 return $captchaInstance;
71         }
72
73         /**
74          * Initiates the CAPTCHA
75          *
76          * @return      void
77          */
78         public function initiateCaptcha () {
79                 // Get total length
80                 $captchaLength = $this->getConfigInstance()->getConfigEntry('captcha_string_length');
81
82                 // Get max string length
83                 $strLength = $this->getConfigInstance()->getConfigEntry('random_string_length');
84
85                 // Calculate starting position based on random place
86                 $start = $this->getRngInstance()->randomNumber(0, ($strLength - $captchaLength));
87
88                 // Test it
89                 assert($start >= 0);
90
91                 // Generate a random string for confirmation
92                 $randomString = $this->getRngInstance()->randomString($strLength);
93
94                 // Encode the string with BASE64
95                 $base64String = base64_encode($randomString);
96
97                 // Make this string a bit more readable for humans
98                 $captchaString = substr($base64String, $start, $captchaLength);
99
100                 // Get all characters we want to replace
101                 $searchChars = $this->getConfigInstance()->getConfigEntry('captcha_search_chars');
102
103                 // Get fixed salt and use it as "replacement characters"
104                 $replaceChars = $this->getRngInstance()->getExtraSalt();
105
106                 // Remove any plus, equals or slashes
107                 for ($searchIdx = 0; $searchIdx < strlen($searchChars); $searchIdx++) {
108                         // Get search character
109                         $search = substr($searchChars, $searchIdx, 1);
110
111                         // Random array index
112                         $charIdx = $this->getRngInstance()->randomNumber(0, (strlen($replaceChars) - 1));
113
114                         // Get replacement
115                         $replace = substr($replaceChars, $charIdx, 1);
116
117                         // Replace character
118                         $captchaString = str_replace($search, $replace, $captchaString, $captchaLength);
119                 } // END - foreach
120
121                 // Get crypto instance
122                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
123
124                 // Hash the CAPTCHA code for later comparison
125                 $this->hashedString = $cryptoInstance->hashString($captchaString);
126
127                 // Encrypt the string for later usage
128                 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
129         }
130
131         /**
132          * Render the CAPTCHA code
133          *
134          * @return      void
135          */
136         public function renderCode () {
137                 // Get helper instance
138                 $helperInstance = $this->getHelperInstance();
139
140                 // Get template instance
141                 $templateInstance = $helperInstance->getTemplateInstance();
142
143                 // Load a template for this CAPTCHA
144                 $templateInstance->loadCodeTemplate('captch_graphic_code');
145
146                 // Rename variable
147                 $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName() . '_captcha');
148                 $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName() . '_hash');
149                 $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName() . '_encrypt');
150
151                 // Assign variables
152                 $templateInstance->assignVariable($helperInstance->getFormName() . '_encrypt', urlencode(base64_encode($this->encryptedString)));
153                 $templateInstance->assignVariable($helperInstance->getFormName() . '_hash', $this->hashedString);
154
155                 // Compile the template
156                 $templateInstance->compileTemplate();
157
158                 // Get the content back
159                 $this->addContent($templateInstance->getRawTemplateData());
160         }
161
162 }