OrbitedPlugin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Plugin to do "real time" updates using Orbited + STOMP
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. require_once INSTALLDIR . '/plugins/Realtime/RealtimePlugin.php';
  27. /**
  28. * Plugin to do realtime updates using Orbited + STOMP
  29. *
  30. * This plugin pushes data to a STOMP server which is then served to the
  31. * browser by the Orbited server.
  32. *
  33. * @category Plugin
  34. * @package GNUsocial
  35. * @author Evan Prodromou <evan@status.net>
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class OrbitedPlugin extends RealtimePlugin
  39. {
  40. const PLUGIN_VERSION = '2.0.0';
  41. public $webserver = null;
  42. public $webport = null;
  43. public $channelbase = null;
  44. public $stompserver = null;
  45. public $stompport = null;
  46. public $username = null;
  47. public $password = null;
  48. public $webuser = null;
  49. public $webpass = null;
  50. protected $con = null;
  51. public function onStartShowHeadElements($action)
  52. {
  53. // See http://orbited.org/wiki/Deployment#Cross-SubdomainDeployment
  54. $action->element('script', null, ' document.domain = document.domain; ');
  55. }
  56. public function _getScripts()
  57. {
  58. $scripts = parent::_getScripts();
  59. $port = (is_null($this->webport)) ? 8000 : $this->webport;
  60. $server = (is_null($this->webserver)) ? common_config('site', 'server') : $this->webserver;
  61. $root = 'http://'.$server.(($port == 80) ? '':':'.$port);
  62. $scripts[] = $root.'/static/Orbited.js';
  63. $scripts[] = $this->path('js/orbitedextra.js');
  64. $scripts[] = $root.'/static/protocols/stomp/stomp.js';
  65. $scripts[] = $this->path('js/orbitedupdater.js');
  66. return $scripts;
  67. }
  68. public function _updateInitialize($timeline, $user_id)
  69. {
  70. $script = parent::_updateInitialize($timeline, $user_id);
  71. $server = $this->_getStompServer();
  72. $port = $this->_getStompPort();
  73. return $script." OrbitedUpdater.init(\"$server\", $port, ".
  74. "\"{$timeline}\", \"{$this->webuser}\", \"{$this->webpass}\");";
  75. }
  76. public function _connect()
  77. {
  78. $url = $this->_getStompUrl();
  79. $this->con = new Stomp($url);
  80. if ($this->con->connect($this->username, $this->password)) {
  81. $this->log(LOG_INFO, "Connected.");
  82. } else {
  83. $this->log(LOG_ERR, 'Failed to connect to queue server');
  84. // TRANS: Server exception thrown when no connection can be made to a queue server.
  85. throw new ServerException(_m('Failed to connect to queue server.'));
  86. }
  87. }
  88. public function _publish($channel, $message)
  89. {
  90. $result = $this->con->send($channel, json_encode($message));
  91. return $result;
  92. // @todo Parse and deal with result.
  93. }
  94. public function _disconnect()
  95. {
  96. $this->con->disconnect();
  97. }
  98. public function _pathToChannel($path)
  99. {
  100. if (!empty($this->channelbase)) {
  101. array_unshift($path, $this->channelbase);
  102. }
  103. return '/' . implode('/', $path);
  104. }
  105. public function _getStompServer()
  106. {
  107. return (!is_null($this->stompserver)) ? $this->stompserver :
  108. (!is_null($this->webserver)) ? $this->webserver :
  109. common_config('site', 'server');
  110. }
  111. public function _getStompPort()
  112. {
  113. return (!is_null($this->stompport)) ? $this->stompport : 61613;
  114. }
  115. public function _getStompUrl()
  116. {
  117. $server = $this->_getStompServer();
  118. $port = $this->_getStompPort();
  119. return "tcp://$server:$port/";
  120. }
  121. /**
  122. * Add our version information to output
  123. *
  124. * @param array &$versions Array of version-data arrays
  125. *
  126. * @return boolean hook value
  127. */
  128. public function onPluginVersion(array &$versions): bool
  129. {
  130. $versions[] = array('name' => 'Orbited',
  131. 'version' => self::PLUGIN_VERSION,
  132. 'author' => 'Evan Prodromou',
  133. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/Orbited',
  134. 'rawdescription' =>
  135. // TRANS: Plugin description.
  136. _m('Plugin to make updates using Orbited and STOMP.'));
  137. return true;
  138. }
  139. }