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']));
115 require_once('simplepie/simplepie.inc');
116 $feed = new SimplePie();
117 $xml = fetch_url($poll);
119 $feed->set_raw_data($xml);
123 $vcard['photo'] = $feed->get_image_url();
124 $author = $feed->get_author();
126 $vcard['fn'] = unxmlify(trim($author->get_name()));
128 $vcard['fn'] = trim(unxmlify($author->get_email()));
129 if(strpos($vcard['fn'],'@') !== false)
130 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
131 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
132 if(strpos($vcard['nick'],' '))
133 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
134 $email = unxmlify($author->get_email());
137 $item = $feed->get_item(0);
139 $author = $item->get_author();
141 $vcard['fn'] = trim(unxmlify($author->get_name()));
143 $vcard['fn'] = trim(unxmlify($author->get_email()));
144 if(strpos($vcard['fn'],'@') !== false)
145 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
146 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
147 if(strpos($vcard['nick'],' '))
148 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
149 $email = unxmlify($author->get_email());
151 if(! $vcard['photo']) {
152 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
153 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
154 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
158 if((! $vcard['photo']) && strlen($email))
159 $vcard['photo'] = gravatar_img($email);
160 if($poll === $profile)
161 $lnk = $feed->get_permalink();
162 if(isset($lnk) && strlen($lnk))
164 if(! (x($vcard,'fn')))
165 $vcard['fn'] = notags($feed->get_title());
166 if(! (x($vcard,'fn')))
167 $vcard['fn'] = notags($feed->get_description());
173 logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
175 $vcard['fn'] = notags($vcard['fn']);
176 $vcard['nick'] = notags($vcard['nick']);
178 // do we have enough information?
180 if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
181 notice( t('The profile address specified does not provide adequate information.') . EOL);
182 goaway($_SESSION['return_url']);
187 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
190 if(! x($vcard,'photo'))
191 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
193 // check if we already have a contact
194 // the poll url is more reliable than the profile url, as we may have
195 // indirect links or webfinger links
197 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
198 intval(local_user()),
204 if($r[0]['rel'] == REL_VIP) {
205 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
213 // create contact record
214 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
215 `blocked`, `readonly`, `pending` )
216 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
217 intval(local_user()),
218 dbesc(datetime_convert()),
224 dbesc($vcard['nick']),
225 dbesc($vcard['photo']),
232 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
238 notice( t('Unable to retrieve contact information.') . EOL);
239 goaway($_SESSION['return_url']);
244 $contact_id = $r[0]['id'];
246 require_once("Photo.php");
248 $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
250 $r = q("UPDATE `contact` SET `photo` = '%s',
256 WHERE `id` = %d LIMIT 1
261 dbesc(datetime_convert()),
262 dbesc(datetime_convert()),
263 dbesc(datetime_convert()),
268 // pull feed and consume it, which should subscribe to the hub.
270 proc_run('php',"include/poller.php","$contact_id");
272 // create a follow slap
274 $tpl = load_view_file('view/follow_slap.tpl');
275 $slap = replace_macros($tpl, array(
276 '$name' => $a->user['username'],
277 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
278 '$photo' => $a->contact['photo'],
279 '$thumb' => $a->contact['thumb'],
280 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
281 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
284 '$content' => t('following'),
285 '$nick' => $a->user['nickname'],
286 '$verb' => ACTIVITY_FOLLOW,
287 '$ostat_follow' => ''
290 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
291 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
296 if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
297 require_once('include/salmon.php');
298 slapper($r[0],$contact['notify'],$slap);
301 goaway($_SESSION['return_url']);