d07317a4b1b3809f361a904b70d78decf5862aeb
[shipsimu.git] / inc / classes / main / user / class_Guest.php
1 <?php
2 /**
3  * A generic class for handling guests
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class Guest extends BaseFrameworkSystem implements ManageableUser, Registerable {
25         /**
26          * Instance of the database result
27          */
28         private $resultInstance = null;
29
30         /**
31          * Username of current user
32          */
33         private $userName = "";
34
35         /**
36          * Email of current user
37          */
38         private $email = "";
39
40         // Exceptions
41         const EXCEPTION_USERNAME_NOT_FOUND   = 0x060;
42         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x061;
43         const EXCEPTION_USER_PASS_MISMATCH   = 0x062;
44
45         /**
46          * Protected constructor
47          *
48          * @param       $className      Name of the class
49          * @return      void
50          */
51         protected function __construct ($className = "") {
52                 // Is the class name empty? Then this is not a specialized user class
53                 if (empty($className)) $className = __CLASS__;
54
55                 // Call parent constructor
56                 parent::__construct($className);
57
58                 // Set part description
59                 $this->setObjectDescription("Generic user class");
60
61                 // Create unique ID number
62                 $this->generateUniqueId();
63
64                 // Clean up a little
65                 $this->removeNumberFormaters();
66                 $this->removeSystemArray();
67         }
68
69         /**
70          * Creates an instance of this user class by a provided username. This
71          * factory method will check if the username is already taken and if not
72          * so it will throw an exception.
73          *
74          * @param       $userName               Username we need a class instance for
75          * @return      $userInstance   An instance of this user class
76          * @throws      UsernameMissingException        If the username does not exist
77          */
78         public final static function createGuestByUsername ($userName) {
79                 // Get a new instance
80                 $userInstance = new Guest();
81
82                 // Set the username
83                 $userInstance->setUserName($userName);
84
85                 // Check if the username exists
86                 if (!$userInstance->ifUsernameExists()) {
87                         // Throw an exception here
88                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
89                 }
90
91                 // Return the instance
92                 return $userInstance;
93         }
94
95         /**
96          * Creates an instance of this user class by a provided email address. This
97          * factory method will not check if the email address is there.
98          *
99          * @param       $email                  Email address of the user
100          * @return      $userInstance   An instance of this user class
101          */
102         public final static function createGuestByEmail ($email) {
103                 // Get a new instance
104                 $userInstance = new User();
105
106                 // Set the username
107                 $userInstance->setEmail($email);
108
109                 // Return the instance
110                 return $userInstance;
111         }
112
113         /**
114          * "Getter" for databse entry
115          *
116          * @return      $entry  An array with database entries
117          * @throws      NullPointerException    If the database result is not found
118          * @throws      InvalidDatabaseResultException  If the database result is invalid
119          */
120         private function getDatabaseEntry () {
121                 // Is there an instance?
122                 if (is_null($this->resultInstance)) {
123                         // Throw new exception
124                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
125                 } // END - if
126
127                 // Rewind it
128                 $this->resultInstance->rewind();
129
130                 // Do we have an entry?
131                 if (!$this->resultInstance->valid()) {
132                         throw new InvalidDatabaseResultException(array($this, $this->resultInstance), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
133                 } // END - if
134
135                 // Get next entry
136                 $this->resultInstance->next();
137
138                 // Fetch it
139                 $entry = $this->resultInstance->current();
140
141                 // And return it
142                 return $entry;
143         }
144
145         /**
146          * Setter for username
147          *
148          * @param       $userName       The username to set
149          * @return      void
150          */
151         public final function setUserName ($userName) {
152                 $this->userName = $userName;
153         }
154
155         /**
156          * Setter for email
157          *
158          * @param       $email  The email to set
159          * @return      void
160          */
161         protected final function setEmail ($email) {
162                 $this->email = $email;
163         }
164
165         /**
166          * Getter for username
167          *
168          * @return      $userName       The username to get
169          */
170         public final function getUsername () {
171                 return $this->userName;
172         }
173
174         /**
175          * Getter for email
176          *
177          * @return      $email  The email to get
178          */
179         public final function getEmail () {
180                 return $this->email;
181         }
182
183         /**
184          * Determines wether the username exists or not
185          *
186          * @return      $exists         Wether the username exists
187          */
188         public function ifUsernameExists () {
189                 // By default the username does not exist
190                 $exists = false;
191
192                 // Is a previous result there?
193                 if (is_null($this->resultInstance)) {
194                         // Get a UserDatabaseWrapper instance
195                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
196
197                         // Create a search criteria
198                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
199
200                         // Add the username as a criteria and set limit to one entry
201                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
202                         $criteriaInstance->setLimit(1);
203
204                         // Get a search result
205                         $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
206                 } else {
207                         // Rewind it
208                         $this->resultInstance->rewind();
209                 }
210
211                 // Search for it
212                 if ($this->resultInstance->next()) {
213                         // Entry found
214                         $exists = true;
215                 } // END - if
216
217                 // Return the status
218                 return $exists;
219         }
220
221         /**
222          * Determines wether the email exists or not
223          *
224          * @return      $exists         Wether the email exists
225          */
226         public function ifEmailAddressExists () {
227                 // By default the email does not exist
228                 $exists = false;
229
230                 // Is a previous result there?
231                 if (is_null($this->resultInstance)) {
232                         // Get a UserDatabaseWrapper instance
233                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
234
235                         // Create a search criteria
236                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
237
238                         // Add the username as a criteria and set limit to one entry
239                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
240                         $criteriaInstance->setLimit(1);
241
242                         // Get a search resultInstance
243                         $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
244                 } else {
245                         // Rewind it
246                         $this->resultInstance->rewind();
247                 }
248
249                 // Search for it
250                 if ($this->resultInstance->next()) {
251                         // Entry found
252                         $exists = true;
253                 } // END - if
254
255                 // Return the status
256                 return $exists;
257         }
258
259         /**
260          * Checks if the supplied password hash in request matches with the stored
261          * in database.
262          *
263          * @param       $requestInstance        A requestable class instance
264          * @return      $matches                        Wether the supplied password hash matches
265          */
266         public function ifPasswordHashMatches (Requestable $requestInstance) {
267                 // By default nothing matches... ;)
268                 $matches = false;
269
270                 // Get a UserDatabaseWrapper instance
271                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
272
273                 // Create a search criteria
274                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
275
276                 // Add the username as a criteria and set limit to one entry
277                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
278                 $criteriaInstance->setLimit(1);
279
280                 // Get a search resultInstance
281                 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
282
283                 // Search for it
284                 if ($this->resultInstance->next()) {
285                         // Get the current entry (can only be one!)
286                         $entry = $this->resultInstance->current();
287
288                         // So does the hashes match?
289                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
290                         $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
291                 } // END - if
292
293                 // Return the status
294                 return $matches;
295         }
296
297         /**
298          * Adds data for later complete update
299          *
300          * @param       $column         Column we want to update
301          * @param       $value          New value to store in database
302          * @return      void
303          */
304         public function addUpdateData ($column, $value) {
305                 $this->partialStub("Column={$column}, value={$value}");
306         }
307
308         /**
309          * "Getter" for user's password hash
310          *
311          * @return      $passHash       User's password hash from database result
312          */
313         public function getPasswordHash () {
314                 // Default is missing password hash
315                 $passHash = null;
316
317                 // Get a database entry
318                 $entry = $this->getDatabaseEntry();
319
320                 // Is the password hash there?
321                 if (isset($entry['pass_hash'])) {
322                         // Get it
323                         $passHash = $entry['pass_hash'];
324                 }
325
326                 // And return the hash
327                 return $passHash;
328         }
329
330         /**
331          * Updates the last activity timestamp and last performed action in the
332          * database result. You should call flushUpdates() to flush these updates
333          * to the database layer.
334          *
335          * @param       $requestInstance        A requestable class instance
336          * @return      void
337          */
338         public function updateLastActivity (Requestable $requestInstance) {
339                 // No activity will be logged for guest accounts
340         }
341
342         /**
343          * Flushs all updated entries to the database layer
344          *
345          * @return      void
346          */
347         public function flushUpdates () {
348                 // No updates will be flushed to database!
349         }
350 }
351
352 // [EOF]
353 ?>