]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
06e81ceedd66b7aed2f7ac62d00fca875ff71842
[friendica.git] / mod / follow.php
1 <?php
2
3 require_once('Scrape.php');
4
5 function follow_post(&$a) {
6
7         if(! local_user()) {
8                 notice( t('Permission denied.') . EOL);
9                 goaway($_SESSION['return_url']);
10                 // NOTREACHED
11         }
12
13         $url = $orig_url = notags(trim($_POST['url']));
14         $diaspora = false;      
15         $email_conversant = false;
16
17         if($url) {
18                 $links = lrdd($url);
19
20                 if(count($links)) {
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')
33                                         $diaspora = true;
34
35
36                         }
37
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.
44
45                         foreach($links as $link) {
46                                 if($link['@attributes']['rel'] === 'alias') {
47                                         if(strpos($link['@attributes']['href'],'@') === false) {
48                                                 if(isset($profile)) {
49                                                         if($link['@attributes']['href'] !== $profile)
50                                                                 $alias = unamp($link['@attributes']['href']);
51                                                 }
52                                                 else
53                                                         $profile = unamp($link['@attributes']['href']);
54                                         }
55                                 }
56                         }
57                 }
58                 else {
59                         if((strpos($orig_url,'@')) && validate_email($orig_url)) {
60                                 $email_conversant = true;
61                         }
62                 }
63         }       
64
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.
67
68         if(strlen($dfrn)) {
69                 $ret = scrape_dfrn($dfrn);
70                 if(is_array($ret) && x($ret,'dfrn-request')) {
71                         if(strlen($a->path))
72                                 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
73                         else
74                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
75  
76                         goaway($ret['dfrn-request'] . "&addr=$myaddr");
77                 
78                         // NOTREACHED
79                 }
80         }
81
82         $network  = 'stat';
83         $priority = 0;
84
85         if($hcard) {
86                 $vcard = scrape_vcard($hcard);
87
88                 // Google doesn't use absolute url in profile photos
89
90                 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
91                         $h = @parse_url($hcard);
92                         if($h)
93                                 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
94                 }
95         }
96
97         if(! $profile) {
98                 if($diaspora)
99                         $profile = $hcard;
100                 else
101                         $profile = $url;
102         }
103
104         if(! x($vcard,'fn'))
105                 if(x($vcard,'nick'))
106                         $vcard['fn'] = $vcard['nick'];
107
108         if((! isset($vcard)) && (! $poll)) {
109
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']));
114                         $vcard = array();
115                         require_once('simplepie/simplepie.inc');
116                     $feed = new SimplePie();
117                         $xml = fetch_url($poll);
118
119                 $feed->set_raw_data($xml);
120
121                     $feed->init();
122
123                         $vcard['photo'] = $feed->get_image_url();
124                         $author = $feed->get_author();
125                         if($author) {                   
126                                 $vcard['fn'] = unxmlify(trim($author->get_name()));
127                                 if(! $vcard['fn'])
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());
135                         }
136                         else {
137                                 $item = $feed->get_item(0);
138                                 if($item) {
139                                         $author = $item->get_author();
140                                         if($author) {                   
141                                                 $vcard['fn'] = trim(unxmlify($author->get_name()));
142                                                 if(! $vcard['fn'])
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());
150                                         }
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']);
155                                         }
156                                 }
157                         }
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))
163                                 $profile = $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());
168                         $network = 'feed';
169                         $priority = 2;
170                 }
171         }
172
173         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
174
175         $vcard['fn'] = notags($vcard['fn']);
176         $vcard['nick'] = notags($vcard['nick']);
177
178         // do we have enough information?
179         
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']);
183         }
184
185
186         if(! $notify) {
187                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
188         }
189
190         if(! x($vcard,'photo'))
191                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
192
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
196
197         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
198                 intval(local_user()),
199                 dbesc($poll)
200         );                      
201
202         if(count($r)) {
203                 // update contact
204                 if($r[0]['rel'] == REL_VIP) {
205                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
206                                 intval(REL_BUD),
207                                 intval($r[0]['id']),
208                                 intval(local_user())
209                         );
210                 }
211         }
212         else {
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()),
219                         dbesc($profile),
220                         dbesc($alias),
221                         dbesc($notify),
222                         dbesc($poll),
223                         dbesc($vcard['fn']),
224                         dbesc($vcard['nick']),
225                         dbesc($vcard['photo']),
226                         dbesc($network),
227                         intval(REL_FAN),
228                         intval($priority)
229                 );
230         }
231
232         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
233                 dbesc($profile),
234                 intval(local_user())
235         );
236
237         if(! count($r)) {
238                 notice( t('Unable to retrieve contact information.') . EOL);
239                 goaway($_SESSION['return_url']);
240                 // NOTREACHED
241         }
242
243         $contact = $r[0];
244         $contact_id  = $r[0]['id'];
245
246         require_once("Photo.php");
247
248         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
249
250         $r = q("UPDATE `contact` SET `photo` = '%s', 
251                         `thumb` = '%s',
252                         `micro` = '%s', 
253                         `name-date` = '%s', 
254                         `uri-date` = '%s', 
255                         `avatar-date` = '%s'
256                         WHERE `id` = %d LIMIT 1
257                 ",
258                         dbesc($photos[0]),
259                         dbesc($photos[1]),
260                         dbesc($photos[2]),
261                         dbesc(datetime_convert()),
262                         dbesc(datetime_convert()),
263                         dbesc(datetime_convert()),
264                         intval($contact_id)
265                 );                      
266
267
268         // pull feed and consume it, which should subscribe to the hub.
269
270         proc_run('php',"include/poller.php","$contact_id");
271
272         // create a follow slap
273
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(),
282                 '$title' => '',
283                 '$type' => 'text',
284                 '$content' => t('following'),
285                 '$nick' => $a->user['nickname'],
286                 '$verb' => ACTIVITY_FOLLOW,
287                 '$ostat_follow' => ''
288         ));
289
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",
292                         intval(local_user())
293         );
294
295
296         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
297                 require_once('include/salmon.php');
298                 slapper($r[0],$contact['notify'],$slap);
299         }
300
301         goaway($_SESSION['return_url']);
302         // NOTREACHED
303 }