4b982b04b9b0b5aa30f0e429bb672875b33782da
[shipsimu.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 the supplied username has
5  * an invalid form. It could also intercept the filter chain if the email
6  * address is already used by some one if configuration requires this.
7  *
8  * @author              Roland Haeder <webmaster@ship-simu.org>
9  * @version             0.0.0
10  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
11  * @license             GNU GPL 3.0 or any newer version
12  * @link                http://www.ship-simu.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 BaseFrameworkSystem implements Filterable {
28         /**
29          * Private constructor
30          *
31          * @return      void
32          */
33         protected function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36
37                 // Set part description
38                 $this->setObjectDescription("A filter for Email validation");
39
40                 // Create unique ID number
41                 $this->createUniqueID();
42
43                 // Clean up a little
44                 $this->removeNumberFormaters();
45                 $this->removeSystemArray();
46         }
47
48         /**
49          * Creates an instance of this filter class
50          *
51          * @return      $filterInstance         An instance of this filter class
52          */
53         public final static function createEmailValidatorFilter () {
54                 // Get a new instance
55                 $filterInstance = new EmailValidatorFilter();
56
57                 // Return the instance
58                 return $filterInstance;
59         }
60
61         /**
62          * Executes the filter with given request and response objects
63          *
64          * @param       $requestInstance        An instance of a class with an Requestable interface
65          * @param       $responseInstance       An instance of a class with an Responseable interface
66          * @return      void
67          */
68         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
69                 // Get Email from request
70                 $email = $requestInstance->getRequestElement('email');
71
72                 // Is the Email set?
73                 if ((is_null($email)) || ($this->getConfigInstance()->readConfig('register_email_unique') == "Y")) {
74                         // Try it again
75                         $email1 = $requestInstance->getRequestElement('email1');
76                         $email2 = $requestInstance->getRequestElement('email2');
77
78                         // Is the email still not set?
79                         if ((is_null($email1)) || (is_null($email2))) {
80                                 // Not found in form so stop the filtering process
81                                 $requestInstance->requestIsValid(false);
82
83                                 // Add a message to the response
84                                 $responseInstance->addFatalMessage('email_unset');
85
86                                 // Abort here
87                                 return false;
88                         } elseif ((empty($email1)) || (empty($email2))) {
89                                 // Email is empty
90                                 $requestInstance->requestIsValid(false);
91
92                                 // Is the email empty?
93                                 if (empty($email1)) {
94                                         // Add a message to the response
95                                         $responseInstance->addFatalMessage('email1_empty');
96                                 } // END - if
97
98                                 // Is the confirmation empty?
99                                 if (empty($email2)) {
100                                         // Add a message to the response
101                                         $responseInstance->addFatalMessage('email2_empty');
102                                 } // END - if
103
104                                 // Abort here
105                                 return false;
106                         } elseif ($this->ifEmailIsTaken($email1)) {
107                                 // Email is already taken
108                                 $requestInstance->requestIsValid(false);
109
110                                 // Add a message to the response
111                                 $responseInstance->addFatalMessage('email_taken');
112
113                                 // Abort here
114                                 return false;
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                         return false;
125                 } // END - elseif
126         }
127
128         /**
129          * Check wether the email as already been taken
130          *
131          * @param       $email          Email to check for existence
132          * @return      $alreadyTaken   Wether the email has been taken
133          */
134         private function ifEmailIsTaken ($email) {
135                 // Default is already taken
136                 $alreadyTaken = true;
137                 $this->partialStub(sprintf("Email: %s", $email));
138
139                 // Return the result
140                 return $alreadyTaken;
141         }
142 }
143
144 // [EOF]
145 ?>