| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
function encrypt_passwords() |
|---|
| 4 |
{ |
|---|
| 5 |
global $db; |
|---|
| 6 |
$pwds = 0; |
|---|
| 7 |
outn("Updating administrators passwords..."); |
|---|
| 8 |
$sql = "SELECT * FROM ampusers"; |
|---|
| 9 |
$users = $db->getAll($sql,NULL,DB_FETCHMODE_ASSOC); |
|---|
| 10 |
if (DB::IsError($users)) { // Error while getting the users list to update... bad |
|---|
| 11 |
die($users->getMessage()); |
|---|
| 12 |
} else { |
|---|
| 13 |
outn(count($users)." accounts to update..."); |
|---|
| 14 |
foreach ($users as $index => $ufields) |
|---|
| 15 |
{ |
|---|
| 16 |
$sql = "UPDATE ampusers SET password_sha256='".hash("sha256",$ufields['password'])."' WHERE username='".$ufields['username']."'"; |
|---|
| 17 |
$result = $db->query($sql); |
|---|
| 18 |
if (DB::IsError($result)) { |
|---|
| 19 |
outn("Error while updating account: ".$ufields['username']." (".$result->getMessage.")"); |
|---|
| 20 |
} else { |
|---|
| 21 |
$pwds++; |
|---|
| 22 |
} |
|---|
| 23 |
} |
|---|
| 24 |
} |
|---|
| 25 |
outn($pwds." accounts updated."); |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
$sql = "SELECT password_sha256 FROM ampusers"; |
|---|
| 29 |
$passfield = $db->getRow($sql, DB_FETCHMODE_ASSOC); |
|---|
| 30 |
if (!DB::IsError($passfield)) { // no error... Already done |
|---|
| 31 |
$sql = "SELECT password FROM ampusers"; |
|---|
| 32 |
$passfield = $db->getRow($sql, DB_FETCHMODE_ASSOC); |
|---|
| 33 |
if (DB::IsError($passfield)) { //password field do not exist, done |
|---|
| 34 |
outn("Field password_sha265 exists, field password do not. No change needed."); |
|---|
| 35 |
} else { //Field password still exist, update of passwords is needed. |
|---|
| 36 |
encrypt_passwords(); |
|---|
| 37 |
} |
|---|
| 38 |
} else { |
|---|
| 39 |
if ($passfield->code == DB_ERROR_NOSUCHFIELD) { |
|---|
| 40 |
outn("Creating field password_sha256..."); |
|---|
| 41 |
$sql = "ALTER TABLE ampusers ADD password_sha256 VARCHAR ( 64 ) NOT NULL AFTER password"; |
|---|
| 42 |
$results = $db->query($sql); |
|---|
| 43 |
if (DB::IsError($results)) { |
|---|
| 44 |
die($results->getMessage()); |
|---|
| 45 |
} else { |
|---|
| 46 |
outn("Field Created."); |
|---|
| 47 |
encrypt_passwords(); |
|---|
| 48 |
outn("Deleting old field password..."); |
|---|
| 49 |
$sql = "ALTER TABLE ampusers DROP password"; |
|---|
| 50 |
$results = $db->query($sql); |
|---|
| 51 |
if (DB::IsError($results)) { |
|---|
| 52 |
die($results->getMessage()); |
|---|
| 53 |
} else { |
|---|
| 54 |
outn("Done."); |
|---|
| 55 |
} |
|---|
| 56 |
} |
|---|
| 57 |
} else { //The error was not about the field... |
|---|
| 58 |
die($passfield->getMessage()); |
|---|
| 59 |
} |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
?> |
|---|