Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
class user
{
private $conn;
public $username;
public $id;
public $table;
public function __call($name, $arguments)/* here this $arguments is of default enumarated array function which
consists of the passed parameters so if you wanted to call a value where this is saved just call or use like
exactly how you treat the array $arguments[0],[1] and soooo onnnn */
{
//$name = "getBio";
$property = preg_replace("/[^0-9a-zA-Z]/", "", substr($name, 3));
//this regex removes the get and returns the remaining with the help of substr function which actually
//has to be provided with a string input,offset(where to start),length in our func above it removes first three letters
$property = strtolower(preg_replace('/\B([A-Z])/', '_$1', $property));
//this regex converts camelcase to sanke case eg HarishRagavenDhar=>harish_ragaven_dhar;
if(substr($name, 0, 3) == "get") {
return $this->_get_data($property);
} elseif(substr($name, 0, 3) == "set") {
return $this->_set_data($property, $arguments);
} else {
throw new Exception("No such function is available");
}
}
public static function validate($user, $email, $pass, $phone)//signup page
{
$conn = database::getconnetion();
$options = [
'cost' => 9
];
$pass = password_hash($pass, PASSWORD_DEFAULT, $options);
$sql = "INSERT INTO `user_data` (`Username`, `Email`, `Password`, `phone`,`blocked`,`active`)
VALUES ('$user', '$email', '$pass', '$phone','0','1')";
$result = false;
if ($conn->query($sql) === true) {
$result = true;
} else {
print("Error: " . $sql . "<br>" . $conn->error) ;
$result = false;
}
$conn->close();
return $result;
}
public static function login($user, $pass)//login
{
$conn = database::getconnetion();
// $pass=password_verify($pass);
$sql = "SELECT*FROM user_data WHERE Username='$user'";
// echo $sql;
$result = $conn->query($sql);
if($result->num_rows === 1) {
$row = $result->fetch_assoc();
//if($row['Username']==$user and $row['Password']==$pass)
if(password_verify($pass, $row['Password'])) {
return $row['Username'];
} else {
return false;
}
}
}
//in __construct we are fetching the username using username and the Id allotted to that user from the userSession
public function __construct($username)
{
$this->table = 'user_data';
$this->conn = database::getconnetion();
$this->username = $username;
$sql = "SELECT*FROM user_data WHERE `Username`='$username'OR`ID`='$username'";
// in the OR class the $uid from the userSession.class will will be there instead of username
//(i.e) ID = $username($this->uid) eventually we coul fetch the user from user_data table
//print($sql);
$result = $this->conn->query($sql);
if($result->num_rows === 1) {
$row = $result->fetch_assoc();
$this->id = $row['ID'];
// return $this->id;
// print_r($row['ID']);
} //else {
// throw new Exception("invalid username");
// }
}
private function _set_data($variable, $data)
{
if(!$this->conn) {
$this->conn = database::getconnetion();
}
TODO:/* They have replaced user_personal with users if needed that change has to be done*/
$sql = "UPDATE '$this->table' SET $variable ='$data[0]' WHERE ID ='$this->id' " ;
if($this->conn->query($sql)) {
return true;
} else {
return false;
}
}
private function _get_data($variable)
{
if(!$this->conn);
{
$this->conn = database::getconnetion();
}
$sql = "SELECT $variable FROM '$this->table' WHERE ID='$this->id'";
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
$result = $this->conn->query($sql);
if($result->num_rows === 1) {
$row = $result->fetch_assoc();
return $row[$variable];
}
}
public function getUsername()
{
return $this->username;
}
public function setdob($year, $month, $day)
{
return $this->_set_data('dob', $year, $month, $day);
}
public function getDob()
{
return $this->_get_data('dob');
}
// public function setBio($bio)
// {
// return $this->_set_data('bio', $bio);
// }
// public function getBio()
// {
// return $this->_get_data('bio');
// }
// public function setAvatar($avatar)
// {
// return $this->_set_data('avatar', $avatar);
// }
// public function getAvatar()
// {
// return $this->_get_data('avatar');
// }
// public function setFirstname($first)
// {
// return $this->_set_data('firstname', $first);
// }
// public function getFirstname()
// {
// return $this->_get_data('firstname');
// }
// public function setLastname($last)
// {
// return $this->_set_data('lastname', $last);
// }
// public function getLastname()
// {
// return $this->_get_data('lastname');
// }
// public function setInstagram($insta)
// {
// return $this->_set_data('instagram', $insta);
// }
// public function getInstagram()
// {
// return $this->_get_data('instagram');
// }
// public function setFacebook($fb)
// {
// return $this->_set_data('facebook', $fb);
// }
// public function getFacebook()
// {
// return $this->_get_data('facebook');
// }
// public function setTwitter($tweet)
// {
// return $this->_set_data('twitter', $tweet);
// }
// public function getTwitter()
// {
// return $this->_get_data('twitter');
// }
}