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.

132 lines
3.9 KiB

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