-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserObject.php
executable file
·476 lines (415 loc) · 14.9 KB
/
userObject.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/*
File: lib/userObject
Author: Nathan Davies
Created: 8th August 2007
Purpose: To hold the functions used to control access etc
Ammendment:
3rd July 2008 - ND - removing unrequired functions under the new CLO development structure
7th July 2008 - ND - added ensureUser()
20th August 2008 - ND - Added isModerator
*/
class userObject
{
var $loDataObject ;
var $lnUserID ;
var $llIsModerator ;
var $llIsAdministrator ;
var $lcEmail ;
var $lcDisplay ;
var $lcDisplayName ;
var $lcName ;
var $lcPostOrder ;
var $lnFoodID;
var $lnChurchID ;
var $lnLoginCount ;
var $ldDate ;
var $llNewUser ;
var $llReactivation ;
var $llUserNameChanged ;
var $lcAccess ;
var $lnBibleRef ;
// NewZapp API details
var $lcNZAPIPWord ;
var $lcNZUserName ;
var $lcNZCID ;
var $lcNZRequest ;
var $lcNZResponse ;
public function userObject($poDB)
{
$this->loDataObject = $poDB ;
$this->ldDate = date("Y-m-d") ;
}
function setUserSession($pcUserName, $pcPassword)
{
if(!empty($pcUserName) && !empty($pcPassword))
{
$lcUserName = $pcUserName ;
// hash the password ready for the data check
$lcPassword = $this->loDataObject->encryptData($pcPassword) ;
// database check
$llContinue = $this->checkForUserInDB($lcUserName, $lcPassword) ;
if($llContinue)
{
if (!empty($this->lnUserID))
{
// set some session variables
$_SESSION['userID'] = $this->lnUserID ;
$_SESSION['isModerator'] = $this->llIsModerator ;
$_SESSION['isAdmin'] = $this->llIsAdministrator ;
$_SESSION['email'] = $this->lcEmail;
$_SESSION['display'] = $this->lcDisplay ;
$_SESSION['name'] = $this->lcName ;
$_SESSION['postOrder'] = $this->lnPostOrder ;
$_SESSION['foodID'] = $this->lnFoodID ;
$_SESSION['churchID'] = $this->lnChurchID ;
$_SESSION['bibleRef'] = $this->lnBibleRef ;
$lnLoginCount = $this->lnLoginCount ;
$lnUserID = $this->lnUserID ;
$lcUpdateLoginHistory =
"UPDATE tbl_user
SET
tbl_user.lastLogin = now(),
tbl_user.loginCount = $lnLoginCount
WHERE tbl_user.ID = $lnUserID" ;
$this->loDataObject->iQuery($lcUpdateLoginHistory);
$_SESSION['hasUpdated'] = true;
$_SESSION['isLoggedIn'] = true;
}
}
else
{
$_SESSION['accessname'] = null ;
$_SESSION['isLoggedID'] = false ;
$lnAccessType = 99 ; // erroneous data entry
}
}
}
function checkForUserInDB($pcUserName, $pcPassword)
{
$llReturn = false ;
$lcAccessCheck =
"SELECT
a.ID, a.email, a.display, a.name, a.postOrder, a.foodID, a.churchID, a.bibleID, a.loginCount + 1 as newLoginCount, a.isModerator, isAdministrator
FROM tbl_user a
WHERE a.email = '$pcUserName' AND a.access = '$pcPassword' AND a.isRegistered = 2" ;
$laUserData = $this->loDataObject->queryGetData(false, $lcAccessCheck) ;
if($laUserData)
{
$llReturn = true ;
$this->lnUserID = $laUserData[0]['ID'] ;
$this->llIsModerator = $laUserData[0]['isModerator'] ;
$this->llIsAdministrator= $laUserData[0]['isAdministrator'] ;
$this->lcEmail = $laUserData[0]['email'] ;
$this->lcDisplay = $laUserData[0]['display'] ;
$this->lcName = $laUserData[0]['name'] ;
$this->lnPostOrder = $laUserData[0]['postOrder'] ;
$this->lnFoodID = $laUserData[0]['foodID'] ;
$this->lnChurchID = $laUserData[0]['churchID'] ;
$this->lnBibleRef = $laUserData[0]['bibleID'] ;
$this->lnLoginCount = $laUserData[0]['newLoginCount'] ;
}
return $llReturn ;
}
function logOut()
{
// not used at present - as this needs to be done before the main page is loaded to ensure it loads correctly
$_SESSION['userID'] = 0 ;
$_SESSION['isModerator'] = 0 ;
$_SESSION['isAdmin'] = 0 ;
$_SESSION['email'] = null;
$_SESSION['display'] = null ;
$_SESSION['name'] = null ;
$_SESSION['postOrder'] = 0 ;
$_SESSION['foodID'] = 0 ;
$_SESSION['churchID'] = 0 ;
$_SESSION['bibleRef'] = 31 ;
$_SESSION['isLoggedIn'] = 0 ;
}
function updatePassword($pcPasswordToStore, $pnUserID)
{
$lcUpdatePassword =
"UPDATE tbl_user
SET
tbl_user.access = '$pcPasswordToStore',
tbl_user.isRegistered = 2,
tbl_user.editDate = '" . $this->ldDate ."'
WHERE tbl_user.ID = $pnUserID";
$llUpdated = $this->loDataObject->iQuery($lcUpdatePassword);
return $llUpdated ;
}
function isRegistered($pnEventID, $plCheckBooked)
{
$llReturn = false ;
$lnUserID = $_SESSION['userID'] ;
if (!empty($pnEventID))
{
$lcCheckPreviousRegistration =
"SELECT
a.ID
FROM tbl_userevent a
WHERE a.userID = '$lnUserID' AND a.eventID = '$pnEventID'" ;
if($plCheckBooked)
{
$lcCheckPreviousRegistration .= " AND a.isBooked=1" ;
}
$laPreviousReg = $this->loDataObject->queryGetData(false, $lcCheckPreviousRegistration) ;
if($laPreviousReg)
{
$llReturn = true ;
}
}
return $llReturn ;
}
function onCourse($pnCourseID, $plCheckBooked)
{
$llReturn = false ;
$lnUserID = $_SESSION['userID'] ;
if (!empty($pnCourseID))
{
$lcCheckPreviousRegistration =
"SELECT
a.ID
FROM tbl_usertraining a
WHERE a.leaderID = '$lnUserID' AND a.courseID = '$pnCourseID'" ;
if($plCheckBooked)
{
$lcCheckPreviousRegistration .= " AND a.isBooked=1" ;
}
$laPreviousReg = $this->loDataObject->queryGetData(false, $lcCheckPreviousRegistration) ;
if($laPreviousReg)
{
$llReturn = true ;
}
}
return $llReturn ;
}
function getUserEmailAndDisplayNameByID($pnUserID)
{
$lcGetUserEmail =
"SELECT
a.email, a.display
FROM tbl_user a
WHERE a.ID = $pnUserID" ;
$laList = $this->loDataObject->queryGetData(false, $lcGetUserEmail) ;
$lcEmail = $laList[0]['email'] ;
$lcDisplay = $laList[0]['display'] ;
$lcReturn = $lcDisplay . '&&' . $lcEmail ;
return $lcReturn ;
}
function ensureUser($pcEmailAddress, $pcDesiredDisplay)
{
if($_SESSION['isLoggedIn'])
{
$this->llNewUser = false ;
$this->llUserNameChanged = false ;
$this->lnUserID = $_SESSION['userID'] ;
$this->lcDisplayName = $_SESSION['display'] ;
}
else
{
if(!empty($pcEmailAddress) && ereg('^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$', $pcEmailAddress))
{
// check for the email address in the database
$lcEmailCheck =
"SELECT
a.ID, a.display, a.isRegistered
FROM tbl_user a
WHERE a.email = '$pcEmailAddress'" ;
$laUserList = $this->loDataObject->queryGetData(true, $lcEmailCheck) ;
if($this->loDataObject->nRecentCount > 0)
{
$this->lnUserID = $laUserList[0]['ID'] ;
$this->lcDisplayName = $laUserList[0]['display'] ;
switch($laUserList[0]['isRegistered'])
{
case 0: // banned user , set userID to -1
$this->lnUserID = -1 ;
break ;
case 1: // deactivated account
$lnUserID = $this->lnUserID ;
$lcUpdate = "UPDATE tbl_user SET isRegistered = 2, editDate = now() WHERE ID = $lnUserID" ;
$this->loDataObject->iQuery($lcUpdate) ;
$this->llReactivation = true;
break;
}
}
else // no match - create a user
{
$this->llNewUser = true ;
// ensure uniqueness of display name; if this has to be altered simply add numbers to the end till it is unique.
$this->loDataObject->nRecentCount = 99 ; // a dummy value;
$lnLoopCount = 0 ;
$this->lcDisplayName = trim($pcDesiredDisplay) ;
$lcDisplayName = trim($pcDesiredDisplay) ;
while($this->loDataObject->nRecentCount > 0)
{
if($lnLoopCount > 0)
{
$this->lcDisplayName = $pcDesiredDisplay . str_pad($lnLoopCount, 3, '0', STR_PAD_LEFT) ;
$lcDisplayName = $this->lcDisplayName ;
$this->llUserNameChanged = true ;
}
$lcCheckDisplayName =
"SELECT
a.ID
FROM tbl_user a
WHERE a.display = '$lcDisplayName'" ;
$laUserList = $this->loDataObject->queryGetData(true, $lcCheckDisplayName);
$lnLoopCount++ ;
if($lnLoopCount > 999 && $this->loDataObject->nRecentCount>0) // run out of options with the desired base name
{
$lnLoopCount = 0 ;
$this->loDataObject->nRecentCount = 99 ;
$this->lcDisplayName = $this->loDataObject->usernameGenerator($lcDisplayName, 8) ; // this enters a loop to generate a random username of 8 characters
}
}
// now we have a unique display name
$this->lcAccess = $this->loDataObject->passwordGenerator(9) ;
$lcPassword = $this->loDataObject->encryptData($this->lcAccess);
$lcValueList = '"' . trim($pcEmailAddress) . '", "' . $lcPassword . '", "' . trim($this->lcDisplayName) . '",1,1, "' . $this->ldDate . '", "' . $this->ldDate . '", "' .
$this->ldDate . '"' ;
$this->lnUserID = $this->loDataObject->queryInsert("tbl_user", "email,access,display,mailingList,loginCount,lastLogin,createDate,editdate", $lcValueList, true);
$this->addToMailingList($pcEmailAddress) ;
}
}
else // no valid email supplied post the review as guest
{
$this->lnUserID = 3 ; // this is the guest
$this->lcDisplayName = "Guest" ;
}
}
}
function updateName($pnUserID, $pcName)
{
// updates the actual name in tbl_user
if(!empty($pcName))
{
$lcUpdate = "UPDATE tbl_user SET name = '$pcName', editDate = now() WHERE ID = $pnUserID" ;
$this->loDataObject->iQuery($lcUpdate) ;
}
}
function unRegister()
{
$lnUserID = $_SESSION['userID'] ;
$lcUpdate = "UPDATE tbl_user SET isRegistered = 1, editDate = now() WHERE ID = $lnUserID" ;
$this->loDataObject->iQuery($lcUpdate) ;
$lcUpdate = "UPDATE tbl_threaduser SET isSubscribed = 0, editDate = now() WHERE userID = $lnUserID" ;
$this->loDataObject->iQuery($lcUpdate) ;
}
function endAccount($pnUserID)
{
$lcUpdateUser = "UPDATE tbl_user SET isRegistered=0, editDate=now()WHERE ID=$pnUserID" ;
$this->loDataObject->iquery($lcUpdateUser) ;
}
function updateSession($pcEmail, $pcDisplayName, $pcName, $pnPost, $pnFood, $pnChurch, $pnBibleID)
{
$_SESSION['email'] = $pcEmail;
$_SESSION['display'] = $pcDisplayName ;
$_SESSION['name'] = $pcName ;
$_SESSION['postOrder'] = $pnPost ;
$_SESSION['foodID'] = $pnFood ;
$_SESSION['churchID'] = $pnChurch ;
$_SESSION['bibleRef'] = $pnBibleID ;
}
/*------------------------------------------------------------
NEWZAPP API INTEGRATION
------------------------------------------------------------*/
function ensureNewZappCredentials()
{
$this->lcNZAPIPWord = '' ;
$this->lcNZUserName = '' ;
$this->lcNZCID = '' ;
}
function newZappCommunicator($pcNZServerURL)
{
$llReturn = false ; // default position
// this sends the relevant message and passes back the response
$loRequest =& new HTTP_Request($pcNZServerURL);
//$loRequest->addHeader("Content-Type", "text/xml");
//$loRequest->addHeader("Content-Length", strlen($this->lNZRequest));
$loRequest->setMethod(HTTP_REQUEST_METHOD_POST);
$loRequest->addRawPostData($this->lcNZRequest, true);
$loRequest->sendRequest();
$this->lcNZResponse = $loRequest->getResponseBody();
if(strpos(strtolower($this->lcNZResponse), "true"))
{
$llReturn = true ;
}
return $llReturn ;
}
function getMailingListStatus()
{
$llReturn = false ;
$this->ensureNewZappCredentials() ;
// this works on $_SESSION['email']
$lcEmailToCheck = $_SESSION['email'] ;
$this->lcNZRequest = '<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1">' ;
$this->lcNZRequest .= '<SOAP:Body>' ;
$this->lcNZRequest .= '<Request>';
$this->lcNZRequest .= '<CID>' . $this->lcNZCID . '</CID>';
$this->lcNZRequest .= '<GetEmail>' . $lcEmailToCheck . '</GetEmail>';
$this->lcNZRequest .= '<UserName>' . $this->lcNZUserName . '</UserName>';
$this->lcNZRequest .= '<Password>' . $this->lcNZAPIPWord . '</Password>';
$this->lcNZRequest .= '</Request>';
$this->lcNZRequest .= '</SOAP:Body>';
$this->lcNZRequest .= '</SOAP:Envelope>' ;
$llReturn = $this->newZappCommunicator("https://system.newzapp.co.uk/SOAP/GetSubscriberWithAPI.asp") ;
return $llReturn ;
}
function setMailingListStatus($pnMailing)
{
$this->ensureNewZappCredentials() ;
$ldDate = date('d/m/Y') ;
if($pnMailing == 1) // add to/edit the mailing list
{
$this->lcNZRequest = '<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1">' ;
$this->lcNZRequest .= '<SOAP:Body>' ;
$this->lcNZRequest .= '<Request>';
$this->lcNZRequest .= '<CID>' . $this->lcNZCID . '</CID>';
$this->lcNZRequest .= '<UserName>' . $this->lcNZUserName . '</UserName>';
$this->lcNZRequest .= '<Password>' . $this->lcNZAPIPWord . '</Password>';
$this->lcNZRequest .= '<Email>' . $_SESSION['email'] . '</Email>';
$this->lcNZRequest .= '<EditDate>' . $ldDate . '</EditDate>';
$this->lcNZRequest .= '</Request>';
$this->lcNZRequest .= '</SOAP:Body>';
$this->lcNZRequest .= '</SOAP:Envelope>' ;
$lcURL = "https://system.newzapp.co.uk/SOAP/ThankyouSubscribeAPI.asp" ;
}
else // delete from the mailing list
{
$this->lcNZRequest = '<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1">' ;
$this->lcNZRequest .= '<SOAP:Body>' ;
$this->lcNZRequest .= '<Request>';
$this->lcNZRequest .= '<CID>' . $this->lcNZCID . '</CID>';
$this->lcNZRequest .= '<UserName>' . $this->lcNZUserName . '</UserName>';
$this->lcNZRequest .= '<Password>' . $this->lcNZAPIPWord . '</Password>';
$this->lcNZRequest .= '<DeleteEmail>' . $_SESSION['email'] . '</DeleteEmail>';
$this->lcNZRequest .= '</Request>';
$this->lcNZRequest .= '</SOAP:Body>';
$this->lcNZRequest .= '</SOAP:Envelope>' ;
$lcURL = "https://system.newzapp.co.uk/SOAP/ThankyouDeleteAPI.asp" ;
}
$llReturn = $this->newZappCommunicator($lcURL) ;
return $llReturn ;
}
function addToMailingList($pcEmailAddress)
{
$this->ensureNewZappCredentials() ;
$ldDate = date('d/m/Y') ;
$this->lcNZRequest = '<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1">' ;
$this->lcNZRequest .= '<SOAP:Body>' ;
$this->lcNZRequest .= '<Request>';
$this->lcNZRequest .= '<CID>' . $this->lcNZCID . '</CID>';
$this->lcNZRequest .= '<UserName>' . $this->lcNZUserName . '</UserName>';
$this->lcNZRequest .= '<Password>' . $this->lcNZAPIPWord . '</Password>';
$this->lcNZRequest .= '<Email>' . $pcEmailAddress . '</Email>';
$this->lcNZRequest .= '<Group>ChristianLeadership.org</Group>' ;
$this->lcNZRequest .= '<EditDate>' . $ldDate . '</EditDate>';
$this->lcNZRequest .= '</Request>';
$this->lcNZRequest .= '</SOAP:Body>';
$this->lcNZRequest .= '</SOAP:Envelope>' ;
$llReturn = $this->newZappCommunicator("https://system.newzapp.co.uk/SOAP/ThankyouSubscribeToGroupAPI.asp") ;
return $llReturn ;
}
} // end of class definition