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