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