OrbitedPlugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. const PLUGIN_VERSION = '2.0.0';
  48. public $webserver = null;
  49. public $webport = null;
  50. public $channelbase = null;
  51. public $stompserver = null;
  52. public $stompport = null;
  53. public $username = null;
  54. public $password = null;
  55. public $webuser = null;
  56. public $webpass = null;
  57. protected $con = null;
  58. function onStartShowHeadElements($action)
  59. {
  60. // See http://orbited.org/wiki/Deployment#Cross-SubdomainDeployment
  61. $action->element('script', null, ' document.domain = document.domain; ');
  62. }
  63. function _getScripts()
  64. {
  65. $scripts = parent::_getScripts();
  66. $port = (is_null($this->webport)) ? 8000 : $this->webport;
  67. $server = (is_null($this->webserver)) ? common_config('site', 'server') : $this->webserver;
  68. $root = 'http://'.$server.(($port == 80) ? '':':'.$port);
  69. $scripts[] = $root.'/static/Orbited.js';
  70. $scripts[] = $this->path('js/orbitedextra.js');
  71. $scripts[] = $root.'/static/protocols/stomp/stomp.js';
  72. $scripts[] = $this->path('js/orbitedupdater.js');
  73. return $scripts;
  74. }
  75. function _updateInitialize($timeline, $user_id)
  76. {
  77. $script = parent::_updateInitialize($timeline, $user_id);
  78. $server = $this->_getStompServer();
  79. $port = $this->_getStompPort();
  80. return $script." OrbitedUpdater.init(\"$server\", $port, ".
  81. "\"{$timeline}\", \"{$this->webuser}\", \"{$this->webpass}\");";
  82. }
  83. function _connect()
  84. {
  85. require_once(INSTALLDIR.'/extlib/Stomp.php');
  86. $url = $this->_getStompUrl();
  87. $this->con = new Stomp($url);
  88. if ($this->con->connect($this->username, $this->password)) {
  89. $this->log(LOG_INFO, "Connected.");
  90. } else {
  91. $this->log(LOG_ERR, 'Failed to connect to queue server');
  92. // TRANS: Server exception thrown when no connection can be made to a queue server.
  93. throw new ServerException(_m('Failed to connect to queue server.'));
  94. }
  95. }
  96. function _publish($channel, $message)
  97. {
  98. $result = $this->con->send($channel,
  99. json_encode($message));
  100. return $result;
  101. // @todo Parse and deal with result.
  102. }
  103. function _disconnect()
  104. {
  105. $this->con->disconnect();
  106. }
  107. function _pathToChannel($path)
  108. {
  109. if (!empty($this->channelbase)) {
  110. array_unshift($path, $this->channelbase);
  111. }
  112. return '/' . implode('/', $path);
  113. }
  114. function _getStompServer()
  115. {
  116. return (!is_null($this->stompserver)) ? $this->stompserver :
  117. (!is_null($this->webserver)) ? $this->webserver :
  118. common_config('site', 'server');
  119. }
  120. function _getStompPort()
  121. {
  122. return (!is_null($this->stompport)) ? $this->stompport : 61613;
  123. }
  124. function _getStompUrl()
  125. {
  126. $server = $this->_getStompServer();
  127. $port = $this->_getStompPort();
  128. return "tcp://$server:$port/";
  129. }
  130. /**
  131. * Add our version information to output
  132. *
  133. * @param array &$versions Array of version-data arrays
  134. *
  135. * @return boolean hook value
  136. */
  137. function onPluginVersion(array &$versions)
  138. {
  139. $versions[] = array('name' => 'Orbited',
  140. 'version' => self::PLUGIN_VERSION,
  141. 'author' => 'Evan Prodromou',
  142. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Orbited',
  143. 'rawdescription' =>
  144. // TRANS: Plugin description.
  145. _m('Plugin to make updates using Orbited and STOMP.'));
  146. return true;
  147. }
  148. }