5 require_once("include/Photo.php");
6 define("IMPORT_DEBUG", False);
8 function last_insert_id() {
13 return dba::lastInsertId();
16 function last_error() {
22 * Remove columns from array $arr that aren't in table $table
24 * @param string $table Table name
25 * @param array &$arr Column=>Value array from json (by ref)
27 function check_cols($table, &$arr) {
28 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
29 logger("uimport: $query", LOGGER_DEBUG);
32 // get a plain array of column names
33 foreach ($r as $tcol) {
34 $tcols[] = $tcol['Field'];
36 // remove inexistent columns
37 foreach ($arr as $icol => $ival) {
38 if (!in_array($icol, $tcols)) {
45 * Import data into table $table
47 * @param string $table Table name
48 * @param array $arr Column=>Value array from json
50 function db_import_assoc($table, $arr) {
51 if (isset($arr['id']))
53 check_cols($table, $arr);
54 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
55 $vals = implode("','", array_map('dbesc', array_values($arr)));
56 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
57 logger("uimport: $query", LOGGER_TRACE);
65 * @brief Import account file exported from mod/uexport
67 * @param App $a Friendica App Class
68 * @param array $file array from $_FILES
70 function import_account(App $a, $file) {
71 logger("Start user import from " . $file['tmp_name']);
75 2. replace old baseurl with new baseurl
76 3. import data (look at user id and contacts id)
77 4. archive non-dfrn contacts
78 5. send message to dfrn contacts
81 $account = json_decode(file_get_contents($file['tmp_name']), true);
82 if ($account === null) {
83 notice(t("Error decoding account file"));
88 if (!x($account, 'version')) {
89 notice(t("Error! No version data in file! This is not a Friendica account file?"));
94 * @TODO Old-lost code?
95 // this is not required as we remove columns in json not in current db schema
96 if ($account['schema'] != DB_UPDATE_VERSION) {
97 notice(t("Error! I can't import this file: DB schema version is not compatible."));
102 // check for username
103 $r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
105 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
106 notice(t('Error! Cannot check nickname'));
109 if (dbm::is_result($r) > 0) {
110 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
113 // check if username matches deleted account
114 $r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
116 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
117 notice(t('Error! Cannot check nickname'));
120 if (dbm::is_result($r) > 0) {
121 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
125 $oldbaseurl = $account['baseurl'];
126 $newbaseurl = App::get_baseurl();
127 $olduid = $account['user']['uid'];
129 unset($account['user']['uid']);
130 unset($account['user']['account_expired']);
131 unset($account['user']['account_expires_on']);
132 unset($account['user']['expire_notification_sent']);
134 foreach ($account['user'] as $k => &$v) {
135 $v = str_replace($oldbaseurl, $newbaseurl, $v);
139 $r = db_import_assoc('user', $account['user']);
141 //echo "<pre>"; var_dump($r, $query, mysql_error()); killme();
142 logger("uimport:insert user : ERROR : " . last_error(), LOGGER_NORMAL);
143 notice(t("User creation error"));
146 $newuid = last_insert_id();
149 // Generate a new guid for the account. Otherwise there will be problems with diaspora
150 q("UPDATE `user` SET `guid` = '%s' WHERE `uid` = %d",
151 dbesc(generate_user_guid()), intval($newuid));
153 foreach ($account['profile'] as &$profile) {
154 foreach ($profile as $k => &$v) {
155 $v = str_replace($oldbaseurl, $newbaseurl, $v);
156 foreach (array("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 = db_import_assoc('profile', $profile);
163 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
164 info(t("User profile creation error"));
165 dba::delete('user', array('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, $newbaseurl, $v);
175 foreach (array("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 poller to update urls
182 $contact["avatar-date"] = NULL_DATE;
184 switch ($contact['network']) {
186 // send relocate message (below)
189 /// @TODO handle zot network
199 // archive other contacts
200 $contact['archive'] = "1";
203 $contact['uid'] = $newuid;
204 $r = db_import_assoc('contact', $contact);
206 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
209 $contact['newid'] = last_insert_id();
212 if ($errorcount > 0) {
213 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
216 foreach ($account['group'] as &$group) {
217 $group['uid'] = $newuid;
218 $r = db_import_assoc('group', $group);
220 logger("uimport:insert group " . $group['name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
222 $group['newid'] = last_insert_id();
226 foreach ($account['group_member'] as &$group_member) {
227 $group_member['uid'] = $newuid;
230 foreach ($account['group'] as $group) {
231 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
232 $group_member['gid'] = $group['newid'];
237 foreach ($account['contact'] as $contact) {
238 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
239 $group_member['contact-id'] = $contact['newid'];
245 $r = db_import_assoc('group_member', $group_member);
247 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
252 foreach ($account['photo'] as &$photo) {
253 $photo['uid'] = $newuid;
254 $photo['data'] = hex2bin($photo['data']);
256 $p = new Photo($photo['data'], $photo['type']);
258 $photo['uid'], $photo['contact-id'], //0
259 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
260 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
264 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
268 foreach ($account['pconfig'] as &$pconfig) {
269 $pconfig['uid'] = $newuid;
270 $r = db_import_assoc('pconfig', $pconfig);
272 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
276 // send relocate messages
277 proc_run(PRIORITY_HIGH, 'include/notifier.php', 'relocate', $newuid);
279 info(t("Done. You can now login with your username and password"));
280 goaway(App::get_baseurl() . "/login");