]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Orbited/OrbitedPlugin.php
Update OrbitedPlugin to work with RealtimePlugin framework
[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
49 class OrbitedPlugin extends RealtimePlugin
50 {
51     public $webserver   = null;
52     public $webport     = null;
53     public $channelbase = null;
54     public $stompserver = null;
55     public $stompport   = null;
56     public $username    = null;
57     public $password    = null;
58     public $webuser     = null;
59     public $webpass     = null;
60
61     protected $con      = null;
62
63     function _getScripts()
64     {
65         $scripts = parent::_getScripts();
66
67         $port = (is_null($this->webport)) ? 8000 : $this->webport;
68
69         $server = (is_null($this->webserver)) ? common_config('site', 'server') : $this->webserver;
70
71         $root = 'http://'.$server.(($port == 80) ? '':':'.$port);
72
73         $scripts[] = $root.'/static/Orbited.js';
74         $scripts[] = $root.'/static/protocols/stomp/stomp.js';
75         $scripts[] = common_path('plugins/Orbited/orbitedupdater.js');
76
77         return $scripts;
78     }
79
80     function _updateInitialize($timeline, $user_id)
81     {
82         $script = parent::_updateInitialize($timeline, $user_id);
83
84         $server = $this->_getStompServer();
85         $port   = $this->_getStompPort();
86
87         return $script." OrbitedUpdater.init(\"$server\", $port, ".
88           "\"{$timeline}\", \"{$this->webuser}\", \"{$this->webpass}\");";
89     }
90
91     function _connect()
92     {
93         require_once(INSTALLDIR.'/extlibs/Stomp.php');
94
95         $url = $this->_getStompUrl();
96
97         $this->con = new Stomp($url);
98
99         if ($this->con->connect($this->username, $this->password)) {
100             $this->_log(LOG_INFO, "Connected.");
101         } else {
102             $this->_log(LOG_ERR, 'Failed to connect to queue server');
103             throw new ServerException('Failed to connect to queue server');
104         }
105     }
106
107     function _publish($channel, $message)
108     {
109         $result = $this->con->send($channel,
110                                    json_encode($message));
111
112         return $result;
113         // TODO: parse and deal with result
114     }
115
116     function _disconnect()
117     {
118         $this->con->disconnect();
119     }
120
121     function _pathToChannel($path)
122     {
123         if (!empty($this->channelbase)) {
124             array_unshift($path, $this->channelbase);
125         }
126         return '/' . implode('/', $path);
127     }
128
129     function _getStompServer()
130     {
131         $server = (!is_null($this->stompserver)) ? $this->stompserver :
132         (!is_null($this->webserver)) ? $this->webserver :
133         common_config('site', 'server');
134         return $server;
135     }
136
137     function _getStompPort()
138     {
139         $port = (!is_null($this->stompport)) ? $this->stompport : 61613;
140     }
141
142     function _getStompUrl()
143     {
144         $server = $this->_getStompServer();
145         $port   = $this->_getStompPort();
146         return "tcp://$server:$port/";
147     }
148 }