ClientController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * @copyright Copyright (c) 2019 Sergio Brighenti <sergio@brighenti.me>
  4. *
  5. * @author Sergio Brighenti <sergio@brighenti.me>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. */
  21. namespace App\Controllers;
  22. use Psr\Http\Message\ResponseInterface as Response;
  23. use Psr\Http\Message\ServerRequestInterface as Request;
  24. use Slim\Exception\HttpNotFoundException;
  25. use Slim\Exception\HttpUnauthorizedException;
  26. class ClientController extends Controller
  27. {
  28. /**
  29. * @param Request $request
  30. * @param Response $response
  31. * @param int $id
  32. *
  33. * @throws HttpNotFoundException
  34. * @throws HttpUnauthorizedException
  35. *
  36. * @return Response
  37. */
  38. public function getShareXConfig(Request $request, Response $response, int $id): Response
  39. {
  40. $user = $this->getUser($request, $id, true);
  41. if ($user->token === null || $user->token === '') {
  42. $this->session->alert(lang('no_upload_token'), 'danger');
  43. return redirect($response, $request->getHeaderLine('Referer'));
  44. }
  45. $json = array(
  46. 'DestinationType' => 'ImageUploader, TextUploader, FileUploader',
  47. 'RequestURL' => route('upload'),
  48. 'FileFormName' => 'upload',
  49. 'Arguments' => array(
  50. 'file' => '$filename$',
  51. 'text' => '$input$',
  52. 'token' => $user->token,
  53. ),
  54. 'URL' => '$json:url$',
  55. 'ThumbnailURL' => '$json:url$/raw',
  56. 'DeletionURL' => '$json:url$/delete/'.$user->token,
  57. );
  58. return json($response, $json, 200, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
  59. ->withHeader('Content-Disposition', 'attachment;filename="'.$user->username.'-ShareX.sxcu"');
  60. }
  61. /**
  62. * @param Request $request
  63. * @param Response $response
  64. * @param int $id
  65. *
  66. * @throws HttpNotFoundException
  67. * @throws HttpUnauthorizedException
  68. * @throws \Twig\Error\LoaderError
  69. * @throws \Twig\Error\RuntimeError
  70. * @throws \Twig\Error\SyntaxError
  71. *
  72. * @return Response
  73. */
  74. public function getBashScript(Request $request, Response $response, int $id): Response
  75. {
  76. $user = $this->getUser($request, $id, true);
  77. if ($user->token === null || $user->token === '') {
  78. $this->session->alert(lang('no_upload_token'), 'danger');
  79. return redirect($response, $request->getHeaderLine('Referer'));
  80. }
  81. return view()->render($response->withHeader('Content-Disposition', 'attachment;filename="xbackbone_uploader_'.$user->username.'.sh"'),
  82. 'scripts/xbackbone_uploader.sh.twig',
  83. array(
  84. 'username' => $user->username,
  85. 'upload_url' => route('upload'),
  86. 'token' => $user->token,
  87. )
  88. );
  89. }
  90. }