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