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