]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/RSSCloudPlugin.php
b1af9b59cbe8514fc79131395684ee45d200f8e1
[quix0rs-gnu-social.git] / plugins / RSSCloud / RSSCloudPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to support RSSCloud
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    Zach Copley <zach@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')) {
31     exit(1);
32 }
33
34 /**
35  * Plugin class for adding RSSCloud capabilities to StatusNet
36  *
37  * @category Plugin
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  **/
43
44 class RSSCloudPlugin extends Plugin
45 {
46     /**
47      * Our friend, the constructor
48      *
49      * @return void
50      */
51     function __construct()
52     {
53         parent::__construct();
54     }
55
56     /**
57      * Setup the info for the subscription handler. Allow overriding
58      * to point at another cloud hub (not currently used).
59      *
60      * @return void
61      */
62
63     function onInitializePlugin()
64     {
65         $this->domain   = common_config('rsscloud', 'domain');
66         $this->port     = common_config('rsscloud', 'port');
67         $this->path     = common_config('rsscloud', 'path');
68         $this->funct    = common_config('rsscloud', 'function');
69         $this->protocol = common_config('rsscloud', 'protocol');
70
71         // set defaults
72
73         $local_server = parse_url(common_path('/main/rsscloud/request_notify'));
74
75         if (empty($this->domain)) {
76             $this->domain = $local_server['host'];
77         }
78
79         if (empty($this->port)) {
80             $this->port = '80';
81         }
82
83         if (empty($this->path)) {
84             $this->path = '/main/rsscloud/request_notify';
85         }
86
87         if (empty($this->funct)) {
88             $this->funct = '';
89         }
90
91         if (empty($this->protocol)) {
92             $this->protocol = 'http-post';
93         }
94     }
95
96     /**
97      * Add RSSCloud-related paths to the router table
98      *
99      * Hook for RouterInitialized event.
100      *
101      * @return boolean hook return
102      */
103
104     function onRouterInitialized(&$m)
105     {
106         $m->connect('/main/rsscloud/request_notify', array('action' => 'RSSCloudRequestNotify'));
107
108         // XXX: This is just for end-to-end testing. Uncomment if you need to pretend
109         //      to be a cloud hub for some reason.
110         // $m->connect('/main/rsscloud/notify', array('action' => 'LoggingAggregator'));
111
112         return true;
113     }
114
115     /**
116      * Automatically load the actions and libraries used by
117      * the RSSCloud plugin
118      *
119      * @param Class $cls the class
120      *
121      * @return boolean hook return
122      *
123      */
124
125     function onAutoload($cls)
126     {
127         switch ($cls)
128         {
129          case 'RSSCloudSubscription':
130             include_once(INSTALLDIR . '/plugins/RSSCloud/RSSCloudSubscription.php');
131             return false;
132          case 'RSSCloudNotifier':
133             include_once(INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php');
134             return false;
135          case 'RSSCloudRequestNotifyAction':
136          case 'LoggingAggregatorAction':
137             include_once(INSTALLDIR . '/plugins/RSSCloud/' . mb_substr($cls, 0, -6) . '.php');
138             return false;
139          default:
140             return true;
141         }
142     }
143
144     /**
145      * Add a <cloud> element to the RSS feed (after the rss <channel>
146      * element is started).
147      *
148      * @param Action $action
149      *
150      * @return void
151      */
152
153     function onStartApiRss($action)
154     {
155         if (get_class($action) == 'ApiTimelineUserAction') {
156
157             $attrs = array('domain'            => $this->domain,
158                            'port'              => $this->port,
159                            'path'              => $this->path,
160                            'registerProcedure' => $this->funct,
161                            'protocol'          => $this->protocol);
162
163             // Dipping into XMLWriter to avoid a full end element (</cloud>).
164
165             $action->xw->startElement('cloud');
166             foreach ($attrs as $name => $value) {
167                 $action->xw->writeAttribute($name, $value);
168             }
169
170             $action->xw->endElement();
171         }
172     }
173
174     /**
175      * Add an RSSCloud queue item for each notice
176      *
177      * @param Notice $notice      the notice
178      * @param array  &$transports the list of transports (queues)
179      *
180      * @return boolean hook return
181      */
182
183     function onStartEnqueueNotice($notice, &$transports)
184     {
185         array_push($transports, 'rsscloud');
186         return true;
187     }
188
189     /**
190      * broadcast the message when not using queuehandler
191      *
192      * @param Notice &$notice the notice
193      * @param array  $queue   destination queue
194      *
195      * @return boolean hook return
196      */
197
198     function onUnqueueHandleNotice(&$notice, $queue)
199     {
200         if (($queue == 'rsscloud') && ($this->_isLocal($notice))) {
201
202             common_debug('broadcasting rssCloud bound notice ' . $notice->id);
203
204             $profile = $notice->getProfile();
205
206             $notifier = new RSSCloudNotifier();
207             $notifier->notify($profile);
208
209             return false;
210         }
211
212         return true;
213     }
214
215     /**
216      * Determine whether the notice was locally created
217      *
218      * @param Notice $notice
219      *
220      * @return boolean locality
221      */
222
223     function _isLocal($notice)
224     {
225         return ($notice->is_local == Notice::LOCAL_PUBLIC ||
226                 $notice->is_local == Notice::LOCAL_NONPUBLIC);
227     }
228
229     /**
230      * Create the rsscloud_subscription table if it's not
231      * already in the DB
232      *
233      * @return boolean hook return
234      */
235
236     function onCheckSchema() {
237         $schema = Schema::get();
238         $schema->ensureTable('rsscloud_subscription',
239                              array(new ColumnDef('subscribed', 'integer',
240                                                  null, false, 'PRI'),
241                                    new ColumnDef('url', 'varchar',
242                                                  '255', false, 'PRI'),
243                                    new ColumnDef('failures', 'integer',
244                                                  null, false, null, 0),
245                                    new ColumnDef('created', 'datetime',
246                                                  null, false),
247                                    new ColumnDef('modified', 'timestamp',
248                                                  null, false, null,
249                                                  'CURRENT_TIMESTAMP',
250                                                  'on update CURRENT_TIMESTAMP')
251                                   )
252                             );
253          return true;
254     }
255
256     /**
257      * Add RSSCloudQueueHandler to the list of valid daemons to
258      * start
259      *
260      * @param array $daemons the list of daemons to run
261      *
262      * @return boolean hook return
263      *
264      */
265
266     function onGetValidDaemons($daemons)
267     {
268         array_push($daemons, INSTALLDIR .
269                    '/plugins/RSSCloud/RSSCloudQueueHandler.php');
270         return true;
271     }
272
273 }
274