edit_profile.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // TODO: Dont pass user as argument, use currently logged in one.
  28. $user = filter_input(INPUT_POST, 'user');
  29. if (filter_input(INPUT_POST, "mail")){
  30. $mail = filter_input(INPUT_POST, 'mail');
  31. if (!filter_var($mail, FILTER_VALIDATE_EMAIL)){
  32. return -1;
  33. }
  34. $s = $db->prepare('UPDATE user SET mail = :mail WHERE id = :user;');
  35. $s->bindValue(':user', $user, SQLITE3_TEXT);
  36. $s->bindValue(':mail', $mail, SQLITE3_TEXT);
  37. if(!$s->execute()){
  38. return -2;
  39. }
  40. }
  41. if (filter_input(INPUT_POST, "pass")){
  42. $pass = sha1(filter_input(INPUT_POST, 'pass'));
  43. $currentPass = sha1(filter_input(INPUT_POST, 'currentPass'));
  44. $s = $db->prepare('SELECT COUNT(id) AS count FROM user WHERE id = :id AND password = :currentPass;');
  45. $s->bindValue(':id', $user, SQLITE3_TEXT);
  46. $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
  47. $q = $s->execute();
  48. $r = $q->fetchArray(SQLITE3_ASSOC);
  49. if ($r["count"] != 1){
  50. return -3;
  51. }
  52. $s = $db->prepare('UPDATE user SET password = :pass WHERE id = :id AND password = :currentPass;');
  53. $s->bindValue(':id', $user, SQLITE3_TEXT);
  54. $s->bindValue(':pass', $pass, SQLITE3_TEXT);
  55. $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
  56. if(!$s->execute()){
  57. return -4;
  58. }
  59. }
  60. if (filter_input(INPUT_POST, "api")){
  61. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  62. $charactersLength = 16;
  63. $api = '';
  64. for ($i = 0; $i < $charactersLength; $i++) {
  65. $api .= $characters[rand(0, $charactersLength - 1)];
  66. }
  67. $s = $db->prepare('UPDATE user SET api_key = :api WHERE id = :id;');
  68. $s->bindValue(':id', $user, SQLITE3_TEXT);
  69. $s->bindValue(':api', $api, SQLITE3_TEXT);
  70. if(!$s->execute()){
  71. return -5;
  72. }
  73. $response = $api;
  74. }
  75. if ($response == null){
  76. return 0;
  77. }
  78. else{
  79. return $response;
  80. }
  81. }
  82. ?>