apiaccountupdateprofile.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Update the authenticating user's profile
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category API
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * API analog to the profile settings page
  34. * Only the parameters specified will be updated.
  35. *
  36. * @category API
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class ApiAccountUpdateProfileAction extends ApiAuthAction
  43. {
  44. protected $needPost = true;
  45. /**
  46. * Take arguments for running
  47. *
  48. * @param array $args $_REQUEST args
  49. *
  50. * @return boolean success flag
  51. */
  52. protected function prepare(array $args=array())
  53. {
  54. parent::prepare($args);
  55. $this->user = $this->auth_user;
  56. $this->name = $this->trimmed('name');
  57. $this->url = $this->trimmed('url');
  58. $this->location = $this->trimmed('location');
  59. $this->description = $this->trimmed('description');
  60. return true;
  61. }
  62. /**
  63. * Handle the request
  64. *
  65. * See which request params have been set, and update the profile
  66. *
  67. * @return void
  68. */
  69. protected function handle()
  70. {
  71. parent::handle();
  72. if (!in_array($this->format, array('xml', 'json'))) {
  73. // TRANS: Client error displayed when coming across a non-supported API method.
  74. $this->clientError(_('API method not found.'), 404);
  75. }
  76. if (empty($this->user)) {
  77. // TRANS: Client error displayed if a user could not be found.
  78. $this->clientError(_('No such user.'), 404);
  79. }
  80. $profile = $this->user->getProfile();
  81. if (empty($profile)) {
  82. // TRANS: Error message displayed when referring to a user without a profile.
  83. $this->clientError(_('User has no profile.'));
  84. }
  85. $original = clone($profile);
  86. $profile->fullname = $this->name;
  87. $profile->homepage = $this->url;
  88. $profile->bio = $this->description;
  89. $profile->location = $this->location;
  90. if (!empty($this->location)) {
  91. $loc = Location::fromName($this->location);
  92. if (!empty($loc)) {
  93. $profile->lat = $loc->lat;
  94. $profile->lon = $loc->lon;
  95. $profile->location_id = $loc->location_id;
  96. $profile->location_ns = $loc->location_ns;
  97. }
  98. } else {
  99. // location is empty so reset the extrapolated information too
  100. $profile->lat = '';
  101. $profile->lon = '';
  102. $profile->location_id = '';
  103. $profile->location_ns = '';
  104. }
  105. $result = $profile->update($original);
  106. if (!$result) {
  107. common_log_db_error($profile, 'UPDATE', __FILE__);
  108. // TRANS: Server error displayed if a user profile could not be saved.
  109. $this->serverError(_('Could not save profile.'));
  110. }
  111. $twitter_user = $this->twitterUserArray($profile, true);
  112. if ($this->format == 'xml') {
  113. $this->initDocument('xml');
  114. $this->showTwitterXmlUser($twitter_user, 'user', true);
  115. $this->endDocument('xml');
  116. } elseif ($this->format == 'json') {
  117. $this->initDocument('json');
  118. $this->showJsonObjects($twitter_user);
  119. $this->endDocument('json');
  120. }
  121. }
  122. }