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