eventsource.php 3.7 KB

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