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