PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/eventsource.php

https://github.com/sezuan/core
PHP | 85 lines | 43 code | 6 blank | 36 comment | 5 complexity | da687b89ad8b4d0deddb0b5da5f32cd4 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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. $this->fallback=isset($_GET['fallback']) and $_GET['fallback']=='true';
  35. if($this->fallback) {
  36. $this->fallBackId=$_GET['fallback_id'];
  37. header("Content-Type: text/html");
  38. echo str_repeat('<span></span>'.PHP_EOL, 10); //dummy data to keep IE happy
  39. }else{
  40. header("Content-Type: text/event-stream");
  41. }
  42. if( !OC_Util::isCallRegistered()) {
  43. $this->send('error', 'Possible CSRF attack. Connection will be closed.');
  44. exit();
  45. }
  46. flush();
  47. }
  48. /**
  49. * send a message to the client
  50. * @param string $type
  51. * @param object $data
  52. *
  53. * if only one parameter is given, a typeless message will be send with that parameter as data
  54. */
  55. public function send($type, $data=null) {
  56. if(is_null($data)) {
  57. $data=$type;
  58. $type=null;
  59. }
  60. if($this->fallback) {
  61. $response='<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
  62. .$this->fallBackId.',"'.$type.'",'.json_encode($data).')</script>'.PHP_EOL;
  63. echo $response;
  64. }else{
  65. if($type) {
  66. echo 'event: '.$type.PHP_EOL;
  67. }
  68. echo 'data: '.json_encode($data).PHP_EOL;
  69. }
  70. echo PHP_EOL;
  71. flush();
  72. }
  73. /**
  74. * close the connection of the even source
  75. */
  76. public function close() {
  77. $this->send('__internal__', 'close');//server side closing can be an issue, let the client do it
  78. }
  79. }