3 * @file src/Core/UserImport.php
5 namespace Friendica\Core;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Protocol;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Photo;
13 use Friendica\Object\Image;
14 use Friendica\Util\Strings;
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 $query = sprintf("SHOW COLUMNS IN `%s`", DBA::escape($table));
42 Logger::log("uimport: $query", Logger::DEBUG);
45 // get a plain array of column names
46 foreach ($r as $tcol) {
47 $tcols[] = $tcol['Field'];
49 // remove inexistent columns
50 foreach ($arr as $icol => $ival) {
51 if (!in_array($icol, $tcols)) {
58 * Import data into table $table
60 * @param string $table Table name
61 * @param array $arr Column=>Value array from json
65 private static function dbImportAssoc($table, $arr)
67 if (isset($arr['id'])) {
71 self::checkCols($table, $arr);
72 $cols = implode("`,`", array_map(['Friendica\Database\DBA', 'escape'], array_keys($arr)));
73 $vals = implode("','", array_map(['Friendica\Database\DBA', 'escape'], array_values($arr)));
74 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
75 Logger::log("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
89 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
90 * @throws \ImagickException
92 public static function importAccount(App $a, $file)
94 Logger::log("Start user import from " . $file['tmp_name']);
98 2. replace old baseurl with new baseurl
99 3. import data (look at user id and contacts id)
100 4. archive non-dfrn contacts
101 5. send message to dfrn contacts
104 $account = json_decode(file_get_contents($file['tmp_name']), true);
105 if ($account === null) {
106 notice(L10n::t("Error decoding account file"));
111 if (empty($account['version'])) {
112 notice(L10n::t("Error! No version data in file! This is not a Friendica account file?"));
116 // check for username
117 // check if username matches deleted account
118 if (DBA::exists('user', ['nickname' => $account['user']['nickname']])
119 || DBA::exists('userd', ['username' => $account['user']['nickname']])) {
120 notice(L10n::t("User '%s' already exists on this server!", $account['user']['nickname']));
124 $oldbaseurl = $account['baseurl'];
125 $newbaseurl = System::baseUrl();
127 $oldaddr = str_replace('http://', '@', Strings::normaliseLink($oldbaseurl));
128 $newaddr = str_replace('http://', '@', Strings::normaliseLink($newbaseurl));
130 if (!empty($account['profile']['addr'])) {
131 $old_handle = $account['profile']['addr'];
133 $old_handle = $account['user']['nickname'].$oldaddr;
136 // Creating a new guid to avoid problems with Diaspora
137 $account['user']['guid'] = System::createUUID();
139 $olduid = $account['user']['uid'];
141 unset($account['user']['uid']);
142 unset($account['user']['account_expired']);
143 unset($account['user']['account_expires_on']);
144 unset($account['user']['expire_notification_sent']);
146 $callback = function (&$value) use ($oldbaseurl, $oldaddr, $newbaseurl, $newaddr) {
147 $value = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $value);
150 array_walk($account['user'], $callback);
153 $r = self::dbImportAssoc('user', $account['user']);
155 Logger::log("uimport:insert user : ERROR : " . DBA::errorMessage(), Logger::INFO);
156 notice(L10n::t("User creation error"));
159 $newuid = self::lastInsertId();
161 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
163 foreach ($account['profile'] as &$profile) {
164 foreach ($profile as $k => &$v) {
165 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
166 foreach (["profile", "avatar"] as $k) {
167 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
170 $profile['uid'] = $newuid;
171 $r = self::dbImportAssoc('profile', $profile);
173 Logger::log("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
174 info(L10n::t("User profile creation error"));
175 DBA::delete('user', ['uid' => $newuid]);
181 foreach ($account['contact'] as &$contact) {
182 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
183 foreach ($contact as $k => &$v) {
184 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
185 foreach (["profile", "avatar", "micro"] as $k) {
186 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
190 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
191 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
192 $contact["avatar-date"] = DBA::NULL_DATETIME;
194 switch ($contact['network']) {
196 case Protocol::DIASPORA:
197 // send relocate message (below)
204 // archive other contacts
205 $contact['archive'] = "1";
208 $contact['uid'] = $newuid;
209 $r = self::dbImportAssoc('contact', $contact);
211 Logger::log("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
214 $contact['newid'] = self::lastInsertId();
217 if ($errorcount > 0) {
218 notice(L10n::tt("%d contact not imported", "%d contacts not imported", $errorcount));
221 foreach ($account['group'] as &$group) {
222 $group['uid'] = $newuid;
223 $r = self::dbImportAssoc('group', $group);
225 Logger::log("uimport:insert group " . $group['name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
227 $group['newid'] = self::lastInsertId();
231 foreach ($account['group_member'] as &$group_member) {
233 foreach ($account['group'] as $group) {
234 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
235 $group_member['gid'] = $group['newid'];
240 foreach ($account['contact'] as $contact) {
241 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
242 $group_member['contact-id'] = $contact['newid'];
248 $r = self::dbImportAssoc('group_member', $group_member);
250 Logger::log("uimport:insert group member " . $group_member['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
255 foreach ($account['photo'] as &$photo) {
256 $photo['uid'] = $newuid;
257 $photo['data'] = hex2bin($photo['data']);
259 $Image = new Image($photo['data'], $photo['type']);
262 $photo['uid'], $photo['contact-id'], //0
263 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
264 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
268 Logger::log("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
272 foreach ($account['pconfig'] as &$pconfig) {
273 $pconfig['uid'] = $newuid;
274 $r = self::dbImportAssoc('pconfig', $pconfig);
276 Logger::log("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
280 // send relocate messages
281 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
283 info(L10n::t("Done. You can now login with your username and password"));
284 $a->internalRedirect('login');