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