3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Settings;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\Core\System;
29 use Friendica\Database\DBA;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post;
33 use Friendica\Module\BaseSettings;
34 use Friendica\Network\HTTPException;
37 * Module to export user data
39 class UserExport extends BaseSettings
42 * Handle the request to export data.
43 * At the moment one can export three different data set
44 * 1. The profile data that can be used by uimport to resettle
45 * to a different Friendica instance
46 * 2. The entire data-set, profile plus postings
47 * 3. A list of contacts as CSV file similar to the export of Mastodon
49 * If there is an action required through the URL / path, react
50 * accordingly and export the requested data.
53 * @throws HTTPException\ForbiddenException
54 * @throws HTTPException\InternalServerErrorException
56 protected function content(array $request = []): string
58 if (!Session::getLocalUser()) {
59 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
65 * options shown on "Export personal data" page
66 * list of array( 'link url', 'link text', 'help text' )
69 ['settings/userexport/account', DI::l10n()->t('Export account'), DI::l10n()->t('Export your account info and contacts. Use this to make a backup of your account and/or to move it to another server.')],
70 ['settings/userexport/backup', DI::l10n()->t('Export all'), DI::l10n()->t("Export your account info, contacts and all your items as json. Could be a very big file, and could take a lot of time. Use this to make a full backup of your account \x28photos are not exported\x29")],
71 ['settings/userexport/contact', DI::l10n()->t('Export Contacts to CSV'), DI::l10n()->t("Export the list of the accounts you are following as CSV file. Compatible to e.g. Mastodon.")],
73 Hook::callAll('uexport_options', $options);
75 $tpl = Renderer::getMarkupTemplate("settings/userexport.tpl");
76 return Renderer::replaceMacros($tpl, [
77 '$title' => DI::l10n()->t('Export personal data'),
78 '$options' => $options
83 * raw content generated for the different choices made
84 * by the user. At the moment this returns a JSON file
85 * to the browser which then offers a save / open dialog
88 * @throws HTTPException\ForbiddenException
90 protected function rawContent(array $request = [])
92 if (!DI::app()->isLoggedIn()) {
93 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
97 if ($args->getArgc() == 3) {
98 // @TODO Replace with router-provided arguments
99 $action = $args->get(2);
102 header("Content-type: application/json");
103 header('Content-Disposition: attachment; filename="' . DI::app()->getLoggedInUserNickname() . '.' . $action . '"');
104 self::exportAll(Session::getLocalUser());
107 header("Content-type: application/json");
108 header('Content-Disposition: attachment; filename="' . DI::app()->getLoggedInUserNickname() . '.' . $action . '"');
109 self::exportAccount(Session::getLocalUser());
112 header("Content-type: application/csv");
113 header('Content-Disposition: attachment; filename="' . DI::app()->getLoggedInUserNickname() . '-contacts.csv' . '"');
114 self::exportContactsAsCSV(Session::getLocalUser());
122 * @param string $query
126 private static function exportMultiRow(string $query)
128 $dbStructure = DI::dbaDefinition()->getAll();
130 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
134 $rows = DBA::p($query);
135 while ($row = DBA::fetch($rows)) {
137 foreach ($dbStructure[$table]['fields'] as $column => $field) {
138 if (!isset($row[$column])) {
141 if ($field['type'] == 'datetime') {
142 $p[$column] = $row[$column] ?? DBA::NULL_DATETIME;
144 $p[$column] = $row[$column];
154 * @param string $query
158 private static function exportRow(string $query)
160 $dbStructure = DI::dbaDefinition()->getAll();
162 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
166 $rows = DBA::p($query);
167 while ($row = DBA::fetch($rows)) {
168 foreach ($row as $k => $v) {
169 if (empty($dbStructure[$table]['fields'][$k])) {
173 switch ($dbStructure[$table]['fields'][$k]['type']) {
175 $result[$k] = $v ?? DBA::NULL_DATETIME;
189 * Export a list of the contacts as CSV file as e.g. Mastodon and Pleroma are doing.
191 * @param int $user_id
194 private static function exportContactsAsCSV(int $user_id)
197 throw new \RuntimeException(DI::l10n()->t('Permission denied.'));
200 // write the table header (like Mastodon)
201 echo "Account address, Show boosts\n";
202 // get all the contacts
203 $contacts = DBA::select('contact', ['addr', 'url'], ['uid' => $user_id, 'self' => false, 'rel' => [1, 3], 'deleted' => false]);
204 while ($contact = DBA::fetch($contacts)) {
205 echo ($contact['addr'] ?: $contact['url']) . ", true\n";
207 DBA::close($contacts);
211 * @param int $user_id
214 private static function exportAccount(int $user_id)
217 throw new \RuntimeException(DI::l10n()->t('Permission denied.'));
220 $user = self::exportRow(
221 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", $user_id)
224 $contact = self::exportMultiRow(
225 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", $user_id)
229 $profile = self::exportMultiRow(
230 sprintf("SELECT *, 'default' AS `profile_name`, 1 AS `is-default` FROM `profile` WHERE `uid` = %d ", $user_id)
233 $profile_fields = self::exportMultiRow(
234 sprintf("SELECT * FROM `profile_field` WHERE `uid` = %d ", $user_id)
237 $photo = self::exportMultiRow(
238 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", $user_id)
240 foreach ($photo as &$p) {
241 $p['data'] = bin2hex($p['data']);
244 $pconfig = self::exportMultiRow(
245 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", $user_id)
248 $group = self::exportMultiRow(
249 sprintf("SELECT * FROM `group` WHERE uid = %d", $user_id)
252 $group_member = self::exportMultiRow(
253 sprintf("SELECT `group_member`.`gid`, `group_member`.`contact-id` FROM `group_member` INNER JOIN `group` ON `group`.`id` = `group_member`.`gid` WHERE `group`.`uid` = %d", $user_id)
257 'version' => App::VERSION,
258 'schema' => DB_UPDATE_VERSION,
259 'baseurl' => DI::baseUrl(),
261 'contact' => $contact,
262 'profile' => $profile,
263 'profile_fields' => $profile_fields,
265 'pconfig' => $pconfig,
267 'group_member' => $group_member,
270 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
274 * echoes account data and items as separated json, one per line
276 * @param int $user_id
279 private static function exportAll(int $user_id)
282 throw new \RuntimeException(DI::l10n()->t('Permission denied.'));
285 self::exportAccount($user_id);
288 $total = Post::count(['uid' => $user_id]);
289 // chunk the output to avoid exhausting memory
291 for ($x = 0; $x < $total; $x += 500) {
292 $items = Post::selectToArray(Item::ITEM_FIELDLIST, ['uid' => $user_id], ['limit' => [$x, 500]]);
293 $output = ['item' => $items];
294 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR) . "\n";