3 function update_contact($id) {
5 Warning: Never ever fetch the public key via probe_url and write it into the contacts.
6 This will reliably kill your communication with Friendica contacts.
9 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network` FROM `contact` WHERE `id` = %d", intval($id));
13 $ret = probe_url($r[0]["url"]);
15 // If probe_url fails the network code will be different
16 if ($ret["network"] != $r[0]["network"])
19 // make sure to not overwrite existing values with blank entries
20 foreach ($ret AS $key => $val)
21 if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
22 $ret[$key] = $r[0][$key];
24 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s', `name` = '%s', `nick` = '%s' WHERE `id` = %d",
26 dbesc(normalise_link($ret['url'])),
30 dbesc($ret['notify']),
40 // Takes a $uid and a url/handle and adds a new contact
41 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
44 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
46 // $return['success'] boolean true if successful
47 // $return['message'] error text if success is false.
51 function new_contact($uid,$url,$interactive = false) {
53 $result = array('cid' => -1, 'success' => false,'message' => '');
57 // remove ajax junk, e.g. Twitter
59 $url = str_replace('/#!/','/',$url);
61 if(! allowed_url($url)) {
62 $result['message'] = t('Disallowed profile URL.');
67 $result['message'] = t('Connect URL missing.');
71 $arr = array('url' => $url, 'contact' => array());
73 call_hooks('follow', $arr);
75 if(x($arr['contact'],'name'))
76 $ret = $arr['contact'];
78 $ret = probe_url($url);
80 if($ret['network'] === NETWORK_DFRN) {
83 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
85 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
87 goaway($ret['request'] . "&addr=$myaddr");
93 if(get_config('system','dfrn_only')) {
94 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
95 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
105 // This extra param just confuses things, remove it
106 if($ret['network'] === NETWORK_DIASPORA)
107 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
110 // do we have enough information?
112 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
113 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
115 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
117 $result['message'] .= t('An author or name was not found.') . EOL;
119 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
120 if(strpos($url,'@') !== false) {
121 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
122 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
127 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
128 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
137 if(! $ret['notify']) {
138 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
141 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
143 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
145 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
147 if($ret['network'] === NETWORK_MAIL) {
151 if($ret['network'] === NETWORK_DIASPORA)
154 // check if we already have a contact
155 // the poll url is more reliable than the profile url, as we may have
156 // indirect links or webfinger links
158 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
161 dbesc(normalise_link($ret['poll'])),
162 dbesc($ret['network'])
167 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
168 q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
169 intval(CONTACT_IS_FRIEND),
179 // check service class limits
181 $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0",
185 $total_contacts = $r[0]['total'];
187 if(! service_class_allows($uid,'total_contacts',$total_contacts)) {
188 $result['message'] .= upgrade_message();
192 $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0",
197 $total_network = $r[0]['total'];
199 if(! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
200 $result['message'] .= upgrade_message();
204 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
205 if($ret['network'] === NETWORK_DIASPORA)
206 $new_relation = CONTACT_IS_FOLLOWER;
208 // create contact record
209 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
210 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
211 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
213 dbesc(datetime_convert()),
215 dbesc(normalise_link($ret['url'])),
217 dbesc($ret['alias']),
218 dbesc($ret['batch']),
219 dbesc($ret['notify']),
224 dbesc($ret['network']),
225 dbesc($ret['pubkey']),
226 intval($new_relation),
227 intval($ret['priority']),
234 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
236 dbesc($ret['network']),
241 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
246 $contact_id = $r[0]['id'];
247 $result['cid'] = $contact_id;
249 $g = q("select def_gid from user where uid = %d limit 1",
252 if($g && intval($g[0]['def_gid'])) {
253 require_once('include/group.php');
254 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
257 require_once("include/Photo.php");
259 $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
261 $r = q("UPDATE `contact` SET `photo` = '%s',
271 dbesc(datetime_convert()),
272 dbesc(datetime_convert()),
273 dbesc(datetime_convert()),
278 // pull feed and consume it, which should subscribe to the hub.
280 proc_run('php',"include/poller.php","$contact_id");
282 // create a follow slap
284 $tpl = get_markup_template('follow_slap.tpl');
285 $slap = replace_macros($tpl, array(
286 '$name' => $a->user['username'],
287 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
288 '$photo' => $a->contact['photo'],
289 '$thumb' => $a->contact['thumb'],
290 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
291 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
294 '$content' => t('following'),
295 '$nick' => $a->user['nickname'],
296 '$verb' => ACTIVITY_FOLLOW,
297 '$ostat_follow' => ''
300 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
301 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
306 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
307 require_once('include/salmon.php');
308 slapper($r[0],$contact['notify'],$slap);
310 if($contact['network'] == NETWORK_DIASPORA) {
311 require_once('include/diaspora.php');
312 $ret = diaspora_share($a->user,$contact);
313 logger('mod_follow: diaspora_share returns: ' . $ret);
317 $result['success'] = true;