apiaccountupdateprofileimage.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Update the authenticating user's profile image
  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. * Updates the authenticating user's profile image. Note that this API method
  34. * expects raw multipart data, not a URL to an image.
  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 ApiAccountUpdateProfileImageAction extends ApiAuthAction
  43. {
  44. protected $needPost = true;
  45. /**
  46. * Handle the request
  47. *
  48. * Check whether the credentials are valid and output the result
  49. *
  50. * @return void
  51. */
  52. protected function handle()
  53. {
  54. parent::handle();
  55. // Workaround for PHP returning empty $_POST and $_FILES when POST
  56. // length > post_max_size in php.ini
  57. if (empty($_FILES)
  58. && empty($_POST)
  59. && ($_SERVER['CONTENT_LENGTH'] > 0)
  60. ) {
  61. // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
  62. // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
  63. $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
  64. 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
  65. intval($_SERVER['CONTENT_LENGTH']));
  66. $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
  67. }
  68. if (empty($this->user)) {
  69. // TRANS: Client error displayed updating profile image without having a user object.
  70. $this->clientError(_('No such user.'), 404);
  71. }
  72. try {
  73. $imagefile = ImageFile::fromUpload('image');
  74. } catch (Exception $e) {
  75. $this->clientError($e->getMessage());
  76. }
  77. $type = $imagefile->preferredType();
  78. $filename = Avatar::filename(
  79. $user->id,
  80. image_type_to_extension($type),
  81. null,
  82. 'tmp'.common_timestamp()
  83. );
  84. $filepath = Avatar::path($filename);
  85. $imagefile->copyTo($filepath);
  86. $profile = $this->user->getProfile();
  87. $profile->setOriginal($filename);
  88. $twitter_user = $this->twitterUserArray($profile, true);
  89. if ($this->format == 'xml') {
  90. $this->initDocument('xml');
  91. $this->showTwitterXmlUser($twitter_user, 'user', true);
  92. $this->endDocument('xml');
  93. } elseif ($this->format == 'json') {
  94. $this->initDocument('json');
  95. $this->showJsonObjects($twitter_user);
  96. $this->endDocument('json');
  97. }
  98. }
  99. }