EventSource.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christian Oliff <christianoliff@yahoo.com>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Felix Moeller <mail@felixmoeller.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC;
  31. use OCP\IEventSource;
  32. use OCP\IRequest;
  33. class EventSource implements IEventSource {
  34. private bool $fallback = false;
  35. private int $fallBackId = 0;
  36. private bool $started = false;
  37. public function __construct(
  38. private IRequest $request,
  39. ) {
  40. }
  41. protected function init(): void {
  42. if ($this->started) {
  43. return;
  44. }
  45. $this->started = true;
  46. // prevent php output buffering, caching and nginx buffering
  47. \OC_Util::obEnd();
  48. header('Cache-Control: no-cache');
  49. header('X-Accel-Buffering: no');
  50. $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true';
  51. if ($this->fallback) {
  52. $this->fallBackId = (int)$_GET['fallback_id'];
  53. /**
  54. * FIXME: The default content-security-policy of ownCloud forbids inline
  55. * JavaScript for security reasons. IE starting on Windows 10 will
  56. * however also obey the CSP which will break the event source fallback.
  57. *
  58. * As a workaround thus we set a custom policy which allows the execution
  59. * of inline JavaScript.
  60. *
  61. * @link https://github.com/owncloud/core/issues/14286
  62. */
  63. header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
  64. header("Content-Type: text/html");
  65. echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
  66. } else {
  67. header("Content-Type: text/event-stream");
  68. }
  69. if (!$this->request->passesStrictCookieCheck()) {
  70. header('Location: '.\OC::$WEBROOT);
  71. exit();
  72. }
  73. if (!$this->request->passesCSRFCheck()) {
  74. $this->send('error', 'Possible CSRF attack. Connection will be closed.');
  75. $this->close();
  76. exit();
  77. }
  78. flush();
  79. }
  80. /**
  81. * send a message to the client
  82. *
  83. * @param string $type
  84. * @param mixed $data
  85. *
  86. * @throws \BadMethodCallException
  87. * if only one parameter is given, a typeless message will be send with that parameter as data
  88. * @suppress PhanDeprecatedFunction
  89. */
  90. public function send($type, $data = null) {
  91. if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
  92. throw new \BadMethodCallException('Type needs to be alphanumeric ('. $type .')');
  93. }
  94. $this->init();
  95. if (is_null($data)) {
  96. $data = $type;
  97. $type = null;
  98. }
  99. if ($this->fallback) {
  100. $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
  101. . $this->fallBackId . ',"' . ($type ?? '') . '",' . json_encode($data, JSON_HEX_TAG) . ')</script>' . PHP_EOL;
  102. echo $response;
  103. } else {
  104. if ($type) {
  105. echo 'event: ' . $type . PHP_EOL;
  106. }
  107. echo 'data: ' . json_encode($data, JSON_HEX_TAG) . PHP_EOL;
  108. }
  109. echo PHP_EOL;
  110. flush();
  111. }
  112. /**
  113. * close the connection of the event source
  114. */
  115. public function close() {
  116. $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it
  117. }
  118. }