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.
 
 
 
 
 
 

158 lines
3.9 KiB

  1. <?php
  2. function uexport_init(App &$a){
  3. if (! local_user()) {
  4. killme();
  5. }
  6. require_once("mod/settings.php");
  7. settings_init($a);
  8. }
  9. /// @TODO Change space -> tab where wanted
  10. function uexport_content(App &$a){
  11. if ($a->argc > 1) {
  12. header("Content-type: application/json");
  13. header('Content-Disposition: attachment; filename="'.$a->user['nickname'].'.'.$a->argv[1].'"');
  14. switch($a->argv[1]) {
  15. case "backup": uexport_all($a); killme(); break;
  16. case "account": uexport_account($a); killme(); break;
  17. default:
  18. killme();
  19. }
  20. }
  21. /**
  22. * options shown on "Export personal data" page
  23. * list of array( 'link url', 'link text', 'help text' )
  24. */
  25. $options = array(
  26. 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.')),
  27. 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)')),
  28. );
  29. call_hooks('uexport_options', $options);
  30. $tpl = get_markup_template("uexport.tpl");
  31. return replace_macros($tpl, array(
  32. '$baseurl' => App::get_baseurl(),
  33. '$title' => t('Export personal data'),
  34. '$options' => $options
  35. ));
  36. }
  37. function _uexport_multirow($query) {
  38. $result = array();
  39. $r = q($query);
  40. // if (dbm::is_result($r)) {
  41. if ($r){
  42. foreach($r as $rr){
  43. $p = array();
  44. foreach($rr as $k => $v)
  45. $p[$k] = $v;
  46. $result[] = $p;
  47. }
  48. }
  49. return $result;
  50. }
  51. function _uexport_row($query) {
  52. $result = array();
  53. $r = q($query);
  54. if ($r) {
  55. foreach($r as $rr)
  56. foreach($rr as $k => $v)
  57. $result[$k] = $v;
  58. }
  59. return $result;
  60. }
  61. function uexport_account($a){
  62. $user = _uexport_row(
  63. sprintf( "SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()) )
  64. );
  65. $contact = _uexport_multirow(
  66. sprintf( "SELECT * FROM `contact` WHERE `uid` = %d ",intval(local_user()) )
  67. );
  68. $profile =_uexport_multirow(
  69. sprintf( "SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()) )
  70. );
  71. $photo = _uexport_multirow(
  72. sprintf( "SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()) )
  73. );
  74. foreach ($photo as &$p) $p['data'] = bin2hex($p['data']);
  75. $pconfig = _uexport_multirow(
  76. sprintf( "SELECT * FROM `pconfig` WHERE uid = %d",intval(local_user()) )
  77. );
  78. $group = _uexport_multirow(
  79. sprintf( "SELECT * FROM `group` WHERE uid = %d",intval(local_user()) )
  80. );
  81. $group_member = _uexport_multirow(
  82. sprintf( "SELECT * FROM `group_member` WHERE uid = %d",intval(local_user()) )
  83. );
  84. $output = array(
  85. 'version' => FRIENDICA_VERSION,
  86. 'schema' => DB_UPDATE_VERSION,
  87. 'baseurl' => App::get_baseurl(),
  88. 'user' => $user,
  89. 'contact' => $contact,
  90. 'profile' => $profile,
  91. 'photo' => $photo,
  92. 'pconfig' => $pconfig,
  93. 'group' => $group,
  94. 'group_member' => $group_member,
  95. );
  96. //echo "<pre>"; var_dump(json_encode($output)); killme();
  97. echo json_encode($output);
  98. }
  99. /**
  100. * echoes account data and items as separated json, one per line
  101. */
  102. function uexport_all(App &$a) {
  103. uexport_account($a);
  104. echo "\n";
  105. $r = q("SELECT count(*) as `total` FROM `item` WHERE `uid` = %d ",
  106. intval(local_user())
  107. );
  108. if (dbm::is_result($r))
  109. $total = $r[0]['total'];
  110. // chunk the output to avoid exhausting memory
  111. for($x = 0; $x < $total; $x += 500) {
  112. $item = array();
  113. $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
  114. intval(local_user()),
  115. intval($x),
  116. intval(500)
  117. );
  118. /*if (dbm::is_result($r)) {
  119. foreach($r as $rr)
  120. foreach($rr as $k => $v)
  121. $item[][$k] = $v;
  122. }*/
  123. $output = array('item' => $r);
  124. echo json_encode($output)."\n";
  125. }
  126. }