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";
18 define("IMPORT_DEBUG", false);
21 * @brief UserImport class
25 private static function lastInsertId()
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);
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 foreach ($account['user'] as $k => &$v) {
159 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
163 $r = self::dbImportAssoc('user', $account['user']);
165 logger("uimport:insert user : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
166 notice(t("User creation error"));
169 $newuid = self::lastInsertId();
171 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
173 // Generate a new guid for the account. Otherwise there will be problems with diaspora
174 dba::update('user', ['guid' => generate_user_guid()], ['uid' => $newuid]);
176 foreach ($account['profile'] as &$profile) {
177 foreach ($profile as $k => &$v) {
178 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
179 foreach (array("profile", "avatar") as $k) {
180 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
183 $profile['uid'] = $newuid;
184 $r = self::dbImportAssoc('profile', $profile);
186 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
187 info(t("User profile creation error"));
188 dba::delete('user', array('uid' => $newuid));
194 foreach ($account['contact'] as &$contact) {
195 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
196 foreach ($contact as $k => &$v) {
197 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
198 foreach (array("profile", "avatar", "micro") as $k) {
199 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
203 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
204 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
205 $contact["avatar-date"] = NULL_DATE;
207 switch ($contact['network']) {
209 case NETWORK_DIASPORA:
210 // send relocate message (below)
217 // archive other contacts
218 $contact['archive'] = "1";
221 $contact['uid'] = $newuid;
222 $r = self::dbImportAssoc('contact', $contact);
224 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
227 $contact['newid'] = self::lastInsertId();
230 if ($errorcount > 0) {
231 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
234 foreach ($account['group'] as &$group) {
235 $group['uid'] = $newuid;
236 $r = self::dbImportAssoc('group', $group);
238 logger("uimport:insert group " . $group['name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
240 $group['newid'] = self::lastInsertId();
244 foreach ($account['group_member'] as &$group_member) {
246 foreach ($account['group'] as $group) {
247 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
248 $group_member['gid'] = $group['newid'];
253 foreach ($account['contact'] as $contact) {
254 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
255 $group_member['contact-id'] = $contact['newid'];
261 $r = self::dbImportAssoc('group_member', $group_member);
263 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
268 foreach ($account['photo'] as &$photo) {
269 $photo['uid'] = $newuid;
270 $photo['data'] = hex2bin($photo['data']);
272 $Image = new Image($photo['data'], $photo['type']);
275 $photo['uid'], $photo['contact-id'], //0
276 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
277 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
281 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
285 foreach ($account['pconfig'] as &$pconfig) {
286 $pconfig['uid'] = $newuid;
287 $r = self::dbImportAssoc('pconfig', $pconfig);
289 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
293 // send relocate messages
294 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
296 info(t("Done. You can now login with your username and password"));
297 goaway(System::baseUrl() . "/login");