3 * @file include/uimport.php
6 use Friendica\Core\System;
7 use Friendica\Core\PConfig;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Photo;
11 use Friendica\Object\Image;
13 define("IMPORT_DEBUG", False);
15 function last_insert_id() {
20 return dba::lastInsertId();
24 * Remove columns from array $arr that aren't in table $table
26 * @param string $table Table name
27 * @param array &$arr Column=>Value array from json (by ref)
29 function check_cols($table, &$arr) {
30 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
31 logger("uimport: $query", LOGGER_DEBUG);
34 // get a plain array of column names
35 foreach ($r as $tcol) {
36 $tcols[] = $tcol['Field'];
38 // remove inexistent columns
39 foreach ($arr as $icol => $ival) {
40 if (!in_array($icol, $tcols)) {
47 * Import data into table $table
49 * @param string $table Table name
50 * @param array $arr Column=>Value array from json
52 function db_import_assoc($table, $arr) {
53 if (isset($arr['id']))
55 check_cols($table, $arr);
56 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
57 $vals = implode("','", array_map('dbesc', array_values($arr)));
58 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
59 logger("uimport: $query", LOGGER_TRACE);
67 * @brief Import account file exported from mod/uexport
69 * @param App $a Friendica App Class
70 * @param array $file array from $_FILES
72 function import_account(App $a, $file) {
73 logger("Start user import from " . $file['tmp_name']);
77 2. replace old baseurl with new baseurl
78 3. import data (look at user id and contacts id)
79 4. archive non-dfrn contacts
80 5. send message to dfrn contacts
83 $account = json_decode(file_get_contents($file['tmp_name']), true);
84 if ($account === null) {
85 notice(t("Error decoding account file"));
90 if (!x($account, 'version')) {
91 notice(t("Error! No version data in file! This is not a Friendica account file?"));
96 * @TODO Old-lost code?
97 // this is not required as we remove columns in json not in current db schema
98 if ($account['schema'] != DB_UPDATE_VERSION) {
99 notice(t("Error! I can't import this file: DB schema version is not compatible."));
104 // check for username
105 $r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
107 logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
108 notice(t('Error! Cannot check nickname'));
111 if (DBM::is_result($r) > 0) {
112 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
115 // check if username matches deleted account
116 $r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
118 logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
119 notice(t('Error! Cannot check nickname'));
122 if (DBM::is_result($r) > 0) {
123 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
127 $oldbaseurl = $account['baseurl'];
128 $newbaseurl = System::baseUrl();
130 $oldaddr = str_replace('http://', '@', normalise_link($oldbaseurl));
131 $newaddr = str_replace('http://', '@', normalise_link($newbaseurl));
133 if (!empty($account['profile']['addr'])) {
134 $old_handle = $account['profile']['addr'];
136 $old_handle = $account['user']['nickname'].$oldaddr;
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 foreach ($account['user'] as $k => &$v) {
147 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
151 $r = db_import_assoc('user', $account['user']);
153 logger("uimport:insert user : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
154 notice(t("User creation error"));
157 $newuid = last_insert_id();
160 PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
162 // Generate a new guid for the account. Otherwise there will be problems with diaspora
163 q("UPDATE `user` SET `guid` = '%s' WHERE `uid` = %d",
164 dbesc(generate_user_guid()), intval($newuid));
166 foreach ($account['profile'] as &$profile) {
167 foreach ($profile as $k => &$v) {
168 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
169 foreach (array("profile", "avatar") as $k) {
170 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
173 $profile['uid'] = $newuid;
174 $r = db_import_assoc('profile', $profile);
176 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
177 info(t("User profile creation error"));
178 dba::delete('user', array('uid' => $newuid));
184 foreach ($account['contact'] as &$contact) {
185 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
186 foreach ($contact as $k => &$v) {
187 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
188 foreach (array("profile", "avatar", "micro") as $k) {
189 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
193 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
194 // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
195 $contact["avatar-date"] = NULL_DATE;
197 switch ($contact['network']) {
199 case NETWORK_DIASPORA:
200 // send relocate message (below)
207 // archive other contacts
208 $contact['archive'] = "1";
211 $contact['uid'] = $newuid;
212 $r = db_import_assoc('contact', $contact);
214 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
217 $contact['newid'] = last_insert_id();
220 if ($errorcount > 0) {
221 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
224 foreach ($account['group'] as &$group) {
225 $group['uid'] = $newuid;
226 $r = db_import_assoc('group', $group);
228 logger("uimport:insert group " . $group['name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
230 $group['newid'] = last_insert_id();
234 foreach ($account['group_member'] as &$group_member) {
235 $group_member['uid'] = $newuid;
238 foreach ($account['group'] as $group) {
239 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
240 $group_member['gid'] = $group['newid'];
245 foreach ($account['contact'] as $contact) {
246 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
247 $group_member['contact-id'] = $contact['newid'];
253 $r = db_import_assoc('group_member', $group_member);
255 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
260 foreach ($account['photo'] as &$photo) {
261 $photo['uid'] = $newuid;
262 $photo['data'] = hex2bin($photo['data']);
264 $Image = new Image($photo['data'], $photo['type']);
267 $photo['uid'], $photo['contact-id'], //0
268 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
269 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
273 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
277 foreach ($account['pconfig'] as &$pconfig) {
278 $pconfig['uid'] = $newuid;
279 $r = db_import_assoc('pconfig', $pconfig);
281 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
285 // send relocate messages
286 Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
288 info(t("Done. You can now login with your username and password"));
289 goaway(System::baseUrl() . "/login");