Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

162 строки
3.7KB

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