CometPlugin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to do "real time" updates using Comet/Bayeux
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL') && !defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR.'/plugins/Realtime/RealtimePlugin.php';
  33. /**
  34. * Plugin to do realtime updates using Comet
  35. *
  36. * @category Plugin
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class CometPlugin extends RealtimePlugin
  43. {
  44. public $server = null;
  45. public $username = null;
  46. public $password = null;
  47. public $prefix = null;
  48. protected $bay = null;
  49. function __construct($server=null, $username=null, $password=null, $prefix=null)
  50. {
  51. $this->server = $server;
  52. $this->username = $username;
  53. $this->password = $password;
  54. $this->prefix = $prefix;
  55. parent::__construct();
  56. }
  57. function _getScripts()
  58. {
  59. $scripts = parent::_getScripts();
  60. $ours = array('js/jquery.comet.js', 'js/cometupdate.js');
  61. foreach ($ours as $script) {
  62. $scripts[] = $this->path($script);
  63. }
  64. return $scripts;
  65. }
  66. function _updateInitialize($timeline, $user_id)
  67. {
  68. $script = parent::_updateInitialize($timeline, $user_id);
  69. return $script." CometUpdate.init(\"$this->server\", \"$timeline\", $user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\");";
  70. }
  71. function _connect()
  72. {
  73. require_once INSTALLDIR.'/plugins/Comet/extlib/Bayeux/Bayeux.class.php';
  74. // Bayeux? Comet? Huh? These terms confuse me
  75. $this->bay = new Bayeux($this->server, $this->user, $this->password);
  76. }
  77. function _publish($timeline, $json)
  78. {
  79. $this->bay->publish($timeline, $json);
  80. }
  81. function _disconnect()
  82. {
  83. unset($this->bay);
  84. }
  85. function _pathToChannel($path)
  86. {
  87. if (!empty($this->prefix)) {
  88. array_unshift($path, $this->prefix);
  89. }
  90. return '/' . implode('/', $path);
  91. }
  92. function onPluginVersion(array &$versions)
  93. {
  94. $versions[] = array('name' => 'Comet',
  95. 'version' => GNUSOCIAL_VERSION,
  96. 'author' => 'Evan Prodromou',
  97. 'homepage' => 'http://status.net/wiki/Plugin:Comet',
  98. 'rawdescription' =>
  99. // TRANS: Plugin description message. Bayeux is a protocol for transporting asynchronous messages
  100. // TRANS: and Comet is a web application model.
  101. _m('Plugin to make updates using Comet and Bayeux.'));
  102. return true;
  103. }
  104. }