3 require_once('Scrape.php');
5 function follow_init(&$a) {
8 notice( t('Permission denied.') . EOL);
9 goaway($_SESSION['return_url']);
14 $url = $orig_url = notags(trim($_REQUEST['url']));
15 $return_url = $_SESSION['return_url'];
18 // remove ajax junk, e.g. Twitter
20 $url = str_replace('/#!/','/',$url);
22 if(! allowed_url($url)) {
23 notice( t('Disallowed profile URL.') . EOL);
30 notice( t('Connect URL missing.') . EOL);
35 $arr = array('url' => $url, 'contact' => array());
37 call_hooks('follow', $arr);
39 if(x($arr['contact'],'name'))
40 $ret = $arr['contact'];
42 $ret = probe_url($url);
44 if($ret['network'] === NETWORK_DFRN) {
46 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
48 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
50 goaway($ret['request'] . "&addr=$myaddr");
55 if(get_config('system','dfrn_only')) {
56 notice( t('This site is not configured to allow communications with other networks.') . EOL);
57 notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
62 // This extra param just confuses things, remove it
63 if($ret['network'] === NETWORK_DIASPORA)
64 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
67 // do we have enough information?
69 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
70 notice( t('The profile address specified does not provide adequate information.') . EOL);
72 notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
74 notice( t('An author or name was not found.') . EOL);
76 notice( t('No browser URL could be matched to this address.') . EOL);
77 if(strpos($url,'@') !== false) {
78 notice( t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL);
79 notice( t('Use mailto: in front of address to force email check.') . EOL);
84 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
85 notice( t('The profile address specified belongs to a network which has been disabled on this site.') . EOL);
89 if(! $ret['notify']) {
90 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
93 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
94 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
96 if($ret['network'] === NETWORK_MAIL) {
100 if($ret['network'] === NETWORK_DIASPORA)
103 // check if we already have a contact
104 // the poll url is more reliable than the profile url, as we may have
105 // indirect links or webfinger links
107 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
115 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
116 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
117 intval(CONTACT_IS_FRIEND),
125 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
126 if($ret['network'] === NETWORK_DIASPORA)
127 $new_relation = CONTACT_IS_FOLLOWER;
129 // create contact record
130 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
131 `writable`, `hidden`, `blocked`, `readonly`, `pending` )
132 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
134 dbesc(datetime_convert()),
136 dbesc(normalise_link($ret['url'])),
138 dbesc($ret['alias']),
139 dbesc($ret['batch']),
140 dbesc($ret['notify']),
145 dbesc($ret['photo']),
146 dbesc($ret['network']),
147 dbesc($ret['pubkey']),
148 intval($new_relation),
149 intval($ret['priority']),
155 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
161 notice( t('Unable to retrieve contact information.') . EOL);
167 $contact_id = $r[0]['id'];
170 $g = q("select def_gid from user where uid = %d limit 1",
173 if($g && intval($g[0]['def_gid'])) {
174 require_once('include/group.php');
175 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
178 require_once("Photo.php");
180 $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
182 $r = q("UPDATE `contact` SET `photo` = '%s',
188 WHERE `id` = %d LIMIT 1
193 dbesc(datetime_convert()),
194 dbesc(datetime_convert()),
195 dbesc(datetime_convert()),
200 // pull feed and consume it, which should subscribe to the hub.
202 proc_run('php',"include/poller.php","$contact_id");
204 // create a follow slap
206 $tpl = get_markup_template('follow_slap.tpl');
207 $slap = replace_macros($tpl, array(
208 '$name' => $a->user['username'],
209 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
210 '$photo' => $a->contact['photo'],
211 '$thumb' => $a->contact['thumb'],
212 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
213 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
216 '$content' => t('following'),
217 '$nick' => $a->user['nickname'],
218 '$verb' => ACTIVITY_FOLLOW,
219 '$ostat_follow' => ''
222 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
223 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
228 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
229 require_once('include/salmon.php');
230 slapper($r[0],$contact['notify'],$slap);
232 if($contact['network'] == NETWORK_DIASPORA) {
233 require_once('include/diaspora.php');
234 $ret = diaspora_share($a->user,$contact);
235 logger('mod_follow: diaspora_share returns: ' . $ret);
239 if(strstr($return_url,'contacts'))
240 goaway($a->get_baseurl() . '/contacts/' . $contact_id);