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;
70 // This extra param just confuses things, remove it
71 if($ret['network'] === NETWORK_DIASPORA)
72 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
75 // do we have enough information?
77 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
78 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
80 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
82 $result['message'] .= t('An author or name was not found.') . EOL;
84 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
85 if(strpos($url,'@') !== false) {
86 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
87 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
92 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
93 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
102 if(! $ret['notify']) {
103 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
106 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
108 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
110 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
112 if($ret['network'] === NETWORK_MAIL) {
116 if($ret['network'] === NETWORK_DIASPORA)
119 // check if we already have a contact
120 // the poll url is more reliable than the profile url, as we may have
121 // indirect links or webfinger links
123 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
131 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
132 q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
133 intval(CONTACT_IS_FRIEND),
143 // check service class limits
145 $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0",
149 $total_contacts = $r[0]['total'];
151 if(! service_class_allows($uid,'total_contacts',$total_contacts)) {
152 $result['message'] .= upgrade_message();
156 $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0",
161 $total_network = $r[0]['total'];
163 if(! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
164 $result['message'] .= upgrade_message();
168 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
169 if($ret['network'] === NETWORK_DIASPORA)
170 $new_relation = CONTACT_IS_FOLLOWER;
172 // create contact record
173 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
174 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
175 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
177 dbesc(datetime_convert()),
179 dbesc(normalise_link($ret['url'])),
181 dbesc($ret['alias']),
182 dbesc($ret['batch']),
183 dbesc($ret['notify']),
188 dbesc($ret['photo']),
189 dbesc($ret['network']),
190 dbesc($ret['pubkey']),
191 intval($new_relation),
192 intval($ret['priority']),
199 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
205 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
210 $contact_id = $r[0]['id'];
213 $g = q("select def_gid from user where uid = %d limit 1",
216 if($g && intval($g[0]['def_gid'])) {
217 require_once('include/group.php');
218 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
221 require_once("Photo.php");
223 $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
225 $r = q("UPDATE `contact` SET `photo` = '%s',
231 WHERE `id` = %d LIMIT 1
236 dbesc(datetime_convert()),
237 dbesc(datetime_convert()),
238 dbesc(datetime_convert()),
243 // pull feed and consume it, which should subscribe to the hub.
245 proc_run('php',"include/poller.php","$contact_id");
247 // create a follow slap
249 $tpl = get_markup_template('follow_slap.tpl');
250 $slap = replace_macros($tpl, array(
251 '$name' => $a->user['username'],
252 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
253 '$photo' => $a->contact['photo'],
254 '$thumb' => $a->contact['thumb'],
255 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
256 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
259 '$content' => t('following'),
260 '$nick' => $a->user['nickname'],
261 '$verb' => ACTIVITY_FOLLOW,
262 '$ostat_follow' => ''
265 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
266 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
271 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
272 require_once('include/salmon.php');
273 slapper($r[0],$contact['notify'],$slap);
275 if($contact['network'] == NETWORK_DIASPORA) {
276 require_once('include/diaspora.php');
277 $ret = diaspora_share($a->user,$contact);
278 logger('mod_follow: diaspora_share returns: ' . $ret);
282 $result['success'] = true;