]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
change activitystreams unfollow to stop-follow but also send out OStatus unfollow
[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                 // Google doesn't use absolute url in profile photos
55
56                 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
57                         $h = parse_url($hcard);
58                         if($h)
59                                 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
60                 }
61         }
62
63         if(! $profile)
64                 $profile = $url;
65
66         // do we have enough information?
67
68         if(! x($vcard,'fn'))
69                 if(x($vcard,'nick'))
70                         $vcard['fn'] = $vcard['nick'];
71
72         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
73         
74         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
75                 notice( t('The profile address specified does not provide adequate information.') . EOL);
76                 goaway($_SESSION['return_url']);
77         }
78
79         if(! $notify) {
80                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
81         }
82
83         if(! x($vcard,'photo'))
84                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
85
86         // check if we already have a contact
87         // the poll url is more reliable than the profile url, as we may have
88         // indirect links or webfinger links
89
90         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
91                 intval(local_user()),
92                 dbesc($poll)
93         );                      
94
95         if(count($r)) {
96                 // update contact
97                 if($r[0]['rel'] == REL_VIP) {
98                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
99                                 intval(REL_BUD),
100                                 intval($r[0]['id']),
101                                 intval(local_user())
102                         );
103                 }
104         }
105         else {
106                 // create contact record 
107                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, 
108                         `blocked`, `readonly`, `pending` )
109                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
110                         intval(local_user()),
111                         dbesc(datetime_convert()),
112                         dbesc($profile),
113                         dbesc($notify),
114                         dbesc($poll),
115                         dbesc($vcard['fn']),
116                         dbesc($vcard['nick']),
117                         dbesc($vcard['photo']),
118                         dbesc('stat'),
119                         intval(REL_FAN)
120                 );
121         }
122
123         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
124                 dbesc($profile),
125                 intval(local_user())
126         );
127
128         if(! count($r)) {
129                 notice( t('Unable to retrieve contact information.') . EOL);
130                 goaway($_SESSION['return_url']);
131                 // NOTREACHED
132         }
133
134         $contact = $r[0];
135         $contact_id  = $r[0]['id'];
136
137         require_once("Photo.php");
138
139         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
140
141         $r = q("UPDATE `contact` SET `photo` = '%s', 
142                         `thumb` = '%s',
143                         `micro` = '%s', 
144                         `name-date` = '%s', 
145                         `uri-date` = '%s', 
146                         `avatar-date` = '%s'
147                         WHERE `id` = %d LIMIT 1
148                 ",
149                         dbesc($photos[0]),
150                         dbesc($photos[1]),
151                         dbesc($photos[2]),
152                         dbesc(datetime_convert()),
153                         dbesc(datetime_convert()),
154                         dbesc(datetime_convert()),
155                         intval($contact_id)
156                 );                      
157
158
159         // pull feed and consume it, which should subscribe to the hub.
160
161
162         // create a follow slap
163
164         $tpl = load_view_file('view/follow_slap.tpl');
165         $slap = replace_macros($tpl, array(
166                 '$name' => $a->user['username'],
167                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
168                 '$photo' => $a->contact['photo'],
169                 '$thumb' => $a->contact['thumb'],
170                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
171                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
172                 '$title' => '',
173                 '$type' => 'text',
174                 '$content' => t('following'),
175                 '$nick' => $a->user['nickname'],
176                 '$verb' => ACTIVITY_FOLLOW,
177                 '$ostat_follow' => ''
178         ));
179
180         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
181                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
182                         intval(local_user())
183         );
184
185
186         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
187                 require_once('include/salmon.php');
188                 slapper($r[0],$contact['notify'],$slap);
189         }
190
191         goaway($_SESSION['return_url']);
192         // NOTREACHED
193 }