Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.9 KiB

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