]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
"profile locator/address" is now called "Identity Address" to reduce confusion
[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                 logger('mod_follow: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
97         }
98
99         if(! $profile) {
100                 if($diaspora)
101                         $profile = $hcard;
102                 else
103                         $profile = $url;
104         }
105
106         if(! x($vcard,'fn'))
107                 if(x($vcard,'nick'))
108                         $vcard['fn'] = $vcard['nick'];
109
110         if((! isset($vcard)) && (! $poll)) {
111
112                 $ret = scrape_feed($url);
113                 logger('mod_follow: scrape_feed returns: ' . print_r($ret,true), LOGGER_DATA);
114                 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
115                         $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
116                         $vcard = array();
117                                 if(x($ret,'photo'))
118                                         $vcard['photo'] = $ret['photo'];
119                         require_once('simplepie/simplepie.inc');
120                     $feed = new SimplePie();
121                         $xml = fetch_url($poll);
122
123                 $feed->set_raw_data($xml);
124
125                     $feed->init();
126
127                         if(! x($vcard,'photo'))
128                                 $vcard['photo'] = $feed->get_image_url();
129                         $author = $feed->get_author();
130                         if($author) {                   
131                                 $vcard['fn'] = unxmlify(trim($author->get_name()));
132                                 if(! $vcard['fn'])
133                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
134                                 if(strpos($vcard['fn'],'@') !== false)
135                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
136                                 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
137                                 if(strpos($vcard['nick'],' '))
138                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
139                                 $email = unxmlify($author->get_email());
140                         }
141                         else {
142                                 $item = $feed->get_item(0);
143                                 if($item) {
144                                         $author = $item->get_author();
145                                         if($author) {                   
146                                                 $vcard['fn'] = trim(unxmlify($author->get_name()));
147                                                 if(! $vcard['fn'])
148                                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
149                                                 if(strpos($vcard['fn'],'@') !== false)
150                                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
151                                                 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
152                                                 if(strpos($vcard['nick'],' '))
153                                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
154                                                 $email = unxmlify($author->get_email());
155                                         }
156                                         if(! $vcard['photo']) {
157                                                 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
158                                                 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
159                                                         $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
160                                         }
161                                 }
162                         }
163                         if((! $vcard['photo']) && strlen($email))
164                                 $vcard['photo'] = gravatar_img($email);
165                         if($poll === $profile)
166                                 $lnk = $feed->get_permalink();
167                         if(isset($lnk) && strlen($lnk))
168                                 $profile = $lnk;        
169                         if(! (x($vcard,'fn')))
170                                 $vcard['fn'] = notags($feed->get_title());
171                         if(! (x($vcard,'fn')))
172                                 $vcard['fn'] = notags($feed->get_description());
173                         $network = 'feed';
174                         $priority = 2;
175                 }
176         }
177
178         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
179
180         $vcard['fn'] = notags($vcard['fn']);
181         $vcard['nick'] = notags($vcard['nick']);
182
183         // do we have enough information?
184         
185         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
186                 notice( t('The profile address specified does not provide adequate information.') . EOL);
187                 goaway($_SESSION['return_url']);
188         }
189
190
191         if(! $notify) {
192                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
193         }
194
195         if(! x($vcard,'photo'))
196                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
197
198
199         $writeable = ((($network === 'stat') && ($notify)) ? 1 : 0);
200
201         // check if we already have a contact
202         // the poll url is more reliable than the profile url, as we may have
203         // indirect links or webfinger links
204
205         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
206                 intval(local_user()),
207                 dbesc($poll)
208         );                      
209
210         if(count($r)) {
211                 // update contact
212                 if($r[0]['rel'] == REL_VIP) {
213                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
214                                 intval(REL_BUD),
215                                 intval($r[0]['id']),
216                                 intval(local_user())
217                         );
218                 }
219         }
220         else {
221                 // create contact record 
222                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
223                         `writable`, `blocked`, `readonly`, `pending` )
224                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
225                         intval(local_user()),
226                         dbesc(datetime_convert()),
227                         dbesc($profile),
228                         dbesc($alias),
229                         dbesc($notify),
230                         dbesc($poll),
231                         dbesc($vcard['fn']),
232                         dbesc($vcard['nick']),
233                         dbesc($vcard['photo']),
234                         dbesc($network),
235                         intval(REL_FAN),
236                         intval($priority),
237                         intval($writable)
238                 );
239         }
240
241         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
242                 dbesc($profile),
243                 intval(local_user())
244         );
245
246         if(! count($r)) {
247                 notice( t('Unable to retrieve contact information.') . EOL);
248                 goaway($_SESSION['return_url']);
249                 // NOTREACHED
250         }
251
252         $contact = $r[0];
253         $contact_id  = $r[0]['id'];
254
255         require_once("Photo.php");
256
257         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
258
259         $r = q("UPDATE `contact` SET `photo` = '%s', 
260                         `thumb` = '%s',
261                         `micro` = '%s', 
262                         `name-date` = '%s', 
263                         `uri-date` = '%s', 
264                         `avatar-date` = '%s'
265                         WHERE `id` = %d LIMIT 1
266                 ",
267                         dbesc($photos[0]),
268                         dbesc($photos[1]),
269                         dbesc($photos[2]),
270                         dbesc(datetime_convert()),
271                         dbesc(datetime_convert()),
272                         dbesc(datetime_convert()),
273                         intval($contact_id)
274                 );                      
275
276
277         // pull feed and consume it, which should subscribe to the hub.
278
279         proc_run('php',"include/poller.php","$contact_id");
280
281         // create a follow slap
282
283         $tpl = load_view_file('view/follow_slap.tpl');
284         $slap = replace_macros($tpl, array(
285                 '$name' => $a->user['username'],
286                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
287                 '$photo' => $a->contact['photo'],
288                 '$thumb' => $a->contact['thumb'],
289                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
290                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
291                 '$title' => '',
292                 '$type' => 'text',
293                 '$content' => t('following'),
294                 '$nick' => $a->user['nickname'],
295                 '$verb' => ACTIVITY_FOLLOW,
296                 '$ostat_follow' => ''
297         ));
298
299         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
300                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
301                         intval(local_user())
302         );
303
304
305         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
306                 require_once('include/salmon.php');
307                 slapper($r[0],$contact['notify'],$slap);
308         }
309
310         goaway($_SESSION['return_url']);
311         // NOTREACHED
312 }