]> git.mxchange.org Git - core.git/blob - inc/main/classes/helper/captcha/web/class_GraphicalCodeCaptcha.php
513ae522df0a660400793ebe62f2ac8e52eec112
[core.git] / inc / 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\Generic\FrameworkInterface;
7
8 /**
9  * A solveable graphical code CAPTCHA
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
31         /**
32          * Hash of the CAPTCHA string
33          */
34         private $hashedString = '';
35
36         /**
37          * Encrypted string
38          */
39         private $encryptedString = '';
40
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
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 static final 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()->getConfigEntry('captcha_string_length');
80
81                 // Get max string length
82                 $strLength = $this->getConfigInstance()->getConfigEntry('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()->getConfigEntry('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 }