1
0

OC_EventSource.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. class OC_EventSource implements \OCP\IEventSource {
  30. /**
  31. * @var bool
  32. */
  33. private $fallback;
  34. /**
  35. * @var int
  36. */
  37. private $fallBackId = 0;
  38. /**
  39. * @var bool
  40. */
  41. private $started = false;
  42. protected function init() {
  43. if ($this->started) {
  44. return;
  45. }
  46. $this->started = true;
  47. // prevent php output buffering, caching and nginx buffering
  48. OC_Util::obEnd();
  49. header('Cache-Control: no-cache');
  50. header('X-Accel-Buffering: no');
  51. $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true';
  52. if ($this->fallback) {
  53. $this->fallBackId = (int)$_GET['fallback_id'];
  54. /**
  55. * FIXME: The default content-security-policy of ownCloud forbids inline
  56. * JavaScript for security reasons. IE starting on Windows 10 will
  57. * however also obey the CSP which will break the event source fallback.
  58. *
  59. * As a workaround thus we set a custom policy which allows the execution
  60. * of inline JavaScript.
  61. *
  62. * @link https://github.com/owncloud/core/issues/14286
  63. */
  64. header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
  65. header("Content-Type: text/html");
  66. echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
  67. } else {
  68. header("Content-Type: text/event-stream");
  69. }
  70. if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
  71. header('Location: '.\OC::$WEBROOT);
  72. exit();
  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. * @suppress PhanDeprecatedFunction
  90. */
  91. public function send($type, $data = null) {
  92. if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
  93. throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')');
  94. }
  95. $this->init();
  96. if (is_null($data)) {
  97. $data = $type;
  98. $type = null;
  99. }
  100. if ($this->fallback) {
  101. $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
  102. . $this->fallBackId . ',"' . $type . '",' . OC_JSON::encode($data) . ')</script>' . PHP_EOL;
  103. echo $response;
  104. } else {
  105. if ($type) {
  106. echo 'event: ' . $type . PHP_EOL;
  107. }
  108. echo 'data: ' . OC_JSON::encode($data) . PHP_EOL;
  109. }
  110. echo PHP_EOL;
  111. flush();
  112. }
  113. /**
  114. * close the connection of the event source
  115. */
  116. public function close() {
  117. $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it
  118. }
  119. }