5 // Takes a $uid and a url/handle and adds a new contact
6 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
9 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
11 // $return['success'] boolean true if successful
12 // $return['message'] error text if success is false.
16 function new_contact($uid,$url,$interactive = false) {
18 $result = array('success' => false,'message' => '');
22 // remove ajax junk, e.g. Twitter
24 $url = str_replace('/#!/','/',$url);
26 if(! allowed_url($url)) {
27 $result['message'] = t('Disallowed profile URL.');
32 $result['message'] = t('Connect URL missing.');
36 $arr = array('url' => $url, 'contact' => array());
38 call_hooks('follow', $arr);
40 if(x($arr['contact'],'name'))
41 $ret = $arr['contact'];
43 $ret = probe_url($url);
45 if($ret['network'] === NETWORK_DFRN) {
48 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
50 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
52 goaway($ret['request'] . "&addr=$myaddr");
58 if(get_config('system','dfrn_only')) {
59 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
60 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
65 // This extra param just confuses things, remove it
66 if($ret['network'] === NETWORK_DIASPORA)
67 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
70 // do we have enough information?
72 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
73 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
75 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
77 $result['message'] .= t('An author or name was not found.') . EOL;
79 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
80 if(strpos($url,'@') !== false) {
81 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
82 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
87 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
88 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
92 if(! $ret['notify']) {
93 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
96 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
98 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
100 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
102 if($ret['network'] === NETWORK_MAIL) {
106 if($ret['network'] === NETWORK_DIASPORA)
109 // check if we already have a contact
110 // the poll url is more reliable than the profile url, as we may have
111 // indirect links or webfinger links
113 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
121 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
122 q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
123 intval(CONTACT_IS_FRIEND),
132 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
133 if($ret['network'] === NETWORK_DIASPORA)
134 $new_relation = CONTACT_IS_FOLLOWER;
136 // create contact record
137 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
138 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
139 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
141 dbesc(datetime_convert()),
143 dbesc(normalise_link($ret['url'])),
145 dbesc($ret['alias']),
146 dbesc($ret['batch']),
147 dbesc($ret['notify']),
152 dbesc($ret['photo']),
153 dbesc($ret['network']),
154 dbesc($ret['pubkey']),
155 intval($new_relation),
156 intval($ret['priority']),
163 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
169 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
174 $contact_id = $r[0]['id'];
177 $g = q("select def_gid from user where uid = %d limit 1",
180 if($g && intval($g[0]['def_gid'])) {
181 require_once('include/group.php');
182 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
185 require_once("Photo.php");
187 $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
189 $r = q("UPDATE `contact` SET `photo` = '%s',
195 WHERE `id` = %d LIMIT 1
200 dbesc(datetime_convert()),
201 dbesc(datetime_convert()),
202 dbesc(datetime_convert()),
207 // pull feed and consume it, which should subscribe to the hub.
209 proc_run('php',"include/poller.php","$contact_id");
211 // create a follow slap
213 $tpl = get_markup_template('follow_slap.tpl');
214 $slap = replace_macros($tpl, array(
215 '$name' => $a->user['username'],
216 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
217 '$photo' => $a->contact['photo'],
218 '$thumb' => $a->contact['thumb'],
219 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
220 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
223 '$content' => t('following'),
224 '$nick' => $a->user['nickname'],
225 '$verb' => ACTIVITY_FOLLOW,
226 '$ostat_follow' => ''
229 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
230 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
235 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
236 require_once('include/salmon.php');
237 slapper($r[0],$contact['notify'],$slap);
239 if($contact['network'] == NETWORK_DIASPORA) {
240 require_once('include/diaspora.php');
241 $ret = diaspora_share($a->user,$contact);
242 logger('mod_follow: diaspora_share returns: ' . $ret);
246 $result['success'] = true;