]> git.mxchange.org Git - friendica.git/blob - include/Contact.php
Merge branch 'release/3.4' into develop
[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:' . random_string(),
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 if(! function_exists('contact_photo_menu')){
195 function contact_photo_menu($contact) {
196
197         $a = get_app();
198
199         $contact_url="";
200         $pm_url="";
201         $status_link="";
202         $photos_link="";
203         $posts_link="";
204         $contact_drop_link = "";
205         $poke_link="";
206
207         $sparkle = false;
208         if($contact['network'] === NETWORK_DFRN) {
209                 $sparkle = true;
210                 $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
211         }
212         else
213                 $profile_link = $contact['url'];
214
215         if($profile_link === 'mailbox')
216                 $profile_link = '';
217
218         if($sparkle) {
219                 $status_link = $profile_link . "?url=status";
220                 $photos_link = $profile_link . "?url=photos";
221                 $profile_link = $profile_link . "?url=profile";
222                 $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
223         }
224
225         $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
226         $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
227         $posts_link = $a->get_baseurl() . '/network/0?nets=all&cid=' . $contact['id'];
228         $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
229
230
231         $menu = Array(
232                 'poke' => array(t("Poke"), $poke_link),
233                 'status' => array(t("View Status"), $status_link),
234                 'profile' => array(t("View Profile"), $profile_link),
235                 'photos' => array(t("View Photos"), $photos_link),
236                 'network' => array(t("Network Posts"), $posts_link),
237                 'edit' => array(t("Edit Contact"), $contact_url),
238                 'drop' => array(t("Drop Contact"), $contact_drop_link),
239                 'pm' => array(t("Send PM"), $pm_url),
240         );
241
242
243         $args = array('contact' => $contact, 'menu' => &$menu);
244
245         call_hooks('contact_photo_menu', $args);
246
247 /*      $o = "";
248         foreach($menu as $k=>$v){
249                 if ($v!="") {
250                         if(($k !== t("Network Posts")) && ($k !== t("Send PM")) && ($k !== t('Edit Contact')))
251                                 $o .= "<li><a target=\"redir\" href=\"$v\">$k</a></li>\n";
252                         else
253                                 $o .= "<li><a href=\"$v\">$k</a></li>\n";
254                 }
255         }
256         return $o;*/
257         foreach($menu as $k=>$v){
258                 if ($v[1]!="") {
259                         if(($v[0] !== t("Network Posts")) && ($v[0] !== t("Send PM")) && ($v[0] !== t('Edit Contact')))
260                                 $menu[$k][2] = 1;
261                         else
262                                 $menu[$k][2] = 0;
263                 }
264         }
265         return $menu;
266 }}
267
268
269 function random_profile() {
270         $r = q("select url from gcontact where url like '%%://%%/profile/%%' order by rand() limit 1");
271         if(count($r))
272                 return dirname($r[0]['url']);
273         return '';
274 }
275
276
277 function contacts_not_grouped($uid,$start = 0,$count = 0) {
278
279         if(! $count) {
280                 $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) ",
281                         intval($uid),
282                         intval($uid)
283                 );
284
285                 return $r;
286
287
288         }
289
290         $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",
291                 intval($uid),
292                 intval($uid),
293                 intval($start),
294                 intval($count)
295         );
296
297         return $r;
298 }
299
300 function get_contact($url, $uid = 0) {
301         require_once("include/Scrape.php");
302
303         $data = array();
304         $contactid = 0;
305
306         // is it an address in the format user@server.tld?
307         if (!strstr($url, "http") OR strstr($url, "@")) {
308                 $data = probe_url($url);
309                 $url = $data["url"];
310                 if ($url == "")
311                         return 0;
312         }
313
314         $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
315                         dbesc(normalise_link($url)),
316                         intval($uid));
317
318         if (!$contact)
319                 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d",
320                                 dbesc($url),
321                                 dbesc(normalise_link($url)),
322                                 intval($uid));
323
324         if ($contact) {
325                 $contactid = $contact[0]["id"];
326
327                 // Update the contact every 7 days
328                 $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
329                 //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
330
331                 if (!$update_photo)
332                         return($contactid);
333         }
334
335         if (!count($data))
336                 $data = probe_url($url);
337
338         // Does this address belongs to a valid network?
339         if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
340                 return 0;
341
342         // tempory programming. Can be deleted after 2015-02-07
343         if (($data["alias"] == "") AND (normalise_link($data["url"]) != normalise_link($url)))
344                 $data["alias"] = normalise_link($url);
345
346         if ($contactid == 0) {
347                 q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
348                                         `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
349                                         `batch`, `request`, `confirm`, `poco`,
350                                         `writable`, `blocked`, `readonly`, `pending`)
351                                         VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
352                         intval($uid),
353                         dbesc(datetime_convert()),
354                         dbesc($data["url"]),
355                         dbesc(normalise_link($data["url"])),
356                         dbesc($data["addr"]),
357                         dbesc($data["alias"]),
358                         dbesc($data["notify"]),
359                         dbesc($data["poll"]),
360                         dbesc($data["name"]),
361                         dbesc($data["nick"]),
362                         dbesc($data["photo"]),
363                         dbesc($data["network"]),
364                         dbesc($data["pubkey"]),
365                         intval(CONTACT_IS_SHARING),
366                         intval($data["priority"]),
367                         dbesc($data["batch"]),
368                         dbesc($data["request"]),
369                         dbesc($data["confirm"]),
370                         dbesc($data["poco"])
371                 );
372
373                 $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
374                                 dbesc(normalise_link($data["url"])),
375                                 intval($uid));
376                 if (!$contact)
377                         return 0;
378
379                 $contactid = $contact[0]["id"];
380         }
381
382         require_once("Photo.php");
383
384         $photos = import_profile_photo($data["photo"],$uid,$contactid);
385
386         q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s',
387                 `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
388                 `name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
389                 dbesc($photos[0]),
390                 dbesc($photos[1]),
391                 dbesc($photos[2]),
392                 dbesc($data["addr"]),
393                 dbesc($data["alias"]),
394                 dbesc($data["name"]),
395                 dbesc($data["nick"]),
396                 dbesc(datetime_convert()),
397                 dbesc(datetime_convert()),
398                 dbesc(datetime_convert()),
399                 intval($contactid)
400         );
401
402         return $contactid;
403 }