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