]> git.mxchange.org Git - friendica.git/blob - include/uimport.php
regenerated credits.txt
[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         if (IMPORT_DEBUG)\r
15                 return 1;\r
16         if ($db->mysqli) {\r
17                 $thedb = $db->getdb();\r
18                 return $thedb->insert_id;\r
19         } else {\r
20                 return mysql_insert_id();\r
21         }\r
22 }\r
23 \r
24 function last_error() {\r
25         global $db;\r
26         return $db->error;\r
27 }\r
28 \r
29 /**\r
30  * Remove columns from array $arr that aren't in table $table\r
31  *\r
32  * @param string $table Table name\r
33  * @param array &$arr Column=>Value array from json (by ref)\r
34  */\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
38         $r = q($query);\r
39         $tcols = array();\r
40         // get a plain array of column names\r
41         foreach ($r as $tcol) {\r
42                 $tcols[] = $tcol['Field'];\r
43         }\r
44         // remove inexistent columns\r
45         foreach ($arr as $icol => $ival) {\r
46                 if (!in_array($icol, $tcols)) {\r
47                         unset($arr[$icol]);\r
48                 }\r
49         }\r
50 }\r
51 \r
52 /**\r
53  * Import data into table $table\r
54  *\r
55  * @param string $table Table name\r
56  * @param array $arr Column=>Value array from json\r
57  */\r
58 function db_import_assoc($table, $arr) {\r
59         if (isset($arr['id']))\r
60                 unset($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
66         if (IMPORT_DEBUG)\r
67                 return true;\r
68         return q($query);\r
69 }\r
70 \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
79 }\r
80 \r
81 function import_account(&$a, $file) {\r
82         logger("Start user import from " . $file['tmp_name']);\r
83         /*\r
84           STEPS\r
85           1. checks\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
90          */\r
91 \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
95                 return;\r
96         }\r
97 \r
98 \r
99         if (!x($account, 'version')) {\r
100                 notice(t("Error! No version data in file! This is not a Friendica account file?"));\r
101                 return;\r
102         }\r
103 \r
104         /*\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
108                 return;\r
109         }\r
110         */\r
111 \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
117                 return;\r
118         }\r
119         if (count($r) > 0) {\r
120                 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));\r
121                 return;\r
122         }\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
128                 return;\r
129         }\r
130         if (count($r) > 0) {\r
131                 notice(sprintf(t("User '%s' already exists on this server!"), $account['user']['nickname']));\r
132                 return;\r
133         }\r
134 \r
135         $oldbaseurl = $account['baseurl'];\r
136         $newbaseurl = $a->get_baseurl();\r
137         $olduid = $account['user']['uid'];\r
138 \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
145         }\r
146 \r
147 \r
148         // import user\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
154                 return;\r
155         }\r
156         $newuid = last_insert_id();\r
157         //~ $newuid = 1;\r
158 \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
162 \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
168                 }\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
175                         return;\r
176                 }\r
177         }\r
178 \r
179         $errorcount = 0;\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
186                         }\r
187                 }\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
191 \r
192 \r
193                         switch ($contact['network']) {\r
194                                 case NETWORK_DFRN:\r
195                                         //  send relocate message (below)\r
196                                         break;\r
197                                 case NETWORK_ZOT:\r
198                                         // TODO handle zot network\r
199                                         break;\r
200                                 case NETWORK_MAIL2:\r
201                                         // TODO ?\r
202                                         break;\r
203                                 case NETWORK_FEED:\r
204                                 case NETWORK_MAIL:\r
205                                         // Nothing to do\r
206                                         break;\r
207                                 default:\r
208                                         // archive other contacts\r
209                                         $contact['archive'] = "1";\r
210                         }\r
211                 }\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
216                         $errorcount++;\r
217                 } else {\r
218                         $contact['newid'] = last_insert_id();\r
219                 }\r
220         }\r
221         if ($errorcount > 0) {\r
222                 notice(sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount));\r
223         }\r
224 \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
230                 } else {\r
231                         $group['newid'] = last_insert_id();\r
232                 }\r
233         }\r
234 \r
235         foreach ($account['group_member'] as &$group_member) {\r
236                 $group_member['uid'] = $newuid;\r
237 \r
238                 $import = 0;\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
242                                 $import++;\r
243                                 break;\r
244                         }\r
245                 }\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
249                                 $import++;\r
250                                 break;\r
251                         }\r
252                 }\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
257                         }\r
258                 }\r
259         }\r
260 \r
261 \r
262 \r
263 \r
264 \r
265         foreach ($account['photo'] as &$photo) {\r
266                 $photo['uid'] = $newuid;\r
267                 $photo['data'] = hex2bin($photo['data']);\r
268 \r
269                 $p = new Photo($photo['data'], $photo['type']);\r
270                 $r = $p->store(\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
274                 );\r
275 \r
276                 if ($r === false) {\r
277                         logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);\r
278                 }\r
279         }\r
280 \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
286                 }\r
287         }\r
288 \r
289         // send relocate messages\r
290         proc_run('php', 'include/notifier.php', 'relocate', $newuid);\r
291 \r
292         info(t("Done. You can now login with your username and password"));\r
293         goaway($a->get_baseurl() . "/login");\r
294 }\r