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