]> git.mxchange.org Git - friendica.git/blob - include/uimport.php
0ee15952af56ae0ca0d22312eaef250f0c356b47
[friendica.git] / include / uimport.php
1 <?php\r
2 \r
3 use Friendica\App;\r
4 \r
5 require_once("include/Photo.php");\r
6 define("IMPORT_DEBUG", False);\r
7 \r
8 function last_insert_id() {\r
9         global $db;\r
10 \r
11         if (IMPORT_DEBUG)\r
12                 return 1;\r
13 \r
14         return $db->insert_id();\r
15 }\r
16 \r
17 function last_error() {\r
18         global $db;\r
19         return $db->error;\r
20 }\r
21 \r
22 /**\r
23  * Remove columns from array $arr that aren't in table $table\r
24  *\r
25  * @param string $table Table name\r
26  * @param array &$arr Column=>Value array from json (by ref)\r
27  */\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
31         $r = q($query);\r
32         $tcols = array();\r
33         // get a plain array of column names\r
34         foreach ($r as $tcol) {\r
35                 $tcols[] = $tcol['Field'];\r
36         }\r
37         // remove inexistent columns\r
38         foreach ($arr as $icol => $ival) {\r
39                 if (!in_array($icol, $tcols)) {\r
40                         unset($arr[$icol]);\r
41                 }\r
42         }\r
43 }\r
44 \r
45 /**\r
46  * Import data into table $table\r
47  *\r
48  * @param string $table Table name\r
49  * @param array $arr Column=>Value array from json\r
50  */\r
51 function db_import_assoc($table, $arr) {\r
52         if (isset($arr['id']))\r
53                 unset($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
59         if (IMPORT_DEBUG)\r
60                 return true;\r
61         return q($query);\r
62 }\r
63 \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
72 }\r
73 \r
74 /**\r
75  * @brief Import account file exported from mod/uexport\r
76  *\r
77  * @param App $a Friendica App Class\r
78  * @param array $file array from $_FILES\r
79  */\r
80 function import_account(App $a, $file) {\r
81         logger("Start user import from " . $file['tmp_name']);\r
82         /*\r
83           STEPS\r
84           1. checks\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
89          */\r
90 \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
94                 return;\r
95         }\r
96 \r
97 \r
98         if (!x($account, 'version')) {\r
99                 notice(t("Error! No version data in file! This is not a Friendica account file?"));\r
100                 return;\r
101         }\r
102 \r
103         /*\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
107                 return;\r
108         }\r
109         */\r
110 \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
116                 return;\r
117         }\r
118         if (dbm::is_result($r) > 0) {\r
119                 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));\r
120                 return;\r
121         }\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
127                 return;\r
128         }\r
129         if (dbm::is_result($r) > 0) {\r
130                 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));\r
131                 return;\r
132         }\r
133 \r
134         $oldbaseurl = $account['baseurl'];\r
135         $newbaseurl = App::get_baseurl();\r
136         $olduid = $account['user']['uid'];\r
137 \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
144         }\r
145 \r
146 \r
147         // import user\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
153                 return;\r
154         }\r
155         $newuid = last_insert_id();\r
156         //~ $newuid = 1;\r
157 \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
161 \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
167                 }\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
174                         return;\r
175                 }\r
176         }\r
177 \r
178         $errorcount = 0;\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
185                         }\r
186                 }\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
190 \r
191 \r
192                         switch ($contact['network']) {\r
193                                 case NETWORK_DFRN:\r
194                                         //  send relocate message (below)\r
195                                         break;\r
196                                 case NETWORK_ZOT:\r
197                                         /// @TODO handle zot network\r
198                                         break;\r
199                                 case NETWORK_MAIL2:\r
200                                         /// @TODO ?\r
201                                         break;\r
202                                 case NETWORK_FEED:\r
203                                 case NETWORK_MAIL:\r
204                                         // Nothing to do\r
205                                         break;\r
206                                 default:\r
207                                         // archive other contacts\r
208                                         $contact['archive'] = "1";\r
209                         }\r
210                 }\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
215                         $errorcount++;\r
216                 } else {\r
217                         $contact['newid'] = last_insert_id();\r
218                 }\r
219         }\r
220         if ($errorcount > 0) {\r
221                 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));\r
222         }\r
223 \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
229                 } else {\r
230                         $group['newid'] = last_insert_id();\r
231                 }\r
232         }\r
233 \r
234         foreach ($account['group_member'] as &$group_member) {\r
235                 $group_member['uid'] = $newuid;\r
236 \r
237                 $import = 0;\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
241                                 $import++;\r
242                                 break;\r
243                         }\r
244                 }\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
248                                 $import++;\r
249                                 break;\r
250                         }\r
251                 }\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
256                         }\r
257                 }\r
258         }\r
259 \r
260         foreach ($account['photo'] as &$photo) {\r
261                 $photo['uid'] = $newuid;\r
262                 $photo['data'] = hex2bin($photo['data']);\r
263 \r
264                 $p = new Photo($photo['data'], $photo['type']);\r
265                 $r = $p->store(\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
269                 );\r
270 \r
271                 if ($r === false) {\r
272                         logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);\r
273                 }\r
274         }\r
275 \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
281                 }\r
282         }\r
283 \r
284         // send relocate messages\r
285         proc_run(PRIORITY_HIGH, 'include/notifier.php', 'relocate', $newuid);\r
286 \r
287         info(t("Done. You can now login with your username and password"));\r
288         goaway(App::get_baseurl() . "/login");\r
289 }\r