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