4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Core\Worker;
7 use Friendica\Network\Probe;
8 use Friendica\Protocol\Diaspora;
10 require_once 'include/socgraph.php';
11 require_once 'include/group.php';
12 require_once 'include/salmon.php';
13 require_once 'include/ostatus.php';
14 require_once 'include/Photo.php';
16 function update_contact($id) {
18 Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
19 This will reliably kill your communication with Friendica contacts.
22 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
26 $ret = Probe::uri($r[0]["url"]);
28 // If Probe::uri fails the network code will be different
29 if ($ret["network"] != $r[0]["network"])
34 // make sure to not overwrite existing values with blank entries
35 foreach ($ret AS $key => $val) {
36 if (isset($r[0][$key]) && ($r[0][$key] != "") && ($val == ""))
37 $ret[$key] = $r[0][$key];
39 if (isset($r[0][$key]) && ($ret[$key] != $r[0][$key]))
46 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
48 dbesc(normalise_link($ret['url'])),
52 dbesc($ret['notify']),
58 // Update the corresponding gcontact entry
59 poco_last_updated($ret["url"]);
65 // Takes a $uid and a url/handle and adds a new contact
66 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
69 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
71 // $return['success'] boolean true if successful
72 // $return['message'] error text if success is false.
76 function new_contact($uid, $url, $interactive = false, $network = '') {
78 $result = array('cid' => -1, 'success' => false,'message' => '');
82 // remove ajax junk, e.g. Twitter
84 $url = str_replace('/#!/','/',$url);
86 if (! allowed_url($url)) {
87 $result['message'] = t('Disallowed profile URL.');
91 if (blocked_url($url)) {
92 $result['message'] = t('Blocked domain');
97 $result['message'] = t('Connect URL missing.');
101 $arr = array('url' => $url, 'contact' => array());
103 call_hooks('follow', $arr);
105 if (x($arr['contact'],'name')) {
106 $ret = $arr['contact'];
108 $ret = Probe::uri($url, $network, $uid, false);
111 if (($network != '') && ($ret['network'] != $network)) {
112 logger('Expected network '.$network.' does not match actual network '.$ret['network']);
116 if ($ret['network'] === NETWORK_DFRN) {
118 if (strlen($a->path)) {
119 $myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
121 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
124 goaway($ret['request'] . "&addr=$myaddr");
128 } elseif (Config::get('system','dfrn_only')) {
129 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
130 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
134 // This extra param just confuses things, remove it
135 if ($ret['network'] === NETWORK_DIASPORA) {
136 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
139 // do we have enough information?
141 if (! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
142 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
143 if (! x($ret,'poll')) {
144 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
146 if (! x($ret,'name')) {
147 $result['message'] .= t('An author or name was not found.') . EOL;
149 if (! x($ret,'url')) {
150 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
152 if (strpos($url,'@') !== false) {
153 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
154 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
159 if ($ret['network'] === NETWORK_OSTATUS && Config::get('system','ostatus_disabled')) {
160 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
164 if (! $ret['notify']) {
165 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
168 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
170 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
172 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
174 if (in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) {
178 // check if we already have a contact
179 // the poll url is more reliable than the profile url, as we may have
180 // indirect links or webfinger links
182 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
185 dbesc(normalise_link($ret['poll'])),
186 dbesc($ret['network'])
189 if (!dbm::is_result($r))
190 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
191 intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
194 if (dbm::is_result($r)) {
196 $new_relation = (($r[0]['rel'] == CONTACT_IS_FOLLOWER) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
198 $fields = array('rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false);
199 dba::update('contact', $fields, array('id' => $r[0]['id']));
201 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
203 // create contact record
204 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
205 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
206 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
208 dbesc(datetime_convert()),
210 dbesc(normalise_link($ret['url'])),
212 dbesc($ret['alias']),
213 dbesc($ret['batch']),
214 dbesc($ret['notify']),
219 dbesc($ret['network']),
220 dbesc($ret['pubkey']),
221 intval($new_relation),
222 intval($ret['priority']),
229 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
231 dbesc($ret['network']),
235 if (! dbm::is_result($r)) {
236 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
241 $contact_id = $r[0]['id'];
242 $result['cid'] = $contact_id;
244 $def_gid = get_default_group($uid, $contact["network"]);
245 if (intval($def_gid)) {
246 group_add_member($uid, '', $contact_id, $def_gid);
250 update_contact_avatar($ret['photo'],$uid,$contact_id);
252 // pull feed and consume it, which should subscribe to the hub.
254 Worker::add(PRIORITY_HIGH, "onepoll", $contact_id, "force");
256 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
257 WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
261 if (dbm::is_result($r)) {
262 if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
263 // create a follow slap
265 $item['verb'] = ACTIVITY_FOLLOW;
266 $item['follow'] = $contact["url"];
267 $slap = ostatus::salmon($item, $r[0]);
268 slapper($r[0], $contact['notify'], $slap);
271 if ($contact['network'] == NETWORK_DIASPORA) {
272 $ret = Diaspora::send_share($a->user,$contact);
273 logger('share returns: '.$ret);
277 $result['success'] = true;