4 * @file include/follow.php
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;
20 function update_contact($id)
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.
27 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
32 $ret = Probe::uri($r[0]["url"]);
34 // If Probe::uri fails the network code will be different
35 if ($ret["network"] != $r[0]["network"]) {
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];
46 if (isset($r[0][$key]) && ($ret[$key] != $r[0][$key]))
54 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
56 dbesc(normalise_link($ret['url'])),
60 dbesc($ret['notify']),
66 // Update the corresponding gcontact entry
67 PortableContact::lastUpdated($ret["url"]);
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
77 * Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
80 * $return['success'] boolean true if successful
81 * $return['message'] error text if success is false.
83 * @brief Takes a $uid and a url/handle and adds a new contact
86 * @param bool $interactive
87 * @param string $network
88 * @return boolean|string
90 function new_contact($uid, $url, $interactive = false, $network = '')
92 $result = array('cid' => -1, 'success' => false, 'message' => '');
96 // remove ajax junk, e.g. Twitter
97 $url = str_replace('/#!/', '/', $url);
99 if (!allowed_url($url)) {
100 $result['message'] = t('Disallowed profile URL.');
104 if (blocked_url($url)) {
105 $result['message'] = t('Blocked domain');
110 $result['message'] = t('Connect URL missing.');
114 $arr = array('url' => $url, 'contact' => array());
116 call_hooks('follow', $arr);
118 if (x($arr['contact'], 'name')) {
119 $ret = $arr['contact'];
121 $ret = Probe::uri($url, $network, $uid, false);
124 if (($network != '') && ($ret['network'] != $network)) {
125 logger('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
129 if ($ret['network'] === NETWORK_DFRN) {
131 if (strlen($a->path)) {
132 $myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
134 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
137 goaway($ret['request'] . "&addr=$myaddr");
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;
147 // This extra param just confuses things, remove it
148 if ($ret['network'] === NETWORK_DIASPORA) {
149 $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
152 // do we have enough information?
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;
159 if (!x($ret, 'name')) {
160 $result['message'] .= t('An author or name was not found.') . EOL;
162 if (!x($ret, 'url')) {
163 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
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;
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;
177 if (!$ret['notify']) {
178 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
181 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
183 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
185 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
187 if (in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) {
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
195 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
198 dbesc(normalise_link($ret['poll'])),
199 dbesc($ret['network'])
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'])
208 if (DBM::is_result($r)) {
210 $new_relation = (($r[0]['rel'] == CONTACT_IS_FOLLOWER) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
212 $fields = array('rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false);
213 dba::update('contact', $fields, array('id' => $r[0]['id']));
215 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
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 ) ",
222 dbesc(datetime_convert()),
224 dbesc(normalise_link($ret['url'])),
226 dbesc($ret['alias']),
227 dbesc($ret['batch']),
228 dbesc($ret['notify']),
233 dbesc($ret['network']),
234 dbesc($ret['pubkey']),
235 intval($new_relation),
236 intval($ret['priority']),
243 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
245 dbesc($ret['network']),
249 if (!DBM::is_result($r)) {
250 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
255 $contact_id = $r[0]['id'];
256 $result['cid'] = $contact_id;
258 Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id);
261 Contact::updateAvatar($ret['photo'], $uid, $contact_id);
263 // pull feed and consume it, which should subscribe to the hub.
265 Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force");
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",
272 if (DBM::is_result($r)) {
273 if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
274 // create a follow slap
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);
282 if ($contact['network'] == NETWORK_DIASPORA) {
283 $ret = Diaspora::sendShare($a->user, $contact);
284 logger('share returns: ' . $ret);
288 $result['success'] = true;