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