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;
21 foreach($links as $link) {
22 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
23 $dfrn = unamp($link['@attributes']['href']);
24 if($link['@attributes']['rel'] === 'salmon')
25 $notify = unamp($link['@attributes']['href']);
26 if($link['@attributes']['rel'] === NAMESPACE_FEED)
27 $poll = unamp($link['@attributes']['href']);
28 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
29 $hcard = unamp($link['@attributes']['href']);
30 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
31 $profile = unamp($link['@attributes']['href']);
32 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location')
38 // Status.Net can have more than one profile URL. We need to match the profile URL
39 // to a contact on incoming messages to prevent spam, and we won't know which one
40 // to match. So in case of two, one of them is stored as an alias. Only store URL's
41 // and not webfinger user@host aliases. If they've got more than two non-email style
42 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
43 // otherwise we're screwed.
45 foreach($links as $link) {
46 if($link['@attributes']['rel'] === 'alias') {
47 if(strpos($link['@attributes']['href'],'@') === false) {
49 if($link['@attributes']['href'] !== $profile)
50 $alias = unamp($link['@attributes']['href']);
53 $profile = unamp($link['@attributes']['href']);
59 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
60 $email_conversant = true;
65 // If we find a DFRN site, send our subscriber to the other person's
66 // dfrn_request page and all the other details will get sorted.
69 $ret = scrape_dfrn($dfrn);
70 if(is_array($ret) && x($ret,'dfrn-request')) {
72 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
74 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
76 goaway($ret['dfrn-request'] . "&addr=$myaddr");
86 $vcard = scrape_vcard($hcard);
88 // Google doesn't use absolute url in profile photos
90 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
91 $h = @parse_url($hcard);
93 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
106 $vcard['fn'] = $vcard['nick'];
108 if((! isset($vcard)) && (! $poll)) {
110 $ret = scrape_feed($url);
111 logger('mod_follow: scrape_feed returns: ' . print_r($ret,true), LOGGER_DATA);
112 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
113 $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
116 $vcard['photo'] = $ret['photo'];
117 require_once('simplepie/simplepie.inc');
118 $feed = new SimplePie();
119 $xml = fetch_url($poll);
121 $feed->set_raw_data($xml);
125 if(! x($vcard,'photo'))
126 $vcard['photo'] = $feed->get_image_url();
127 $author = $feed->get_author();
129 $vcard['fn'] = unxmlify(trim($author->get_name()));
131 $vcard['fn'] = trim(unxmlify($author->get_email()));
132 if(strpos($vcard['fn'],'@') !== false)
133 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
134 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
135 if(strpos($vcard['nick'],' '))
136 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
137 $email = unxmlify($author->get_email());
140 $item = $feed->get_item(0);
142 $author = $item->get_author();
144 $vcard['fn'] = trim(unxmlify($author->get_name()));
146 $vcard['fn'] = trim(unxmlify($author->get_email()));
147 if(strpos($vcard['fn'],'@') !== false)
148 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
149 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
150 if(strpos($vcard['nick'],' '))
151 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
152 $email = unxmlify($author->get_email());
154 if(! $vcard['photo']) {
155 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
156 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
157 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
161 if((! $vcard['photo']) && strlen($email))
162 $vcard['photo'] = gravatar_img($email);
163 if($poll === $profile)
164 $lnk = $feed->get_permalink();
165 if(isset($lnk) && strlen($lnk))
167 if(! (x($vcard,'fn')))
168 $vcard['fn'] = notags($feed->get_title());
169 if(! (x($vcard,'fn')))
170 $vcard['fn'] = notags($feed->get_description());
176 logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
178 $vcard['fn'] = notags($vcard['fn']);
179 $vcard['nick'] = notags($vcard['nick']);
181 // do we have enough information?
183 if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
184 notice( t('The profile address specified does not provide adequate information.') . EOL);
185 goaway($_SESSION['return_url']);
190 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
193 if(! x($vcard,'photo'))
194 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
196 // check if we already have a contact
197 // the poll url is more reliable than the profile url, as we may have
198 // indirect links or webfinger links
200 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
201 intval(local_user()),
207 if($r[0]['rel'] == REL_VIP) {
208 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
216 // create contact record
217 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
218 `blocked`, `readonly`, `pending` )
219 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
220 intval(local_user()),
221 dbesc(datetime_convert()),
227 dbesc($vcard['nick']),
228 dbesc($vcard['photo']),
235 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
241 notice( t('Unable to retrieve contact information.') . EOL);
242 goaway($_SESSION['return_url']);
247 $contact_id = $r[0]['id'];
249 require_once("Photo.php");
251 $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
253 $r = q("UPDATE `contact` SET `photo` = '%s',
259 WHERE `id` = %d LIMIT 1
264 dbesc(datetime_convert()),
265 dbesc(datetime_convert()),
266 dbesc(datetime_convert()),
271 // pull feed and consume it, which should subscribe to the hub.
273 proc_run('php',"include/poller.php","$contact_id");
275 // create a follow slap
277 $tpl = load_view_file('view/follow_slap.tpl');
278 $slap = replace_macros($tpl, array(
279 '$name' => $a->user['username'],
280 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
281 '$photo' => $a->contact['photo'],
282 '$thumb' => $a->contact['thumb'],
283 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
284 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
287 '$content' => t('following'),
288 '$nick' => $a->user['nickname'],
289 '$verb' => ACTIVITY_FOLLOW,
290 '$ostat_follow' => ''
293 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
294 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
299 if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
300 require_once('include/salmon.php');
301 slapper($r[0],$contact['notify'],$slap);
304 goaway($_SESSION['return_url']);