]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
bad2e74f9cef47adedbdd777c459d6087ac72c6c
[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 = notags(trim($_POST['url']));
14
15         if($url) {
16                 $links = lrdd($url);
17                 if(count($links)) {
18                         foreach($links as $link) {
19                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
20                                         $dfrn = $link['@attributes']['href'];
21                                 if($link['@attributes']['rel'] === 'salmon')
22                                         $notify = $link['@attributes']['href'];
23                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
24                                         $poll = $link['@attributes']['href'];
25                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
26                                         $hcard = $link['@attributes']['href'];
27                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
28                                         $profile = $link['@attributes']['href'];
29
30                         }
31                 }
32         }       
33
34         // If we find a DFRN site, send our subscriber to the other person's
35         // dfrn_request page and all the other details will get sorted.
36
37         if(strlen($dfrn)) {
38                 $ret = scrape_dfrn($dfrn);
39                 if(is_array($ret) && x($ret,'dfrn-request')) {
40                         if(strlen($a->path))
41                                 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
42                         else
43                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
44  
45                         goaway($ret['dfrn-request'] . "&addr=$myaddr");
46                 
47                         // NOTREACHED
48                 }
49         }
50
51         $network = 'stat';
52
53         if($hcard) {
54                 $vcard = scrape_vcard($hcard);
55
56                 // Google doesn't use absolute url in profile photos
57
58                 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
59                         $h = parse_url($hcard);
60                         if($h)
61                                 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
62                 }
63         }
64
65         if(! $profile)
66                 $profile = $url;
67
68
69         if(! x($vcard,'fn'))
70                 if(x($vcard,'nick'))
71                         $vcard['fn'] = $vcard['nick'];
72
73         if((! isset($vcard)) && (! $poll)) {
74
75                 $ret = scrape_feed($url);
76
77                 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
78                         $poll = ((x($ret,'feed_atom')) ? $ret['feed_atom'] : $ret['feed_rss']);
79                         $vcard = array();
80                         require_once('simplepie/simplepie.inc');
81                     $feed = new SimplePie();
82                         $xml = fetch_url($poll);
83
84                 $feed->set_raw_data($xml);
85
86                     $feed->init();
87
88                         $vcard['photo'] = $feed->get_image_url();
89                         $author = $feed->get_author();
90                         if($author) {                   
91                                 $vcard['fn'] = trim($author->get_name());
92                                 $vcard['nick'] = strtolower($vcard['fn']);
93                                 if(strpos($vcard['nick'],' '))
94                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
95                                 $email = $author->get_email();
96                         }
97                         else {
98                                 $item = $feed->get_item(0);
99                                 if($item) {
100                                         $author = $item->get_author();
101                                         if($author) {                   
102                                                 $vcard['fn'] = trim($author->get_name());
103                                                 $vcard['nick'] = strtolower($vcard['fn']);
104                                                 if(strpos($vcard['nick'],' '))
105                                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
106                                                 $email = $author->get_email();
107                                         }
108                                 }
109                         }
110                         if((! $vcard['photo']) && strlen($email))
111                                 $vcard['photo'] = gravatar_img($email);
112                         $network = 'feed';
113         }
114         }
115
116         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
117
118         // do we have enough information?
119         
120         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
121                 notice( t('The profile address specified does not provide adequate information.') . EOL);
122                 goaway($_SESSION['return_url']);
123         }
124
125         if(! $notify) {
126                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
127         }
128
129         if(! x($vcard,'photo'))
130                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
131
132         // check if we already have a contact
133         // the poll url is more reliable than the profile url, as we may have
134         // indirect links or webfinger links
135
136         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
137                 intval(local_user()),
138                 dbesc($poll)
139         );                      
140
141         if(count($r)) {
142                 // update contact
143                 if($r[0]['rel'] == REL_VIP) {
144                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
145                                 intval(REL_BUD),
146                                 intval($r[0]['id']),
147                                 intval(local_user())
148                         );
149                 }
150         }
151         else {
152                 // create contact record 
153                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, 
154                         `blocked`, `readonly`, `pending` )
155                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
156                         intval(local_user()),
157                         dbesc(datetime_convert()),
158                         dbesc($profile),
159                         dbesc($notify),
160                         dbesc($poll),
161                         dbesc($vcard['fn']),
162                         dbesc($vcard['nick']),
163                         dbesc($vcard['photo']),
164                         dbesc($network),
165                         intval(REL_FAN)
166                 );
167         }
168
169         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
170                 dbesc($profile),
171                 intval(local_user())
172         );
173
174         if(! count($r)) {
175                 notice( t('Unable to retrieve contact information.') . EOL);
176                 goaway($_SESSION['return_url']);
177                 // NOTREACHED
178         }
179
180         $contact = $r[0];
181         $contact_id  = $r[0]['id'];
182
183         require_once("Photo.php");
184
185         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
186
187         $r = q("UPDATE `contact` SET `photo` = '%s', 
188                         `thumb` = '%s',
189                         `micro` = '%s', 
190                         `name-date` = '%s', 
191                         `uri-date` = '%s', 
192                         `avatar-date` = '%s'
193                         WHERE `id` = %d LIMIT 1
194                 ",
195                         dbesc($photos[0]),
196                         dbesc($photos[1]),
197                         dbesc($photos[2]),
198                         dbesc(datetime_convert()),
199                         dbesc(datetime_convert()),
200                         dbesc(datetime_convert()),
201                         intval($contact_id)
202                 );                      
203
204
205         // pull feed and consume it, which should subscribe to the hub.
206
207         $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
208         proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
209
210
211         // create a follow slap
212
213         $tpl = load_view_file('view/follow_slap.tpl');
214         $slap = replace_macros($tpl, array(
215                 '$name' => $a->user['username'],
216                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
217                 '$photo' => $a->contact['photo'],
218                 '$thumb' => $a->contact['thumb'],
219                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
220                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
221                 '$title' => '',
222                 '$type' => 'text',
223                 '$content' => t('following'),
224                 '$nick' => $a->user['nickname'],
225                 '$verb' => ACTIVITY_FOLLOW,
226                 '$ostat_follow' => ''
227         ));
228
229         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
230                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
231                         intval(local_user())
232         );
233
234
235         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
236                 require_once('include/salmon.php');
237                 slapper($r[0],$contact['notify'],$slap);
238         }
239
240         goaway($_SESSION['return_url']);
241         // NOTREACHED
242 }