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
17 $thedb = $db->getdb();
\r
18 return $thedb->insert_id;
\r
20 return mysql_insert_id();
\r
24 function last_error() {
\r
30 * Remove columns from array $arr that aren't in table $table
\r
32 * @param string $table Table name
\r
33 * @param array &$arr Column=>Value array from json (by ref)
\r
35 function check_cols($table, &$arr) {
\r
36 $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
\r
37 logger("uimport: $query", LOGGER_DEBUG);
\r
40 // get a plain array of column names
\r
41 foreach ($r as $tcol) {
\r
42 $tcols[] = $tcol['Field'];
\r
44 // remove inexistent columns
\r
45 foreach ($arr as $icol => $ival) {
\r
46 if (!in_array($icol, $tcols)) {
\r
53 * Import data into table $table
\r
55 * @param string $table Table name
\r
56 * @param array $arr Column=>Value array from json
\r
58 function db_import_assoc($table, $arr) {
\r
59 if (isset($arr['id']))
\r
61 check_cols($table, $arr);
\r
62 $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
\r
63 $vals = implode("','", array_map('dbesc', array_values($arr)));
\r
64 $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
\r
65 logger("uimport: $query", LOGGER_TRACE);
\r
71 function import_cleanup($newuid) {
\r
72 q("DELETE FROM `user` WHERE uid = %d", $newuid);
\r
73 q("DELETE FROM `contact` WHERE uid = %d", $newuid);
\r
74 q("DELETE FROM `profile` WHERE uid = %d", $newuid);
\r
75 q("DELETE FROM `photo` WHERE uid = %d", $newuid);
\r
76 q("DELETE FROM `group` WHERE uid = %d", $newuid);
\r
77 q("DELETE FROM `group_member` WHERE uid = %d", $newuid);
\r
78 q("DELETE FROM `pconfig` WHERE uid = %d", $newuid);
\r
81 function import_account(App $a, $file) {
\r
82 logger("Start user import from " . $file['tmp_name']);
\r
86 2. replace old baseurl with new baseurl
\r
87 3. import data (look at user id and contacts id)
\r
88 4. archive non-dfrn contacts
\r
89 5. send message to dfrn contacts
\r
92 $account = json_decode(file_get_contents($file['tmp_name']), true);
\r
93 if ($account === null) {
\r
94 notice(t("Error decoding account file"));
\r
99 if (!x($account, 'version')) {
\r
100 notice(t("Error! No version data in file! This is not a Friendica account file?"));
\r
105 // this is not required as we remove columns in json not in current db schema
\r
106 if ($account['schema'] != DB_UPDATE_VERSION) {
\r
107 notice(t("Error! I can't import this file: DB schema version is not compatible."));
\r
112 // check for username
\r
113 $r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
\r
114 if ($r === false) {
\r
115 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
\r
116 notice(t('Error! Cannot check nickname'));
\r
119 if (dbm::is_result($r) > 0) {
\r
120 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
\r
123 // check if username matches deleted account
\r
124 $r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
\r
125 if ($r === false) {
\r
126 logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
\r
127 notice(t('Error! Cannot check nickname'));
\r
130 if (dbm::is_result($r) > 0) {
\r
131 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
\r
135 $oldbaseurl = $account['baseurl'];
\r
136 $newbaseurl = App::get_baseurl();
\r
137 $olduid = $account['user']['uid'];
\r
139 unset($account['user']['uid']);
\r
140 unset($account['user']['account_expired']);
\r
141 unset($account['user']['account_expires_on']);
\r
142 unset($account['user']['expire_notification_sent']);
\r
143 foreach ($account['user'] as $k => &$v) {
\r
144 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
149 $r = db_import_assoc('user', $account['user']);
\r
150 if ($r === false) {
\r
151 //echo "<pre>"; var_dump($r, $query, mysql_error()); killme();
\r
152 logger("uimport:insert user : ERROR : " . last_error(), LOGGER_NORMAL);
\r
153 notice(t("User creation error"));
\r
156 $newuid = last_insert_id();
\r
159 // Generate a new guid for the account. Otherwise there will be problems with diaspora
\r
160 q("UPDATE `user` SET `guid` = '%s' WHERE `uid` = %d",
\r
161 dbesc(generate_user_guid()), intval($newuid));
\r
163 foreach ($account['profile'] as &$profile) {
\r
164 foreach ($profile as $k => &$v) {
\r
165 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
166 foreach (array("profile", "avatar") as $k)
\r
167 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
\r
169 $profile['uid'] = $newuid;
\r
170 $r = db_import_assoc('profile', $profile);
\r
171 if ($r === false) {
\r
172 logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
173 info(t("User profile creation error"));
\r
174 import_cleanup($newuid);
\r
180 foreach ($account['contact'] as &$contact) {
\r
181 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
\r
182 foreach ($contact as $k => &$v) {
\r
183 $v = str_replace($oldbaseurl, $newbaseurl, $v);
\r
184 foreach (array("profile", "avatar", "micro") as $k)
\r
185 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
\r
188 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
\r
189 // set contacts 'avatar-date' to "0000-00-00 00:00:00" to let poller to update urls
\r
190 $contact["avatar-date"] = "0000-00-00 00:00:00" ;
\r
193 switch ($contact['network']) {
\r
195 // send relocate message (below)
\r
198 /// @TODO handle zot network
\r
200 case NETWORK_MAIL2:
\r
208 // archive other contacts
\r
209 $contact['archive'] = "1";
\r
212 $contact['uid'] = $newuid;
\r
213 $r = db_import_assoc('contact', $contact);
\r
214 if ($r === false) {
\r
215 logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
218 $contact['newid'] = last_insert_id();
\r
221 if ($errorcount > 0) {
\r
222 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
\r
225 foreach ($account['group'] as &$group) {
\r
226 $group['uid'] = $newuid;
\r
227 $r = db_import_assoc('group', $group);
\r
228 if ($r === false) {
\r
229 logger("uimport:insert group " . $group['name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
231 $group['newid'] = last_insert_id();
\r
235 foreach ($account['group_member'] as &$group_member) {
\r
236 $group_member['uid'] = $newuid;
\r
239 foreach ($account['group'] as $group) {
\r
240 if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
\r
241 $group_member['gid'] = $group['newid'];
\r
246 foreach ($account['contact'] as $contact) {
\r
247 if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
\r
248 $group_member['contact-id'] = $contact['newid'];
\r
253 if ($import == 2) {
\r
254 $r = db_import_assoc('group_member', $group_member);
\r
255 if ($r === false) {
\r
256 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
265 foreach ($account['photo'] as &$photo) {
\r
266 $photo['uid'] = $newuid;
\r
267 $photo['data'] = hex2bin($photo['data']);
\r
269 $p = new Photo($photo['data'], $photo['type']);
\r
271 $photo['uid'], $photo['contact-id'], //0
\r
272 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
\r
273 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
\r
276 if ($r === false) {
\r
277 logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
281 foreach ($account['pconfig'] as &$pconfig) {
\r
282 $pconfig['uid'] = $newuid;
\r
283 $r = db_import_assoc('pconfig', $pconfig);
\r
284 if ($r === false) {
\r
285 logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
\r
289 // send relocate messages
\r
290 proc_run(PRIORITY_HIGH, 'include/notifier.php', 'relocate', $newuid);
\r
292 info(t("Done. You can now login with your username and password"));
\r
293 goaway(App::get_baseurl() . "/login");
\r