ea023a0b5ea9cc0972de320e861f6b4770a53db5
[core.git] / inc / main / classes / filter / verifier / class_GraphicalCodeCaptchaVerifierFilter.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Filter\Verifier\Captcha;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Filter\BaseFilter;
8 use CoreFramework\Request\Requestable;
9 use CoreFramework\Response\Responseable;
10
11 /**
12  * A concrete filter for validating code graphical CAPTCHAs with hashes
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 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 GraphicalCodeCaptchaVerifierFilter extends BaseFilter implements Filterable {
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Creates an instance of this filter class
46          *
47          * @return      $filterInstance         An instance of this filter class
48          */
49         public static final function createGraphicalCodeCaptchaVerifierFilter () {
50                 // Get a new instance
51                 $filterInstance = new GraphicalCodeCaptchaVerifierFilter();
52
53                 // Return the instance
54                 return $filterInstance;
55         }
56
57         /**
58          * Executes the filter with given request and response objects
59          *
60          * @param       $requestInstance        An instance of a class with an Requestable interface
61          * @param       $responseInstance       An instance of a class with an Responseable interface
62          * @return      void
63          * @throws      FilterChainException    If this filter fails to operate
64          */
65         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
66                 // Is the form set?
67                 if (($requestInstance->getRequestElement('command') !== 'do_form') ||  (!$requestInstance->isRequestElementSet('form'))) {
68                         // Required field not set
69                         $requestInstance->requestIsValid(FALSE);
70
71                         // Add fatal message
72                         $responseInstance->addFatalMessage('command_form_invalid');
73
74                         // Skip further processing
75                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
76                 } // END - if
77
78                 // Create config entry
79                 $configKey = sprintf('%s_captcha_secured',
80                         $requestInstance->getRequestElement('form')
81                 );
82
83                 // Is the CAPTCHA enabled?
84                 if ($this->getConfigInstance()->getConfigEntry($configKey) != 'Y') {
85                         // Not enabled, so don't check
86                         return;
87                 } // END - if
88
89                 // Get the captcha code
90                 $captchaCode = $requestInstance->getRequestElement('c_code');
91
92                 // Is this set?
93                 if (is_null($captchaCode)) {
94                         // Not set so request is invalid
95                         $requestInstance->requestIsValid(FALSE);
96
97                         // Add fatal message
98                         $responseInstance->addFatalMessage('captcha_code_unset');
99
100                         // Skip further processing
101                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
102                 } elseif (empty($captchaCode)) {
103                         // Empty value so request is invalid
104                         $requestInstance->requestIsValid(FALSE);
105
106                         // Add fatal message
107                         $responseInstance->addFatalMessage('captcha_code_empty');
108
109                         // Skip further processing
110                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
111                 }
112
113                 // Get the hash as well
114                 $captchaHash = $requestInstance->getRequestElement('hash');
115
116                 // Is this set?
117                 if (is_null($captchaHash)) {
118                         // Not set so request is invalid
119                         $requestInstance->requestIsValid(FALSE);
120
121                         // Add fatal message
122                         $responseInstance->addFatalMessage('captcha_hash_unset');
123
124                         // Skip further processing
125                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
126                 } elseif (empty($captchaHash)) {
127                         // Empty value so request is invalid
128                         $requestInstance->requestIsValid(FALSE);
129
130                         // Add fatal message
131                         $responseInstance->addFatalMessage('captcha_hash_empty');
132
133                         // Skip further processing
134                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
135                 }
136
137                 // Now, both are set hash the given one. First get a crypto instance
138                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
139
140                 // Then hash the code
141                 $hashedCode = $cryptoInstance->hashString($captchaCode, $captchaHash);
142
143                 // Is this CAPTCHA valid?
144                 if ($hashedCode != $captchaHash) {
145                         // Not the same so request is invalid
146                         $requestInstance->requestIsValid(FALSE);
147
148                         // Add fatal message
149                         $responseInstance->addFatalMessage('captcha_hash_mismatch');
150
151                         // Skip further processing
152                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
153                 } // END - not the same!
154         }
155
156 }