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.

289 lines
8.7 KiB

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