Copyright year updated, converted double->single quotes
[core.git] / inc / classes / main / filter / validator / class_EmailValidatorFilter.php
1 <?php
2 /**
3  * A concrete filter for validating the email address. This filter may intercept
4  * the filter chain if no email address is given or if supplied email has an
5  * invalid form. It could also intercept our filter chain if email address is
6  * already used by some one if configuration requires this.
7  *
8  * @author              Roland Haeder <webmaster@shipsimu.org>
9  * @version             0.0.0
10  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
11  * @license             GNU GPL 3.0 or any newer version
12  * @link                http://www.shipsimu.org
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 class EmailValidatorFilter extends BaseFilter implements Filterable {
28         /**
29          * Protected constructor
30          *
31          * @return      void
32          */
33         protected function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36         }
37
38         /**
39          * Creates an instance of this filter class
40          *
41          * @return      $filterInstance                 An instance of this filter class
42          */
43         public static final function createEmailValidatorFilter () {
44                 // Get a new instance
45                 $filterInstance = new EmailValidatorFilter();
46
47                 // Return the instance
48                 return $filterInstance;
49         }
50
51         /**
52          * Executes the filter with given request and response objects
53          *
54          * @param       $requestInstance        An instance of a class with an Requestable interface
55          * @param       $responseInstance       An instance of a class with an Responseable interface
56          * @return      void
57          * @throws      FilterChainException    If this filter fails to operate
58          */
59         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
60                 // Get Email from request
61                 $email = $requestInstance->getRequestElement('email');
62
63                 // Is the Email set?
64                 if ((is_null($email)) || ($this->getConfigInstance()->getConfigEntry('register_email_unique') == 'Y')) {
65                         // Try it again
66                         $email1 = $requestInstance->getRequestElement('email1');
67                         $email2 = $requestInstance->getRequestElement('email2');
68
69                         // Is the email still not set?
70                         if ((is_null($email1)) || (is_null($email2))) {
71                                 // Not found in form so stop the filtering process
72                                 $requestInstance->requestIsValid(FALSE);
73
74                                 // Add a message to the response
75                                 $responseInstance->addFatalMessage('email_unset');
76
77                                 // Abort here
78                                 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
79                         } elseif ((empty($email1)) || (empty($email2))) {
80                                 // Email is empty
81                                 $requestInstance->requestIsValid(FALSE);
82
83                                 // Is the email empty?
84                                 if (empty($email1)) {
85                                         // Add a message to the response
86                                         $responseInstance->addFatalMessage('email1_empty');
87                                 } // END - if
88
89                                 // Is the confirmation empty?
90                                 if (empty($email2)) {
91                                         // Add a message to the response
92                                         $responseInstance->addFatalMessage('email2_empty');
93                                 } // END - if
94
95                                 // Abort here
96                                 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
97                         } elseif ($this->ifEmailIsTaken($email1)) {
98                                 // Email is already taken
99                                 $requestInstance->requestIsValid(FALSE);
100
101                                 // Add a message to the response
102                                 $responseInstance->addFatalMessage('email_taken');
103
104                                 // Abort here
105                                 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
106                         } elseif ($email1 != $email2) {
107                                 // Emails didn't match
108                                 $requestInstance->requestIsValid(FALSE);
109
110                                 // Add a message to the response
111                                 $responseInstance->addFatalMessage('emails_mismatch');
112
113                                 // Abort here
114                                 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
115                         } // END - elseif
116                 } elseif (empty($email)) {
117                         // Empty field!
118                         $requestInstance->requestIsValid(FALSE);
119
120                         // Add a message to the response
121                         $responseInstance->addFatalMessage('email_empty');
122
123                         // Abort here
124                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
125                 } // END - elseif
126         }
127
128         /**
129          * Check whether the email as already been taken
130          *
131          * @param       $email                  Email to check for existence
132          * @return      $alreadyTaken   Whether the email has been taken
133          */
134         private function ifEmailIsTaken ($email) {
135                 // Default is already taken
136                 $alreadyTaken = TRUE;
137
138                 // Initialize instance
139                 $userInstance = NULL;
140
141                 // Get a registry instance
142                 $registry = Registry::getRegistry();
143
144                 // Is the user already there?
145                 if ($registry->instanceExists('user')) {
146                         // Use the instance for checking for the email
147                         $userInstance = $registry->getInstance('user');
148                         $userInstance->setEmailAddress($email);
149                 } else {
150                         // If this instance is created then the username *does* exist
151                         $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByEmail'), array($email));
152
153                         // Remember this user instance in our registry for later usage
154                         $registry->addInstance('user', $userInstance);
155                 }
156
157                 // Does the email exist?
158                 if ($userInstance->ifEmailAddressExists() === FALSE) {
159                         // This email has not being used yet
160                         $alreadyTaken = FALSE;
161                 }
162
163                 // Return the result
164                 return $alreadyTaken;
165         }
166 }
167
168 // [EOF]
169 ?>