3 * @file mod/uexport.php
7 use Friendica\Core\Hook;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\Database\DBStructure;
14 function uexport_init(App $a) {
15 /// @todo Don't forget to move this global field as static field in src/Modules
22 require_once("mod/settings.php");
25 $dbStructure = DBStructure::definition($a->getBasePath());
28 function uexport_content(App $a) {
31 header("Content-type: application/json");
32 header('Content-Disposition: attachment; filename="' . $a->user['nickname'] . '.' . $a->argv[1] . '"');
33 switch ($a->argv[1]) {
48 * options shown on "Export personal data" page
49 * list of array( 'link url', 'link text', 'help text' )
52 ['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.')],
53 ['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")],
55 Hook::callAll('uexport_options', $options);
57 $tpl = Renderer::getMarkupTemplate("uexport.tpl");
58 return Renderer::replaceMacros($tpl, [
59 '$title' => L10n::t('Export personal data'),
60 '$options' => $options
64 function _uexport_multirow($query) {
67 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
72 if (DBA::isResult($r)) {
75 foreach ($rr as $k => $v) {
76 switch ($dbStructure[$table]['fields'][$k]['type']) {
78 $p[$k] = $v ?? DBA::NULL_DATETIME;
91 function _uexport_row($query) {
94 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
99 if (DBA::isResult($r)) {
101 foreach ($r as $rr) {
102 foreach ($rr as $k => $v) {
103 switch ($dbStructure[$table]['fields'][$k]['type']) {
105 $result[$k] = $v ?? DBA::NULL_DATETIME;
117 function uexport_account($a) {
119 $user = _uexport_row(
120 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
123 $contact = _uexport_multirow(
124 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
128 $profile = _uexport_multirow(
129 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
132 $photo = _uexport_multirow(
133 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
135 foreach ($photo as &$p) {
136 $p['data'] = bin2hex($p['data']);
139 $pconfig = _uexport_multirow(
140 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
143 $group = _uexport_multirow(
144 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
147 $group_member = _uexport_multirow(
148 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()))
152 'version' => FRIENDICA_VERSION,
153 'schema' => DB_UPDATE_VERSION,
154 'baseurl' => System::baseUrl(),
156 'contact' => $contact,
157 'profile' => $profile,
159 'pconfig' => $pconfig,
161 'group_member' => $group_member,
164 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
168 * echoes account data and items as separated json, one per line
173 function uexport_all(App $a) {
179 $r = q("SELECT count(*) as `total` FROM `item` WHERE `uid` = %d ",
182 if (DBA::isResult($r)) {
183 $total = $r[0]['total'];
185 // chunk the output to avoid exhausting memory
187 for ($x = 0; $x < $total; $x += 500) {
188 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
189 intval(local_user()),
194 $output = ['item' => $r];
195 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";