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) {
21 require_once("mod/settings.php");
24 $dbStructure = DBStructure::definition($a->getBasePath());
27 function uexport_content(App $a) {
30 header("Content-type: application/json");
31 header('Content-Disposition: attachment; filename="' . $a->user['nickname'] . '.' . $a->argv[1] . '"');
32 switch ($a->argv[1]) {
47 * options shown on "Export personal data" page
48 * list of array( 'link url', 'link text', 'help text' )
51 ['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.')],
52 ['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")],
54 Hook::callAll('uexport_options', $options);
56 $tpl = Renderer::getMarkupTemplate("uexport.tpl");
57 return Renderer::replaceMacros($tpl, [
58 '$title' => L10n::t('Export personal data'),
59 '$options' => $options
63 function _uexport_multirow($query) {
66 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
71 if (DBA::isResult($r)) {
74 foreach ($rr as $k => $v) {
75 switch ($dbStructure[$table]['fields'][$k]['type']) {
77 $p[$k] = !empty($v) ? $v : DBA::NULL_DATETIME;
90 function _uexport_row($query) {
93 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
98 if (DBA::isResult($r)) {
100 foreach ($r as $rr) {
101 foreach ($rr as $k => $v) {
102 switch ($dbStructure[$table]['fields'][$k]['type']) {
104 $result[$k] = !empty($v) ? $v : DBA::NULL_DATETIME;
116 function uexport_account($a) {
118 $user = _uexport_row(
119 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
122 $contact = _uexport_multirow(
123 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
127 $profile = _uexport_multirow(
128 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
131 $photo = _uexport_multirow(
132 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
134 foreach ($photo as &$p) {
135 $p['data'] = bin2hex($p['data']);
138 $pconfig = _uexport_multirow(
139 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
142 $group = _uexport_multirow(
143 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
146 $group_member = _uexport_multirow(
147 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()))
151 'version' => FRIENDICA_VERSION,
152 'schema' => DB_UPDATE_VERSION,
153 'baseurl' => System::baseUrl(),
155 'contact' => $contact,
156 'profile' => $profile,
158 'pconfig' => $pconfig,
160 'group_member' => $group_member,
163 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
167 * echoes account data and items as separated json, one per line
172 function uexport_all(App $a) {
178 $r = q("SELECT count(*) as `total` FROM `item` WHERE `uid` = %d ",
181 if (DBA::isResult($r)) {
182 $total = $r[0]['total'];
184 // chunk the output to avoid exhausting memory
186 for ($x = 0; $x < $total; $x += 500) {
187 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
188 intval(local_user()),
193 $output = ['item' => $r];
194 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";