3 require_once('Scrape.php');
5 function follow_post(&$a) {
8 notice( t('Permission denied.') . EOL);
9 goaway($_SESSION['return_url']);
13 $url = $orig_url = notags(trim($_POST['url']));
15 $email_conversant = false;
20 foreach($links as $link) {
21 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
22 $dfrn = unamp($link['@attributes']['href']);
23 if($link['@attributes']['rel'] === 'salmon')
24 $notify = unamp($link['@attributes']['href']);
25 if($link['@attributes']['rel'] === NAMESPACE_FEED)
26 $poll = unamp($link['@attributes']['href']);
27 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
28 $hcard = unamp($link['@attributes']['href']);
29 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
30 $profile = unamp($link['@attributes']['href']);
34 // Status.Net can have more than one profile URL. We need to match the profile URL
35 // to a contact on incoming messages to prevent spam, and we won't know which one
36 // to match. So in case of two, one of them is stored as an alias. Only store URL's
37 // and not webfinger user@host aliases. If they've got more than two non-email style
38 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
39 // otherwise we're screwed.
41 foreach($links as $link) {
42 if($link['@attributes']['rel'] === 'alias') {
43 if(strpos($link['@attributes']['href'],'@') === false) {
45 if($link['@attributes']['href'] !== $profile)
46 $alias = unamp($link['@attributes']['href']);
49 $profile = unamp($link['@attributes']['href']);
55 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
56 $email_conversant = true;
61 // If we find a DFRN site, send our subscriber to the other person's
62 // dfrn_request page and all the other details will get sorted.
65 $ret = scrape_dfrn($dfrn);
66 if(is_array($ret) && x($ret,'dfrn-request')) {
68 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
70 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
72 goaway($ret['dfrn-request'] . "&addr=$myaddr");
82 $vcard = scrape_vcard($hcard);
84 // Google doesn't use absolute url in profile photos
86 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
87 $h = parse_url($hcard);
89 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
99 $vcard['fn'] = $vcard['nick'];
101 if((! isset($vcard)) && (! $poll)) {
103 $ret = scrape_feed($url);
105 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
106 $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
108 require_once('simplepie/simplepie.inc');
109 $feed = new SimplePie();
110 $xml = fetch_url($poll);
112 $feed->set_raw_data($xml);
116 $vcard['photo'] = $feed->get_image_url();
117 $author = $feed->get_author();
119 $vcard['fn'] = unxmlify(trim($author->get_name()));
120 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
121 if(strpos($vcard['nick'],' '))
122 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
123 $email = unxmlify($author->get_email());
126 $item = $feed->get_item(0);
128 $author = $item->get_author();
130 $vcard['fn'] = trim(unxmlify($author->get_name()));
132 $vcard['fn'] = trim(unxmlify($author->get_email()));
133 if(strpos($vcard['fn'],'@') !== false)
134 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
135 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
136 if(strpos($vcard['nick'],' '))
137 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
138 $email = unxmlify($author->get_email());
140 if(! $vcard['photo']) {
141 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
142 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
143 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
147 if((! $vcard['photo']) && strlen($email))
148 $vcard['photo'] = gravatar_img($email);
155 logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
157 $vcard['fn'] = notags($vcard['fn']);
158 $vcard['nick'] = notags($vcard['nick']);
160 // do we have enough information?
162 if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
163 notice( t('The profile address specified does not provide adequate information.') . EOL);
164 goaway($_SESSION['return_url']);
169 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
172 if(! x($vcard,'photo'))
173 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
175 // check if we already have a contact
176 // the poll url is more reliable than the profile url, as we may have
177 // indirect links or webfinger links
179 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
180 intval(local_user()),
186 if($r[0]['rel'] == REL_VIP) {
187 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
195 // create contact record
196 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
197 `blocked`, `readonly`, `pending` )
198 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
199 intval(local_user()),
200 dbesc(datetime_convert()),
206 dbesc($vcard['nick']),
207 dbesc($vcard['photo']),
214 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
220 notice( t('Unable to retrieve contact information.') . EOL);
221 goaway($_SESSION['return_url']);
226 $contact_id = $r[0]['id'];
228 require_once("Photo.php");
230 $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
232 $r = q("UPDATE `contact` SET `photo` = '%s',
238 WHERE `id` = %d LIMIT 1
243 dbesc(datetime_convert()),
244 dbesc(datetime_convert()),
245 dbesc(datetime_convert()),
250 // pull feed and consume it, which should subscribe to the hub.
252 $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
253 //proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
254 proc_run($php_path,"include/poller.php","$contact_id");
256 // create a follow slap
258 $tpl = load_view_file('view/follow_slap.tpl');
259 $slap = replace_macros($tpl, array(
260 '$name' => $a->user['username'],
261 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
262 '$photo' => $a->contact['photo'],
263 '$thumb' => $a->contact['thumb'],
264 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
265 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
268 '$content' => t('following'),
269 '$nick' => $a->user['nickname'],
270 '$verb' => ACTIVITY_FOLLOW,
271 '$ostat_follow' => ''
274 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
275 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
280 if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
281 require_once('include/salmon.php');
282 slapper($r[0],$contact['notify'],$slap);
285 goaway($_SESSION['return_url']);