-
Notifications
You must be signed in to change notification settings - Fork 4
/
_init.php
499 lines (422 loc) · 19.7 KB
/
_init.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
<?php
/*
Copyright 2020 FWBer.com
This file is part of FWBer.
FWBer is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FWBer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero Public License for more details.
You should have received a copy of the GNU Affero Public License
along with FWBer. If not, see <https://www.gnu.org/licenses/>.
*/
include("_debug.php");
//include_once("_secrets.php");
//include_once("_names.php");
//include_once("_getProfile.php");
//include_once("_getMatches.php");
//include_once("_emailFunctions.php");
//=========================================================================================
function getSaltedPassword($password,$date)
{//=========================================================================================
include("_secrets.php");
return md5($password.$salt.$date);
}
//=========================================================================================
function mysqli_result($search, $row, $field)
{//=========================================================================================
$i=0;
while($results=mysqli_fetch_array($search))
{
if ($i==$row){$result=$results[$field];}
$i++;
}
return $result;
}
//=========================================================================================
function currentPageURL()
{//=========================================================================================
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
//=========================================================================================
function validateSessionOrCookiesReturnLoggedIn()
{//=========================================================================================
include("_secrets.php");
include("_debug.php");
$loggedIn = false;
//first we'll handle validating the session
if(!isset($_SESSION["token"])){}//if($debug)echo "<!-- DEBUG: No session token -->\n";}
else if($_SESSION["token"]==null){}//if($debug)echo "<!-- DEBUG: Session token null -->\n";}
else if(!isset($_SESSION["email"])){}//if($debug)echo "<!-- DEBUG: No session email -->\n";}
else if($_SESSION["email"]==null){}//if($debug)echo "<!-- DEBUG: Session email null -->\n";}
else if(strlen($_SESSION["email"])==0){}//if($debug)echo "<!-- DEBUG: Session email zero length -->\n";}
else {
//check salt, if doesn't match, delete cookies.
$db = mysqli_connect($dburl,$dbuser,$dbpass);
if(!$db)exit(mysqli_connect_error());
$email = mysqli_real_escape_string($db,$_SESSION["email"]);
$dbquerystring = sprintf("SELECT id, email, passwordHash, dateLastSeen, dateLastSignedIn FROM ".$dbname.".users WHERE email='%s'",$email);
$dbquery = mysqli_query($db, $dbquerystring);
$dbresults = mysqli_fetch_array($dbquery);
mysqli_free_result($dbquery);
if
(
$dbresults==null
||$dbresults['email']==null
||$dbresults['email']!=$email
||$dbresults['passwordHash']==null
||getSaltedPassword($dbresults['passwordHash'],$dbresults['dateLastSignedIn'])!=$_SESSION["token"]
)
{
//if($debug)echo "<!-- DEBUG: No email, no password, or password didn't match. Destroying session. -->\n";
session_destroy();
$loggedIn = false;
}
else
{
$loggedIn=true;
//if($debug)echo "<!-- DEBUG: Logged in with session token -->\n";
//update the last seen every 2 hours
$time2h = 7200; //3600 per hour
if(($dbresults['dateLastSeen']==null) || (time() > $dbresults['dateLastSeen'] + $time2h))
{
mysqli_query($db, "UPDATE ".$dbname.".users SET `dateLastSeen` = '".time()."' WHERE `email` = '".$email."'");
}
}
//done with the db
mysqli_close($db);
}
//then we'll handle the cookies
if(!isset($_COOKIE["token"])){}//if($debug)echo "<!-- DEBUG: No cookie token -->\n";}
else if($_COOKIE["token"]==null){}//if($debug)echo "<!-- DEBUG: Cookie token null -->\n";}
else if(!isset($_COOKIE["email"])){}//if($debug)echo "<!-- DEBUG: No cookie email -->\n";}
else if($_COOKIE["email"]==null){}//if($debug)echo "<!-- DEBUG: Cookie email null -->\n";}
else if(strlen($_COOKIE["email"])==0){}//if($debug)echo "<!-- DEBUG: Cookie email zero length -->\n";}
else
{
//check salt, if doesn't match, delete cookies.
$db = mysqli_connect($dburl,$dbuser,$dbpass);
if(!$db)exit(mysqli_connect_error());
$email = mysqli_real_escape_string($db, $_COOKIE["email"]);
$dbquerystring = sprintf("SELECT id, email, passwordHash, dateLastSeen, dateLastSignedIn FROM ".$dbname.".users WHERE email='%s'",$email);
$dbquery = mysqli_query($db, $dbquerystring);
$dbresults = mysqli_fetch_array($dbquery);
mysqli_free_result($dbquery);
if(
$dbresults==null
||$dbresults['passwordHash']==null
||md5(getSaltedPassword($dbresults['passwordHash'],$dbresults['dateLastSignedIn']))!=$_COOKIE["token"]
)
{
//if($debug)echo "<!-- DEBUG: Cookie email not found in db, no passwordHash, or passwordHash didn't match cookie. Destroying cookie. -->\n";
setcookie("email","",time()-1000,'/',".".getSiteDomain());
setcookie("token","",time()-1000,'/',".".getSiteDomain());
}
else
{
//verified user.
if($loggedIn==false) //we don't have a valid session but we have a valid cookie
{
//if($debug)echo "<!-- DEBUG: Logged in with cookie and set session token -->\n";
//update the last seen every 2 hours
$time2h = 7200; //3600 per hour
if(($dbresults['dateLastSeen']==null) || (time() > $dbresults['dateLastSeen'] + $time2h))
{
mysqli_query($db, "UPDATE ".$dbname.".users SET `dateLastSeen` = '".time()."' WHERE `email` = '".$email."'");
}
//set the session email and token from the cookie
$_SESSION['email']=$email;
$_SESSION['token']=$_COOKIE["token"];
}
$loggedIn = true;
}
//done with the db
mysqli_close($db);
}
return $loggedIn;
}
//=========================================================================================
function goHomeIfCookieNotSet()
{//=========================================================================================
// make sure signed in, otherwise go home
if(isCookieSet()==false)
{
header('Location: '.getSiteURL());
echo '<meta http-equiv="refresh" content="1;url='.getSiteURL().'"/>';
exit();
}
}
//=========================================================================================
function isProfileDone()
{//=========================================================================================
include("_secrets.php");
$db = mysqli_connect($dburl,$dbuser,$dbpass);
if(!$db)exit(mysqli_connect_error());
$email = mysqli_real_escape_string($db, $_SESSION["email"]);
$dbquerystring = sprintf("SELECT id, email, profileDone FROM ".$dbname.".users WHERE email='%s'",$email);
$dbquery = mysqli_query($db, $dbquerystring);
$dbresults = mysqli_fetch_array($dbquery);
mysqli_free_result($dbquery);
//done with the db
mysqli_close($db);
if(
$dbresults!=null&&$dbresults['profileDone']==1
)
{
//let's set the profileDone for below, whether to pull vars from db or not
return 1;
}
else return 0;
}
//=========================================================================================
function isCookieSet()
{//=========================================================================================
if(isset($_SESSION["email"])&&$_SESSION["email"]!=null&&strlen($_SESSION["email"])>0)
return 1;
else
return 0;
}
//=========================================================================================
function getDistanceBetweenPoints($lat1,$lon1,$lat2,$lon2)
{//=========================================================================================
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return round($miles,2);
}
//=========================================================================================
function getGenderString($gender)
{//=========================================================================================
if($gender=='male')return "Man";
else if($gender=='female')return "Woman";
else if($gender=='mtf')return "TS Woman";
else if($gender=='ftm')return "TS Man";
else if($gender=='cdmtf')return "CD Woman";
else if($gender=='cdftm')return "CD Man";
else if($gender=='mf')return "Man And Woman Couple";
else if($gender=='mm')return "Man And Man Couple";
else if($gender=='ff')return "Woman And Woman Couple";
else if($gender=='group')return "Group Of People";
else return $gender;
}
//=========================================================================================
function getAge($month,$day,$year)
{//=========================================================================================
return (date("md", date("U", mktime(0,0,0, $month, $day, $year))) > date("md") ? ((date("Y")-$year)-1):(date("Y")-$year));
}
//=========================================================================================
function getTimeElapsedStringSinceTimestamp($time)
{//=========================================================================================
$secs = time()-$time;
$bit = array
(
' year' => $secs / 31556926 % 12,
' week' => $secs / 604800 % 52,
' day' => $secs / 86400 % 7,
' hour' =>$secs / 3600 % 24
//,
//' minute' => $secs / 60 % 60,
//' second' => $secs % 60
);
$ret = null;
foreach($bit as $k => $v)
{
if($v > 1)$ret[] = $v . $k . 's';
if($v == 1)$ret[] = $v . $k;
}
if($ret!=null)
{
array_splice($ret, count($ret)-1, 0, 'and');
$ret[] = 'ago.';
return join(' ', $ret);
}
else
return "Online Now!";
}
//=========================================================================================
function getDateStringFromTimestamp($time)
{//=========================================================================================
return date("F j, Y, g:i a",$time);
}
//=========================================================================================
function printGenderChar($g)
{//=========================================================================================
if($g['gender']=="male") echo '<span style="color:#0397ff;text-shadow:#339 1px 1px 2px;">♂ </span>';
else if($g['gender']=="female") echo '<span style="color:#ff66cb;text-shadow:#917 1px 1px 2px;">♀ </span>';
else if($g['gender']=="mtf") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">⚥ </span>';
else if($g['gender']=="ftm") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">⚥ </span>';
else if($g['gender']=="cdmtf") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">⚥ </span>';
else if($g['gender']=="cdftm") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">⚥ </span>';
else if($g['gender']=="mf") echo '<span style="color:#0397ff;text-shadow:#339 1px 1px 2px;">♂</span><span style="color:#ff66cb;text-shadow:#917 1px 1px 2px;">♀</span>';
else if($g['gender']=="mm") echo '<span style="color:#0397ff;text-shadow:#339 1px 1px 2px;">♂♂</span>';
else if($g['gender']=="ff") echo '<span style="color:#ff66cb;text-shadow:#917 1px 1px 2px;">♀♀</span>';
else if($g['gender']=="group") echo '<span style="color:#0397ff;text-shadow:#339 1px 1px 2px;">♂</span><span style="color:#ff66cb;text-shadow:#917 1px 1px 2px;">♀</span><span style="color:#0397ff;text-shadow:#339 1px 1px 2px;">♂</span><span style="color:#ff66cb;text-shadow:#917 1px 1px 2px;">♀</span>';
else echo $g['gender'];
}
//=========================================================================================
function printGenderString($g)
{//=========================================================================================
$genderString = getGenderString($g['gender']);
if($g['gender']=="male") echo '<span style="color:#0397ff;text-shadow:#339 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="female") echo '<span style="color:#ff66cb;text-shadow:#917 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="mtf") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="ftm") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="cdmtf") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="cdftm") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="mf") echo '<span style="color:#0397ff;text-shadow:#339 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="mm") echo '<span style="color:#0397ff;text-shadow:#339 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="ff") echo '<span style="color:#ff66cb;text-shadow:#917 1px 1px 2px;">'.$genderString.'</span>';
else if($g['gender']=="group") echo '<span style="color:#84f;text-shadow:#319 1px 1px 2px;">'.$genderString.'</span>';
else echo $g['gender'];
}
//=========================================================================================
function hasPenis($g)
{//=========================================================================================
if(
$g['gender']=='male'||
$g['gender']=='mtf'||
$g['gender']=='cdmtf'||
$g['gender']=='male'||
$g['gender']=='mf'||
$g['gender']=='mm'
)
return true;
return false;
}
//=========================================================================================
function hasBreasts($g)
{//=========================================================================================
if(
$g['gender']=='female'||
$g['gender']=='ftm'||
$g['gender']=='mtf'||
$g['gender']=='mf'||
$g['gender']=='ff'
)
return true;
return false;
}
//=========================================================================================
function hasBodyHair($g)
{//=========================================================================================
if(
$g['gender']=='male'||
$g['gender']=='ftm'||
$g['gender']=='mtf'||
$g['gender']=='cdmtf'||
$g['gender']=='mf'||
$g['gender']=='mm'
)
return true;
return false;
}
//=========================================================================================
function wantsPenis($g)
{//=========================================================================================
if(
$g['b_wantGenderMan']==1||
$g['b_wantGenderTSWoman']==1||
$g['b_wantGenderCDWoman']==1||
$g['b_wantGenderCoupleMF']==1||
$g['b_wantGenderCoupleMM']==1
)
return true;
return false;
}
//=========================================================================================
function wantsBreasts($g)
{//=========================================================================================
if(
$g['b_wantGenderWoman']==1||
$g['b_wantGenderTSWoman']==1||
$g['b_wantGenderTSMan']==1||
$g['b_wantGenderCoupleMF']==1||
$g['b_wantGenderCoupleFF']==1
)
return true;
return false;
}
//=========================================================================================
function wantsBodyHair($g)
{//=========================================================================================
if(
$g['b_wantGenderMan']==1||
$g['b_wantGenderTSMan']==1||
$g['b_wantGenderCDMan']==1||
$g['b_wantGenderCoupleMF']==1||
$g['b_wantGenderCoupleMM']==1
)
return true;
return false;
}
//=========================================================================================
function usortByArrayKey(&$array, $key, $asc=SORT_ASC)
{//=========================================================================================
$sort_flags = array(SORT_ASC, SORT_DESC);
if(!in_array($asc, $sort_flags)) throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
$cmp = function(array $a, array $b) use ($key, $asc, $sort_flags)
{
if(!is_array($key))
{ //just one key and sort direction
if(!isset($a[$key]) || !isset($b[$key]))
{
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$key] == $b[$key]) return 0;
return ($asc==SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
}
else
{ //using multiple keys for sort and sub-sort
foreach($key as $sub_key => $sub_asc)
{
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if(!in_array($sub_asc, $sort_flags)) { $sub_key = $sub_asc; $sub_asc = $asc; }
//just like above, except 'continue' in place of return 0
if(!isset($a[$sub_key]) || !isset($b[$sub_key]))
{
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$sub_key] == $b[$sub_key]) continue;
return ($sub_asc==SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
};
usort($array, $cmp);
}
//=========================================================================================
function rem_array(&$array,$str)
{//=========================================================================================
foreach($array as $key => $value)
{
if($array[$key] == "$str") unset($array[$key]);
}
return $array;
}
//=========================================================================================
function convert_line_breaks($string, $line_break="<br>")
{//=========================================================================================
$patterns = array(
"/(<br>|<br \/>|<br\/>)\s*/i",
"/(\r\n|\r|\n)/"
);
$replacements = array(
PHP_EOL,
$line_break
);
$string = preg_replace($patterns, $replacements, $string);
return $string;
}