edit_profile.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // TODO: Validate mail
  30. $s = $db->prepare('UPDATE player SET mail = :mail WHERE uid = :uid;');
  31. $s->bindValue(':uid', $player);
  32. $s->bindValue(':mail', $mail);
  33. if(!$s->execute()){
  34. return -1;
  35. }
  36. }
  37. if (filter_input(INPUT_POST, "pass")){
  38. $pass = sha1(filter_input(INPUT_POST, 'pass'));
  39. $currentPass = sha1(filter_input(INPUT_POST, 'currentPass'));
  40. // TODO: Validate current
  41. $s = $db->prepare('UPDATE player SET password = :pass WHERE uid = :uid AND password = :currentPass;');
  42. $s->bindValue(':uid', $player);
  43. $s->bindValue(':pass', $mail);
  44. $s->bindValue(':currentPass', $currentPass);
  45. if(!$s->execute()){
  46. return -2;
  47. }
  48. }
  49. if (filter_input(INPUT_POST, "api")){
  50. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  51. $charactersLength =16;
  52. $api = '';
  53. for ($i = 0; $i < $length; $i++) {
  54. $api .= $characters[rand(0, $charactersLength - 1)];
  55. }
  56. $s = $db->prepare('UPDATE player SET api_key = :api WHERE uid = :uid;');
  57. $s->bindValue(':uid', $player);
  58. $s->bindValue(':api', $api);
  59. if(!$s->execute()){
  60. return -3;
  61. }
  62. $response = $api;
  63. }
  64. if ($response == null){
  65. return 0;
  66. }
  67. else{
  68. return $response;
  69. }
  70. }
  71. ?>