Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

288 řádky
8.7KB

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