4 // Included here for completeness, but this is a very dangerous operation.
5 // It is the caller's responsibility to confirm the requestor's intent and
6 // authorisation to do this.
8 function user_remove($uid) {
12 logger('Removing user: ' . $uid);
14 $r = q("select * from user where uid = %d limit 1", intval($uid));
16 call_hooks('remove_user',$r[0]);
18 // save username (actually the nickname as it is guaranteed
19 // unique), so it cannot be re-registered in the future.
21 q("insert into userd ( username ) values ( '%s' )",
25 // don't delete yet, will be done later when contacts have deleted my stuff
26 // q("DELETE FROM `contact` WHERE `uid` = %d", intval($uid));
27 q("DELETE FROM `gcign` WHERE `uid` = %d", intval($uid));
28 q("DELETE FROM `group` WHERE `uid` = %d", intval($uid));
29 q("DELETE FROM `group_member` WHERE `uid` = %d", intval($uid));
30 q("DELETE FROM `intro` WHERE `uid` = %d", intval($uid));
31 q("DELETE FROM `event` WHERE `uid` = %d", intval($uid));
32 q("DELETE FROM `item` WHERE `uid` = %d", intval($uid));
33 q("DELETE FROM `item_id` WHERE `uid` = %d", intval($uid));
34 q("DELETE FROM `mail` WHERE `uid` = %d", intval($uid));
35 q("DELETE FROM `mailacct` WHERE `uid` = %d", intval($uid));
36 q("DELETE FROM `manage` WHERE `uid` = %d", intval($uid));
37 q("DELETE FROM `notify` WHERE `uid` = %d", intval($uid));
38 q("DELETE FROM `photo` WHERE `uid` = %d", intval($uid));
39 q("DELETE FROM `attach` WHERE `uid` = %d", intval($uid));
40 q("DELETE FROM `profile` WHERE `uid` = %d", intval($uid));
41 q("DELETE FROM `profile_check` WHERE `uid` = %d", intval($uid));
42 q("DELETE FROM `pconfig` WHERE `uid` = %d", intval($uid));
43 q("DELETE FROM `search` WHERE `uid` = %d", intval($uid));
44 q("DELETE FROM `spam` WHERE `uid` = %d", intval($uid));
45 // don't delete yet, will be done later when contacts have deleted my stuff
46 // q("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
47 q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
48 proc_run('php', "include/notifier.php", "removeme", $uid);
50 // Send an update to the directory
51 proc_run('php', "include/directory.php", $r[0]['url']);
53 if($uid == local_user()) {
54 unset($_SESSION['authenticated']);
55 unset($_SESSION['uid']);
56 goaway($a->get_baseurl());
61 function contact_remove($id) {
63 $r = q("select uid from contact where id = %d limit 1",
66 if((! count($r)) || (! intval($r[0]['uid'])))
69 $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts');
71 q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d",
77 q("DELETE FROM `contact` WHERE `id` = %d",
80 q("DELETE FROM `item` WHERE `contact-id` = %d ",
83 q("DELETE FROM `photo` WHERE `contact-id` = %d ",
86 q("DELETE FROM `mail` WHERE `contact-id` = %d ",
89 q("DELETE FROM `event` WHERE `cid` = %d ",
92 q("DELETE FROM `queue` WHERE `cid` = %d ",
99 // sends an unfriend message. Does not remove the contact
101 function terminate_friendship($user,$self,$contact) {
106 require_once('include/datetime.php');
108 if($contact['network'] === NETWORK_OSTATUS) {
110 $slap = replace_macros(get_markup_template('follow_slap.tpl'), array(
111 '$name' => $user['username'],
112 '$profile_page' => $a->get_baseurl() . '/profile/' . $user['nickname'],
113 '$photo' => $self['photo'],
114 '$thumb' => $self['thumb'],
115 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
116 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . get_guid(32),
119 '$content' => t('stopped following'),
120 '$nick' => $user['nickname'],
121 '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
122 '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
125 if((x($contact,'notify')) && (strlen($contact['notify']))) {
126 require_once('include/salmon.php');
127 slapper($user,$contact['notify'],$slap);
130 elseif($contact['network'] === NETWORK_DIASPORA) {
131 require_once('include/diaspora.php');
132 diaspora_unshare($user,$contact);
134 elseif($contact['network'] === NETWORK_DFRN) {
135 require_once('include/items.php');
136 dfrn_deliver($user,$contact,'placeholder', 1);
142 // Contact has refused to recognise us as a friend. We will start a countdown.
143 // If they still don't recognise us in 32 days, the relationship is over,
144 // and we won't waste any more time trying to communicate with them.
145 // This provides for the possibility that their database is temporarily messed
146 // up or some other transient event and that there's a possibility we could recover from it.
148 if(! function_exists('mark_for_death')) {
149 function mark_for_death($contact) {
151 if($contact['archive'])
154 if($contact['term-date'] == '0000-00-00 00:00:00') {
155 q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
156 dbesc(datetime_convert()),
157 intval($contact['id'])
162 // TODO: We really should send a notification to the owner after 2-3 weeks
163 // so they won't be surprised when the contact vanishes and can take
164 // remedial action if this was a serious mistake or glitch
166 $expiry = $contact['term-date'] . ' + 32 days ';
167 if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
169 // relationship is really truly dead.
170 // archive them rather than delete
171 // though if the owner tries to unarchive them we'll start the whole process over again
173 q("update contact set `archive` = 1 where id = %d",
174 intval($contact['id'])
176 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact['id']), intval($contact['uid']));
178 //contact_remove($contact['id']);
185 if(! function_exists('unmark_for_death')) {
186 function unmark_for_death($contact) {
187 // It's a miracle. Our dead contact has inexplicably come back to life.
188 q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
189 dbesc('0000-00-00 00:00:00'),
190 intval($contact['id'])
194 function get_contact_details_by_url($url, $uid = -1) {
195 require_once("mod/proxy.php");
196 require_once("include/bbcode.php");
201 $r = q("SELECT `url`, `name`, `nick`, `photo`, `location`, `about`, `keywords`, `gender`, `community`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
202 dbesc(normalise_link($url)));
207 $r = q("SELECT `url`, `name`, `nick`, `avatar` AS `photo`, `location`, `about` FROM `unique_contacts` WHERE `url` = '%s'",
208 dbesc(normalise_link($url)));
212 $profile["keywords"] = "";
213 $profile["gender"] = "";
214 $profile["community"] = false;
215 $profile["network"] = "";
219 // Fetching further contact data from the contact table
220 $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `network` = '%s'",
221 dbesc(normalise_link($url)), intval($uid), dbesc($profile["network"]));
224 $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
225 dbesc(normalise_link($url)), intval($uid));
228 $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
229 dbesc(normalise_link($url)));
232 if (isset($r[0]["url"]) AND $r[0]["url"])
233 $profile["url"] = $r[0]["url"];
234 if (isset($r[0]["name"]) AND $r[0]["name"])
235 $profile["name"] = $r[0]["name"];
236 if (isset($r[0]["nick"]) AND $r[0]["nick"] AND ($profile["nick"] == ""))
237 $profile["nick"] = $r[0]["nick"];
238 if (isset($r[0]["photo"]) AND $r[0]["photo"])
239 $profile["photo"] = $r[0]["photo"];
240 if (isset($r[0]["location"]) AND $r[0]["location"])
241 $profile["location"] = $r[0]["location"];
242 if (isset($r[0]["about"]) AND $r[0]["about"])
243 $profile["about"] = $r[0]["about"];
244 if (isset($r[0]["keywords"]) AND $r[0]["keywords"])
245 $profile["keywords"] = $r[0]["keywords"];
246 if (isset($r[0]["gender"]) AND $r[0]["gender"])
247 $profile["gender"] = $r[0]["gender"];
248 if (isset($r[0]["forum"]) OR isset($r[0]["prv"]))
249 $profile["community"] = ($r[0]["forum"] OR $r[0]["prv"]);
250 if (isset($r[0]["network"]) AND $r[0]["network"])
251 $profile["network"] = $r[0]["network"];
252 if (isset($r[0]["addr"]) AND $r[0]["addr"])
253 $profile["addr"] = $r[0]["addr"];
254 if (isset($r[0]["bd"]) AND $r[0]["bd"])
255 $profile["bd"] = $r[0]["bd"];
256 if ($r[0]["uid"] == 0)
259 $profile["cid"] = $r[0]["id"];
263 if (isset($profile["photo"]))
264 $profile["photo"] = proxy_url($profile["photo"], false, PROXY_SIZE_SMALL);
266 if (isset($profile["location"]))
267 $profile["location"] = bbcode($profile["location"]);
269 if (isset($profile["about"]))
270 $profile["about"] = bbcode($profile["about"]);
272 if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
273 $profile["location"] = "";
274 $profile["about"] = "";
280 if(! function_exists('contact_photo_menu')){
281 function contact_photo_menu($contact) {
290 $contact_drop_link = "";
294 if($contact['network'] === NETWORK_DFRN) {
296 $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
299 $profile_link = $contact['url'];
301 if($profile_link === 'mailbox')
305 $status_link = $profile_link . "?url=status";
306 $photos_link = $profile_link . "?url=photos";
307 $profile_link = $profile_link . "?url=profile";
310 if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA)))
311 $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
313 if ($contact["network"] == NETWORK_DFRN)
314 $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
316 $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
317 $posts_link = $a->get_baseurl() . '/network/0?nets=all&cid=' . $contact['id'];
318 $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
322 'status' => array(t("View Status"), $status_link),
323 'profile' => array(t("View Profile"), $profile_link),
324 'photos' => array(t("View Photos"), $photos_link),
325 'network' => array(t("Network Posts"), $posts_link),
326 'edit' => array(t("Edit Contact"), $contact_url),
327 'drop' => array(t("Drop Contact"), $contact_drop_link),
328 'pm' => array(t("Send PM"), $pm_url),
329 'poke' => array(t("Poke"), $poke_link),
333 $args = array('contact' => $contact, 'menu' => &$menu);
335 call_hooks('contact_photo_menu', $args);
338 foreach($menu as $k=>$v){
340 if(($k !== t("Network Posts")) && ($k !== t("Send PM")) && ($k !== t('Edit Contact')))
341 $o .= "<li><a target=\"redir\" href=\"$v\">$k</a></li>\n";
343 $o .= "<li><a href=\"$v\">$k</a></li>\n";
348 foreach($menu as $k=>$v){
350 if(($v[0] !== t("Network Posts")) && ($v[0] !== t("Send PM")) && ($v[0] !== t('Edit Contact')))
357 $menucondensed = array();
359 foreach ($menu AS $menuitem)
360 if ($menuitem[1] != "")
361 $menucondensed[] = $menuitem;
363 return $menucondensed;
367 function random_profile() {
368 $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
369 AND `last_contact` >= `last_failure`
370 AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
371 ORDER BY rand() LIMIT 1",
372 dbesc(NETWORK_DFRN));
375 return dirname($r[0]['url']);
380 function contacts_not_grouped($uid,$start = 0,$count = 0) {
383 $r = q("select count(*) as total from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) ",
393 $r = q("select * from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) and blocked = 0 and pending = 0 limit %d, %d",
403 function get_contact($url, $uid = 0) {
404 require_once("include/Scrape.php");
409 // is it an address in the format user@server.tld?
410 if (!strstr($url, "http") OR strstr($url, "@")) {
411 $data = probe_url($url);
417 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
418 dbesc(normalise_link($url)),
422 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d",
424 dbesc(normalise_link($url)),
428 $contactid = $contact[0]["id"];
430 // Update the contact every 7 days
431 $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
432 //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
440 $data = probe_url($url);
442 // Does this address belongs to a valid network?
443 if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
446 // tempory programming. Can be deleted after 2015-02-07
447 if (($data["alias"] == "") AND (normalise_link($data["url"]) != normalise_link($url)))
448 $data["alias"] = normalise_link($url);
450 if ($contactid == 0) {
451 q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
452 `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
453 `batch`, `request`, `confirm`, `poco`,
454 `writable`, `blocked`, `readonly`, `pending`)
455 VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
457 dbesc(datetime_convert()),
459 dbesc(normalise_link($data["url"])),
460 dbesc($data["addr"]),
461 dbesc($data["alias"]),
462 dbesc($data["notify"]),
463 dbesc($data["poll"]),
464 dbesc($data["name"]),
465 dbesc($data["nick"]),
466 dbesc($data["photo"]),
467 dbesc($data["network"]),
468 dbesc($data["pubkey"]),
469 intval(CONTACT_IS_SHARING),
470 intval($data["priority"]),
471 dbesc($data["batch"]),
472 dbesc($data["request"]),
473 dbesc($data["confirm"]),
477 $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
478 dbesc(normalise_link($data["url"])),
483 $contactid = $contact[0]["id"];
486 require_once("Photo.php");
488 $photos = import_profile_photo($data["photo"],$uid,$contactid);
490 q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s',
491 `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
492 `name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
496 dbesc($data["addr"]),
497 dbesc($data["alias"]),
498 dbesc($data["name"]),
499 dbesc($data["nick"]),
500 dbesc(datetime_convert()),
501 dbesc(datetime_convert()),
502 dbesc(datetime_convert()),