3 * @file src/Core/UserImport.php
5 namespace Friendica\Core;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Core\PConfig;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Photo;
14 use Friendica\Object\Image;
17 require_once "include/dba.php";
20 * @brief UserImport class
24 const IMPORT_DEBUG = false;
26 private static function lastInsertId()
28 if (self::IMPORT_DEBUG) {
32 return dba::lastInsertId();
36 * Remove columns from array $arr that aren't in table $table
38 * @param string $table Table name
39 * @param array &$arr Column=>Value array from json (by ref)
41 private static function checkCols($table, &$arr)
43 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
44 logger("uimport: $query", LOGGER_DEBUG);
47 // get a plain array of column names
48 foreach ($r as $tcol) {
49 $tcols[] = $tcol['Field'];
51 // remove inexistent columns
52 foreach ($arr as $icol => $ival) {
53 if (!in_array($icol, $tcols)) {
60 * Import data into table $table
62 * @param string $table Table name
63 * @param array $arr Column=>Value array from json
65 private static function dbImportAssoc($table, $arr)
67 if (isset($arr['id'])) {
71 self::check_cols($table, $arr);
72 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
73 $vals = implode("','", array_map('dbesc', array_values($arr)));
74 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
75 logger("uimport: $query", LOGGER_TRACE);
77 if (self::IMPORT_DEBUG) {
85 * @brief Import account file exported from mod/uexport
87 * @param App $a Friendica App Class
88 * @param array $file array from $_FILES
90 public static function importAccount(App $a, $file)
92 logger("Start user import from " . $file['tmp_name']);
96 2. replace old baseurl with new baseurl
97 3. import data (look at user id and contacts id)
98 4. archive non-dfrn contacts
99 5. send message to dfrn contacts
102 $account = json_decode(file_get_contents($file['tmp_name']), true);
103 if ($account === null) {
104 notice(L10n::t("Error decoding account file"));
109 if (!x($account, 'version')) {
110 notice(L10n::t("Error! No version data in file! This is not a Friendica account file?"));
114 // check for username
115 $r = dba::selectFirst('user', ['uid'], ['nickname' => $account['user']['nickname']]);
117 logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
118 notice(L10n::t('Error! Cannot check nickname'));
122 if (DBM::is_result($r) > 0) {
123 notice(L10n::t("User '%s' already exists on this server!", $account['user']['nickname']));
127 // check if username matches deleted account
128 $r = dba::selectFirst('userd', ['id'], ['username' => $account['user']['nickname']]);
130 logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
131 notice(L10n::t('Error! Cannot check nickname'));
135 if (DBM::is_result($r) > 0) {
136 notice(L10n::t("User '%s' already exists on this server!", $account['user']['nickname']));
140 $oldbaseurl = $account['baseurl'];
141 $newbaseurl = System::baseUrl();
143 $oldaddr = str_replace('http://', '@', normalise_link($oldbaseurl));
144 $newaddr = str_replace('http://', '@', normalise_link($newbaseurl));
146 if (!empty($account['profile']['addr'])) {
147 $old_handle = $account['profile']['addr'];
149 $old_handle = $account['user']['nickname'].$oldaddr;
152 $olduid = $account['user']['uid'];
154 unset($account['user']['uid']);
155 unset($account['user']['account_expired']);
156 unset($account['user']['account_expires_on']);
157 unset($account['user']['expire_notification_sent']);
159 $callback = function (&$value) use ($oldbaseurl, $oldaddr, $newbaseurl, $newaddr) {
160 $value = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $value);
163 array_walk($account['user'], $callback);
166 $r = self::dbImportAssoc('user', $account['user']);
168 logger("uimport:insert user : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
169 notice(L10n::t("User creation error"));
172 $newuid = self::lastInsertId();
174 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
176 // Generate a new guid for the account. Otherwise there will be problems with diaspora
177 dba::update('user', ['guid' => generate_user_guid()], ['uid' => $newuid]);
179 foreach ($account['profile'] as &$profile) {
180 foreach ($profile as $k => &$v) {
181 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
182 foreach (["profile", "avatar"] as $k) {
183 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
186 $profile['uid'] = $newuid;
187 $r = self::dbImportAssoc('profile', $profile);
189 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
190 info(L10n::t("User profile creation error"));
191 dba::delete('user', ['uid' => $newuid]);
197 foreach ($account['contact'] as &$contact) {
198 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
199 foreach ($contact as $k => &$v) {
200 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
201 foreach (["profile", "avatar", "micro"] as $k) {
202 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
206 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
207 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
208 $contact["avatar-date"] = NULL_DATE;
210 switch ($contact['network']) {
212 case NETWORK_DIASPORA:
213 // send relocate message (below)
220 // archive other contacts
221 $contact['archive'] = "1";
224 $contact['uid'] = $newuid;
225 $r = self::dbImportAssoc('contact', $contact);
227 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
230 $contact['newid'] = self::lastInsertId();
233 if ($errorcount > 0) {
234 notice(L10n::tt("%d contact not imported", "%d contacts not imported", $errorcount));
237 foreach ($account['group'] as &$group) {
238 $group['uid'] = $newuid;
239 $r = self::dbImportAssoc('group', $group);
241 logger("uimport:insert group " . $group['name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
243 $group['newid'] = self::lastInsertId();
247 foreach ($account['group_member'] as &$group_member) {
249 foreach ($account['group'] as $group) {
250 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
251 $group_member['gid'] = $group['newid'];
256 foreach ($account['contact'] as $contact) {
257 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
258 $group_member['contact-id'] = $contact['newid'];
264 $r = self::dbImportAssoc('group_member', $group_member);
266 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
271 foreach ($account['photo'] as &$photo) {
272 $photo['uid'] = $newuid;
273 $photo['data'] = hex2bin($photo['data']);
275 $Image = new Image($photo['data'], $photo['type']);
278 $photo['uid'], $photo['contact-id'], //0
279 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
280 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
284 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
288 foreach ($account['pconfig'] as &$pconfig) {
289 $pconfig['uid'] = $newuid;
290 $r = self::dbImportAssoc('pconfig', $pconfig);
292 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
296 // send relocate messages
297 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
299 info(L10n::t("Done. You can now login with your username and password"));
300 goaway(System::baseUrl() . "/login");