Continued with renaming-season:
[core.git] / framework / main / classes / filter / verifier / class_UserGuestVerifierFilter.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Filter\Verifier\User;
4
5 // Import framework stuff
6 use CoreFramework\Filter\BaseFilter;
7 use CoreFramework\Filter\Filterable;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Request\Requestable;
10 use CoreFramework\Response\Responseable;
11
12 /**
13  * A concrete filter for verfying the guest username. This filter may intercept the
14  * filter chain if no username is given or if supplied username has an invalid
15  * form. It could also intercept our filter chain if username was not found.
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
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 UserGuestVerifierFilter extends BaseFilter implements Filterable {
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45         }
46
47         /**
48          * Creates an instance of this filter class
49          *
50          * @return      $filterInstance         An instance of this filter class
51          */
52         public static final function createUserGuestVerifierFilter () {
53                 // Get a new instance
54                 $filterInstance = new UserGuestVerifierFilter();
55
56                 // Return the instance
57                 return $filterInstance;
58         }
59
60         /**
61          * Executes the filter with given request and response objects
62          *
63          * @param       $requestInstance        An instance of a class with an Requestable interface
64          * @param       $responseInstance       An instance of a class with an Responseable interface
65          * @return      void
66          * @throws      FilterChainException    If this filter fails to operate
67          */
68         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
69                 // Get username from request
70                 $userName = $requestInstance->getRequestElement('user');
71
72                 // Is the username set?
73                 if (is_null($userName)) {
74                         // Not found in form so stop the filtering process
75                         $requestInstance->requestIsValid(FALSE);
76
77                         // Add a message to the response
78                         $responseInstance->addFatalMessage('username_guest_unset');
79
80                         // Abort here
81                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
82                 } elseif (empty($userName)) {
83                         // Empty field!
84                         $requestInstance->requestIsValid(FALSE);
85
86                         // Add a message to the response
87                         $responseInstance->addFatalMessage('username_guest_empty');
88
89                         // Abort here
90                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
91                 } elseif ($this->ifUserGuestIsTaken($userName) === FALSE) {
92                         // Username is already taken
93                         $requestInstance->requestIsValid(FALSE);
94
95                         // Add a message to the response
96                         $responseInstance->addFatalMessage('username_guest_not_found');
97
98                         // Abort here
99                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
100                 }
101
102                 // Set the element for compatiblity reasons
103                 $requestInstance->setRequestElement('username', $userName);
104         }
105
106         /**
107          * Check whether the username as already been taken
108          *
109          * @param       $userName               Username to check for existence
110          * @return      $alreadyTaken   Whether the username has been taken
111          */
112         private function ifUserGuestIsTaken ($userName) {
113                 // Default is already taken
114                 $alreadyTaken = TRUE;
115
116                 // Initialize instance
117                 $userInstance = NULL;
118
119                 // Get a registry instance
120                 $registry = Registry::getRegistry();
121
122                 // Is the user already there?
123                 if ($registry->instanceExists('user')) {
124                         // Use the instance for checking for the email
125                         $userInstance = $registry->getInstance('user');
126                         $userInstance->setUserGuest($userName);
127                 } else {
128                         // If this instance is created then the username *does* exist
129                         try {
130                                 // Get a new instance
131                                 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('guest_class'), 'createGuestByUsername'), array($userName));
132
133                                 // Remember this user instance in our registry for later usage
134                                 $registry->addInstance('user', $userInstance);
135                         } catch (UsernameMissingException $e) {
136                                 // User was not found
137                         }
138                 }
139
140                 // Does the username exist?
141                 if ((is_null($userInstance)) || ($userInstance->ifUsernameExists() === FALSE)) {
142                         // This username is still available
143                         $alreadyTaken = FALSE;
144                 }
145
146                 // Return the result
147                 return $alreadyTaken;
148         }
149
150 }