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