]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Meteor/MeteorPlugin.php
make meteor protocol (http or https) configurable
[quix0rs-gnu-social.git] / plugins / Meteor / MeteorPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to do "real time" updates using Meteor
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   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/plugins/Realtime/RealtimePlugin.php';
35
36 /**
37  * Plugin to do realtime updates using Meteor
38  *
39  * @category Plugin
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45 class MeteorPlugin extends RealtimePlugin
46 {
47     public $webserver     = null;
48     public $webport       = null;
49     public $controlport   = null;
50     public $controlserver = null;
51     public $channelbase   = null;
52     public $protocol      = null;
53     public $persistent    = true;
54     protected $_socket    = null;
55
56     function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='', $protocol='http')
57     {
58         global $config;
59
60         $this->webserver     = (empty($webserver)) ? $config['site']['server'] : $webserver;
61         $this->webport       = $webport;
62         $this->controlport   = $controlport;
63         $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
64         $this->channelbase   = $channelbase;
65                 $this->protocol      = $protocol;
66                 
67         parent::__construct();
68     }
69
70     /**
71      * Pull settings from config file/database if set.
72      */
73     function initialize()
74     {
75         $settings = array('webserver',
76                           'webport',
77                           'controlport',
78                           'controlserver',
79                           'channelbase',
80                           'protocol');
81         foreach ($settings as $name) {
82             $val = common_config('meteor', $name);
83             if ($val !== false) {
84                 $this->$name = $val;
85             }
86         }
87
88         return parent::initialize();
89     }
90
91     function _getScripts()
92     {
93         $scripts = parent::_getScripts();
94         if ($this->protocol == 'https') {
95                 $scripts[] = 'https://'.$this->webserver.(($this->webport == 443) ? '':':'.$this->webport).'/meteor.js';
96         } else {
97                 $scripts[] = 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js';
98         }
99         $scripts[] = $this->path('meteorupdater.min.js');
100         return $scripts;
101     }
102
103     function _updateInitialize($timeline, $user_id)
104     {
105         $script = parent::_updateInitialize($timeline, $user_id);
106         return $script." MeteorUpdater.init(\"$this->webserver\", $this->webport, \"{$timeline}\");";
107     }
108
109     function _connect()
110     {
111         $controlserver = (empty($this->controlserver)) ? $this->webserver : $this->controlserver;
112
113         $errno = $errstr = null;
114         $timeout = 5;
115         $flags = STREAM_CLIENT_CONNECT;
116         if ($this->persistent) $flags |= STREAM_CLIENT_PERSISTENT;
117
118         // May throw an exception.
119         $this->_socket = stream_socket_client("tcp://{$controlserver}:{$this->controlport}", $errno, $errstr, $timeout, $flags);
120         if (!$this->_socket) {
121             // TRANS: Exception. %1$s is the control server, %2$s is the control port.
122             throw new Exception(sprintf(_m('Could not connect to %1$s on %2$s.'),$controlserver,$this->controlport));
123         }
124     }
125
126     function _publish($channel, $message)
127     {
128         $message = json_encode($message);
129         $message = addslashes($message);
130         $cmd = "ADDMESSAGE $channel $message\n";
131         $cnt = fwrite($this->_socket, $cmd);
132         $result = fgets($this->_socket);
133         if (preg_match('/^ERR (.*)$/', $result, $matches)) {
134             // TRANS: Exception. %s is the Meteor message that could not be added.
135             throw new Exception(sprintf(_m('Error adding meteor message "%s".'),$matches[1]));
136         }
137         // TODO: parse and deal with result
138     }
139
140     function _disconnect()
141     {
142         if (!$this->persistent) {
143             $cnt = fwrite($this->_socket, "QUIT\n");
144             @fclose($this->_socket);
145         }
146     }
147
148     // Meteord flips out with default '/' separator
149
150     function _pathToChannel($path)
151     {
152         if (!empty($this->channelbase)) {
153             array_unshift($path, $this->channelbase);
154         }
155         return implode('-', $path);
156     }
157
158     function onPluginVersion(&$versions)
159     {
160         $versions[] = array('name' => 'Meteor',
161                             'version' => STATUSNET_VERSION,
162                             'author' => 'Evan Prodromou',
163                             'homepage' => 'http://status.net/wiki/Plugin:Meteor',
164                             'rawdescription' =>
165                             // TRANS: Plugin description.
166                             _m('Plugin to do "real time" updates using Meteor.'));
167         return true;
168     }
169 }