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