-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthManager.class.php
More file actions
executable file
·358 lines (295 loc) · 10.8 KB
/
Copy pathAuthManager.class.php
File metadata and controls
executable file
·358 lines (295 loc) · 10.8 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/*
Licence-Terms
===============
This project is protected by Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported licence
Please read the online version of this licence here:
http://creativecommons.org/licenses/by-nc-sa/3.0/
An offline copy is in the root folder of this project by the name "licence.pdf"
This licence is to keep the commercial fishes off the waters. Don't be disheartened. I am always an email away :)
Mail me at: prashant.dwivedi@outlook.com
*/
?>
<?php
// csbees/auth/AuthManager.class.php
// =================================
require_once('includes.php');
class AuthManager
{ public $socialuser;
public $db;
public $fbClassObj;
public $newUserSignupFlag;
public function __construct()
{ $this->fbClassObj = new SocialFB();
if(!($this->db = new DB()))
die('<br/>Oops! Something went wrong! Try refreshing the browser or closing and reopening it again!<br/>');
//try to retrieve previously saved user from session
if(isset($_SESSION['user']))
{ //echo 'constructor: got session user';
$this->socialuser = $_SESSION['user'];
//if profile was changed in previous request, update it from the database.
if($this->socialuser->profileNeedsRefresh())
{ $type = $this->socialuser->getUserType();
if($type != $this->generateNewSocialUser($type))
{ $error .= '<li>Sorry! Something went wrong with your session.</li>
<li>Try logging out and relogin again.</li>';
}
//echo '<br/><br/>authman constructor Generating socialuser';
$this->socialuser = $this->generateNewSocialUser($type);
}
}
else
{ //echo 'constructor: got guest user';
//construct a guest user, as passing nothing in generateNewUser will cause it to create default user (guest)
//echo 'creating guest user';
$this->generateNewSocialUser(null);
}
}
public function announceNewUploadPublicly($uploadtitle, $link)
{ if($this->socialuser->getUserType()=="fb")
{ $data = array(
'message' => 'I uploaded "'. $uploadtitle .'" on CSBEES! Click the link below to access it',
'name' => 'CSBEES',
'link' => $link,
'description'=> 'CSBEES helps u to download & create digital backup of your notes + a lot more!',
'picture' => __FB_POSTS_CSBEES_ICON, //'http://csbees.neurals.in/images/class_notes.jpg',
'caption' => 'Class Notes Management & Sharing Portal | CSBEES',
);
$this->fbClassObj->publishOnWall($data);
//die('-------new user flag true--------');
}
}
public function announceNewUserPublicly()
{ if($this->newUserSignupFlag==TRUE)
{
if($this->socialuser->getUserType()=="fb")
{ $data = array(
'message' => 'I use CSBEES :)',
'name' => 'CSBEES',
'link' => 'csbees.neurals.in',
'description'=> 'CSBEES is a network of students and professors who share classroom notes!',
'picture' => __FB_POSTS_CSBEES_ICON, //'http://csbees.neurals.in/images/class_notes.jpg',
'caption' => 'Class Notes Anytime | CSBEES',
);
$this->fbClassObj->publishOnWall($data);
//die('-------new user flag true--------');
}
}
//die('from newUserSignedUP() :D');
$this->newUserFlag=FALSE;
}
//this function sets into $this->socialuser, a new user of type defined by $type (default is guest user)
public function generateNewSocialUser($type)
{ $this->socialuser = null;
//session_unset();
//session_destoy();
switch($type)
{ case "fb": $user_profile = $this->getProfileOfFBUserLoggedin();
//echo '<br/><br/>generateNewSocialUser:: user_profile (fb) = ';
//var_dump($user_profile);
if($user_profile['loggedIn'])
{ $user_profile['type'] = "fb";
$dbprofile = $this->db->manageUserLogin($user_profile);
//print_r($dbprofile);
//die('inside generateNewSocialUser--type=fb');
if($dbprofile['newUser'])
{ $this->newUserSignupFlag = TRUE;
if($dbprofile['error'] == TRUE)
$user_profile['msg'] .= '<li>Error in registration! Please try again.</li>';
else
$user_profile['msg'] .= '<li>Welcome! Please complete your profile <a href="'.__PROFILE_PAGE_URL.'">here</a> for a better experience!</li>';
}
$user_profile['name'] = $dbprofile['name'];
$user_profile['college'] = $dbprofile['college'];
$user_profile['semester'] = $dbprofile['semester'];
$user_profile['branch'] = $dbprofile['branch'];
$user_profile['course'] = $dbprofile['course'];
$user_profile['reputation'] = $dbprofile['reputation'];
$user_profile['userTagsList'] = $dbprofile['userTagsList'];
/*echo '<br/><br/>AuthMan.php in generateSocialUser: $dbprofile::<br/>';
var_dump($dbprofile);
echo '<br/><br/>AuthMan.php in generateSocialUser: $user_profile::<br/>';
var_dump($user_profile);
*/
$this->socialuser = new SocialUser($user_profile);
//echo '<br/><br/>AuthMan.php in Generated SocialUser: $$this->socialuser::<br/>';
//var_dump($dbprofile);
}
else
{ //echo '<br/><br/>inside generateNewSocialUser: getProfileOfFBUserLoggedin() returns: '.var_dump($user_profile);
//die();
return FALSE;
}
break;
default: $params = array (
'fbloginURL' => $this->fbClassObj->getFBLoginURL()
);
//echo 'default user creating';
$this->socialuser = SocialUser::generateGuestUser($params);
break;
}
$this->db->updateLog($this->socialuser->getRefID());
if($this->newUserSignupFlag==TRUE)
{ $this->announceNewUserPublicly();
}
$_SESSION['user'] = &$this->socialuser;
return $type;
}
public function getProfileOfFBUserLoggedin()
{ $user_profile = $this->fbClassObj->getFBUser();
//only valid for simulating a facebook login while in development mode!
/*if(__SIMULATE_FB_LOGIN)
{ $user_profile['loggedIn'] = TRUE;
$user_profile['refID'] = __SIMULATE_FB_LOGIN;
}
*/
if($user_profile['loggedIn'] == TRUE)
return $user_profile;
else
return FALSE;
}
public function logoutCurrentUser($type)
{ switch($type)
{ case "fb": $this->fbClassObj->logout();
break;
}
//unset($_SESSION);
session_unset();
session_destroy();
//session_start();
}
public function manageSocialLogin($type)
{ $this->socialuser = null;
//url where user will be redirected after this function
$redirectURL = 'unknown';
//create new social user of type $type.. if this method returns false, user of asked $type is not logged in! in that case, redirect to login page to choose login option
if( $this->generateNewSocialUser($type)!= $type)
{ //login failed.. redirecting to login page with error message
//$this->logoutCurrentUser($type);
$_SESSION['loginError'] = 'invalid login';
$redirectURL = __LOGIN_PAGE_URL; //'/login.php';
}
else
{ //user has successfully logged in.. send him to his destined page..
$redirectURL = __INDEX_PAGE_URL; '/index.php?loggedIn';
}
//echo '<br/><br/><br/><br/>After processing, socialuser object = <br/>'.var_dump($this->socialuser);
//echo '<br/><br/><br/>returned type is: '.var_dump($type);
//echo '<br/><br/>and session variable=<br/>'.var_dump($_SESSION);
//die('<br/>by manageSocialLogin, you are being redirected to: <a href="'.$redirectURL .'">'.$redirectURL.'</a>');
header('Location: '.$redirectURL);
}
public function verifySocialUserLoggedIn($type)
{ switch($type)
{ case "fb": if($this->fbClassObj->verifyFBUserLoggedIn($this->socialuser->getRefID()))
return TRUE;
}
return FALSE;
}
public function isUserLoggedIn()
{ if($this->socialuser->isloggedIn() == TRUE)
{ if($this->verifySocialUserLoggedIn($this->socialuser->getUserType()))
return TRUE;
}
//in all other casese return false
return FALSE;
}
public function restrictedPage($redirectUrl, $accessLevel=null)
{
if($this->isUserLoggedIn())
{ // user is safely logged in
if($accessLevel!=null)
if($this->socialuser->getUserAccessLevel() == $accessLevel)
return TRUE;
}
else
{ // not logged in.. redirect to the url given
//die('<br/>AuthMan::restrictedPage()- Restricted area.. redirecting to <a href="'. $redirectUrl .'">here</a>' );
header('Location: '.$redirectUrl);
}
}
public function profileEditor($newDetails)
{ if($this->socialuser->isloggedIn())
{ $error = $this->db->checkUsernameExists($newDetails['name'], $this->socialuser->getRefID());
if($error)
{ $result['error'] = '<li>Username Already Taken. Try Something Different!</li>';
return $result;
}
//echo '<userId=> '.$this->socialuser->getRefID();
$success = $this->db->updateUser($this->socialuser->getRefID(), $newDetails);
if($success)
{ $msg .= '<li>Profile Updated!</li>
';
$type = $this->socialuser->getUserType();
//echo '<br/>user type= '.$type;
if($type != $this->generateNewSocialUser($type))
{ $error .= '<li>Sorry! Something went wrong with your session.</li>
<li>Try logging out and relogin again.</li>';
}
}
else
{ $error .= '<li>Sorry! Something went wrong while updating! Please try again or contact us.</li>';
}
$result['error']=$error;
$result['msg'] = $msg;
return $result;
}
}
public function userTagsEditor($tagsIDArray)
{ $msg; $error=FALSE;
if($this->socialuser->isloggedIn())
{
if($this->db->setUserTags($this->socialuser->getRefID(), $tagsIDArray))
{ $msg .= '<li>Subscription Tags Successfully Updated!</li>';
$type = $this->socialuser->getUserType();
if($type != $this->generateNewSocialUser($type))
{ $msg .= '<li>Sorry! Something went wrong with your session.</li>
<li>Try logging out and relogin again.</li>';
$error = TRUE;
}
}
else
{ $msg .= '<li>Sorry! Error updating your Tags.</li>
<li>Please try again or contact us.</li>';
$error = TRUE;
}
//$this->socialuser->forceProfileRefresh(True);
}
$result['error']=$error;
$result['msg'] = $msg;
return $result;
}
/*
public function retrieveUser($type)
{ if($this->socialUser->isloggedIn() == FALSE)
{ if($this->socialuser == null)
{ $profile = new SocialFB();
$this->socialuser = new SocialUser();
$refID = $profile['refID'];
if( ($this->db = new DB()) )
{ $profile['DBValues'] = $this->db->getUserDetails($refID);
}
}
$_SESSION['user'] = $this->socialuser;
}
return $this->socialuser;
}
//generic function to retrieve the type of user defined by argument..by default, it is facebook user
public function retrieveSocialUser($type="fb")
{ if(!$this->socialuser)
{ if(isset($_SESSION['user']))
{ $this->socialuser = $_SESSION['user'];
if(!isset($this->socialuser->dbprofile))
{ $refID = $profile['refID'];
if( ($this->db = new DB()) )
{ $profile['DBValues'] = $this->db->getUserDetails($refID);
}
}
}
else
{ return FALSE
}
}
}
*/
}