]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
linkify - need at least one character after ://
[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']) {
78                         $poll = $ret['feed_atom'];
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
119         // do we have enough information?
120         
121         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
122                 notice( t('The profile address specified does not provide adequate information.') . EOL);
123                 goaway($_SESSION['return_url']);
124         }
125
126         if(! $notify) {
127                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
128         }
129
130         if(! x($vcard,'photo'))
131                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
132
133         // check if we already have a contact
134         // the poll url is more reliable than the profile url, as we may have
135         // indirect links or webfinger links
136
137         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
138                 intval(local_user()),
139                 dbesc($poll)
140         );                      
141
142         if(count($r)) {
143                 // update contact
144                 if($r[0]['rel'] == REL_VIP) {
145                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
146                                 intval(REL_BUD),
147                                 intval($r[0]['id']),
148                                 intval(local_user())
149                         );
150                 }
151         }
152         else {
153                 // create contact record 
154                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, 
155                         `blocked`, `readonly`, `pending` )
156                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
157                         intval(local_user()),
158                         dbesc(datetime_convert()),
159                         dbesc($profile),
160                         dbesc($notify),
161                         dbesc($poll),
162                         dbesc($vcard['fn']),
163                         dbesc($vcard['nick']),
164                         dbesc($vcard['photo']),
165                         dbesc($network),
166                         intval(REL_FAN)
167                 );
168         }
169
170         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
171                 dbesc($profile),
172                 intval(local_user())
173         );
174
175         if(! count($r)) {
176                 notice( t('Unable to retrieve contact information.') . EOL);
177                 goaway($_SESSION['return_url']);
178                 // NOTREACHED
179         }
180
181         $contact = $r[0];
182         $contact_id  = $r[0]['id'];
183
184         require_once("Photo.php");
185
186         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
187
188         $r = q("UPDATE `contact` SET `photo` = '%s', 
189                         `thumb` = '%s',
190                         `micro` = '%s', 
191                         `name-date` = '%s', 
192                         `uri-date` = '%s', 
193                         `avatar-date` = '%s'
194                         WHERE `id` = %d LIMIT 1
195                 ",
196                         dbesc($photos[0]),
197                         dbesc($photos[1]),
198                         dbesc($photos[2]),
199                         dbesc(datetime_convert()),
200                         dbesc(datetime_convert()),
201                         dbesc(datetime_convert()),
202                         intval($contact_id)
203                 );                      
204
205
206         // pull feed and consume it, which should subscribe to the hub.
207
208         $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
209         proc_close(proc_open("\"$php_path\" \"include/poller.php\" \"$contact_id\" &", array(), $foo));
210
211
212         // create a follow slap
213
214         $tpl = load_view_file('view/follow_slap.tpl');
215         $slap = replace_macros($tpl, array(
216                 '$name' => $a->user['username'],
217                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
218                 '$photo' => $a->contact['photo'],
219                 '$thumb' => $a->contact['thumb'],
220                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
221                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
222                 '$title' => '',
223                 '$type' => 'text',
224                 '$content' => t('following'),
225                 '$nick' => $a->user['nickname'],
226                 '$verb' => ACTIVITY_FOLLOW,
227                 '$ostat_follow' => ''
228         ));
229
230         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
231                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
232                         intval(local_user())
233         );
234
235
236         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
237                 require_once('include/salmon.php');
238                 slapper($r[0],$contact['notify'],$slap);
239         }
240
241         goaway($_SESSION['return_url']);
242         // NOTREACHED
243 }