edit_profile.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * @return int|string 0 on success, negative values on error. If the API
  20. * key has been updated, the new key.
  21. * @category Action
  22. * @global resource Database connection.
  23. */
  24. function action(){
  25. global $db;
  26. $response = null;
  27. $player = filter_input(INPUT_POST, 'uid');
  28. if (filter_input(INPUT_POST, "mail")){
  29. $mail = filter_input(INPUT_POST, 'mail');
  30. if (!filter_var($mail, FILTER_VALIDATE_EMAIL)){
  31. return -1;
  32. }
  33. $s = $db->prepare('UPDATE player SET mail = :mail WHERE uid = :uid;');
  34. $s->bindValue(':uid', $player, SQLITE3_TEXT);
  35. $s->bindValue(':mail', $mail, SQLITE3_TEXT);
  36. if(!$s->execute()){
  37. return -2;
  38. }
  39. }
  40. if (filter_input(INPUT_POST, "pass")){
  41. $pass = sha1(filter_input(INPUT_POST, 'pass'));
  42. $currentPass = sha1(filter_input(INPUT_POST, 'currentPass'));
  43. $s = $db->prepare('SELECT COUNT(uid) AS count FROM player WHERE uid = :uid AND password = :currentPass;');
  44. $s->bindValue(':uid', $player, SQLITE3_TEXT);
  45. $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
  46. $q = $s->execute();
  47. $r = $q->fetchArray(SQLITE3_ASSOC);
  48. if ($r["count"] != 1){
  49. return -3;
  50. }
  51. $s = $db->prepare('UPDATE player SET password = :pass WHERE uid = :uid AND password = :currentPass;');
  52. $s->bindValue(':uid', $player, SQLITE3_TEXT);
  53. $s->bindValue(':pass', $pass, SQLITE3_TEXT);
  54. $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
  55. if(!$s->execute()){
  56. return -4;
  57. }
  58. }
  59. if (filter_input(INPUT_POST, "api")){
  60. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  61. $charactersLength = 16;
  62. $api = '';
  63. for ($i = 0; $i < $charactersLength; $i++) {
  64. $api .= $characters[rand(0, $charactersLength - 1)];
  65. }
  66. $s = $db->prepare('UPDATE player SET api_key = :api WHERE uid = :uid;');
  67. $s->bindValue(':uid', $player, SQLITE3_TEXT);
  68. $s->bindValue(':api', $api, SQLITE3_TEXT);
  69. if(!$s->execute()){
  70. return -5;
  71. }
  72. $response = $api;
  73. }
  74. if ($response == null){
  75. return 0;
  76. }
  77. else{
  78. return $response;
  79. }
  80. }
  81. ?>