avatar.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Retrieve user avatar by filename action class.
  18. *
  19. * @category Action
  20. * @package GNUsocial
  21. *
  22. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  23. */
  24. defined('GNUSOCIAL') || die;
  25. /**
  26. * Retrieve user avatar by filename action class.
  27. *
  28. * @category Action
  29. * @package GNUsocial
  30. *
  31. * @author Evan Prodromou <evan@status.net>
  32. * @author Robin Millette <millette@status.net>
  33. * @author Mikael Nordfeldth <mmn@hethane.se>
  34. * @author Hugo Sales <hugo@fc.up.pt>
  35. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  36. *
  37. * @see http://www.gnu.org/software/social/
  38. */
  39. class AvatarAction extends Action
  40. {
  41. public $filename = null;
  42. public $filepath = null;
  43. public $mimetype = null;
  44. protected function prepare(array $args = [])
  45. {
  46. parent::prepare($args);
  47. $this->filename = File::tryFilename($this->trimmed('file'));
  48. $this->filepath = File::path($this->filename, common_config('avatar', 'dir'), false);
  49. if (!file_exists($this->filepath)) {
  50. // TRANS: Client error displayed trying to get a non-existing avatar.
  51. $this->clientError(_m('No such avatar.'), 404);
  52. }
  53. $this->mimetype = (new ImageFile(-1, $this->filepath))->mimetype;
  54. return true;
  55. }
  56. protected function handle()
  57. {
  58. parent::handle();
  59. common_send_file($this->filepath, $this->mimetype, $this->filename, 'inline');
  60. }
  61. }