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;
12 require_once "include/dba.php";
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)
36 private static function checkCols($table, &$arr)
38 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
39 logger("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
60 private static function dbImportAssoc($table, $arr)
62 if (isset($arr['id'])) {
66 self::checkCols($table, $arr);
67 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
68 $vals = implode("','", array_map('dbesc', array_values($arr)));
69 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
70 logger("uimport: $query", LOGGER_TRACE);
72 if (self::IMPORT_DEBUG) {
80 * @brief Import account file exported from mod/uexport
82 * @param App $a Friendica App Class
83 * @param array $file array from $_FILES
85 public static function importAccount(App $a, $file)
87 logger("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 (!x($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://', '@', normalise_link($oldbaseurl));
121 $newaddr = str_replace('http://', '@', normalise_link($newbaseurl));
123 if (!empty($account['profile']['addr'])) {
124 $old_handle = $account['profile']['addr'];
126 $old_handle = $account['user']['nickname'].$oldaddr;
129 $olduid = $account['user']['uid'];
131 unset($account['user']['uid']);
132 unset($account['user']['account_expired']);
133 unset($account['user']['account_expires_on']);
134 unset($account['user']['expire_notification_sent']);
136 $callback = function (&$value) use ($oldbaseurl, $oldaddr, $newbaseurl, $newaddr) {
137 $value = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $value);
140 array_walk($account['user'], $callback);
143 $r = self::dbImportAssoc('user', $account['user']);
145 logger("uimport:insert user : ERROR : " . DBA::errorMessage(), LOGGER_INFO);
146 notice(L10n::t("User creation error"));
149 $newuid = self::lastInsertId();
151 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
153 foreach ($account['profile'] as &$profile) {
154 foreach ($profile as $k => &$v) {
155 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
156 foreach (["profile", "avatar"] as $k) {
157 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
160 $profile['uid'] = $newuid;
161 $r = self::dbImportAssoc('profile', $profile);
163 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . DBA::errorMessage(), LOGGER_INFO);
164 info(L10n::t("User profile creation error"));
165 DBA::delete('user', ['uid' => $newuid]);
171 foreach ($account['contact'] as &$contact) {
172 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
173 foreach ($contact as $k => &$v) {
174 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
175 foreach (["profile", "avatar", "micro"] as $k) {
176 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
180 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
181 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
182 $contact["avatar-date"] = NULL_DATE;
184 switch ($contact['network']) {
186 case NETWORK_DIASPORA:
187 // send relocate message (below)
194 // archive other contacts
195 $contact['archive'] = "1";
198 $contact['uid'] = $newuid;
199 $r = self::dbImportAssoc('contact', $contact);
201 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . DBA::errorMessage(), LOGGER_INFO);
204 $contact['newid'] = self::lastInsertId();
207 if ($errorcount > 0) {
208 notice(L10n::tt("%d contact not imported", "%d contacts not imported", $errorcount));
211 foreach ($account['group'] as &$group) {
212 $group['uid'] = $newuid;
213 $r = self::dbImportAssoc('group', $group);
215 logger("uimport:insert group " . $group['name'] . " : ERROR : " . DBA::errorMessage(), LOGGER_INFO);
217 $group['newid'] = self::lastInsertId();
221 foreach ($account['group_member'] as &$group_member) {
223 foreach ($account['group'] as $group) {
224 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
225 $group_member['gid'] = $group['newid'];
230 foreach ($account['contact'] as $contact) {
231 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
232 $group_member['contact-id'] = $contact['newid'];
238 $r = self::dbImportAssoc('group_member', $group_member);
240 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . DBA::errorMessage(), LOGGER_INFO);
245 foreach ($account['photo'] as &$photo) {
246 $photo['uid'] = $newuid;
247 $photo['data'] = hex2bin($photo['data']);
249 $Image = new Image($photo['data'], $photo['type']);
252 $photo['uid'], $photo['contact-id'], //0
253 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
254 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
258 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . DBA::errorMessage(), LOGGER_INFO);
262 foreach ($account['pconfig'] as &$pconfig) {
263 $pconfig['uid'] = $newuid;
264 $r = self::dbImportAssoc('pconfig', $pconfig);
266 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . DBA::errorMessage(), LOGGER_INFO);
270 // send relocate messages
271 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
273 info(L10n::t("Done. You can now login with your username and password"));
274 goaway(System::baseUrl() . "/login");