]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Meteor/MeteorPlugin.php
[TRANSLATION] Update license and copyright notice in translation files
[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('GNUSOCIAL') && !defined('STATUSNET')) {
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     const PLUGIN_VERSION = '2.0.0';
48
49     public $webserver     = null;
50     public $webport       = null;
51     public $controlport   = null;
52     public $controlserver = null;
53     public $channelbase   = null;
54     public $protocol      = null;
55     public $persistent    = true;
56     protected $_socket    = null;
57
58     function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='', $protocol='http')
59     {
60         global $config;
61
62         $this->webserver     = (empty($webserver)) ? $config['site']['server'] : $webserver;
63         $this->webport       = $webport;
64         $this->controlport   = $controlport;
65         $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
66         $this->channelbase   = $channelbase;
67                 $this->protocol      = $protocol;
68                 
69         parent::__construct();
70     }
71
72     /**
73      * Pull settings from config file/database if set.
74      */
75     function initialize()
76     {
77         $settings = array('webserver',
78                           'webport',
79                           'controlport',
80                           'controlserver',
81                           'channelbase',
82                           'protocol');
83         foreach ($settings as $name) {
84             $val = common_config('meteor', $name);
85             if ($val !== false) {
86                 $this->$name = $val;
87             }
88         }
89
90         return parent::initialize();
91     }
92
93     function _getScripts()
94     {
95         $scripts = parent::_getScripts();
96         if ($this->protocol == 'https') {
97                 $scripts[] = 'https://'.$this->webserver.(($this->webport == 443) ? '':':'.$this->webport).'/meteor.js';
98         } else {
99                 $scripts[] = 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js';
100         }
101         $scripts[] = $this->path('js/meteorupdater.js');
102         return $scripts;
103     }
104
105     function _updateInitialize($timeline, $user_id)
106     {
107         $script = parent::_updateInitialize($timeline, $user_id);
108         $ours = sprintf("MeteorUpdater.init(%s, %s, %s, %s);",
109                         json_encode($this->webserver),
110                         json_encode($this->webport),
111                         json_encode($this->protocol),
112                         json_encode($timeline));
113         return $script." ".$ours;
114     }
115
116     function _connect()
117     {
118         $controlserver = (empty($this->controlserver)) ? $this->webserver : $this->controlserver;
119
120         $errno = $errstr = null;
121         $timeout = 5;
122         $flags = STREAM_CLIENT_CONNECT;
123         if ($this->persistent) $flags |= STREAM_CLIENT_PERSISTENT;
124
125         // May throw an exception.
126         $this->_socket = stream_socket_client("tcp://{$controlserver}:{$this->controlport}", $errno, $errstr, $timeout, $flags);
127         if (!$this->_socket) {
128             // TRANS: Exception. %1$s is the control server, %2$s is the control port.
129             throw new Exception(sprintf(_m('Could not connect to %1$s on %2$s.'),$controlserver,$this->controlport));
130         }
131     }
132
133     function _publish($channel, $message)
134     {
135         $message = json_encode($message);
136         $message = addslashes($message);
137         $cmd = "ADDMESSAGE $channel $message\n";
138         $cnt = fwrite($this->_socket, $cmd);
139         $result = fgets($this->_socket);
140         if (preg_match('/^ERR (.*)$/', $result, $matches)) {
141             // TRANS: Exception. %s is the Meteor message that could not be added.
142             throw new Exception(sprintf(_m('Error adding meteor message "%s".'),$matches[1]));
143         }
144         // TODO: parse and deal with result
145     }
146
147     function _disconnect()
148     {
149         if (!$this->persistent) {
150             $cnt = fwrite($this->_socket, "QUIT\n");
151             @fclose($this->_socket);
152         }
153     }
154
155     // Meteord flips out with default '/' separator
156
157     function _pathToChannel($path)
158     {
159         if (!empty($this->channelbase)) {
160             array_unshift($path, $this->channelbase);
161         }
162         return implode('-', $path);
163     }
164
165     function onPluginVersion(array &$versions)
166     {
167         $versions[] = array('name' => 'Meteor',
168                             'version' => self::PLUGIN_VERSION,
169                             'author' => 'Evan Prodromou',
170                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Meteor',
171                             'rawdescription' =>
172                             // TRANS: Plugin description.
173                             _m('Plugin to do "real time" updates using Meteor.'));
174         return true;
175     }
176 }