Edit_Profile_Action.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. require_once(PATH::ACTION . "Action.php");
  12. /**
  13. * Executes the profile update.
  14. *
  15. * Reads the POST parameters looking for the following KEYS:
  16. * mail: Optional, email to update.
  17. * pass: Optional, password to update.
  18. * currentPass: Mandatory for changing password, current one.
  19. * api: Any value, indicates that the api key is to be changed.
  20. * Then it updates the selected info with the prameter vlue. Multiple itemss can be updated at the
  21. * same time.
  22. *
  23. * @return int|string 201 on success, HTTP error codes on failure. If the API key has been updated,
  24. * the new key is returned instead.
  25. * @category Action
  26. */
  27. class Edit_Profile_Action extends Action{
  28. /**
  29. * Executes the action.
  30. */
  31. function execute(){
  32. // Get the current user ID.
  33. $user = get_context()->get_user()->get_id();
  34. if (filter_input(INPUT_POST, "mail")){
  35. $mail = filter_input(INPUT_POST, 'mail');
  36. if (! filter_var($mail, FILTER_VALIDATE_EMAIL)){
  37. $this->code = 400;
  38. $this->message = "Invalid email address.";
  39. return;
  40. }
  41. $s = get_context()->get_db()->prepare('UPDATE user SET mail = :mail WHERE id = :user;');
  42. $s->bindValue(':user', $user, SQLITE3_TEXT);
  43. $s->bindValue(':mail', $mail, SQLITE3_TEXT);
  44. $this->code = 500;
  45. $this->message = "Internal error.";
  46. return;
  47. }
  48. if (filter_input(INPUT_POST, "pass")){
  49. $pass = sha1(filter_input(INPUT_POST, 'pass'));
  50. $currentPass = sha1(filter_input(INPUT_POST, 'currentPass'));
  51. $s = get_context()->get_db()->prepare('SELECT COUNT(id) AS count FROM user WHERE id = :id AND password = :currentPass;');
  52. $s->bindValue(':id', $user, SQLITE3_TEXT);
  53. $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
  54. $q = $s->execute();
  55. $r = $q->fetchArray(SQLITE3_ASSOC);
  56. if ($r["count"] != 1){
  57. $this->code = 500;
  58. $this->message = "Internal error.";
  59. return;
  60. }
  61. $s = get_context()->get_db()->prepare('UPDATE user SET password = :pass WHERE id = :id AND password = :currentPass;');
  62. $s->bindValue(':id', $user, SQLITE3_TEXT);
  63. $s->bindValue(':pass', $pass, SQLITE3_TEXT);
  64. $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
  65. if(!$s->execute()){
  66. $this->code = 500;
  67. $this->message = "Internal error.";
  68. return;
  69. }
  70. }
  71. if (filter_input(INPUT_POST, "api")){
  72. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  73. $charactersLength = 16;
  74. $api = '';
  75. for ($i = 0; $i < $charactersLength; $i++) {
  76. $api .= $characters[rand(0, $charactersLength - 1)];
  77. }
  78. $s = get_context()->get_db()->prepare('UPDATE user SET api_key = :api WHERE id = :id;');
  79. $s->bindValue(':id', $user, SQLITE3_TEXT);
  80. $s->bindValue(':api', $api, SQLITE3_TEXT);
  81. if(! $s->execute()){
  82. $this->code = 500;
  83. $this->message = "Internal error.";
  84. return;
  85. }
  86. $this->code = 200;
  87. $this->message = "OK.";
  88. $this->output = $api;
  89. }
  90. }
  91. }