]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
29c5f4230686344181b6ae12f0177678e4a4cfa1
[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 = urlencode($a->get_baseurl() . '/profile/' . $a->user['nickname']);
42                         else
43                                 $myaddr = urlencode($a->user['nickname'] . '@' . $a->get_hostname());
44  
45                         goaway($ret['dfrn-request'] . "&address=$myaddr");
46                 
47                         // NOTREACHED
48                 }
49         }
50
51         if($hcard) {
52                 $vcard = scrape_vcard($hcard);
53         }
54
55         if(! $profile)
56                 $profile = $url;
57
58         // do we have enough information?
59
60         if(! x($vcard,'fn'))
61                 if(x($vcard,'nick'))
62                         $vcard['fn'] = $vcard['nick'];
63
64         if(! ((x($vcard['fn'])) && ($poll) && ($notify) && ($profile))) {
65                 notice( t('The profile address specified does not provide adequate information.') . EOL);
66                 goaway($_SESSION['return_url']);
67         } 
68
69         if(! x($vcard,'photo'))
70                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
71
72         // check if we already have a contact
73         // the poll url is more reliable than the profile url, as we may have
74         // indirect links or webfinger links
75
76         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
77                 intval(local_user()),
78                 dbesc($poll)
79         );                      
80         if(count($r)) {
81                 // update contact
82                 if($r[0]['rel'] == REL_VIP) {
83                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
84                                 intval(REL_BUD),
85                                 intval($r[0]['id']),
86                                 intval(local_user())
87                         );
88                 }
89         }
90         else {
91                 // create contact record 
92                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, 
93                         `blocked`, `readonly`, `pending` )
94                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
95                         intval(local_user()),
96                         dbesc(datetime_convert()),
97                         dbesc($profile),
98                         dbesc($notify),
99                         dbesc($poll),
100                         dbesc($vcard['fn']),
101                         dbesc($vcard['nick']),
102                         dbesc($vcard['photo']),
103                         dbesc('stat'),
104                         intval(REL_FAN)
105                 );
106         }
107         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
108                 dbesc($profile),
109                 intval(local_user())
110         );
111         if(! count($r)) {
112                 notice( t('Unable to retrieve contact information.') . EOL);
113                 goaway($_SESSION['return_url']);
114                 // NOTREACHED
115         }
116
117         $contact = $r[0];
118         $contact_id  = $r[0]['id'];
119
120         require_once("Photo.php");
121
122         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
123
124         $r = q("UPDATE `contact` SET `photo` = '%s', 
125                         `thumb` = '%s', 
126                         `name-date` = '%s', 
127                         `uri-date` = '%s', 
128                         `avatar-date` = '%s'
129                         WHERE `id` = %d LIMIT 1
130                 ",
131                         dbesc($photos[0]),
132                         dbesc($photos[1]),
133                         dbesc(datetime_convert()),
134                         dbesc(datetime_convert()),
135                         dbesc(datetime_convert()),
136                         intval($contact_id)
137                 );                      
138
139
140         // pull feed and consume it, which should subscribe to the hub.
141
142
143         // create a follow slap
144
145         $tpl = load_view_file('view/follow_slap.tpl');
146         $slap = replace_macros($tpl, array(
147                 '$name' => $a->user['username'],
148                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
149                 '$photo' => $a->contact['photo'],
150                 '$thumb' => $a->contact['thumb'],
151                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
152                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
153                 '$title' => '',
154                 '$type' => 'text',
155                 '$content' => t('following'),
156                 '$nick' => $a->user['nickname'],
157                 '$verb' => ACTIVITY_FOLLOW
158         ));
159
160         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
161                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
162                         intval(local_user())
163         );
164
165         require_once('include/salmon.php');
166         slapper($r[0],$contact,$slap);
167
168         goaway($_SESSION['return_url']);
169         // NOTREACHED
170 }