]> git.mxchange.org Git - friendica.git/blob - include/Contact.php
Merge remote branch 'upstream/master'
[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         q("DELETE FROM `contact` WHERE `uid` = %d", intval($uid));
26         q("DELETE FROM `gcign` WHERE `uid` = %d", intval($uid));
27         q("DELETE FROM `group` WHERE `uid` = %d", intval($uid));
28         q("DELETE FROM `group_member` WHERE `uid` = %d", intval($uid));
29         q("DELETE FROM `intro` WHERE `uid` = %d", intval($uid));
30         q("DELETE FROM `event` WHERE `uid` = %d", intval($uid));
31         q("DELETE FROM `item` WHERE `uid` = %d", intval($uid));
32         q("DELETE FROM `item_id` WHERE `uid` = %d", intval($uid));
33         q("DELETE FROM `mail` WHERE `uid` = %d", intval($uid));
34         q("DELETE FROM `mailacct` WHERE `uid` = %d", intval($uid));
35         q("DELETE FROM `manage` WHERE `uid` = %d", intval($uid));
36         q("DELETE FROM `notify` WHERE `uid` = %d", intval($uid));
37         q("DELETE FROM `photo` WHERE `uid` = %d", intval($uid));
38         q("DELETE FROM `attach` WHERE `uid` = %d", intval($uid));
39         q("DELETE FROM `profile` WHERE `uid` = %d", intval($uid));
40         q("DELETE FROM `profile_check` WHERE `uid` = %d", intval($uid));
41         q("DELETE FROM `pconfig` WHERE `uid` = %d", intval($uid));
42         q("DELETE FROM `search` WHERE `uid` = %d", intval($uid));
43         q("DELETE FROM `spam` WHERE `uid` = %d", intval($uid));
44         q("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
45         if($uid == local_user()) {
46                 unset($_SESSION['authenticated']);
47                 unset($_SESSION['uid']);
48                 goaway($a->get_baseurl());
49         }
50 }
51
52
53 function contact_remove($id) {
54
55         $r = q("select uid from contact where id = %d limit 1",
56                 intval($id)
57         );
58         if((! count($r)) || (! intval($r[0]['uid'])))
59                 return;
60
61         $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts');
62         if($archive) {
63                 q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d limit 1",
64                         intval($id)
65                 );
66                 return;
67         }
68
69         q("DELETE FROM `contact` WHERE `id` = %d LIMIT 1",
70                 intval($id)
71         );
72         q("DELETE FROM `item` WHERE `contact-id` = %d ",
73                 intval($id)
74         );
75         q("DELETE FROM `photo` WHERE `contact-id` = %d ",
76                 intval($id)
77         );
78         q("DELETE FROM `mail` WHERE `contact-id` = %d ",
79                 intval($id)
80         );
81         q("DELETE FROM `event` WHERE `cid` = %d ",
82                 intval($id)
83         );
84         q("DELETE FROM `queue` WHERE `cid` = %d ",
85                 intval($id)
86         );
87
88 }
89
90
91 // sends an unfriend message. Does not remove the contact
92
93 function terminate_friendship($user,$self,$contact) {
94
95
96         $a = get_app();
97
98         require_once('include/datetime.php');
99
100         if($contact['network'] === NETWORK_OSTATUS) {
101
102                 $slap = replace_macros(get_markup_template('follow_slap.tpl'), array(
103                         '$name' => $user['username'],
104                         '$profile_page' => $a->get_baseurl() . '/profile/' . $user['nickname'],
105                         '$photo' => $self['photo'],
106                         '$thumb' => $self['thumb'],
107                         '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
108                         '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . random_string(),
109                         '$title' => '',
110                         '$type' => 'text',
111                         '$content' => t('stopped following'),
112                         '$nick' => $user['nickname'],
113                         '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
114                         '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
115                 ));
116
117                 if((x($contact,'notify')) && (strlen($contact['notify']))) {
118                         require_once('include/salmon.php');
119                         slapper($user,$contact['notify'],$slap);
120                 }
121         }
122         elseif($contact['network'] === NETWORK_DIASPORA) {
123                 require_once('include/diaspora.php');
124                 diaspora_unshare($user,$contact);
125         }
126         elseif($contact['network'] === NETWORK_DFRN) {
127                 require_once('include/items.php');
128                 dfrn_deliver($user,$contact,'placeholder', 1);
129         }
130
131 }
132
133
134 // Contact has refused to recognise us as a friend. We will start a countdown.
135 // If they still don't recognise us in 32 days, the relationship is over,
136 // and we won't waste any more time trying to communicate with them.
137 // This provides for the possibility that their database is temporarily messed
138 // up or some other transient event and that there's a possibility we could recover from it.
139  
140 if(! function_exists('mark_for_death')) {
141 function mark_for_death($contact) {
142
143         if($contact['archive'])
144                 return;
145
146         if($contact['term-date'] == '0000-00-00 00:00:00') {
147                 q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d LIMIT 1",
148                                 dbesc(datetime_convert()),
149                                 intval($contact['id'])
150                 );
151         }
152         else {
153
154                 // TODO: We really should send a notification to the owner after 2-3 weeks
155                 // so they won't be surprised when the contact vanishes and can take
156                 // remedial action if this was a serious mistake or glitch
157
158                 $expiry = $contact['term-date'] . ' + 32 days ';
159                 if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
160
161                         // relationship is really truly dead. 
162                         // archive them rather than delete
163                         // though if the owner tries to unarchive them we'll start the whole process over again
164
165                         q("update contact set `archive` = 1 where id = %d limit 1",
166                                 intval($contact['id'])
167                         );
168
169                         //contact_remove($contact['id']);
170
171                 }
172         }
173
174 }}
175
176 if(! function_exists('unmark_for_death')) {
177 function unmark_for_death($contact) {
178         // It's a miracle. Our dead contact has inexplicably come back to life.
179         q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d LIMIT 1",
180                 dbesc('0000-00-00 00:00:00'),
181                 intval($contact['id'])
182         );
183 }}
184
185 if(! function_exists('contact_photo_menu')){
186 function contact_photo_menu($contact) {
187
188         $a = get_app();
189         
190         $contact_url="";
191         $pm_url="";
192         $status_link="";
193         $photos_link="";
194         $posts_link="";
195         $poke_link="";
196
197         $sparkle = false;
198         if($contact['network'] === NETWORK_DFRN) {
199                 $sparkle = true;
200                 $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
201         }
202         else
203                 $profile_link = $contact['url'];
204
205         if($profile_link === 'mailbox')
206                 $profile_link = '';
207
208         if($sparkle) {
209                 $status_link = $profile_link . "?url=status";
210                 $photos_link = $profile_link . "?url=photos";
211                 $profile_link = $profile_link . "?url=profile";
212                 $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
213         }
214
215         $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
216         $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
217         $posts_link = $a->get_baseurl() . '/network/?cid=' . $contact['id'];
218
219         $menu = Array(
220                 t("Poke") => $poke_link,
221                 t("View Status") => $status_link,
222                 t("View Profile") => $profile_link,
223                 t("View Photos") => $photos_link,               
224                 t("Network Posts") => $posts_link, 
225                 t("Edit Contact") => $contact_url,
226                 t("Send PM") => $pm_url,
227         );
228         
229         
230         $args = array('contact' => $contact, 'menu' => &$menu);
231         
232         call_hooks('contact_photo_menu', $args);
233         
234         $o = "";
235         foreach($menu as $k=>$v){
236                 if ($v!="") {
237                         if(($k !== t("Network Posts")) && ($k !== t("Send PM")) && ($k !== t('Edit Contact')))
238                                 $o .= "<li><a target=\"redir\" href=\"$v\">$k</a></li>\n";
239                         else
240                                 $o .= "<li><a href=\"$v\">$k</a></li>\n";
241                 }
242         }
243         return $o;
244 }}
245
246
247 function random_profile() {
248         $r = q("select url from gcontact where url like '%%://%%/profile/%%' order by rand() limit 1");
249         if(count($r))
250                 return dirname($r[0]['url']);
251         return '';
252 }
253
254
255 function contacts_not_grouped($uid,$start = 0,$count = 0) {
256
257         if(! $count) {
258                 $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) ",
259                         intval($uid),
260                         intval($uid)
261                 );
262
263                 return $r;
264
265
266         }
267
268         $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",
269                 intval($uid),
270                 intval($uid),
271                 intval($start),
272                 intval($count)
273         );
274
275         return $r;
276 }
277