3 require_once('Scrape.php');
5 function follow_post(&$a) {
8 notice( t('Permission denied.') . EOL);
9 goaway($_SESSION['return_url']);
13 $url = notags(trim($_POST['url']));
18 foreach($links as $link) {
19 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
20 $dfrn = $link['@attributes']['href'];
21 if($link['@attributes']['rel'] === 'salmon')
22 $notify = $link['@attributes']['href'];
23 if($link['@attributes']['rel'] === NAMESPACE_FEED)
24 $poll = $link['@attributes']['href'];
25 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
26 $hcard = $link['@attributes']['href'];
27 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
28 $profile = $link['@attributes']['href'];
34 // If we find a DFRN site, send our subscriber to the other person's
35 // dfrn_request page and all the other details will get sorted.
38 $ret = scrape_dfrn($dfrn);
39 if(is_array($ret) && x($ret,'dfrn-request')) {
41 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
43 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
45 goaway($ret['dfrn-request'] . "&addr=$myaddr");
54 $vcard = scrape_vcard($hcard);
56 // Google doesn't use absolute url in profile photos
58 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
59 $h = parse_url($hcard);
61 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
71 $vcard['fn'] = $vcard['nick'];
73 if((! isset($vcard)) && (! $poll)) {
75 $ret = scrape_feed($url);
77 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
78 $poll = ((x($ret,'feed_atom')) ? $ret['feed_atom'] : $ret['feed_rss']);
80 require_once('simplepie/simplepie.inc');
81 $feed = new SimplePie();
82 $xml = fetch_url($poll);
84 $feed->set_raw_data($xml);
88 $vcard['photo'] = $feed->get_image_url();
89 $author = $feed->get_author();
91 $vcard['fn'] = trim($author->get_name());
92 $vcard['nick'] = strtolower($vcard['fn']);
93 if(strpos($vcard['nick'],' '))
94 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
95 $email = $author->get_email();
98 $item = $feed->get_item(0);
100 $author = $item->get_author();
102 $vcard['fn'] = trim($author->get_name());
103 $vcard['nick'] = strtolower($vcard['fn']);
104 if(strpos($vcard['nick'],' '))
105 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
106 $email = $author->get_email();
110 if((! $vcard['photo']) && strlen($email))
111 $vcard['photo'] = gravatar_img($email);
116 logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
118 // do we have enough information?
120 if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
121 notice( t('The profile address specified does not provide adequate information.') . EOL);
122 goaway($_SESSION['return_url']);
126 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
129 if(! x($vcard,'photo'))
130 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
132 // check if we already have a contact
133 // the poll url is more reliable than the profile url, as we may have
134 // indirect links or webfinger links
136 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
137 intval(local_user()),
143 if($r[0]['rel'] == REL_VIP) {
144 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
152 // create contact record
153 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`,
154 `blocked`, `readonly`, `pending` )
155 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
156 intval(local_user()),
157 dbesc(datetime_convert()),
162 dbesc($vcard['nick']),
163 dbesc($vcard['photo']),
169 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
175 notice( t('Unable to retrieve contact information.') . EOL);
176 goaway($_SESSION['return_url']);
181 $contact_id = $r[0]['id'];
183 require_once("Photo.php");
185 $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
187 $r = q("UPDATE `contact` SET `photo` = '%s',
193 WHERE `id` = %d LIMIT 1
198 dbesc(datetime_convert()),
199 dbesc(datetime_convert()),
200 dbesc(datetime_convert()),
205 // pull feed and consume it, which should subscribe to the hub.
207 $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
208 proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
211 // create a follow slap
213 $tpl = load_view_file('view/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((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
236 require_once('include/salmon.php');
237 slapper($r[0],$contact['notify'],$slap);
240 goaway($_SESSION['return_url']);