]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/user/class_BaseUser.php
715544eb65bb635dd29c71dcd09a61288ed3c561
[shipsimu.git] / inc / classes / main / user / class_BaseUser.php
1 <?php
2 /**
3  * A general user class 
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 BaseUser extends BaseFrameworkSystem {
25         /**
26          * Username of current user
27          */
28         private $userName = "";
29
30         /**
31          * Email of current user
32          */
33         private $email = "";
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48         }
49
50         /**
51          * Setter for username
52          *
53          * @param       $userName       The username to set
54          * @return      void
55          */
56         public final function setUserName ($userName) {
57                 $this->userName = $userName;
58         }
59
60         /**
61          * Setter for email
62          *
63          * @param       $email  The email to set
64          * @return      void
65          */
66         protected final function setEmail ($email) {
67                 $this->email = $email;
68         }
69
70         /**
71          * Getter for username
72          *
73          * @return      $userName       The username to get
74          */
75         public final function getUsername () {
76                 return $this->userName;
77         }
78
79         /**
80          * Getter for email
81          *
82          * @return      $email  The email to get
83          */
84         public final function getEmail () {
85                 return $this->email;
86         }
87
88         /**
89          * Determines wether the username exists or not
90          *
91          * @return      $exists         Wether the username exists
92          */
93         public function ifUsernameExists () {
94                 // By default the username does not exist
95                 $exists = false;
96
97                 // Is a previous result there?
98                 if (is_null($this->getResultInstance())) {
99                         // Get a UserDatabaseWrapper instance
100                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
101
102                         // Create a search criteria
103                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
104
105                         // Add the username as a criteria and set limit to one entry
106                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
107                         $criteriaInstance->setLimit(1);
108
109                         // Get a search result
110                         $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
111                 } else {
112                         // Rewind it
113                         $this->getResultInstance()->rewind();
114                 }
115
116                 // Search for it
117                 if ($this->getResultInstance()->next()) {
118                         // Entry found
119                         $exists = true;
120                 } // END - if
121
122                 // Return the status
123                 return $exists;
124         }
125
126         /**
127          * Determines wether the email exists or not
128          *
129          * @return      $exists         Wether the email exists
130          */
131         public function ifEmailAddressExists () {
132                 // By default the email does not exist
133                 $exists = false;
134
135                 // Is a previous result there?
136                 if (is_null($this->getResultInstance())) {
137                         // Get a UserDatabaseWrapper instance
138                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
139
140                         // Create a search criteria
141                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
142
143                         // Add the username as a criteria and set limit to one entry
144                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
145                         $criteriaInstance->setLimit(1);
146
147                         // Get a search resultInstance
148                         $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
149                 } else {
150                         // Rewind it
151                         $this->getResultInstance()->rewind();
152                 }
153
154                 // Search for it
155                 if ($this->getResultInstance()->next()) {
156                         // Entry found
157                         $exists = true;
158
159                         // Is the username set?
160                         if ($this->getUserName() == "") {
161                                 // Get current entry
162                                 $currEntry = $this->getResultInstance()->current();
163
164                                 // Set the username
165                                 $this->setUserName($currEntry['username']);
166                         } // END - if
167                 } // END - if
168
169                 // Return the status
170                 return $exists;
171         }
172
173         /**
174          * Checks if the supplied password hash in request matches with the stored
175          * in database.
176          *
177          * @param       $requestInstance        A requestable class instance
178          * @return      $matches                        Wether the supplied password hash matches
179          */
180         public function ifPasswordHashMatches (Requestable $requestInstance) {
181                 // By default nothing matches... ;)
182                 $matches = false;
183
184                 // Get a UserDatabaseWrapper instance
185                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
186
187                 // Create a search criteria
188                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
189
190                 // Add the username as a criteria and set limit to one entry
191                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
192                 $criteriaInstance->setLimit(1);
193
194                 // Get a search resultInstance
195                 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
196
197                 // Search for it
198                 if ($this->getResultInstance()->next()) {
199                         // Get the current entry (can only be one!)
200                         $entry = $this->getResultInstance()->current();
201
202                         // So does the hashes match?
203                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
204                         $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
205                 } // END - if
206
207                 // Return the status
208                 return $matches;
209         }
210
211         /**
212          * "Getter" for user's password hash
213          *
214          * @return      $passHash       User's password hash from database result
215          */
216         public final function getPasswordHash () {
217                 // Default is missing password hash
218                 $passHash = null;
219
220                 // Get a database entry
221                 $entry = $this->getDatabaseEntry();
222
223                 // Is the password hash there?
224                 if (isset($entry['pass_hash'])) {
225                         // Get it
226                         $passHash = $entry['pass_hash'];
227                 } // END - if
228
229                 // And return the hash
230                 return $passHash;
231         }
232
233         /**
234          * Getter for primary key value
235          *
236          * @return      $primaryValue   Value of the primary key based on database type
237          */
238         public final function getPrimaryKey () {
239                 // Get a user database wrapper
240                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
241
242                 // Get the primary key back from the wrapper
243                 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
244
245                 // Get that field
246                 $primaryValue = $this->getField($primaryKey);
247
248                 // Return the value
249                 return $primaryValue;
250         }
251 }
252
253 // [EOF]
254 ?>