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'];
96 logger('mod_follow: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
108 $vcard['fn'] = $vcard['nick'];
110 if((! isset($vcard)) && (! $poll)) {
112 $ret = scrape_feed($url);
113 logger('mod_follow: scrape_feed returns: ' . print_r($ret,true), LOGGER_DATA);
114 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
115 $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
118 $vcard['photo'] = $ret['photo'];
119 require_once('simplepie/simplepie.inc');
120 $feed = new SimplePie();
121 $xml = fetch_url($poll);
123 $feed->set_raw_data($xml);
127 if(! x($vcard,'photo'))
128 $vcard['photo'] = $feed->get_image_url();
129 $author = $feed->get_author();
131 $vcard['fn'] = unxmlify(trim($author->get_name()));
133 $vcard['fn'] = trim(unxmlify($author->get_email()));
134 if(strpos($vcard['fn'],'@') !== false)
135 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
136 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
137 if(strpos($vcard['nick'],' '))
138 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
139 $email = unxmlify($author->get_email());
142 $item = $feed->get_item(0);
144 $author = $item->get_author();
146 $vcard['fn'] = trim(unxmlify($author->get_name()));
148 $vcard['fn'] = trim(unxmlify($author->get_email()));
149 if(strpos($vcard['fn'],'@') !== false)
150 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
151 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
152 if(strpos($vcard['nick'],' '))
153 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
154 $email = unxmlify($author->get_email());
156 if(! $vcard['photo']) {
157 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
158 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
159 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
163 if((! $vcard['photo']) && strlen($email))
164 $vcard['photo'] = gravatar_img($email);
165 if($poll === $profile)
166 $lnk = $feed->get_permalink();
167 if(isset($lnk) && strlen($lnk))
169 if(! (x($vcard,'fn')))
170 $vcard['fn'] = notags($feed->get_title());
171 if(! (x($vcard,'fn')))
172 $vcard['fn'] = notags($feed->get_description());
178 logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
180 $vcard['fn'] = notags($vcard['fn']);
181 $vcard['nick'] = notags($vcard['nick']);
183 // do we have enough information?
185 if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
186 notice( t('The profile address specified does not provide adequate information.') . EOL);
187 goaway($_SESSION['return_url']);
192 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
195 if(! x($vcard,'photo'))
196 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
198 // check if we already have a contact
199 // the poll url is more reliable than the profile url, as we may have
200 // indirect links or webfinger links
202 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
203 intval(local_user()),
209 if($r[0]['rel'] == REL_VIP) {
210 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
218 // create contact record
219 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
220 `blocked`, `readonly`, `pending` )
221 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
222 intval(local_user()),
223 dbesc(datetime_convert()),
229 dbesc($vcard['nick']),
230 dbesc($vcard['photo']),
237 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
243 notice( t('Unable to retrieve contact information.') . EOL);
244 goaway($_SESSION['return_url']);
249 $contact_id = $r[0]['id'];
251 require_once("Photo.php");
253 $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
255 $r = q("UPDATE `contact` SET `photo` = '%s',
261 WHERE `id` = %d LIMIT 1
266 dbesc(datetime_convert()),
267 dbesc(datetime_convert()),
268 dbesc(datetime_convert()),
273 // pull feed and consume it, which should subscribe to the hub.
275 proc_run('php',"include/poller.php","$contact_id");
277 // create a follow slap
279 $tpl = load_view_file('view/follow_slap.tpl');
280 $slap = replace_macros($tpl, array(
281 '$name' => $a->user['username'],
282 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
283 '$photo' => $a->contact['photo'],
284 '$thumb' => $a->contact['thumb'],
285 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
286 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
289 '$content' => t('following'),
290 '$nick' => $a->user['nickname'],
291 '$verb' => ACTIVITY_FOLLOW,
292 '$ostat_follow' => ''
295 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
296 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
301 if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
302 require_once('include/salmon.php');
303 slapper($r[0],$contact['notify'],$slap);
306 goaway($_SESSION['return_url']);