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");
55 $vcard = scrape_vcard($hcard);
57 // Google doesn't use absolute url in profile photos
59 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
60 $h = parse_url($hcard);
62 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
72 $vcard['fn'] = $vcard['nick'];
74 if((! isset($vcard)) && (! $poll)) {
76 $ret = scrape_feed($url);
78 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
79 $poll = ((x($ret,'feed_atom')) ? $ret['feed_atom'] : $ret['feed_rss']);
81 require_once('simplepie/simplepie.inc');
82 $feed = new SimplePie();
83 $xml = fetch_url($poll);
85 $feed->set_raw_data($xml);
89 $vcard['photo'] = $feed->get_image_url();
90 $author = $feed->get_author();
92 $vcard['fn'] = trim($author->get_name());
93 $vcard['nick'] = strtolower($vcard['fn']);
94 if(strpos($vcard['nick'],' '))
95 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
96 $email = $author->get_email();
99 $item = $feed->get_item(0);
101 $author = $item->get_author();
103 $vcard['fn'] = trim($author->get_name());
104 $vcard['nick'] = strtolower($vcard['fn']);
105 if(strpos($vcard['nick'],' '))
106 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
107 $email = $author->get_email();
111 if((! $vcard['photo']) && strlen($email))
112 $vcard['photo'] = gravatar_img($email);
118 logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
120 // do we have enough information?
122 if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
123 notice( t('The profile address specified does not provide adequate information.') . EOL);
124 goaway($_SESSION['return_url']);
128 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
131 if(! x($vcard,'photo'))
132 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
134 // check if we already have a contact
135 // the poll url is more reliable than the profile url, as we may have
136 // indirect links or webfinger links
138 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
139 intval(local_user()),
145 if($r[0]['rel'] == REL_VIP) {
146 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
154 // create contact record
155 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
156 `blocked`, `readonly`, `pending` )
157 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
158 intval(local_user()),
159 dbesc(datetime_convert()),
164 dbesc($vcard['nick']),
165 dbesc($vcard['photo']),
172 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
178 notice( t('Unable to retrieve contact information.') . EOL);
179 goaway($_SESSION['return_url']);
184 $contact_id = $r[0]['id'];
186 require_once("Photo.php");
188 $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
190 $r = q("UPDATE `contact` SET `photo` = '%s',
196 WHERE `id` = %d LIMIT 1
201 dbesc(datetime_convert()),
202 dbesc(datetime_convert()),
203 dbesc(datetime_convert()),
208 // pull feed and consume it, which should subscribe to the hub.
210 $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
211 proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
214 // create a follow slap
216 $tpl = load_view_file('view/follow_slap.tpl');
217 $slap = replace_macros($tpl, array(
218 '$name' => $a->user['username'],
219 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
220 '$photo' => $a->contact['photo'],
221 '$thumb' => $a->contact['thumb'],
222 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
223 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
226 '$content' => t('following'),
227 '$nick' => $a->user['nickname'],
228 '$verb' => ACTIVITY_FOLLOW,
229 '$ostat_follow' => ''
232 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
233 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
238 if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
239 require_once('include/salmon.php');
240 slapper($r[0],$contact['notify'],$slap);
243 goaway($_SESSION['return_url']);