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`, `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"])
21 // make sure to not overwrite existing values with blank entries
22 foreach ($ret AS $key => $val) {
23 if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
24 $ret[$key] = $r[0][$key];
26 if (isset($r[0][$key]) AND ($ret[$key] != $r[0][$key]))
33 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
35 dbesc(normalise_link($ret['url'])),
39 dbesc($ret['notify']),
47 // Takes a $uid and a url/handle and adds a new contact
48 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
51 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
53 // $return['success'] boolean true if successful
54 // $return['message'] error text if success is false.
58 function new_contact($uid,$url,$interactive = false) {
60 $result = array('cid' => -1, 'success' => false,'message' => '');
64 // remove ajax junk, e.g. Twitter
66 $url = str_replace('/#!/','/',$url);
68 if(! allowed_url($url)) {
69 $result['message'] = t('Disallowed profile URL.');
74 $result['message'] = t('Connect URL missing.');
78 $arr = array('url' => $url, 'contact' => array());
80 call_hooks('follow', $arr);
82 if(x($arr['contact'],'name'))
83 $ret = $arr['contact'];
85 $ret = probe_url($url);
87 if($ret['network'] === NETWORK_DFRN) {
90 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
92 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
94 goaway($ret['request'] . "&addr=$myaddr");
100 if(get_config('system','dfrn_only')) {
101 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
102 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
112 // This extra param just confuses things, remove it
113 if($ret['network'] === NETWORK_DIASPORA)
114 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
117 // do we have enough information?
119 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
120 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
122 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
124 $result['message'] .= t('An author or name was not found.') . EOL;
126 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
127 if(strpos($url,'@') !== false) {
128 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
129 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
134 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
135 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
144 if(! $ret['notify']) {
145 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
148 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
150 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
152 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
154 if($ret['network'] === NETWORK_MAIL) {
158 if($ret['network'] === NETWORK_DIASPORA)
161 // check if we already have a contact
162 // the poll url is more reliable than the profile url, as we may have
163 // indirect links or webfinger links
165 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
168 dbesc(normalise_link($ret['poll'])),
169 dbesc($ret['network'])
174 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
175 q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
176 intval(CONTACT_IS_FRIEND),
186 // check service class limits
188 $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0",
192 $total_contacts = $r[0]['total'];
194 if(! service_class_allows($uid,'total_contacts',$total_contacts)) {
195 $result['message'] .= upgrade_message();
199 $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0",
204 $total_network = $r[0]['total'];
206 if(! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
207 $result['message'] .= upgrade_message();
211 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
212 if($ret['network'] === NETWORK_DIASPORA)
213 $new_relation = CONTACT_IS_FOLLOWER;
215 // create contact record
216 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
217 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
218 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
220 dbesc(datetime_convert()),
222 dbesc(normalise_link($ret['url'])),
224 dbesc($ret['alias']),
225 dbesc($ret['batch']),
226 dbesc($ret['notify']),
231 dbesc($ret['network']),
232 dbesc($ret['pubkey']),
233 intval($new_relation),
234 intval($ret['priority']),
241 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
243 dbesc($ret['network']),
248 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
253 $contact_id = $r[0]['id'];
254 $result['cid'] = $contact_id;
256 $g = q("select def_gid from user where uid = %d limit 1",
259 if($g && intval($g[0]['def_gid'])) {
260 require_once('include/group.php');
261 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
264 require_once("include/Photo.php");
266 $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
268 $r = q("UPDATE `contact` SET `photo` = '%s',
278 dbesc(datetime_convert()),
279 dbesc(datetime_convert()),
280 dbesc(datetime_convert()),
285 // pull feed and consume it, which should subscribe to the hub.
287 proc_run('php',"include/poller.php","$contact_id");
289 // create a follow slap
291 $tpl = get_markup_template('follow_slap.tpl');
292 $slap = replace_macros($tpl, array(
293 '$name' => $a->user['username'],
294 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
295 '$photo' => $a->contact['photo'],
296 '$thumb' => $a->contact['thumb'],
297 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
298 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
301 '$content' => t('following'),
302 '$nick' => $a->user['nickname'],
303 '$verb' => ACTIVITY_FOLLOW,
304 '$ostat_follow' => ''
307 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
308 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
313 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
314 require_once('include/salmon.php');
315 slapper($r[0],$contact['notify'],$slap);
317 if($contact['network'] == NETWORK_DIASPORA) {
318 require_once('include/diaspora.php');
319 $ret = diaspora_share($a->user,$contact);
320 logger('mod_follow: diaspora_share returns: ' . $ret);
324 $result['success'] = true;