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",
114 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
115 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
116 intval(CONTACT_IS_FRIEND),
124 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
125 if($ret['network'] === NETWORK_DIASPORA)
126 $new_relation = CONTACT_IS_FOLLOWER;
128 // create contact record
129 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
130 `writable`, `hidden`, `blocked`, `readonly`, `pending` )
131 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
133 dbesc(datetime_convert()),
135 dbesc(normalise_link($ret['url'])),
137 dbesc($ret['alias']),
138 dbesc($ret['batch']),
139 dbesc($ret['notify']),
144 dbesc($ret['photo']),
145 dbesc($ret['network']),
146 dbesc($ret['pubkey']),
147 intval($new_relation),
148 intval($ret['priority']),
154 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
160 notice( t('Unable to retrieve contact information.') . EOL);
166 $contact_id = $r[0]['id'];
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 if(strstr($return_url,'contacts'))
230 goaway($a->get_baseurl() . '/contacts/' . $contact_id);