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