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