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']) {
78 $poll = $ret['feed_atom'];
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));
119 // do we have enough information?
121 if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
122 notice( t('The profile address specified does not provide adequate information.') . EOL);
123 goaway($_SESSION['return_url']);
127 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
130 if(! x($vcard,'photo'))
131 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
133 // check if we already have a contact
134 // the poll url is more reliable than the profile url, as we may have
135 // indirect links or webfinger links
137 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
138 intval(local_user()),
144 if($r[0]['rel'] == REL_VIP) {
145 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
153 // create contact record
154 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`,
155 `blocked`, `readonly`, `pending` )
156 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
157 intval(local_user()),
158 dbesc(datetime_convert()),
163 dbesc($vcard['nick']),
164 dbesc($vcard['photo']),
170 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
176 notice( t('Unable to retrieve contact information.') . EOL);
177 goaway($_SESSION['return_url']);
182 $contact_id = $r[0]['id'];
184 require_once("Photo.php");
186 $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
188 $r = q("UPDATE `contact` SET `photo` = '%s',
194 WHERE `id` = %d LIMIT 1
199 dbesc(datetime_convert()),
200 dbesc(datetime_convert()),
201 dbesc(datetime_convert()),
206 // pull feed and consume it, which should subscribe to the hub.
208 $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
209 proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
212 // create a follow slap
214 $tpl = load_view_file('view/follow_slap.tpl');
215 $slap = replace_macros($tpl, array(
216 '$name' => $a->user['username'],
217 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
218 '$photo' => $a->contact['photo'],
219 '$thumb' => $a->contact['thumb'],
220 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
221 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
224 '$content' => t('following'),
225 '$nick' => $a->user['nickname'],
226 '$verb' => ACTIVITY_FOLLOW,
227 '$ostat_follow' => ''
230 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
231 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
236 if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
237 require_once('include/salmon.php');
238 slapper($r[0],$contact['notify'],$slap);
241 goaway($_SESSION['return_url']);