| 1 |
<?php |
| 2 |
# |
| 3 |
# PHP routine to set new password for user |
| 4 |
# |
| 5 |
|
| 6 |
# |
| 7 |
# MD5 Hash |
| 8 |
# |
| 9 |
# Linux command line: echo -n "string" | sha1sum |
| 10 |
# PHP command: sha1("string"); |
| 11 |
# |
| 12 |
|
| 13 |
# |
| 14 |
# Get 'posted' password information |
| 15 |
# |
| 16 |
$QCUser = $_POST['username']; |
| 17 |
$QColdpass = sha1($_POST['oldpass']); |
| 18 |
$QCnewpass = sha1($_POST['newpass']); |
| 19 |
|
| 20 |
# |
| 21 |
# If unable to open account file, return error message |
| 22 |
# |
| 23 |
$authfile = "account"; |
| 24 |
$authorized = 0; |
| 25 |
$fh = fopen($authfile,'r'); |
| 26 |
if (! $fh) { |
| 27 |
echo "Unable to open account file"; |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
# |
| 32 |
# Attempt to verify username/password against data in account file. |
| 33 |
# |
| 34 |
$contents = array(); |
| 35 |
while ( !feof($fh)) { |
| 36 |
$line = fgets($fh); |
| 37 |
$contents[] = $line; |
| 38 |
$info = split(",",$line); |
| 39 |
if ( ($QCUser == trim($info[0]) ) && ($QColdpass == trim($info[1]) ) ) { |
| 40 |
$authorized = 1; |
| 41 |
} |
| 42 |
} |
| 43 |
fclose($fh); |
| 44 |
|
| 45 |
# |
| 46 |
# If hash of old password does not match value in file, return error |
| 47 |
# |
| 48 |
if ( $authorized != 1) { |
| 49 |
echo "Old password does not match password on file."; |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
# |
| 54 |
# Update contents of authorization file stored in memory |
| 55 |
# |
| 56 |
$i=0; |
| 57 |
while ($contents[$i]) { |
| 58 |
$info = split(",",$contents[$i]); |
| 59 |
if ($QCUser == trim($info[0])) { |
| 60 |
$contents[$i] = "$QCUser, $QCnewpass\n"; |
| 61 |
} |
| 62 |
$i++; |
| 63 |
} |
| 64 |
|
| 65 |
# |
| 66 |
# Attempt to write username/password data to account file. |
| 67 |
# |
| 68 |
$fh = fopen($authfile,'w'); |
| 69 |
if (! $fh) { |
| 70 |
echo "Unable to open account file for writing."; |
| 71 |
return; |
| 72 |
} |
| 73 |
for ($i=0; $i<count($contents); $i++ ) { |
| 74 |
fwrite($fh, $contents[$i]); |
| 75 |
} |
| 76 |
fclose($fh); |
| 77 |
echo 0; |
| 78 |
return; |
| 79 |
|
| 80 |
?> |