3 * @file src/Core/UserImport.php
5 namespace Friendica\Core;
8 use Friendica\Core\System;
9 use Friendica\Core\PConfig;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Photo;
13 use Friendica\Object\Image;
16 require_once "include/dba.php";
19 * @brief UserImport class
23 const IMPORT_DEBUG = false;
25 private static function lastInsertId()
27 if (self::IMPORT_DEBUG) {
31 return dba::lastInsertId();
35 * Remove columns from array $arr that aren't in table $table
37 * @param string $table Table name
38 * @param array &$arr Column=>Value array from json (by ref)
40 private static function checkCols($table, &$arr)
42 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
43 logger("uimport: $query", LOGGER_DEBUG);
46 // get a plain array of column names
47 foreach ($r as $tcol) {
48 $tcols[] = $tcol['Field'];
50 // remove inexistent columns
51 foreach ($arr as $icol => $ival) {
52 if (!in_array($icol, $tcols)) {
59 * Import data into table $table
61 * @param string $table Table name
62 * @param array $arr Column=>Value array from json
64 private static function dbImportAssoc($table, $arr)
66 if (isset($arr['id'])) {
70 self::check_cols($table, $arr);
71 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
72 $vals = implode("','", array_map('dbesc', array_values($arr)));
73 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
74 logger("uimport: $query", LOGGER_TRACE);
76 if (self::IMPORT_DEBUG) {
84 * @brief Import account file exported from mod/uexport
86 * @param App $a Friendica App Class
87 * @param array $file array from $_FILES
89 public static function importAccount(App $a, $file)
91 logger("Start user import from " . $file['tmp_name']);
95 2. replace old baseurl with new baseurl
96 3. import data (look at user id and contacts id)
97 4. archive non-dfrn contacts
98 5. send message to dfrn contacts
101 $account = json_decode(file_get_contents($file['tmp_name']), true);
102 if ($account === null) {
103 notice(t("Error decoding account file"));
108 if (!x($account, 'version')) {
109 notice(t("Error! No version data in file! This is not a Friendica account file?"));
113 // check for username
114 $r = dba::selectFirst('user', ['uid'], ['nickname' => $account['user']['nickname']]);
116 logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
117 notice(t('Error! Cannot check nickname'));
121 if (DBM::is_result($r) > 0) {
122 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
126 // check if username matches deleted account
127 $r = dba::selectFirst('userd', ['id'], ['username' => $account['user']['nickname']]);
129 logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
130 notice(t('Error! Cannot check nickname'));
134 if (DBM::is_result($r) > 0) {
135 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
139 $oldbaseurl = $account['baseurl'];
140 $newbaseurl = System::baseUrl();
142 $oldaddr = str_replace('http://', '@', normalise_link($oldbaseurl));
143 $newaddr = str_replace('http://', '@', normalise_link($newbaseurl));
145 if (!empty($account['profile']['addr'])) {
146 $old_handle = $account['profile']['addr'];
148 $old_handle = $account['user']['nickname'].$oldaddr;
151 $olduid = $account['user']['uid'];
153 unset($account['user']['uid']);
154 unset($account['user']['account_expired']);
155 unset($account['user']['account_expires_on']);
156 unset($account['user']['expire_notification_sent']);
158 $callback = function (&$value) use ($oldbaseurl, $oldaddr, $newbaseurl, $newaddr) {
159 $value = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $value);
162 array_walk($account['user'], $callback);
165 $r = self::dbImportAssoc('user', $account['user']);
167 logger("uimport:insert user : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
168 notice(t("User creation error"));
171 $newuid = self::lastInsertId();
173 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
175 // Generate a new guid for the account. Otherwise there will be problems with diaspora
176 dba::update('user', ['guid' => generate_user_guid()], ['uid' => $newuid]);
178 foreach ($account['profile'] as &$profile) {
179 foreach ($profile as $k => &$v) {
180 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
181 foreach (["profile", "avatar"] as $k) {
182 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
185 $profile['uid'] = $newuid;
186 $r = self::dbImportAssoc('profile', $profile);
188 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
189 info(t("User profile creation error"));
190 dba::delete('user', ['uid' => $newuid]);
196 foreach ($account['contact'] as &$contact) {
197 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
198 foreach ($contact as $k => &$v) {
199 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
200 foreach (["profile", "avatar", "micro"] as $k) {
201 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
205 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
206 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
207 $contact["avatar-date"] = NULL_DATE;
209 switch ($contact['network']) {
211 case NETWORK_DIASPORA:
212 // send relocate message (below)
219 // archive other contacts
220 $contact['archive'] = "1";
223 $contact['uid'] = $newuid;
224 $r = self::dbImportAssoc('contact', $contact);
226 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
229 $contact['newid'] = self::lastInsertId();
232 if ($errorcount > 0) {
233 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
236 foreach ($account['group'] as &$group) {
237 $group['uid'] = $newuid;
238 $r = self::dbImportAssoc('group', $group);
240 logger("uimport:insert group " . $group['name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
242 $group['newid'] = self::lastInsertId();
246 foreach ($account['group_member'] as &$group_member) {
248 foreach ($account['group'] as $group) {
249 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
250 $group_member['gid'] = $group['newid'];
255 foreach ($account['contact'] as $contact) {
256 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
257 $group_member['contact-id'] = $contact['newid'];
263 $r = self::dbImportAssoc('group_member', $group_member);
265 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
270 foreach ($account['photo'] as &$photo) {
271 $photo['uid'] = $newuid;
272 $photo['data'] = hex2bin($photo['data']);
274 $Image = new Image($photo['data'], $photo['type']);
277 $photo['uid'], $photo['contact-id'], //0
278 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
279 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
283 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
287 foreach ($account['pconfig'] as &$pconfig) {
288 $pconfig['uid'] = $newuid;
289 $r = self::dbImportAssoc('pconfig', $pconfig);
291 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
295 // send relocate messages
296 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
298 info(t("Done. You can now login with your username and password"));
299 goaway(System::baseUrl() . "/login");