]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Orbited/OrbitedPlugin.php
d3b73a96148b0184bde067633c909bc285a8507c
[quix0rs-gnu-social.git] / plugins / Orbited / OrbitedPlugin.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Plugin to do "real time" updates using Orbited + STOMP
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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/plugins/Realtime/RealtimePlugin.php';
35
36 /**
37  * Plugin to do realtime updates using Orbited + STOMP
38  *
39  * This plugin pushes data to a STOMP server which is then served to the
40  * browser by the Orbited server.
41  *
42  * @category Plugin
43  * @package  Laconica
44  * @author   Evan Prodromou <evan@controlyourself.ca>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://laconi.ca/
47  */
48 class OrbitedPlugin extends RealtimePlugin
49 {
50     public $webserver   = null;
51     public $webport     = null;
52     public $channelbase = null;
53     public $stompserver = null;
54     public $stompport   = null;
55     public $username    = null;
56     public $password    = null;
57     public $webuser     = null;
58     public $webpass     = null;
59
60     protected $con      = null;
61
62     function onStartShowHeadElements($action)
63     {
64         // See http://orbited.org/wiki/Deployment#Cross-SubdomainDeployment
65         $action->element('script', null, ' document.domain = document.domain; ');
66     }
67
68     function _getScripts()
69     {
70         $scripts = parent::_getScripts();
71
72         $port = (is_null($this->webport)) ? 8000 : $this->webport;
73
74         $server = (is_null($this->webserver)) ? common_config('site', 'server') : $this->webserver;
75
76         $root = 'http://'.$server.(($port == 80) ? '':':'.$port);
77
78         $scripts[] = $root.'/static/Orbited.js';
79         $scripts[] = 'plugins/Orbited/orbitedextra.js';
80         $scripts[] = $root.'/static/protocols/stomp/stomp.js';
81         $scripts[] = 'plugins/Orbited/orbitedupdater.js';
82
83         return $scripts;
84     }
85
86     function _updateInitialize($timeline, $user_id)
87     {
88         $script = parent::_updateInitialize($timeline, $user_id);
89
90         $server = $this->_getStompServer();
91         $port   = $this->_getStompPort();
92
93         return $script." OrbitedUpdater.init(\"$server\", $port, ".
94           "\"{$timeline}\", \"{$this->webuser}\", \"{$this->webpass}\");";
95     }
96
97     function _connect()
98     {
99         require_once(INSTALLDIR.'/extlib/Stomp.php');
100
101         $url = $this->_getStompUrl();
102
103         $this->con = new Stomp($url);
104
105         if ($this->con->connect($this->username, $this->password)) {
106             $this->log(LOG_INFO, "Connected.");
107         } else {
108             $this->log(LOG_ERR, 'Failed to connect to queue server');
109             // TRANS: Server exception thrown when no connection can be made to a queue server.
110             throw new ServerException(_m('Failed to connect to queue server.'));
111         }
112     }
113
114     function _publish($channel, $message)
115     {
116         $result = $this->con->send($channel,
117                                    json_encode($message));
118
119         return $result;
120         // @todo Parse and deal with result.
121     }
122
123     function _disconnect()
124     {
125         $this->con->disconnect();
126     }
127
128     function _pathToChannel($path)
129     {
130         if (!empty($this->channelbase)) {
131             array_unshift($path, $this->channelbase);
132         }
133         return '/' . implode('/', $path);
134     }
135
136     function _getStompServer()
137     {
138         return (!is_null($this->stompserver)) ? $this->stompserver :
139         (!is_null($this->webserver)) ? $this->webserver :
140         common_config('site', 'server');
141     }
142
143     function _getStompPort()
144     {
145         return (!is_null($this->stompport)) ? $this->stompport : 61613;
146     }
147
148     function _getStompUrl()
149     {
150         $server = $this->_getStompServer();
151         $port   = $this->_getStompPort();
152         return "tcp://$server:$port/";
153     }
154
155     /**
156      * Add our version information to output
157      *
158      * @param array &$versions Array of version-data arrays
159      *
160      * @return boolean hook value
161      */
162     function onPluginVersion(&$versions)
163     {
164         $versions[] = array('name' => 'Orbited',
165                             'version' => STATUSNET_VERSION,
166                             'author' => 'Evan Prodromou',
167                             'homepage' => 'http://status.net/wiki/Plugin:Orbited',
168                             'rawdescription' =>
169                             // TRANS: Plugin description.
170                             _m('Plugin to make updates using Orbited and STOMP.'));
171         return true;
172     }
173 }