4 use Friendica\Core\Addon;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
8 function uexport_init(App $a) {
13 require_once("mod/settings.php");
17 function uexport_content(App $a) {
20 header("Content-type: application/json");
21 header('Content-Disposition: attachment; filename="' . $a->user['nickname'] . '.' . $a->argv[1] . '"');
22 switch ($a->argv[1]) {
37 * options shown on "Export personal data" page
38 * list of array( 'link url', 'link text', 'help text' )
41 ['uexport/account', t('Export account'), t('Export your account info and contacts. Use this to make a backup of your account and/or to move it to another server.')],
42 ['uexport/backup', t('Export all'), 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 (photos are not exported)')],
44 Addon::callHooks('uexport_options', $options);
46 $tpl = get_markup_template("uexport.tpl");
47 return replace_macros($tpl, [
48 '$baseurl' => System::baseUrl(),
49 '$title' => t('Export personal data'),
50 '$options' => $options
54 function _uexport_multirow($query) {
57 if (DBM::is_result($r)) {
60 foreach ($rr as $k => $v) {
69 function _uexport_row($query) {
72 if (DBM::is_result($r)) {
74 foreach ($rr as $k => $v) {
82 function uexport_account($a) {
85 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
88 $contact = _uexport_multirow(
89 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
93 $profile = _uexport_multirow(
94 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
97 $photo = _uexport_multirow(
98 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
100 foreach ($photo as &$p) {
101 $p['data'] = bin2hex($p['data']);
104 $pconfig = _uexport_multirow(
105 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
108 $group = _uexport_multirow(
109 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
112 $group_member = _uexport_multirow(
113 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()))
117 'version' => FRIENDICA_VERSION,
118 'schema' => DB_UPDATE_VERSION,
119 'baseurl' => System::baseUrl(),
121 'contact' => $contact,
122 'profile' => $profile,
124 'pconfig' => $pconfig,
126 'group_member' => $group_member,
129 //echo "<pre>"; var_dump(json_encode($output)); killme();
130 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
134 * echoes account data and items as separated json, one per line
136 function uexport_all(App $a) {
141 $r = q("SELECT count(*) as `total` FROM `item` WHERE `uid` = %d ",
144 if (DBM::is_result($r)) {
145 $total = $r[0]['total'];
147 // chunk the output to avoid exhausting memory
149 for ($x = 0; $x < $total; $x += 500) {
151 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
152 intval(local_user()),
157 $output = ['item' => $r];
158 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";