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