3 require_once('Scrape.php');
5 function follow_init(&$a) {
8 notice( t('Permission denied.') . EOL);
9 goaway($_SESSION['return_url']);
13 $url = $orig_url = notags(trim($_REQUEST['url']));
15 // remove ajax junk, e.g. Twitter
17 $url = str_replace('/#!/','/',$url);
19 if(! allowed_url($url)) {
20 notice( t('Disallowed profile URL.') . EOL);
21 goaway($_SESSION['return_url']);
27 notice( t('Connect URL missing.') . EOL);
28 goaway($_SESSION['return_url']);
33 $ret = probe_url($url);
35 if($ret['network'] === NETWORK_DFRN) {
37 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
39 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
41 goaway($ret['request'] . "&addr=$myaddr");
46 if(get_config('system','dfrn_only')) {
47 notice( t('This site is not configured to allow communications with other networks.') . EOL);
48 notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
49 goaway($_SESSION['return_url']);
53 // This 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 notice( t('The profile address specified does not provide adequate information.') . EOL);
63 notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
65 notice( t('An author or name was not found.') . EOL);
67 notice( t('No browser URL could be matched to this address.') . EOL);
68 if(strpos($url,'@') !== false)
69 notice('Unable to match @-style Identity Address with a known protocol or email contact');
70 goaway($_SESSION['return_url']);
73 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
74 notice( t('The profile address specified belongs to a network which has been disabled on this site.') . EOL);
78 if(! $ret['notify']) {
79 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
82 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
83 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
85 if($ret['network'] === NETWORK_MAIL) {
89 if($ret['network'] === NETWORK_DIASPORA)
92 // check if we already have a contact
93 // the poll url is more reliable than the profile url, as we may have
94 // indirect links or webfinger links
96 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
103 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
104 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
105 intval(CONTACT_IS_FRIEND),
113 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
114 if($ret['network'] === NETWORK_DIASPORA)
115 $new_relation = CONTACT_IS_FOLLOWER;
117 // create contact record
118 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
119 `writable`, `hidden`, `blocked`, `readonly`, `pending` )
120 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
121 intval(local_user()),
122 dbesc(datetime_convert()),
124 dbesc(normalise_link($ret['url'])),
126 dbesc($ret['alias']),
127 dbesc($ret['batch']),
128 dbesc($ret['notify']),
133 dbesc($ret['photo']),
134 dbesc($ret['network']),
135 dbesc($ret['pubkey']),
136 intval($new_relation),
137 intval($ret['priority']),
143 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
149 notice( t('Unable to retrieve contact information.') . EOL);
150 goaway($_SESSION['return_url']);
155 $contact_id = $r[0]['id'];
157 require_once("Photo.php");
159 $photos = import_profile_photo($ret['photo'],local_user(),$contact_id);
161 $r = q("UPDATE `contact` SET `photo` = '%s',
167 WHERE `id` = %d LIMIT 1
172 dbesc(datetime_convert()),
173 dbesc(datetime_convert()),
174 dbesc(datetime_convert()),
179 // pull feed and consume it, which should subscribe to the hub.
181 proc_run('php',"include/poller.php","$contact_id");
183 // create a follow slap
185 $tpl = get_markup_template('follow_slap.tpl');
186 $slap = replace_macros($tpl, array(
187 '$name' => $a->user['username'],
188 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
189 '$photo' => $a->contact['photo'],
190 '$thumb' => $a->contact['thumb'],
191 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
192 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
195 '$content' => t('following'),
196 '$nick' => $a->user['nickname'],
197 '$verb' => ACTIVITY_FOLLOW,
198 '$ostat_follow' => ''
201 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
202 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
207 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
208 require_once('include/salmon.php');
209 slapper($r[0],$contact['notify'],$slap);
211 if($contact['network'] == NETWORK_DIASPORA) {
212 require_once('include/diaspora.php');
213 $ret = diaspora_share($a->user,$contact);
214 logger('mod_follow: diaspora_share returns: ' . $ret);
218 if(strstr($_SESSION['return_url'],'contacts'))
219 goaway($a->get_baseurl() . '/contacts/' . $contact_id);
221 goaway($_SESSION['return_url']);