]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
cleanup untracked files
[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                 if(count($links)) {
20                         foreach($links as $link) {
21                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
22                                         $dfrn = unamp($link['@attributes']['href']);
23                                 if($link['@attributes']['rel'] === 'salmon')
24                                         $notify = unamp($link['@attributes']['href']);
25                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
26                                         $poll = unamp($link['@attributes']['href']);
27                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
28                                         $hcard = unamp($link['@attributes']['href']);
29                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
30                                         $profile = unamp($link['@attributes']['href']);
31                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location')
32                                         $diaspora = true;
33
34
35                         }
36
37                         // Status.Net can have more than one profile URL. We need to match the profile URL
38                         // to a contact on incoming messages to prevent spam, and we won't know which one
39                         // to match. So in case of two, one of them is stored as an alias. Only store URL's
40                         // and not webfinger user@host aliases. If they've got more than two non-email style
41                         // aliases, let's hope we're lucky and get one that matches the feed author-uri because 
42                         // otherwise we're screwed.
43
44                         foreach($links as $link) {
45                                 if($link['@attributes']['rel'] === 'alias') {
46                                         if(strpos($link['@attributes']['href'],'@') === false) {
47                                                 if(isset($profile)) {
48                                                         if($link['@attributes']['href'] !== $profile)
49                                                                 $alias = unamp($link['@attributes']['href']);
50                                                 }
51                                                 else
52                                                         $profile = unamp($link['@attributes']['href']);
53                                         }
54                                 }
55                         }
56                 }
57                 else {
58                         if((strpos($orig_url,'@')) && validate_email($orig_url)) {
59                                 $email_conversant = true;
60                         }
61                 }
62         }       
63
64         // If we find a DFRN site, send our subscriber to the other person's
65         // dfrn_request page and all the other details will get sorted.
66
67         if(strlen($dfrn)) {
68                 $ret = scrape_dfrn($dfrn);
69                 if(is_array($ret) && x($ret,'dfrn-request')) {
70                         if(strlen($a->path))
71                                 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
72                         else
73                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
74  
75                         goaway($ret['dfrn-request'] . "&addr=$myaddr");
76                 
77                         // NOTREACHED
78                 }
79         }
80
81         $network  = 'stat';
82         $priority = 0;
83
84         if($hcard) {
85                 $vcard = scrape_vcard($hcard);
86
87                 // Google doesn't use absolute url in profile photos
88
89                 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
90                         $h = @parse_url($hcard);
91                         if($h)
92                                 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
93                 }
94         }
95
96         if(! $profile) {
97                 if($diaspora)
98                         $profile = $hcard;
99                 else
100                         $profile = $url;
101         }
102
103         if(! x($vcard,'fn'))
104                 if(x($vcard,'nick'))
105                         $vcard['fn'] = $vcard['nick'];
106
107         if((! isset($vcard)) && (! $poll)) {
108
109                 $ret = scrape_feed($url);
110
111                 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
112                         $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
113                         $vcard = array();
114                         require_once('simplepie/simplepie.inc');
115                     $feed = new SimplePie();
116                         $xml = fetch_url($poll);
117
118                 $feed->set_raw_data($xml);
119
120                     $feed->init();
121
122                         $vcard['photo'] = $feed->get_image_url();
123                         $author = $feed->get_author();
124                         if($author) {                   
125                                 $vcard['fn'] = unxmlify(trim($author->get_name()));
126                                 if(! $vcard['fn'])
127                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
128                                 if(strpos($vcard['fn'],'@') !== false)
129                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
130                                 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
131                                 if(strpos($vcard['nick'],' '))
132                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
133                                 $email = unxmlify($author->get_email());
134                         }
135                         else {
136                                 $item = $feed->get_item(0);
137                                 if($item) {
138                                         $author = $item->get_author();
139                                         if($author) {                   
140                                                 $vcard['fn'] = trim(unxmlify($author->get_name()));
141                                                 if(! $vcard['fn'])
142                                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
143                                                 if(strpos($vcard['fn'],'@') !== false)
144                                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
145                                                 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
146                                                 if(strpos($vcard['nick'],' '))
147                                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
148                                                 $email = unxmlify($author->get_email());
149                                         }
150                                         if(! $vcard['photo']) {
151                                                 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
152                                                 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
153                                                         $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
154                                         }
155                                 }
156                         }
157                         if((! $vcard['photo']) && strlen($email))
158                                 $vcard['photo'] = gravatar_img($email);
159                         
160                         $network = 'feed';
161                         $priority = 2;
162                 }
163         }
164
165         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
166
167         $vcard['fn'] = notags($vcard['fn']);
168         $vcard['nick'] = notags($vcard['nick']);
169
170         // do we have enough information?
171         
172         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
173                 notice( t('The profile address specified does not provide adequate information.') . EOL);
174                 goaway($_SESSION['return_url']);
175         }
176
177
178         if(! $notify) {
179                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
180         }
181
182         if(! x($vcard,'photo'))
183                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
184
185         // check if we already have a contact
186         // the poll url is more reliable than the profile url, as we may have
187         // indirect links or webfinger links
188
189         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
190                 intval(local_user()),
191                 dbesc($poll)
192         );                      
193
194         if(count($r)) {
195                 // update contact
196                 if($r[0]['rel'] == REL_VIP) {
197                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
198                                 intval(REL_BUD),
199                                 intval($r[0]['id']),
200                                 intval(local_user())
201                         );
202                 }
203         }
204         else {
205                 // create contact record 
206                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
207                         `blocked`, `readonly`, `pending` )
208                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
209                         intval(local_user()),
210                         dbesc(datetime_convert()),
211                         dbesc($profile),
212                         dbesc($alias),
213                         dbesc($notify),
214                         dbesc($poll),
215                         dbesc($vcard['fn']),
216                         dbesc($vcard['nick']),
217                         dbesc($vcard['photo']),
218                         dbesc($network),
219                         intval(REL_FAN),
220                         intval($priority)
221                 );
222         }
223
224         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
225                 dbesc($profile),
226                 intval(local_user())
227         );
228
229         if(! count($r)) {
230                 notice( t('Unable to retrieve contact information.') . EOL);
231                 goaway($_SESSION['return_url']);
232                 // NOTREACHED
233         }
234
235         $contact = $r[0];
236         $contact_id  = $r[0]['id'];
237
238         require_once("Photo.php");
239
240         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
241
242         $r = q("UPDATE `contact` SET `photo` = '%s', 
243                         `thumb` = '%s',
244                         `micro` = '%s', 
245                         `name-date` = '%s', 
246                         `uri-date` = '%s', 
247                         `avatar-date` = '%s'
248                         WHERE `id` = %d LIMIT 1
249                 ",
250                         dbesc($photos[0]),
251                         dbesc($photos[1]),
252                         dbesc($photos[2]),
253                         dbesc(datetime_convert()),
254                         dbesc(datetime_convert()),
255                         dbesc(datetime_convert()),
256                         intval($contact_id)
257                 );                      
258
259
260         // pull feed and consume it, which should subscribe to the hub.
261
262         proc_run('php',"include/poller.php","$contact_id");
263
264         // create a follow slap
265
266         $tpl = load_view_file('view/follow_slap.tpl');
267         $slap = replace_macros($tpl, array(
268                 '$name' => $a->user['username'],
269                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
270                 '$photo' => $a->contact['photo'],
271                 '$thumb' => $a->contact['thumb'],
272                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
273                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
274                 '$title' => '',
275                 '$type' => 'text',
276                 '$content' => t('following'),
277                 '$nick' => $a->user['nickname'],
278                 '$verb' => ACTIVITY_FOLLOW,
279                 '$ostat_follow' => ''
280         ));
281
282         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
283                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
284                         intval(local_user())
285         );
286
287
288         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
289                 require_once('include/salmon.php');
290                 slapper($r[0],$contact['notify'],$slap);
291         }
292
293         goaway($_SESSION['return_url']);
294         // NOTREACHED
295 }