]> git.mxchange.org Git - friendica.git/blob - mod/uexport.php
Merge https://github.com/friendica/friendica into pull
[friendica.git] / mod / uexport.php
1 <?php
2
3 function uexport_init(&$a){
4         if(! local_user())
5                 killme();
6         
7         $tabs = array(
8                 array(
9                         'label' => t('Account settings'),
10                         'url'   => $a->get_baseurl(true).'/settings',
11                         'selected'      => '',
12                 ),      
13                 array(
14                         'label' => t('Display settings'),
15                         'url'   => $a->get_baseurl(true).'/settings/display',
16                         'selected'      =>'',
17                 ),      
18                 
19                 array(
20                         'label' => t('Connector settings'),
21                         'url'   => $a->get_baseurl(true).'/settings/connectors',
22                         'selected'      => '',
23                 ),
24                 array(
25                         'label' => t('Plugin settings'),
26                         'url'   => $a->get_baseurl(true).'/settings/addon',
27                         'selected'      => '',
28                 ),
29                 array(
30                         'label' => t('Connected apps'),
31                         'url' => $a->get_baseurl(true) . '/settings/oauth',
32                         'selected' => '',
33                 ),
34                 array(
35                         'label' => t('Export personal data'),
36                         'url' => $a->get_baseurl(true) . '/uexport',
37                         'selected' => 'active'
38                 ),
39                 array(
40                         'label' => t('Remove account'),
41                         'url' => $a->get_baseurl(true) . '/removeme',
42                         'selected' => ''
43                 )
44         );
45         
46         $tabtpl = get_markup_template("generic_links_widget.tpl");
47         $a->page['aside'] = replace_macros($tabtpl, array(
48                 '$title' => t('Settings'),
49                 '$class' => 'settings-widget',
50                 '$items' => $tabs,
51         ));
52 }
53
54 function uexport_content(&$a){
55     
56     if ($a->argc > 1) {
57         header("Content-type: application/json");
58         header('Content-Disposition: attachment; filename="'.$a->user['nickname'].'.'.$a->argv[1].'"');
59         switch($a->argv[1]) {
60             case "backup": uexport_all($a); killme(); break;
61             case "account": uexport_account($a); killme(); break;
62             default:
63                 killme();
64         }
65     }
66
67     /**
68       * options shown on "Export personal data" page
69       * list of array( 'link url', 'link text', 'help text' )
70       */
71     $options = array(
72             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.')),
73             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)')),
74     );
75     call_hooks('uexport_options', $options);
76         
77     $tpl = get_markup_template("uexport.tpl");
78     return replace_macros($tpl, array(
79         '$baseurl' => $a->get_baseurl(),
80         '$title' => t('Export personal data'),
81         '$options' => $options
82     ));
83     
84     
85 }
86
87 function _uexport_multirow($query) {
88         $result = array();
89         $r = q($query);
90 //      if(count($r)) {
91         if ($r){
92                 foreach($r as $rr){
93             $p = array();
94                         foreach($rr as $k => $v)
95                                 $p[$k] = $v;
96             $result[] = $p;
97         }
98         }
99     return $result;
100 }
101
102 function _uexport_row($query) {
103         $result = array();
104         $r = q($query);
105         if ($r) {
106                 foreach($r as $rr)
107                         foreach($rr as $k => $v)
108                                 $result[$k] = $v;
109
110         }
111     return $result;
112 }
113
114
115 function uexport_account($a){
116
117         $user = _uexport_row(
118         sprintf( "SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()) )
119         );
120     
121         $contact = _uexport_multirow(
122         sprintf( "SELECT * FROM `contact` WHERE `uid` = %d ",intval(local_user()) )
123         );
124
125
126         $profile =_uexport_multirow(
127         sprintf( "SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()) )
128         );
129
130     $photo = _uexport_multirow(
131         sprintf( "SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()) )
132     );
133     foreach ($photo as &$p) $p['data'] = bin2hex($p['data']);
134
135     $pconfig = _uexport_multirow(
136         sprintf( "SELECT * FROM `pconfig` WHERE uid = %d",intval(local_user()) )
137     );
138
139     $group = _uexport_multirow(
140         sprintf( "SELECT * FROM `group` WHERE uid = %d",intval(local_user()) )
141     );
142     
143     $group_member = _uexport_multirow(
144         sprintf( "SELECT * FROM `group_member` WHERE uid = %d",intval(local_user()) )
145     );
146
147         $output = array(
148         'version' => FRIENDICA_VERSION,
149         'schema' => DB_UPDATE_VERSION,
150         'baseurl' => $a->get_baseurl(),
151         'user' => $user, 
152         'contact' => $contact, 
153         'profile' => $profile, 
154         'photo' => $photo,
155         'pconfig' => $pconfig,
156         'group' => $group,
157         'group_member' => $group_member,
158     );
159
160     //echo "<pre>"; var_dump(json_encode($output)); killme();
161         echo json_encode($output);
162
163 }
164
165 /**
166  * echoes account data and items as separated json, one per line
167  */
168 function uexport_all(&$a) {
169     
170     uexport_account($a);
171
172         $r = q("SELECT count(*) as `total` FROM `item` WHERE `uid` = %d ",
173                 intval(local_user())
174         );
175         if(count($r))
176                 $total = $r[0]['total'];
177
178         // chunk the output to avoid exhausting memory
179
180         for($x = 0; $x < $total; $x += 500) {
181                 $item = array();
182                 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
183                         intval(local_user()),
184                         intval($x),
185                         intval(500)
186                 );
187                 if(count($r)) {
188                         foreach($r as $rr)
189                                 foreach($rr as $k => $v)
190                                         $item[][$k] = $v;
191                 }
192
193                 $output = array('item' => $item);
194                 echo json_encode($output);
195         }
196
197 }