ApiController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Stefan Weil <sw@weilnetz.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', $this->corsMaxAge);
  82. $response->addHeader('Access-Control-Allow-Headers', $this->corsAllowedHeaders);
  83. $response->addHeader('Access-Control-Allow-Credentials', 'false');
  84. return $response;
  85. }
  86. }