]> git.mxchange.org Git - friendica.git/blob - include/uimport.php
Revert unneeded changes in third-party libraries
[friendica.git] / include / uimport.php
1 <?php\r
2 \r
3 /**\r
4  * import account file exported from mod/uexport\r
5  * args:\r
6  *  $a       App     Friendica App Class\r
7  *  $file   Array   array from $_FILES\r
8  */\r
9 require_once("include/Photo.php");\r
10 define("IMPORT_DEBUG", False);\r
11 \r
12 function last_insert_id() {\r
13         global $db;\r
14 \r
15         if (IMPORT_DEBUG)\r
16                 return 1;\r
17 \r
18         return $db->insert_id();\r
19 }\r
20 \r
21 function last_error() {\r
22         global $db;\r
23         return $db->error;\r
24 }\r
25 \r
26 /**\r
27  * Remove columns from array $arr that aren't in table $table\r
28  *\r
29  * @param string $table Table name\r
30  * @param array &$arr Column=>Value array from json (by ref)\r
31  */\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
35         $r = q($query);\r
36         $tcols = array();\r
37         // get a plain array of column names\r
38         foreach ($r as $tcol) {\r
39                 $tcols[] = $tcol['Field'];\r
40         }\r
41         // remove inexistent columns\r
42         foreach ($arr as $icol => $ival) {\r
43                 if (!in_array($icol, $tcols)) {\r
44                         unset($arr[$icol]);\r
45                 }\r
46         }\r
47 }\r
48 \r
49 /**\r
50  * Import data into table $table\r
51  *\r
52  * @param string $table Table name\r
53  * @param array $arr Column=>Value array from json\r
54  */\r
55 function db_import_assoc($table, $arr) {\r
56         if (isset($arr['id']))\r
57                 unset($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
63         if (IMPORT_DEBUG)\r
64                 return true;\r
65         return q($query);\r
66 }\r
67 \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
76 }\r
77 \r
78 function import_account(App $a, $file) {\r
79         logger("Start user import from " . $file['tmp_name']);\r
80         /*\r
81           STEPS\r
82           1. checks\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
87          */\r
88 \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
92                 return;\r
93         }\r
94 \r
95 \r
96         if (!x($account, 'version')) {\r
97                 notice(t("Error! No version data in file! This is not a Friendica account file?"));\r
98                 return;\r
99         }\r
100 \r
101         /*\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
105                 return;\r
106         }\r
107         */\r
108 \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
114                 return;\r
115         }\r
116         if (dbm::is_result($r) > 0) {\r
117                 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));\r
118                 return;\r
119         }\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
125                 return;\r
126         }\r
127         if (dbm::is_result($r) > 0) {\r
128                 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));\r
129                 return;\r
130         }\r
131 \r
132         $oldbaseurl = $account['baseurl'];\r
133         $newbaseurl = App::get_baseurl();\r
134         $olduid = $account['user']['uid'];\r
135 \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
142         }\r
143 \r
144 \r
145         // import user\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
151                 return;\r
152         }\r
153         $newuid = last_insert_id();\r
154         //~ $newuid = 1;\r
155 \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
159 \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
165                 }\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
172                         return;\r
173                 }\r
174         }\r
175 \r
176         $errorcount = 0;\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
183                         }\r
184                 }\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
188 \r
189 \r
190                         switch ($contact['network']) {\r
191                                 case NETWORK_DFRN:\r
192                                         //  send relocate message (below)\r
193                                         break;\r
194                                 case NETWORK_ZOT:\r
195                                         /// @TODO handle zot network\r
196                                         break;\r
197                                 case NETWORK_MAIL2:\r
198                                         /// @TODO ?\r
199                                         break;\r
200                                 case NETWORK_FEED:\r
201                                 case NETWORK_MAIL:\r
202                                         // Nothing to do\r
203                                         break;\r
204                                 default:\r
205                                         // archive other contacts\r
206                                         $contact['archive'] = "1";\r
207                         }\r
208                 }\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
213                         $errorcount++;\r
214                 } else {\r
215                         $contact['newid'] = last_insert_id();\r
216                 }\r
217         }\r
218         if ($errorcount > 0) {\r
219                 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));\r
220         }\r
221 \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
227                 } else {\r
228                         $group['newid'] = last_insert_id();\r
229                 }\r
230         }\r
231 \r
232         foreach ($account['group_member'] as &$group_member) {\r
233                 $group_member['uid'] = $newuid;\r
234 \r
235                 $import = 0;\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
239                                 $import++;\r
240                                 break;\r
241                         }\r
242                 }\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
246                                 $import++;\r
247                                 break;\r
248                         }\r
249                 }\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
254                         }\r
255                 }\r
256         }\r
257 \r
258 \r
259 \r
260 \r
261 \r
262         foreach ($account['photo'] as &$photo) {\r
263                 $photo['uid'] = $newuid;\r
264                 $photo['data'] = hex2bin($photo['data']);\r
265 \r
266                 $p = new Photo($photo['data'], $photo['type']);\r
267                 $r = $p->store(\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
271                 );\r
272 \r
273                 if ($r === false) {\r
274                         logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);\r
275                 }\r
276         }\r
277 \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
283                 }\r
284         }\r
285 \r
286         // send relocate messages\r
287         proc_run(PRIORITY_HIGH, 'include/notifier.php', 'relocate', $newuid);\r
288 \r
289         info(t("Done. You can now login with your username and password"));\r
290         goaway(App::get_baseurl() . "/login");\r
291 }\r