5 require_once("include/Photo.php");
\r
6 define("IMPORT_DEBUG", False);
\r
8 function last_insert_id() {
\r
14 return $db->insert_id();
\r
17 function last_error() {
\r
23 * Remove columns from array $arr that aren't in table $table
\r
25 * @param string $table Table name
\r
26 * @param array &$arr Column=>Value array from json (by ref)
\r
28 function check_cols($table, &$arr) {
\r
29 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
\r
30 logger("uimport: $query", LOGGER_DEBUG);
\r
33 // get a plain array of column names
\r
34 foreach ($r as $tcol) {
\r
35 $tcols[] = $tcol['Field'];
\r
37 // remove inexistent columns
\r
38 foreach ($arr as $icol => $ival) {
\r
39 if (!in_array($icol, $tcols)) {
\r
46 * Import data into table $table
\r
48 * @param string $table Table name
\r
49 * @param array $arr Column=>Value array from json
\r
51 function db_import_assoc($table, $arr) {
\r
52 if (isset($arr['id']))
\r
54 check_cols($table, $arr);
\r
55 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
\r
56 $vals = implode("','", array_map('dbesc', array_values($arr)));
\r
57 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
\r
58 logger("uimport: $query", LOGGER_TRACE);
\r
64 function import_cleanup($newuid) {
\r
65 q("DELETE FROM `user` WHERE uid = %d", $newuid);
\r
66 q("DELETE FROM `contact` WHERE uid = %d", $newuid);
\r
67 q("DELETE FROM `profile` WHERE uid = %d", $newuid);
\r
68 q("DELETE FROM `photo` WHERE uid = %d", $newuid);
\r
69 q("DELETE FROM `group` WHERE uid = %d", $newuid);
\r
70 q("DELETE FROM `group_member` WHERE uid = %d", $newuid);
\r
71 q("DELETE FROM `pconfig` WHERE uid = %d", $newuid);
\r
75 * @brief Import account file exported from mod/uexport
\r
77 * @param App $a Friendica App Class
\r
78 * @param array $file array from $_FILES
\r
80 function import_account(App $a, $file) {
\r
81 logger("Start user import from " . $file['tmp_name']);
\r
85 2. replace old baseurl with new baseurl
\r
86 3. import data (look at user id and contacts id)
\r
87 4. archive non-dfrn contacts
\r
88 5. send message to dfrn contacts
\r
91 $account = json_decode(file_get_contents($file['tmp_name']), true);
\r
92 if ($account === null) {
\r
93 notice(t("Error decoding account file"));
\r
98 if (!x($account, 'version')) {
\r
99 notice(t("Error! No version data in file! This is not a Friendica account file?"));
\r
104 // this is not required as we remove columns in json not in current db schema
\r
105 if ($account['schema'] != DB_UPDATE_VERSION) {
\r
106 notice(t("Error! I can't import this file: DB schema version is not compatible."));
\r
111 // check for username
\r
112 $r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
\r
113 if ($r === false) {
\r
114 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
\r
115 notice(t('Error! Cannot check nickname'));
\r
118 if (dbm::is_result($r) > 0) {
\r
119 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
\r
122 // check if username matches deleted account
\r
123 $r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
\r
124 if ($r === false) {
\r
125 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
\r
126 notice(t('Error! Cannot check nickname'));
\r
129 if (dbm::is_result($r) > 0) {
\r
130 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
\r
134 $oldbaseurl = $account['baseurl'];
\r
135 $newbaseurl = App::get_baseurl();
\r
136 $olduid = $account['user']['uid'];
\r
138 unset($account['user']['uid']);
\r
139 unset($account['user']['account_expired']);
\r
140 unset($account['user']['account_expires_on']);
\r
141 unset($account['user']['expire_notification_sent']);
\r
142 foreach ($account['user'] as $k => &$v) {
\r
143 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
148 $r = db_import_assoc('user', $account['user']);
\r
149 if ($r === false) {
\r
150 //echo "<pre>"; var_dump($r, $query, mysql_error()); killme();
\r
151 logger("uimport:insert user : ERROR : " . last_error(), LOGGER_NORMAL);
\r
152 notice(t("User creation error"));
\r
155 $newuid = last_insert_id();
\r
158 // Generate a new guid for the account. Otherwise there will be problems with diaspora
\r
159 q("UPDATE `user` SET `guid` = '%s' WHERE `uid` = %d",
\r
160 dbesc(generate_user_guid()), intval($newuid));
\r
162 foreach ($account['profile'] as &$profile) {
\r
163 foreach ($profile as $k => &$v) {
\r
164 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
165 foreach (array("profile", "avatar") as $k)
\r
166 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
\r
168 $profile['uid'] = $newuid;
\r
169 $r = db_import_assoc('profile', $profile);
\r
170 if ($r === false) {
\r
171 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
172 info(t("User profile creation error"));
\r
173 import_cleanup($newuid);
\r
179 foreach ($account['contact'] as &$contact) {
\r
180 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
\r
181 foreach ($contact as $k => &$v) {
\r
182 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
183 foreach (array("profile", "avatar", "micro") as $k)
\r
184 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
\r
187 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
\r
188 // set contacts 'avatar-date' to NULL_DATE to let poller to update urls
\r
189 $contact["avatar-date"] = NULL_DATE;
\r
192 switch ($contact['network']) {
\r
194 // send relocate message (below)
\r
197 /// @TODO handle zot network
\r
199 case NETWORK_MAIL2:
\r
207 // archive other contacts
\r
208 $contact['archive'] = "1";
\r
211 $contact['uid'] = $newuid;
\r
212 $r = db_import_assoc('contact', $contact);
\r
213 if ($r === false) {
\r
214 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
217 $contact['newid'] = last_insert_id();
\r
220 if ($errorcount > 0) {
\r
221 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
\r
224 foreach ($account['group'] as &$group) {
\r
225 $group['uid'] = $newuid;
\r
226 $r = db_import_assoc('group', $group);
\r
227 if ($r === false) {
\r
228 logger("uimport:insert group " . $group['name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
230 $group['newid'] = last_insert_id();
\r
234 foreach ($account['group_member'] as &$group_member) {
\r
235 $group_member['uid'] = $newuid;
\r
238 foreach ($account['group'] as $group) {
\r
239 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
\r
240 $group_member['gid'] = $group['newid'];
\r
245 foreach ($account['contact'] as $contact) {
\r
246 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
\r
247 $group_member['contact-id'] = $contact['newid'];
\r
252 if ($import == 2) {
\r
253 $r = db_import_assoc('group_member', $group_member);
\r
254 if ($r === false) {
\r
255 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
260 foreach ($account['photo'] as &$photo) {
\r
261 $photo['uid'] = $newuid;
\r
262 $photo['data'] = hex2bin($photo['data']);
\r
264 $p = new Photo($photo['data'], $photo['type']);
\r
266 $photo['uid'], $photo['contact-id'], //0
\r
267 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
\r
268 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
\r
271 if ($r === false) {
\r
272 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
276 foreach ($account['pconfig'] as &$pconfig) {
\r
277 $pconfig['uid'] = $newuid;
\r
278 $r = db_import_assoc('pconfig', $pconfig);
\r
279 if ($r === false) {
\r
280 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
284 // send relocate messages
\r
285 proc_run(PRIORITY_HIGH, 'include/notifier.php', 'relocate', $newuid);
\r
287 info(t("Done. You can now login with your username and password"));
\r
288 goaway(App::get_baseurl() . "/login");
\r