3 * @file include/follow.php
6 use Friendica\Core\Config;
7 use Friendica\Core\System;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBM;
10 use Friendica\Network\Probe;
11 use Friendica\Object\Contact;
12 use Friendica\Protocol\Diaspora;
13 use Friendica\Protocol\OStatus;
14 use Friendica\Protocol\PortableContact;
16 require_once 'include/group.php';
17 require_once 'include/salmon.php';
19 function update_contact($id) {
21 Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
22 This will reliably kill your communication with Friendica contacts.
25 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
29 $ret = Probe::uri($r[0]["url"]);
31 // If Probe::uri fails the network code will be different
32 if ($ret["network"] != $r[0]["network"])
37 // make sure to not overwrite existing values with blank entries
38 foreach ($ret AS $key => $val) {
39 if (isset($r[0][$key]) && ($r[0][$key] != "") && ($val == ""))
40 $ret[$key] = $r[0][$key];
42 if (isset($r[0][$key]) && ($ret[$key] != $r[0][$key]))
49 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
51 dbesc(normalise_link($ret['url'])),
55 dbesc($ret['notify']),
61 // Update the corresponding gcontact entry
62 PortableContact::lastUpdated($ret["url"]);
68 // Takes a $uid and a url/handle and adds a new contact
69 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
72 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
74 // $return['success'] boolean true if successful
75 // $return['message'] error text if success is false.
79 function new_contact($uid, $url, $interactive = false, $network = '') {
81 $result = array('cid' => -1, 'success' => false,'message' => '');
85 // remove ajax junk, e.g. Twitter
87 $url = str_replace('/#!/','/',$url);
89 if (! allowed_url($url)) {
90 $result['message'] = t('Disallowed profile URL.');
94 if (blocked_url($url)) {
95 $result['message'] = t('Blocked domain');
100 $result['message'] = t('Connect URL missing.');
104 $arr = array('url' => $url, 'contact' => array());
106 call_hooks('follow', $arr);
108 if (x($arr['contact'],'name')) {
109 $ret = $arr['contact'];
111 $ret = Probe::uri($url, $network, $uid, false);
114 if (($network != '') && ($ret['network'] != $network)) {
115 logger('Expected network '.$network.' does not match actual network '.$ret['network']);
119 if ($ret['network'] === NETWORK_DFRN) {
121 if (strlen($a->path)) {
122 $myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
124 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
127 goaway($ret['request'] . "&addr=$myaddr");
131 } elseif (Config::get('system','dfrn_only')) {
132 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
133 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
137 // This extra param just confuses things, remove it
138 if ($ret['network'] === NETWORK_DIASPORA) {
139 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
142 // do we have enough information?
144 if (! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
145 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
146 if (! x($ret,'poll')) {
147 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
149 if (! x($ret,'name')) {
150 $result['message'] .= t('An author or name was not found.') . EOL;
152 if (! x($ret,'url')) {
153 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
155 if (strpos($url,'@') !== false) {
156 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
157 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
162 if ($ret['network'] === NETWORK_OSTATUS && Config::get('system','ostatus_disabled')) {
163 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
167 if (! $ret['notify']) {
168 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
171 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
173 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
175 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
177 if (in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) {
181 // check if we already have a contact
182 // the poll url is more reliable than the profile url, as we may have
183 // indirect links or webfinger links
185 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
188 dbesc(normalise_link($ret['poll'])),
189 dbesc($ret['network'])
192 if (!DBM::is_result($r))
193 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
194 intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
197 if (DBM::is_result($r)) {
199 $new_relation = (($r[0]['rel'] == CONTACT_IS_FOLLOWER) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
201 $fields = array('rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false);
202 dba::update('contact', $fields, array('id' => $r[0]['id']));
204 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
206 // create contact record
207 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
208 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
209 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
211 dbesc(datetime_convert()),
213 dbesc(normalise_link($ret['url'])),
215 dbesc($ret['alias']),
216 dbesc($ret['batch']),
217 dbesc($ret['notify']),
222 dbesc($ret['network']),
223 dbesc($ret['pubkey']),
224 intval($new_relation),
225 intval($ret['priority']),
232 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
234 dbesc($ret['network']),
238 if (! DBM::is_result($r)) {
239 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
244 $contact_id = $r[0]['id'];
245 $result['cid'] = $contact_id;
247 $def_gid = get_default_group($uid, $contact["network"]);
248 if (intval($def_gid)) {
249 group_add_member($uid, '', $contact_id, $def_gid);
253 Contact::updateAvatar($ret['photo'], $uid, $contact_id);
255 // pull feed and consume it, which should subscribe to the hub.
257 Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force");
259 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
260 WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
264 if (DBM::is_result($r)) {
265 if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
266 // create a follow slap
268 $item['verb'] = ACTIVITY_FOLLOW;
269 $item['follow'] = $contact["url"];
270 $slap = OStatus::salmon($item, $r[0]);
271 slapper($r[0], $contact['notify'], $slap);
274 if ($contact['network'] == NETWORK_DIASPORA) {
275 $ret = Diaspora::sendShare($a->user, $contact);
276 logger('share returns: '.$ret);
280 $result['success'] = true;