-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.php
286 lines (284 loc) · 12.9 KB
/
user.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
class user {
public $userName, $userEmail, $userAvatar, $userPermission, $verifiedBy, $externalUserID, $greeted, $userScreenName, $internalUserId;
function __construct( $userName, $verifiedBy, $externalUserID = null, $eMail = null, $token = null, $secret = null, $avatarURL = null, $userScreenName = null) {
$getUser = new PDO(__DB__CONNECT__STRING, __DB__USER, __DB__PASSWORD);
$skip = false;
$cookieId = false;
switch($verifiedBy){
case 'twitter':
$getUserSql = "SELECT
userId,
userName,
userScreenName,
userAvatar,
userEmail,
userGreeted,
userPermission
FROM
tblUsers
WHERE
twitterId = :userId;";
$newUserSQL = "INSERT INTO
tblUsers
(userName, userRegistered, twitterId, userAvatar, userScreenName)
VALUES
(:userName, :userRegistered, :externalId, :avatarURL, :userScreenName)";
if(isset($_COOKIE['logon']['facebook'])){
$updateUserSQL .= " facebookId = :cookieId";
$cookieId = $_COOKIE['logon']['facebook'];
}
setcookie('logon[twitter]', $externalUserID, time()+90*24*60*60);
break;
case 'facebook':
$getUserSql = "SELECT
userId,
userName,
userScreenName,
userAvatar,
userEmail,
userGreeted,
userPermission
FROM
tblUsers
WHERE
facebookId = :userId;";
$newUserSQL = "INSERT INTO
tblUsers
(userName, userRegistered, facebookId, userAvatar, userScreenName)
VALUES
(:userName, :userRegistered, :externalId, :avatarURL, :userScreenName)";
if(isset($_COOKIE['logon']['twitter'])){
$updateUserSQL .= " twitterId = :cookieId";
$cookieId = $_COOKIE['logon']['twitter'];
}
setcookie('logon[facebook]', $externalUserID, time()+90*24*60*60);
break;
case 'organic':
setcookie('organicId', $userName, time()+90*24*60*60);
setcookie('sessionKey', md5($_SERVER["REMOTE_ADDR"]) , time()+90*24*60*60);
$skip = true;
break;
}
if(!$skip){
$userDetails = $getUser->prepare($getUserSql);
$userDetails->execute(array(':userId' => $externalUserID));
$user = $userDetails->fetch();
if($user){
$this->verifiedBy = $verifiedBy;
$this->greeted = $user['userGreeted'];
$this->userName = $user['userName'];
$this->userScreenName = $dataArray['userScreenName'];
$this->userPermission = $user['userPermission'];
$this->internalUserId = $user['userId'];
setcookie('userId', $user['userId'], time()+90*24*60*60);
setcookie('loggedIn', uniqid(time(), true) );
$updateCookie = $getUser->prepare("INSERT INTO tblSessions (userId, sessionKey) VALUES (".$this->internalUserId .",". strip_tags($_COOKIE['loggedIn']) . ")");
$updateCookie->execute();
if($verifiedBy == 'twitter'){
//User is relogging in manually, so we need to store new keys
require_once('encrypt.php');
$crypt = new proCrypt();
$tokenSQL = "INSERT INTO
tblOAuth
(oAuthKey, oAuthSecret, userId)
VALUES
(:key, :secret, :id)
ON DUPLICATE KEY UPDATE
oAuthKey = :OAKey, oAuthSecret = :OASecret";
$encSecret = $crypt->encrypt($secret);
$encToken = $crypt->encrypt($token);
$writeTokens = $getUser->prepare($tokenSQL);
$writeTokens->execute(array(':key' => $encToken, ':secret' => $encSecret, ':id' => $this->internalUserId, ':OAKey' => $encToken, ':OASecret' => $encSecret));
//write tokens for twitter
}else{
$submitUser = $getUser->prepare($newUserSQL);
$user = $submitUser->execute(array (':userName' => $userName, ":userRegistered" => date('m-d-y', time()), ":externalId" => $externalUserID, ":avatarURL"=>$avatarURL, ":userScreenName" => $userScreenName ));
$id = $getUser->lastInsertId();
$this->greeted = false;
$this->userScreenName = $userScreenName;
$this->verifiedBy = $verifiedBy;
$this->userPermission = 1;
$this->userAvatar = $avatarURL ? $avatarURL : strtolower($verifiedBy);
setcookie('userId', $user['userId'], time()+90*24*60*60);
setcookie('loggedIn', uniqid(time(), true) );
$updateCookie = $getUser->prepare("INSERT INTO tblSessions (userId, sessionKey) VALUES (".$this->internalUserId .",.". strip_tags($_COOKIE['loggedIn']) . ")");
$updateCookie->execute();
if($verifiedBy == 'twitter'){
//User is relogging in manually, so we need to store new keys
require_once('encrypt.php');
$crypt = new proCrypt();
$tokenSQL = "INSERT INTO
tblOAuth
(oAuthKey, oAuthSecret, userId)
VALUES
(:key, :secret, :id)
ON DUPLICATE KEY UPDATE
oAuthKey = :OAKey, oAuthSecret = :OASecret";
$encSecret = $crypt->encrypt($secret);
$encToken = $crypt->encrypt($token);
$writeTokens = $getUser->prepare($tokenSQL);
$writeTokens->execute(array(':key' => $encToken, ':secret' => $encSecret, ':id' => $this->internalUserId, ':OAKey' => $encToken, ':OASecret' => $encSecret));
//write tokens for twitter
}
}
}else{
$this->greeted = false;
$this->userScreenName = $userScreenName;
$this->verifiedBy = $verifiedBy;
$this->userPermission = 1;
$this->userAvatar = $avatarURL ? $avatarURL : null;
$this->userEmail = $eMail;
$this->userName = $userScreenName;
$this->externalUserID = false;
}
}
}
private function checkUserExists(){
$userDetails = $getUser->prepare($getUserSql);
$userDetails->execute(array(':userId' => $externalUserID));
$user = $userDetails->fetch();
}
private function setDetails(){
$this->userAvatar = $user['userAvatar'];
$this->userName = $user['name'];
$this->userPermission = $user['permission'];
}
public function updateStatus($message){
}
public static function banUser($userName, $reason, $duration){
//TODO: Put something here
}
public static function getUserProfile($uid) {
$lookupSQL = "SELECT
tblUsers.userScreenName,
tblUsers.userName,
tblUsers.userURL,
tblUsers.userRegistered,
posts.count,
votes.upVotes,
votes.downVotes
FROM
tblUsers
LEFT JOIN (
SELECT
COUNT(tblContent.uniqueID) as count,
tblContent.postAuthor
FROM
tblContent
GROUP BY tblContent.postAuthor
)posts
ON posts.postAuthor= tblUsers.userId
LEFT JOIN
(
SELECT
voterId,
SUM(CASE WHEN tblVotes.voteType = 'up' THEN 1 ELSE 0 END) as upVotes,
SUM(CASE WHEN tblVotes.voteType = 'down' THEN 1 ELSE 0 END) as downVotes
FROM
tblVotes
GROUP BY
tblVotes.voterId
) votes
ON votes.voterId = tblUsers.userId
WHERE
tblUsers.userId = :user";
$getCard = new PDO(__DB__CONNECT__STRING, __DB__USER, __DB__PASSWORD);
$getUser = $getCard->prepare($lookupSQL);
$getUser->execute(array(':user'=>$uid));
while($r = $getUser->fetch()){
$user['name'] = $r['userScreenName'] ? $r['userScreenName'] : $r['userName'];
$user['registered'] = $r['userRegistered'];
$user['url'] = $r['userURL'];
$user['count'] = $r['count'];
}
return $user;
}
//end getUserCard
public static function getPostList($uid) {
$lookupSQL = "SELECT
tblUsers.userScreenName,
tblUsers.userName,
tblUsers.userURL,
tblUsers.userRegistered,
tblContent.postTitle,
tblContent.uniqueId
FROM
tblUsers
LEFT JOIN
tblContent
ON
tblUsers.userId=tblContent.postAuthor
WHERE
tblUsers.userId = :user";
$getList = new PDO("mysql:host=localhost;dbname=tweetf5_Combined", 'tweetf5_write', 'B4rgle99!');
$getPosts = $getList->prepare($lookupSQL);
$getPosts->execute(array(':user'=>$uid));
while($r = $getPosts->fetch()){
$post[]['postTitle'] = $r['postTitle'];
$post[]['postId'] = $r['uniqueId'];
}
}
public static function getUserAvatar($uid) {
$SQL = "SELECT
tblUsers.userAvatar,
tblOAuth.oAuthKey,
tblOAuth.oAuthSecret,
tblUsers.facebookId
FROM
tblUsers
LEFT JOIN
tblOAuth
ON
tblUsers.userId=tblOAuth.userId
WHERE
tblUsers.userId = :user";
$getAvatar = new PDO("mysql:host=localhost;dbname=tweetf5_Combined", 'tweetf5_write', 'B4rgle99!');
$findURL = $getAvatar->prepare($SQL);
$findURL->execute(array(':user'=>$uid));
while($r = $findURL->fetch()){
if(strtolower(substr($r['userAvatar'], 0, strlen(SITE_URL)))== SITE_URL){
$avatarURL = $r['userAvatar'];
}elseif($r['userAvatar'] == 'facebook'){
$avatarURL = "http://graph.facebook.com/{$r['facebookId']}/picture";
}elseif($r['userAvatar'] == 'twitter'){
//POLL API FOR AVATAR URL
require_once('twitter/twitteroauth/twitteroauth.php');
require_once('twitter/config.php');
require_once ('encrypt.php');
$crypt = new proCrypt();
$oAuthToken = trim($crypt->decrypt($r['oAuthKey']));
$oAuthSecret = trim($crypt->decrypt($r['oAuthSecret']));
$twitteroauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,$oAuthToken,$oAuthSecret);
$twitterUser = $twitteroauth->get('account/verify_credentials');
$avatarURL = $twitterUser->profile_image_url;
}else{
$avatarURL = "/images/blankly.jpg";
}
}
return $avatarURL;
}
//END getUserAvatar
public static function loginViaCookie($cookieId, $cookieKey){
$SQL = "
SELECT
tblUsers.userId,
tblUsers.userScreenName,
tblUsers.userAvatar
FROM
tblSessions
LEFT JOIN
tblSessions
ON tblSessions.sessionUser = tblUsers.userId
WHERE
tblSessions.sessionKey = :sessionKey
";
//TODO: Finish this
}
public static function checkIfUserIsLoggedIn() {
$user = $_SESSION['user'] ? $_SESSION['user'] : $_COOKIE['user'];
return (!!$user);
}
//END checkIfUserIsLoggedIn
}
?>