apiuserprofileimage.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Return a user's avatar 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 Brion Vibber <brion@status.net>
  25. * @copyright 2010 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. * Ouputs avatar URL for a user, specified by screen name.
  34. * Unlike most API endpoints, this returns an HTTP redirect rather than direct data.
  35. *
  36. * @category API
  37. * @package StatusNet
  38. * @author Brion Vibber <brion@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 ApiUserProfileImageAction extends ApiPrivateAuthAction
  43. {
  44. /**
  45. * Take arguments for running
  46. *
  47. * @param array $args $_REQUEST args
  48. *
  49. * @return boolean success flag
  50. *
  51. */
  52. protected function prepare(array $args=array())
  53. {
  54. parent::prepare($args);
  55. $user = User::getKV('nickname', $this->arg('screen_name'));
  56. if (!($user instanceof User)) {
  57. // TRANS: Client error displayed when requesting user information for a non-existing user.
  58. $this->clientError(_('User not found.'), 404);
  59. }
  60. $this->target = $user->getProfile();
  61. $this->size = $this->arg('size');
  62. return true;
  63. }
  64. /**
  65. * Handle the request
  66. *
  67. * Check the format and show the user info
  68. *
  69. * @return void
  70. */
  71. protected function handle()
  72. {
  73. parent::handle();
  74. $size = $this->avatarSize();
  75. $url = $this->target->avatarUrl($size);
  76. // We don't actually output JSON or XML data -- redirect!
  77. common_redirect($url, 302);
  78. }
  79. /**
  80. * Get the appropriate pixel size for an avatar based on the request...
  81. *
  82. * @return int
  83. */
  84. private function avatarSize()
  85. {
  86. switch ($this->size) {
  87. case 'mini':
  88. return AVATAR_MINI_SIZE; // 24x24
  89. case 'bigger':
  90. return AVATAR_PROFILE_SIZE; // Twitter does 73x73, but we do 96x96
  91. case 'normal': // fall through
  92. default:
  93. return AVATAR_STREAM_SIZE; // 48x48
  94. }
  95. }
  96. /**
  97. * Return true if read only.
  98. *
  99. * MAY override
  100. *
  101. * @param array $args other arguments
  102. *
  103. * @return boolean is read only action?
  104. */
  105. function isReadOnly($args)
  106. {
  107. return true;
  108. }
  109. }