Avatar.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Component\Avatar\Controller;
  19. use App\Core\Controller;
  20. use App\Core\DB\DB;
  21. use App\Core\Event;
  22. use App\Core\Form;
  23. use App\Core\GSFile;
  24. use App\Core\GSFile as M;
  25. use function App\Core\I18n\_m;
  26. use App\Core\Log;
  27. use App\Util\Common;
  28. use App\Util\Exception\ClientException;
  29. use App\Util\Exception\NotFoundException;
  30. use App\Util\TemporaryFile;
  31. use Component\Avatar\Entity\Avatar as AvatarEntity;
  32. use Exception;
  33. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  34. use Symfony\Component\Form\Extension\Core\Type\FileType;
  35. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  36. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  37. use Symfony\Component\Form\FormError;
  38. use Symfony\Component\HttpFoundation\Request;
  39. use Symfony\Component\HttpFoundation\Response;
  40. class Avatar extends Controller
  41. {
  42. /**
  43. * @throws Exception
  44. */
  45. public function avatar_view(Request $request, int $gsactor_id, string $size): Response
  46. {
  47. switch ($size) {
  48. case 'full':
  49. $res = \Component\Avatar\Avatar::getAvatarFileInfo($gsactor_id);
  50. return M::sendFile($res['filepath'], $res['mimetype'], $res['title']);
  51. default:
  52. throw new Exception('Not implemented');
  53. }
  54. }
  55. /**
  56. * Local user avatar panel
  57. */
  58. public static function settings_avatar(Request $request): array
  59. {
  60. $form = Form::create([
  61. ['avatar', FileType::class, ['label' => _m('Avatar'), 'help' => _m('You can upload your personal avatar. The maximum file size is 2MB.'), 'multiple' => false, 'required' => false]],
  62. ['remove', CheckboxType::class, ['label' => _m('Remove avatar'), 'help' => _m('Remove your avatar and use the default one'), 'required' => false, 'value' => false]],
  63. ['hidden', HiddenType::class, []],
  64. ['save_avatar', SubmitType::class, ['label' => _m('Submit')]],
  65. ]);
  66. $form->handleRequest($request);
  67. if ($form->isSubmitted() && $form->isValid()) {
  68. $data = $form->getData();
  69. $user = Common::user();
  70. $gsactor_id = $user->getId();
  71. if ($data['remove'] == true) {
  72. try {
  73. $avatar = DB::findOneBy('avatar', ['gsactor_id' => $gsactor_id]);
  74. $avatar->delete();
  75. Event::handle('AvatarUpdate', [$user->getId()]);
  76. } catch (NotFoundException) {
  77. $form->addError(new FormError(_m('No avatar set, so cannot delete')));
  78. }
  79. } else {
  80. if (isset($data['hidden'])) {
  81. // Cropped client side
  82. $matches = [];
  83. if (!empty(preg_match('/data:([^;]*)(;(base64))?,(.*)/', $data['hidden'], $matches))) {
  84. list(, , , $encoding_user, $data_user) = $matches;
  85. if ($encoding_user === 'base64') {
  86. $data_user = base64_decode($data_user);
  87. $tempfile = new TemporaryFile(['prefix' => 'gs-avatar']);
  88. $tempfile->write($data_user);
  89. } else {
  90. Log::info('Avatar upload got an invalid encoding, something\'s fishy and/or wrong');
  91. }
  92. }
  93. } elseif (isset($data['avatar'])) {
  94. // Cropping failed (e.g. disabled js), use file as uploaded
  95. $file = $data['avatar'];
  96. } else {
  97. throw new ClientException('Invalid form');
  98. }
  99. $attachment = GSFile::sanitizeAndStoreFileAsAttachment(
  100. $file
  101. );
  102. // Delete current avatar if there's one
  103. $avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]);
  104. $avatar?->delete();
  105. DB::persist($attachment);
  106. // Can only get new id after inserting
  107. DB::flush();
  108. DB::persist(AvatarEntity::create(['gsactor_id' => $gsactor_id, 'attachment_id' => $attachment->getId(), 'filename' => $file->getClientOriginalName()]));
  109. DB::flush();
  110. Event::handle('AvatarUpdate', [$user->getId()]);
  111. }
  112. }
  113. return ['_template' => 'settings/avatar.html.twig', 'avatar' => $form->createView()];
  114. }
  115. }