123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- if (!defined('GNUSOCIAL') && !defined('STATUSNET')) {
- exit(1);
- }
- require_once INSTALLDIR.'/plugins/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;
- function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='', $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();
- }
-
- function initialize()
- {
- $settings = array('webserver',
- 'webport',
- 'controlport',
- 'controlserver',
- 'channelbase',
- 'protocol');
- foreach ($settings as $name) {
- $val = common_config('meteor', $name);
- if ($val !== false) {
- $this->$name = $val;
- }
- }
- return parent::initialize();
- }
- 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;
- }
- function _updateInitialize($timeline, $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;
- }
- 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));
- }
- }
- 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]));
- }
-
- }
- function _disconnect()
- {
- if (!$this->persistent) {
- $cnt = fwrite($this->_socket, "QUIT\n");
- @fclose($this->_socket);
- }
- }
-
- function _pathToChannel($path)
- {
- if (!empty($this->channelbase)) {
- array_unshift($path, $this->channelbase);
- }
- return implode('-', $path);
- }
- function onPluginVersion(array &$versions)
- {
- $versions[] = array('name' => 'Meteor',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Evan Prodromou',
- 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Meteor',
- 'rawdescription' =>
-
- _m('Plugin to do "real time" updates using Meteor.'));
- return true;
- }
- }
|