Continued:
[core.git] / inc / main / classes / user / class_BaseUser.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\User;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Request\Requestable;
9
10 /**
11  * A general user class
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class BaseUser extends BaseFrameworkSystem implements Updateable {
33         // Exception constances
34         const EXCEPTION_USERNAME_NOT_FOUND   = 0x150;
35         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
36         const EXCEPTION_USER_PASS_MISMATCH   = 0x152;
37         const EXCEPTION_USER_IS_GUEST        = 0x153;
38
39         /**
40          * Username of current user
41          */
42         private $userName = '';
43
44         /**
45          * User id of current user
46          */
47         private $userId = 0;
48
49         /**
50          * Email of current user
51          */
52         private $email = '';
53
54         /**
55          * Protected constructor
56          *
57          * @param       $className      Name of the class
58          * @return      void
59          */
60         protected function __construct ($className) {
61                 // Call parent constructor
62                 parent::__construct($className);
63         }
64
65         /**
66          * Setter for username
67          *
68          * @param       $userName       The username to set
69          * @return      void
70          */
71         public final function setUserName ($userName) {
72                 $this->userName = (string) $userName;
73         }
74
75         /**
76          * Getter for username
77          *
78          * @return      $userName       The username to get
79          */
80         public final function getUserName () {
81                 return $this->userName;
82         }
83
84         /**
85          * Setter for user id
86          *
87          * @param       $userId         The user id to set
88          * @return      void
89          * @todo        Find a way of casting here. "(int)" might destroy the user id > 32766
90          */
91         public final function setUserId ($userId) {
92                 $this->userId = $userId;
93         }
94
95         /**
96          * Getter for user id
97          *
98          * @return      $userId The user id to get
99          */
100         public final function getUserId () {
101                 return $this->userId;
102         }
103
104         /**
105          * Setter for email
106          *
107          * @param       $email  The email to set
108          * @return      void
109          */
110         protected final function setEmail ($email) {
111                 $this->email = (string) $email;
112         }
113
114         /**
115          * Getter for email
116          *
117          * @return      $email  The email to get
118          */
119         public final function getEmail () {
120                 return $this->email;
121         }
122
123         /**
124          * Determines whether the username exists or not
125          *
126          * @return      $exists         Whether the username exists
127          */
128         public function ifUsernameExists () {
129                 // By default the username does not exist
130                 $exists = FALSE;
131
132                 // Is a previous result there?
133                 if (!$this->getResultInstance() instanceof SearchableResult) {
134                         // Get a UserDatabaseWrapper instance
135                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
136
137                         // Create a search criteria
138                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
139
140                         // Add the username as a criteria and set limit to one entry
141                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
142                         $criteriaInstance->setLimit(1);
143
144                         // Get a search result
145                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
146
147                         // Set the index "solver"
148                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
149
150                         // And finally set it
151                         $this->setResultInstance($resultInstance);
152                 } // END - if
153
154                 // Rewind it
155                 $this->getResultInstance()->rewind();
156
157                 // Search for it
158                 if ($this->getResultInstance()->next()) {
159                         // Entry found
160                         $exists = TRUE;
161                 } // END - if
162
163                 // Return the status
164                 return $exists;
165         }
166
167         /**
168          * Determines whether the email exists or not
169          *
170          * @return      $exists         Whether the email exists
171          */
172         public function ifEmailAddressExists () {
173                 // By default the email does not exist
174                 $exists = FALSE;
175
176                 // Is a previous result there?
177                 if (!$this->getResultInstance() instanceof SearchableResult) {
178                         // Get a UserDatabaseWrapper instance
179                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
180
181                         // Create a search criteria
182                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
183
184                         // Add the username as a criteria and set limit to one entry
185                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
186                         $criteriaInstance->setLimit(1);
187
188                         // Get a search result
189                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
190
191                         // Set the index "solver"
192                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
193
194                         // And finally set it
195                         $this->setResultInstance($resultInstance);
196                 } // END - if
197
198                 // Rewind it
199                 $this->getResultInstance()->rewind();
200
201                 // Search for it
202                 if ($this->getResultInstance()->next()) {
203                         // Entry found
204                         $exists = TRUE;
205
206                         // Is the username set?
207                         if ($this->getUserName() == '') {
208                                 // Get current entry
209                                 $currEntry = $this->getResultInstance()->current();
210
211                                 // Set the username
212                                 $this->setUserName($currEntry['username']);
213                         } // END - if
214                 } // END - if
215
216                 // Return the status
217                 return $exists;
218         }
219
220         /**
221          * Checks if supplied password hash in request matches with the stored in
222          * database.
223          *
224          * @param       $requestInstance        A Requestable class instance
225          * @return      $matches                        Whether the supplied password hash matches
226          */
227         public function ifPasswordHashMatches (Requestable $requestInstance) {
228                 // By default nothing matches... ;)
229                 $matches = FALSE;
230
231                 // Is a previous result there?
232                 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
233                         // Get a UserDatabaseWrapper instance
234                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
235
236                         // Create a search criteria
237                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
238
239                         // Add the username as a criteria and set limit to one entry
240                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
241                         $criteriaInstance->setLimit(1);
242
243                         // Get a search result
244                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
245
246                         // Set the index "solver"
247                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
248
249                         // And finally set it
250                         $this->setResultInstance($resultInstance);
251                 } // END - if
252
253                 // Rewind it and advance to first entry
254                 $this->getResultInstance()->rewind();
255
256                 // This call set the result instance to a clean state
257                 $this->getResultInstance()->next();
258
259                 // Search for it
260                 if ($this->getResultInstance()->find('pass_hash')) {
261                         // So does the hashes match?
262                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
263                         $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
264                 } // END - if
265
266                 // Return the status
267                 return $matches;
268         }
269
270         /**
271          * "Getter" for user's password hash
272          *
273          * @return      $passHash       User's password hash from database result
274          */
275         public final function getPasswordHash () {
276                 // Default is missing password hash
277                 $passHash = NULL;
278
279                 // Get a database entry
280                 $entry = $this->getDatabaseEntry();
281
282                 // Is the password hash there?
283                 if (isset($entry['pass_hash'])) {
284                         // Get it
285                         $passHash = $entry['pass_hash'];
286                 } // END - if
287
288                 // And return the hash
289                 return $passHash;
290         }
291
292         /**
293          * Getter for primary key value
294          *
295          * @return      $primaryValue   Value of the primary key based on database type
296          */
297         public final function getPrimaryKey () {
298                 // Get a user database wrapper
299                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
300
301                 // Get the primary key back from the wrapper
302                 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
303
304                 // Get that field
305                 $primaryValue = $this->getField($primaryKey);
306
307                 // Return the value
308                 return $primaryValue;
309         }
310
311         /**
312          * Updates a given field with new value
313          *
314          * @param       $fieldName              Field to update
315          * @param       $fieldValue             New value to store
316          * @return      void
317          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
318          */
319         public function updateDatabaseField ($fieldName, $fieldValue) {
320                 // Get a critieria instance
321                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
322
323                 // Add search criteria
324                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
325                 $searchInstance->setLimit(1);
326
327                 // Now get another criteria
328                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
329
330                 // Add criteria entry which we shall update
331                 $updateInstance->addCriteria($fieldName, $fieldValue);
332
333                 // Add the search criteria for searching for the right entry
334                 $updateInstance->setSearchInstance($searchInstance);
335
336                 // Set wrapper class name
337                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
338
339                 // Remember the update in database result
340                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
341         }
342
343         /**
344          * Checks whether the user status is 'confirmed'
345          *
346          * @return      $isConfirmed    Whether the user status is 'confirmed'
347          */
348         public function isConfirmed () {
349                 // Determine it
350                 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
351
352                 // Return it
353                 return $isConfirmed;
354         }
355
356         /**
357          * Checks whether the user status is 'guest'
358          *
359          * @return      $isGuest        Whether the user status is 'guest'
360          */
361         public function isGuest () {
362                 // Determine it
363                 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));
364
365                 // Return it
366                 return $isGuest;
367         }
368
369 }