3 namespace CoreFramework\Filter\Verifier\User;
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;
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.
17 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
36 class UserGuestVerifierFilter extends BaseFilter implements Filterable {
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
48 * Creates an instance of this filter class
50 * @return $filterInstance An instance of this filter class
52 public static final function createUserGuestVerifierFilter () {
54 $filterInstance = new UserGuestVerifierFilter();
56 // Return the instance
57 return $filterInstance;
61 * Executes the filter with given request and response objects
63 * @param $requestInstance An instance of a class with an Requestable interface
64 * @param $responseInstance An instance of a class with an Responseable interface
66 * @throws FilterChainException If this filter fails to operate
68 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
69 // Get username from request
70 $userName = $requestInstance->getRequestElement('user');
72 // Is the username set?
73 if (is_null($userName)) {
74 // Not found in form so stop the filtering process
75 $requestInstance->requestIsValid(FALSE);
77 // Add a message to the response
78 $responseInstance->addFatalMessage('username_guest_unset');
81 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
82 } elseif (empty($userName)) {
84 $requestInstance->requestIsValid(FALSE);
86 // Add a message to the response
87 $responseInstance->addFatalMessage('username_guest_empty');
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);
95 // Add a message to the response
96 $responseInstance->addFatalMessage('username_guest_not_found');
99 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
102 // Set the element for compatiblity reasons
103 $requestInstance->setRequestElement('username', $userName);
107 * Check whether the username as already been taken
109 * @param $userName Username to check for existence
110 * @return $alreadyTaken Whether the username has been taken
112 private function ifUserGuestIsTaken ($userName) {
113 // Default is already taken
114 $alreadyTaken = TRUE;
116 // Initialize instance
117 $userInstance = NULL;
119 // Get a registry instance
120 $registry = Registry::getRegistry();
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);
128 // If this instance is created then the username *does* exist
130 // Get a new instance
131 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('guest_class'), 'createGuestByUsername'), array($userName));
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
140 // Does the username exist?
141 if ((is_null($userInstance)) || ($userInstance->ifUsernameExists() === FALSE)) {
142 // This username is still available
143 $alreadyTaken = FALSE;
147 return $alreadyTaken;