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 = $link['@attributes']['href'];
23 if($link['@attributes']['rel'] === 'salmon')
24 $notify = $link['@attributes']['href'];
25 if($link['@attributes']['rel'] === NAMESPACE_FEED)
26 $poll = $link['@attributes']['href'];
27 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
28 $hcard = $link['@attributes']['href'];
29 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
30 $profile = $link['@attributes']['href'];
35 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
36 $email_conversant = true;
41 // If we find a DFRN site, send our subscriber to the other person's
42 // dfrn_request page and all the other details will get sorted.
45 $ret = scrape_dfrn($dfrn);
46 if(is_array($ret) && x($ret,'dfrn-request')) {
48 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
50 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
52 goaway($ret['dfrn-request'] . "&addr=$myaddr");
62 $vcard = scrape_vcard($hcard);
64 // Google doesn't use absolute url in profile photos
66 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
67 $h = parse_url($hcard);
69 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
79 $vcard['fn'] = $vcard['nick'];
81 if((! isset($vcard)) && (! $poll)) {
83 $ret = scrape_feed($url);
85 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
86 $poll = ((x($ret,'feed_atom')) ? $ret['feed_atom'] : $ret['feed_rss']);
88 require_once('simplepie/simplepie.inc');
89 $feed = new SimplePie();
90 $xml = fetch_url($poll);
92 $feed->set_raw_data($xml);
96 $vcard['photo'] = $feed->get_image_url();
97 $author = $feed->get_author();
99 $vcard['fn'] = trim($author->get_name());
100 $vcard['nick'] = strtolower($vcard['fn']);
101 if(strpos($vcard['nick'],' '))
102 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
103 $email = $author->get_email();
106 $item = $feed->get_item(0);
108 $author = $item->get_author();
110 $vcard['fn'] = trim($author->get_name());
111 $vcard['nick'] = strtolower($vcard['fn']);
112 if(strpos($vcard['nick'],' '))
113 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
114 $email = $author->get_email();
118 if((! $vcard['photo']) && strlen($email))
119 $vcard['photo'] = gravatar_img($email);
125 logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
127 // do we have enough information?
129 if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
130 notice( t('The profile address specified does not provide adequate information.') . EOL);
131 goaway($_SESSION['return_url']);
135 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
138 if(! x($vcard,'photo'))
139 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
141 // check if we already have a contact
142 // the poll url is more reliable than the profile url, as we may have
143 // indirect links or webfinger links
145 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
146 intval(local_user()),
152 if($r[0]['rel'] == REL_VIP) {
153 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
161 // create contact record
162 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
163 `blocked`, `readonly`, `pending` )
164 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
165 intval(local_user()),
166 dbesc(datetime_convert()),
171 dbesc($vcard['nick']),
172 dbesc($vcard['photo']),
179 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
185 notice( t('Unable to retrieve contact information.') . EOL);
186 goaway($_SESSION['return_url']);
191 $contact_id = $r[0]['id'];
193 require_once("Photo.php");
195 $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
197 $r = q("UPDATE `contact` SET `photo` = '%s',
203 WHERE `id` = %d LIMIT 1
208 dbesc(datetime_convert()),
209 dbesc(datetime_convert()),
210 dbesc(datetime_convert()),
215 // pull feed and consume it, which should subscribe to the hub.
217 $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
218 //proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
219 proc_run($php_path,"include/poller.php","$contact_id");
221 // create a follow slap
223 $tpl = load_view_file('view/follow_slap.tpl');
224 $slap = replace_macros($tpl, array(
225 '$name' => $a->user['username'],
226 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
227 '$photo' => $a->contact['photo'],
228 '$thumb' => $a->contact['thumb'],
229 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
230 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
233 '$content' => t('following'),
234 '$nick' => $a->user['nickname'],
235 '$verb' => ACTIVITY_FOLLOW,
236 '$ostat_follow' => ''
239 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
240 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
245 if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
246 require_once('include/salmon.php');
247 slapper($r[0],$contact['notify'],$slap);
250 goaway($_SESSION['return_url']);