3 * @file src/Core/UserImport.php
5 namespace Friendica\Core;
8 use Friendica\Database\DBA;
9 use Friendica\Model\Photo;
10 use Friendica\Object\Image;
11 use Friendica\Util\Strings;
14 * @brief UserImport class
18 const IMPORT_DEBUG = false;
20 private static function lastInsertId()
22 if (self::IMPORT_DEBUG) {
26 return DBA::lastInsertId();
30 * Remove columns from array $arr that aren't in table $table
32 * @param string $table Table name
33 * @param array &$arr Column=>Value array from json (by ref)
36 private static function checkCols($table, &$arr)
38 $query = sprintf("SHOW COLUMNS IN `%s`", DBA::escape($table));
39 Logger::log("uimport: $query", Logger::DEBUG);
42 // get a plain array of column names
43 foreach ($r as $tcol) {
44 $tcols[] = $tcol['Field'];
46 // remove inexistent columns
47 foreach ($arr as $icol => $ival) {
48 if (!in_array($icol, $tcols)) {
55 * Import data into table $table
57 * @param string $table Table name
58 * @param array $arr Column=>Value array from json
62 private static function dbImportAssoc($table, $arr)
64 if (isset($arr['id'])) {
68 self::checkCols($table, $arr);
69 $cols = implode("`,`", array_map(['Friendica\Database\DBA', 'escape'], array_keys($arr)));
70 $vals = implode("','", array_map(['Friendica\Database\DBA', 'escape'], array_values($arr)));
71 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
72 Logger::log("uimport: $query", Logger::TRACE);
74 if (self::IMPORT_DEBUG) {
82 * @brief Import account file exported from mod/uexport
84 * @param App $a Friendica App Class
85 * @param array $file array from $_FILES
86 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
87 * @throws \ImagickException
89 public static function importAccount(App $a, $file)
91 Logger::log("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(L10n::t("Error decoding account file"));
108 if (empty($account['version'])) {
109 notice(L10n::t("Error! No version data in file! This is not a Friendica account file?"));
113 // check for username
114 // check if username matches deleted account
115 if (DBA::exists('user', ['nickname' => $account['user']['nickname']])
116 || DBA::exists('userd', ['username' => $account['user']['nickname']])) {
117 notice(L10n::t("User '%s' already exists on this server!", $account['user']['nickname']));
121 $oldbaseurl = $account['baseurl'];
122 $newbaseurl = System::baseUrl();
124 $oldaddr = str_replace('http://', '@', Strings::normaliseLink($oldbaseurl));
125 $newaddr = str_replace('http://', '@', Strings::normaliseLink($newbaseurl));
127 if (!empty($account['profile']['addr'])) {
128 $old_handle = $account['profile']['addr'];
130 $old_handle = $account['user']['nickname'].$oldaddr;
133 // Creating a new guid to avoid problems with Diaspora
134 $account['user']['guid'] = System::createUUID();
136 $olduid = $account['user']['uid'];
138 unset($account['user']['uid']);
139 unset($account['user']['account_expired']);
140 unset($account['user']['account_expires_on']);
141 unset($account['user']['expire_notification_sent']);
143 $callback = function (&$value) use ($oldbaseurl, $oldaddr, $newbaseurl, $newaddr) {
144 $value = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $value);
147 array_walk($account['user'], $callback);
150 $r = self::dbImportAssoc('user', $account['user']);
152 Logger::log("uimport:insert user : ERROR : " . DBA::errorMessage(), Logger::INFO);
153 notice(L10n::t("User creation error"));
156 $newuid = self::lastInsertId();
158 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
160 foreach ($account['profile'] as &$profile) {
161 foreach ($profile as $k => &$v) {
162 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
163 foreach (["profile", "avatar"] as $k) {
164 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
167 $profile['uid'] = $newuid;
168 $r = self::dbImportAssoc('profile', $profile);
170 Logger::log("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
171 info(L10n::t("User profile creation error"));
172 DBA::delete('user', ['uid' => $newuid]);
178 foreach ($account['contact'] as &$contact) {
179 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
180 foreach ($contact as $k => &$v) {
181 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
182 foreach (["profile", "avatar", "micro"] as $k) {
183 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
187 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
188 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
189 $contact["avatar-date"] = DBA::NULL_DATETIME;
191 switch ($contact['network']) {
193 case Protocol::DIASPORA:
194 // send relocate message (below)
201 // archive other contacts
202 $contact['archive'] = "1";
205 $contact['uid'] = $newuid;
206 $r = self::dbImportAssoc('contact', $contact);
208 Logger::log("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
211 $contact['newid'] = self::lastInsertId();
214 if ($errorcount > 0) {
215 notice(L10n::tt("%d contact not imported", "%d contacts not imported", $errorcount));
218 foreach ($account['group'] as &$group) {
219 $group['uid'] = $newuid;
220 $r = self::dbImportAssoc('group', $group);
222 Logger::log("uimport:insert group " . $group['name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
224 $group['newid'] = self::lastInsertId();
228 foreach ($account['group_member'] as &$group_member) {
230 foreach ($account['group'] as $group) {
231 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
232 $group_member['gid'] = $group['newid'];
237 foreach ($account['contact'] as $contact) {
238 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
239 $group_member['contact-id'] = $contact['newid'];
245 $r = self::dbImportAssoc('group_member', $group_member);
247 Logger::log("uimport:insert group member " . $group_member['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
252 foreach ($account['photo'] as &$photo) {
253 $photo['uid'] = $newuid;
254 $photo['data'] = hex2bin($photo['data']);
256 $Image = new Image($photo['data'], $photo['type']);
259 $photo['uid'], $photo['contact-id'], //0
260 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
261 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
265 Logger::log("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
269 foreach ($account['pconfig'] as &$pconfig) {
270 $pconfig['uid'] = $newuid;
271 $r = self::dbImportAssoc('pconfig', $pconfig);
273 Logger::log("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
277 // send relocate messages
278 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
280 info(L10n::t("Done. You can now login with your username and password"));
281 $a->internalRedirect('login');