3 * handles CMS/WWW related functions regarding user management & registration.
4 * inherits from the Users class. The methods of this class have to be rewritten according to the CMS's functionality that you wish to use.
5 * The drupal_module has a webusers class of its own in the module itself.
6 * @author Daan Janssens, mentored by Matthew Lagoe
8 class WebUsers
extends Users
{
10 private $uId; /**< The user id */
11 private $login; /**< The username */
12 private $email; /**< The email address */
13 private $firstname; /**< The users first name */
14 private $lastname; /**< The users last name */
15 private $gender; /**< The gender */
16 private $country; /**< 2 letter word matching the country of the user */
17 private $receiveMail; /**< configuration regarding if the user wants to receive email notifications or not. */
18 private $language; /**< Language of the user */
23 * loads the object with the UID, if none is given it will use 0.
24 * @param $UId the UID of the user you want to instantiate.
26 function __construct($UId = 0) {
32 * sets the object's attributes.
33 * @param $values should be an array.
35 public function set($values){
36 $this->uId
= $values['UId'];
37 $this->login
= $values['Login'];
38 $this->email
= $values['Email'];
39 $this->firstname
= $values['FirstName'];
40 $this->lastname
= $values['LastName'];
41 $this->gender
= $values['Gender'];
42 $this->country
= $values['Country'];
43 $this->receiveMail
= $values['ReceiveMail'];
44 $this->language
= $values['Language'];
49 * function that checks if a username exists already or not.
50 * This function overrides the function of the base class.
51 * @param $username the username in question
52 * @return string Info: Returns 0 if the user is not in the web db, else a positive number is returned.
54 protected function checkUserNameExists($username){
55 $dbw = new DBLayer("web");
56 return $dbw->select("ams_user", array('name' => $username), "Login = :name")->rowCount();
61 * function that checks if a email exists already or not.
62 * This function overrides the function of the base class.
63 * @param $email the email address in question.
64 * @return string Info: Returns 0 if the email address is not in the web db, else a positive number is returned.
66 protected function checkEmailExists($email){
67 $dbw = new DBLayer("web");
68 return $dbw->select("ams_user" ,array('email' => $email),"Email = :email")->rowCount();
73 * check if the login username/email and password match the db.
74 * @param $value the inserted username or email
75 * @param $password the inserted password (unhashed)
76 * @return the logged in user's db row as array if login was a success, else "fail" will be returned.
78 public static function checkLoginMatch($value,$password){
80 $dbw = new DBLayer("web");
81 $statement = $dbw->select("ams_user", array('value' => $value),"Login=:value OR Email=:value");
82 $row = $statement->fetch();
83 if ($row['Password'][0] == '$')
85 $salt = substr($row['Password'], 0, 19);
89 $salt = substr($row['Password'], 0, 2);
91 $hashed_input_pass = crypt($password, $salt);
92 if($hashed_input_pass == $row['Password']){
101 * returns the id for a given username
102 * @param $username the username
103 * @return the user's id linked to the username
105 public static function getId($username){
106 $dbw = new DBLayer("web");
107 $statement = $dbw->select("ams_user", array('username' => $username), "Login=:username");
108 $row = $statement->fetch();
114 * returns the id for a given emailaddress
115 * @param $email the emailaddress
116 * @return the user's id linked to the emailaddress
118 public static function getIdFromEmail($email){
119 $dbw = new DBLayer("web");
120 $statement = $dbw->select("ams_user", array('email' => $email), "Email=:email");
121 $row = $statement->fetch();
131 * get uId attribute of the object.
133 public function getUId(){
139 * get login attribute of the object.(username)
141 public function getUsername(){
142 $dbw = new DBLayer("web");
143 if(! isset($this->login
) ||
$this->login
== ""){
144 $statement = $dbw->select("ams_user", array('id' => $this->uId
), "UId=:id");
145 $row = $statement->fetch();
153 * get email attribute of the object.
155 public function getEmail(){
156 $dbw = new DBLayer("web");
157 if(! isset($this->email
) ||
$this->email
== ""){
158 $statement = $dbw->select("ams_user", array('id' => $this->uId
), "UId=:id");
159 $row = $statement->fetch();
166 * get the hashed password
168 public function getHashedPass(){
169 $dbw = new DBLayer("web");
170 $statement = $dbw->select("ams_user", array('id' => $this->uId
), "UId=:id");
171 $row = $statement->fetch();
172 return $row['Password'];
177 * get basic info of the object.
178 * @return returns an array in the form of Array('FirstName' => $this->firstname, 'LastName' => $this->lastname, 'Gender' => $this->gender, 'Country' => $this->country, 'ReceiveMail' => $this->receiveMail)
180 public function getInfo(){
181 $dbw = new DBLayer("web");
182 if(! (isset($this->firstname
) && isset($this->lastname
) && isset($this->gender
) && isset($this->country
) && isset($this->receiveMail
) ) ||
183 $this->firstname
== "" ||
$this->lastname
== "" ||
$this->gender
== "" ||
$this->country
== "" ||
$this->receiveMail
== ""){
184 $statement = $dbw->select("ams_user", array('id' => $this->uId
), "UId=:id");
185 $row = $statement->fetch();
188 $result = Array('FirstName' => $this->firstname
, 'LastName' => $this->lastname
, 'Gender' => $this->gender
, 'Country' => $this->country
, 'ReceiveMail' => $this->receiveMail
);
194 * get receiveMail attribute of the object.
196 public function getReceiveMail(){
197 $dbw = new DBLayer("web");
198 if(! isset($this->receiveMail
) ||
$this->receiveMail
== ""){
199 $statement = $dbw->select("ams_user", array('id' => $this->uId
), "UId=:id");
200 $row = $statement->fetch();
203 return $this->receiveMail
;
208 * get language attribute of the object.
210 public function getLanguage(){
211 $dbw = new DBLayer("web");
212 if(! isset($this->language
) ||
$this->language
== ""){
213 $statement = $dbw->select("ams_user", array('id' => $this->uId
), "UId=:id");
214 $row = $statement->fetch();
217 return $this->language
;
222 * check if the user is logged in.
223 * @return true or false
225 public static function isLoggedIn(){
226 if(isset($_SESSION['user'])){
234 * update the password.
235 * update the password in the shard + update the password in the www/CMS version.
236 * @param $user the username
237 * @param $pass the new password.
238 * @return ok if it worked, if the lib or shard is offline it will return liboffline or shardoffline.
240 public static function setPassword($user, $pass){
242 $hashpass = crypt($pass, WebUsers
::generateSALT());
243 $reply = WebUsers
::setAmsPassword($user, $hashpass);
244 $values = Array('Password' => $hashpass);
246 //make connection with and put into shard db
247 $dbw = new DBLayer("web");
248 $dbw->update("ams_user", $values,"Login = '$user'");
250 catch (PDOException
$e) {
251 //ERROR: the web DB is offline
258 * update the emailaddress.
259 * update the emailaddress in the shard + update the emailaddress in the www/CMS version.
260 * @param $user the username
261 * @param $mail the new emailaddress.
262 * @return ok if it worked, if the lib or shard is offline it will return liboffline or shardoffline.
264 public static function setEmail($user, $mail){
265 $reply = WebUsers
::setAmsEmail($user, $mail);
266 $values = Array('Email' => $mail);
268 //make connection with and put into shard db
269 $dbw = new DBLayer("web");
270 $dbw->update("ams_user", $values, "Login = '$user'");
272 catch (PDOException
$e) {
273 //ERROR: the web DB is offline
280 * update the setReceiveMail value in the db.
281 * update the receiveMail in the www/CMS version.
282 * @param $user the username
283 * @param $receivemail the receivemail setting .
285 public static function setReceiveMail($user, $receivemail){
286 $values = Array('Receivemail' => $receivemail);
288 //make connection with and put into shard db
289 $dbw = new DBLayer("web");
290 $dbw->update("ams_user", $values, "UId = $user" );
292 catch (PDOException
$e) {
293 //ERROR: the web DB is offline
299 * update the language value in the db.
300 * update the language in the www/CMS version.
301 * @param $user the username
302 * @param $language the new language value.
304 public static function setLanguage($user, $language){
305 $values = Array('Language' => $language);
307 //make connection with and put into shard db
308 $dbw = new DBLayer("web");
309 $dbw->update("ams_user", $values, "UId = $user");
311 catch (PDOException
$e) {
312 //ERROR: the web DB is offline
319 * @return return an array of users
321 public function getUsers(){
322 $dbl = new DBLayer("web");
323 $data = $dbl->executeWithoutParams("SELECT * FROM ams_user");
329 * return the query that should get all users.
330 * @return string: the query to receive all users.
332 public static function getAllUsersQuery(){
333 return "SELECT * FROM ams_user";
339 * it will set the language matching to the language cookie setting and add it to the www/CMS's DB.
340 * @param $name the username
341 * @param $pass the unhashed password
342 * @param $mail the email address
344 public static function createWebuser($name, $pass, $mail){
346 //register account with the correct language (check if cookie is already set)!
347 if ( isset( $_COOKIE['Language'] ) ) {
348 $lang = $_COOKIE['Language'];
350 global $DEFAULT_LANGUAGE;
351 $lang = $DEFAULT_LANGUAGE;
354 $values = Array('Login' => $name, 'Password' => $pass, 'Email' => $mail, 'Language' => $lang);
357 $dbw = new DBLayer("web");
358 return $dbw->executeReturnId("ams_user", $values);
360 catch (PDOException
$e) {
361 //ERROR: the web DB is offline