]> git.mxchange.org Git - friendica.git/blob - include/uimport.php
Zot was never really supported, so it is now removed completely
[friendica.git] / include / uimport.php
1 <?php
2 /**
3  * @file include/uimport.php
4  */
5 use Friendica\App;
6 use Friendica\Core\System;
7 use Friendica\Core\PConfig;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBM;
10 use Friendica\Object\Photo;
11
12 define("IMPORT_DEBUG", False);
13
14 function last_insert_id() {
15         if (IMPORT_DEBUG) {
16                 return 1;
17         }
18
19         return dba::lastInsertId();
20 }
21
22 /**
23  * Remove columns from array $arr that aren't in table $table
24  *
25  * @param string $table Table name
26  * @param array &$arr Column=>Value array from json (by ref)
27  */
28 function check_cols($table, &$arr) {
29         $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
30         logger("uimport: $query", LOGGER_DEBUG);
31         $r = q($query);
32         $tcols = array();
33         // get a plain array of column names
34         foreach ($r as $tcol) {
35                 $tcols[] = $tcol['Field'];
36         }
37         // remove inexistent columns
38         foreach ($arr as $icol => $ival) {
39                 if (!in_array($icol, $tcols)) {
40                         unset($arr[$icol]);
41                 }
42         }
43 }
44
45 /**
46  * Import data into table $table
47  *
48  * @param string $table Table name
49  * @param array $arr Column=>Value array from json
50  */
51 function db_import_assoc($table, $arr) {
52         if (isset($arr['id']))
53                 unset($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);
59         if (IMPORT_DEBUG) {
60                 return true;
61         }
62         return q($query);
63 }
64
65 /**
66  * @brief Import account file exported from mod/uexport
67  *
68  * @param App $a Friendica App Class
69  * @param array $file array from $_FILES
70  */
71 function import_account(App $a, $file) {
72         logger("Start user import from " . $file['tmp_name']);
73         /*
74           STEPS
75           1. checks
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
80          */
81
82         $account = json_decode(file_get_contents($file['tmp_name']), true);
83         if ($account === null) {
84                 notice(t("Error decoding account file"));
85                 return;
86         }
87
88
89         if (!x($account, 'version')) {
90                 notice(t("Error! No version data in file! This is not a Friendica account file?"));
91                 return;
92         }
93
94         /*
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."));
99                 return;
100         }
101         */
102
103         // check for username
104         $r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
105         if ($r === false) {
106                 logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
107                 notice(t('Error! Cannot check nickname'));
108                 return;
109         }
110         if (DBM::is_result($r) > 0) {
111                 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
112                 return;
113         }
114         // check if username matches deleted account
115         $r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
116         if ($r === false) {
117                 logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
118                 notice(t('Error! Cannot check nickname'));
119                 return;
120         }
121         if (DBM::is_result($r) > 0) {
122                 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));
123                 return;
124         }
125
126         $oldbaseurl = $account['baseurl'];
127         $newbaseurl = System::baseUrl();
128
129         $oldaddr = str_replace('http://', '@', normalise_link($oldbaseurl));
130         $newaddr = str_replace('http://', '@', normalise_link($newbaseurl));
131
132         if (!empty($account['profile']['addr'])) {
133                 $old_handle = $account['profile']['addr'];
134         } else {
135                 $old_handle = $account['user']['nickname'].$oldaddr;
136         }
137
138         $olduid = $account['user']['uid'];
139
140         unset($account['user']['uid']);
141         unset($account['user']['account_expired']);
142         unset($account['user']['account_expires_on']);
143         unset($account['user']['expire_notification_sent']);
144
145         foreach ($account['user'] as $k => &$v) {
146                 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
147         }
148
149         // import user
150         $r = db_import_assoc('user', $account['user']);
151         if ($r === false) {
152                 logger("uimport:insert user : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
153                 notice(t("User creation error"));
154                 return;
155         }
156         $newuid = last_insert_id();
157         //~ $newuid = 1;
158
159         PConfig::set($newuid, 'system', 'previous_addr', $old_handle);
160
161         // Generate a new guid for the account. Otherwise there will be problems with diaspora
162         q("UPDATE `user` SET `guid` = '%s' WHERE `uid` = %d",
163                 dbesc(generate_user_guid()), intval($newuid));
164
165         foreach ($account['profile'] as &$profile) {
166                 foreach ($profile as $k => &$v) {
167                         $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
168                         foreach (array("profile", "avatar") as $k) {
169                                 $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
170                         }
171                 }
172                 $profile['uid'] = $newuid;
173                 $r = db_import_assoc('profile', $profile);
174                 if ($r === false) {
175                         logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
176                         info(t("User profile creation error"));
177                         dba::delete('user', array('uid' => $newuid));
178                         return;
179                 }
180         }
181
182         $errorcount = 0;
183         foreach ($account['contact'] as &$contact) {
184                 if ($contact['uid'] == $olduid && $contact['self'] == '1') {
185                         foreach ($contact as $k => &$v) {
186                                 $v = str_replace(array($oldbaseurl, $oldaddr), array($newbaseurl, $newaddr), $v);
187                                 foreach (array("profile", "avatar", "micro") as $k) {
188                                         $v = str_replace($oldbaseurl . "/photo/" . $k . "/" . $olduid . ".jpg", $newbaseurl . "/photo/" . $k . "/" . $newuid . ".jpg", $v);
189                                 }
190                         }
191                 }
192                 if ($contact['uid'] == $olduid && $contact['self'] == '0') {
193                         // set contacts 'avatar-date' to NULL_DATE to let worker to update urls
194                         $contact["avatar-date"] = NULL_DATE;
195
196                         switch ($contact['network']) {
197                                 case NETWORK_DFRN:
198                                 case NETWORK_DIASPORA:
199                                         //  send relocate message (below)
200                                         break;
201                                 case NETWORK_MAIL2:
202                                         /// @TODO ?
203                                         break;
204                                 case NETWORK_FEED:
205                                 case NETWORK_MAIL:
206                                         // Nothing to do
207                                         break;
208                                 default:
209                                         // archive other contacts
210                                         $contact['archive'] = "1";
211                         }
212                 }
213                 $contact['uid'] = $newuid;
214                 $r = db_import_assoc('contact', $contact);
215                 if ($r === false) {
216                         logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
217                         $errorcount++;
218                 } else {
219                         $contact['newid'] = last_insert_id();
220                 }
221         }
222         if ($errorcount > 0) {
223                 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));
224         }
225
226         foreach ($account['group'] as &$group) {
227                 $group['uid'] = $newuid;
228                 $r = db_import_assoc('group', $group);
229                 if ($r === false) {
230                         logger("uimport:insert group " . $group['name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
231                 } else {
232                         $group['newid'] = last_insert_id();
233                 }
234         }
235
236         foreach ($account['group_member'] as &$group_member) {
237                 $group_member['uid'] = $newuid;
238
239                 $import = 0;
240                 foreach ($account['group'] as $group) {
241                         if ($group['id'] == $group_member['gid'] && isset($group['newid'])) {
242                                 $group_member['gid'] = $group['newid'];
243                                 $import++;
244                                 break;
245                         }
246                 }
247                 foreach ($account['contact'] as $contact) {
248                         if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) {
249                                 $group_member['contact-id'] = $contact['newid'];
250                                 $import++;
251                                 break;
252                         }
253                 }
254                 if ($import == 2) {
255                         $r = db_import_assoc('group_member', $group_member);
256                         if ($r === false) {
257                                 logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
258                         }
259                 }
260         }
261
262         foreach ($account['photo'] as &$photo) {
263                 $photo['uid'] = $newuid;
264                 $photo['data'] = hex2bin($photo['data']);
265
266                 $p = new Photo($photo['data'], $photo['type']);
267                 $r = $p->store(
268                                 $photo['uid'], $photo['contact-id'], //0
269                                 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
270                                 $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
271                 );
272
273                 if ($r === false) {
274                         logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
275                 }
276         }
277
278         foreach ($account['pconfig'] as &$pconfig) {
279                 $pconfig['uid'] = $newuid;
280                 $r = db_import_assoc('pconfig', $pconfig);
281                 if ($r === false) {
282                         logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
283                 }
284         }
285
286         // send relocate messages
287         Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
288
289         info(t("Done. You can now login with your username and password"));
290         goaway(System::baseUrl() . "/login");
291 }