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