-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataObject.php
executable file
·559 lines (493 loc) · 16.9 KB
/
dataObject.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php
/*
File: lib/dataObject.php
Author: Nathan Davies
Created: 08/06/2007
Purpose: To hold all the php-MySQL data connection functions and methods. The aim is to eventually build this as a global object that is initialised when index.php first loads
Ammendment;
ND 15/06/2007 Improving the code to make this a true Data Abstraction Layer, not using TRY in the new version of doing things
ND 18/06/2007 Completing the code for INSERT, DELETE, UPDATE, compareCheckum, exceptionHandler and getLastAutoInc
ND 20/06/2007 Parameterising dataObject() for future re-use
ND 18/07/2007 Added encryptData() to the object
ND 25/07/2007 Added new class - writeApplication()
ND 03/08/2007 Added new function passwordGenerator() to the dataObject() class. It has been added to this class as passwords are connected strongly with the database.
It could in time be moved to a userObject()
ND 20/06/2008 Moved into new CLO development space and changed the database details - hardcoded for ease of development.
*/
class dataObject
{
var $database_name ;
var $database_user ;
var $database_password ;
var $database_host ;
var $database_link ;
var $nRecentCount ;
var $ldDate ;
var $lnBadUserID ;
public function dataObject()
{
$this->database_name = "" ;
$this->database_user = "" ;
$this->database_password = "" ;
$this->database_host = "" ;
$this->ldDate = date("Y-m-d") ;
}
// Property Manipulation Functions
function changeUser($user)
{
$this->database_user = $user;
}
function changePass($pass)
{
$this->database_pass = $pass;
}
function changeHost($host)
{
$this->database_host = $host;
}
function changeName($name)
{
$this->databse_name = $name;
}
function changeAll($user, $pass, $host, $name)
{
$this->database_user = $user;
$this->database_pass = $pass;
$this->database_host = $host;
$this->database_name = $name;
}
/*
====================
Connection Functions
====================
*/
function makeConnection()
{
// connect to the data server
$this->database_link = mysql_connect($this->database_host, $this->database_user, $this->database_password) or die("Could not make the connection to MySQL");
// connect to the database required
mysql_select_db($this->database_name) or die("Could not open database: ". $this->database_name);
}
function closeConnection()
{
if(isset($this->database_link))
{
mysql_close($this->database_link);
}
else
{
mysql_close();
}
}
function ensureConnection()
{
if(!isset($this->database_link))
{
$this->makeConnection();
}
}
/*
===============
Query Functions
===============
*/
function queryGetData($plSetCount, $pcQuery)
{
// ensure there is a connection
$this->ensureConnection();
// run the supplied query
$result = mysql_query($pcQuery, $this->database_link) or die("Error: ". mysql_error());
$laReturnArray = array();
$i=0;
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
if ($row)
{
$laReturnArray[$i++]=$row;
}
}
if($plSetCount)
{
$this->nRecentCount = mysql_num_rows($result);
}
mysql_free_result($result);
return $laReturnArray;
}
function queryInsert($pcTableName, $pcFieldList, $pcValueList, $plReturnID)
{
// ensure there is a connection
$this->ensureConnection();
// build up the insert string
$lcInsertCommand = "INSERT INTO " . $pcTableName . "(" . $pcFieldList . ") VALUES(" . $pcValueList . ")" ;
// insert the records
$lvResult = mysql_query($lcInsertCommand, $this->database_link) or die("Error: ". mysql_error());
if ($plReturnID)
{
$lvResult = $this->getLastAutoInc($pcTableName);
}
return $lvResult;
}
function iQuery($pcQuery)
{
// this can be used for delete statments and update statements
$this->ensureConnection();
$lnhandle = fopen('../sql.txt', 'a+') ;
fwrite($lnhandle, $pcQuery) ;
fclose($lnhandle) ;
$lvResult = mysql_query($pcQuery, $this->database_link) or die("Error: ". mysql_error());
return $lvResult;
}
/*
====================
Ancilliary Functions
====================
*/
function compareChecksum($pcValueList, $pcCheckSum)
{
$llIsValid = (crc32($pcValueList) == $pcCheckSum);
return $llIsValid;
}
function handleException($pnExceptionNumber)
{
// handling errors that are not handled by the die option
header("Location:potentialissue.php?pi=$pnExceptionNumber");
}
function getLastAutoInc($pcTableName)
{
$lnReturn = mysql_insert_id($this->database_link);
return $lnReturn;
}
function encryptData($pcToEncrypt)
{
//hash
return hash('sha256', $pcToEncrypt) ;
}
function secure($data, $plIsEmail)
{
// prevent the majority of attacks by removing certain elements from the data.
if ($plIsEmail)
{
$replace = array('<' => '' , '>' => '' , '&' => '' , ',' => '' , '*' => '' , '/' => '' );
}
else
{
$replace = array('<' => '' , '>' => '' , '&' => '' , '.' => '' , ',' => '' , '*' => '' , '/' => '' , '@' => '');
}
$data = strtr($data , $replace);
return $data;
}
function escapeString($pcString)
{
$lcReturnString = "" ;
if (!empty($pcString))
{
$this->ensureConnection() ;
if(get_magic_quotes_gpc())
{
$pcString = stripslashes($pcString) ;
}
$lcReturnString = mysql_real_escape_string($pcString, $this->database_link) ;
}
return $lcReturnString ;
}
function passwordGenerator($pnResultLength)
{
// list all possible characters
$possible = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = '';
$i = 0;
while ($i < $pnResultLength)
{
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
function usernameGenerator($pcPotentialName, $pnResultLength)
{
$lcUserName = !empty($pcPotentialName)?$pcPotentialName:$this->passwordGenerator($pnResultLength) ;
// check for uniqueness
$lcCheckUser =
"SELECT
a.ID
FROM tbl_user a
WHERE a.name = '$lcUserName' " ;
$laResults = $this->queryGetData(true, $lcCheckUser) ;
if($laResults)
{
// there is a match so it's no good, make a recursive call
$lcUserName = $this->usernameGenerator("", $pnResultLength) ;
}
else
{
return $lcUserName ;
}
}
/*
======================
Moderation Functions
======================
*/
function deleteThread($pnThreadID, $plIsBanned)
{
// get the USER ID of the thread starter
$lcGetStarterSQL =
"SELECT
a.userID, a.title,
b.email, b.display
FROM tbl_thread a
LEFT OUTER JOIN tbl_user b on b.ID = a.userID
WHERE a.ID = $pnThreadID" ;
$laThreadStarter = $this->queryGetData(false, $lcGetStarterSQL);
$lcUserEmail = $laThreadStarter[0]['email'];
$lcUserDisplay = $laThreadStarter[0]['display'];
$lcThreadTitle = $laThreadStarter[0]['title'] ;
$this->lnBadUserID = $laThreadStarter[0]['userID'];
// delete the thread and the post
$lcDeleteThread = "DELETE FROM tbl_thread WHERE ID = $pnThreadID" ;
$lcDeletePosts = "DELETE FROM tbl_post WHERE threadID = $pnThreadID" ;
$this->iQuery($lcDeleteThread) ;
$this->iQuery($lcDeletePosts) ;
// email the thread starter
require_once '../lib/emailObject.php' ;
$loEmailObject = new emailObject($lcUserDisplay .'&&' . $lcUserEmail, $_SESSION['email'], $_SESSION['email'], false, "", "") ;
$lcMessage = $lcUserDisplay . "||" ;
$lcMessage .= "We have deleted a thread you started in one of the forums. This thread was found to be in breach of our terms and conditions.||" ;
$lcMessage .= "Thread title: " . $lcThreadTitle . "||" ;
if($plIsBanned)
{
$lcMessage .= "Your account has now been suspended.||" ;
}
else
{
$lcMessage .= "If you continue to breach the terms and conditions your account will be removed.||" ;
}
$lcMessage .= "If you believe someone else has started this thread using your account then please contact us immediately and we will look into the matter further.||" ;
$lcMessage .= "Many thanks||Christian Leaders Forum" ;
$lcSubject = "Thread in breach of terms and conditions" ;
$loEmailObject->sendEmail($lcSubject, $lcMessage) ;
$lcDisplay = '<p>The thread — ' . $lcThreadTitle . ' — has been deleted and the member informed.</p>' ;
return $lcDisplay ;
}
function deletePost($pnPostID, $plIsBanned) // doesn't delete the post but simply overwrites it with a comment about the removal
{
// get the user ID of the poster
$lcGetPosterSQL =
"SELECT
a.userID,
b.email, b.display,
c.title
FROM tbl_post a
LEFT OUTER JOIN tbl_user b on b.ID = a.userID
LEFT OUTER JOIN tbl_thread c ON c.ID = a.threadID
WHERE a.ID = $pnPostID";
$laPostStarter = $this->queryGetData(false, $lcGetPosterSQL) ;
$lcUserEmail = $laPostStarter[0]['email'] ;
$lcUserDisplay = $laPostStarter[0]['display'] ;
$lcThreadTitle = $laPostStarter[0]['title'] ;
$this->lnBadUserID = $laPostStarter[0]['userID'];
// overwrite the post contents with a message to say it was deleted by moderator
$lcNewPost = "The original post has been removed due to a breach of terms and conditions.<br />" ;
$lcNewPost .= "Post originally made by " . $lcUserDisplay ;
$lnModID = $_SESSION['userID'] ;
$lcUpdatePost = "UPDATE tbl_post SET userID = $lnModID, content='" . $lcNewPost . "', editDate=now(), isOnReport=0 WHERE ID=$pnPostID" ;
$this->iQuery($lcUpdatePost) ;
// email the poster
require_once '../lib/emailObject.php' ;
$loEmailObject = new emailObject($lcUserDisplay .'&&' . $lcUserEmail, $_SESSION['email'], $_SESSION['email'], false, "", "") ;
$lcMessage .= $lcUserDisplay . "||" ;
$lcMessage .= "We have deleted a post you submitted under thread listed below. This post was found to be in breach of our terms and conditions.||" ;
$lcMessage .= "Thread title: " . $lcThreadTitle . "||" ;
if($plIsBanned)
{
$lcMessage .= "Your account has now been suspended.||" ;
}
else
{
$lcMessage .= "If you continue to breach the terms and conditions your account will be removed.||" ;
}
$lcMessage .= "If you believe someone else has submitted this post using your account then please contact us immediately and we will look into the matter further.||" ;
$lcMessage .= "Many thanks||Christian Leaders Forum" ;
$lcSubject = "Post in breach of terms and conditions" ;
$loEmailObject->sendEmail($lcSubject, $lcMessage) ;
$lcDisplay = '<p>The post under thread — ' . $lcThreadTitle . ' — has been deleted and the member informed.</p>' ;
return $lcDisplay ;
}
function deleteReview($pnType, $pnReviewID, $plIsBanned)
{
switch($pnType)
{
case 11: // download
$lcGetSQL =
"SELECT
a.userID,
b.email, b.display,
c.title
FROM tbl_resourcereview a
LEFT OUTER JOIN tbl_user b on b.ID = a.userID
LEFT OUTER JOIN tbl_resource c ON c.ID = a.resourceID
WHERE a.ID = $pnReviewID";
$lcDelete = "DELETE FROM tbl_resourcereview WHERE ID = $pnReviewID" ;
break ;
case 12: // article
$lcGetSQL =
"SELECT
a.userID,
b.email, b.display,
c.title
FROM tbl_articlereview a
LEFT OUTER JOIN tbl_user b on b.ID = a.userID
LEFT OUTER JOIN tbl_article c ON c.ID = a.articleID
WHERE a.ID = $pnReviewID";
$lcDelete = "DELETE FROM tbl_articlereview WHERE ID = $pnReviewID" ;
break ;
case 13: // event
$lcGetSQL =
"SELECT
a.userID,
b.email, b.display,
c.title
FROM tbl_eventreview a
LEFT OUTER JOIN tbl_user b on b.ID = a.userID
LEFT OUTER JOIN tbl_event c ON c.ID = a.eventID
WHERE a.ID = $pnReviewID";
$lcDelete = "DELETE FROM tbl_eventreview WHERE ID = $pnReviewID" ;
break ;
case 21: // training
$lcGetSQL =
"SELECT
a.userID,
b.email, b.display,
c.title
FROM tbl_trainingreview a
LEFT OUTER JOIN tbl_user b on b.ID = a.userID
LEFT OUTER JOIN tbl_training c ON c.ID = a.trainingID
WHERE a.ID = $pnReviewID";
$lcDelete = "DELETE FROM tbl_trainingreview WHERE ID = $pnReviewID" ;
break ;
}
// get the user ID of the reviewer
$laStarter = $this->queryGetData(false, $lcGetSQL) ;
$lcUserEmail = $laStarter[0]['email'] ;
$lcUserDisplay = $laStarter[0]['display'] ;
$lcResource = $laStarter[0]['title'] ;
$this->lnBadUserID = $laStarter[0]['userID'];
// delete the review
$this->iQuery($lcDelete) ;
// email the poster
require_once '../lib/emailObject.php' ;
$loEmailObject = new emailObject($lcUserDisplay .'&&' . $lcUserEmail, $_SESSION['email'], $_SESSION['email'], false, "", "") ;
$lcMessage = $lcUserDisplay . "||" ;
$lcMessage .= "We have deleted a review you submitted for the item listed below. This review was found to be in breach of our terms and conditions.||" ;
$lcMessage .= "Review of: " . $lcResource . "||" ;
if($plIsBanned)
{
$lcMessage .= "Your account has now been suspended.||" ;
}
else
{
$lcMessage .= "If you continue to breach the terms and conditions your account will be removed.||" ;
}
$lcMessage .= "If you believe someone else has submitted this review using your account then please contact us immediately and we will look into the matter further.||" ;
$lcMessage .= "Many thanks||Christian Leaders Forum" ;
$lcSubject = "Review in breach of terms and conditions" ;
$loEmailObject->sendEmail($lcSubject, $lcMessage) ;
$lcDisplay = '<p>The review for ' . $lcResource . ' has been deleted and the member informed.</p>' ;
return $lcDisplay ;
}
/*
=========================
Admin Related Functions
=========================
*/
function setDisplayOrder($pcTableName, $pnDesiredOrder, $pnRecordID)
{
// this re-sets all the orders in the table apart from the one being edited
$llUpdate = false ;
$lcUpdateStatement = "" ;
// get the required information to make this work (if $pnRecordID == 0 then it's a new record we need to deal with ultimately)
$lcSelectMaxOrder =
"SELECT
max(a.displayOrder) as maxOrder
FROM " . $pcTableName . " a";
$laMaxOrder = $this->queryGetData(false, $lcSelectMaxOrder) ;
$lnMaxOrder = $laMaxOrder[0]['maxOrder'] ;
$lnNextOrder = $lnMaxOrder + 1 ;
// ensure the desired order doesn't exceed the max+1
if($pnDesiredOrder > $lnNextOrder)
{
$pnDesiredOrder = $lnNextOrder ;
}
if($pnRecordID > 0)
{
$lcSelectCurrentOrder =
"SELECT
a.displayOrder
FROM " . $pcTableName . " a
WHERE ID = $pnRecordID" ;
$laCurrentOrder = $this->queryGetData(false, $lcSelectCurrentOrder) ;
$lnCurrentOrder = $laCurrentOrder[0]['displayOrder'] ;
if($pnDesiredOrder < $lnCurrentOrder)
{
$lcUpdateStatement = "UPDATE " . $pcTableName ." SET displayOrder = displayOrder+1 WHERE displayOrder >= $pnDesiredOrder AND displayOrder < $lnCurrentOrder" ;
}
elseif($pnDesiredOrder > $lnCurrentOrder)
{
$lcUpdateStatement = "UPDATE " . $pcTableName ." SET displayOrder = displayOrder-1 WHERE displayOrder <= $pnDesiredOrder AND displayOrder > $lnCurrentOrder" ;
}
// note if current and desired are the same do nothing
}
else // new record being added
{
$lcUpdateStatement = "UPDATE " . $pcTableName . " SET displayOrder = displayOrder+1 WHERE displayOrder >= $pnDesiredOrder" ;
}
if(!empty($lcUpdateStatement))
{
if($pnDesiredOrder <> $lnNextOrder)
{
$llUpdate = $this->iQuery($lcUpdateStatement) ;
}
else
{
$llUpdated = true ; // mo uppdate neede as the desired order is next in sequence
}
}
return $llUpdate ;
}
function dateChecker($pcYear, $pcMonth, $pcDay)
{
// format the parameters into a date
$ldDate = date('Y-m-d', strtotime($pcDay . '-' . $pcMonth . '-' . $pcYear)) ;
// if this is 31 Feb 2003 (not a leap year) the result is 2nd March 2003
// the way the date is created - via dropdowns menas this is the only issue there could be, rolling forward seems reasonable
return $ldDate ;
}
function formatText($pcText)
{
$lcText = nl2br($pcText) ;
$laParagraphs = explode('<br />', $lcText) ;
$lcReturn = '' ;
$lnCount = count($laParagraphs) ;
for($i = 0; $i < $lnCount; $i++)
{
$lcReturn .= '<p>' . $laParagraphs[$i] . '</p>' ;
}
// final formatting - this allows for repeated calls to be made without extra tags being left in.
$lcReturn = str_replace('<p></p>', '', $lcReturn) ;
$lcReturn = str_replace('</p></p>', '', $lcReturn) ;
$lcReturn = str_replace('<p><p>', '', $lcReturn) ;
if(strpos($lcReturn, '['))
{
// update the story to replace any [] with proper html tags
$lcReturn = str_replace('[', '<', $lcReturn) ;
$lcReturn = str_replace(']', '>', $lcReturn) ;
}
return $lcReturn ;
}
/*
====================
Specific Queries
====================
*/
} // end of class definition
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/