Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

282 行
8.3KB

  1. <?php
  2. use Friendica\App;
  3. use Friendica\Core\System;
  4. require_once("include/Photo.php");
  5. define("IMPORT_DEBUG", False);
  6. function last_insert_id() {
  7. if (IMPORT_DEBUG) {
  8. return 1;
  9. }
  10. return dba::lastInsertId();
  11. }
  12. /**
  13. * Remove columns from array $arr that aren't in table $table
  14. *
  15. * @param string $table Table name
  16. * @param array &$arr Column=>Value array from json (by ref)
  17. */
  18. function check_cols($table, &$arr) {
  19. $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
  20. logger("uimport: $query", LOGGER_DEBUG);
  21. $r = q($query);
  22. $tcols = array();
  23. // get a plain array of column names
  24. foreach ($r as $tcol) {
  25. $tcols[] = $tcol['Field'];
  26. }
  27. // remove inexistent columns
  28. foreach ($arr as $icol => $ival) {
  29. if (!in_array($icol, $tcols)) {
  30. unset($arr[$icol]);
  31. }
  32. }
  33. }
  34. /**
  35. * Import data into table $table
  36. *
  37. * @param string $table Table name
  38. * @param array $arr Column=>Value array from json
  39. */
  40. function db_import_assoc($table, $arr) {
  41. if (isset($arr['id']))
  42. unset($arr['id']);
  43. check_cols($table, $arr);
  44. $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
  45. $vals = implode("','", array_map('dbesc', array_values($arr)));
  46. $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
  47. logger("uimport: $query", LOGGER_TRACE);
  48. if (IMPORT_DEBUG) {
  49. return true;
  50. }
  51. return q($query);
  52. }
  53. /**
  54. * @brief Import account file exported from mod/uexport
  55. *
  56. * @param App $a Friendica App Class
  57. * @param array $file array from $_FILES
  58. */
  59. function import_account(App $a, $file) {
  60. logger("Start user import from " . $file['tmp_name']);
  61. /*
  62. STEPS
  63. 1. checks
  64. 2. replace old baseurl with new baseurl
  65. 3. import data (look at user id and contacts id)
  66. 4. archive non-dfrn contacts
  67. 5. send message to dfrn contacts
  68. */
  69. $account = json_decode(file_get_contents($file['tmp_name']), true);
  70. if ($account === null) {
  71. notice(t("Error decoding account file"));
  72. return;
  73. }
  74. if (!x($account, 'version')) {
  75. notice(t("Error! No version data in file! This is not a Friendica account file?"));
  76. return;
  77. }
  78. /*
  79. * @TODO Old-lost code?
  80. // this is not required as we remove columns in json not in current db schema
  81. if ($account['schema'] != DB_UPDATE_VERSION) {
  82. notice(t("Error! I can't import this file: DB schema version is not compatible."));
  83. return;
  84. }
  85. */
  86. // check for username
  87. $r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
  88. if ($r === false) {
  89. logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  90. notice(t('Error! Cannot check nickname'));
  91. return;
  92. }
  93. if (dbm::is_result($r) > 0) {
  94. notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
  95. return;
  96. }
  97. // check if username matches deleted account
  98. $r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
  99. if ($r === false) {
  100. logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  101. notice(t('Error! Cannot check nickname'));
  102. return;
  103. }
  104. if (dbm::is_result($r) > 0) {
  105. notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
  106. return;
  107. }
  108. $oldbaseurl = $account['baseurl'];
  109. $newbaseurl = System::baseUrl();
  110. $oldaddr = str_replace('http://', '@', normalise_link($oldbaseurl));
  111. $newaddr = str_replace('http://', '@', normalise_link($newbaseurl));
  112. $olduid = $account['user']['uid'];
  113. unset($account['user']['uid']);
  114. unset($account['user']['account_expired']);
  115. unset($account['user']['account_expires_on']);
  116. unset($account['user']['expire_notification_sent']);
  117. foreach ($account['user'] as $k => &$v) {
  118. $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
  119. }
  120. // import user
  121. $r = db_import_assoc('user', $account['user']);
  122. if ($r === false) {
  123. //echo "<pre>"; var_dump($r, $query, mysql_error()); killme();
  124. logger("uimport:insert user : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  125. notice(t("User creation error"));
  126. return;
  127. }
  128. $newuid = last_insert_id();
  129. //~ $newuid = 1;
  130. // Generate a new guid for the account. Otherwise there will be problems with diaspora
  131. q("UPDATE `user` SET `guid` = '%s' WHERE `uid` = %d",
  132. dbesc(generate_user_guid()), intval($newuid));
  133. foreach ($account['profile'] as &$profile) {
  134. foreach ($profile as $k => &$v) {
  135. $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
  136. foreach (array("profile", "avatar") as $k) {
  137. $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
  138. }
  139. }
  140. $profile['uid'] = $newuid;
  141. $r = db_import_assoc('profile', $profile);
  142. if ($r === false) {
  143. logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  144. info(t("User profile creation error"));
  145. dba::delete('user', array('uid' => $newuid));
  146. return;
  147. }
  148. }
  149. $errorcount = 0;
  150. foreach ($account['contact'] as &$contact) {
  151. if ($contact['uid'] == $olduid && $contact['self'] == '1') {
  152. foreach ($contact as $k => &$v) {
  153. $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
  154. foreach (array("profile", "avatar", "micro") as $k) {
  155. $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
  156. }
  157. }
  158. }
  159. if ($contact['uid'] == $olduid && $contact['self'] == '0') {
  160. // set contacts 'avatar-date' to NULL_DATE to let poller to update urls
  161. $contact["avatar-date"] = NULL_DATE;
  162. switch ($contact['network']) {
  163. case NETWORK_DFRN:
  164. // send relocate message (below)
  165. break;
  166. case NETWORK_ZOT:
  167. /// @TODO handle zot network
  168. break;
  169. case NETWORK_MAIL2:
  170. /// @TODO ?
  171. break;
  172. case NETWORK_FEED:
  173. case NETWORK_MAIL:
  174. // Nothing to do
  175. break;
  176. default:
  177. // archive other contacts
  178. $contact['archive'] = "1";
  179. }
  180. }
  181. $contact['uid'] = $newuid;
  182. $r = db_import_assoc('contact', $contact);
  183. if ($r === false) {
  184. logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  185. $errorcount++;
  186. } else {
  187. $contact['newid'] = last_insert_id();
  188. }
  189. }
  190. if ($errorcount > 0) {
  191. notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
  192. }
  193. foreach ($account['group'] as &$group) {
  194. $group['uid'] = $newuid;
  195. $r = db_import_assoc('group', $group);
  196. if ($r === false) {
  197. logger("uimport:insert group " . $group['name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  198. } else {
  199. $group['newid'] = last_insert_id();
  200. }
  201. }
  202. foreach ($account['group_member'] as &$group_member) {
  203. $group_member['uid'] = $newuid;
  204. $import = 0;
  205. foreach ($account['group'] as $group) {
  206. if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
  207. $group_member['gid'] = $group['newid'];
  208. $import++;
  209. break;
  210. }
  211. }
  212. foreach ($account['contact'] as $contact) {
  213. if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
  214. $group_member['contact-id'] = $contact['newid'];
  215. $import++;
  216. break;
  217. }
  218. }
  219. if ($import == 2) {
  220. $r = db_import_assoc('group_member', $group_member);
  221. if ($r === false) {
  222. logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  223. }
  224. }
  225. }
  226. foreach ($account['photo'] as &$photo) {
  227. $photo['uid'] = $newuid;
  228. $photo['data'] = hex2bin($photo['data']);
  229. $p = new Photo($photo['data'], $photo['type']);
  230. $r = $p->store(
  231. $photo['uid'], $photo['contact-id'], //0
  232. $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
  233. $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
  234. );
  235. if ($r === false) {
  236. logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  237. }
  238. }
  239. foreach ($account['pconfig'] as &$pconfig) {
  240. $pconfig['uid'] = $newuid;
  241. $r = db_import_assoc('pconfig', $pconfig);
  242. if ($r === false) {
  243. logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
  244. }
  245. }
  246. // send relocate messages
  247. proc_run(PRIORITY_HIGH, 'include/notifier.php', 'relocate', $newuid);
  248. info(t("Done. You can now login with your username and password"));
  249. goaway(System::baseUrl() . "/login");
  250. }