]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OfflineBackup/lib/offlinebackupqueuehandler.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / OfflineBackup / lib / offlinebackupqueuehandler.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Offline backup queue handler
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Offline backup
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Offline backup queue handler
39  *
40  * @category  General
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2011 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47
48 class OfflineBackupQueueHandler extends QueueHandler
49 {
50     function transport()
51     {
52         return 'backoff';
53     }
54
55     function handle($object)
56     {
57         $userId = $object;
58
59         $user = User::getKV($userId);
60
61         common_log(LOG_INFO, "Making backup file for user ".$user->nickname);
62
63         $fileName = $this->makeBackupFile($user);
64
65         common_log(LOG_INFO, "Notifying user ".$user->nickname . " of their new backup file.");
66
67         $this->notifyBackupFile($user, $fileName);
68
69         return true;
70     }
71
72     function makeBackupFile($user)
73     {
74         // XXX: this is pretty lose-y;  try another way
75
76         $tmpdir = sys_get_temp_dir() . '/offline-backup/' . $user->nickname . '/' . common_date_iso8601(common_sql_now());
77
78         common_log(LOG_INFO, 'Writing backup data to ' . $tmpdir . ' for ' . $user->nickname);
79
80         mkdir($tmpdir, 0700, true);
81
82         $this->dumpNotices($user, $tmpdir);
83         $this->dumpFaves($user, $tmpdir);
84         $this->dumpSubscriptions($user, $tmpdir);
85         $this->dumpSubscribers($user, $tmpdir);
86         $this->dumpGroups($user, $tmpdir);
87
88         $fileName = File::filename($user->getProfile(), "backup", "application/atom+xml");
89         $fullPath = File::path($fileName);
90
91         $this->makeActivityFeed($user, $tmpdir, $fullPath);
92
93         $this->delTree($tmpdir);
94
95         return $fileName;
96     }
97
98     function notifyBackupFile($user, $fileName)
99     {
100         $fileUrl = File::url($fileName);
101
102         $body = sprintf(_m("The backup file you requested is ready for download.\n\n".
103                            "%s\n".
104                            "Thanks for your time,\n",
105                            "%s\n"),
106                         $fileUrl,
107                         common_config('site', 'name'));
108
109         $headers = _mail_prepare_headers('offlinebackup', $user->nickname, $user->nickname);
110
111         mail_to_user($user, _('Backup file ready for download'), $body, $headers);
112     }
113
114     function dumpNotices($user, $dir)
115     {
116         common_log(LOG_INFO, 'dumping notices by ' . $user->nickname . ' to directory ' . $dir);
117
118         $profile = $user->getProfile();
119
120         $stream = new ProfileNoticeStream($profile, $profile);
121
122         $page = 1;
123
124         do {
125
126             $notice = $stream->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
127
128             while ($notice->fetch()) {
129                 try {
130                     $fname = $dir . '/'. common_date_iso8601($notice->created) . '-notice-' . $notice->id . '.atom'; 
131                     $data  = $notice->asAtomEntry(false, false, false, null);
132                     common_log(LOG_INFO, 'dumping notice ' . $notice->id . ' to file ' . $fname);
133                     file_put_contents($fname, $data);
134                     $data  = null;
135                 } catch (Exception $e) {
136                     common_log(LOG_ERR, "Error backing up notice " . $notice->id . ": " . $e->getMessage());
137                     continue;
138                 }
139             }
140
141             $page++;
142
143         } while ($notice->N > NOTICES_PER_PAGE);
144     }
145
146     function dumpFaves($user, $dir)
147     {
148         common_log(LOG_INFO, 'dumping faves by ' . $user->nickname . ' to directory ' . $dir);
149         
150         $page = 1;
151
152         do {
153             $fave = Fave::byProfile($user->id, ($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
154
155             while ($fave->fetch()) {
156                 try {
157                     $fname = $dir . '/'. common_date_iso8601($fave->modified) . '-fave-' . $fave->notice_id . '.atom'; 
158                     $act   = $fave->asActivity();
159                     $data  = $act->asString(false, false, false);
160                     common_log(LOG_INFO, 'dumping fave of ' . $fave->notice_id . ' to file ' . $fname);
161                     file_put_contents($fname, $data);
162                     $data  = null;
163                 } catch (Exception $e) {
164                     common_log(LOG_ERR, "Error backing up fave of " . $fave->notice_id . ": " . $e->getMessage());
165                     continue;
166                 }
167             }
168             
169             $page++;
170
171         } while ($fave->N > NOTICES_PER_PAGE);
172     }
173
174     function dumpSubscriptions($user, $dir)
175     {
176         common_log(LOG_INFO, 'dumping subscriptions by ' . $user->nickname . ' to directory ' . $dir);
177         
178         $page = 1;
179
180         do {
181             $sub = Subscription::bySubscriber($user->id, ($page-1)*PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1);
182
183             while ($sub->fetch()) {
184                 try {
185                     if ($sub->subscribed == $user->id) {
186                         continue;
187                     }
188                     $fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscription-' . $sub->subscribed . '.atom'; 
189                     $act   = $sub->asActivity();
190                     $data  = $act->asString(false, false, false);
191                     common_log(LOG_INFO, 'dumping sub of ' . $sub->subscribed . ' to file ' . $fname);
192                     file_put_contents($fname, $data);
193                     $data  = null;
194                 } catch (Exception $e) {
195                     common_log(LOG_ERR, "Error backing up subscription to " . $sub->subscribed . ": " . $e->getMessage());
196                     continue;
197                 }
198             }
199
200             $page++;
201
202         } while ($sub->N > PROFILES_PER_PAGE);
203     }
204
205     function dumpSubscribers($user, $dir)
206     {
207         common_log(LOG_INFO, 'dumping subscribers to ' . $user->nickname . ' to directory ' . $dir);
208         
209         $page = 1;
210
211         do {
212             $sub = Subscription::bySubscribed($user->id, ($page-1)*PROFILES_PER_PAGE, PROFILES_PER_PAGE + 1);
213
214             while ($sub->fetch()) {
215                 try {
216                     if ($sub->subscriber == $user->id) {
217                         continue;
218                     }
219                     $fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscriber-' . $sub->subscriber . '.atom'; 
220                     $act   = $sub->asActivity();
221                     $data  = $act->asString(false, true, false);
222                     common_log(LOG_INFO, 'dumping sub by ' . $sub->subscriber . ' to file ' . $fname);
223                     file_put_contents($fname, $data);
224                     $data  = null;
225                 } catch (Exception $e) {
226                     common_log(LOG_ERR, "Error backing up subscription from " . $sub->subscriber . ": " . $e->getMessage());
227                     continue;
228                 }
229             }
230
231             $page++;
232
233         } while ($sub->N > PROFILES_PER_PAGE);
234     }
235
236     function dumpGroups($user, $dir)
237     {
238         common_log(LOG_INFO, 'dumping memberships of ' . $user->nickname . ' to directory ' . $dir);
239         
240         $page = 1;
241
242         do {
243
244             $mem = Group_member::byMember($user->id, ($page-1)*GROUPS_PER_PAGE, GROUPS_PER_PAGE + 1);
245
246             while ($mem->fetch()) {
247                 try {
248                     $fname = $dir . '/'. common_date_iso8601($mem->created) . '-membership-' . $mem->group_id . '.atom'; 
249                     $act   = $mem->asActivity();
250                     $data  = $act->asString(false, false, false);
251                     common_log(LOG_INFO, 'dumping membership in ' . $mem->group_id . ' to file ' . $fname);
252                     file_put_contents($fname, $data);
253                     $data  = null;
254                 } catch (Exception $e) {
255                     common_log(LOG_ERR, "Error backing up membership in " . $mem->group_id . ": " . $e->getMessage());
256                     continue;
257                 }
258             }
259
260             $page++;
261
262         } while ($mem->N > GROUPS_PER_PAGE);
263     }
264
265     function makeActivityFeed($user, $tmpdir, $fullPath)
266     {
267         $handle = fopen($fullPath, 'c');
268
269         $this->writeFeedHeader($user, $handle);
270
271         $objects = scandir($tmpdir);
272
273         rsort($objects);
274
275         foreach ($objects as $object) {
276             $objFull = $tmpdir . '/' . $object;
277             if (!is_dir($objFull)) {
278                 $entry = file_get_contents($objFull);
279                 fwrite($handle, $entry);
280                 $entry = null;
281             }
282         }
283
284         $this->writeFeedFooter($user, $handle);
285         fclose($handle);
286     }
287
288     function writeFeedHeader($user, $handle)
289     {
290         fwrite($handle, '<?xml version="1.0" encoding="UTF-8"?>');
291         fwrite($handle, "\n");
292         fwrite($handle, '<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">');
293         fwrite($handle, "\n");
294
295         $profile = $user->getProfile();
296
297         $author = $profile->asActivityObject();
298
299         $xs = new XMLStringer();
300         $author->outputTo($xs, 'author');
301         fwrite($handle, $xs->getString());
302         fwrite($handle, "\n");
303     }
304
305     function writeFeedFooter($user, $handle)
306     {
307         fwrite($handle, '</feed>');
308     }
309
310     function delTree($dir)
311     {
312         if (is_dir($dir)) {
313             $objects = scandir($dir);
314             foreach ($objects as $object) {
315                 if ($object != "." && $object != "..") {
316                     if (filetype($dir."/".$object) == "dir") {
317                         $this->delTree($dir."/".$object);
318                     } else {
319                         unlink($dir."/".$object);
320                     }
321                 }
322             }
323             reset($objects);
324             rmdir($dir);
325         }
326     }
327 }