]> git.mxchange.org Git - friendica.git/blob - mod/uexport.php
friendica-6950 uimport doesn't support `"pwdreset_time":null`
[friendica.git] / mod / uexport.php
1 <?php
2 /**
3  * @file mod/uexport.php
4  */
5
6 use Friendica\App;
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;
13
14 function uexport_init(App $a) {
15         global $dbStructure;
16
17         if (!local_user()) {
18                 exit();
19         }
20
21         require_once("mod/settings.php");
22         settings_init($a);
23
24         $dbStructure = DBStructure::definition($a->getBasePath());
25 }
26
27 function uexport_content(App $a) {
28
29         if ($a->argc > 1) {
30                 header("Content-type: application/json");
31                 header('Content-Disposition: attachment; filename="' . $a->user['nickname'] . '.' . $a->argv[1] . '"');
32                 switch ($a->argv[1]) {
33                         case "backup":
34                                 uexport_all($a);
35                                 exit();
36                                 break;
37                         case "account":
38                                 uexport_account($a);
39                                 exit();
40                                 break;
41                         default:
42                                 exit();
43                 }
44         }
45
46         /**
47          * options shown on "Export personal data" page
48          * list of array( 'link url', 'link text', 'help text' )
49          */
50         $options = [
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")],
53         ];
54         Hook::callAll('uexport_options', $options);
55
56         $tpl = Renderer::getMarkupTemplate("uexport.tpl");
57         return Renderer::replaceMacros($tpl, [
58                 '$title' => L10n::t('Export personal data'),
59                 '$options' => $options
60         ]);
61 }
62
63 function _uexport_multirow($query) {
64         global $dbStructure;
65
66         preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
67         $table = $match[1];
68
69         $result = [];
70         $r = q($query);
71         if (DBA::isResult($r)) {
72                 foreach ($r as $rr) {
73                         $p = [];
74                         foreach ($rr as $k => $v) {
75                                 switch ($dbStructure[$table]['fields'][$k]['type']) {
76                                         case 'datetime':
77                                                 $p[$k] = !empty($v) ? $v : DBA::NULL_DATETIME;
78                                                 break;
79                                         default:
80                                                 $p[$k] = $v;
81                                                 break;
82                                 }
83                         }
84                         $result[] = $p;
85                 }
86         }
87         return $result;
88 }
89
90 function _uexport_row($query) {
91         global $dbStructure;
92
93         preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
94         $table = $match[1];
95
96         $result = [];
97         $r = q($query);
98         if (DBA::isResult($r)) {
99
100                 foreach ($r as $rr) {
101                         foreach ($rr as $k => $v) {
102                                 switch ($dbStructure[$table]['fields'][$k]['type']) {
103                                         case 'datetime':
104                                                 $result[$k] = !empty($v) ? $v : DBA::NULL_DATETIME;
105                                                 break;
106                                         default:
107                                                 $result[$k] = $v;
108                                                 break;
109                                 }
110                         }
111                 }
112         }
113         return $result;
114 }
115
116 function uexport_account($a) {
117
118         $user = _uexport_row(
119                 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
120         );
121
122         $contact = _uexport_multirow(
123                 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
124         );
125
126
127         $profile = _uexport_multirow(
128                 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
129         );
130
131         $photo = _uexport_multirow(
132                 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
133         );
134         foreach ($photo as &$p) {
135                 $p['data'] = bin2hex($p['data']);
136         }
137
138         $pconfig = _uexport_multirow(
139                 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
140         );
141
142         $group = _uexport_multirow(
143                 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
144         );
145
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()))
148         );
149
150         $output = [
151                 'version' => FRIENDICA_VERSION,
152                 'schema' => DB_UPDATE_VERSION,
153                 'baseurl' => System::baseUrl(),
154                 'user' => $user,
155                 'contact' => $contact,
156                 'profile' => $profile,
157                 'photo' => $photo,
158                 'pconfig' => $pconfig,
159                 'group' => $group,
160                 'group_member' => $group_member,
161         ];
162
163         echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
164 }
165
166 /**
167  * echoes account data and items as separated json, one per line
168  *
169  * @param App $a
170  * @throws Exception
171  */
172 function uexport_all(App $a) {
173
174         uexport_account($a);
175         echo "\n";
176
177         $total = 0;
178         $r = q("SELECT count(*) as `total` FROM `item` WHERE `uid` = %d ",
179                 intval(local_user())
180         );
181         if (DBA::isResult($r)) {
182                 $total = $r[0]['total'];
183         }
184         // chunk the output to avoid exhausting memory
185
186         for ($x = 0; $x < $total; $x += 500) {
187                 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
188                         intval(local_user()),
189                         intval($x),
190                         intval(500)
191                 );
192
193                 $output = ['item' => $r];
194                 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";
195         }
196 }