3 * @file src/Modules/Settings/UserExport.php
6 namespace Friendica\Module\Settings;
9 use Friendica\App\Arguments;
10 use Friendica\BaseModule;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\Database\DBStructure;
17 use Friendica\Module\BaseSettingsModule;
20 * Module to export user data
22 class UserExport extends BaseSettingsModule
25 * Handle the request to export data.
26 * At the moment one can export two different data set
27 * 1. The profile data that can be used by uimport to resettle
28 * to a different Friendica instance
29 * 2. The entire data-set, profile plus postings
31 * If there is an action required through the URL / path, react
32 * accordingly and export the requested data.
34 public static function content()
39 * options shown on "Export personal data" page
40 * list of array( 'link url', 'link text', 'help text' )
43 ['settings/uexport/account', L10n::t('Export account'), 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.')],
44 ['settings/uexport/backup', L10n::t('Export all'), L10n::t("Export your accout 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")],
46 Hook::callAll('uexport_options', $options);
48 $tpl = Renderer::getMarkupTemplate("settings/userexport.tpl");
49 return Renderer::replaceMacros($tpl, [
50 '$title' => L10n::t('Export personal data'),
51 '$options' => $options
55 * raw content generated for the different choices made
56 * by the user. At the moment this returns a JSON file
57 * to the browser which then offers a save / open dialog
60 public static function rawContent()
62 $args = self::getClass(Arguments::class);
63 if ($args->getArgc() == 3) {
64 // @TODO Replace with router-provided arguments
65 $action = $args->get(2);
66 $user = self::getApp()->user;
67 header("Content-type: application/json");
68 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
71 self::exportAll(self::getApp());
75 self::exportAccount(self::getApp());
83 private static function exportMultiRow(string $query)
85 $dbStructure = DBStructure::definition(self::getApp()->getBasePath(), false);
87 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
92 if (DBA::isResult($r)) {
95 foreach ($rr as $k => $v) {
96 switch ($dbStructure[$table]['fields'][$k]['type']) {
98 $p[$k] = $v ?? DBA::NULL_DATETIME;
111 private static function exportRow(string $query)
113 $dbStructure = DBStructure::definition(self::getApp()->getBasePath(), false);
115 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
120 if (DBA::isResult($r)) {
122 foreach ($r as $rr) {
123 foreach ($rr as $k => $v) {
124 switch ($dbStructure[$table]['fields'][$k]['type']) {
126 $result[$k] = $v ?? DBA::NULL_DATETIME;
138 private static function exportAccount(App $a)
140 $user = self::exportRow(
141 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
144 $contact = self::exportMultiRow(
145 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
149 $profile = self::exportMultiRow(
150 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
153 $photo = self::exportMultiRow(
154 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
156 foreach ($photo as &$p) {
157 $p['data'] = bin2hex($p['data']);
160 $pconfig = self::exportMultiRow(
161 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
164 $group = self::exportMultiRow(
165 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
168 $group_member = self::exportMultiRow(
169 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", intval(local_user()))
173 'version' => FRIENDICA_VERSION,
174 'schema' => DB_UPDATE_VERSION,
175 'baseurl' => System::baseUrl(),
177 'contact' => $contact,
178 'profile' => $profile,
180 'pconfig' => $pconfig,
182 'group_member' => $group_member,
185 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
189 * echoes account data and items as separated json, one per line
194 private static function exportAll(App $a)
196 self::exportAccount($a);
199 $total = DBA::count('item', ['uid' => local_user()]);
200 // chunk the output to avoid exhausting memory
202 for ($x = 0; $x < $total; $x += 500) {
203 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
204 intval(local_user()),
209 $output = ['item' => $r];
210 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";