4 * import account file exported from mod/uexport
\r
6 * $a App Friendica App Class
\r
7 * $file Array array from $_FILES
\r
9 require_once("include/Photo.php");
\r
10 define("IMPORT_DEBUG", False);
\r
12 function last_insert_id() {
\r
18 return $db->insert_id();
\r
21 function last_error() {
\r
27 * Remove columns from array $arr that aren't in table $table
\r
29 * @param string $table Table name
\r
30 * @param array &$arr Column=>Value array from json (by ref)
\r
32 function check_cols($table, &$arr) {
\r
33 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
\r
34 logger("uimport: $query", LOGGER_DEBUG);
\r
37 // get a plain array of column names
\r
38 foreach ($r as $tcol) {
\r
39 $tcols[] = $tcol['Field'];
\r
41 // remove inexistent columns
\r
42 foreach ($arr as $icol => $ival) {
\r
43 if (!in_array($icol, $tcols)) {
\r
50 * Import data into table $table
\r
52 * @param string $table Table name
\r
53 * @param array $arr Column=>Value array from json
\r
55 function db_import_assoc($table, $arr) {
\r
56 if (isset($arr['id']))
\r
58 check_cols($table, $arr);
\r
59 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
\r
60 $vals = implode("','", array_map('dbesc', array_values($arr)));
\r
61 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
\r
62 logger("uimport: $query", LOGGER_TRACE);
\r
68 function import_cleanup($newuid) {
\r
69 q("DELETE FROM `user` WHERE uid = %d", $newuid);
\r
70 q("DELETE FROM `contact` WHERE uid = %d", $newuid);
\r
71 q("DELETE FROM `profile` WHERE uid = %d", $newuid);
\r
72 q("DELETE FROM `photo` WHERE uid = %d", $newuid);
\r
73 q("DELETE FROM `group` WHERE uid = %d", $newuid);
\r
74 q("DELETE FROM `group_member` WHERE uid = %d", $newuid);
\r
75 q("DELETE FROM `pconfig` WHERE uid = %d", $newuid);
\r
78 function import_account(App $a, $file) {
\r
79 logger("Start user import from " . $file['tmp_name']);
\r
83 2. replace old baseurl with new baseurl
\r
84 3. import data (look at user id and contacts id)
\r
85 4. archive non-dfrn contacts
\r
86 5. send message to dfrn contacts
\r
89 $account = json_decode(file_get_contents($file['tmp_name']), true);
\r
90 if ($account === null) {
\r
91 notice(t("Error decoding account file"));
\r
96 if (!x($account, 'version')) {
\r
97 notice(t("Error! No version data in file! This is not a Friendica account file?"));
\r
102 // this is not required as we remove columns in json not in current db schema
\r
103 if ($account['schema'] != DB_UPDATE_VERSION) {
\r
104 notice(t("Error! I can't import this file: DB schema version is not compatible."));
\r
109 // check for username
\r
110 $r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
\r
111 if ($r === false) {
\r
112 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
\r
113 notice(t('Error! Cannot check nickname'));
\r
116 if (dbm::is_result($r) > 0) {
\r
117 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
\r
120 // check if username matches deleted account
\r
121 $r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
\r
122 if ($r === false) {
\r
123 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
\r
124 notice(t('Error! Cannot check nickname'));
\r
127 if (dbm::is_result($r) > 0) {
\r
128 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
\r
132 $oldbaseurl = $account['baseurl'];
\r
133 $newbaseurl = App::get_baseurl();
\r
134 $olduid = $account['user']['uid'];
\r
136 unset($account['user']['uid']);
\r
137 unset($account['user']['account_expired']);
\r
138 unset($account['user']['account_expires_on']);
\r
139 unset($account['user']['expire_notification_sent']);
\r
140 foreach ($account['user'] as $k => &$v) {
\r
141 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
146 $r = db_import_assoc('user', $account['user']);
\r
147 if ($r === false) {
\r
148 //echo "<pre>"; var_dump($r, $query, mysql_error()); killme();
\r
149 logger("uimport:insert user : ERROR : " . last_error(), LOGGER_NORMAL);
\r
150 notice(t("User creation error"));
\r
153 $newuid = last_insert_id();
\r
156 // Generate a new guid for the account. Otherwise there will be problems with diaspora
\r
157 q("UPDATE `user` SET `guid` = '%s' WHERE `uid` = %d",
\r
158 dbesc(generate_user_guid()), intval($newuid));
\r
160 foreach ($account['profile'] as &$profile) {
\r
161 foreach ($profile as $k => &$v) {
\r
162 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
163 foreach (array("profile", "avatar") as $k)
\r
164 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
\r
166 $profile['uid'] = $newuid;
\r
167 $r = db_import_assoc('profile', $profile);
\r
168 if ($r === false) {
\r
169 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
170 info(t("User profile creation error"));
\r
171 import_cleanup($newuid);
\r
177 foreach ($account['contact'] as &$contact) {
\r
178 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
\r
179 foreach ($contact as $k => &$v) {
\r
180 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
181 foreach (array("profile", "avatar", "micro") as $k)
\r
182 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
\r
185 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
\r
186 // set contacts 'avatar-date' to NULL_DATE to let poller to update urls
\r
187 $contact["avatar-date"] = NULL_DATE;
\r
190 switch ($contact['network']) {
\r
192 // send relocate message (below)
\r
195 /// @TODO handle zot network
\r
197 case NETWORK_MAIL2:
\r
205 // archive other contacts
\r
206 $contact['archive'] = "1";
\r
209 $contact['uid'] = $newuid;
\r
210 $r = db_import_assoc('contact', $contact);
\r
211 if ($r === false) {
\r
212 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
215 $contact['newid'] = last_insert_id();
\r
218 if ($errorcount > 0) {
\r
219 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
\r
222 foreach ($account['group'] as &$group) {
\r
223 $group['uid'] = $newuid;
\r
224 $r = db_import_assoc('group', $group);
\r
225 if ($r === false) {
\r
226 logger("uimport:insert group " . $group['name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
228 $group['newid'] = last_insert_id();
\r
232 foreach ($account['group_member'] as &$group_member) {
\r
233 $group_member['uid'] = $newuid;
\r
236 foreach ($account['group'] as $group) {
\r
237 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
\r
238 $group_member['gid'] = $group['newid'];
\r
243 foreach ($account['contact'] as $contact) {
\r
244 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
\r
245 $group_member['contact-id'] = $contact['newid'];
\r
250 if ($import == 2) {
\r
251 $r = db_import_assoc('group_member', $group_member);
\r
252 if ($r === false) {
\r
253 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
262 foreach ($account['photo'] as &$photo) {
\r
263 $photo['uid'] = $newuid;
\r
264 $photo['data'] = hex2bin($photo['data']);
\r
266 $p = new Photo($photo['data'], $photo['type']);
\r
268 $photo['uid'], $photo['contact-id'], //0
\r
269 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
\r
270 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
\r
273 if ($r === false) {
\r
274 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
278 foreach ($account['pconfig'] as &$pconfig) {
\r
279 $pconfig['uid'] = $newuid;
\r
280 $r = db_import_assoc('pconfig', $pconfig);
\r
281 if ($r === false) {
\r
282 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
286 // send relocate messages
\r
287 proc_run(PRIORITY_HIGH, 'include/notifier.php', 'relocate', $newuid);
\r
289 info(t("Done. You can now login with your username and password"));
\r
290 goaway(App::get_baseurl() . "/login");
\r