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.

234 lines
7.7 KiB

  1. <?php
  2. /**
  3. * import account file exported from mod/uexport
  4. * args:
  5. * $a App Friendica App Class
  6. * $file Array array from $_FILES
  7. */
  8. require_once("include/Photo.php");
  9. define("IMPORT_DEBUG", False);
  10. function last_insert_id(){
  11. global $db;
  12. if (IMPORT_DEBUG) return 1;
  13. if($db->mysqli){
  14. $thedb = $db->getdb();
  15. return $thedb->insert_id;
  16. } else {
  17. return mysql_insert_id();
  18. }
  19. }
  20. function last_error(){
  21. global $db;
  22. return $db->error;
  23. }
  24. function db_import_assoc($table, $arr){
  25. if (IMPORT_DEBUG) return true;
  26. if (isset($arr['id'])) unset($arr['id']);
  27. $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
  28. $vals = implode("','", array_map('dbesc', array_values($arr)));
  29. $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
  30. logger("uimport: $query",LOGGER_TRACE);
  31. return q($query);
  32. }
  33. function import_cleanup($newuid) {
  34. q("DELETE FROM `user` WHERE uid = %d", $newuid);
  35. q("DELETE FROM `contact` WHERE uid = %d", $newuid);
  36. q("DELETE FROM `profile` WHERE uid = %d", $newuid);
  37. q("DELETE FROM `photo` WHERE uid = %d", $newuid);
  38. q("DELETE FROM `group` WHERE uid = %d", $newuid);
  39. q("DELETE FROM `group_member` WHERE uid = %d", $newuid);
  40. q("DELETE FROM `pconfig` WHERE uid = %d", $newuid);
  41. }
  42. function import_account(&$a, $file) {
  43. logger("Start user import from ".$file['tmp_name']);
  44. /*
  45. STEPS
  46. 1. checks
  47. 2. replace old baseurl with new baseurl
  48. 3. import data (look at user id and contacts id)
  49. 4. archive non-dfrn contacts
  50. 5. send message to dfrn contacts
  51. */
  52. $account = json_decode(file_get_contents($file['tmp_name']), true);
  53. if ($account===null) {
  54. notice(t("Error decoding account file"));
  55. return;
  56. }
  57. if (!x($account, 'version')) {
  58. notice(t("Error! No version data in file! This is not a Friendica account file?"));
  59. return;
  60. }
  61. if ($account['schema'] != DB_UPDATE_VERSION) {
  62. notice(t("Error! I can't import this file: DB schema version is not compatible."));
  63. return;
  64. }
  65. $oldbaseurl = $account['baseurl'];
  66. $newbaseurl = $a->get_baseurl();
  67. $olduid = $account['user']['uid'];
  68. unset($account['user']['uid']);
  69. foreach($account['user'] as $k => &$v) {
  70. $v = str_replace($oldbaseurl, $newbaseurl, $v);
  71. }
  72. // import user
  73. $r = db_import_assoc('user', $account['user']);
  74. if ($r===false) {
  75. //echo "<pre>"; var_dump($r, $query, mysql_error()); killme();
  76. logger("uimport:insert user : ERROR : ".last_error(), LOGGER_NORMAL);
  77. notice(t("User creation error"));
  78. return;
  79. }
  80. $newuid = last_insert_id();
  81. //~ $newuid = 1;
  82. foreach($account['profile'] as &$profile) {
  83. foreach($profile as $k=>&$v) {
  84. $v = str_replace($oldbaseurl, $newbaseurl, $v);
  85. foreach(array("profile","avatar") as $k)
  86. $v = str_replace($newbaseurl."/photo/".$k."/".$olduid.".jpg", $newbaseurl."/photo/".$k."/".$newuid.".jpg", $v);
  87. }
  88. $profile['uid'] = $newuid;
  89. $r = db_import_assoc('profile', $profile);
  90. if ($r===false) {
  91. logger("uimport:insert profile ".$profile['profile-name']." : ERROR : ".last_error(), LOGGER_NORMAL);
  92. info(t("User profile creation error"));
  93. import_cleanup($newuid);
  94. return;
  95. }
  96. }
  97. $errorcount=0;
  98. foreach($account['contact'] as &$contact) {
  99. if ($contact['uid'] == $olduid && $contact['self'] == '1'){
  100. foreach($contact as $k=>&$v) {
  101. $v = str_replace($oldbaseurl, $newbaseurl, $v);
  102. foreach(array("profile","avatar","micro") as $k)
  103. $v = str_replace($newbaseurl."/photo/".$k."/".$olduid.".jpg", $newbaseurl."/photo/".$k."/".$newuid.".jpg", $v);
  104. }
  105. }
  106. if ($contact['uid'] == $olduid && $contact['self'] == '0') {
  107. switch ($contact['network']){
  108. case NETWORK_DFRN:
  109. // send moved message
  110. break;
  111. case NETWORK_ZOT:
  112. // TODO handle zot network
  113. break;
  114. case NETWORK_MAIL2:
  115. // TODO ?
  116. break;
  117. case NETWORK_FEED:
  118. case NETWORK_MAIL:
  119. // Nothing to do
  120. break;
  121. default:
  122. // archive other contacts
  123. $contact['archive'] = "1";
  124. }
  125. }
  126. $contact['uid'] = $newuid;
  127. $r = db_import_assoc('contact', $contact);
  128. if ($r===false) {
  129. logger("uimport:insert contact ".$contact['nick'].",".$contact['network']." : ERROR : ".last_error(), LOGGER_NORMAL);
  130. $errorcount++;
  131. } else {
  132. $contact['newid'] = last_insert_id();
  133. }
  134. }
  135. if ($errorcount>0) {
  136. notice( sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount) );
  137. }
  138. foreach($account['group'] as &$group) {
  139. $group['uid'] = $newuid;
  140. $r = db_import_assoc('group', $group);
  141. if ($r===false) {
  142. logger("uimport:insert group ".$group['name']." : ERROR : ".last_error(), LOGGER_NORMAL);
  143. } else {
  144. $group['newid'] = last_insert_id();
  145. }
  146. }
  147. foreach($account['group_member'] as &$group_member) {
  148. $group_member['uid'] = $newuid;
  149. $import = 0;
  150. foreach($account['group'] as $group) {
  151. if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
  152. $group_member['gid'] = $group['newid'];
  153. $import++;
  154. break;
  155. }
  156. }
  157. foreach($account['contact'] as $contact) {
  158. if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
  159. $group_member['contact-id'] = $contact['newid'];
  160. $import++;
  161. break;
  162. }
  163. }
  164. if ($import==2) {
  165. $r = db_import_assoc('group_member', $group_member);
  166. if ($r===false) {
  167. logger("uimport:insert group member ".$group_member['id']." : ERROR : ".last_error(), LOGGER_NORMAL);
  168. }
  169. }
  170. }
  171. foreach($account['photo'] as &$photo) {
  172. $photo['uid'] = $newuid;
  173. $photo['data'] = hex2bin($photo['data']);
  174. $p = new Photo($photo['data'], $photo['type']);
  175. $r = $p->store(
  176. $photo['uid'],
  177. $photo['contact-id'], //0
  178. $photo['resource-id'],
  179. $photo['filename'],
  180. $photo['album'],
  181. $photo['scale'],
  182. $photo['profile'], //1
  183. $photo['allow_cid'],
  184. $photo['allow_gid'],
  185. $photo['deny_cid'],
  186. $photo['deny_gid']
  187. );
  188. if ($r===false) {
  189. logger("uimport:insert photo ".$photo['resource-id'].",". $photo['scale']. " : ERROR : ".last_error(), LOGGER_NORMAL);
  190. }
  191. }
  192. foreach($account['pconfig'] as &$pconfig) {
  193. $pconfig['uid'] = $newuid;
  194. $r = db_import_assoc('pconfig', $pconfig);
  195. if ($r===false) {
  196. logger("uimport:insert pconfig ".$pconfig['id']. " : ERROR : ".last_error(), LOGGER_NORMAL);
  197. }
  198. }
  199. info(t("Done. You can now login with your username and password"));
  200. goaway( $a->get_baseurl() ."/login");
  201. }