3 * @file src/Core/UserImport.php
5 namespace Friendica\Core;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Protocol;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Photo;
12 use Friendica\Object\Image;
14 require_once "include/dba.php";
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)
38 private static function checkCols($table, &$arr)
40 $query = sprintf("SHOW COLUMNS IN `%s`", DBA::escape($table));
41 Logger::log("uimport: $query", Logger::DEBUG);
44 // get a plain array of column names
45 foreach ($r as $tcol) {
46 $tcols[] = $tcol['Field'];
48 // remove inexistent columns
49 foreach ($arr as $icol => $ival) {
50 if (!in_array($icol, $tcols)) {
57 * Import data into table $table
59 * @param string $table Table name
60 * @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
87 public static function importAccount(App $a, $file)
89 Logger::log("Start user import from " . $file['tmp_name']);
93 2. replace old baseurl with new baseurl
94 3. import data (look at user id and contacts id)
95 4. archive non-dfrn contacts
96 5. send message to dfrn contacts
99 $account = json_decode(file_get_contents($file['tmp_name']), true);
100 if ($account === null) {
101 notice(L10n::t("Error decoding account file"));
106 if (!x($account, 'version')) {
107 notice(L10n::t("Error! No version data in file! This is not a Friendica account file?"));
111 // check for username
112 // check if username matches deleted account
113 if (DBA::exists('user', ['nickname' => $account['user']['nickname']])
114 || DBA::exists('userd', ['username' => $account['user']['nickname']])) {
115 notice(L10n::t("User '%s' already exists on this server!", $account['user']['nickname']));
119 $oldbaseurl = $account['baseurl'];
120 $newbaseurl = System::baseUrl();
122 $oldaddr = str_replace('http://', '@', normalise_link($oldbaseurl));
123 $newaddr = str_replace('http://', '@', normalise_link($newbaseurl));
125 if (!empty($account['profile']['addr'])) {
126 $old_handle = $account['profile']['addr'];
128 $old_handle = $account['user']['nickname'].$oldaddr;
131 $olduid = $account['user']['uid'];
133 unset($account['user']['uid']);
134 unset($account['user']['account_expired']);
135 unset($account['user']['account_expires_on']);
136 unset($account['user']['expire_notification_sent']);
138 $callback = function (&$value) use ($oldbaseurl, $oldaddr, $newbaseurl, $newaddr) {
139 $value = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $value);
142 array_walk($account['user'], $callback);
145 $r = self::dbImportAssoc('user', $account['user']);
147 Logger::log("uimport:insert user : ERROR : " . DBA::errorMessage(), Logger::INFO);
148 notice(L10n::t("User creation error"));
151 $newuid = self::lastInsertId();
153 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
155 foreach ($account['profile'] as &$profile) {
156 foreach ($profile as $k => &$v) {
157 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
158 foreach (["profile", "avatar"] as $k) {
159 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
162 $profile['uid'] = $newuid;
163 $r = self::dbImportAssoc('profile', $profile);
165 Logger::log("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
166 info(L10n::t("User profile creation error"));
167 DBA::delete('user', ['uid' => $newuid]);
173 foreach ($account['contact'] as &$contact) {
174 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
175 foreach ($contact as $k => &$v) {
176 $v = str_replace([$oldbaseurl, $oldaddr], [$newbaseurl, $newaddr], $v);
177 foreach (["profile", "avatar", "micro"] as $k) {
178 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
182 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
183 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
184 $contact["avatar-date"] = DBA::NULL_DATETIME;
186 switch ($contact['network']) {
188 case Protocol::DIASPORA:
189 // send relocate message (below)
196 // archive other contacts
197 $contact['archive'] = "1";
200 $contact['uid'] = $newuid;
201 $r = self::dbImportAssoc('contact', $contact);
203 Logger::log("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
206 $contact['newid'] = self::lastInsertId();
209 if ($errorcount > 0) {
210 notice(L10n::tt("%d contact not imported", "%d contacts not imported", $errorcount));
213 foreach ($account['group'] as &$group) {
214 $group['uid'] = $newuid;
215 $r = self::dbImportAssoc('group', $group);
217 Logger::log("uimport:insert group " . $group['name'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
219 $group['newid'] = self::lastInsertId();
223 foreach ($account['group_member'] as &$group_member) {
225 foreach ($account['group'] as $group) {
226 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
227 $group_member['gid'] = $group['newid'];
232 foreach ($account['contact'] as $contact) {
233 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
234 $group_member['contact-id'] = $contact['newid'];
240 $r = self::dbImportAssoc('group_member', $group_member);
242 Logger::log("uimport:insert group member " . $group_member['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
247 foreach ($account['photo'] as &$photo) {
248 $photo['uid'] = $newuid;
249 $photo['data'] = hex2bin($photo['data']);
251 $Image = new Image($photo['data'], $photo['type']);
254 $photo['uid'], $photo['contact-id'], //0
255 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
256 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
260 Logger::log("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
264 foreach ($account['pconfig'] as &$pconfig) {
265 $pconfig['uid'] = $newuid;
266 $r = self::dbImportAssoc('pconfig', $pconfig);
268 Logger::log("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . DBA::errorMessage(), Logger::INFO);
272 // send relocate messages
273 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
275 info(L10n::t("Done. You can now login with your username and password"));
276 $a->internalRedirect('login');