]> git.mxchange.org Git - friendica.git/blob - mod/uexport.php
Merge pull request #4322 from annando/forum-mention
[friendica.git] / mod / uexport.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Addon;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
7
8 function uexport_init(App $a) {
9         if (!local_user()) {
10                 killme();
11         }
12
13         require_once("mod/settings.php");
14         settings_init($a);
15 }
16
17 function uexport_content(App $a) {
18
19         if ($a->argc > 1) {
20                 header("Content-type: application/json");
21                 header('Content-Disposition: attachment; filename="' . $a->user['nickname'] . '.' . $a->argv[1] . '"');
22                 switch ($a->argv[1]) {
23                         case "backup":
24                                 uexport_all($a);
25                                 killme();
26                                 break;
27                         case "account":
28                                 uexport_account($a);
29                                 killme();
30                                 break;
31                         default:
32                                 killme();
33                 }
34         }
35
36         /**
37          * options shown on "Export personal data" page
38          * list of array( 'link url', 'link text', 'help text' )
39          */
40         $options = [
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)')],
43         ];
44         Addon::callHooks('uexport_options', $options);
45
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
51         ]);
52 }
53
54 function _uexport_multirow($query) {
55         $result = [];
56         $r = q($query);
57         if (DBM::is_result($r)) {
58                 foreach ($r as $rr) {
59                         $p = [];
60                         foreach ($rr as $k => $v) {
61                                 $p[$k] = $v;
62                         }
63                         $result[] = $p;
64                 }
65         }
66         return $result;
67 }
68
69 function _uexport_row($query) {
70         $result = [];
71         $r = q($query);
72         if (DBM::is_result($r)) {
73                 foreach ($r as $rr) {
74                         foreach ($rr as $k => $v) {
75                                 $result[$k] = $v;
76                         }
77                 }
78         }
79         return $result;
80 }
81
82 function uexport_account($a) {
83
84         $user = _uexport_row(
85                 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
86         );
87
88         $contact = _uexport_multirow(
89                 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
90         );
91
92
93         $profile = _uexport_multirow(
94                 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
95         );
96
97         $photo = _uexport_multirow(
98                 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
99         );
100         foreach ($photo as &$p) {
101                 $p['data'] = bin2hex($p['data']);
102         }
103
104         $pconfig = _uexport_multirow(
105                 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
106         );
107
108         $group = _uexport_multirow(
109                 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
110         );
111
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()))
114         );
115
116         $output = [
117                 'version' => FRIENDICA_VERSION,
118                 'schema' => DB_UPDATE_VERSION,
119                 'baseurl' => System::baseUrl(),
120                 'user' => $user,
121                 'contact' => $contact,
122                 'profile' => $profile,
123                 'photo' => $photo,
124                 'pconfig' => $pconfig,
125                 'group' => $group,
126                 'group_member' => $group_member,
127         ];
128
129         //echo "<pre>"; var_dump(json_encode($output)); killme();
130         echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
131 }
132
133 /**
134  * echoes account data and items as separated json, one per line
135  */
136 function uexport_all(App $a) {
137
138         uexport_account($a);
139         echo "\n";
140
141         $r = q("SELECT count(*) as `total` FROM `item` WHERE `uid` = %d ",
142                 intval(local_user())
143         );
144         if (DBM::is_result($r)) {
145                 $total = $r[0]['total'];
146         }
147         // chunk the output to avoid exhausting memory
148
149         for ($x = 0; $x < $total; $x += 500) {
150                 $item = [];
151                 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
152                         intval(local_user()),
153                         intval($x),
154                         intval(500)
155                 );
156
157                 $output = ['item' => $r];
158                 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";
159         }
160 }