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