]> git.mxchange.org Git - friendica.git/blob - include/follow.php
8d80538e0c924856c51c0547511d4e4769f750aa
[friendica.git] / include / follow.php
1 <?php
2 require_once("include/Scrape.php");
3 require_once("include/socgraph.php");
4 require_once('include/group.php');
5 require_once('include/salmon.php');
6 require_once('include/ostatus.php');
7 require_once("include/Photo.php");
8 require_once('include/diaspora.php');
9
10 function update_contact($id) {
11         /*
12         Warning: Never ever fetch the public key via probe_url and write it into the contacts.
13         This will reliably kill your communication with Friendica contacts.
14         */
15
16         $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
17         if (!$r)
18                 return false;
19
20         $ret = probe_url($r[0]["url"]);
21
22         // If probe_url fails the network code will be different
23         if ($ret["network"] != $r[0]["network"])
24                 return false;
25
26         $update = false;
27
28         // make sure to not overwrite existing values with blank entries
29         foreach ($ret AS $key => $val) {
30                 if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
31                         $ret[$key] = $r[0][$key];
32
33                 if (isset($r[0][$key]) AND ($ret[$key] != $r[0][$key]))
34                         $update = true;
35         }
36
37         if (!$update)
38                 return true;
39
40         q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
41                 dbesc($ret['url']),
42                 dbesc(normalise_link($ret['url'])),
43                 dbesc($ret['addr']),
44                 dbesc($ret['alias']),
45                 dbesc($ret['batch']),
46                 dbesc($ret['notify']),
47                 dbesc($ret['poll']),
48                 dbesc($ret['poco']),
49                 intval($id)
50         );
51
52         // Update the corresponding gcontact entry
53         poco_last_updated($ret["url"]);
54
55         return true;
56 }
57
58 //
59 // Takes a $uid and a url/handle and adds a new contact
60 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
61 // dfrn_request page.
62
63 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
64 // Returns an array
65 //  $return['success'] boolean true if successful
66 //  $return['message'] error text if success is false.
67
68
69
70 function new_contact($uid,$url,$interactive = false) {
71
72         $result = array('cid' => -1, 'success' => false,'message' => '');
73
74         $a = get_app();
75
76         // remove ajax junk, e.g. Twitter
77
78         $url = str_replace('/#!/','/',$url);
79
80         if (! allowed_url($url)) {
81                 $result['message'] = t('Disallowed profile URL.');
82                 return $result;
83         }
84
85         if (! $url) {
86                 $result['message'] = t('Connect URL missing.');
87                 return $result;
88         }
89
90         $arr = array('url' => $url, 'contact' => array());
91
92         call_hooks('follow', $arr);
93
94         if (x($arr['contact'],'name')) {
95                 $ret = $arr['contact'];
96         }
97         else {
98                 $ret = probe_url($url);
99         }
100
101         if ($ret['network'] === NETWORK_DFRN) {
102                 if ($interactive) {
103                         if (strlen($a->path)) {
104                                 $myaddr = bin2hex(App::get_baseurl() . '/profile/' . $a->user['nickname']);
105                         } else {
106                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
107                         }
108
109                         goaway($ret['request'] . "&addr=$myaddr");
110
111                         // NOTREACHED
112                 }
113         } else {
114                 if (get_config('system','dfrn_only')) {
115                         $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
116                         $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
117                         return $result;
118                 }
119         }
120
121         // This extra param just confuses things, remove it
122         if ($ret['network'] === NETWORK_DIASPORA) {
123                 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
124         }
125
126         // do we have enough information?
127
128         if (! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
129                 $result['message'] .=  t('The profile address specified does not provide adequate information.') . EOL;
130                 if (! x($ret,'poll')) {
131                         $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
132                 }
133                 if (! x($ret,'name')) {
134                         $result['message'] .=  t('An author or name was not found.') . EOL;
135                 }
136                 if (! x($ret,'url')) {
137                         $result['message'] .=  t('No browser URL could be matched to this address.') . EOL;
138                 }
139                 if (strpos($url,'@') !== false) {
140                         $result['message'] .=  t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
141                         $result['message'] .=  t('Use mailto: in front of address to force email check.') . EOL;
142                 }
143                 return $result;
144         }
145
146         if ($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
147                 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
148                 $ret['notify'] = '';
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
165         // check if we already have a contact
166         // the poll url is more reliable than the profile url, as we may have
167         // indirect links or webfinger links
168
169         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
170                 intval($uid),
171                 dbesc($ret['poll']),
172                 dbesc(normalise_link($ret['poll'])),
173                 dbesc($ret['network'])
174         );
175
176         if (!dbm::is_result($r))
177                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
178                         intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
179         );
180
181         if (dbm::is_result($r)) {
182                 // update contact
183                 if ($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
184                         q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
185                                 intval(CONTACT_IS_FRIEND),
186                                 intval($subhub),
187                                 intval($r[0]['id']),
188                                 intval($uid)
189                         );
190                 }
191         } else {
192                 // check service class limits
193
194                 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `pending` = 0 AND `self` = 0",
195                         intval($uid)
196                 );
197                 if (dbm::is_result($r))
198                         $total_contacts = $r[0]['total'];
199
200                 if (! service_class_allows($uid,'total_contacts',$total_contacts)) {
201                         $result['message'] .= upgrade_message();
202                         return $result;
203                 }
204
205                 $r = q("SELECT COUNT(`network`) AS `total` FROM `contact` WHERE `uid` = %d AND `network` = '%s' AND `pending` = 0 AND `self` = 0",
206                         intval($uid),
207                         dbesc($network)
208                 );
209                 if (dbm::is_result($r)) {
210                         $total_network = $r[0]['total'];
211                 }
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 (! dbm::is_result($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         $def_gid = get_default_group($uid, $contact["network"]);
262         if (intval($def_gid)) {
263                 group_add_member($uid, '', $contact_id, $def_gid);
264         }
265
266         // Update the avatar
267         update_contact_avatar($ret['photo'],$uid,$contact_id);
268
269         // pull feed and consume it, which should subscribe to the hub.
270
271         proc_run(PRIORITY_HIGH, "include/onepoll.php", $contact_id, "force");
272
273         $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
274                         WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
275                         intval($uid)
276         );
277
278         if (dbm::is_result($r)) {
279                 if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
280                         // create a follow slap
281                         $item = array();
282                         $item['verb'] = ACTIVITY_FOLLOW;
283                         $item['follow'] = $contact["url"];
284                         $slap = ostatus::salmon($item, $r[0]);
285                         slapper($r[0], $contact['notify'], $slap);
286                 }
287
288                 if ($contact['network'] == NETWORK_DIASPORA) {
289                         $ret = Diaspora::send_share($a->user,$contact);
290                         logger('share returns: '.$ret);
291                 }
292         }
293
294         $result['success'] = true;
295         return $result;
296 }