]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
Merge branch 'master' of https://github.com/friendica/friendica
[friendica.git] / mod / follow.php
1 <?php
2
3 require_once('Scrape.php');
4
5 function follow_init(&$a) {
6
7         if(! local_user()) {
8                 notice( t('Permission denied.') . EOL);
9                 goaway($_SESSION['return_url']);
10                 // NOTREACHED
11         }
12
13         $url = $orig_url = notags(trim($_REQUEST['url']));
14
15         // remove ajax junk, e.g. Twitter
16
17         $url = str_replace('/#!/','/',$url);
18
19         if(! allowed_url($url)) {
20                 notice( t('Disallowed profile URL.') . EOL);
21                 goaway($_SESSION['return_url']);
22                 // NOTREACHED
23         }
24
25
26         if(! $url) {
27                 notice( t('Connect URL missing.') . EOL);
28                 goaway($_SESSION['return_url']);
29                 // NOTREACHED
30         }
31
32
33         $ret = probe_url($url);
34
35         if($ret['network'] === NETWORK_DFRN) {
36                 if(strlen($a->path))
37                         $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
38                 else
39                         $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
40  
41                 goaway($ret['request'] . "&addr=$myaddr");
42                 
43                 // NOTREACHED
44         }
45         else {
46                 if(get_config('system','dfrn_only')) {
47                         notice( t('This site is not configured to allow communications with other networks.') . EOL);
48                         notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
49                         goaway($_SESSION['return_url']);
50                 }
51         }
52         
53         // This just confuses things, remove it
54         if($ret['network'] === NETWORK_DIASPORA)
55                 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
56
57
58         // do we have enough information?
59         
60         if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
61                 notice( t('The profile address specified does not provide adequate information.') . EOL);
62                 if(! x($ret,'poll'))
63                         notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
64                 if(! x($ret,'name'))
65                         notice( t('An author or name was not found.') . EOL);
66                 if(! x($ret,'url'))
67                         notice( t('No browser URL could be matched to this address.') . EOL);
68                 if(strpos($url,'@') !== false)
69                         notice('Unable to match @-style Identity Address with a known protocol or email contact');
70                 goaway($_SESSION['return_url']);
71         }
72
73         if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
74                 notice( t('The profile address specified belongs to a network which has been disabled on this site.') . EOL);
75                 $ret['notify'] = '';
76         }
77
78         if(! $ret['notify']) {
79                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
80         }
81
82         $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
83         $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
84
85         if($ret['network'] === NETWORK_MAIL) {
86                 $writeable = 1;
87                 
88         }
89         if($ret['network'] === NETWORK_DIASPORA)
90                 $writeable = 1;
91
92         // check if we already have a contact
93         // the poll url is more reliable than the profile url, as we may have
94         // indirect links or webfinger links
95
96         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
97                 intval(local_user()),
98                 dbesc($ret['poll'])
99         );                      
100
101         if(count($r)) {
102                 // update contact
103                 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
104                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
105                                 intval(CONTACT_IS_FRIEND),
106                                 intval($r[0]['id']),
107                                 intval(local_user())
108                         );
109                 }
110         }
111         else {
112
113                 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
114                 if($ret['network'] === NETWORK_DIASPORA)
115                         $new_relation = CONTACT_IS_FOLLOWER;
116
117                 // create contact record 
118                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
119                         `writable`, `hidden`, `blocked`, `readonly`, `pending` )
120                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
121                         intval(local_user()),
122                         dbesc(datetime_convert()),
123                         dbesc($ret['url']),
124                         dbesc(normalise_link($ret['url'])),
125                         dbesc($ret['addr']),
126                         dbesc($ret['alias']),
127                         dbesc($ret['batch']),
128                         dbesc($ret['notify']),
129                         dbesc($ret['poll']),
130                         dbesc($ret['poco']),
131                         dbesc($ret['name']),
132                         dbesc($ret['nick']),
133                         dbesc($ret['photo']),
134                         dbesc($ret['network']),
135                         dbesc($ret['pubkey']),
136                         intval($new_relation),
137                         intval($ret['priority']),
138                         intval($writeable),
139                         intval($hidden)
140                 );
141         }
142
143         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
144                 dbesc($ret['url']),
145                 intval(local_user())
146         );
147
148         if(! count($r)) {
149                 notice( t('Unable to retrieve contact information.') . EOL);
150                 goaway($_SESSION['return_url']);
151                 // NOTREACHED
152         }
153
154         $contact = $r[0];
155         $contact_id  = $r[0]['id'];
156
157         require_once("Photo.php");
158
159         $photos = import_profile_photo($ret['photo'],local_user(),$contact_id);
160
161         $r = q("UPDATE `contact` SET `photo` = '%s', 
162                         `thumb` = '%s',
163                         `micro` = '%s', 
164                         `name-date` = '%s', 
165                         `uri-date` = '%s', 
166                         `avatar-date` = '%s'
167                         WHERE `id` = %d LIMIT 1
168                 ",
169                         dbesc($photos[0]),
170                         dbesc($photos[1]),
171                         dbesc($photos[2]),
172                         dbesc(datetime_convert()),
173                         dbesc(datetime_convert()),
174                         dbesc(datetime_convert()),
175                         intval($contact_id)
176                 );                      
177
178
179         // pull feed and consume it, which should subscribe to the hub.
180
181         proc_run('php',"include/poller.php","$contact_id");
182
183         // create a follow slap
184
185         $tpl = get_markup_template('follow_slap.tpl');
186         $slap = replace_macros($tpl, array(
187                 '$name' => $a->user['username'],
188                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
189                 '$photo' => $a->contact['photo'],
190                 '$thumb' => $a->contact['thumb'],
191                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
192                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
193                 '$title' => '',
194                 '$type' => 'text',
195                 '$content' => t('following'),
196                 '$nick' => $a->user['nickname'],
197                 '$verb' => ACTIVITY_FOLLOW,
198                 '$ostat_follow' => ''
199         ));
200
201         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
202                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
203                         intval(local_user())
204         );
205
206         if(count($r)) {
207                 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
208                         require_once('include/salmon.php');
209                         slapper($r[0],$contact['notify'],$slap);
210                 }
211                 if($contact['network'] == NETWORK_DIASPORA) {
212                         require_once('include/diaspora.php');
213                         $ret = diaspora_share($a->user,$contact);
214                         logger('mod_follow: diaspora_share returns: ' . $ret);
215                 }
216         }
217
218         if(strstr($_SESSION['return_url'],'contacts'))
219                 goaway($a->get_baseurl() . '/contacts/' . $contact_id);
220
221         goaway($_SESSION['return_url']);
222         // NOTREACHED
223 }