4 use Friendica\Core\System;
5 use Friendica\Network\Probe;
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';
15 function update_contact($id) {
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.
21 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
25 $ret = probe_url($r[0]["url"]);
27 // If probe_url fails the network code will be different
28 if ($ret["network"] != $r[0]["network"])
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];
38 if (isset($r[0][$key]) && ($ret[$key] != $r[0][$key]))
45 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
47 dbesc(normalise_link($ret['url'])),
51 dbesc($ret['notify']),
57 // Update the corresponding gcontact entry
58 poco_last_updated($ret["url"]);
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
68 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
70 // $return['success'] boolean true if successful
71 // $return['message'] error text if success is false.
75 function new_contact($uid, $url, $interactive = false, $network = '') {
77 $result = array('cid' => -1, 'success' => false,'message' => '');
81 // remove ajax junk, e.g. Twitter
83 $url = str_replace('/#!/','/',$url);
85 if (! allowed_url($url)) {
86 $result['message'] = t('Disallowed profile URL.');
90 if (blocked_url($url)) {
91 $result['message'] = t('Blocked domain');
96 $result['message'] = t('Connect URL missing.');
100 $arr = array('url' => $url, 'contact' => array());
102 call_hooks('follow', $arr);
104 if (x($arr['contact'],'name')) {
105 $ret = $arr['contact'];
107 $ret = Probe::uri($url, $network, $uid, false);
110 if (($network != '') && ($ret['network'] != $network)) {
111 logger('Expected network '.$network.' does not match actual network '.$ret['network']);
115 if ($ret['network'] === NETWORK_DFRN) {
117 if (strlen($a->path)) {
118 $myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
120 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
123 goaway($ret['request'] . "&addr=$myaddr");
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;
133 // This extra param just confuses things, remove it
134 if ($ret['network'] === NETWORK_DIASPORA) {
135 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
138 // do we have enough information?
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;
145 if (! x($ret,'name')) {
146 $result['message'] .= t('An author or name was not found.') . EOL;
148 if (! x($ret,'url')) {
149 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
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;
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;
163 if (! $ret['notify']) {
164 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
167 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
169 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
171 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
173 if (in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) {
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
181 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
184 dbesc(normalise_link($ret['poll'])),
185 dbesc($ret['network'])
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'])
193 if (dbm::is_result($r)) {
195 $new_relation = (($r[0]['rel'] == CONTACT_IS_FOLLOWER) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
197 $fields = array('rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false);
198 dba::update('contact', $fields, array('id' => $r[0]['id']));
200 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
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 ) ",
207 dbesc(datetime_convert()),
209 dbesc(normalise_link($ret['url'])),
211 dbesc($ret['alias']),
212 dbesc($ret['batch']),
213 dbesc($ret['notify']),
218 dbesc($ret['network']),
219 dbesc($ret['pubkey']),
220 intval($new_relation),
221 intval($ret['priority']),
228 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
230 dbesc($ret['network']),
234 if (! dbm::is_result($r)) {
235 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
240 $contact_id = $r[0]['id'];
241 $result['cid'] = $contact_id;
243 $def_gid = get_default_group($uid, $contact["network"]);
244 if (intval($def_gid)) {
245 group_add_member($uid, '', $contact_id, $def_gid);
249 update_contact_avatar($ret['photo'],$uid,$contact_id);
251 // pull feed and consume it, which should subscribe to the hub.
253 proc_run(PRIORITY_HIGH, "include/onepoll.php", $contact_id, "force");
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",
260 if (dbm::is_result($r)) {
261 if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
262 // create a follow slap
264 $item['verb'] = ACTIVITY_FOLLOW;
265 $item['follow'] = $contact["url"];
266 $slap = ostatus::salmon($item, $r[0]);
267 slapper($r[0], $contact['notify'], $slap);
270 if ($contact['network'] == NETWORK_DIASPORA) {
271 $ret = Diaspora::send_share($a->user,$contact);
272 logger('share returns: '.$ret);
276 $result['success'] = true;