]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/helper/captcha/web/class_GraphicalCodeCaptcha.php
Graphical code CAPTCHA partly finished, crypto class supports encryption/decryption...
[shipsimu.git] / inc / classes / main / helper / captcha / web / class_GraphicalCodeCaptcha.php
1 <?php
2 /**
3  * A solveable graphical code CAPTCHA
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
25         /**
26          * Hash of the CAPTCHA string
27          */
28         private $hashedString = "";
29
30         /**
31          * Encrypted string
32          */
33         private $encryptedString = "";
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43
44                 // Set part description
45                 $this->setObjectDescription("A solveable graphical code CAPTCHA");
46
47                 // Create unique ID number
48                 $this->generateUniqueId();
49         }
50
51         /**
52          * Creates an instance of this captcha class
53          *
54          * @param       $templateInstance       An instance of a template engine
55          * @param       $extraInstance          An extra instance, just for better hash data
56          * @return      $captchaInstance        An instance of this captcha class
57          */
58         public final static function createGraphicalCodeCaptcha (CompileableTemplate $templateInstance, FrameworkInterface $extraInstance = null) {
59                 // Get a new instance
60                 $captchaInstance = new GraphicalCodeCaptcha();
61
62                 // Set template instance
63                 $captchaInstance->setTemplateInstance($templateInstance);
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()->readConfig('captcha_string_length');
80
81                 // Get max string length
82                 $strLength = $this->getConfigInstance()->readConfig('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()->readConfig('captcha_search_chars');
101
102                 // Get fixed salt and use it as "replacement characters"
103                 $replaceChars = $this->getRngInstance()->getFixedSalt();
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                 // Encrypt the string for later usage
124                 $this->encryptedString = $cryptoInstance->encryptString($captchaString);
125
126                 // Hash the encrypted string for later comparison
127                 $this->hashedString = $cryptoInstance->hashString($this->encryptedString);
128         }
129
130         /**
131          * Render the CAPTCHA code
132          *
133          * @return      void
134          */
135         public function renderCode () {
136                 $this->partialStub("Please implement this method.");
137         }
138 }
139
140 // [EOF]
141 ?>