3 * @file src/Core/UserImport.php
5 namespace Friendica\Core;
8 use Friendica\Database\DBA;
9 use Friendica\Database\DBStructure;
10 use Friendica\Model\Photo;
11 use Friendica\Object\Image;
12 use Friendica\Util\Strings;
15 * @brief UserImport class
19 const IMPORT_DEBUG = false;
21 private static function lastInsertId()
23 if (self::IMPORT_DEBUG) {
27 return DBA::lastInsertId();
31 * Remove columns from array $arr that aren't in table $table
33 * @param string $table Table name
34 * @param array &$arr Column=>Value array from json (by ref)
37 private static function checkCols($table, &$arr)
39 $tableColumns = DBStructure::getColumns($table);
42 // get a plain array of column names
43 foreach ($tableColumns 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);
70 if (self::IMPORT_DEBUG) {
74 return DBA::insert($table, $arr);
78 * @brief Import account file exported from mod/uexport
80 * @param App $a Friendica App Class
81 * @param array $file array from $_FILES
82 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
83 * @throws \ImagickException
85 public static function importAccount(App $a, $file)
87 Logger::log("Start user import from " . $file['tmp_name']);
91 2. replace old baseurl with new baseurl
92 3. import data (look at user id and contacts id)
93 4. archive non-dfrn contacts
94 5. send message to dfrn contacts
97 $account = json_decode(file_get_contents($file['tmp_name']), true);
98 if ($account === null) {
99 notice(L10n::t("Error decoding account file"));
104 if (empty($account['version'])) {
105 notice(L10n::t("Error! No version data in file! This is not a Friendica account file?"));
109 // check for username
110 // check if username matches deleted account
111 if (DBA::exists('user', ['nickname' => $account['user']['nickname']])
112 || DBA::exists('userd', ['username' => $account['user']['nickname']])) {
113 notice(L10n::t("User '%s' already exists on this server!", $account['user']['nickname']));
117 $oldbaseurl = $account['baseurl'];
118 $newbaseurl = System::baseUrl();
120 $oldaddr = str_replace('http://', '@', Strings::normaliseLink($oldbaseurl));
121 $newaddr = str_replace('http://', '@', Strings::normaliseLink($newbaseurl));
123 if (!empty($account['profile']['addr'])) {
124 $old_handle = $account['profile']['addr'];
126 $old_handle = $account['user']['nickname'].$oldaddr;
129 // Creating a new guid to avoid problems with Diaspora
130 $account['user']['guid'] = System::createUUID();
132 $olduid = $account['user']['uid'];
134 unset($account['user']['uid']);
135 unset($account['user']['account_expired']);
136 unset($account['user']['account_expires_on']);
137 unset($account['user']['expire_notification_sent']);
139 $callback = function (&$value) use ($oldbaseurl, $oldaddr, $newbaseurl, $newaddr) {
140 $value = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $value);
143 array_walk($account['user'], $callback);
146 $r = self::dbImportAssoc('user', $account['user']);
148 Logger::log("uimport:insert user : ERROR : " . DBA::errorMessage(), Logger::INFO);
149 notice(L10n::t("User creation error"));
152 $newuid = self::lastInsertId();
154 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
156 foreach ($account['profile'] as &$profile) {
157 foreach ($profile as $k => &$v) {
158 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
159 foreach (["profile", "avatar"] as $k) {
160 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
163 $profile['uid'] = $newuid;
164 $r = self::dbImportAssoc('profile', $profile);
166 Logger::log("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
167 info(L10n::t("User profile creation error"));
168 DBA::delete('user', ['uid' => $newuid]);
174 foreach ($account['contact'] as &$contact) {
175 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
176 foreach ($contact as $k => &$v) {
177 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
178 foreach (["profile", "avatar", "micro"] as $k) {
179 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
183 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
184 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
185 $contact["avatar-date"] = DBA::NULL_DATETIME;
187 switch ($contact['network']) {
189 case Protocol::DIASPORA:
190 // send relocate message (below)
197 // archive other contacts
198 $contact['archive'] = "1";
201 $contact['uid'] = $newuid;
202 $r = self::dbImportAssoc('contact', $contact);
204 Logger::log("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
207 $contact['newid'] = self::lastInsertId();
210 if ($errorcount > 0) {
211 notice(L10n::tt("%d contact not imported", "%d contacts not imported", $errorcount));
214 foreach ($account['group'] as &$group) {
215 $group['uid'] = $newuid;
216 $r = self::dbImportAssoc('group', $group);
218 Logger::log("uimport:insert group " . $group['name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
220 $group['newid'] = self::lastInsertId();
224 foreach ($account['group_member'] as &$group_member) {
226 foreach ($account['group'] as $group) {
227 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
228 $group_member['gid'] = $group['newid'];
233 foreach ($account['contact'] as $contact) {
234 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
235 $group_member['contact-id'] = $contact['newid'];
241 $r = self::dbImportAssoc('group_member', $group_member);
243 Logger::log("uimport:insert group member " . $group_member['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
248 foreach ($account['photo'] as &$photo) {
249 $photo['uid'] = $newuid;
250 $photo['data'] = hex2bin($photo['data']);
252 $Image = new Image($photo['data'], $photo['type']);
255 $photo['uid'], $photo['contact-id'], //0
256 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
257 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
261 Logger::log("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
265 foreach ($account['pconfig'] as &$pconfig) {
266 $pconfig['uid'] = $newuid;
267 $r = self::dbImportAssoc('pconfig', $pconfig);
269 Logger::log("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
273 // send relocate messages
274 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
276 info(L10n::t("Done. You can now login with your username and password"));
277 $a->internalRedirect('login');