edit_profile.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. /**
  12. * Executes the profile update.
  13. *
  14. * Reads the POST parameters looking for the following KEYS:
  15. * mail: Optional, email to update.
  16. * pass: Optional, password to update.
  17. * currentPass: Mandatory for changing password, current one.
  18. * api: Any value, indicates that the api key is to be changed.
  19. * Then it updates the selected info with the prameter vlue. Multiple itemss can be updated at the
  20. * same time.
  21. *
  22. * @return int|string 201 on success, HTTP error codes on failure. If the API key has been updated,
  23. * the new key is returned instead.
  24. * @category Action
  25. */
  26. function action(){
  27. $response = null;
  28. // Get the current user ID.
  29. $user = get_context()->get_user()->get_id();
  30. if (filter_input(INPUT_POST, "mail")){
  31. $mail = filter_input(INPUT_POST, 'mail');
  32. if (!filter_var($mail, FILTER_VALIDATE_EMAIL)){
  33. return -1;
  34. }
  35. $s = get_context()->get_db()->prepare('UPDATE user SET mail = :mail WHERE id = :user;');
  36. $s->bindValue(':user', $user, SQLITE3_TEXT);
  37. $s->bindValue(':mail', $mail, SQLITE3_TEXT);
  38. if(!$s->execute()){
  39. return 500;
  40. }
  41. }
  42. if (filter_input(INPUT_POST, "pass")){
  43. $pass = sha1(filter_input(INPUT_POST, 'pass'));
  44. $currentPass = sha1(filter_input(INPUT_POST, 'currentPass'));
  45. $s = get_context()->get_db()->prepare('SELECT COUNT(id) AS count FROM user WHERE id = :id AND password = :currentPass;');
  46. $s->bindValue(':id', $user, SQLITE3_TEXT);
  47. $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
  48. $q = $s->execute();
  49. $r = $q->fetchArray(SQLITE3_ASSOC);
  50. if ($r["count"] != 1){
  51. return -500;
  52. }
  53. $s = get_context()->get_db()->prepare('UPDATE user SET password = :pass WHERE id = :id AND password = :currentPass;');
  54. $s->bindValue(':id', $user, SQLITE3_TEXT);
  55. $s->bindValue(':pass', $pass, SQLITE3_TEXT);
  56. $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
  57. if(!$s->execute()){
  58. return -4;
  59. }
  60. }
  61. if (filter_input(INPUT_POST, "api")){
  62. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  63. $charactersLength = 16;
  64. $api = '';
  65. for ($i = 0; $i < $charactersLength; $i++) {
  66. $api .= $characters[rand(0, $charactersLength - 1)];
  67. }
  68. $s = get_context()->get_db()->prepare('UPDATE user SET api_key = :api WHERE id = :id;');
  69. $s->bindValue(':id', $user, SQLITE3_TEXT);
  70. $s->bindValue(':api', $api, SQLITE3_TEXT);
  71. if(! $s->execute()){
  72. return 500;
  73. }
  74. $response = $api;
  75. }
  76. if ($response == null){
  77. return 201;
  78. }
  79. else{
  80. return $response;
  81. }
  82. }