]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/TwitterBridgePlugin.php
SQL syntax error when initializing notice_to_status table
[quix0rs-gnu-social.git] / plugins / TwitterBridge / TwitterBridgePlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * PHP version 5
6  *
7  * LICENCE: This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @category  Plugin
21  * @package   StatusNet
22  * @author    Zach Copley <zach@status.net>
23  * @author    Julien C <chaumond@gmail.com>
24  * @copyright 2009-2010 Control Yourself, Inc.
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link      http://status.net/
27  */
28
29 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
34
35 /**
36  * Plugin for sending and importing Twitter statuses
37  *
38  * This class allows users to link their Twitter accounts
39  *
40  * @category Plugin
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @author   Julien C <chaumond@gmail.com>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  * @link     http://twitter.com/
47  */
48
49 class TwitterBridgePlugin extends Plugin
50 {
51
52     const VERSION = STATUSNET_VERSION;
53     public $adminImportControl = false; // Should the 'import' checkbox be exposed in the admin panel?
54
55     /**
56      * Initializer for the plugin.
57      */
58
59     function initialize()
60     {
61         // Allow the key and secret to be passed in
62         // Control panel will override
63
64         if (isset($this->consumer_key)) {
65             $key = common_config('twitter', 'consumer_key');
66             if (empty($key)) {
67                 Config::save('twitter', 'consumer_key', $this->consumer_key);
68             }
69         }
70
71         if (isset($this->consumer_secret)) {
72             $secret = common_config('twitter', 'consumer_secret');
73             if (empty($secret)) {
74                 Config::save(
75                     'twitter',
76                     'consumer_secret',
77                     $this->consumer_secret
78                 );
79             }
80         }
81     }
82
83     /**
84      * Check to see if there is a consumer key and secret defined
85      * for Twitter integration.
86      *
87      * @return boolean result
88      */
89
90     static function hasKeys()
91     {
92         $ckey    = common_config('twitter', 'consumer_key');
93         $csecret = common_config('twitter', 'consumer_secret');
94
95         if (empty($ckey) && empty($csecret)) {
96             $ckey    = common_config('twitter', 'global_consumer_key');
97             $csecret = common_config('twitter', 'global_consumer_secret');
98         }
99
100         if (!empty($ckey) && !empty($csecret)) {
101             return true;
102         }
103
104         return false;
105     }
106
107     /**
108      * Add Twitter-related paths to the router table
109      *
110      * Hook for RouterInitialized event.
111      *
112      * @param Net_URL_Mapper $m path-to-action mapper
113      *
114      * @return boolean hook return
115      */
116
117     function onRouterInitialized($m)
118     {
119         $m->connect('admin/twitter', array('action' => 'twitteradminpanel'));
120
121         if (self::hasKeys()) {
122             $m->connect(
123                 'twitter/authorization',
124                 array('action' => 'twitterauthorization')
125             );
126             $m->connect(
127                 'settings/twitter', array(
128                     'action' => 'twittersettings'
129                     )
130                 );
131             if (common_config('twitter', 'signin')) {
132                 $m->connect(
133                     'main/twitterlogin',
134                     array('action' => 'twitterlogin')
135                 );
136             }
137         }
138
139         return true;
140     }
141
142     /*
143      * Add a login tab for 'Sign in with Twitter'
144      *
145      * @param Action &action the current action
146      *
147      * @return void
148      */
149     function onEndLoginGroupNav(&$action)
150     {
151         $action_name = $action->trimmed('action');
152
153         if (self::hasKeys() && common_config('twitter', 'signin')) {
154             $action->menuItem(
155                 common_local_url('twitterlogin'),
156                 _m('Twitter'),
157                 _m('Login or register using Twitter'),
158                 'twitterlogin' === $action_name
159             );
160         }
161
162         return true;
163     }
164
165     /**
166      * Add the Twitter Settings page to the Connect Settings menu
167      *
168      * @param Action &$action The calling page
169      *
170      * @return boolean hook return
171      */
172     function onEndConnectSettingsNav(&$action)
173     {
174         if (self::hasKeys()) {
175             $action_name = $action->trimmed('action');
176
177             $action->menuItem(
178                 common_local_url('twittersettings'),
179                 _m('Twitter'),
180                 _m('Twitter integration options'),
181                 $action_name === 'twittersettings'
182             );
183         }
184         return true;
185     }
186
187     /**
188      * Automatically load the actions and libraries used by the Twitter bridge
189      *
190      * @param Class $cls the class
191      *
192      * @return boolean hook return
193      *
194      */
195     function onAutoload($cls)
196     {
197         $dir = dirname(__FILE__);
198
199         switch ($cls) {
200         case 'TwittersettingsAction':
201         case 'TwitterauthorizationAction':
202         case 'TwitterloginAction':
203         case 'TwitteradminpanelAction':
204             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
205             return false;
206         case 'TwitterOAuthClient':
207         case 'TwitterQueueHandler':
208             include_once $dir . '/' . strtolower($cls) . '.php';
209             return false;
210         case 'Notice_to_status':
211             include_once $dir . '/' . $cls . '.php';
212             return false;
213         default:
214             return true;
215         }
216     }
217
218     /**
219      * Add a Twitter queue item for each notice
220      *
221      * @param Notice $notice      the notice
222      * @param array  &$transports the list of transports (queues)
223      *
224      * @return boolean hook return
225      */
226     function onStartEnqueueNotice($notice, &$transports)
227     {
228         if (self::hasKeys() && $notice->isLocal()) {
229             // Avoid a possible loop
230             if ($notice->source != 'twitter') {
231                 array_push($transports, 'twitter');
232             }
233         }
234         return true;
235     }
236
237     /**
238      * Add Twitter bridge daemons to the list of daemons to start
239      *
240      * @param array $daemons the list fo daemons to run
241      *
242      * @return boolean hook return
243      */
244     function onGetValidDaemons($daemons)
245     {
246         if (self::hasKeys()) {
247             array_push(
248                 $daemons,
249                 INSTALLDIR
250                 . '/plugins/TwitterBridge/daemons/synctwitterfriends.php'
251             );
252             if (common_config('twitterimport', 'enabled')) {
253                 array_push(
254                     $daemons,
255                     INSTALLDIR
256                     . '/plugins/TwitterBridge/daemons/twitterstatusfetcher.php'
257                     );
258             }
259         }
260
261         return true;
262     }
263
264     /**
265      * Register Twitter notice queue handler
266      *
267      * @param QueueManager $manager
268      *
269      * @return boolean hook return
270      */
271     function onEndInitializeQueueManager($manager)
272     {
273         if (self::hasKeys()) {
274             $manager->connect('twitter', 'TwitterQueueHandler');
275         }
276         return true;
277     }
278
279     /**
280      * Add a Twitter tab to the admin panel
281      *
282      * @param Widget $nav Admin panel nav
283      *
284      * @return boolean hook value
285      */
286
287     function onEndAdminPanelNav($nav)
288     {
289         if (AdminPanelAction::canAdmin('twitter')) {
290
291             $action_name = $nav->action->trimmed('action');
292
293             $nav->out->menuItem(
294                 common_local_url('twitteradminpanel'),
295                 _m('Twitter'),
296                 _m('Twitter bridge configuration'),
297                 $action_name == 'twitteradminpanel',
298                 'nav_twitter_admin_panel'
299             );
300         }
301
302         return true;
303     }
304
305     /**
306      * Plugin version data
307      *
308      * @param array &$versions array of version blocks
309      *
310      * @return boolean hook value
311      */
312
313     function onPluginVersion(&$versions)
314     {
315         $versions[] = array(
316             'name' => 'TwitterBridge',
317             'version' => self::VERSION,
318             'author' => 'Zach Copley, Julien C',
319             'homepage' => 'http://status.net/wiki/Plugin:TwitterBridge',
320             'rawdescription' => _m(
321                 'The Twitter "bridge" plugin allows you to integrate ' .
322                 'your StatusNet instance with ' .
323                 '<a href="http://twitter.com/">Twitter</a>.'
324             )
325         );
326         return true;
327     }
328
329     /**
330      * Expose the adminImportControl setting to the administration panel code.
331      * This allows us to disable the import bridge enabling checkbox for administrators,
332      * since on a bulk farm site we can't yet automate the import daemon setup.
333      *
334      * @return boolean hook value;
335      */
336     function onTwitterBridgeAdminImportControl()
337     {
338         return (bool)$this->adminImportControl;
339     }
340
341     /**
342      * When the site is set to ssl=sometimes mode, we should make sure our
343      * various auth-related pages are on SSL to keep things looking happy.
344      * Although we're not submitting passwords directly, we do link out to
345      * an authentication source and it's a lot happier if we've got some
346      * protection against MitM.
347      *
348      * @param string $action name
349      * @param boolean $ssl outval to force SSL
350      * @return mixed hook return value
351      */
352     function onSensitiveAction($action, &$ssl)
353     {
354         $sensitive = array('twitteradminpanel',
355                            'twittersettings',
356                            'twitterauthorization',
357                            'twitterlogin');
358         if (in_array($action, $sensitive)) {
359             $ssl = true;
360             return false;
361         } else {
362             return true;
363         }
364     }
365
366     /**
367      * Database schema setup
368      *
369      * We maintain a table mapping StatusNet notices to Twitter statuses
370      *
371      * @see Schema
372      * @see ColumnDef
373      *
374      * @return boolean hook value; true means continue processing, false means stop.
375      */
376
377     function onCheckSchema()
378     {
379         $schema = Schema::get();
380
381         // For storing user-submitted flags on profiles
382
383         $schema->ensureTable('notice_to_status',
384                              array(new ColumnDef('notice_id', 'integer', null,
385                                                  false, 'PRI'),
386                                    new ColumnDef('status_id', 'integer', null,
387                                                  false, 'UNI'),
388                                    new ColumnDef('created', 'datetime', null,
389                                                  false)));
390
391         // We update any notices that may have come in from
392         // Twitter that we don't have a status_id for. Note that
393         // this won't catch notices that originated at this StatusNet site.
394
395         $n = new Notice();
396
397         $n->query('SELECT notice.id, notice.uri ' .
398                   'FROM notice LEFT JOIN notice_to_status ' .
399                   'ON notice.id = notice_to_status.notice_id ' .
400                   'WHERE notice.source = "twitter"' .
401                   'AND notice_to_status.status_id IS NULL');
402
403         while ($n->fetch()) {
404             if (preg_match('#^http://twitter.com/[\w_.]+/status/(\d+)$#', $n->uri, $match)) {
405
406                 $status_id = $match[1];
407
408                 Notice_to_status::saveNew($n->id, $status_id);
409             }
410         }
411
412         return true;
413     }
414
415     /**
416      * If a notice gets deleted, remove the Notice_to_status mapping
417      *
418      * @param Notice $notice The notice getting deleted
419      *
420      * @return boolean hook value
421      */
422
423     function onNoticeDeleteRelated($notice)
424     {
425         $n2s = Notice_to_status::staticGet('notice_id', $notice->id);
426         if (!empty($n2s)) {
427             $n2s->delete();
428         }
429         return true;
430     }
431 }