2 require_once("include/Scrape.php");
3 require_once("include/socgraph.php");
4 require_once('include/group.php');
6 function update_contact($id) {
8 Warning: Never ever fetch the public key via probe_url and write it into the contacts.
9 This will reliably kill your communication with Friendica contacts.
12 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
16 $ret = probe_url($r[0]["url"]);
18 // If probe_url fails the network code will be different
19 if ($ret["network"] != $r[0]["network"])
24 // make sure to not overwrite existing values with blank entries
25 foreach ($ret AS $key => $val) {
26 if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
27 $ret[$key] = $r[0][$key];
29 if (isset($r[0][$key]) AND ($ret[$key] != $r[0][$key]))
36 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
38 dbesc(normalise_link($ret['url'])),
42 dbesc($ret['notify']),
48 // Update the corresponding gcontact entry
49 poco_last_updated($ret["url"]);
55 // Takes a $uid and a url/handle and adds a new contact
56 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
59 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
61 // $return['success'] boolean true if successful
62 // $return['message'] error text if success is false.
66 function new_contact($uid,$url,$interactive = false) {
68 $result = array('cid' => -1, 'success' => false,'message' => '');
72 // remove ajax junk, e.g. Twitter
74 $url = str_replace('/#!/','/',$url);
76 if(! allowed_url($url)) {
77 $result['message'] = t('Disallowed profile URL.');
82 $result['message'] = t('Connect URL missing.');
86 $arr = array('url' => $url, 'contact' => array());
88 call_hooks('follow', $arr);
90 if(x($arr['contact'],'name'))
91 $ret = $arr['contact'];
93 $ret = probe_url($url);
95 if($ret['network'] === NETWORK_DFRN) {
98 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
100 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
102 goaway($ret['request'] . "&addr=$myaddr");
108 if(get_config('system','dfrn_only')) {
109 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
110 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
120 // This extra param just confuses things, remove it
121 if($ret['network'] === NETWORK_DIASPORA)
122 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
125 // do we have enough information?
127 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
128 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
130 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
132 $result['message'] .= t('An author or name was not found.') . EOL;
134 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
135 if(strpos($url,'@') !== false) {
136 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
137 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
142 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
143 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
152 if(! $ret['notify']) {
153 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
156 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
158 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
160 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
162 if(in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA)))
165 // check if we already have a contact
166 // the poll url is more reliable than the profile url, as we may have
167 // indirect links or webfinger links
169 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
172 dbesc(normalise_link($ret['poll'])),
173 dbesc($ret['network'])
177 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
178 intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
183 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
184 q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
185 intval(CONTACT_IS_FRIEND),
194 // check service class limits
196 $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0",
200 $total_contacts = $r[0]['total'];
202 if(! service_class_allows($uid,'total_contacts',$total_contacts)) {
203 $result['message'] .= upgrade_message();
207 $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0",
212 $total_network = $r[0]['total'];
214 if(! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
215 $result['message'] .= upgrade_message();
219 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
221 // create contact record
222 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
223 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
224 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
226 dbesc(datetime_convert()),
228 dbesc(normalise_link($ret['url'])),
230 dbesc($ret['alias']),
231 dbesc($ret['batch']),
232 dbesc($ret['notify']),
237 dbesc($ret['network']),
238 dbesc($ret['pubkey']),
239 intval($new_relation),
240 intval($ret['priority']),
247 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
249 dbesc($ret['network']),
254 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
259 $contact_id = $r[0]['id'];
260 $result['cid'] = $contact_id;
262 $def_gid = get_default_group($uid, $contact["network"]);
263 if (intval($def_gid))
264 group_add_member($uid, '', $contact_id, $def_gid);
266 require_once("include/Photo.php");
269 update_contact_avatar($ret['photo'],$uid,$contact_id);
271 // pull feed and consume it, which should subscribe to the hub.
273 proc_run(PRIORITY_MEDIUM, "include/onepoll.php", $contact_id, "force");
275 // create a follow slap
277 $tpl = get_markup_template('follow_slap.tpl');
278 $slap = replace_macros($tpl, array(
279 '$name' => $a->user['username'],
280 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
281 '$photo' => $a->contact['photo'],
282 '$thumb' => $a->contact['thumb'],
283 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
284 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . get_guid(32),
287 '$content' => t('following'),
288 '$nick' => $a->user['nickname'],
289 '$verb' => ACTIVITY_FOLLOW,
290 '$ostat_follow' => ''
293 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
294 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
299 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
300 require_once('include/salmon.php');
301 slapper($r[0],$contact['notify'],$slap);
303 if($contact['network'] == NETWORK_DIASPORA) {
304 require_once('include/diaspora.php');
305 $ret = diaspora::send_share($a->user,$contact);
306 logger('share returns: '.$ret);
310 $result['success'] = true;