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