/
forgotpassword.php
779 lines (604 loc) · 24.4 KB
/
forgotpassword.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
<?php
ob_start();
error_reporting(0);
include_once('config.php');
$mode = $_REQUEST['mode'];
$serverurl = $_SERVER['HTTP_HOST'];
if($mode == "setpassword"){
$userid = $_REQUEST['userid'];
$userpassword = $_REQUEST['newpassword'];
$result=array();
// Generate the new password hash.
$salt = genRandomPassword(32);
$crypted = getCryptedPassword($userpassword, $salt);
$password = $crypted.':'.$salt;
$hashparts = explode (':' , $password);
if(mysql_query("UPDATE ".$prefix."users set password = '$password',activation='' where id = '$userid' ")){
//header('Content-Type: application/json');
$result['valid']='1'; // Invalid Token
echo json_encode($result);
//echo"1";// Invalid Token
}else{
//header('Content-Type: application/json');
$result['invalid']='0'; // Invalid Token
echo json_encode($result);
}
}
elseif($mode == "verifytoken"){
$username = $_REQUEST['username'];
$token = $_REQUEST['token'];
$result=array();
############################ CHECKING USERNAME EXIST OR NOT ##################
$sql_username = " SELECT id,activation from ".$prefix."users where username = '".$username."' ";
$rs_username = mysql_query($sql_username);
$num_rows = mysql_num_rows($rs_username);
if($num_rows > 0){
$tokenrs = mysql_fetch_assoc($rs_username);
$crypt = $tokenrs['activation'];
$testcrypt = md5($token);
if(!($crypt == $testcrypt)){
//header('Content-Type: application/json');
$result['invalid']='2'; // Invalid Token
echo json_encode($result);
}else{
//header('Content-Type: application/json');
$result['valid']= '1'; // valid token
$result['userid']= $tokenrs['id'];
$result['token']=$crypt.':'.$salt;
echo json_encode($result);
}
}else{
//header('Content-Type: application/json');
$result['wrong user']='0'; // Invalid Token
echo json_encode($result); /*************** wrong user *************/
}
}
################ FOrgot Username #############
else if($mode == "forgotusername"){
$email = $_REQUEST['email'];
//header('Content-Type: application/json');
############################ CHECKING USERNAME EXIST OR NOT ##################
$sql_username = " SELECT id,username from ".$prefix."users where email = '".$email."' ";
$rs_username = mysql_query($sql_username);
$num_rows = mysql_num_rows($rs_username);
if($num_rows > 0){
############### send email for token ##############
$records = mysql_fetch_assoc($rs_username);
$username = $records['username'];
######################################################
$msg = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width; maximum-scale=1.0;">
<title>RAS</title>
<style type="text/css">
body{ margin:0px; padding:0px;}
@media only screen and (max-width:598px){
table[class="mainWd"]{ width:100% !important; }
.img{ width:100% !important; }
}
@media only screen and (max-width:599px){
table{ float:none !important; }
table[class="mainWd"]{ width:100% !important; }
table[class="table-width"]{ float:left !important}
.img{ width:100% !important; }
@media only screen and (max-width:480px){
td[class="wd660"]{ width:100% !important; float:left !important; text-align:center !important; }
.img1{ display:none !important}
td[class="wd360"]{ width:100% !important; float:left !important; text-align:center; margin-bottom:20px; }
table[class="full_480"]{ width:220px !important; text-align:center !important; float:none !important; }
td[class="mob_hide"]{ display:none !important; }
}
.img {width:100% !important; }
.img {width:100% !important; }
</style>
</head>
<body style="background:#cccccc;-moz-text-size-adjust:none; -webkit-text-size-adjust:none; -ms-text-size-adjust:none; ">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" >
<tr><td align="center">
<table width="650" border="0" align="center" cellpadding="0" cellspacing="0" class="mainWd" >
<tr><td height="25" align="center" valign="middle" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#ffffff; background:#2a4c75">Can’t see this email? View it in your browser. </td></tr>
<tr>
<td align="left" valign="top" class="bg" bgcolor="#ffffff">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr> <td height="20" align="left" valign="top"> <img src="http://'.$serverurl.'" alt=" " class="img" border="0" align="left" style="display:block;width:100%"></td> </tr>
<tr><td height="20" align="center" valign="top"> </td></tr>
<tr><td align="center" valign="top">
<table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td align="left" valign="top"><span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#343434; font-weight:normal;">Dear <span style="color:#343434;">user,</span><br /><br /> Please find your username below. You can use the ONM app to login to your account.</span>
<br /><br />
<span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#343434; font-weight:normal;">
<strong style="color:#f77635">Username:</strong> '.$username.' </span>
</td></tr>
<tr><td height="20" align="center" valign="top"> </td></tr>
<tr><td align="left" valign="top">
<span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#343434; font-weight:normal;">Best regards,<br />
Team ONM</span>
</td></tr>
<tr><td height="20" align="center" valign="top"> </td></tr>
</table>
</td>
</tr>
<tr><td align="center" valign="top" bgcolor="#e10000" >
<br />
<span style="font-family:Arial, Helvetica, sans-serif; font-size:26px; line-height:20px; color:#ffffff; font-weight:bold;"> Contact Us</span><br /><br />
<span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#ffffff; font-weight:normal;"> <strong style="text-transform:uppercase"> ONM.</strong> <br />
29 Tai Seng Avenue, #06-04A Natural Cool Lifestyle Hub, Singapore 534119.</span>
</td></tr>
<tr><td height="20" align="center" valign="top" bgcolor="#e10000" > </td></tr>
<tr>
<td align="center" valign="top" bgcolor="#e10000">
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" class="full_480" >
<tr>
<td align="center" valign="top"><table width="48%" border="0" align="left" cellpadding="0" cellspacing="0" class="full_480" >
<tr> <td width="13"><img src="http://'.$serverurl.'" width="9" height="13" /></td><td width="5"></td>
<td width="180" align="left" valign="middle" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#ffffff;"> Call us on: (65) 6479 7723 </td> </tr>
</table>
<table width="48%" border="0" align="right" cellpadding="0" cellspacing="0" class="full_480">
<tr>
<td width="13"><img src="http://'.$serverurl.'" width="13" height="10" /></td><td width="5"></td>
<td align="left" valign="middle" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#ffffff;"> Email us: <a href="mailto:info@onm.com" style="font-size:12px; color:#ffffff;">info@onm.com</a></td> </tr>
</table>
</td></tr>
<tr>
</tr>
</table></td></tr>
<tr><td height="20" align="center" valign="top" bgcolor="#e10000" > </td></tr>
</table>
</td>
</tr>
<tr> <td align="center" valign="middle" height="37 " bgcolor="#2a4c75" > <span style="font-family:Arial, Helvetica, sans-serif; font-size:12px ; color:#ffffff;-webkit-text-size-adjust: none;">Copyright © 2016. ONM All rights reserved </span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center"> </td>
</tr>
</table>
</body>
</html>
';
#####################################################
//echo $msg; die;
$subject = "Forgot Username Request";
sendmail($email,$subject,$msg,"info@onm.com");
echo json_encode(array("message"=>"1"));
}
else{
echo json_encode(array("message"=>"0"));
/*************** wrong email *************/
}
}
//}
############ END HERE ############################
############ Forgot Password Mail Request ########################
else{
$email = $_REQUEST['email'];
############################ CHECKING USERNAME EXIST OR NOT ##################
$sql_username = " SELECT id,username from ".$prefix."users where email = '".$email."' ";
$rs_username = mysql_query($sql_username);
$num_rows = mysql_num_rows($rs_username);
if($num_rows > 0){
############### send email for token ##############
$records = mysql_fetch_assoc($rs_username);
$id = $records['id'];
$secret = 'JfXAcjoH0jbAMqF4';
// Generate a new token
$random = genRandomPassword();
$randpasstemp = '';
for($ik=0; $ik<4; $ik++){
$randpasstemp .= chr(mt_rand(48,57));
}
$token = md5($randpasstemp);
$salt = getSalt('crypt-md5');
$hashedToken = md5($token.$salt).':'.$salt;
$query = "UPDATE ".$prefix."users SET activation = '".$token."' WHERE id = ". $id ." ";
mysql_query($query);
/*$msg .= "Hello,'.$records['id']";
$msg .= "<br><br>A request has been made to reset your account password. To reset your password, you will need to submit this verification code in order to verify that the request was legitimate.";
$msg .= "<br><br>The verification code is ".$randpasstemp."<br><br>";
//$msg .= $upload_fullpath."registration?view=reset&layout=confirm";
$msg .= "Go to Mobile App and Reset Your Password";
$msg .= "<br><br>Thank you."; */
######################################################
$msg = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width; maximum-scale=1.0;">
<title>RAS</title>
<style type="text/css">
body{ margin:0px; padding:0px;}
@media only screen and (max-width:598px){
table[class="mainWd"]{ width:100% !important; }
.img{ width:100% !important; }
}
@media only screen and (max-width:599px){
table{ float:none !important; }
table[class="mainWd"]{ width:100% !important; }
table[class="table-width"]{ float:left !important}
.img{ width:100% !important; }
@media only screen and (max-width:480px){
td[class="wd660"]{ width:100% !important; float:left !important; text-align:center !important; }
.img1{ display:none !important}
td[class="wd360"]{ width:100% !important; float:left !important; text-align:center; margin-bottom:20px; }
table[class="full_480"]{ width:220px !important; text-align:center !important; float:none !important; }
td[class="mob_hide"]{ display:none !important; }
}
.img {width:100% !important; }
.img {width:100% !important; }
</style>
</head>
<body style="background:#cccccc;-moz-text-size-adjust:none; -webkit-text-size-adjust:none; -ms-text-size-adjust:none; ">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" >
<tr><td align="center">
<table width="650" border="0" align="center" cellpadding="0" cellspacing="0" class="mainWd" >
<tr><td height="25" align="center" valign="middle" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#ffffff; background:#2a4c75">Can’t see this email? View it in your browser. </td></tr>
<tr>
<td align="left" valign="top" class="bg" bgcolor="#ffffff">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr> <td height="20" align="left" valign="top"> <img src="http://'.$serverurl.'" alt=" " class="img" border="0" align="left" style="display:block;width:100%"></td> </tr>
<tr><td height="20" align="center" valign="top"> </td></tr>
<tr><td align="center" valign="top">
<table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td align="left" valign="top"><span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#343434; font-weight:normal;">Dear <span style="color:#343434;">user,</span><br /><br /> We have received a request regarding the change of your password.Please use the <br/>ONM mobile app to do the same. </span>
<br /><br />
<span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#343434; font-weight:normal;">The verification code is <strong style="color:#f77635">'.$randpasstemp.'</strong></span> <br /> <br />
<span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#343434; font-weight:normal;">Please ignore this mail if you do not want to reset your password.</span>
</td></tr>
<tr><td height="20" align="center" valign="top"> </td></tr>
<tr><td align="left" valign="top">
<span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#343434; font-weight:normal;">Best regards,<br />
Team ONM</span>
</td></tr>
<tr><td height="20" align="center" valign="top"> </td></tr>
</table>
</td>
</tr>
<tr><td align="center" valign="top" bgcolor="#e10000" >
<br />
<span style="font-family:Arial, Helvetica, sans-serif; font-size:26px; line-height:20px; color:#ffffff; font-weight:bold;"> Contact Us</span><br /><br />
<span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#ffffff; font-weight:normal;"> <strong style="text-transform:uppercase"> ONM.</strong> <br />
29 Tai Seng Avenue, #06-04A Natural Cool Lifestyle Hub, Singapore 534119.</span>
</td></tr>
<tr><td height="20" align="center" valign="top" bgcolor="#e10000" > </td></tr>
<tr>
<td align="center" valign="top" bgcolor="#e10000">
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" class="full_480" >
<tr>
<td align="center" valign="top"><table width="48%" border="0" align="left" cellpadding="0" cellspacing="0" class="full_480" >
<tr> <td width="13"><img src="http://'.$serverurl.'/images/icon_mob.jpg" width="9" height="13" /></td><td width="5"></td>
<td width="180" align="left" valign="middle" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#ffffff;"> Call us on: (65) 6479 7723 </td> </tr>
</table>
<table width="48%" border="0" align="right" cellpadding="0" cellspacing="0" class="full_480">
<tr>
<td width="13"><img src="http://'.$serverurl.'" width="13" height="10" /></td><td width="5"></td>
<td align="left" valign="middle" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#ffffff;"> Email us: <a href="mailto:info@onm.com" style="font-size:12px; color:#ffffff;">info@onm.com</a></td> </tr>
</table>
</td></tr>
<tr>
</tr>
</table></td></tr>
<tr><td height="20" align="center" valign="top" bgcolor="#e10000" > </td></tr>
</table>
</td>
</tr>
<tr> <td align="center" valign="middle" height="37 " bgcolor="#2a4c75" > <span style="font-family:Arial, Helvetica, sans-serif; font-size:12px ; color:#ffffff;-webkit-text-size-adjust: none;">Copyright © 2016.ONM All rights reserved </span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center"> </td>
</tr>
</table>
</body>
</html>
';
#####################################################
// echo $msg; die;
$subject = "Forgot Password Request";
sendmail($email,$subject,$msg,"info@onm.com");
echo json_encode(array("message"=>"1"));
}
else{
echo json_encode(array("message"=>"0"));
/*************** wrong email *************/
}
}
#####################################################################################################
function genRandomPassword($length = 8){
$salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$base = strlen($salt);
$makepass = '';
/*
* Start with a cryptographic strength random string, then convert it to
* a string with the numeric base of the salt.
* Shift the base conversion on each character so the character
* distribution is even, and randomize the start shift so it's not
* predictable.
*/
$random = genRandomBytes($length + 1);
$shift = ord($random[0]);
for ($i = 1; $i <= $length; ++$i)
{
$makepass .= $salt[($shift + ord($random[$i])) % $base];
$shift += ord($random[$i]);
}
return $makepass;
}
function genRandomBytes($length = 16)
{
$sslStr = '';
/*
* if a secure randomness generator exists and we don't
* have a buggy PHP version use it.
*/
if (function_exists('openssl_random_pseudo_bytes'))
{
$sslStr = openssl_random_pseudo_bytes($length, $strong);
if ($strong)
{
return $sslStr;
}
}
/*
* Collect any entropy available in the system along with a number
* of time measurements of operating system randomness.
*/
$bitsPerRound = 2;
$maxTimeMicro = 400;
$shaHashLength = 20;
$randomStr = '';
$total = $length;
// Check if we can use /dev/urandom.
$urandom = false;
$handle = null;
// This is PHP 5.3.3 and up
if (function_exists('stream_set_read_buffer') && @is_readable('/dev/urandom'))
{
$handle = @fopen('/dev/urandom', 'rb');
if ($handle)
{
$urandom = true;
}
}
while ($length > strlen($randomStr))
{
$bytes = ($total > $shaHashLength)? $shaHashLength : $total;
$total -= $bytes;
/*
* Collect any entropy available from the PHP system and filesystem.
* If we have ssl data that isn't strong, we use it once.
*/
$entropy = rand() . uniqid(mt_rand(), true) . $sslStr;
$entropy .= implode('', @fstat(fopen(__FILE__, 'r')));
$entropy .= memory_get_usage();
$sslStr = '';
if ($urandom)
{
stream_set_read_buffer($handle, 0);
$entropy .= @fread($handle, $bytes);
}
else
{
/*
* There is no external source of entropy so we repeat calls
* to mt_rand until we are assured there's real randomness in
* the result.
*
* Measure the time that the operations will take on average.
*/
$samples = 3;
$duration = 0;
for ($pass = 0; $pass < $samples; ++$pass)
{
$microStart = microtime(true) * 1000000;
$hash = sha1(mt_rand(), true);
for ($count = 0; $count < 50; ++$count)
{
$hash = sha1($hash, true);
}
$microEnd = microtime(true) * 1000000;
$entropy .= $microStart . $microEnd;
if ($microStart > $microEnd)
{
$microEnd += 1000000;
}
$duration += $microEnd - $microStart;
}
$duration = $duration / $samples;
/*
* Based on the average time, determine the total rounds so that
* the total running time is bounded to a reasonable number.
*/
$rounds = (int) (($maxTimeMicro / $duration) * 50);
/*
* Take additional measurements. On average we can expect
* at least $bitsPerRound bits of entropy from each measurement.
*/
$iter = $bytes * (int) ceil(8 / $bitsPerRound);
for ($pass = 0; $pass < $iter; ++$pass)
{
$microStart = microtime(true);
$hash = sha1(mt_rand(), true);
for ($count = 0; $count < $rounds; ++$count)
{
$hash = sha1($hash, true);
}
$entropy .= $microStart . microtime(true);
}
}
$randomStr .= sha1($entropy, true);
}
if ($urandom)
{
@fclose($handle);
}
return substr($randomStr, 0, $length);
}
function getSalt($encryption = 'md5-hex', $seed = '', $plaintext = '')
{
// Encrypt the password.
switch ($encryption)
{
case 'crypt':
case 'crypt-des':
if ($seed)
{
return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 2);
}
else
{
return substr(md5(mt_rand()), 0, 2);
}
break;
case 'crypt-md5':
if ($seed)
{
return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
}
else
{
return '$1$' . substr(md5(mt_rand()), 0, 8) . '$';
}
break;
case 'crypt-blowfish':
if ($seed)
{
return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 16);
}
else
{
return '$2$' . substr(md5(mt_rand()), 0, 12) . '$';
}
break;
case 'ssha':
if ($seed)
{
return substr(preg_replace('|^{SSHA}|', '', $seed), -20);
}
else
{
return mhash_keygen_s2k(MHASH_SHA1, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
}
break;
case 'smd5':
if ($seed)
{
return substr(preg_replace('|^{SMD5}|', '', $seed), -16);
}
else
{
return mhash_keygen_s2k(MHASH_MD5, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
}
break;
case 'aprmd5': /* 64 characters that are valid for APRMD5 passwords. */
$APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
if ($seed)
{
return substr(preg_replace('/^\$apr1\$(.{8}).*/', '\\1', $seed), 0, 8);
}
else
{
$salt = '';
for ($i = 0; $i < 8; $i++)
{
$salt .= $APRMD5{rand(0, 63)};
}
return $salt;
}
break;
default:
$salt = '';
if ($seed)
{
$salt = $seed;
}
return $salt;
break;
}
}
function getCryptedPassword($plaintext, $salt = '', $encryption = 'md5-hex', $show_encrypt = false)
{
// Get the salt to use.
$salt = getSalt($encryption, $salt, $plaintext);
// Encrypt the password.
switch ($encryption)
{
case 'plain':
return $plaintext;
case 'sha':
$encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext));
return ($show_encrypt) ? '{SHA}' . $encrypted : $encrypted;
case 'crypt':
case 'crypt-des':
case 'crypt-md5':
case 'crypt-blowfish':
return ($show_encrypt ? '{crypt}' : '') . crypt($plaintext, $salt);
case 'md5-base64':
$encrypted = base64_encode(mhash(MHASH_MD5, $plaintext));
return ($show_encrypt) ? '{MD5}' . $encrypted : $encrypted;
case 'ssha':
$encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext . $salt) . $salt);
return ($show_encrypt) ? '{SSHA}' . $encrypted : $encrypted;
case 'smd5':
$encrypted = base64_encode(mhash(MHASH_MD5, $plaintext . $salt) . $salt);
return ($show_encrypt) ? '{SMD5}' . $encrypted : $encrypted;
case 'aprmd5':
$length = strlen($plaintext);
$context = $plaintext . '$apr1$' . $salt;
$binary = JUserHelper::_bin(md5($plaintext . $salt . $plaintext));
for ($i = $length; $i > 0; $i -= 16)
{
$context .= substr($binary, 0, ($i > 16 ? 16 : $i));
}
for ($i = $length; $i > 0; $i >>= 1)
{
$context .= ($i & 1) ? chr(0) : $plaintext[0];
}
$binary = JUserHelper::_bin(md5($context));
for ($i = 0; $i < 1000; $i++)
{
$new = ($i & 1) ? $plaintext : substr($binary, 0, 16);
if ($i % 3)
{
$new .= $salt;
}
if ($i % 7)
{
$new .= $plaintext;
}
$new .= ($i & 1) ? substr($binary, 0, 16) : $plaintext;
$binary = JUserHelper::_bin(md5($new));
}
$p = array();
for ($i = 0; $i < 5; $i++)
{
$k = $i + 6;
$j = $i + 12;
if ($j == 16)
{
$j = 5;
}
$p[] = JUserHelper::_toAPRMD5((ord($binary[$i]) << 16) | (ord($binary[$k]) << 8) | (ord($binary[$j])), 5);
}
return '$apr1$' . $salt . '$' . implode('', $p) . JUserHelper::_toAPRMD5(ord($binary[11]), 3);
case 'md5-hex':
default:
$encrypted = ($salt) ? md5($plaintext . $salt) : md5($plaintext);
return ($show_encrypt) ? '{MD5}' . $encrypted : $encrypted;
}
}
function sendmail($to,$subject,$message,$from){
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' .$from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
@mail($to,$subject,$message,$headers);
}
?>