4 use Friendica\Core\System;
6 require_once("include/Photo.php");
7 define("IMPORT_DEBUG", False);
9 function last_insert_id() {
14 return dba::lastInsertId();
17 function last_error() {
23 * Remove columns from array $arr that aren't in table $table
25 * @param string $table Table name
26 * @param array &$arr Column=>Value array from json (by ref)
28 function check_cols($table, &$arr) {
29 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
30 logger("uimport: $query", LOGGER_DEBUG);
33 // get a plain array of column names
34 foreach ($r as $tcol) {
35 $tcols[] = $tcol['Field'];
37 // remove inexistent columns
38 foreach ($arr as $icol => $ival) {
39 if (!in_array($icol, $tcols)) {
46 * Import data into table $table
48 * @param string $table Table name
49 * @param array $arr Column=>Value array from json
51 function db_import_assoc($table, $arr) {
52 if (isset($arr['id']))
54 check_cols($table, $arr);
55 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
56 $vals = implode("','", array_map('dbesc', array_values($arr)));
57 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
58 logger("uimport: $query", LOGGER_TRACE);
66 * @brief Import account file exported from mod/uexport
68 * @param App $a Friendica App Class
69 * @param array $file array from $_FILES
71 function import_account(App $a, $file) {
72 logger("Start user import from " . $file['tmp_name']);
76 2. replace old baseurl with new baseurl
77 3. import data (look at user id and contacts id)
78 4. archive non-dfrn contacts
79 5. send message to dfrn contacts
82 $account = json_decode(file_get_contents($file['tmp_name']), true);
83 if ($account === null) {
84 notice(t("Error decoding account file"));
89 if (!x($account, 'version')) {
90 notice(t("Error! No version data in file! This is not a Friendica account file?"));
95 * @TODO Old-lost code?
96 // this is not required as we remove columns in json not in current db schema
97 if ($account['schema'] != DB_UPDATE_VERSION) {
98 notice(t("Error! I can't import this file: DB schema version is not compatible."));
103 // check for username
104 $r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
106 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
107 notice(t('Error! Cannot check nickname'));
110 if (dbm::is_result($r) > 0) {
111 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
114 // check if username matches deleted account
115 $r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
117 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
118 notice(t('Error! Cannot check nickname'));
121 if (dbm::is_result($r) > 0) {
122 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
126 $oldbaseurl = $account['baseurl'];
127 $newbaseurl = System::baseUrl();
129 $oldaddr = str_replace('http://', '@', normalise_link($oldbaseurl));
130 $newaddr = str_replace('http://', '@', normalise_link($newbaseurl));
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 foreach ($account['user'] as $k => &$v) {
140 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
144 $r = db_import_assoc('user', $account['user']);
146 //echo "<pre>"; var_dump($r, $query, mysql_error()); killme();
147 logger("uimport:insert user : ERROR : " . last_error(), LOGGER_NORMAL);
148 notice(t("User creation error"));
151 $newuid = last_insert_id();
154 // Generate a new guid for the account. Otherwise there will be problems with diaspora
155 q("UPDATE `user` SET `guid` = '%s' WHERE `uid` = %d",
156 dbesc(generate_user_guid()), intval($newuid));
158 foreach ($account['profile'] as &$profile) {
159 foreach ($profile as $k => &$v) {
160 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
161 foreach (array("profile", "avatar") as $k) {
162 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
165 $profile['uid'] = $newuid;
166 $r = db_import_assoc('profile', $profile);
168 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
169 info(t("User profile creation error"));
170 dba::delete('user', array('uid' => $newuid));
176 foreach ($account['contact'] as &$contact) {
177 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
178 foreach ($contact as $k => &$v) {
179 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
180 foreach (array("profile", "avatar", "micro") as $k) {
181 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
185 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
186 // set contacts 'avatar-date' to NULL_DATE to let poller to update urls
187 $contact["avatar-date"] = NULL_DATE;
189 switch ($contact['network']) {
191 // send relocate message (below)
194 /// @TODO handle zot network
204 // archive other contacts
205 $contact['archive'] = "1";
208 $contact['uid'] = $newuid;
209 $r = db_import_assoc('contact', $contact);
211 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
214 $contact['newid'] = last_insert_id();
217 if ($errorcount > 0) {
218 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
221 foreach ($account['group'] as &$group) {
222 $group['uid'] = $newuid;
223 $r = db_import_assoc('group', $group);
225 logger("uimport:insert group " . $group['name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
227 $group['newid'] = last_insert_id();
231 foreach ($account['group_member'] as &$group_member) {
232 $group_member['uid'] = $newuid;
235 foreach ($account['group'] as $group) {
236 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
237 $group_member['gid'] = $group['newid'];
242 foreach ($account['contact'] as $contact) {
243 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
244 $group_member['contact-id'] = $contact['newid'];
250 $r = db_import_assoc('group_member', $group_member);
252 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
257 foreach ($account['photo'] as &$photo) {
258 $photo['uid'] = $newuid;
259 $photo['data'] = hex2bin($photo['data']);
261 $p = new Photo($photo['data'], $photo['type']);
263 $photo['uid'], $photo['contact-id'], //0
264 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
265 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
269 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
273 foreach ($account['pconfig'] as &$pconfig) {
274 $pconfig['uid'] = $newuid;
275 $r = db_import_assoc('pconfig', $pconfig);
277 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
281 // send relocate messages
282 proc_run(PRIORITY_HIGH, 'include/notifier.php', 'relocate', $newuid);
284 info(t("Done. You can now login with your username and password"));
285 goaway(System::baseUrl() . "/login");