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