123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- defined('GNUSOCIAL') || die();
- require_once INSTALLDIR . DIRECTORY_SEPARATOR . 'lib/modules/Realtime/RealtimePlugin.php';
- class MeteorPlugin extends RealtimePlugin
- {
- const PLUGIN_VERSION = '2.0.0';
- public $webserver = null;
- public $webport = null;
- public $controlport = null;
- public $controlserver = null;
- public $channelbase = null;
- public $protocol = null;
- public $persistent = true;
- protected $_socket = null;
- public function __construct(
- ?string $webserver = null,
- int $webport = 4670,
- int $controlport = 4671,
- ?string $controlserver = null,
- string $channelbase = '',
- string $protocol = 'http'
- ) {
- global $config;
- $this->webserver = (empty($webserver)) ? $config['site']['server'] : $webserver;
- $this->webport = $webport;
- $this->controlport = $controlport;
- $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
- $this->channelbase = $channelbase;
- $this->protocol = $protocol;
- parent::__construct();
- }
-
- public function initialize()
- {
- $settings = [
- 'webserver',
- 'webport',
- 'controlport',
- 'controlserver',
- 'channelbase',
- 'protocol',
- ];
- foreach ($settings as $name) {
- $val = common_config('meteor', $name);
- if ($val !== false) {
- $this->$name = $val;
- }
- }
- return parent::initialize();
- }
- public function _getScripts()
- {
- $scripts = parent::_getScripts();
- if ($this->protocol == 'https') {
- $scripts[] = 'https://' . $this->webserver . (($this->webport == 443) ? '' : ':' . $this->webport) . '/meteor.js';
- } else {
- $scripts[] = 'http://' . $this->webserver . (($this->webport == 80) ? '' : ':' . $this->webport) . '/meteor.js';
- }
- $scripts[] = $this->path('js/meteorupdater.js');
- return $scripts;
- }
- public function _updateInitialize($timeline, int $user_id)
- {
- $script = parent::_updateInitialize($timeline, $user_id);
- $ours = sprintf(
- "MeteorUpdater.init(%s, %s, %s, %s);",
- json_encode($this->webserver),
- json_encode($this->webport),
- json_encode($this->protocol),
- json_encode($timeline)
- );
- return $script." ".$ours;
- }
- public function _connect()
- {
- $controlserver = (empty($this->controlserver)) ? $this->webserver : $this->controlserver;
- $errno = $errstr = null;
- $timeout = 5;
- $flags = STREAM_CLIENT_CONNECT;
- if ($this->persistent) {
- $flags |= STREAM_CLIENT_PERSISTENT;
- }
-
- $this->_socket = stream_socket_client(
- "tcp://{$controlserver}:{$this->controlport}",
- $errno,
- $errstr,
- $timeout,
- $flags
- );
- if (!$this->_socket) {
-
- throw new Exception(sprintf(_m('Could not connect to %1$s on %2$s.'), $controlserver, $this->controlport));
- }
- }
- public function _publish($channel, $message)
- {
- $message = json_encode($message);
- $message = addslashes($message);
- $cmd = "ADDMESSAGE $channel $message\n";
- $cnt = fwrite($this->_socket, $cmd);
- $result = fgets($this->_socket);
- if (preg_match('/^ERR (.*)$/', $result, $matches)) {
-
- throw new Exception(sprintf(_m('Error adding meteor message "%s".'), $matches[1]));
- }
-
- }
- public function _disconnect()
- {
- if (!$this->persistent) {
- $cnt = fwrite($this->_socket, "QUIT\n");
- @fclose($this->_socket);
- }
- }
-
- public function _pathToChannel(array $path): string
- {
- if (!empty($this->channelbase)) {
- array_unshift($path, $this->channelbase);
- }
- return implode('-', $path);
- }
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = [
- 'name' => 'Meteor',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Evan Prodromou',
- 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/Meteor',
- 'rawdescription' =>
-
- _m('Plugin to do "real time" updates using Meteor.')
- ];
- return true;
- }
- }
|