]> git.mxchange.org Git - friendica.git/blob - include/Contact.php
Merge pull request #2013 from annando/1511-addr2
[friendica.git] / include / Contact.php
1 <?php
2
3
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.
7
8 function user_remove($uid) {
9         if(! $uid)
10                 return;
11         $a = get_app();
12         logger('Removing user: ' . $uid);
13
14         $r = q("select * from user where uid = %d limit 1", intval($uid));
15
16         call_hooks('remove_user',$r[0]);
17
18         // save username (actually the nickname as it is guaranteed
19         // unique), so it cannot be re-registered in the future.
20
21         q("insert into userd ( username ) values ( '%s' )",
22                 $r[0]['nickname']
23         );
24
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);
49
50         // Send an update to the directory
51         proc_run('php', "include/directory.php", $r[0]['url']);
52
53         if($uid == local_user()) {
54                 unset($_SESSION['authenticated']);
55                 unset($_SESSION['uid']);
56                 goaway($a->get_baseurl());
57         }
58 }
59
60
61 function contact_remove($id) {
62
63         $r = q("select uid from contact where id = %d limit 1",
64                 intval($id)
65         );
66         if((! count($r)) || (! intval($r[0]['uid'])))
67                 return;
68
69         $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts');
70         if($archive) {
71                 q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d",
72                         intval($id)
73                 );
74                 return;
75         }
76
77         q("DELETE FROM `contact` WHERE `id` = %d",
78                 intval($id)
79         );
80         q("DELETE FROM `item` WHERE `contact-id` = %d ",
81                 intval($id)
82         );
83         q("DELETE FROM `photo` WHERE `contact-id` = %d ",
84                 intval($id)
85         );
86         q("DELETE FROM `mail` WHERE `contact-id` = %d ",
87                 intval($id)
88         );
89         q("DELETE FROM `event` WHERE `cid` = %d ",
90                 intval($id)
91         );
92         q("DELETE FROM `queue` WHERE `cid` = %d ",
93                 intval($id)
94         );
95
96 }
97
98
99 // sends an unfriend message. Does not remove the contact
100
101 function terminate_friendship($user,$self,$contact) {
102
103
104         $a = get_app();
105
106         require_once('include/datetime.php');
107
108         if($contact['network'] === NETWORK_OSTATUS) {
109
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),
117                         '$title' => '',
118                         '$type' => 'text',
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"
123                 ));
124
125                 if((x($contact,'notify')) && (strlen($contact['notify']))) {
126                         require_once('include/salmon.php');
127                         slapper($user,$contact['notify'],$slap);
128                 }
129         }
130         elseif($contact['network'] === NETWORK_DIASPORA) {
131                 require_once('include/diaspora.php');
132                 diaspora_unshare($user,$contact);
133         }
134         elseif($contact['network'] === NETWORK_DFRN) {
135                 require_once('include/items.php');
136                 dfrn_deliver($user,$contact,'placeholder', 1);
137         }
138
139 }
140
141
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.
147  
148 if(! function_exists('mark_for_death')) {
149 function mark_for_death($contact) {
150
151         if($contact['archive'])
152                 return;
153
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'])
158                 );
159         }
160         else {
161
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
165
166                 $expiry = $contact['term-date'] . ' + 32 days ';
167                 if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
168
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
172
173                         q("update contact set `archive` = 1 where id = %d",
174                                 intval($contact['id'])
175                         );
176                         q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact['id']), intval($contact['uid']));
177
178                         //contact_remove($contact['id']);
179
180                 }
181         }
182
183 }}
184
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'])
191         );
192 }}
193
194 function get_contact_details_by_url($url, $uid = -1) {
195         require_once("mod/proxy.php");
196         require_once("include/bbcode.php");
197
198         if ($uid == -1)
199                 $uid = local_user();
200
201         $r = q("SELECT `id` AS `gid`, `url`, `name`, `nick`, `addr`, `photo`, `location`, `about`, `keywords`, `gender`, `community`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
202                 dbesc(normalise_link($url)));
203
204         if ($r) {
205                 $profile = $r[0];
206
207                 if ($profile["addr"] == "")
208                         proc_run('php',"include/update_gcontact.php", $profile["gid"]);
209
210         } else {
211                 $r = q("SELECT `url`, `name`, `nick`, `avatar` AS `photo`, `location`, `about` FROM `unique_contacts` WHERE `url` = '%s'",
212                         dbesc(normalise_link($url)));
213
214                 if (count($r)) {
215                         $profile = $r[0];
216                         $profile["keywords"] = "";
217                         $profile["gender"] = "";
218                         $profile["community"] = false;
219                         $profile["network"] = "";
220                         $profile["addr"] = "";
221                 }
222         }
223
224         // Fetching further contact data from the contact table
225         $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `addr`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `network` = '%s'",
226                 dbesc(normalise_link($url)), intval($uid), dbesc($profile["network"]));
227
228         if (!count($r))
229                 $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `addr`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
230                         dbesc(normalise_link($url)), intval($uid));
231
232         if (!count($r))
233                 $r = q("SELECT `id`, `uid`, `url`, `network`, `name`, `nick`, `addr`, `location`, `about`, `keywords`, `gender`, `photo`, `addr`, `forum`, `prv`, `bd` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
234                         dbesc(normalise_link($url)));
235
236         if ($r) {
237                 if (isset($r[0]["url"]) AND $r[0]["url"])
238                         $profile["url"] = $r[0]["url"];
239                 if (isset($r[0]["name"]) AND $r[0]["name"])
240                         $profile["name"] = $r[0]["name"];
241                 if (isset($r[0]["nick"]) AND $r[0]["nick"] AND ($profile["nick"] == ""))
242                         $profile["nick"] = $r[0]["nick"];
243                 if (isset($r[0]["addr"]) AND $r[0]["addr"] AND ($profile["addr"] == ""))
244                         $profile["addr"] = $r[0]["addr"];
245                 if (isset($r[0]["photo"]) AND $r[0]["photo"])
246                         $profile["photo"] = $r[0]["photo"];
247                 if (isset($r[0]["location"]) AND $r[0]["location"])
248                         $profile["location"] = $r[0]["location"];
249                 if (isset($r[0]["about"]) AND $r[0]["about"])
250                         $profile["about"] = $r[0]["about"];
251                 if (isset($r[0]["keywords"]) AND $r[0]["keywords"])
252                         $profile["keywords"] = $r[0]["keywords"];
253                 if (isset($r[0]["gender"]) AND $r[0]["gender"])
254                         $profile["gender"] = $r[0]["gender"];
255                 if (isset($r[0]["forum"]) OR isset($r[0]["prv"]))
256                         $profile["community"] = ($r[0]["forum"] OR $r[0]["prv"]);
257                 if (isset($r[0]["network"]) AND $r[0]["network"])
258                         $profile["network"] = $r[0]["network"];
259                 if (isset($r[0]["addr"]) AND $r[0]["addr"])
260                         $profile["addr"] = $r[0]["addr"];
261                 if (isset($r[0]["bd"]) AND $r[0]["bd"])
262                         $profile["bd"] = $r[0]["bd"];
263                 if ($r[0]["uid"] == 0)
264                         $profile["cid"] = 0;
265                 else
266                         $profile["cid"] = $r[0]["id"];
267         } else
268                 $profile["cid"] = 0;
269
270         if (isset($profile["photo"]))
271                 $profile["photo"] = proxy_url($profile["photo"], false, PROXY_SIZE_SMALL);
272
273         if (isset($profile["location"]))
274                 $profile["location"] = bbcode($profile["location"]);
275
276         if (isset($profile["about"]))
277                 $profile["about"] = bbcode($profile["about"]);
278
279         if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
280                 $profile["location"] = "";
281                 $profile["about"] = "";
282         }
283
284         return($profile);
285 }
286
287 if(! function_exists('contact_photo_menu')){
288 function contact_photo_menu($contact) {
289
290         $a = get_app();
291
292         $contact_url="";
293         $pm_url="";
294         $status_link="";
295         $photos_link="";
296         $posts_link="";
297         $contact_drop_link = "";
298         $poke_link="";
299
300         $sparkle = false;
301         if($contact['network'] === NETWORK_DFRN) {
302                 $sparkle = true;
303                 $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
304         }
305         else
306                 $profile_link = $contact['url'];
307
308         if($profile_link === 'mailbox')
309                 $profile_link = '';
310
311         if($sparkle) {
312                 $status_link = $profile_link . "?url=status";
313                 $photos_link = $profile_link . "?url=photos";
314                 $profile_link = $profile_link . "?url=profile";
315         }
316
317         if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA)))
318                 $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
319
320         if ($contact["network"] == NETWORK_DFRN)
321                 $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
322
323         $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
324         $posts_link = $a->get_baseurl() . '/network/0?nets=all&cid=' . $contact['id'];
325         $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
326
327
328         $menu = Array(
329                 'status' => array(t("View Status"), $status_link),
330                 'profile' => array(t("View Profile"), $profile_link),
331                 'photos' => array(t("View Photos"), $photos_link),
332                 'network' => array(t("Network Posts"), $posts_link),
333                 'edit' => array(t("Edit Contact"), $contact_url),
334                 'drop' => array(t("Drop Contact"), $contact_drop_link),
335                 'pm' => array(t("Send PM"), $pm_url),
336                 'poke' => array(t("Poke"), $poke_link),
337         );
338
339
340         $args = array('contact' => $contact, 'menu' => &$menu);
341
342         call_hooks('contact_photo_menu', $args);
343
344 /*      $o = "";
345         foreach($menu as $k=>$v){
346                 if ($v!="") {
347                         if(($k !== t("Network Posts")) && ($k !== t("Send PM")) && ($k !== t('Edit Contact')))
348                                 $o .= "<li><a target=\"redir\" href=\"$v\">$k</a></li>\n";
349                         else
350                                 $o .= "<li><a href=\"$v\">$k</a></li>\n";
351                 }
352         }
353         return $o;*/
354
355         foreach($menu as $k=>$v){
356                 if ($v[1]!="") {
357                         if(($v[0] !== t("Network Posts")) && ($v[0] !== t("Send PM")) && ($v[0] !== t('Edit Contact')))
358                                 $menu[$k][2] = 1;
359                         else
360                                 $menu[$k][2] = 0;
361                 }
362         }
363
364         $menucondensed = array();
365
366         foreach ($menu AS $menuitem)
367                 if ($menuitem[1] != "")
368                         $menucondensed[] = $menuitem;
369
370         return $menucondensed;
371 }}
372
373
374 function random_profile() {
375         $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
376                                 AND `last_contact` >= `last_failure`
377                                 AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
378                         ORDER BY rand() LIMIT 1",
379                 dbesc(NETWORK_DFRN));
380
381         if(count($r))
382                 return dirname($r[0]['url']);
383         return '';
384 }
385
386
387 function contacts_not_grouped($uid,$start = 0,$count = 0) {
388
389         if(! $count) {
390                 $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) ",
391                         intval($uid),
392                         intval($uid)
393                 );
394
395                 return $r;
396
397
398         }
399
400         $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",
401                 intval($uid),
402                 intval($uid),
403                 intval($start),
404                 intval($count)
405         );
406
407         return $r;
408 }
409
410 function get_contact($url, $uid = 0) {
411         require_once("include/Scrape.php");
412
413         $data = array();
414         $contactid = 0;
415
416         // is it an address in the format user@server.tld?
417         if (!strstr($url, "http") OR strstr($url, "@")) {
418                 $data = probe_url($url);
419                 $url = $data["url"];
420                 if ($url == "")
421                         return 0;
422         }
423
424         $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
425                         dbesc(normalise_link($url)),
426                         intval($uid));
427
428         if (!$contact)
429                 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d",
430                                 dbesc($url),
431                                 dbesc(normalise_link($url)),
432                                 intval($uid));
433
434         if ($contact) {
435                 $contactid = $contact[0]["id"];
436
437                 // Update the contact every 7 days
438                 $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
439                 //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
440
441                 if (!$update_photo)
442                         return($contactid);
443         } elseif ($uid != 0)
444                 return 0;
445
446         if (!count($data))
447                 $data = probe_url($url);
448
449         // Does this address belongs to a valid network?
450         if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
451                 return 0;
452
453         // tempory programming. Can be deleted after 2015-02-07
454         if (($data["alias"] == "") AND (normalise_link($data["url"]) != normalise_link($url)))
455                 $data["alias"] = normalise_link($url);
456
457         if ($contactid == 0) {
458                 q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
459                                         `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
460                                         `batch`, `request`, `confirm`, `poco`,
461                                         `writable`, `blocked`, `readonly`, `pending`)
462                                         VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
463                         intval($uid),
464                         dbesc(datetime_convert()),
465                         dbesc($data["url"]),
466                         dbesc(normalise_link($data["url"])),
467                         dbesc($data["addr"]),
468                         dbesc($data["alias"]),
469                         dbesc($data["notify"]),
470                         dbesc($data["poll"]),
471                         dbesc($data["name"]),
472                         dbesc($data["nick"]),
473                         dbesc($data["photo"]),
474                         dbesc($data["network"]),
475                         dbesc($data["pubkey"]),
476                         intval(CONTACT_IS_SHARING),
477                         intval($data["priority"]),
478                         dbesc($data["batch"]),
479                         dbesc($data["request"]),
480                         dbesc($data["confirm"]),
481                         dbesc($data["poco"])
482                 );
483
484                 $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
485                                 dbesc(normalise_link($data["url"])),
486                                 intval($uid));
487                 if (!$contact)
488                         return 0;
489
490                 $contactid = $contact[0]["id"];
491         }
492
493         require_once("Photo.php");
494
495         $photos = import_profile_photo($data["photo"],$uid,$contactid);
496
497         q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s',
498                 `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
499                 `name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
500                 dbesc($photos[0]),
501                 dbesc($photos[1]),
502                 dbesc($photos[2]),
503                 dbesc($data["addr"]),
504                 dbesc($data["alias"]),
505                 dbesc($data["name"]),
506                 dbesc($data["nick"]),
507                 dbesc(datetime_convert()),
508                 dbesc(datetime_convert()),
509                 dbesc(datetime_convert()),
510                 intval($contactid)
511         );
512
513         return $contactid;
514 }