]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/UserExport.php
9c4e4913cfdfdedbbcb4cb3418733ca64eb893ae
[friendica.git] / src / Module / Settings / UserExport.php
1 <?php
2 /**
3  * @file src/Modules/Settings/UserExport.php
4  */
5
6 namespace Friendica\Module\Settings;
7
8 use Friendica\App;
9 use Friendica\App\Arguments;
10 use Friendica\BaseModule;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\Database\DBStructure;
17 use Friendica\Module\BaseSettingsModule;
18
19 /**
20  * Module to export user data
21  **/
22 class UserExport extends BaseSettingsModule
23 {
24         /**
25          * Handle the request to export data.
26          * At the moment one can export three different data set
27          * 1. The profile data that can be used by uimport to resettle
28          *    to a different Friendica instance
29          * 2. The entire data-set, profile plus postings
30          * 3. A list of contacts as CSV file similar to the export of Mastodon
31          *
32          * If there is an action required through the URL / path, react
33          * accordingly and export the requested data.
34          **/
35         public static function content()
36         {
37                 parent::content();
38
39                 /**
40                  * options shown on "Export personal data" page
41                  * list of array( 'link url', 'link text', 'help text' )
42                  */
43                 $options = [
44                         ['settings/userexport/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.')],
45                         ['settings/userexport/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")],
46                         ['settings/userexport/contact', L10n::t('Export Contacts to CSV'), L10n::t("Export the list of the accounts you are following as CSV file. Compatible to e.g. Mastodon.")],
47                 ];
48                 Hook::callAll('uexport_options', $options);
49
50                 $tpl = Renderer::getMarkupTemplate("settings/userexport.tpl");
51                 return Renderer::replaceMacros($tpl, [
52                         '$title' => L10n::t('Export personal data'),
53                         '$options' => $options
54                 ]);
55         }
56         /**
57          * raw content generated for the different choices made
58          * by the user. At the moment this returns a JSON file
59          * to the browser which then offers a save / open dialog
60          * to the user.
61          **/
62         public static function rawContent($parameters)
63         {
64                 $args = self::getClass(Arguments::class);
65                 if ($args->getArgc() == 3) {
66                         // @TODO Replace with router-provided arguments
67                         $action = $args->get(2);
68                         $user = self::getApp()->user;
69                         switch ($action) {
70                                 case "backup":
71                                         header("Content-type: application/json");
72                                         header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
73                                         self::exportAll(self::getApp());
74                                         exit();
75                                         break;
76                                 case "account":
77                                         header("Content-type: application/json");
78                                         header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
79                                         self::exportAccount(self::getApp());
80                                         exit();
81                                         break;
82                                 case "contact":
83                                         header("Content-type: application/csv");
84                                         header('Content-Disposition: attachment; filename="' . $user['nickname'] . '-contacts.csv'. '"');
85                                         self::exportContactsAsCSV();
86                                         exit();
87                                         break;
88                                 default:
89                                         exit();
90                         }
91                 }
92         }
93         private static function exportMultiRow(string $query)
94         {
95                 $dbStructure = DBStructure::definition(self::getApp()->getBasePath(), false);
96
97                 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
98                 $table = $match[1];
99
100                 $result = [];
101                 $rows = DBA::p($query);
102                 while ($row = DBA::fetch($rows)) {
103                         $p = [];
104                         foreach ($row as $k => $v) {
105                                 switch ($dbStructure[$table]['fields'][$k]['type']) {
106                                         case 'datetime':
107                                                 $p[$k] = $v ?? DBA::NULL_DATETIME;
108                                                 break;
109                                         default:
110                                                 $p[$k] = $v;
111                                                 break;
112                                 }
113                         }
114                         $result[] = $p;
115                 }
116                 DBA::close($rows);
117                 return $result;
118         }
119
120         private static function exportRow(string $query)
121         {
122                 $dbStructure = DBStructure::definition(self::getApp()->getBasePath(), false);
123
124                 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
125                 $table = $match[1];
126
127                 $result = [];
128                 $r = q($query);
129                 if (DBA::isResult($r)) {
130
131                         foreach ($r as $rr) {
132                                 foreach ($rr as $k => $v) {
133                                         switch ($dbStructure[$table]['fields'][$k]['type']) {
134                                                 case 'datetime':
135                                                         $result[$k] = $v ?? DBA::NULL_DATETIME;
136                                                         break;
137                                                 default:
138                                                         $result[$k] = $v;
139                                                         break;
140                                         }
141                                 }
142                         }
143                 }
144                 return $result;
145         }
146
147         /**
148          * Export a list of the contacts as CSV file as e.g. Mastodon and Pleroma are doing.
149          **/
150         private static function exportContactsAsCSV()
151         {
152                 // write the table header (like Mastodon)
153                 echo "Account address, Show boosts\n";
154                 // get all the contacts
155                 $contacts = DBA::select('contact', ['addr'], ['uid' => $_SESSION['uid'], 'self' => false, 'rel' => [1,3], 'deleted' => false]);
156                 while ($contact = DBA::fetch($contacts)) {
157                         echo $contact['addr'] . ", true\n";
158                 }
159                 DBA::close($contacts);
160         }
161         private static function exportAccount(App $a)
162         {
163                 $user = self::exportRow(
164                         sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
165                 );
166
167                 $contact = self::exportMultiRow(
168                         sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
169                 );
170
171
172                 $profile = self::exportMultiRow(
173                         sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
174                 );
175
176                 $photo = self::exportMultiRow(
177                         sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
178                 );
179                 foreach ($photo as &$p) {
180                         $p['data'] = bin2hex($p['data']);
181                 }
182
183                 $pconfig = self::exportMultiRow(
184                         sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
185                 );
186
187                 $group = self::exportMultiRow(
188                         sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
189                 );
190
191                 $group_member = self::exportMultiRow(
192                         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()))
193                 );
194
195                 $output = [
196                         'version' => FRIENDICA_VERSION,
197                         'schema' => DB_UPDATE_VERSION,
198                         'baseurl' => System::baseUrl(),
199                         'user' => $user,
200                         'contact' => $contact,
201                         'profile' => $profile,
202                         'photo' => $photo,
203                         'pconfig' => $pconfig,
204                         'group' => $group,
205                         'group_member' => $group_member,
206                 ];
207
208                 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
209         }
210
211         /**
212          * echoes account data and items as separated json, one per line
213          *
214          * @param App $a
215          * @throws Exception
216          */
217         private static function exportAll(App $a)
218         {
219                 self::exportAccount($a);
220                 echo "\n";
221
222                 $total = DBA::count('item', ['uid' => local_user()]);
223                 // chunk the output to avoid exhausting memory
224
225                 for ($x = 0; $x < $total; $x += 500) {
226                         $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
227                                 intval(local_user()),
228                                 intval($x),
229                                 intval(500)
230                         );
231
232                         $output = ['item' => $r];
233                         echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";
234                 }
235         }
236 }