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