]> git.mxchange.org Git - friendica.git/blob - include/follow.php
The number of queries is reduced dramatically
[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 (blocked_url($url)) {
86                 $result['message'] = t('Blocked domain');
87                 return $result;
88         }
89
90         if (! $url) {
91                 $result['message'] = t('Connect URL missing.');
92                 return $result;
93         }
94
95         $arr = array('url' => $url, 'contact' => array());
96
97         call_hooks('follow', $arr);
98
99         if (x($arr['contact'],'name')) {
100                 $ret = $arr['contact'];
101         }
102         else {
103                 $ret = probe_url($url);
104         }
105
106         if ($ret['network'] === NETWORK_DFRN) {
107                 if ($interactive) {
108                         if (strlen($a->path)) {
109                                 $myaddr = bin2hex(App::get_baseurl() . '/profile/' . $a->user['nickname']);
110                         } else {
111                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
112                         }
113
114                         goaway($ret['request'] . "&addr=$myaddr");
115
116                         // NOTREACHED
117                 }
118         } elseif (get_config('system','dfrn_only')) {
119                 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
120                 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
121                 return $result;
122         }
123
124         // This extra param just confuses things, remove it
125         if ($ret['network'] === NETWORK_DIASPORA) {
126                 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
127         }
128
129         // do we have enough information?
130
131         if (! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
132                 $result['message'] .=  t('The profile address specified does not provide adequate information.') . EOL;
133                 if (! x($ret,'poll')) {
134                         $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
135                 }
136                 if (! x($ret,'name')) {
137                         $result['message'] .=  t('An author or name was not found.') . EOL;
138                 }
139                 if (! x($ret,'url')) {
140                         $result['message'] .=  t('No browser URL could be matched to this address.') . EOL;
141                 }
142                 if (strpos($url,'@') !== false) {
143                         $result['message'] .=  t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
144                         $result['message'] .=  t('Use mailto: in front of address to force email check.') . EOL;
145                 }
146                 return $result;
147         }
148
149         if ($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
150                 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
151                 $ret['notify'] = '';
152         }
153
154         if (! $ret['notify']) {
155                 $result['message'] .=  t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
156         }
157
158         $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
159
160         $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
161
162         $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
163
164         if (in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) {
165                 $writeable = 1;
166         }
167
168         // check if we already have a contact
169         // the poll url is more reliable than the profile url, as we may have
170         // indirect links or webfinger links
171
172         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
173                 intval($uid),
174                 dbesc($ret['poll']),
175                 dbesc(normalise_link($ret['poll'])),
176                 dbesc($ret['network'])
177         );
178
179         if (!dbm::is_result($r))
180                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
181                         intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
182         );
183
184         if (dbm::is_result($r)) {
185                 // update contact
186                 if ($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
187                         q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
188                                 intval(CONTACT_IS_FRIEND),
189                                 intval($subhub),
190                                 intval($r[0]['id']),
191                                 intval($uid)
192                         );
193                 }
194         } else {
195                 // check service class limits
196
197                 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `pending` = 0 AND `self` = 0",
198                         intval($uid)
199                 );
200                 if (dbm::is_result($r))
201                         $total_contacts = $r[0]['total'];
202
203                 if (! service_class_allows($uid,'total_contacts',$total_contacts)) {
204                         $result['message'] .= upgrade_message();
205                         return $result;
206                 }
207
208                 $r = q("SELECT COUNT(`network`) AS `total` FROM `contact` WHERE `uid` = %d AND `network` = '%s' AND `pending` = 0 AND `self` = 0",
209                         intval($uid),
210                         dbesc($network)
211                 );
212                 if (dbm::is_result($r)) {
213                         $total_network = $r[0]['total'];
214                 }
215
216                 if (! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
217                         $result['message'] .= upgrade_message();
218                         return $result;
219                 }
220
221                 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
222
223                 // create contact record
224                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
225                         `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
226                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
227                         intval($uid),
228                         dbesc(datetime_convert()),
229                         dbesc($ret['url']),
230                         dbesc(normalise_link($ret['url'])),
231                         dbesc($ret['addr']),
232                         dbesc($ret['alias']),
233                         dbesc($ret['batch']),
234                         dbesc($ret['notify']),
235                         dbesc($ret['poll']),
236                         dbesc($ret['poco']),
237                         dbesc($ret['name']),
238                         dbesc($ret['nick']),
239                         dbesc($ret['network']),
240                         dbesc($ret['pubkey']),
241                         intval($new_relation),
242                         intval($ret['priority']),
243                         intval($writeable),
244                         intval($hidden),
245                         intval($subhub)
246                 );
247         }
248
249         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
250                 dbesc($ret['url']),
251                 dbesc($ret['network']),
252                 intval($uid)
253         );
254
255         if (! dbm::is_result($r)) {
256                 $result['message'] .=  t('Unable to retrieve contact information.') . EOL;
257                 return $result;
258         }
259
260         $contact = $r[0];
261         $contact_id  = $r[0]['id'];
262         $result['cid'] = $contact_id;
263
264         $def_gid = get_default_group($uid, $contact["network"]);
265         if (intval($def_gid)) {
266                 group_add_member($uid, '', $contact_id, $def_gid);
267         }
268
269         // Update the avatar
270         update_contact_avatar($ret['photo'],$uid,$contact_id);
271
272         // pull feed and consume it, which should subscribe to the hub.
273
274         proc_run(PRIORITY_HIGH, "include/onepoll.php", $contact_id, "force");
275
276         $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
277                         WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
278                         intval($uid)
279         );
280
281         if (dbm::is_result($r)) {
282                 if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
283                         // create a follow slap
284                         $item = array();
285                         $item['verb'] = ACTIVITY_FOLLOW;
286                         $item['follow'] = $contact["url"];
287                         $slap = ostatus::salmon($item, $r[0]);
288                         slapper($r[0], $contact['notify'], $slap);
289                 }
290
291                 if ($contact['network'] == NETWORK_DIASPORA) {
292                         $ret = Diaspora::send_share($a->user,$contact);
293                         logger('share returns: '.$ret);
294                 }
295         }
296
297         $result['success'] = true;
298         return $result;
299 }