]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/daemons/twitterstatusfetcher.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / TwitterBridge / daemons / twitterstatusfetcher.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008-2010, StatusNet, Inc.
6  *
7  * 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
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
22
23 // Tune number of processes and how often to poll Twitter
24 // XXX: Should these things be in config.php?
25 define('MAXCHILDREN', 2);
26 define('POLL_INTERVAL', 70); // in seconds, Twitter API v1.1 says 15 calls every 15 mins
27
28 $shortoptions = 'di::';
29 $longoptions = array('id::', 'debug');
30
31 $helptext = <<<END_OF_TRIM_HELP
32 Batch script for retrieving Twitter messages from foreign service.
33
34   -i --id              Identity (default 'generic')
35   -d --debug           Debug (lots of log output)
36
37 END_OF_TRIM_HELP;
38
39 require_once INSTALLDIR . '/scripts/commandline.inc';
40 require_once INSTALLDIR . '/lib/common.php';
41 require_once INSTALLDIR . '/lib/daemon.php';
42 require_once dirname(__DIR__) . '/twitter.php';
43
44 /**
45  * Fetch statuses from Twitter
46  *
47  * Fetches statuses from Twitter and inserts them as notices
48  *
49  * NOTE: an Avatar path MUST be set in config.php for this
50  * script to work, e.g.:
51  *     $config['avatar']['path'] = $config['site']['path'] . '/avatar/';
52  *
53  * @todo @fixme @gar Fix the above. For some reason $_path is always empty when
54  * this script is run, so the default avatar path is always set wrong in
55  * default.php. Therefore it must be set explicitly in config.php. --Z
56  *
57  * @category Twitter
58  * @package  StatusNet
59  * @author   Zach Copley <zach@status.net>
60  * @author   Evan Prodromou <evan@status.net>
61  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
62  * @link     http://status.net/
63  */
64 class TwitterStatusFetcher extends ParallelizingDaemon
65 {
66     /**
67      *  Constructor
68      *
69      * @param string  $id           the name/id of this daemon
70      * @param int     $interval     sleep this long before doing everything again
71      * @param int     $max_children maximum number of child processes at a time
72      * @param boolean $debug        debug output flag
73      *
74      * @return void
75      *
76      **/
77     function __construct($id = null, $interval = 60,
78                          $max_children = 2, $debug = null)
79     {
80         parent::__construct($id, $interval, $max_children, $debug);
81     }
82
83     /**
84      * Name of this daemon
85      *
86      * @return string Name of the daemon.
87      */
88     function name()
89     {
90         return ('twitterstatusfetcher.'.$this->_id);
91     }
92
93     /**
94      * Find all the Twitter foreign links for users who have requested
95      * importing of their friends' timelines
96      *
97      * @return array flinks an array of Foreign_link objects
98      */
99     function getObjects()
100     {
101         global $_DB_DATAOBJECT;
102         $flink = new Foreign_link();
103         $conn = &$flink->getDatabaseConnection();
104
105         $flink->service = TWITTER_SERVICE;
106         $flink->orderBy('last_noticesync');
107         $flink->find();
108
109         $flinks = array();
110
111         while ($flink->fetch()) {
112
113             if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
114                 FOREIGN_NOTICE_RECV) {
115                 $flinks[] = clone($flink);
116                 common_log(LOG_INFO, "sync: foreign id $flink->foreign_id");
117             } else {
118                 common_log(LOG_INFO, "nothing to sync");
119             }
120         }
121
122         $flink->free();
123         unset($flink);
124
125         $conn->disconnect();
126         unset($_DB_DATAOBJECT['CONNECTIONS']);
127
128         return $flinks;
129     }
130
131     // FIXME: make it so we can force a Foreign_link here without colliding with parent
132     function childTask($flink) {
133         // Each child ps needs its own DB connection
134
135         // Note: DataObject::getDatabaseConnection() creates
136         // a new connection if there isn't one already
137         $conn = &$flink->getDatabaseConnection();
138
139         $this->getTimeline($flink, 'home_timeline');
140         $this->getTimeline($flink, 'mentions_timeline');
141
142         $flink->last_friendsync = common_sql_now();
143         $flink->update();
144
145         $conn->disconnect();
146
147         // XXX: Couldn't find a less brutal way to blow
148         // away a cached connection
149         global $_DB_DATAOBJECT;
150         unset($_DB_DATAOBJECT['CONNECTIONS']);
151     }
152
153     function getTimeline(Foreign_link $flink, $timelineUri = 'home_timeline')
154     {
155         common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri .
156                    ' timeline for Twitter user ' . $flink->foreign_id);
157
158         $client = null;
159
160         if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
161             $token = TwitterOAuthClient::unpackToken($flink->credentials);
162             $client = new TwitterOAuthClient($token->key, $token->secret);
163             common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
164         } else {
165             common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
166                        $flink->foreign_id . " since not OAuth.");
167         }
168
169         $timeline = null;
170
171         $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
172
173         common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" .
174                      $flink->foreign_id . "' and timeline '" . $timelineUri. "'");
175
176         try {
177             $timeline = $client->statusesTimeline($lastId, $timelineUri);
178         } catch (Exception $e) {
179             common_log(LOG_ERR, $this->name() .
180                        ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
181                        ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
182         }
183
184         if (empty($timeline)) {
185             common_log(LOG_DEBUG, $this->name() .  " - Empty '" . $timelineUri . "' timeline.");
186             return;
187         }
188
189         common_log(LOG_INFO, $this->name() .
190                    ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
191                    ' - for user ' . $flink->user_id);
192
193         if (!empty($timeline)) {
194             $qm = QueueManager::get();
195
196             // Reverse to preserve order
197             foreach (array_reverse($timeline) as $status) {
198                 $data = array(
199                     'status' => $status,
200                     'for_user' => $flink->foreign_id,
201                 );
202                 $qm->enqueue($data, 'tweetin');
203             }
204
205             $lastId = twitter_id($timeline[0]);
206             Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
207             common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
208                          $timelineUri . "'");
209         }
210
211         // Okay, record the time we synced with Twitter for posterity
212         $flink->last_noticesync = common_sql_now();
213         $flink->update();
214     }
215 }
216
217 $id    = null;
218 $debug = null;
219
220 if (have_option('i')) {
221     $id = get_option_value('i');
222 } else if (have_option('--id')) {
223     $id = get_option_value('--id');
224 } else if (count($args) > 0) {
225     $id = $args[0];
226 } else {
227     $id = null;
228 }
229
230 if (have_option('d') || have_option('debug')) {
231     $debug = true;
232 }
233
234 $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);
235 $fetcher->runOnce();