OrbitedPlugin.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Laconica, the distributed open-source microblogging tool
  4. *
  5. * Plugin to do "real time" updates using Orbited + STOMP
  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 Laconica
  24. * @author Evan Prodromou <evan@controlyourself.ca>
  25. * @copyright 2009 Control Yourself, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://laconi.ca/
  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 Orbited + STOMP
  35. *
  36. * This plugin pushes data to a STOMP server which is then served to the
  37. * browser by the Orbited server.
  38. *
  39. * @category Plugin
  40. * @package Laconica
  41. * @author Evan Prodromou <evan@controlyourself.ca>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://laconi.ca/
  44. */
  45. class OrbitedPlugin extends RealtimePlugin
  46. {
  47. public $webserver = null;
  48. public $webport = null;
  49. public $channelbase = null;
  50. public $stompserver = null;
  51. public $stompport = null;
  52. public $username = null;
  53. public $password = null;
  54. public $webuser = null;
  55. public $webpass = null;
  56. protected $con = null;
  57. function onStartShowHeadElements($action)
  58. {
  59. // See http://orbited.org/wiki/Deployment#Cross-SubdomainDeployment
  60. $action->element('script', null, ' document.domain = document.domain; ');
  61. }
  62. function _getScripts()
  63. {
  64. $scripts = parent::_getScripts();
  65. $port = (is_null($this->webport)) ? 8000 : $this->webport;
  66. $server = (is_null($this->webserver)) ? common_config('site', 'server') : $this->webserver;
  67. $root = 'http://'.$server.(($port == 80) ? '':':'.$port);
  68. $scripts[] = $root.'/static/Orbited.js';
  69. $scripts[] = $this->path('js/orbitedextra.js');
  70. $scripts[] = $root.'/static/protocols/stomp/stomp.js';
  71. $scripts[] = $this->path('js/orbitedupdater.js');
  72. return $scripts;
  73. }
  74. function _updateInitialize($timeline, $user_id)
  75. {
  76. $script = parent::_updateInitialize($timeline, $user_id);
  77. $server = $this->_getStompServer();
  78. $port = $this->_getStompPort();
  79. return $script." OrbitedUpdater.init(\"$server\", $port, ".
  80. "\"{$timeline}\", \"{$this->webuser}\", \"{$this->webpass}\");";
  81. }
  82. function _connect()
  83. {
  84. require_once(INSTALLDIR.'/extlib/Stomp.php');
  85. $url = $this->_getStompUrl();
  86. $this->con = new Stomp($url);
  87. if ($this->con->connect($this->username, $this->password)) {
  88. $this->log(LOG_INFO, "Connected.");
  89. } else {
  90. $this->log(LOG_ERR, 'Failed to connect to queue server');
  91. // TRANS: Server exception thrown when no connection can be made to a queue server.
  92. throw new ServerException(_m('Failed to connect to queue server.'));
  93. }
  94. }
  95. function _publish($channel, $message)
  96. {
  97. $result = $this->con->send($channel,
  98. json_encode($message));
  99. return $result;
  100. // @todo Parse and deal with result.
  101. }
  102. function _disconnect()
  103. {
  104. $this->con->disconnect();
  105. }
  106. function _pathToChannel($path)
  107. {
  108. if (!empty($this->channelbase)) {
  109. array_unshift($path, $this->channelbase);
  110. }
  111. return '/' . implode('/', $path);
  112. }
  113. function _getStompServer()
  114. {
  115. return (!is_null($this->stompserver)) ? $this->stompserver :
  116. (!is_null($this->webserver)) ? $this->webserver :
  117. common_config('site', 'server');
  118. }
  119. function _getStompPort()
  120. {
  121. return (!is_null($this->stompport)) ? $this->stompport : 61613;
  122. }
  123. function _getStompUrl()
  124. {
  125. $server = $this->_getStompServer();
  126. $port = $this->_getStompPort();
  127. return "tcp://$server:$port/";
  128. }
  129. /**
  130. * Add our version information to output
  131. *
  132. * @param array &$versions Array of version-data arrays
  133. *
  134. * @return boolean hook value
  135. */
  136. function onPluginVersion(array &$versions)
  137. {
  138. $versions[] = array('name' => 'Orbited',
  139. 'version' => GNUSOCIAL_VERSION,
  140. 'author' => 'Evan Prodromou',
  141. 'homepage' => 'http://status.net/wiki/Plugin:Orbited',
  142. 'rawdescription' =>
  143. // TRANS: Plugin description.
  144. _m('Plugin to make updates using Orbited and STOMP.'));
  145. return true;
  146. }
  147. }