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