edit_profile.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * File for the profile edit action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @category Action
  8. */
  9. /**
  10. * Executes the profile update.
  11. *
  12. * Reads the POST parameters looking for the following KEYS:
  13. * mail
  14. * pass + currentPass
  15. * api
  16. * Then it updates the selected info with the prameter vlue. Multiple
  17. * itemas can be updated at the same time.
  18. *
  19. * @param resource $db Database connection.
  20. * @return int|string 0 on success, negative values on error. If the API
  21. * key has been updated, the new key.
  22. * @category Action
  23. */
  24. function action($db = null){
  25. $response = null;
  26. $player = filter_input(INPUT_POST, 'uid');
  27. if (filter_input(INPUT_POST, "mail")){
  28. $mail = filter_input(INPUT_POST, 'mail');
  29. if (!filter_var($mail, FILTER_VALIDATE_EMAIL)){
  30. return -1;
  31. }
  32. $s = $db->prepare('UPDATE player SET mail = :mail WHERE uid = :uid;');
  33. $s->bindValue(':uid', $player);
  34. $s->bindValue(':mail', $mail);
  35. if(!$s->execute()){
  36. return -2;
  37. }
  38. }
  39. if (filter_input(INPUT_POST, "pass")){
  40. $pass = sha1(filter_input(INPUT_POST, 'pass'));
  41. $currentPass = sha1(filter_input(INPUT_POST, 'currentPass'));
  42. $s = $db->prepare('SELECT COUNT(uid) AS count FROM player WHERE uid = :uid AND password = :currentPass;');
  43. $s->bindValue(':uid', $player);
  44. $s->bindValue(':currentPass', $currentPass);
  45. $q = $s->execute();
  46. $r = $q->fetchArray(SQLITE3_ASSOC);
  47. if ($r["count"] != 1){
  48. return -3;
  49. }
  50. $s = $db->prepare('UPDATE player SET password = :pass WHERE uid = :uid AND password = :currentPass;');
  51. $s->bindValue(':uid', $player);
  52. $s->bindValue(':pass', $mail);
  53. $s->bindValue(':currentPass', $currentPass);
  54. if(!$s->execute()){
  55. return -4;
  56. }
  57. }
  58. if (filter_input(INPUT_POST, "api")){
  59. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  60. $charactersLength = 16;
  61. $api = '';
  62. for ($i = 0; $i < $length; $i++) {
  63. $api .= $characters[rand(0, $charactersLength - 1)];
  64. }
  65. $s = $db->prepare('UPDATE player SET api_key = :api WHERE uid = :uid;');
  66. $s->bindValue(':uid', $player);
  67. $s->bindValue(':api', $api);
  68. if(!$s->execute()){
  69. return -5;
  70. }
  71. $response = $api;
  72. }
  73. if ($response == null){
  74. return 0;
  75. }
  76. else{
  77. return $response;
  78. }
  79. }
  80. ?>