]> git.mxchange.org Git - friendica.git/blob - include/follow.php
Only update communication relevant data. And only update if needed.
[friendica.git] / include / follow.php
1 <?php
2
3 function update_contact($id) {
4         /*
5         Warning: Never ever fetch the public key via probe_url and write it into the contacts.
6         This will reliably kill your communication with Friendica contacts.
7         */
8
9         $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
10         if (!$r)
11                 return;
12
13         $ret = probe_url($r[0]["url"]);
14
15         // If probe_url fails the network code will be different
16         if ($ret["network"] != $r[0]["network"])
17                 return;
18
19         $update = false;
20
21         // make sure to not overwrite existing values with blank entries
22         foreach ($ret AS $key => $val) {
23                 if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
24                         $ret[$key] = $r[0][$key];
25
26                 if (isset($r[0][$key]) AND ($ret[$key] != $r[0][$key]))
27                         $update = true;
28         }
29
30         if (!$update)
31                 return;
32
33         q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
34                 dbesc($ret['url']),
35                 dbesc(normalise_link($ret['url'])),
36                 dbesc($ret['addr']),
37                 dbesc($ret['alias']),
38                 dbesc($ret['batch']),
39                 dbesc($ret['notify']),
40                 dbesc($ret['poll']),
41                 dbesc($ret['poco']),
42                 intval($id)
43         );
44 }
45
46 //
47 // Takes a $uid and a url/handle and adds a new contact
48 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
49 // dfrn_request page.
50
51 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
52 // Returns an array
53 //  $return['success'] boolean true if successful
54 //  $return['message'] error text if success is false.
55
56
57
58 function new_contact($uid,$url,$interactive = false) {
59
60         $result = array('cid' => -1, 'success' => false,'message' => '');
61
62         $a = get_app();
63
64         // remove ajax junk, e.g. Twitter
65
66         $url = str_replace('/#!/','/',$url);
67
68         if(! allowed_url($url)) {
69                 $result['message'] = t('Disallowed profile URL.');
70                 return $result;
71         }
72
73         if(! $url) {
74                 $result['message'] = t('Connect URL missing.');
75                 return $result;
76         }
77
78         $arr = array('url' => $url, 'contact' => array());
79
80         call_hooks('follow', $arr);
81
82         if(x($arr['contact'],'name'))
83                 $ret = $arr['contact'];
84         else
85                 $ret = probe_url($url);
86
87         if($ret['network'] === NETWORK_DFRN) {
88                 if($interactive) {
89                         if(strlen($a->path))
90                                 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
91                         else
92                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
93
94                         goaway($ret['request'] . "&addr=$myaddr");
95
96                         // NOTREACHED
97                 }
98         }
99         else {
100                 if(get_config('system','dfrn_only')) {
101                         $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
102                         $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
103                         return $result;
104                 }
105         }
106
107
108
109
110
111
112         // This extra param just confuses things, remove it
113         if($ret['network'] === NETWORK_DIASPORA)
114                 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
115
116
117         // do we have enough information?
118
119         if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
120                 $result['message'] .=  t('The profile address specified does not provide adequate information.') . EOL;
121                 if(! x($ret,'poll'))
122                         $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
123                 if(! x($ret,'name'))
124                         $result['message'] .=  t('An author or name was not found.') . EOL;
125                 if(! x($ret,'url'))
126                         $result['message'] .=  t('No browser URL could be matched to this address.') . EOL;
127                 if(strpos($url,'@') !== false) {
128                         $result['message'] .=  t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
129                         $result['message'] .=  t('Use mailto: in front of address to force email check.') . EOL;
130                 }
131                 return $result;
132         }
133
134         if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
135                 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
136                 $ret['notify'] = '';
137         }
138
139
140
141
142
143
144         if(! $ret['notify']) {
145                 $result['message'] .=  t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
146         }
147
148         $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
149
150         $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
151
152         $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
153
154         if($ret['network'] === NETWORK_MAIL) {
155                 $writeable = 1;
156
157         }
158         if($ret['network'] === NETWORK_DIASPORA)
159                 $writeable = 1;
160
161         // check if we already have a contact
162         // the poll url is more reliable than the profile url, as we may have
163         // indirect links or webfinger links
164
165         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
166                 intval($uid),
167                 dbesc($ret['poll']),
168                 dbesc(normalise_link($ret['poll'])),
169                 dbesc($ret['network'])
170         );
171
172         if(count($r)) {
173                 // update contact
174                 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
175                         q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
176                                 intval(CONTACT_IS_FRIEND),
177                                 intval($subhub),
178                                 intval($r[0]['id']),
179                                 intval($uid)
180                         );
181                 }
182
183         } else {
184
185
186                 // check service class limits
187
188                 $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0",
189                         intval($uid)
190                 );
191                 if(count($r))
192                         $total_contacts = $r[0]['total'];
193
194                 if(! service_class_allows($uid,'total_contacts',$total_contacts)) {
195                         $result['message'] .= upgrade_message();
196                         return $result;
197                 }
198
199                 $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0",
200                         intval($uid),
201                         dbesc($network)
202                 );
203                 if(count($r))
204                         $total_network = $r[0]['total'];
205
206                 if(! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
207                         $result['message'] .= upgrade_message();
208                         return $result;
209                 }
210
211                 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
212                 if($ret['network'] === NETWORK_DIASPORA)
213                         $new_relation = CONTACT_IS_FOLLOWER;
214
215                 // create contact record
216                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
217                         `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
218                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
219                         intval($uid),
220                         dbesc(datetime_convert()),
221                         dbesc($ret['url']),
222                         dbesc(normalise_link($ret['url'])),
223                         dbesc($ret['addr']),
224                         dbesc($ret['alias']),
225                         dbesc($ret['batch']),
226                         dbesc($ret['notify']),
227                         dbesc($ret['poll']),
228                         dbesc($ret['poco']),
229                         dbesc($ret['name']),
230                         dbesc($ret['nick']),
231                         dbesc($ret['network']),
232                         dbesc($ret['pubkey']),
233                         intval($new_relation),
234                         intval($ret['priority']),
235                         intval($writeable),
236                         intval($hidden),
237                         intval($subhub)
238                 );
239         }
240
241         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
242                 dbesc($ret['url']),
243                 dbesc($ret['network']),
244                 intval($uid)
245         );
246
247         if(! count($r)) {
248                 $result['message'] .=  t('Unable to retrieve contact information.') . EOL;
249                 return $result;
250         }
251
252         $contact = $r[0];
253         $contact_id  = $r[0]['id'];
254         $result['cid'] = $contact_id;
255
256         $g = q("select def_gid from user where uid = %d limit 1",
257                 intval($uid)
258         );
259         if($g && intval($g[0]['def_gid'])) {
260                 require_once('include/group.php');
261                 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
262         }
263
264         require_once("include/Photo.php");
265
266         $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
267
268         $r = q("UPDATE `contact` SET `photo` = '%s',
269                         `thumb` = '%s',
270                         `micro` = '%s',
271                         `name-date` = '%s',
272                         `uri-date` = '%s',
273                         `avatar-date` = '%s'
274                         WHERE `id` = %d",
275                         dbesc($photos[0]),
276                         dbesc($photos[1]),
277                         dbesc($photos[2]),
278                         dbesc(datetime_convert()),
279                         dbesc(datetime_convert()),
280                         dbesc(datetime_convert()),
281                         intval($contact_id)
282                 );
283
284
285         // pull feed and consume it, which should subscribe to the hub.
286
287         proc_run('php',"include/poller.php","$contact_id");
288
289         // create a follow slap
290
291         $tpl = get_markup_template('follow_slap.tpl');
292         $slap = replace_macros($tpl, array(
293                 '$name' => $a->user['username'],
294                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
295                 '$photo' => $a->contact['photo'],
296                 '$thumb' => $a->contact['thumb'],
297                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
298                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
299                 '$title' => '',
300                 '$type' => 'text',
301                 '$content' => t('following'),
302                 '$nick' => $a->user['nickname'],
303                 '$verb' => ACTIVITY_FOLLOW,
304                 '$ostat_follow' => ''
305         ));
306
307         $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
308                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
309                         intval($uid)
310         );
311
312         if(count($r)) {
313                 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
314                         require_once('include/salmon.php');
315                         slapper($r[0],$contact['notify'],$slap);
316                 }
317                 if($contact['network'] == NETWORK_DIASPORA) {
318                         require_once('include/diaspora.php');
319                         $ret = diaspora_share($a->user,$contact);
320                         logger('mod_follow: diaspora_share returns: ' . $ret);
321                 }
322         }
323
324         $result['success'] = true;
325         return $result;
326 }