4 function new_contact($uid,$url,$interactive = false) {
6 $result = array('success' => false,'message' => '');
10 // remove ajax junk, e.g. Twitter
12 $url = str_replace('/#!/','/',$url);
14 if(! allowed_url($url)) {
15 $result['message'] = t('Disallowed profile URL.');
20 $result['message'] = t('Connect URL missing.');
24 $arr = array('url' => $url, 'contact' => array());
26 call_hooks('follow', $arr);
28 if(x($arr['contact'],'name'))
29 $ret = $arr['contact'];
31 $ret = probe_url($url);
33 if($ret['network'] === NETWORK_DFRN) {
36 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
38 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
40 goaway($ret['request'] . "&addr=$myaddr");
46 if(get_config('system','dfrn_only')) {
47 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
48 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
53 // This extra param just confuses things, remove it
54 if($ret['network'] === NETWORK_DIASPORA)
55 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
58 // do we have enough information?
60 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
61 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
63 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
65 $result['message'] .= t('An author or name was not found.') . EOL;
67 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
68 if(strpos($url,'@') !== false) {
69 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
70 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
75 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
76 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
80 if(! $ret['notify']) {
81 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
84 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
85 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
87 if($ret['network'] === NETWORK_MAIL) {
91 if($ret['network'] === NETWORK_DIASPORA)
94 // check if we already have a contact
95 // the poll url is more reliable than the profile url, as we may have
96 // indirect links or webfinger links
98 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
106 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
107 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
108 intval(CONTACT_IS_FRIEND),
116 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
117 if($ret['network'] === NETWORK_DIASPORA)
118 $new_relation = CONTACT_IS_FOLLOWER;
120 // create contact record
121 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
122 `writable`, `hidden`, `blocked`, `readonly`, `pending` )
123 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
125 dbesc(datetime_convert()),
127 dbesc(normalise_link($ret['url'])),
129 dbesc($ret['alias']),
130 dbesc($ret['batch']),
131 dbesc($ret['notify']),
136 dbesc($ret['photo']),
137 dbesc($ret['network']),
138 dbesc($ret['pubkey']),
139 intval($new_relation),
140 intval($ret['priority']),
146 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
152 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
157 $contact_id = $r[0]['id'];
160 $g = q("select def_gid from user where uid = %d limit 1",
163 if($g && intval($g[0]['def_gid'])) {
164 require_once('include/group.php');
165 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
168 require_once("Photo.php");
170 $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
172 $r = q("UPDATE `contact` SET `photo` = '%s',
178 WHERE `id` = %d LIMIT 1
183 dbesc(datetime_convert()),
184 dbesc(datetime_convert()),
185 dbesc(datetime_convert()),
190 // pull feed and consume it, which should subscribe to the hub.
192 proc_run('php',"include/poller.php","$contact_id");
194 // create a follow slap
196 $tpl = get_markup_template('follow_slap.tpl');
197 $slap = replace_macros($tpl, array(
198 '$name' => $a->user['username'],
199 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
200 '$photo' => $a->contact['photo'],
201 '$thumb' => $a->contact['thumb'],
202 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
203 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
206 '$content' => t('following'),
207 '$nick' => $a->user['nickname'],
208 '$verb' => ACTIVITY_FOLLOW,
209 '$ostat_follow' => ''
212 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
213 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
218 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
219 require_once('include/salmon.php');
220 slapper($r[0],$contact['notify'],$slap);
222 if($contact['network'] == NETWORK_DIASPORA) {
223 require_once('include/diaspora.php');
224 $ret = diaspora_share($a->user,$contact);
225 logger('mod_follow: diaspora_share returns: ' . $ret);
229 $result['success'] = true;