eventsource.php 3.2 KB

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