]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
Merge remote branch 'upstream/master'
[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
113         if(count($r)) {
114                 // update contact
115                 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
116                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
117                                 intval(CONTACT_IS_FRIEND),
118                                 intval($r[0]['id']),
119                                 intval($uid)
120                         );
121                 }
122         }
123         else {
124
125                 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
126                 if($ret['network'] === NETWORK_DIASPORA)
127                         $new_relation = CONTACT_IS_FOLLOWER;
128
129                 // create contact record 
130                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
131                         `writable`, `hidden`, `blocked`, `readonly`, `pending` )
132                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
133                         intval($uid),
134                         dbesc(datetime_convert()),
135                         dbesc($ret['url']),
136                         dbesc(normalise_link($ret['url'])),
137                         dbesc($ret['addr']),
138                         dbesc($ret['alias']),
139                         dbesc($ret['batch']),
140                         dbesc($ret['notify']),
141                         dbesc($ret['poll']),
142                         dbesc($ret['poco']),
143                         dbesc($ret['name']),
144                         dbesc($ret['nick']),
145                         dbesc($ret['photo']),
146                         dbesc($ret['network']),
147                         dbesc($ret['pubkey']),
148                         intval($new_relation),
149                         intval($ret['priority']),
150                         intval($writeable),
151                         intval($hidden)
152                 );
153         }
154
155         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
156                 dbesc($ret['url']),
157                 intval($uid)
158         );
159
160         if(! count($r)) {
161                 notice( t('Unable to retrieve contact information.') . EOL);
162                 goaway($return_url);
163                 // NOTREACHED
164         }
165
166         $contact = $r[0];
167         $contact_id  = $r[0]['id'];
168
169
170         $g = q("select def_gid from user where uid = %d limit 1",
171                 intval($uid)
172         );
173         if($g && intval($g[0]['def_gid'])) {
174                 require_once('include/group.php');
175                 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
176         }
177
178         require_once("Photo.php");
179
180         $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
181
182         $r = q("UPDATE `contact` SET `photo` = '%s', 
183                         `thumb` = '%s',
184                         `micro` = '%s', 
185                         `name-date` = '%s', 
186                         `uri-date` = '%s', 
187                         `avatar-date` = '%s'
188                         WHERE `id` = %d LIMIT 1
189                 ",
190                         dbesc($photos[0]),
191                         dbesc($photos[1]),
192                         dbesc($photos[2]),
193                         dbesc(datetime_convert()),
194                         dbesc(datetime_convert()),
195                         dbesc(datetime_convert()),
196                         intval($contact_id)
197                 );                      
198
199
200         // pull feed and consume it, which should subscribe to the hub.
201
202         proc_run('php',"include/poller.php","$contact_id");
203
204         // create a follow slap
205
206         $tpl = get_markup_template('follow_slap.tpl');
207         $slap = replace_macros($tpl, array(
208                 '$name' => $a->user['username'],
209                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
210                 '$photo' => $a->contact['photo'],
211                 '$thumb' => $a->contact['thumb'],
212                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
213                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
214                 '$title' => '',
215                 '$type' => 'text',
216                 '$content' => t('following'),
217                 '$nick' => $a->user['nickname'],
218                 '$verb' => ACTIVITY_FOLLOW,
219                 '$ostat_follow' => ''
220         ));
221
222         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
223                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
224                         intval($uid)
225         );
226
227         if(count($r)) {
228                 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
229                         require_once('include/salmon.php');
230                         slapper($r[0],$contact['notify'],$slap);
231                 }
232                 if($contact['network'] == NETWORK_DIASPORA) {
233                         require_once('include/diaspora.php');
234                         $ret = diaspora_share($a->user,$contact);
235                         logger('mod_follow: diaspora_share returns: ' . $ret);
236                 }
237         }
238
239         if(strstr($return_url,'contacts'))
240                 goaway($a->get_baseurl() . '/contacts/' . $contact_id);
241
242         goaway($return_url);
243         // NOTREACHED
244 }