]> git.mxchange.org Git - friendica.git/blob - include/Contact.php
3185e8734829d39b14d6c1492fde63916a90d9ee
[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 
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
166
167                 $expiry = $contact['term-date'] . ' + 32 days ';
168                 if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
169
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
173
174                         q("update contact set `archive` = 1 where id = %d",
175                                 intval($contact['id'])
176                         );
177                         q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact['id']), intval($contact['uid']));
178
179                         //contact_remove($contact['id']);
180
181                 }
182         }
183
184 }}
185
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'])
192         );
193 }}
194
195 function get_contact_details_by_url($url, $uid = -1) {
196         if ($uid == -1)
197                 $uid = local_user();
198
199         $r = q("SELECT `id` AS `gid`, `url`, `name`, `nick`, `addr`, `photo`, `location`, `about`, `keywords`, `gender`, `community`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
200                 dbesc(normalise_link($url)));
201
202         if ($r) {
203                 $profile = $r[0];
204
205                 if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND
206                         in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
207                         proc_run('php',"include/update_gcontact.php", $profile["gid"]);
208         }
209
210         // Fetching further contact data from the contact table
211         $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'",
212                 dbesc(normalise_link($url)), intval($uid), dbesc($profile["network"]));
213
214         if (!count($r))
215                 $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",
216                         dbesc(normalise_link($url)), intval($uid));
217
218         if (!count($r))
219                 $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",
220                         dbesc(normalise_link($url)));
221
222         if ($r) {
223                 if (isset($r[0]["url"]) AND $r[0]["url"])
224                         $profile["url"] = $r[0]["url"];
225                 if (isset($r[0]["name"]) AND $r[0]["name"])
226                         $profile["name"] = $r[0]["name"];
227                 if (isset($r[0]["nick"]) AND $r[0]["nick"] AND ($profile["nick"] == ""))
228                         $profile["nick"] = $r[0]["nick"];
229                 if (isset($r[0]["addr"]) AND $r[0]["addr"] AND ($profile["addr"] == ""))
230                         $profile["addr"] = $r[0]["addr"];
231                 if (isset($r[0]["photo"]) AND $r[0]["photo"])
232                         $profile["photo"] = $r[0]["photo"];
233                 if (isset($r[0]["location"]) AND $r[0]["location"])
234                         $profile["location"] = $r[0]["location"];
235                 if (isset($r[0]["about"]) AND $r[0]["about"])
236                         $profile["about"] = $r[0]["about"];
237                 if (isset($r[0]["keywords"]) AND $r[0]["keywords"])
238                         $profile["keywords"] = $r[0]["keywords"];
239                 if (isset($r[0]["gender"]) AND $r[0]["gender"])
240                         $profile["gender"] = $r[0]["gender"];
241                 if (isset($r[0]["forum"]) OR isset($r[0]["prv"]))
242                         $profile["community"] = ($r[0]["forum"] OR $r[0]["prv"]);
243                 if (isset($r[0]["network"]) AND $r[0]["network"])
244                         $profile["network"] = $r[0]["network"];
245                 if (isset($r[0]["addr"]) AND $r[0]["addr"])
246                         $profile["addr"] = $r[0]["addr"];
247                 if (isset($r[0]["bd"]) AND $r[0]["bd"])
248                         $profile["bd"] = $r[0]["bd"];
249                 if ($r[0]["uid"] == 0)
250                         $profile["cid"] = 0;
251                 else
252                         $profile["cid"] = $r[0]["id"];
253         } else
254                 $profile["cid"] = 0;
255
256         if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
257                 $profile["location"] = "";
258                 $profile["about"] = "";
259         }
260
261         return($profile);
262 }
263
264 if(! function_exists('contact_photo_menu')){
265 function contact_photo_menu($contact, $uid = 0) {
266
267         $a = get_app();
268
269         $contact_url="";
270         $pm_url="";
271         $status_link="";
272         $photos_link="";
273         $posts_link="";
274         $contact_drop_link = "";
275         $poke_link="";
276
277         if ($uid == 0)
278                 $uid = local_user();
279
280         if ($contact["uid"] != $uid) {
281                 if ($uid == 0) {
282                         $profile_link = zrl($contact['url']);
283                         $menu = Array('profile' => array(t("View Profile"), $profile_link, true));
284
285                         return $menu;
286                 }
287
288                 $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `network` = '%s' AND `uid` = %d",
289                         dbesc($contact["nurl"]), dbesc($contact["network"]), intval($uid));
290                 if ($r)
291                         return contact_photo_menu($r[0], $uid);
292                 else {
293                         $profile_link = zrl($contact['url']);
294                         $connlnk = 'follow/?url='.$contact['url'];
295                         $menu = Array(
296                                 'profile' => array(t("View Profile"), $profile_link, true),
297                                 'follow' => array(t("Connect/Follow"), $connlnk, true)
298                                 );
299
300                         return $menu;
301                 }
302         }
303
304         $sparkle = false;
305         if($contact['network'] === NETWORK_DFRN) {
306                 $sparkle = true;
307                 $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
308         }
309         else
310                 $profile_link = $contact['url'];
311
312         if($profile_link === 'mailbox')
313                 $profile_link = '';
314
315         if($sparkle) {
316                 $status_link = $profile_link . "?url=status";
317                 $photos_link = $profile_link . "?url=photos";
318                 $profile_link = $profile_link . "?url=profile";
319         }
320
321         if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA)))
322                 $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
323
324         if ($contact["network"] == NETWORK_DFRN)
325                 $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
326
327         $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
328         $posts_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/posts';
329         $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
330
331
332         /**
333          * menu array:
334          * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
335          */
336         $menu = Array(
337                 'status' => array(t("View Status"), $status_link, true),
338                 'profile' => array(t("View Profile"), $profile_link, true),
339                 'photos' => array(t("View Photos"), $photos_link,true),
340                 'network' => array(t("Network Posts"), $posts_link,false),
341                 'edit' => array(t("Edit Contact"), $contact_url, false),
342                 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
343                 'pm' => array(t("Send PM"), $pm_url, false),
344                 'poke' => array(t("Poke"), $poke_link, false),
345         );
346
347
348         $args = array('contact' => $contact, 'menu' => &$menu);
349
350         call_hooks('contact_photo_menu', $args);
351
352         $menucondensed = array();
353
354         foreach ($menu AS $menuname=>$menuitem)
355                 if ($menuitem[1] != "")
356                         $menucondensed[$menuname] = $menuitem;
357
358         return $menucondensed;
359 }}
360
361
362 function random_profile() {
363         $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
364                                 AND `last_contact` >= `last_failure`
365                                 AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
366                         ORDER BY rand() LIMIT 1",
367                 dbesc(NETWORK_DFRN));
368
369         if(count($r))
370                 return dirname($r[0]['url']);
371         return '';
372 }
373
374
375 function contacts_not_grouped($uid,$start = 0,$count = 0) {
376
377         if(! $count) {
378                 $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) ",
379                         intval($uid),
380                         intval($uid)
381                 );
382
383                 return $r;
384
385
386         }
387
388         $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",
389                 intval($uid),
390                 intval($uid),
391                 intval($start),
392                 intval($count)
393         );
394
395         return $r;
396 }
397
398 function get_contact($url, $uid = 0) {
399         require_once("include/Scrape.php");
400
401         $data = array();
402         $contactid = 0;
403
404         // is it an address in the format user@server.tld?
405         if (!strstr($url, "http") OR strstr($url, "@")) {
406                 $data = probe_url($url);
407                 $url = $data["url"];
408                 if ($url == "")
409                         return 0;
410         }
411
412         $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
413                         dbesc(normalise_link($url)),
414                         intval($uid));
415
416         if (!$contact)
417                 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d",
418                                 dbesc($url),
419                                 dbesc(normalise_link($url)),
420                                 intval($uid));
421
422         if ($contact) {
423                 $contactid = $contact[0]["id"];
424
425                 // Update the contact every 7 days
426                 $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
427                 //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
428
429                 if (!$update_photo)
430                         return($contactid);
431         } elseif ($uid != 0)
432                 return 0;
433
434         if (!count($data))
435                 $data = probe_url($url);
436
437         // Does this address belongs to a valid network?
438         if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
439                 return 0;
440
441         // tempory programming. Can be deleted after 2015-02-07
442         if (($data["alias"] == "") AND (normalise_link($data["url"]) != normalise_link($url)))
443                 $data["alias"] = normalise_link($url);
444
445         if ($contactid == 0) {
446                 q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
447                                         `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
448                                         `batch`, `request`, `confirm`, `poco`,
449                                         `writable`, `blocked`, `readonly`, `pending`)
450                                         VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
451                         intval($uid),
452                         dbesc(datetime_convert()),
453                         dbesc($data["url"]),
454                         dbesc(normalise_link($data["url"])),
455                         dbesc($data["addr"]),
456                         dbesc($data["alias"]),
457                         dbesc($data["notify"]),
458                         dbesc($data["poll"]),
459                         dbesc($data["name"]),
460                         dbesc($data["nick"]),
461                         dbesc($data["photo"]),
462                         dbesc($data["network"]),
463                         dbesc($data["pubkey"]),
464                         intval(CONTACT_IS_SHARING),
465                         intval($data["priority"]),
466                         dbesc($data["batch"]),
467                         dbesc($data["request"]),
468                         dbesc($data["confirm"]),
469                         dbesc($data["poco"])
470                 );
471
472                 $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
473                                 dbesc(normalise_link($data["url"])),
474                                 intval($uid));
475                 if (!$contact)
476                         return 0;
477
478                 $contactid = $contact[0]["id"];
479         }
480
481         require_once("Photo.php");
482
483         $photos = import_profile_photo($data["photo"],$uid,$contactid);
484
485         q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s',
486                 `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
487                 `name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
488                 dbesc($photos[0]),
489                 dbesc($photos[1]),
490                 dbesc($photos[2]),
491                 dbesc($data["addr"]),
492                 dbesc($data["alias"]),
493                 dbesc($data["name"]),
494                 dbesc($data["nick"]),
495                 dbesc(datetime_convert()),
496                 dbesc(datetime_convert()),
497                 dbesc(datetime_convert()),
498                 intval($contactid)
499         );
500
501         return $contactid;
502 }