3 * @file src/Core/UserImport.php
5 namespace Friendica\Core;
8 use Friendica\Database\DBA;
9 use Friendica\Database\DBStructure;
11 use Friendica\Model\Photo;
12 use Friendica\Object\Image;
13 use Friendica\Util\Strings;
14 use Friendica\Worker\Delivery;
17 * @brief UserImport class
21 const IMPORT_DEBUG = false;
23 private static function lastInsertId()
25 if (self::IMPORT_DEBUG) {
29 return DBA::lastInsertId();
33 * Remove columns from array $arr that aren't in table $table
35 * @param string $table Table name
36 * @param array &$arr Column=>Value array from json (by ref)
39 private static function checkCols($table, &$arr)
41 $tableColumns = DBStructure::getColumns($table);
45 // get a plain array of column names
46 foreach ($tableColumns as $tcol) {
47 $tcols[] = $tcol['Field'];
48 $ttype[$tcol['Field']] = $tcol['Type'];
50 // remove inexistent columns
51 foreach ($arr as $icol => $ival) {
52 if (!in_array($icol, $tcols)) {
57 if ($ttype[$icol] === 'datetime') {
58 $arr[$icol] = $ival ?? DBA::NULL_DATETIME;
64 * Import data into table $table
66 * @param string $table Table name
67 * @param array $arr Column=>Value array from json
71 private static function dbImportAssoc($table, $arr)
73 if (isset($arr['id'])) {
77 self::checkCols($table, $arr);
79 if (self::IMPORT_DEBUG) {
83 return DBA::insert($table, $arr);
87 * @brief Import account file exported from mod/uexport
89 * @param array $file array from $_FILES
90 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
91 * @throws \ImagickException
93 public static function importAccount($file)
95 Logger::log("Start user import from " . $file['tmp_name']);
99 2. replace old baseurl with new baseurl
100 3. import data (look at user id and contacts id)
101 4. archive non-dfrn contacts
102 5. send message to dfrn contacts
105 $account = json_decode(file_get_contents($file['tmp_name']), true);
106 if ($account === null) {
107 notice(L10n::t("Error decoding account file"));
112 if (empty($account['version'])) {
113 notice(L10n::t("Error! No version data in file! This is not a Friendica account file?"));
117 // check for username
118 // check if username matches deleted account
119 if (DBA::exists('user', ['nickname' => $account['user']['nickname']])
120 || DBA::exists('userd', ['username' => $account['user']['nickname']])) {
121 notice(L10n::t("User '%s' already exists on this server!", $account['user']['nickname']));
125 $oldbaseurl = $account['baseurl'];
126 $newbaseurl = System::baseUrl();
128 $oldaddr = str_replace('http://', '@', Strings::normaliseLink($oldbaseurl));
129 $newaddr = str_replace('http://', '@', Strings::normaliseLink($newbaseurl));
131 if (!empty($account['profile']['addr'])) {
132 $old_handle = $account['profile']['addr'];
134 $old_handle = $account['user']['nickname'].$oldaddr;
137 // Creating a new guid to avoid problems with Diaspora
138 $account['user']['guid'] = System::createUUID();
140 $olduid = $account['user']['uid'];
142 unset($account['user']['uid']);
143 unset($account['user']['account_expired']);
144 unset($account['user']['account_expires_on']);
145 unset($account['user']['expire_notification_sent']);
147 $callback = function (&$value) use ($oldbaseurl, $oldaddr, $newbaseurl, $newaddr) {
148 $value = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $value);
151 array_walk($account['user'], $callback);
154 $r = self::dbImportAssoc('user', $account['user']);
156 Logger::log("uimport:insert user : ERROR : " . DBA::errorMessage(), Logger::INFO);
157 notice(L10n::t("User creation error"));
160 $newuid = self::lastInsertId();
162 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
164 foreach ($account['profile'] as &$profile) {
165 foreach ($profile as $k => &$v) {
166 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
167 foreach (["profile", "avatar"] as $k) {
168 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
171 $profile['uid'] = $newuid;
172 $r = self::dbImportAssoc('profile', $profile);
174 Logger::log("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
175 info(L10n::t("User profile creation error"));
176 DBA::delete('user', ['uid' => $newuid]);
182 foreach ($account['contact'] as &$contact) {
183 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
184 foreach ($contact as $k => &$v) {
185 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
186 foreach (["profile", "avatar", "micro"] as $k) {
187 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
191 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
192 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
193 $contact["avatar-date"] = DBA::NULL_DATETIME;
195 switch ($contact['network']) {
197 case Protocol::DIASPORA:
198 // send relocate message (below)
205 // archive other contacts
206 $contact['archive'] = "1";
209 $contact['uid'] = $newuid;
210 $r = self::dbImportAssoc('contact', $contact);
212 Logger::log("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
215 $contact['newid'] = self::lastInsertId();
218 if ($errorcount > 0) {
219 notice(L10n::tt("%d contact not imported", "%d contacts not imported", $errorcount));
222 foreach ($account['group'] as &$group) {
223 $group['uid'] = $newuid;
224 $r = self::dbImportAssoc('group', $group);
226 Logger::log("uimport:insert group " . $group['name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
228 $group['newid'] = self::lastInsertId();
232 foreach ($account['group_member'] as &$group_member) {
234 foreach ($account['group'] as $group) {
235 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
236 $group_member['gid'] = $group['newid'];
241 foreach ($account['contact'] as $contact) {
242 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
243 $group_member['contact-id'] = $contact['newid'];
249 $r = self::dbImportAssoc('group_member', $group_member);
251 Logger::log("uimport:insert group member " . $group_member['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
256 foreach ($account['photo'] as &$photo) {
257 $photo['uid'] = $newuid;
258 $photo['data'] = hex2bin($photo['data']);
260 $Image = new Image($photo['data'], $photo['type']);
263 $photo['uid'], $photo['contact-id'], //0
264 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
265 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
269 Logger::log("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
273 foreach ($account['pconfig'] as &$pconfig) {
274 $pconfig['uid'] = $newuid;
275 $r = self::dbImportAssoc('pconfig', $pconfig);
277 Logger::log("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
281 // send relocate messages
282 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::RELOCATION, $newuid);
284 info(L10n::t("Done. You can now login with your username and password"));
285 DI::baseUrl()->redirect('login');