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::send_unshare($user,$contact);
134 elseif($contact['network'] === NETWORK_DFRN) {
135 require_once('include/dfrn.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'])
163 /// We really should send a notification to the owner after 2-3 weeks
164 /// so they won't be surprised when the contact vanishes and can take
165 /// remedial action if this was a serious mistake or glitch
167 $expiry = $contact['term-date'] . ' + 32 days ';
168 if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
170 // relationship is really truly dead.
171 // archive them rather than delete
172 // though if the owner tries to unarchive them we'll start the whole process over again
174 q("update contact set `archive` = 1 where id = %d",
175 intval($contact['id'])
177 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact['id']), intval($contact['uid']));
179 //contact_remove($contact['id']);
186 if(! function_exists('unmark_for_death')) {
187 function unmark_for_death($contact) {
188 // It's a miracle. Our dead contact has inexplicably come back to life.
189 q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
190 dbesc('0000-00-00 00:00:00'),
191 intval($contact['id'])
196 * @brief Get contact data for a given profile link
198 * The function looks at several places (contact table and gcontact table) for the contact
200 * @param string $url The profile link
201 * @param int $uid User id
202 * @param array $default If not data was found take this data as default value
204 * @return array Contact data
206 function get_contact_details_by_url($url, $uid = -1, $default = array()) {
210 // community, nurl, alias, nsfw, birthday
212 // Fetch contact data from the contact table for the user and given network
213 $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
214 `keywords`, `gender`, `photo`, `thumb`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd`, `bd` AS `birthday`, `self`
215 FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `network` IN ('%s', '')",
216 dbesc(normalise_link($url)), intval($uid), dbesc($profile["network"]));
218 // Is the contact present for the user in a different network? (Can happen with OStatus and the "Statusnet" addon)
219 if (!count($r) AND !isset($profile))
220 $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
221 `keywords`, `gender`, `photo`, `thumb`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd`, `bd` AS `birthday`, `self`
222 FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
223 dbesc(normalise_link($url)), intval($uid));
225 // Fetch the data from the contact table with "uid=0" (which is filled automatically)
226 if (!count($r) AND !isset($profile))
227 $r = q("SELECT `id`, 0 AS `cid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
228 `keywords`, `gender`, `photo`, `thumb`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd`, `bd` AS `birthday`, 0 AS `self`
229 FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
230 dbesc(normalise_link($url)));
232 // Fetch the data from the gcontact table
233 if (!count($r) AND !isset($profile))
234 $r = q("SELECT 0 AS `id`, 0 AS `cid`, `id` AS `gid`, 0 AS `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
235 `keywords`, `gender`, `photo`, `photo` AS `thumb`, `community` AS `forum`, 0 AS `prv`, `community`, `birthday`, 0 AS `self`
236 FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
237 dbesc(normalise_link($url)));
243 if (!isset($profile["thumb"]) AND isset($profile["photo"]))
244 $profile["thumb"] = $profile["photo"];
247 if (isset($profile["birthday"]) AND !isset($profile["bd"])){
248 $bd_timestamp = strtotime($profile["birthday"]);
249 $month = date("m", $bd_timestamp);
250 $day = date("d", $bd_timestamp);
252 $current_timestamp = time();
253 $current_year = date("Y", $current_timestamp);
254 $current_month = date("m", $current_timestamp);
255 $current_day = date("d", $current_timestamp);
257 $profile["bd"] = $current_year."-".$month."-".$day;
258 $current = $current_year."-".$current_month."-".$current_day;
260 if ($profile["bd"] < $current)
261 $profile["bd"] = (++$current_year)."-".$month."-".$day;
264 if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND
265 in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
266 proc_run('php',"include/update_gcontact.php", $profile["gid"]);
268 // Show contact details of Diaspora contacts only if connected
269 if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
270 $profile["location"] = "";
271 $profile["about"] = "";
272 $profile["gender"] = "";
273 $profile["birthday"] = "0000-00-00";
279 if(! function_exists('contact_photo_menu')){
280 function contact_photo_menu($contact, $uid = 0) {
289 $contact_drop_link = "";
295 if ($contact["uid"] != $uid) {
297 $profile_link = zrl($contact['url']);
298 $menu = Array('profile' => array(t("View Profile"), $profile_link, true));
303 $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `network` = '%s' AND `uid` = %d",
304 dbesc($contact["nurl"]), dbesc($contact["network"]), intval($uid));
306 return contact_photo_menu($r[0], $uid);
308 $profile_link = zrl($contact['url']);
309 $connlnk = 'follow/?url='.$contact['url'];
311 'profile' => array(t("View Profile"), $profile_link, true),
312 'follow' => array(t("Connect/Follow"), $connlnk, true)
320 if($contact['network'] === NETWORK_DFRN) {
322 $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
325 $profile_link = $contact['url'];
327 if($profile_link === 'mailbox')
331 $status_link = $profile_link . "?url=status";
332 $photos_link = $profile_link . "?url=photos";
333 $profile_link = $profile_link . "?url=profile";
336 if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA)))
337 $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
339 if ($contact["network"] == NETWORK_DFRN)
340 $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
342 $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
343 $posts_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/posts';
344 $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
349 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
352 'status' => array(t("View Status"), $status_link, true),
353 'profile' => array(t("View Profile"), $profile_link, true),
354 'photos' => array(t("View Photos"), $photos_link,true),
355 'network' => array(t("Network Posts"), $posts_link,false),
356 'edit' => array(t("Edit Contact"), $contact_url, false),
357 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
358 'pm' => array(t("Send PM"), $pm_url, false),
359 'poke' => array(t("Poke"), $poke_link, false),
363 $args = array('contact' => $contact, 'menu' => &$menu);
365 call_hooks('contact_photo_menu', $args);
367 $menucondensed = array();
369 foreach ($menu AS $menuname=>$menuitem)
370 if ($menuitem[1] != "")
371 $menucondensed[$menuname] = $menuitem;
373 return $menucondensed;
377 function random_profile() {
378 $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
379 AND `last_contact` >= `last_failure`
380 AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
381 ORDER BY rand() LIMIT 1",
382 dbesc(NETWORK_DFRN));
385 return dirname($r[0]['url']);
390 function contacts_not_grouped($uid,$start = 0,$count = 0) {
393 $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) ",
403 $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",
413 function get_contact($url, $uid = 0) {
414 require_once("include/Scrape.php");
419 // is it an address in the format user@server.tld?
420 /// @todo use gcontact and/or the addr field for a lookup
421 if (!strstr($url, "http") OR strstr($url, "@")) {
422 $data = probe_url($url);
428 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
429 dbesc(normalise_link($url)),
433 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d ORDER BY `id` LIMIT 1",
435 dbesc(normalise_link($url)),
439 $contactid = $contact[0]["id"];
441 // Update the contact every 7 days
442 $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
443 //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
451 $data = probe_url($url);
453 // Does this address belongs to a valid network?
454 if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
459 if ($contactid == 0) {
460 q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
461 `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
462 `batch`, `request`, `confirm`, `poco`,
463 `writable`, `blocked`, `readonly`, `pending`)
464 VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
466 dbesc(datetime_convert()),
468 dbesc(normalise_link($data["url"])),
469 dbesc($data["addr"]),
470 dbesc($data["alias"]),
471 dbesc($data["notify"]),
472 dbesc($data["poll"]),
473 dbesc($data["name"]),
474 dbesc($data["nick"]),
475 dbesc($data["photo"]),
476 dbesc($data["network"]),
477 dbesc($data["pubkey"]),
478 intval(CONTACT_IS_SHARING),
479 intval($data["priority"]),
480 dbesc($data["batch"]),
481 dbesc($data["request"]),
482 dbesc($data["confirm"]),
486 $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
487 dbesc(normalise_link($data["url"])),
492 $contactid = $contact[0]["id"];
495 if ((count($contact) > 1) AND ($uid == 0) AND ($contactid != 0) AND ($url != ""))
496 q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `id` != %d",
497 dbesc(normalise_link($url)),
500 require_once("Photo.php");
502 update_contact_avatar($data["photo"],$uid,$contactid);
504 q("UPDATE `contact` SET `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
505 `name-date` = '%s', `uri-date` = '%s' WHERE `id` = %d",
506 dbesc($data["addr"]),
507 dbesc($data["alias"]),
508 dbesc($data["name"]),
509 dbesc($data["nick"]),
510 dbesc(datetime_convert()),
511 dbesc(datetime_convert()),
519 * @brief Returns posts from a given gcontact
521 * @param App $a argv application class
522 * @param int $gcontact_id Global contact
524 * @return string posts in HTML
526 function posts_from_gcontact($a, $gcontact_id) {
528 require_once('include/conversation.php');
530 // There are no posts with "uid = 0" with connector networks
531 // This speeds up the query a lot
532 $r = q("SELECT `network` FROM `gcontact` WHERE `id` = %d", dbesc($gcontact_id));
533 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
534 $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND `item`.`private`))";
536 $sql = "`item`.`uid` = %d";
538 if(get_config('system', 'old_pager')) {
539 $r = q("SELECT COUNT(*) AS `total` FROM `item`
540 WHERE `gcontact-id` = %d and $sql",
541 intval($gcontact_id),
542 intval(local_user()));
544 $a->set_pager_total($r[0]['total']);
547 $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
548 `author-name` AS `name`, `owner-avatar` AS `photo`,
549 `owner-link` AS `url`, `owner-avatar` AS `thumb`
550 FROM `item` FORCE INDEX (`gcontactid_uid_created`)
551 WHERE `gcontact-id` = %d AND $sql AND
552 NOT `deleted` AND NOT `moderated` AND `visible`
553 ORDER BY `item`.`created` DESC LIMIT %d, %d",
554 intval($gcontact_id),
555 intval(local_user()),
556 intval($a->pager['start']),
557 intval($a->pager['itemspage'])
560 $o = conversation($a,$r,'community',false);
562 if(!get_config('system', 'old_pager')) {
563 $o .= alt_pager($a,count($r));
572 * @brief Returns posts from a given contact
574 * @param App $a argv application class
575 * @param int $contact_id contact
577 * @return string posts in HTML
579 function posts_from_contact($a, $contact_id) {
581 require_once('include/conversation.php');
583 $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id));
589 if(get_config('system', 'old_pager')) {
590 $r = q("SELECT COUNT(*) AS `total` FROM `item`
591 WHERE `item`.`uid` = %d AND `author-link` IN ('%s', '%s')",
592 intval(local_user()),
593 dbesc(str_replace("https://", "http://", $contact["url"])),
594 dbesc(str_replace("http://", "https://", $contact["url"])));
596 $a->set_pager_total($r[0]['total']);
599 $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
600 `author-name` AS `name`, `owner-avatar` AS `photo`,
601 `owner-link` AS `url`, `owner-avatar` AS `thumb`
602 FROM `item` FORCE INDEX (`uid_contactid_created`)
603 WHERE `item`.`uid` = %d AND `contact-id` = %d
604 AND `author-link` IN ('%s', '%s')
605 AND NOT `deleted` AND NOT `moderated` AND `visible`
606 ORDER BY `item`.`created` DESC LIMIT %d, %d",
607 intval(local_user()),
609 dbesc(str_replace("https://", "http://", $contact["url"])),
610 dbesc(str_replace("http://", "https://", $contact["url"])),
611 intval($a->pager['start']),
612 intval($a->pager['itemspage'])
615 $o .= conversation($a,$r,'community',false);
617 if(!get_config('system', 'old_pager'))
618 $o .= alt_pager($a,count($r));
626 * @brief Returns a formatted location string from the given profile array
628 * @param array $profile Profile array (Generated from the "profile" table)
630 * @return string Location string
632 function formatted_location($profile) {
635 if($profile['locality'])
636 $location .= $profile['locality'];
638 if($profile['region'] AND ($profile['locality'] != $profile['region'])) {
642 $location .= $profile['region'];
645 if($profile['country-name']) {
649 $location .= $profile['country-name'];