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