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.

eventsource.php 3.8KB

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