apicontroller.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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. */
  22. /**
  23. * Public interface of ownCloud for apps to use.
  24. * AppFramework\Controller class
  25. */
  26. namespace OCP\AppFramework;
  27. use OCP\AppFramework\Http\Response;
  28. use OCP\IRequest;
  29. /**
  30. * Base class to inherit your controllers from that are used for RESTful APIs
  31. * @since 7.0.0
  32. */
  33. abstract class ApiController extends Controller {
  34. private $corsMethods;
  35. private $corsAllowedHeaders;
  36. private $corsMaxAge;
  37. /**
  38. * constructor of the controller
  39. * @param string $appName the name of the app
  40. * @param IRequest $request an instance of the request
  41. * @param string $corsMethods comma seperated string of HTTP verbs which
  42. * should be allowed for websites or webapps when calling your API, defaults to
  43. * 'PUT, POST, GET, DELETE, PATCH'
  44. * @param string $corsAllowedHeaders comma seperated string of HTTP headers
  45. * which should be allowed for websites or webapps when calling your API,
  46. * defaults to 'Authorization, Content-Type, Accept'
  47. * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS
  48. * request should be cached, defaults to 1728000 seconds
  49. * @since 7.0.0
  50. */
  51. public function __construct($appName,
  52. IRequest $request,
  53. $corsMethods='PUT, POST, GET, DELETE, PATCH',
  54. $corsAllowedHeaders='Authorization, Content-Type, Accept',
  55. $corsMaxAge=1728000){
  56. parent::__construct($appName, $request);
  57. $this->corsMethods = $corsMethods;
  58. $this->corsAllowedHeaders = $corsAllowedHeaders;
  59. $this->corsMaxAge = $corsMaxAge;
  60. }
  61. /**
  62. * This method implements a preflighted cors response for you that you can
  63. * link to for the options request
  64. *
  65. * @NoAdminRequired
  66. * @NoCSRFRequired
  67. * @PublicPage
  68. * @since 7.0.0
  69. */
  70. public function preflightedCors() {
  71. if(isset($this->request->server['HTTP_ORIGIN'])) {
  72. $origin = $this->request->server['HTTP_ORIGIN'];
  73. } else {
  74. $origin = '*';
  75. }
  76. $response = new Response();
  77. $response->addHeader('Access-Control-Allow-Origin', $origin);
  78. $response->addHeader('Access-Control-Allow-Methods', $this->corsMethods);
  79. $response->addHeader('Access-Control-Max-Age', $this->corsMaxAge);
  80. $response->addHeader('Access-Control-Allow-Headers', $this->corsAllowedHeaders);
  81. $response->addHeader('Access-Control-Allow-Credentials', 'false');
  82. return $response;
  83. }
  84. }