You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.1 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. /**
  25. * Public interface of ownCloud for apps to use.
  26. * AppFramework\Controller class
  27. */
  28. namespace OCP\AppFramework;
  29. use OCP\AppFramework\Http\Response;
  30. use OCP\IRequest;
  31. /**
  32. * Base class to inherit your controllers from that are used for RESTful APIs
  33. * @since 7.0.0
  34. */
  35. abstract class ApiController extends Controller {
  36. private $corsMethods;
  37. private $corsAllowedHeaders;
  38. private $corsMaxAge;
  39. /**
  40. * constructor of the controller
  41. * @param string $appName the name of the app
  42. * @param IRequest $request an instance of the request
  43. * @param string $corsMethods comma separated string of HTTP verbs which
  44. * should be allowed for websites or webapps when calling your API, defaults to
  45. * 'PUT, POST, GET, DELETE, PATCH'
  46. * @param string $corsAllowedHeaders comma separated string of HTTP headers
  47. * which should be allowed for websites or webapps when calling your API,
  48. * defaults to 'Authorization, Content-Type, Accept'
  49. * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS
  50. * request should be cached, defaults to 1728000 seconds
  51. * @since 7.0.0
  52. */
  53. public function __construct($appName,
  54. IRequest $request,
  55. $corsMethods='PUT, POST, GET, DELETE, PATCH',
  56. $corsAllowedHeaders='Authorization, Content-Type, Accept',
  57. $corsMaxAge=1728000) {
  58. parent::__construct($appName, $request);
  59. $this->corsMethods = $corsMethods;
  60. $this->corsAllowedHeaders = $corsAllowedHeaders;
  61. $this->corsMaxAge = $corsMaxAge;
  62. }
  63. /**
  64. * This method implements a preflighted cors response for you that you can
  65. * link to for the options request
  66. *
  67. * @NoAdminRequired
  68. * @NoCSRFRequired
  69. * @PublicPage
  70. * @since 7.0.0
  71. */
  72. public function preflightedCors() {
  73. if (isset($this->request->server['HTTP_ORIGIN'])) {
  74. $origin = $this->request->server['HTTP_ORIGIN'];
  75. } else {
  76. $origin = '*';
  77. }
  78. $response = new Response();
  79. $response->addHeader('Access-Control-Allow-Origin', $origin);
  80. $response->addHeader('Access-Control-Allow-Methods', $this->corsMethods);
  81. $response->addHeader('Access-Control-Max-Age', (string)$this->corsMaxAge);
  82. $response->addHeader('Access-Control-Allow-Headers', $this->corsAllowedHeaders);
  83. $response->addHeader('Access-Control-Allow-Credentials', 'false');
  84. return $response;
  85. }
  86. }