eventsource.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind1991@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * wrapper for server side events (http://en.wikipedia.org/wiki/Server-sent_events)
  24. * includes a fallback for older browsers and IE
  25. *
  26. * use server side events with caution, to many open requests can hang the server
  27. */
  28. class OC_EventSource{
  29. private $fallback;
  30. private $fallBackId=0;
  31. public function __construct() {
  32. OC_Util::obEnd();
  33. header('Cache-Control: no-cache');
  34. header('X-Accel-Buffering: no');
  35. $this->fallback=isset($_GET['fallback']) and $_GET['fallback']=='true';
  36. if($this->fallback) {
  37. $this->fallBackId=$_GET['fallback_id'];
  38. header("Content-Type: text/html");
  39. echo str_repeat('<span></span>'.PHP_EOL, 10); //dummy data to keep IE happy
  40. }else{
  41. header("Content-Type: text/event-stream");
  42. }
  43. if( !OC_Util::isCallRegistered()) {
  44. $this->send('error', 'Possible CSRF attack. Connection will be closed.');
  45. $this->close();
  46. exit();
  47. }
  48. flush();
  49. }
  50. /**
  51. * send a message to the client
  52. * @param string $type
  53. * @param mixed $data
  54. *
  55. * if only one parameter is given, a typeless message will be send with that parameter as data
  56. */
  57. public function send($type, $data=null) {
  58. if(is_null($data)) {
  59. $data=$type;
  60. $type=null;
  61. }
  62. if($this->fallback) {
  63. $fallBackId = OC_Util::sanitizeHTML($this->fallBackId);
  64. $response='<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
  65. .$fallBackId.',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL;
  66. echo $response;
  67. }else{
  68. if($type) {
  69. echo 'event: ' . $type.PHP_EOL;
  70. }
  71. echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL;
  72. }
  73. echo PHP_EOL;
  74. flush();
  75. }
  76. /**
  77. * close the connection of the even source
  78. */
  79. public function close() {
  80. $this->send('__internal__', 'close');//server side closing can be an issue, let the client do it
  81. }
  82. }